// JavaScript Document
/*******************************************************************************
*	Function: checkFormField
*	Date: 07.03.02
*	Checks form field to make sure that it is not empty.  If the form is empty
*	execute alert box.
*
*	Paramaters: entered, alertbox
*	entered: (object, required) form field object.
*	alertbox: (string, required) error message.
*
*	Example Useage:
*	function formvalidation(thisform)
*	{
*		with (thisform)
*		{
*			if (checkFormField(fieldName,"Error Message")==false) {fieldName.focus(); return false;};
*		}
*	}
*
********************************************************************************/
function checkSelectField(entered, alertbox)
{
	with (entered.options[entered.selectedIndex])
	{
		if (value==null || value=="")
			{if (alertbox!="") {alert(alertbox);} return false;}
		else {return true;}
	}
}

function checkFormField(entered, alertbox)
{
	with (entered)
	{
		if (value==null || value=="")
			{if (alertbox!="") {alert(alertbox);} return false;}
		else {return true;}
	}
}

/*******************************************************************************
*	Function: radioGetValues
*	Date: 07.03.02
*	Returns the value of a set of radio buttons.
*
*	Paramaters: entered
*	entered: (object, required) form field object.
*
*	Example Useage:
*	value = radioGetValues(formFild);
*
********************************************************************************/
function radioGetValues(entered){
	for(i = 0, n = entered.length; i < n; i++){
		if(entered[i].checked){
			var checkedValue = entered[i].value;
			return checkedValue;
			break;
		}
	}
}

/*******************************************************************************
*	Function: checkRadio
*	Date: 07.03.02
*	Checks a set of radio buttons form fields to make sure that one is selected.  If the form is empty
*	execute alert box.
*
*	Paramaters: entered, alertbox
*	entered: (object, required) form field object.
*	alertbox: (string, required) error message.
*
*	Example Useage:
*	function formvalidation(thisform)
*	{
*		with (thisform)
*		{
*			if (checkRadio(radioButton,"Error Message")==false) {return false;};
*		}
*	}
*
********************************************************************************/
function checkRadio(entered, alertbox) {
	with(entered){
		var radioValue = radioGetValues(entered);
		if (radioValue == null || radioValue == "") {
      			alert(alertbox);
			return false;
   		}
		else{
			return true;
		}
	} 
}

/*******************************************************************************
*	Function: compareFields
*	Date: 07.03.02
*	Compares to form fields to make sure they match.
*
*	Paramaters: formField1, formField2, alertBox
*	formField1: (object, required) field to be checked.
*	formField2: (object, required) confirmation field.
*	alertbox: (string, required) error message.
*
*	Example Useage:
	<input type="text" name="field1"> 
*	<input type="text" name="field2" onblur="compareFields(document.formName.field1, document.formName.field2, "Error Message");"> 
*
*	Or
*
*	function formvalidation(thisform)
*	{
*		with (thisform)
*		{
*			if (compareFields(field1, field2, "Error Message")==false) {field1.focus(); return false;};
*		}
*	}
*	
********************************************************************************/
function compareFields(formField1, formField2, alertBox){
	//	Make sure password1 matches password2.
	if(formField1.value != formField2.value){
		//	Clear password fields.
		formField1.value = "";
		formField2.value = "";
		formField1.focus();
		//	Focus on form field password1.
		// Show alert message.
		alert(alertBox);
		return false;
	}
}

/*******************************************************************************
*	Function: echeck
*	Date: 07.03.02
*	Checks for a valid email address.
*
*	Paramaters: str
*	str: (String, required) email address to be checked.
*
*	Example Useage:
*	function formvalidation(thisform)
*	{
*		with (thisform)
*		{
*			if (echeck(email.value)==false){email.value=""; email.focus();return false}
*		}
*	}
*
********************************************************************************/
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail Address")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail Address")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Invalid E-mail Address")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Invalid E-mail Address")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Invalid E-mail Address")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Invalid E-mail Address")
	    return false
	 }
		
	 if (str.indexOf(" ")!=-1){
	    alert("Invalid E-mail Address")
	    return false
	 }

		 return true					
}

/*******************************************************************************
*	Function: openWindow
*	Date: 01.19.04
*	Checks to make sure form field is a numeric value.
*
*	Paramaters: objName, minval, maxval, comma, period, hyphen
*	str: (String, required) email address to be checked.
*
*	Example Useage:
*	function formvalidation(thisform)
*	{
*		with (thisform)
*		{
*			if (chkNumeric(formFld,'1','9999','','','') == false){formFld.value=""; formFld.focus();return false}
*		}
*	}
*
********************************************************************************/
function openWindow(file, windowName, args){
	newWin = window.open(file, windowName, args);
}

/*******************************************************************************
*	Function: chkNumeric
*	Date: 01.19.04
*	Checks to make sure form field is a numeric value.
*
*	Paramaters: objName, minval, maxval, comma, period, hyphen
*	objName: (Object, required) field being checked.
*	minval: (int, required) Minimum range.
*	maxval: (int, required) Maximum range.
*	comma: (string, required) value ",".
*	period: (string, required) value ".".
*	hyphen: (string, required) value "-".
*
*	Example Useage:
*	function formvalidation(thisform)
*	{
*		with (thisform)
*		{
*			if (chkNumeric(formFld,'1','9999','','','') == false){formFld.value=""; formFld.focus();return false}
*		}
*	}
*
********************************************************************************/
function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
	// only allow 0-9 be entered, plus any values passed
	// (can be in any order, and don't have to be comma, period, or hyphen)
	// if all numbers allow commas, periods, hyphens or whatever,
	// just hard code it here and take out the passed parameters
	var checkOK = "0123456789" + comma + period + hyphen;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	
	for (i = 0;  i < checkStr.value.length;  i++)
	{
		ch = checkStr.value.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
			if (ch != ",")
				allNum += ch;
	}
		
	if (!allValid)
	{	
		alertsay = "Please enter only these values \""
		alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return (false);
	}
		// set the minimum and maximum
	var chkVal = allNum;
	var prsVal = parseInt(allNum);
	if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
	{
		alertsay = "Please enter a value greater than or "
		alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
		alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return (false);
	}
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}