 function isEmpty(formElement)
 {
	 // make sure field is not empty
	 if (formElement.value == "") {
		 formElement.focus();
		 return true;
	 }
	 return false;
 }
 
 // determines if first item in a select field is selected
 function isFirstElementSelected(formElement)
 {
	 if (formElement.selectedIndex == 0) {
		 formElement.focus();
		 return true;
	 }
	 return false;
 }
 
 // determines if a form field value is a valid number
 function isNumeric(formElement,bInteger)
 {
	 // make sure field is a valid number
	 var checkOK;
	 var checkStr = formElement.value;
	 var allValid = true;
	 var decPoints = 0;
	 var allNum = "";
	 if (bInteger) {
		 checkOK = "0123456789-";
	 } else {
		 checkOK = "0123456789-."
	 }
	 for (i = 0;  i < checkStr.length;  i++) {
		 ch = checkStr.charAt(i);
		 for (j = 0;  j < checkOK.length;  j++) {
			 if (ch == checkOK.charAt(j)) {
				 break;
			 }
		 }
		 if (j == checkOK.length) {
			 allValid = false;
			 break;
		 }
		 if (!bInteger && ch == ".") {
			 allNum += ".";
			 decPoints++;
		 } else {
			 allNum += ch;
		 }
	 }
	 if (!allValid) {
		 formElement.focus();
		 return false;
	 }
	 if (!bInteger && decPoints > 1) {
		 formElement.focus();
		 return false;
	 }
	 return true;
 }
 
 function isValidDate(formField)
 {
	 var inputDateAr = new Array();
	 inputDateAr = formField.value.split("/");
	 
	 if (inputDateAr.length != 3) {
		 // Date entered did not have two /'s
		 formField.focus();
		 return false;
	 }

	 var month = inputDateAr[0];
	 var day = inputDateAr[1];
	 var year = inputDateAr[2];
	 
	 if (isNaN(month) || isNaN(day) || isNaN(year)) {
		 return false;
	 }

	 if (month <= 0 || month > 12) {
		 formField.focus();
		 return false;
	 }

	 if (year < 1900) {
		 formField.focus();
		 return false;
	 }


	 if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day > 31 || day < 1)) {
		 return false;
	 }
	 if ((month == 4 || month == 6 || month == 9 || month == 11) && (day > 30 || day < 1)) {
		 return false;
	 }
	 if (month == 2) {
		 if (day < 1) {
			 return false;
		 }
		 if (isLeapYear(year) == true) {
			 if (day > 29) {
				 return false;
			 }
		 } else {
			 if (day > 28) {
				 return false;
			 }
		 }
	 }
	 return true;
 }
 
 function isLeapYear(intYear)
 {
	 if (intYear % 100 == 0) {
		 if (intYear % 400 == 0) {
			 return true;
		 }
	 } else {
		 if ((intYear % 4) == 0) {
			 return true;
		 }
	 }
	 return false;
 }
 //-->
 <!--

function GetSelections(select)
{
 
  var r ="";
  for (var i = 0; i < select.options.length; i++)
  {
	if (select.options[i].selected)
	{
	  if (r != "")
	  {
		r += "|";
	  }

	  r += select.options[i].text;
	}                  
  }
  return r;
  }

 function doBtn(btn)
 {
	 if (btn.name == "BTN_UPDATE")
	  {
		  if (validateForm()) 
		  {
			 document.forms[0].ps_priv.value = GetSelections(document.forms[0].ps_privilege);                        
			 document.forms[0].action="userUpdate.jsp";
			 document.forms[0].submit();
		  }
		 
	 } else if (btn.name == "BTN_NEW") {
		if (validateForm()) 
		{
		 document.forms[0].ps_priv.value = GetSelections(document.forms[0].ps_privilege);							
		 document.forms[0].action="userEnroll.jsp";
		 document.forms[0].submit();
		}
	 } else if (btn.name == "BTN_CANCEL") {
		// history.go(-1);
		 document.forms[0].action="home.jsp";
		 document.forms[0].submit();
	 }
 }
 
