/*
 * Form Verifying JavaScript
 * @author: Michael Parsons
 * @date: 2/17/2009
 */
 
 var requiredFields = ["name",
					    "email",
						"phone",
						"house_style",
						"house_footage",
						"house_age",
						"kitchen_footage",
						"desired_kitchen_footage",
						"like_current",
						"num_adults",
						"num_children",
						"num_teens",
						"num_pets",
						"entertain_style[]",
						"secondary_activity[]",
						"cabinets[]",
						"new_kitchen_style[]",
						"appliances[]"];
 
 /*
  This function goes through the list of required fields and makes sure that they are appropiately
  filled out.  To add fields, and them to the list and then apply the appropiate verifying method to them
 */
 function verify_form(){
	 var notEmptyOk = true;
	 var emailOk = false;
	 var groupOk = true;
	 
	 //if any of the fields or groups that can't be empty is empty the variables will go to false and it will fail
	 //also if the e-mail isn't valid it will go to false and submit will not be allowed
	 for (var i = 0; i < requiredFields.length; i++){
			if (requiredFields[i] == "name" || requiredFields[i] == "phone" || requiredFields[i] == "house_style"
				|| requiredFields[i] == "house_footage" || requiredFields[i] == "house_age" || requiredFields[i] == "kitchen_footage"
				|| requiredFields[i] == "desired_kitchen_footage" || requiredFields[i] == "like_current" || requiredFields[i] == "num_adults" 
				|| requiredFields[i] == "num_children" || requiredFields[i] == "num_teens" || requiredFields[i] == "num_pets"){
				
				if (!notEmpty(requiredFields[i]))
					notEmptyOk = false;
			}else if(requiredFields[i] == "email"){
				emailOk = verifyEmail(requiredFields[i]);	
			}else{
				if (!groupOfOptions(requiredFields[i]))
					groupOk = false;
			}
	 }
	 
	 var submitButton = document.getElementById("submitButton");
	 
	 if (notEmptyOk && emailOk && groupOk){
		 submitButton.disabled = "";
	 }else{
		 submitButton.disabled = "disabled";
	 }
 }
 
 function notEmpty(id){
	 var element = document.getElementById(id);
	 
	 if (element.value == ""){
		 markFieldIncorrect(id);
		 return false;
	 }
	 
	 markFieldCorrect(id);
	 return true;
 }
 
 function verifyEmail(id){
	 var element = document.getElementById(id);
	 
	 if(!val_email(element.value)){
		 markFieldIncorrect(id);
		 return false;
	 }
	 
	 markFieldCorrect(id);
	 return true;
 }
 
 function groupOfOptions(id){
	 var boxes = document.getElementsByName(id);
	 
	 for(var i=0;i < boxes.length; i++){
		if (boxes[i].checked == true){
			markFieldCorrect(id);
			return true;
		}
	 }
	 
	 markFieldIncorrect(id);
	 return false;
 }
 
 function markFieldIncorrect(id){
	 var newId = id + "_stat";
	 document.getElementById(newId).innerHTML = "<span class=\"wrongField\">x</span>";
 }
 
 function markFieldCorrect(id){
	 var newId = id + "_stat";
	 document.getElementById(newId).innerHTML = "";
 }
 
 
 //taken from: http://scripts.franciscocharrua.com/validate-email-address.php
 function val_email(email_address)
 {
 //Assumes that valid email addresses consist of user_name@domain.tld
 at = email_address.indexOf('@');
 dot = email_address.indexOf('.');
 
 if(at == -1 || 
	dot == -1 || 
	dot <= at + 1 ||
	dot == 0 || 
	dot == email_address.length - 1)
	return(false);
	
 user_name = email_address.substr(0, at);
 domain_name = email_address.substr(at + 1, email_address.length);                  
 
 if(Validate_String(user_name) === false || 
	Validate_String(domain_name) === false)
	return(false);                     
 
 return(true);
 }
 
  //taken from: http://scripts.franciscocharrua.com/validate-email-address.php
function Validate_String(string, return_invalid_chars){
	valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	invalid_chars = '';
         
	if(string == null || string == '')
		return(true);
         
 //For every character on the string.   
 for(index = 0; index < string.length; index++)
	{
	char = string.substr(index, 1);                        
	
	//Is it a valid character?
	if(valid_chars.indexOf(char) == -1)
	  {
	  //If not, is it already on the list of invalid characters?
	  if(invalid_chars.indexOf(char) == -1)
		{
		//If it's not, add it.
		if(invalid_chars == '')
		   invalid_chars += char;
		else
		   invalid_chars += ', ' + char;
		}
	  }
	}                     
	
 //If the string does not contain invalid characters, the function will return true.
 //If it does, it will either return false or a list of the invalid characters used
 //in the string, depending on the value of the second parameter.
 if(return_invalid_chars == true && invalid_chars != '')
   {
   last_comma = invalid_chars.lastIndexOf(',');
   
   if(last_comma != -1)
	  invalid_chars = invalid_chars.substr(0, $last_comma) + 
	  ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
			  
   return(invalid_chars);
   }
 else
   return(invalid_chars == ''); 
 }