/*	
	======================================================================
	
	lib/javascript/misc.js

	======================================================================
	Copyright 2006, FQXi
	======================================================================
*/


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Trim
	//
	/////////////////////////////////////////////////////////
	
 		function Trim (str) {
 		
 			return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
 		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Goto_Anchor
	//
	/////////////////////////////////////////////////////////
	
 		function Goto_Anchor (anchorName) {
 		
			window.location.hash = anchorName;
		}
 		
 		
 
	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Get_Element_Reference
	//
	/////////////////////////////////////////////////////////
	
 		function Get_Element_Reference (fieldName) {
 		
			if (document.getElementById) { // DOM3 = IE5, NS6
		
				var myField = document.getElementById(fieldName);
			}
		
			else {
		
				if (document.layers) { // Netscape 4
				
					var myField = document.fieldName;
				}
				
				else { // IE 4
				
					var myField = document.all.fieldName;
				}
			}	
			
			return myField;
 		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Insert_At_Cursor
	//
	/////////////////////////////////////////////////////////
	
 		function Insert_At_Cursor (fieldName, fieldValue) {

		// ---------------------------------------------
		// get element to update
		// ---------------------------------------------
  		
 			var myField = Get_Element_Reference (fieldName);
 		
		// ---------------------------------------------
		// IE
		// ---------------------------------------------
  		
			if (document.selection) {

				myField.focus ();

				sel = document.selection.createRange ();

				sel.text = fieldValue;
				
				sel.moveStart ('character', -fieldValue.length);

				sel.select ();

				myField.focus ();
			}

		// ---------------------------------------------
		// Mozilla / Netscape
		// ---------------------------------------------
 
			else if (myField.selectionStart || myField.selectionStart == '0') {
			
				var startPos = myField.selectionStart;
				
				var endPos = myField.selectionEnd;

				myField.value = myField.value.substring (0, startPos)
					+ fieldValue
					+ myField.value.substring (endPos, myField.value.length);

				myField.selectionStart = startPos;

				myField.selectionEnd = startPos + fieldValue.length;

				myField.focus ();
			} 
			
			else {

				myField.value = myField.value + fieldValue;
			}
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  CloseMessageDiv
	//
	/////////////////////////////////////////////////////////
	
 
		function CloseMessageDiv () {
		
			HideDiv ('messageContainer');
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  ToggleDiv
	//
	/////////////////////////////////////////////////////////
	
		function ToggleDiv (id) {
		
			if (GetDivVisibility (id) == 'none') {
			
				ShowDiv (id);
			}
			
			else {
			
				HideDiv (id);
			}
		}
		
	
	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  GetDivVisibility
	//
	/////////////////////////////////////////////////////////
	
		function GetDivVisibility (id) {
		
			if (document.getElementById) { // DOM3 = IE5, NS6
		
				return (document.getElementById(id).style.display);
			}
		
			else {
		
				if (document.layers) { // Netscape 4
				
					return (document.id.display);
				}
				
				else { // IE 4
				
					return (document.all.id.style.display);
				}
			}		
		}
		
	
	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  HideDiv
	//
	/////////////////////////////////////////////////////////
	
		function HideDiv (id) {
		
			if (document.getElementById) { // DOM3 = IE5, NS6
		
				document.getElementById(id).style.display = 'none';
			}
		
			else {
		
				if (document.layers) { // Netscape 4
				
					document.id.display = 'none';
				}
				
				else { // IE 4
				
					document.all.id.style.display = 'none';
				}
			}
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  ShowDiv
	//
	/////////////////////////////////////////////////////////
	
		function ShowDiv (id) {

			if (document.getElementById) { // DOM3 = IE5, NS6
		
				document.getElementById(id).style.display = 'block';
			}
		
			else {
		
				if (document.layers) { // Netscape 4
		
					document.id.display = 'block';
				}
		
				else { // IE 4
		
					document.all.id.style.display = 'block';
				}
			}
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  IgnoreKeys
	//
	/////////////////////////////////////////////////////////
	
		function IgnoreKeys (event, restrictions) {
		
			var charCode = (event.charCode) ? 
							 event.charCode :
								((event.which) ? 
								   event.which : 
								 event.keyCode);
								 
			// allow arrow keys
			
			if (charCode >= 37 && charCode <= 40) {
				
				return true;
			}
			
			// allow backspace
			
			if (charCode == 8) {
				
				return true;
			}
			
			// allow tab key
			
			if (charCode == 9) {
				
				return true;
			}
			
			// return false if return/enter key

			if (charCode == 13 || charCode == 3 || charCode == 0) {
				
				return false;
			}

			// handle digits restriction
			
			if (restrictions == 'digits') {

				if (charCode < 48 || charCode > 57) {
				
					alert ("Please enter digits only in this field");
				
					return false;
				}
			}
			
			// handle phone number restriction
			
			if (restrictions == 'phoneNumber') {

				if ((charCode != 45) && (charCode < 48 || charCode > 57)) {
				
					alert ("Please enter digits or a hyphen (-) only in this field");
				
					return false;
				}
			}

			return true;
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  UpdateWordsRemaining
	//
	/////////////////////////////////////////////////////////
 
		function UpdateWordsRemaining (fieldName, maxWords) {

			form = document.theForm;
		
			wordArray = form[fieldName].value.split(" ");

			typedWords = wordArray.length;

			remainingWords = maxWords - typedWords;
			
			if (remainingWords < 0) {
			
				wordArray.length = maxWords;
				
				newFieldText = wordArray.join(" ");
				
				form[fieldName].value = newFieldText;
			
				remainingWords = 0;
				
				alertText = "The number of words in the " + fieldName + " field has exceeded " + maxWords + " words, and has been truncated to this length.";
				
				alert (alertText);
			}
			
			document.getElementById('wordCount_' + fieldName).innerHTML = remainingWords + " words remaining";
		
			return true;
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  UpdateCharactersRemaining
	//
	/////////////////////////////////////////////////////////
	
		function UpdateCharactersRemaining (fieldName, maxCharacters) {
		
			form = document.theForm;
		
			typedCharacters = form[fieldName].value.length;

			remainingCharacters = maxCharacters - typedCharacters;
			
			if (remainingCharacters < 0) {
			
				form[fieldName].value = form[fieldName].value.substring (0, maxCharacters);
				
				remainingCharacters = 0;
				
				alertText = "The number of characters in the " + fieldName + " field has exceeded " + maxCharacters + ";  the field has been truncated to this length";
				
				alert (alertText);
			}
			
			document.getElementById('charCount_' + fieldName).innerHTML = remainingCharacters + " characters remaining";
		
			return true;
		}