function validatePassForm()
{
	var form = document.forms[0];
	if (isEmpty(form.p_password)||isEmpty(form.ps_verif_password)) 
	{
		 alert("Please fill both password and verifeye password fields.");
		 return false;
	}
	if (form.p_password.value != form.ps_verif_password.value) 
	{
		 alert("Password and Verify Password do not match.");
		 return false;
	}
	 return true;
}
/*
*Function to trim spaces in string
*/
function ltrim(s)
{
//alert("ltrim.");
 return s.replace(/\s+/,"");
} 	 

 function validateForm()
 {

	 var form = document.forms[0];
	 // check required fields

	 if (isEmpty(form.p_first_name) ||
		  isEmpty(form.p_last_name))
	 {
		 alert("Please fill in your first and last name.");
		 return false;
	 }
	 if (form.ps_type.value != "agentenroll"){
		if(form.ps_type.value == "adminenroll"||form.ps_type.value == "adminupdate"){	
			if (isFirstElementSelected(form.p_business_type)) 
			{
				 alert("Please select business type.");
				 return false;
			}
		 }
		 // length of first name and last name can not exceed 50
		 if (form.p_first_name.value.length + form.p_last_name.value.length > 50) 
		 {
			 alert("The combination of first name and last name can not " +
					 "exceed 50 characters. Please correct this problem.");
			 form.p_first_name.focus();
			 return false;
		 }
		if (isEmpty(form.p_username)) 
		{
			 alert("Please fill in your username.");
			 return false;
		}
		//alert("1");
		 // validate username for special characters
		 if (form.ps_mode.value == "insert" &&
			(form.p_username.value.indexOf(" ") > -1 ||
			 form.p_username.value.indexOf(",") > -1 ||
			 form.p_username.value.indexOf('"') > -1 ||
			 form.p_username.value.indexOf("*") > -1 ||
			 form.p_username.value.indexOf("?") > -1 ||
			 form.p_username.value.indexOf("(") > -1 ||
			 form.p_username.value.indexOf(")") > -1 ||
			 form.p_username.value.indexOf("[") > -1 ||
			 form.p_username.value.indexOf("]") > -1 ||
			 form.p_username.value.indexOf("{") > -1 ||
			 form.p_username.value.indexOf("}") > -1 ||
			 form.p_username.value.indexOf("#") > -1 ||
			 form.p_username.value.indexOf("/") > -1 ||
			 form.p_username.value.indexOf("|") > -1 ||
			 form.p_username.value.indexOf("\\") > -1 ||
			 form.p_username.value.indexOf("+") > -1 ||
			 form.p_username.value.indexOf("=") > -1 ||
			 form.p_username.value.indexOf(";") > -1 ||
			 form.p_username.value.indexOf(":") > -1 ||
			 form.p_username.value.indexOf("'") > -1 ||
			 form.p_username.value.indexOf("`") > -1))
		 {
		   alert("The username you have selected contains invalid characters. " +
				 "Please use only letters and numbers");
			form.p_username.focus();
			return false;
		 }
	 }
	//****special check to make sure all required fields****// 
	//****info are filled in with selfenrollment****//
	
	if (form.ps_type.value == "userenroll"||form.ps_type.value == "userupdate"||form.ps_type.value == "agentenroll")
	{
     	if (isEmpty(form.p_phone)||form.p_phone.value.length < 10) 
		{
			 alert("10 digit numeric number required for phone number.");
			 form.p_phone.focus();
			 return false;
		}
		if(!isEmpty(form.p_phone) && !isNumeric(form.p_phone,true)){
			 alert("Only numeric fields allowed for phone number.");
			 return false;
		}
		if(!isEmpty(form.p_extension) && !isNumeric(form.p_extension,true)){
			 alert("Only numeric fields allowed for phone extension.");
			 return false;
		}
			 	
		if(!isEmpty(form.p_fax)){
    	  //alert("Call ltrim.");
		  //ltrim(form.p_fax.value);
		  if (form.p_fax.value.length < 10 || !isNumeric(form.p_fax,true)){
			 alert("10 digit numeric number required for fax number.");
			 form.p_fax.focus();
			 return false;
		   }	 
		}	
		//if(!isEmpty(form.p_sale_time_phone)){
		//  if (form.p_sale_time_phone.value.length < 10 || !isNumeric(form.p_sale_time_phone,true)){
		//	 alert("0 digit numeric number required for sale time phone number.");
		//	  form.p_sale_time_phone.focus();
		//	 return false;
		//  } 
		//}		
	
		if (isEmpty(form.p_address)||isEmpty(form.p_city)||isFirstElementSelected(form.ps_states)||isEmpty(form.p_zip))
		{
			 alert("Please fill in all the required address information.");
			 return false;
		}
		if ((!isEmpty(form.p_zip)&&!isNumeric(form.p_zip,true))||form.p_zip.value.length < 5) 
		{
			 alert("5 digit numeric number required for zip code.");
			 return false;
		}
		if(!isEmpty(form.p_zip_extension) && !isNumeric(form.p_zip_extension,true)){
			 alert("Only numeric fields allowed for zip extension.");
			 form.p_zip_extension.focus();
			 return false;
		}		//alert(form.p_phone.value.length);
		// validate email address
		if (isEmpty(form.p_email)) 
		{
			 alert("Please fill in your email address.");
			 return false;
		}
		if (form.p_email.value != "" && ((form.p_email.value.indexOf("@") == -1) ||
			(form.p_email.value.indexOf("@") >= form.p_email.value.lastIndexOf(".")))) 
	 	{
			 alert("Please enter email address in the form of 'name@domainname.com'.");
			 form.p_email.focus();
			 return false;
		}		
	//alert("done.");

	}//*****end userneroll/userupdate required check*****

	//****special check to make sure password****// 
	//****info are filled in with selfenrollment****//
	//****if user updates account online****//
	//if (form.ps_type.value == "userupdate"){
	//	if (isEmpty(form.p_password)){
	//		 alert("Please fill in your password.");
	//		 return false;
	//	}
	//}//*****end userupdate required check*****
	
	//Fax number check if field is filled in 
	/*
	if (!isEmpty(form.p_fax))
	{
		if (!isNumeric(form.p_fax,true)) 
		 alert("Only numeric fields allowed for fax number.");
		 return false;
	}//end fax number check
		
	//Sale time phone number check if field is filled in 
	if (!isEmpty(form.p_sale_time_phone))
	{
		if (!isNumeric(form.p_sale_time_phone,true)) 
		 alert("Only numeric fields allowed for sale time phone number.");
		 return false;
	}//end sale time number check
		
	//*****special check to make sure webadmin ****/
	//*****enrollment has all required info****//
	
	if (form.ps_type.value == "adminenroll"||form.ps_type.value == "adminupdate")
	{
		//if(form.ps_billable.checked&&isFirstElementSelected(form.ps_cattrate))
		//{
		//	 alert("Please select Cattlelog Rate Information.");
		//	 return false;
		//}
		//If Billable or Alliance billable or Alliace Rate is checked
		//a rate needs to be selected 
		if(form.ps_billable.checked||form.ps_bill_alliance.checked||form.ps_alliance_rate.checked){ 
			if(isFirstElementSelected(form.ps_cattrate)){
				alert("Please select a rate for this account");
				return false;
			}
			if(!form.ps_cattlelog_customer.checked){
				alert("Please check Cattlelog Customer");
				return false;
		 	}
		 }
		//If Alliance billable is checked
		//Alliace Rate needs to be checked 
		 if(form.ps_bill_alliance.checked&&!form.ps_alliance_rate.checked)
		 {
				alert("Please check Alliance Rate for this account");
				return false;
		 }

		 var priv_sel =0;
		 for (var i = 0; i < document.forms[0].ps_privilege.options.length; i++)                        
		 {
			 if (document.forms[0].ps_privilege.options[i].selected)
				priv_sel += 1;
		 }
	
		 if (priv_sel == 0)
		 {
			alert("Please select a privilege for this user.");
			form.ps_privilege.focus();
			return false;
		 }
	 
	    // make sure we have a username and password if web user checkbox is checked
		if (isEmpty(form.p_username) || (form.ps_mode.value == "insert" && isEmpty(form.p_password)))
		 {
			   alert("Please enter username and password.");
			   return false;
		 }//end username/password check
		 
		 // make sure admin_for present if administrator selected
		 if (form.ps_administrator.checked||form.ps_bill_alliance.checked||form.ps_alliance_rate.checked)
		  {
			 if (isEmpty(form.p_admin_for)) {
				 alert("An Alliance name is required " +
						 "when Alliance Administrator or Alliance Billable or Alliance Rate is selected.");
				 form.p_admin_for.focus();
				 return false;
			 }
		 }//end admin name check
		 
	     //alliance check
		 if (document.forms[0].ps_bill_alliance.checked){
			if(!document.forms[0].ps_cattlelog_customer.checked) {
			 alert("Please check CattleLog Customer");
			 return false;
			 }
			if(!document.forms[0].ps_billable.checked) {
			 alert("Please check Billable");
			 return false;
			 }
			if(!document.forms[0].ps_administrator.checked) {
			 alert("Please check Alliance Administrator");
			 return false;
			 }
		 }//end alliance check
	 
	}//****end webenroll check****
   //alert(form.p_username.value);
   //alert("Done with Checks!");
   return true;
 }
 
 function toggleAdmin()
 {
	 if (document.forms[0].ps_administrator.checked) {
		 adminLabel.style.fontWeight="bold";
		 allianceLabel.style.fontWeight="bold";
		 document.forms[0].p_admin_for.focus();
		 if (!document.forms[0].ps_cattlelog_customer.checked) {
		 	document.forms[0].ps_cattlelog_customer.click();
	 	}
	 	if (!document.forms[0].ps_billable.checked) {
			 document.forms[0].ps_billable.click();
		}
		 //if (document.forms[0].ps_bill_alliance.checked) {
		//	 document.forms[0].ps_bill_alliance.click(); }
		if (!document.forms[0].ps_bill_alliance.checked) {
			alert("If this is the Alliance Billable account, "+
		          "please check Alliance Billable!");		
	   }
	}
    else {
		 adminLabel.style.fontWeight="normal";
		 allianceLabel.style.fontWeight="normal";
		 document.forms[0].p_admin_for.value="";
		 if (document.forms[0].ps_bill_alliance.checked) {
			 document.forms[0].ps_bill_alliance.click();
	    	 alert("This account is no longer the "+
		           "Alliance Billable account!");	 	
		}
	}
 }
 
 function toggleCcust()
 {
	 if (!document.forms[0].ps_cattlelog_customer.checked) {
		 if (document.forms[0].ps_billable.checked) {
			 document.forms[0].ps_billable.click();
		 }
		 if (document.forms[0].ps_administrator.checked) {
			 document.forms[0].ps_administrator.click();
		 }
		 if (document.forms[0].ps_bill_alliance.checked) {
			 document.forms[0].ps_bill_alliance.click();
		 }
	 } else {
		 if (!document.forms[0].ps_billable.checked) {
			 document.forms[0].ps_billable.click();
		 }
	 }
 }

 function allianceAdmin()
 {

	if (document.forms[0].ps_bill_alliance.checked) {
		 if (!document.forms[0].ps_administrator.checked) {
			 document.forms[0].ps_administrator.click();}
		 if (!document.forms[0].ps_cattlelog_customer.checked) {
			 document.forms[0].ps_cattlelog_customer.click();
	 	 }
		 if (!document.forms[0].ps_billable.checked) {
			 document.forms[0].ps_billable.click();
		 }
		 //allianceLabel.style.fontWeight="bold";
		 //document.forms[0].p_admin_for.focus();
 		 
		 // Disable since now account is allowed to load
		 //data against their own account
		 //document.forms[0].p_billacct.value = "";
		 //document.forms[0].p_billacct.disabled = true;
	 } else {
		 //document.forms[0].p_billacct.disabled = false;	 
	 }
 }

 function alertBillable()
 {
	 if (!document.forms[0].ps_billable.checked &&
		  document.forms[0].ps_cattlelog_customer.checked) {
		 alert("You are making this customer " +
				 "not billable for their information. " +
				 "If this is not correct, please recheck Billable.");
	 }
	 else
	 {
		 if (!document.forms[0].ps_cattlelog_customer.checked) {
			 document.forms[0].ps_cattlelog_customer.click();}
	 }
	 
 }
 
 function allianceRate()
 {
 	if(isFirstElementSelected(ps_cattrate)){
		alert("Please select a rate for this account");
		return false;
	}
	return true;
 }
 
