/*
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:
	ConcatDateFields
	CreateSubmitDataString
	DigitValidation
	EmailAddressValidation
	EmptyValidation
	GoToURL
	IsValidDate
	LengthValidation
	MembershipNumberValidation
	MinLengthValidation
	NewWindow
	RadioGroupValidation
	SelectValidation
	SpecialCharValidation
	StateValidation
	SubmitFormOnlyOnce
	Trim
*/
	
// GLOBAL VARIABLES				// USED BY FUNCTION
var submitCount = 0;			// SubmitFormOnlyOnce
var oWinInfo=null;

/************************************************************************
* ConcatDateFields
* 	takes values from month, day and year fields and concatenates 
* 	them into MM/DD/YY(YY) format
************************************************************************/
function ConcatDateFields(fldMonth, fldDay, fldYear)
{
	strDate = fldMonth.options[fldMonth.selectedIndex].value + "/" + fldDay.options[fldDay.selectedIndex].value + "/" + fldYear.options[fldYear.selectedIndex].value;
	return strDate;
}


/************************************************************************
* 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;
}


/************************************************************************
* DigitValidation
* 	verifies numeric value was entered and that it's length is between
* 	the passed min and max
************************************************************************/
function DigitValidation(entered, lenMin, lenMax, alertText)
{
	with (entered)
	{
		checkvalue=parseFloat(value);
		if ((parseFloat(lenMin)==lenMin && value.length<lenMin) || (parseFloat(lenMax)==lenMax && value.length>lenMax) || value!=checkvalue)
		{
			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)
	{
		len=value.length
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		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=="")
	{
		if (alertText)
		{
			alert(alertText);
		} 
		entered.focus();
		entered.select();
		return false;
	}
	else 
		return true;
}

/************************************************************************
* GoToURL
*	Redirects the main frame in a frameset to page or anchor specified
*	in value property of select/option item named "dest"
************************************************************************/
function GoToURL(form) {
        var myindex=form.dest.selectedIndex
        window.open(form.dest.options[myindex].value, target="main"); //change these both to "yes" to get the toolbar
}

/************************************************************************
* IsValidDate
* 	Checks for the following valid date formats: 
* 	MM/DD/YYYY OR MM-DD-YYYY 
*	Also separates date into month, day, and year variables 
************************************************************************/
function IsValidDate(dateField) 
{ 

	var dateStr = dateField.value;

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; 

	var matchArray = dateStr.match(datePat); // is the format ok? 
	if (matchArray == null) 
	{ 
		alert("\"Date of Birth\" must be in entered in the format mm/dd/yyyy.") 
		dateField.select();
		dateField.focus();
		return false; 
	} 

	month = matchArray[1]; // parse date into variables 
	day = matchArray[3]; 
	year = matchArray[4]; 

	if (month < 1 || month > 12) 
	{ // check month range 
		alert("\"Date of Birth\" month must be between 1 and 12."); 
		dateField.select();
		dateField.focus();
		return false; 
	} 

	if (day < 1 || day > 31) 
	{ // check day range
		alert("\"Date of Birth\" day must be between 1 and 31."); 
		dateField.select();
		dateField.focus();
		return false; 
	} 
	
	return true; // date is valid 
} 


/************************************************************************
* 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)
{
	with (entered)
	{
		if (value.length < lenMin || value.length > lenMax)
		{
			if (alertText) 
			{
				alert(alertText);
			} 
			entered.focus();
			entered.select();
			return false;
		}
		else 
			return true;
	}
}


/************************************************************************
* MembershipNumberValidation
*	shows a message (alertText) and returns false if the membership
*	number (entered) does not meet the criteria set up in the function
************************************************************************/
function MembershipNumberValidation(entered, alertText)
{
	with (entered)
	{
		// requirements:
		//		- begin with '600663'
		//		- length of exactly 16 digits
		//		- numeric only
		if ((value.substring(0,6) != '600663') ||
			(value.length != 16) ||
			(value != parseFloat(value)) )
		{
			if (alertText) 
			{
				alert(alertText);
			} 
			entered.focus();
			entered.select();
			return false;
		}
		else 
			return true;

	}
}

/************************************************************************
* MinLengthValidation
*	shows a message (alertText) and returns false if the length of
*	the input text (entered) is too short
************************************************************************/
function MinLengthValidation(entered, lenMin, alertText)
{
	with (entered)
	{
		if (value.length < lenMin )
		{
			if (alertText) 
			{
				alert(alertText);
			} 
			entered.focus();
			entered.select();
			return false;
		}
		else 
			return true;
	}
}

/************************************************************************
* NewWindow
*	opens a new browser window with the specified url, window name
*	and other parameter specs
************************************************************************/
function NewWindow(URL, name, specs) 
{
	var anon_win = window.open(URL, name, specs);
}

/************************************************************************
* RadioGroupValidation
*	shows a message (alertText) and returns false if no selection has 
*	been made between the items in the radio group (groupName)
************************************************************************/
function RadioGroupValidation(groupName, alertText)
{
	var allEmpty = true;
	for (index = 0; index < groupName.length; index++)
	{
  		if (groupName[index].checked)
	  	{
	   		allEmpty = false; //found one to be selected
	    	break;            //get out of the loop
	  	}
	}
	if (allEmpty)
	{
		if (alertText)
			alert(alertText);
		return false;
	}
	else
		return true;
}

/************************************************************************
* SelectValidation
*	shows a message (alertText) and returns false if no data has been 
*	selected in the dropdown list (entered)
************************************************************************/
function SelectValidation(entered, alertText)
{
	var idx = entered.selectedIndex;

	with (entered.options[idx])
		{
		if ( (value == 0) || (value == "") || (value == null) )
		{
			if (alertText) 
			{
				alert(alertText);
			}
			entered.focus();
			return false;
		}
		else 
			return true;
	}
}


/************************************************************************
* SpecialCharValidation
* 	allows only certain characters to be entered (to prevent foreign
*   character issues)
************************************************************************/
function SpecialCharValidation(entered, alertText)
{
	str = entered.value;
	with (str)
	{
		for (i = 0; i < length; i++)
		{
			ch = charAt(i);
			if ( !(ch >= 'a' && ch <= 'z') 
				&& !(ch >= 'A' && ch <= 'Z') 
				&& !(ch == ' ') 
				&& !(ch == ',')
				&& !(ch == '-')
				&& !(ch == '.') 
				&& !(ch == '\'')
				&& !(ch == '\/')
				&& !(ch == '(')
				&& !(ch == ')')
				&& !(ch == '@')
				&& !(ch == '_')
				&& !(ch >='0' && ch <='9'))
			{
				if (alertText) 
					alert(alertText);
				entered.focus();
				entered.select();
				return false;
			}
		}
	}
	return true;
}


/************************************************************************
* StateValidation
*	shows a message and returns false if no state has been selected 
*	in the state dropdown list (stateField) when the country selected
*	in the country dropdown list (countryField) is "US" or "CA" 
*	(United States or Canada)
************************************************************************/
function StateValidation(countryField, stateField)
{
	var strCountry = countryField.value;
	var strState = stateField.options[stateField.selectedIndex].value;
	if ( ( (strCountry=="US") || (strCountry=="USA") ) ||
		 ( (strCountry=="CA") || (strCountry=="CAN") ) ||
		 ( (strCountry=="AU") || (strCountry=="AUS") ) )
	{
		if ( (strState == 0) || (strState=="") || (strState == null) )
		{
			alert("Please enter a State/Province/Territory for US/Canada/Australia.");
			stateField.focus();
			return false;
		}
		else
			return true;
	}
	else
		return true;
}

/************************************************************************
*   submitForm
*   A simple functiuon to submit HTML form
************************************************************************/
function submitForm(pForm){
		pForm.submit();
}

/************************************************************************
* 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;
}

function showWindow( m_url, winname, winwidth, winheight, wndparams)
{	
	
	if(oWinInfo != null){
		try{
			oWinInfo.close();
		}catch(e){}
		oWinInfo = null;		
	}
		
	if ((winname == null) || (winname == "")) winname = "lvs";
	
	winleft = (screen.availWidth / 2) - (winwidth / 2);
	wintop = (screen.availHeight / 2) - (winheight / 2);
	
	if (wndparams == null) wndparams = "scrollbars=no,resizable=no";
	
	oWinInfo=(window.open(m_url, winname,"left=" + winleft + ",top=" + wintop + ",width=" + winwidth + ",height=" + winheight + "," + wndparams));
	//	registerWindow(oWinInfo);
	
	if (!Browser.ie) {
		if(oWinInfo.self != null)  oWinInfo.focus();
	}
	
}

var Browser = new Object();
with (Browser) {
	Browser.b = Browser;
	b.a = navigator.userAgent.toLowerCase();
	b.v = navigator.appVersion;
	b.version = parseFloat(v);
	b.major = parseInt(v);
	b.opera = (a.indexOf('opera') != -1)?true:false;
	b.hotjava = (a.indexOf('hotjava') != -1)?true:false;
	b.webtv = (a.indexOf('webtv') != -1)?true:false;
	b.nav = (a.indexOf('mozilla') != -1 && a.indexOf('spoofer') == -1 && 
		a.indexOf('compatible') == -1 && !opera && !webtv && !hotjava)?true:false;
	b.nav4 = (nav && major == 4)?true:false;
	b.nav6 = (nav && major == 5)?true:false;
	b.nav6up = (nav && major >= 5)?true:false;
	b.aol = (a.indexOf('aol') != -1)?true:false;
	//b.ie = (!opera && a.indexOf('msie') != -1)?true:false;
	b.ie = (opera || a.indexOf('msie') != -1)?true:false;
	b.ie4 = (ie && major == 4 && a.indexOf('msie 4') != -1)?true:false;
	b.ie4up = (ie && major >= 4)?true:false;
	b.ie5 = (ie4up && a.indexOf('msie 5.0') != -1)?true:false;
	b.ie5up = (ie4up && !ie4)?true:false;
	b.macie5=(ie4 && v.indexOf("macintosh")!=-1)?true:false;
	b.ie55 = (ie5up && a.indexOf('msie 5.5') != -1)?true:false;
	b.ie55up = (ie5up && !ie5)?true:false;
	b.ie6 = (ie4up && a.indexOf('msie 6.') != -1)?true:false;
	b.ie6up = (ie55up && !ie55)?true:false;
	b.win95 = (a.indexOf('win95') != -1 || a.indexOf('windows 95') != -1)?true:false;
	b.win98 = (a.indexOf('win98') != -1 || a.indexOf('windows 98') != -1)?true:false;
	b.mac = (a.indexOf('macintosh') != -1)?true:false;
	b.win9x = (win95 || win98)?true:false;
	b.sun = (a.indexOf('sunos') != -1)?true:false;
}

