//form scripts
//********************FormValidator***************
//called by forms on submission to validate forms


function FormValidator(which){
//alert(which)
var validFieldIDValue=""
var pass=false
var formflag=true //final status for the form control determine rather to submit the form
	for (i=0;i<which.length;i++){ //loop thro the controls of the form
	var tempobj=which.elements[i]
	//if the name of the tag starts with the word Valid
	//it contains the validation for the same field ID
	//excluding the Valid text
	if (tempobj.name.substring(0,5)=="Valid"){
		var validFieldID=tempobj.name.replace("Valid","") //determine  the ID of the field to validate
		var validFieldIDType=document.getElementById(validFieldID).type //get  the TYPE of the field to validate
		var validFunct=tempobj.value //get  the VALUE of the field to validate
		//alert("loop"+validFieldIDType)
		if (validFieldIDType=="radio"||validFieldIDType=="checkbox") //if the control is of TYPE checkbox or radio do this
			{

				//the groups of controls in radio or checkboxe groups
				//must be accessed from the names because that ties them together

				var validFieldIDName=document.getElementById(validFieldID).name //get the name of the group of controls

				//alert("found " + validFieldIDName + "|" +validFieldIDType)
				validFieldIDValue=""
				for (j=0;j<document.getElementsByName(validFieldIDName).length;j++) //loop thru the controls with this name
				{
					if (document.getElementsByName(validFieldIDName)[j].checked==true) //if find a control that is checked stop the loop
						{
						validFieldIDValue="found" //set the value to found when an checked element is found
						break // stop the loop
						}
					else
						{
						validFieldIDValue="" //element was not checked set the value to ""
						continue // continue to loop and look for a checked element
						}


				}
			}

		else //else if not a checkbox or radio
			{
			validFieldIDValue=document.getElementById(validFieldID).value //  get the value of the field to validate
			}
		 // determine if the value of the control passes validation
		 pass=fieldValidator(validFunct,validFieldIDValue)
		 //fieldValidator returns false for a failed validation




		if (!pass){
		//if pass not equel to true
		show("Err" + validFieldID) //display the error div
		pass=false
		formflag=false
		}
		else
		{

		hide("Err" + validFieldID) //hide the error div
		}






	}



}
if (formflag){

//if the formflag is true
return true // submit the form
}
else //if the formflag is false
alert("One or more of the required form fields are not completed. Please complete them, then submit again!")
return false //dont submit the form
}

//****************end of checkValid********************************


//****************FieldValidator********************************
//called by FormValidator to validate the various types of fields
//a true value returned indicates the field has passed
//fieldtype=the type of validation to process
//field value = the value to check the validation against

function fieldValidator(fieldtype,fieldvalue){
var testresult=true
switch(fieldtype){
case "noVal":
testresult=true
break
case "numVal":
	var anum=/(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(fieldvalue))
		{
		testresult=true
		}
	else
		{
		testresult=false
		}





break
case "txtVal":
testresult=true
break



case "dateVal":
	var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
	//var testresult=false
	if (!validformat.test(fieldvalue))
		testresult=false
		//alert("Invalid Date Format. Please correct and submit again.")
	else{ //Detailed check for valid date ranges
		var monthfield=fieldvalue.split("/")[0]
		var dayfield=fieldvalue.split("/")[1]
		var yearfield=fieldvalue.split("/")[2]
		var dayobj = new Date(yearfield, monthfield-1, dayfield)

		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
			//alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
			testresult=false
		else
			testresult=true
		}
break

case "selectVal":
{
//alert(fieldvalue)
var noselection="---select an option---"
if (fieldvalue=="---select an option---"){
testresult=false
}
else
{
testresult=true
}

break
}

case "requiredVal":
{
//alert("requiredValfieldvalue" + "|" + fieldvalue + "|")
var noentry=""
if (fieldvalue==null){
testresult=false
}
else if(fieldvalue=="")
{
testresult=false
}
else
{
testresult=true
}

break
}



case "maxchrVal":
var maxlength=50
if (fieldvalue.length>maxlength){
testresult=false
}
else
{
testresult=true
}

break
case "emailVal":
var str=fieldvalue
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresult=true
else{

testresult=false

}
break



}
return testresult
}

//function show shows a hidden div tag when passed the ID of the object

function show(object) {
 
 var el=document.getElementById(object);
   el.style.visibility='visible';

}


//*************end show********************************


//function hide hides a visible div tag when passed the ID of the object

function hide(object) {
   var el=document.getElementById(object);
    el.style.visibility='hidden';
}

function delayHide(object) {
   var delay_hide=500
   var hideIt='hidden'
   var el=document.getElementById(object);
   delayhide=setTimeout('hide("'+object+'")',delay_hide);
}

function clear_delayhide(){
if (window.delayhide)
clearTimeout(delayhide)
}

function lockShow(object) {
 show(object)
 clear_delayhide()
 //var el=document.getElementById(object);
   //el.style.visibility='visible';

}

//*************end hide********************************