function validateListingForm()
{
	var form = document.forms[0];
	if (isEmpty(form.p_headnum)||!isNumeric(form.p_headnum,true)) 
	{
		 alert("Please fill in a Head Number in numeric format.");
		 form.p_headnum.focus();
		 return false;
	}
	if (isEmpty(form.p_class)) 
	{
		 alert("Please fill in Class.");
		 form.p_class.focus();
		 return false;
	}	
 	if(isFirstElementSelected(form.p_gender)){
		alert("Please select a Gender");
		form.p_gender.focus();
		return false;
	}
	if (isEmpty(form.p_weight)||!isNumeric(form.p_weight,true)) 
	{
		 alert("Please fill in Estimated Weight in numeric format.");
		 form.p_weight.focus();
		 return false;
	}
	 //start program check
	 var prog_sel =0;
	 for (var i = 0; i < form.p_program.options.length; i++)                        
	 {
	 	//alert("Entering p_program");
		 if (form.p_program.options[i].selected)
			prog_sel += 1;
	 }

	 if (prog_sel == 0)
	 {
		alert("Please select a program for this listing.");
		form.p_program.focus();
		return false;
	 }
	 if (prog_sel != 0)
	 {
	 	form.p_prog.value = GetSelections(form.p_program);
	 }
     //end program check
	if (isEmpty(form.p_location)) 
	{
		 alert("Please fill in a Location.");
		 form.p_location.focus();
		 return false;
	}
	if (isEmpty(form.p_delivery)) 
	{
		 alert("Please fill in a Delivery Terms.");
		 form.p_delivery.focus();
		 return false;
	}	
 	if(isFirstElementSelected(form.p_del_date)){
		alert("Please select a Delivery Date");
		form.p_del_date.focus();
		return false;
	}
	if (isEmpty(form.p_conditions)) 
	{
		 alert("Please fill in a Weighing Conditions.");
		 form.p_conditions.focus();
		 return false;
	}
	if (isEmpty(form.p_rep)) 
	{
		 alert("Please fill in Represented By.");
		 form.p_rep.focus();
		 return false;
	}	
	if (isEmpty(form.p_auditfl)) 
	{
		 alert("Please fill in Audit File.");
		 form.p_auditfl.focus();
		 return false;
	}
	if (isEmpty(form.p_active)) 
	{
		 alert("Please select Active.");
		 form.p_active.focus();
		 return false;
	}
	if (isEmpty(form.p_sale_type)) 
	{
		 alert("Please fill in a Sale Type.");
		 form.p_sale_type.focus();
		 return false;
	}		
	if (isEmpty(form.p_age)) 
	{
		 alert("Please fill in Birth Date Oldest Animal.");
		 form.p_age.focus();
		 return false;
	}
 	if(isFirstElementSelected(form.p_states)){
		alert("Please select a State");
		form.p_states.focus();
		return false;
	}
	//alert("Leaving check");
	//alert(form.p_prog.value);		
	return true;
}
 
