function isBlank(field) {
	count = 0;
	eval ("if (document.forms[0]." + field + ".value == '') { count = 1; }");
	return count;
}
function isMaxLength(obj, maxlength){
	var mlength = new Number(maxlength);
	if(obj.value.length > mlength){
		obj.value = obj.value.substring(0,mlength);
	}
}
function checkNum(element) {
	var theElement = "document.forms[0]." + element + ".value";
	number = eval(theElement);
	if (isNaN(number))return false; else return true;
}
function checkTel(element,strTel) {
	var theElement = ""
	if(element!="") {
		theElement = "document.forms[0]." + element + ".value";
		number = eval(theElement);
	}
	else number = strTel;
	//if (isNaN(number)) return false; else return true;
	var filter=/[a-zA-Z0-9]|[\s]/
	if (filter.test(number)) return true; else return false;
}
function checkRadioList(element){
	var radio_choice = false;
	var theElement = eval("document.forms[0]." + element);
	
	for (i = 0; i < theElement.length; i++)
	{
		if (theElement[i].checked)
		radio_choice = true; 
	}
	if (!radio_choice) return false; else return true;
}
function checkCheckList(from,to){
	var counter = 0;
	for (i = from; i <= to; i++)
	{
		theElement = eval("document.forms[0].w_" + i);
		if (theElement.checked)
			counter ++; 
	}
	if (counter>0)return false; else return true;
}
function isChecked(field) {
	count = 0;
	eval ("if (document.forms[0]." + field + ".checked) { count = 1; }");
	return count;
}
function isSelected(field) {
	count = 0;
	eval("if (document.forms[0]." + field + ".selectedIndex != 0) { count = 1; }");
	return count;
}
function IsValidObject(field) {
	
	if (eval("document.forms[0]." + field) == null || eval("document.forms[0]." + field) == undefined) {
		return false;
	}
	return true;
}
function deCheck(field){
	eval("document.forms[0]." + field + ".checked = false;")			
}
function deCheckGroup(field){
	var theElement = eval("document.forms[0]." + field);
	
	if (theElement != null)
	{
		for (i = 0; i < theElement.length; i++)
		{
			theElement[i].checked = false;
		}
	}
}
function clearInput(field){
	eval("document.forms[0]." + field + ".value = '';")			
}
function checkemail(field,emailAdd){
	var str="";
	if(field!="")str=eval("document.forms[0]." + field + ".value")
	else str=emailAdd;
	var filter=/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|edu|mil|biz|info|mobi|name|aero|jobs|museum)\b$/i
	if (filter.test(str)) return true; else return false;
}

function checkEmailsMatch(email1,email2)
{
	//check strings match is true , otherwise return false
	
	if (email1==email2)return true;
	else return false;
}

// returns true if the string's length equals "len"
function isLength(str, len){
  return str.length == len;
}

function onlyAlpha(field){
  var str=eval("document.forms[0]." + field + ".value")
  var re = /^[A-Za-z]+$/
  if (re.test(str)) return true;
  return false;
}

function onlyAlphaNumeric(field){
  var str=eval("document.forms[0]." + field + ".value")
  var re = /^[A-Za-z0-9]+$/
  if (re.test(str)) return true;
  return false;
}

