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

	======================================================================
*/


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Send_E_mail
	//
	/////////////////////////////////////////////////////////
	
 		function Send_E_Mail (recipient, ignoreThis, domain, subject) {
 		
			recipient = recipient.replace("qqq", "");

			domain = domain.replace("qqq", "");
			
			if (subject == '') { subject = "Web Site Inquiry"; }
			
			daString = "mail";
			daString += "to:";
			daString += recipient;
			daString += "@";
			daString += domain;
			daString += "?subject=";
			daString += subject;

			location.href = daString;
 		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Change_Class
	//
	/////////////////////////////////////////////////////////
	
		function Change_Class (id, newClass) {
		
			if (document.getElementById) { // DOM3 = IE5, NS6
		
				document.getElementById(id).className = newClass;
			}
		
			else {
		
				if (document.layers) { // Netscape 4
				
					document.id.className = newClass;
				}
				
				else { // IE 4
				
					document.all.id.className = newClass;
				}
			}
		}


	/////////////////////////////////////////////////////////
	//
	//  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:  Fix_Invalid_Index
	//
	/////////////////////////////////////////////////////////
	
		// for a select menu, if the invalid index is selected, the menu is reset to the top element
	
 		function Fix_Invalid_Index (menu, invalidIndex) {
 		
 			if (menu.selectedIndex == invalidIndex) {
 			
 				menu.selectedIndex = 0;
 			}
 		}
 		
 
	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Hide_Div
	//
	/////////////////////////////////////////////////////////
	
		function Hide_Div (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:  Show_Div
	//
	/////////////////////////////////////////////////////////
	
		function Show_Div (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:  Toggle_Div
	//
	/////////////////////////////////////////////////////////
	
		function Toggle_Div (id) {
		
			if (Get_Div_Visibility (id) == 'none') {
			
				Show_Div (id);
			}
			
			else {
			
				Hide_Div (id);
			}
		}
		
	
	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Close_Message_Div
	//
	/////////////////////////////////////////////////////////
	
		function Close_Message_Div () {
		
			Hide_Div ('messageContainer');
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Get_Div_Visibility
	//
	/////////////////////////////////////////////////////////
	
		function Get_Div_Visibility (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:  In_Array
	//
	/////////////////////////////////////////////////////////
	
 		function In_Array (element, array) {
 		
			var i;
			
			for (i = 0; i < array.length; i++) {

				if (array[i] == element) {

					return true;
				}
			}
			
			return false;
 		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Reset_Radio_Buttons
	//
	/////////////////////////////////////////////////////////
	
		function Reset_Radio_Buttons (element) {
 
			for (var i = 0; i < element.length; i++) {
			
				element[i].checked = false;
			} 		
		}
		
		
	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Get_Radio_Button_Value
	//
	/////////////////////////////////////////////////////////
	
  		function Get_Radio_Button_Value (element) {
  		
			for (j = 0; j < element.length; j ++) {
			
				if (element[j].checked) {
				
					return (element[j].value);
				}
			}
			
			return false;
  		}
  		
 		 			
	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Ignore_Keys
	//
	/////////////////////////////////////////////////////////
	
		function Ignore_Keys (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 tab keys
			
			if (charCode == 9) {
				
				return true;
			}
			
			// allow backspace
			
			if (charCode == 8) {
				
				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 decimal restriction
			
			if (restrictions == 'decimal') {
			
				// decimal point
				
				if (charCode == 46) {
				
					return true;
				}
				
				// check for digits

				if (charCode < 48 || charCode > 57) {
				
					alert ("Please enter only digits or a decimal point in this field");
				
					return false;
				}
			}
			
			// handle decimal restriction
			
			if (restrictions == 'decimalPlusMinus') {
			
				// decimal point
				
				if (charCode == 46) {
				
					return true;
				}
				
				// hyphen
				
				if (charCode == 45 || charCode == 109 || charCode == 189 || charCode == 150) {
				
					return true;
				}
				
				// check for digits

				if (charCode < 48 || charCode > 57) {
				
					alert ("Please enter only digits, a decimal point, or a hyphen (negative sign) in this field");
				
					return false;
				}
			}
			
			// handle currency restriction
			
			if (restrictions == 'currency') {
			
				// decimal point
				
				if (charCode == 46) {
				
					return true;
				}
				
				// comma
				
				if (charCode == 44) {
				
					return true;
				}
				
				// check for digits

				if (charCode < 48 || charCode > 57) {
				
					alert ("Please enter only digits, a decimal point, or a comma 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;
				}
			}

			// handle no input restriction
			
			if (restrictions == 'noInput') {

				alert ("Direct input is not allowed in this field");
				
				return false;
			}

			return true;
		}


	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Update_Words_Remaining
	//
	/////////////////////////////////////////////////////////
 
		function Update_Words_Remaining (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:  Update_Characters_Remaining
	//
	/////////////////////////////////////////////////////////
	
		function Update_Characters_Remaining (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;
		}


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

	/////////////////////////////////////////////////////////
	//
	//  FUNCTION:  Validate_Email_Address
	//
	/////////////////////////////////////////////////////////
	
		function Validate_Email_Address (str) {

			var at="@"
			var dot="."
			var lat=str.indexOf(at)
			var lstr=str.length
			var ldot=str.indexOf(dot)
			if (str.indexOf(at)==-1){
			   return false
			}
	
			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			   return false
			}
	
			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
				return false
			}
	
			 if (str.indexOf(at,(lat+1))!=-1){
				return false
			 }
	
			 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
				return false
			 }
	
			 if (str.indexOf(dot,(lat+2))==-1){
				return false
			 }
			
			 if (str.indexOf(" ")!=-1){
				return false
			 }
	
			 return true					
		}