function validateSearchListingForm()
{
	var form = document.forms[0];
	
	if (!isEmpty(form.p_weightstart)) 
	{
		if (isEmpty(form.p_weightend)) 
		{
			 alert("Please fill in an End Weight.");
			 form.p_weightend.focus();
			 return false;
		}
		if (!isNumeric(form.p_weightstart,true)) 
		{
			 alert("Please use numeric only for Start Weight.");
			 form.p_weightstart.focus();
			 return false;
		}
	}		
	if (!isEmpty(form.p_weightend)) 
	{
		if (isEmpty(form.p_weightstart)) 
		{
			 alert("Please fill in a Start Weight.");
			 form.p_weightstart.focus();
			 return false;
		}
		if (!isNumeric(form.p_weightend,true)) 
		{
			 alert("Please use numeric only for End Weight.");
			 form.p_weightend.focus();
			 return false;
		}		
	}

	if (!isEmpty(form.p_salestart)) 
	{
		if (isEmpty(form.p_saleend)) 
		{
			 alert("Please fill in a Sale End Date.");
			 form.p_saleend.focus();
			 return false;
		}
	}
	if (!isEmpty(form.p_saleend)) 
	{
		if (isEmpty(form.p_salestart)) 
		{
			 alert("Please fill in a Sale Start Date.");
			 form.p_salestart.focus();
			 return false;
		}
	}
	 //start program check
	 var prog_sel =0;
	 for (var i = 0; i < form.p_program.options.length; i++)                        
	 {
	 	//alert("Entering p_program");
		 if (form.p_program.options[i].selected)
			prog_sel += 1;
	 }
	 if (prog_sel != 0)
	 {
	 	form.p_prog.value = GetSelections(form.p_program);
	 }
     //end program check	
	// alert("Leaving search");	
    return true;
}