function onlyNumeric(field)
{
	var str=eval("document.forms[0]." + field + ".value")
	var re = /[^0-9]/
	if (re.test(str)) return false;
	return true;
}
function onlyNumericIncDecimal(field)
{
	var str=eval("document.forms[0]." + field + ".value")
	if (parseFloat(str).toString() != str && str !="")
	{
		alert("must be a number");
		var value = parseFloat(str);
		if (isNaN(value)) value = "";
		eval("document.forms[0]." + field + ".value = '" + value.toString() + "';")
		
		return false;
	}
	return true;
}
function onlyNumericWithDecimal(field)
{
	var str=getElement(field).value;
	var isNum = true;
	if (str !="")
	{
		var value = parseFloat(str);
		if (isNaN(value)){isNum = false; value = "";}
		if(isNum){
		     //value = Math.round(value*100)/100;
		     if(value.toFixed) value = value.toFixed(2);
			 getElement(field).value = value;
			 return true;
		}
		else return false;
	}
	return false;
}
//expects date in mm/dd/yyy format
function isDate(DateToCheck){
	var m_arrDate = DateToCheck.split("/");
	var m_MONTH = m_arrDate[0];
	var m_YEAR = m_arrDate[2];
	if(m_YEAR.length > 4){return false;}
	var testDate=new Date(DateToCheck);
	if(testDate.getMonth()+1==m_MONTH){
		return true;
	} 
	else{
		return false;
	}
}
function CountLeaps(y){ return (y-1)/4 - (y-1)/100 + (y-1)/400; }
function getAge(dob) {
	var _month = dob.split('/')[0];
	var _day = dob.split('/')[1];
	var _year = dob.split('/')[2];
	var _milliseconds_in_a_day = 1000*60*60*24;
	var birth = new Date(_year, _month-1, _day);
	var today = new Date();
	var days_since_birth =((today - birth)/_milliseconds_in_a_day) - 1;
	var age = Math.floor(days_since_birth/365);
	var age = Math.floor(days_since_birth/(((365*age)+CountLeaps(age))/age));
	return age;
}
function trimAll(sString) {
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
function getElementValidiation(id) { 
	return document.all ? document.all(id) : 
	document.getElementById ? document.getElementById(id) : 
	document.layers ? document.layers[id] : null; 
} 
function isVisible(id){
	var el1 = getElementValidiation(id); 
	if (!el1) return false;
	if (el1.style){
		if(el1.style.visibility == 'visible') return true;
		else if(el1.style.visibility == 'hidden' || el1.style.visibility == '') return false;
	}
	else if (el.visibility){
		if(el1.visibility == 'show') return true;
		else if(el1.visibility == 'hide' || el1.visibility == '') return false;
	}
}
function ValidateForm(){
	f = document.forms[0];
	errorMsg = "";
	for(i=0;i<f.length;i++){
		var e = f.elements[i];
		elementName = (e.displayName != null && e.displayName != "")?e.displayName:e.name;
		if(e.required ==true){
			switch(e.type){
				case "text":
					if(e.value == "")
						errorMsg += "- " + elementName+" must not be left blank.\n";
					break;
				case "select-one":
					if(e.options[e.selectedIndex].value == null || e.options[e.selectedIndex].value == "")
						errorMsg += "- " + elementName+" must have at least one item selected.\n";
					break;
				case "password":
					if(e.value == "")
						errorMsg += "- " + elementName+" must not be left blank.\n";
					break;
			}
		}
		if(e.type == "radio"){
			if(e.required != null){
				isChecked = false;
				if(e.length != null){
					for(i=0;i<e.length;i++){
						if(e[i].checked){
							isChecked = true;
							break;
						}
					}
				}
				else{
					if(e.checked)
						isChecked = true;
				}
				if(!isChecked)
					errorMsg += "- " + elementName+" must have at least one item selected.\n";
			}
		}
		if(e.value != ""){
			switch(e.checkFormat){
				case "emailaddress":
					if(!checkemail(e.name) && e.value != "")
						errorMsg += "- " + elementName+" is in invalid email address format.\n";
					break;
				case "zip":
					var zipcodeFilter = /^\d{5}(-\d{4})?$/
					if(!zipcodeFilter.test(e.value))
						errorMsg += "- " + elementName+" is in invalid zip code format.\n";
					break;
				
			}
		}
	}
	if(errorMsg != ""){
		errorMsg = "Following field(s) must be completed.\n" + errorMsg;
		alert(errorMsg);
		return false;
	}
	else{
		return true;
	}
	
}
