// JavaScript Document
/* Function: trim(string) - returns passed in string with any spaces 
   to the left or right of the string removed */
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

/*Function: valRadioButton - returns true if a radio within the passed in
 * group name is selected */
function valRadioButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}

function jsApplicationValidation() 
{	
	// \n is a line break;
	var sErrorMessage = "Please enter in the following information:\n";
	var iErrorCount = 0;

	// the following are variables that point to the actual control on the page
	var oFullName = document.getElementById("fullName");
	var oAddress = document.getElementById("address");
	var oCity = document.getElementById("city");
	var oState = document.getElementById("state");
	var oZip = document.getElementById("zip");
	var oPhone = document.getElementById("phone");
	
	frm = document.form1;
	
	var oAge = frm.age;
	
	var oLegal = frm.legal;
	var oConvicted = frm.convicted;
	var oConviction = document.getElementById("conviction");
	
	var oApplyBefore = frm.applybefore;
	var oAppliedDate = document.getElementById("applieddate");
	
	var oRef1nameaddress = document.getElementById("ref1nameaddress");
	var oRef1telephone = document.getElementById("ref1telephone");
	var oRef1business = document.getElementById("ref1business");
	var oRef1howlong = document.getElementById("ref1howlong");
	
	var oRef2nameaddress = document.getElementById("ref2nameaddress");
	var oRef2telephone = document.getElementById("ref2telephone");
	var oRef2business = document.getElementById("ref2business");
	var oRef2howlong = document.getElementById("ref2howlong");
	
	var oRef3nameaddress = document.getElementById("ref3nameaddress");
	var oRef3telephone = document.getElementById("ref3telephone");
	var oRef3business = document.getElementById("ref3business");
	var oRef3howlong = document.getElementById("ref3howlong");
	
	var oLimitations = frm.limitations;
	var oLimitationDesc = document.getElementById("limitationDesc");
	var oEmergencyNotify = document.getElementById("emergencyNotify");
	var oEmergencyPhone = document.getElementById("emergencyPhone");
	var oCertify = document.getElementById("certify");	
	
	if(trim(oFullName.value) == "")
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Your name\n";
	}
	if(trim(oAddress.value) == "")
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Your address\n";
	}
	if(trim(oCity.value) == "")
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Your city\n";
	}
	
	if(trim(oState.value) == "")
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Your state\n";
	}
	if(trim(oZip.value) == "")
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Your zip code\n";
	}
	if(trim(oPhone.value) == "")
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Your phone number\n";
	}
	
	if(valRadioButton(oAge) == null)
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Your age indication (18 years or older)\n";
	}
	if(valRadioButton(oLegal) == null || valRadioButton(oConvicted) == null || (valRadioButton(oConvicted) == "Yes" && trim(oConviction.value) == "") )
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Complete the Special Questions section\n";
	}
	if(valRadioButton(oApplyBefore) == "Yes" && trim(oAppliedDate.value) == "") 
	{
		// the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". You indicated that you applied before, please enter the date\n";
	}
	if(trim(oRef1nameaddress.value) == "" || trim(oRef1telephone.value) == "" || trim(oRef1business.value) == "" || trim(oRef1howlong.value) == "" || 
	    trim(oRef2nameaddress.value) == "" || trim(oRef2telephone.value) == "" || trim(oRef2business.value) == "" || trim(oRef2howlong.value) == "" || 
	    trim(oRef3nameaddress.value) == "" || trim(oRef3telephone.value) == "" || trim(oRef3business.value) == "" || trim(oRef3howlong.value) == "")
    {
        // the next line increments iErrorCount by 1
	iErrorCount++;
	// the next line adds the value on the right hand side to sErrorMessage
	//    variable
	sErrorMessage += iErrorCount + ". Please complete all fields in References section\n";
    }
    if(valRadioButton(oLimitations) == "Yes" && trim(oLimitationDesc.value) == "")
    {
        // the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". You indicated a physical limitation, please enter a description\n";
    }
    if(trim(oEmergencyNotify.value) == "" || trim(oEmergencyPhone.value) == "")
    {
        // the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Please complete the Emergency Notify fields in the Limitations section\n";
    }
    if(oCertify.checked == false)
    {
         // the next line increments iErrorCount by 1
		iErrorCount++;
		// the next line adds the value on the right hand side to sErrorMessage
		//    variable
		sErrorMessage += iErrorCount + ". Please check the Certify checkbox\n";
    }
    	
	
	if(iErrorCount > 0)
	{
		alert(sErrorMessage);
		return false;
	}
	else
	{
		/*var f1 = document.getElemebtById("frm");
		alert('');
		f1.Action = "ApplicationThankYou.aspx";
		f1.Submit(); */
		return true;
	}
}