function allianceEidForm()
{
	var form = document.forms[0];
	if(isEmpty(form.p_lot)&&isEmpty(form.p_location)&&isEmpty(form.p_program)&&isEmpty(form.p_origin)&&isEmpty(form.p_startdate)&&isEmpty(form.p_enddate)) 
	{
		 alert("Please fill in at least one parameter");
		 form.p_lot.focus();
		 return false;
	}
	if (!isEmpty(form.p_startdate)) 
	{
		if (isEmpty(form.p_enddate)) 
		{
			 alert("Please fill in an End Date.");
			 form.p_enddate.focus();
			 return false;
		}
	}
	if (!isEmpty(form.p_enddate)) 
	{
		if (isEmpty(form.p_startdate)) 
		{
			 alert("Please fill in a Start Date.");
			 form.p_startdate.focus();
			 return false;
		}
	}
	return true;
}

function allianceEidPopForm()
{
	var form = document.forms[0];
	if (isFirstElementSelected(form.p_lot)&&isFirstElementSelected(form.p_location)&&isFirstElementSelected(form.p_program)&&isFirstElementSelected(form.p_origin)&&isEmpty(form.p_startdate)&&isEmpty(form.p_enddate)) 
	{
		 alert("Please select at least one parameter");
		 form.p_lot.focus();
		 return false;
	}
	if (!isEmpty(form.p_startdate)) 
	{
		if (isEmpty(form.p_enddate)) 
		{
			 alert("Please fill in an End Date.");
			 form.p_enddate.focus();
			 return false;
		}
	}
	if (!isEmpty(form.p_enddate)) 
	{
		if (isEmpty(form.p_startdate)) 
		{
			 alert("Please fill in a Start Date.");
			 form.p_startdate.focus();
			 return false;
		}
	}		
	return true;
}
function validateEidLookupForm()
{
	var form = document.forms[0];
	if (isEmpty(form.p_eidList)) 
	{
		 alert("Please fill in an End EID.");
		 form.p_eidList.focus();
		 return false;
	}
	return true;
}	