////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  validate function
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validate_form(FORM){

	// validation variables
	var bgBad = "#f7a810";
	var bgGood = "#FFFFFF";
	var valid = true;
	var missingFields = "The following fields are required:\n\n";            
	var numElements = FORM.elements.length;             
	
	// check all fields to ensure that they are not empty       
	var i;
	for(i=0;i<numElements;i++){
		// alert(FORM.elements[i].type);
		if (FORM.elements[i].type != "hidden") {
			if(FORM.elements[i].getAttribute("required") == "true"){
			    if(!FORM.elements[i].value){
			        valid = false;
			        FORM.elements[i].style.backgroundColor = bgBad;
			        missingFields += FORM.elements[i].getAttribute("errormsg") + " \n";						
			    }
			    else{
			        FORM.elements[i].style.backgroundColor = bgGood;
			    }
			}
		}
	}

   // return proper validation status
   if(!valid) {
		alert(missingFields);
		return valid;
	}        
     

	// Begin Login Form Checks ------------------------------------------------------------------
  	if(FORM.name == "frmLogin"){
     	
		var storeno = FORM.storeno;
		var email = FORM.email;

		if((storeno.value.length == 0) && (email.value.length == 0)){
			valid = false;
			storeno.style.backgroundColor = bgBad; 
			email.style.backgroundColor = bgBad; 
			alert("Please enter a store id or company email.");
			email.focus();
			email.blur();
			email.select();
			return valid;
		}
	}
	// End Login Form Checks ------------------------------------------------------------------


	// Begin Add Company Form Checks ------------------------------------------------------------------
  	if(FORM.name == "frmAddCompany"){
     	
		var fname = FORM.companycontactfname;
		var lname = FORM.companycontactlname;
		var city = FORM.city;
		var zip = FORM.postalcode;
		var phone = FORM.companyphone;
		var fax = FORM.companyfax;
		var email = FORM.companyemail;
		var password = FORM.companypassword;
		var passwordconf = FORM.passwordconfirmation;
	
	   if(!(isNaN(fname.value)) && (fname.value != "")){
			valid = false;
			fname.style.backgroundColor = bgBad;
			alert("Your Contact First Name cannot contain numeric characters.");
			fname.focus();
			fname.blur();
			fname.select();
			return valid;
		}
		
		if(!(isNaN(lname.value)) && (lname.value != "")){
			valid = false;
			lname.style.backgroundColor = bgBad; 
			alert("Your Contact Last Name cannot contain numeric characters.");
			lname.focus();
			lname.blur();
			lname.select();
			return valid;
		}

		if (!(isNaN(city.value)) && (city.value != "")) {
			valid = false;
			city.style.backgroundColor = bgBad;
			alert("Your City cannot contain numeric characters.");
			city.focus();
			city.blur();
			city.select();
			return valid;
		}
		
		if (zip.value.length != 0 && zip.value.length < 5) {
			valid = false;
			zip.style.backgroundColor = bgBad;
			alert("Invalid Zip/Postal Code.");
			zip.focus();
			zip.blur();
			zip.select();
			return valid;
		}
	
		if (phone.value.length != 0 && phone.value.length < 7) {
			valid = false;
			phone.style.backgroundColor = bgBad;
			alert("Invalid phone number.");
			phone.focus();
			phone.blur();
			phone.select();
			return valid;
		}
	
		if (fax.value.length != 0 && fax.value.length < 7) {
			valid = false;
			fax.style.backgroundColor = bgBad;
			alert("Invalid fax number.");
			fax.focus();
			fax.blur();
			fax.select();
			return valid;
	   }
	   else{
	      fax.style.backgroundColor = bgGood;
		}
	
		// check email
		if(!(isEmail(email.value))){
			valid = false;
			email.style.backgroundColor = bgBad; 
			alert("Please enter a valid e-mail address.");
			email.focus();
			email.blur();
			email.select();
			return valid;
		}			
	
		// check password length
		if (password.value.length != 0 && password.value.length < 6) {
			valid = false;
			password.style.backgroundColor = bgBad;
			alert("Password must be at least 6 characters.");
			password.focus();
			password.blur();
			password.select();
			return valid;
		}
	
		// check password confirmation
		if (password.value != passwordconf.value) {
			valid = false;
			passwordconf.style.backgroundColor = bgBad;
			alert("Your password does not match your password confirmation.");
			passwordconf.focus();
			passwordconf.blur();
			passwordconf.select();
			return valid;
		}
	
	}
	// End Add Company Form Checks ------------------------------------------------------------------

	
	// Begin Add Store Form Checks ------------------------------------------------------------------
  	if(FORM.name == "frmAddStore"){
     	
		var fname = FORM.storecontactfname;
		var lname = FORM.storecontactlname;
		var city = FORM.city;
		var zip = FORM.postalcode;
		var phone = FORM.storephone;
		var fax = FORM.storefax;
		var email = FORM.storeemail;
	
	   if(!(isNaN(fname.value)) && (fname.value != "")){
			valid = false;
			fname.style.backgroundColor = bgBad;
			alert("Your Contact First Name cannot contain numeric characters.");
			fname.focus();
			fname.blur();
			fname.select();
			return valid;
		}
		
		if(!(isNaN(lname.value)) && (lname.value != "")){
			valid = false;
			lname.style.backgroundColor = bgBad; 
			alert("Your Contact Last Name cannot contain numeric characters.");
			lname.focus();
			lname.blur();
			lname.select();
			return valid;
		}

		if (!(isNaN(city.value)) && (city.value != "")) {
			valid = false;
			city.style.backgroundColor = bgBad;
			alert("Your City cannot contain numeric characters.");
			city.focus();
			city.blur();
			city.select();
			return valid;
		}
		
		if (zip.value.length != 0 && zip.value.length < 5) {
			valid = false;
			zip.style.backgroundColor = bgBad;
			alert("Invalid Zip/Postal Code.");
			zip.focus();
			zip.blur();
			zip.select();
			return valid;
		}
	
		if (phone.value.length != 0 && phone.value.length < 7) {
			valid = false;
			phone.style.backgroundColor = bgBad;
			alert("Invalid phone number.");
			phone.focus();
			phone.blur();
			phone.select();
			return valid;
		} else {
	      phone.style.backgroundColor = bgGood;
		}
	
		if (fax.value.length != 0 && fax.value.length < 7) {
			valid = false;
			fax.style.backgroundColor = bgBad;
			alert("Invalid fax number.");
			fax.focus();
			fax.blur();
			fax.select();
			return valid;
		} else {
	      fax.style.backgroundColor = bgGood;
		}
	
		// check email
		if(!(isEmail(email.value)) && (email.value != "")){
			valid = false;
			email.style.backgroundColor = bgBad; 
			alert("Please enter a valid e-mail address.");
			email.focus();
			email.blur();
			email.select();
			return valid;
		}			
	
	}
	// End Add Store Form Checks ------------------------------------------------------------------

	
	// Begin Add Attendee Form Checks ------------------------------------------------------------------
  	if(FORM.name == "frmAddAttendee"){
     	
		var fname = FORM.attendeefname;
		var lname = FORM.attendeelname;
		var phone = FORM.attendeephone;
		var mobile = FORM.attendeemobile;
		var email = FORM.attendeeemail;
	
	   if(!(isNaN(fname.value)) && (fname.value != "")){
			valid = false;
			fname.style.backgroundColor = bgBad;
			alert("First Name cannot contain numeric characters.");
			fname.focus();
			fname.blur();
			fname.select();
			return valid;
		}
		
		if(!(isNaN(lname.value)) && (lname.value != "")){
			valid = false;
			lname.style.backgroundColor = bgBad; 
			alert("Last Name cannot contain numeric characters.");
			lname.focus();
			lname.blur();
			lname.select();
			return valid;
		}

		// check email
		if(!(isEmail(email.value)) && (email.value != "")){
			valid = false;
			email.style.backgroundColor = bgBad; 
			alert("Please enter a valid e-mail address.");
			email.focus();
			email.blur();
			email.select();
			return valid;
		}			
	
		if (phone.value.length != 0 && phone.value.length < 7) {
			valid = false;
			phone.style.backgroundColor = bgBad;
			alert("Invalid phone number.");
			phone.focus();
			phone.blur();
			phone.select();
			return valid;
		} else {
	      phone.style.backgroundColor = bgGood;
		}
	
		if (mobile.value.length != 0 && mobile.value.length < 7) {
			valid = false;
			mobile.style.backgroundColor = bgBad;
			alert("Invalid mobile number.");
			mobile.focus();
			mobile.blur();
			mobile.select();
			return valid;
		} else {
	      mobile.style.backgroundColor = bgGood;
		}
	
	}
	// End Add Store Form Checks ------------------------------------------------------------------

	
	// Begin Change Password Form Checks ------------------------------------------------------------------
	if(FORM.name == "frmChangePW"){
		var current_pw = FORM.current_password;
		var password = FORM.new_password;
		var passwordconf = FORM.password_confirmation;
		
		// check password length
		if (password.value.length != 0 && password.value.length < 6) {
			valid = false;
			password.style.backgroundColor = bgBad;
			alert("Password must be at least 6 characters.");
			password.focus();
			password.blur();
			password.select();
			return valid;
		}
	
		// check password confirmation
		if (password.value != passwordconf.value) {
			valid = false;
			passwordconf.style.backgroundColor = bgBad;
			alert("Your password does not match your password confirmation.");
			passwordconf.focus();
			passwordconf.blur();
			passwordconf.select();
			return valid;
		}
	}
	// End Change Password Form Checks ------------------------------------------------------------------
	
   // post alert if not valid
   if(!valid) {
		alert(missingFields);
	}

   // return proper validation status
   return valid;

 }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function isEmail(str) {
	var foundOffset;
	foundOffset = str.indexOf("@",1)
	if (foundOffset == -1)
		return false;
	foundOffset = str.indexOf(".",3)
	if (foundOffset == -1)
		return false;
	foundOffset = str.lastIndexOf(".")
	if (foundOffset == (str.length-1))
		return false;
	return true;
}

