// FORM VALIDATION FUNCTIONS

//verify email address
/*function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var emailErrorMessage="Please verify your email address";
	if (str.indexOf(at)==-1){
	   alert(emailErrorMessage)
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert(emailErrorMessage)
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert(emailErrorMessage)
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert(emailErrorMessage)
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert(emailErrorMessage)
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert(emailErrorMessage)
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert(emailErrorMessage)
		return false
	 }

	 return true					
}*/
//call like this:
//if (echeck(theForm.Email.value)==false){
//	theForm.Email.focus();
//	return (false);
//}

//revised echeck created for parent info form
function echeck(str, errorMessage) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var emailErrorMessage
	if (errorMessage == undefined) {
		emailErrorMessage="Please verify your email address"
	} else {
		emailErrorMessage = errorMessage
	}

	if (str.indexOf(at)==-1){
	   alert(emailErrorMessage)
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert(emailErrorMessage)
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert(emailErrorMessage)
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert(emailErrorMessage)
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert(emailErrorMessage)
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert(emailErrorMessage)
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert(emailErrorMessage)
		return false
	 }

	 return true					
}
//call like this:
//if (echeck(theForm.Email.value,"Email address format error")==false){
//	theForm.Email.focus();
//	return (false);
//}



//************************************************
//get value of selected radio button
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
//call like this:
//getCheckedValue (theForm.First_Choice)
//or this:
//getCheckedValue(document['taos_form'].elements['First_Choice']);



//************************************************
//set dropdown value
function setSelectedIndex(s, v) {
    for ( var i = 0; i < s.options.length; i++ ) {
        if ( s.options[i].value == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}
//call like this:
//setSelectedIndex(document.FrontPage_Form1.State, "Outside U.S.")
//must place this code somewhere *after* the drop down - not before
//************************************************



//************************************************
//remove page element (say option in a dropdown menu)
function remove(selection){
   selection.parentNode.removeChild(selection);
}
//************************************************


//************************************************
//format amount for ePayments
//strips $ , converts to number with 2 decimal places
function formatAmount(amountNum) {
	var initialValue
	initialValue= amountNum
	amountNum = amountNum.replace("$", "") 
	amountNum = amountNum.replace(",", "")
	amountNum = Number(amountNum);
	amountNum =amountNum.toFixed(2);
	if (amountNum == 'NaN') {
		return (initialValue);
	}
	else {
		return (amountNum);
	}
	
}
//call like this:
//document.myForm.Amount.value =formatAmount(document.myForm.Amount.value);
///or this
//<input name="Amount" type="text" onchange="this.value =formatAmount(this.value);" />
//IF POSSIBLE USE cleanAmount.asp INSTEAD
//************************************************

//************************************************
//add commas to a number
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
//************************************************
