

/*
This file contains javascript functions that are used globally in various 
web pages throughout the web site.  NOTE: functions are ALPHABETICAL so
if you add a function to the file please place it accordingly and list it
below...

FUNCTIONS IN THIS FILE:
	CreateSubmitDataString
	DigitValidation
	EmailAddressValidation
	EmptyValidation
	LengthValidation
	SubmitEnter
	SubmitFormOnlyOnce
	Trim
	OpenWindow
*/

// GLOBAL VARIABLES				// USED BY FUNCTION
var submitCount = 0;			// SubmitFormOnlyOnce

/************************************************************************
* CreateSubmitDataString
* 	for the passed form returns a string of all form inputs in the 
*   format [field name]: [field value][new line]
************************************************************************/
function CreateSubmitDataString(theForm)
{
	var strValues = "";
	with (theForm)
	{
		// clear any current value
		submit_data.value = "";
		
		for (var i = 0; i < length; i++)
		{
			strValues = strValues + elements[i].name + ": ";
			if (elements[i].type == "select-one")
				strValues = strValues + elements[i].options[elements[i].selectedIndex].value + "\r\n";
			else
				strValues = strValues + elements[i].value + "\r\n";
		}
	}
	return strValues;
}

/************************************************************************
* SubmitEnter
*	used in onkeypress event of form inputs to allow enter key to 
*   kick off form submission process.  works with ie and netscape.
************************************************************************/
function SubmitEnter(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return false;
	
	if (keycode == 13)
	   return true;
	else
	   return false;
}

/************************************************************************
* DigitValidation
* 	verifies numeric value was entered and that it's length is between
* 	the passed min and max
************************************************************************/
function DigitValidation(entered, lenMin, lenMax, alertText)
{
	var strValue = Trim(entered.value);	
	var checkvalue=parseFloat(strValue);
	if ( (parseFloat(lenMin)==lenMin && strValue.length<lenMin) || (parseFloat(lenMax)==lenMax && strValue.length>lenMax) || strValue!=checkvalue || (strValue.indexOf(".")>0) )
	{
		if (alertText) 
			alert(alertText);
		entered.focus();
		entered.select();
		return false;
	}
	else 
		return true;
}

/************************************************************************
* EmailAddressValidation
* 	shows a message (alertText) and returns false if no e-mail address has been specified in the 
*   input text box (entered) or if the address appears invalid due to a number of conditions checked
************************************************************************/
function EmailAddressValidation(entered, alertText) {
	with (entered)
	{
		var len=value.length
		var apos=value.indexOf("@");
		var dotpos=value.lastIndexOf(".");
		var lastpos=value.length-1;
		// if length = 0
		// or "@" is the first character or doesn't exist
		// or "." doesn't follow "@" with at least 2 spaces between them
		// or there are more than 3 characters following the "."
		// or there are less than 2 characters following the "."
		if (len==0 || apos<1 || dotpos-apos<2 || lastpos-dotpos>4 || lastpos-dotpos<2) 
		{
			if (alertText) 
				alert(alertText);
			entered.focus();
			return false;
		}
		else 
			return true;
	}
}

/************************************************************************
* EmptyValidation
*	shows a message (alertText) and returns false if no data has been 
*	entered in the input text box (entered)
************************************************************************/
function EmptyValidation(entered, alertText)
{
	var strValue = Trim(entered.value);
	if (strValue == null || strValue == "" || strValue == "undefined")
	{
		if (alertText)
		{
			alert(alertText);
		} 
		entered.focus();
		entered.select();
		return false;
	}
	else 
		return true;
}

/************************************************************************
* LengthValidation
*	shows a message (alertText) and returns false if the length of
*	the input text (entered) is too short or too long
************************************************************************/
function LengthValidation(entered, lenMin, lenMax, alertText)
{
	var strValue = Trim(entered.value);
	if (strValue.length < lenMin || strValue.length > lenMax)
	{
		if (alertText) 
		{
			alert(alertText);
		} 
		entered.focus();
		entered.select();
		return false;
	}
	else 
		return true;
}

/************************************************************************
* SubmitFormOnlyOnce
*	once the form's submit button is clicked, prevents the form from 
*   being submitted more than once.  
*   NOTE: Make sure to call this function AFTER all validation functions
************************************************************************/
function SubmitFormOnlyOnce()
{
   if (submitCount == 0)
   {
      submitCount++;
      return true;
   }
   else 
   {
      alert("This form has already been submitted.  Thanks!");
      return false;
   }
}

/************************************************************************
* Trim
*	Trims leading and trailing spaces and tabs. 
************************************************************************/
function Trim(sData) 
{
	var sTrimmed = String(sData);
	sTrimmed = sTrimmed.replace(/(^[ |\t]+)|([ |\t]+$)/g, '');
	return sTrimmed;
}

/************************************************************************
* OpenWindow
*	Opens a new window with the url, winName and specified features.
*   Returns a reference to the opened window. 
************************************************************************/
function OpenWindow(URL,features){
	// ----------------------------
	// Define local variables
	var features;
	var popupWin;
	// ----------------------------
	var winName = new Date();
    winName = winName.getSeconds()+"_"+winName.getMinutes()+"_"+winName.getHours();

	popupWin = window.open(URL,winName,features);
	if (!popupWin.opener) popupWin.opener = self;
    popupWin.focus();
    
    return popupWin;
}
