/*Basic Summary:

This script validates form fields. It validates when a form is submitted, and, if desired,
when each field is completed.

Simply call the function 'validate(id)' from the form while passing a variable indicating
what is to be validated.

e.g.
- onSubmit = "validate('form')"  (to check the entire form)
- onBlur = "validate('address')" or onBlur = "validate('zip_code')" (to check a field)
  Note: To validate per field, the variable being passed must match that field's 
  respective div id name in the html form document (e.g. 'address' or 'city').

-------------------------------------------------------------------------------------

*/

/*Global Variables*/

var correct_color = ""; /*Color for correct fields*/
var error_color = "#b90000"; /*Color for error fields*/
var error_msg = ""; /*Only displayed on submission*/
var msg_counter = 0; /*This variable exists to ensure that the message
            '-One or more fields were left empty.\n' only prints once.


/*This function is called from a form. The variable (id) being passed should match
the id of the div tag in the form for the field being referenced. */
function validate(id)
{
	/*valid determines whether or not the entire form is valid.
	Validation is assumed true unless found to be false.*/
    var valid = true;
 	
	/*Error display issues. No functionality*/
	if(id == "form") 
	{
		/*error_msg is wiped clean and builds only for form*/
		error_msg = "Your application form contains one or more errors. Please correct the fields that are indicated in red and resubmit your application.\n\n(Your errors are detailed below)\n\n"; 
    	msg_counter = 0;
    }
	
	/*the id represents whether only one field ('name') is being checked or all fields ('form')*/
	if(id == "first_name" || id == "form")
	{
		/*local_id set in case id is set to form. this variable spares
		rewriting the id out as so...getElementById(local_id)...*/
	     var local_id = "first_name";
	     var input = document.contact_form.first_name.value;
		 
		 /*Function validate_name checeks whether the user_input was acceptable, and then
		 changes the color of that field accordingly.*/
	     if(validate_name(input) == true)
		 {
		      document.getElementById(local_id).style.color=correct_color;
		      
		 }
		 else
		 {	 
		      document.getElementById(local_id).style.color=error_color;
			  valid = false; /*this is inconsequential if id != 'form'. */
		 }
  } 
  
	if(id == "last_name" || id == "form")
	{
		/*local_id set in case id is set to form. this variable spares
		rewriting the id out as so...getElementById(local_id)...*/
	     var local_id = "last_name";
	     var input = document.contact_form.last_name.value;
		 
		 /*Function validate_name checeks whether the user_input was acceptable, and then
		 changes the color of that field accordingly.*/
	     if(validate_name(input) == true)
		 {
		      document.getElementById(local_id).style.color=correct_color;
		 }
		 else
		 {	 
		      document.getElementById(local_id).style.color=error_color;
			  valid = false; /*this is inconsequential if id != 'form'. */
		 }
  }  
  
  if(id == "address" || id == "form")
    {
    	var local_id = "address";
    	var input = document.contact_form.address.value;
    	if(!(standard_display_status(input, local_id))){ valid = false; }
    }
	
	if(id == "city" || id == "form")
	{
		/*local_id set in case id is set to form. this variable spares
		rewriting the id out as so...getElementById(local_id)...*/
	     var local_id = "city";
	     var input = document.contact_form.city.value;
		 
		 /*Function validate_name checeks whether the user_input was acceptable, and then
		 changes the color of that field accordingly.*/
	     if(validate_city(input) == true)
		 {
		      document.getElementById(local_id).style.color=correct_color;
		 }
		 else
		 {	 
		      document.getElementById(local_id).style.color=error_color;
			  valid = false; /*this is inconsequential if id != 'form'. */
		 }
  	}  
  	 if(id == "state" || id == "form")
    {
    	var local_id = "state";
    	var input = document.contact_form.state.value;
    	if(!(standard_display_status(input, local_id))){ valid = false; }
    }
	
	if(id == "zip" || id == "form")
	{
		/*local_id set in case id is set to form. this variable spares
		rewriting the id out as so...getElementById(local_id)...*/
	     var local_id = "zip";
	     var input = document.contact_form.zip.value;
		 
		 /*Function validate_name checeks whether the user_input was acceptable, and then
		 changes the color of that field accordingly.*/
	     if(validate_UsCa_zip(input) == true)
		 {
		      document.getElementById(local_id).style.color=correct_color;
		 }
		 else
		 {	 
		      document.getElementById(local_id).style.color=error_color;
			  valid = false; /*this is inconsequential if id != 'form'. */
		 }
  	}  
	
	if(id == "phone" || id == "form")
	{
		/*local_id set in case id is set to form. this variable spares
		rewriting the id out as so...getElementById(local_id)...*/
	     var local_id = "phone";
	     var input = document.contact_form.phone.value;
		 
		 /*Function validate_name checeks whether the user_input was acceptable, and then
		 changes the color of that field accordingly.*/
		 
	     if(validate_phone(input, 1) == true)
		 {
		      document.getElementById(local_id).style.color=correct_color;
		      
		 }
		 else
		 {	 
		      document.getElementById(local_id).style.color=error_color;
			  valid = false; /*this is inconsequential if id != 'form'. */
		 }
  } 
  
   
    if(id == "email" || id == "form")
	{
	     var local_id = "email";
	     var input = document.contact_form.email.value;
		 
		 if(validate_email(input) == true)
		 {
		      document.getElementById(local_id).style.color=correct_color;
		      
		 }
		 else
		 {	 	
		      document.getElementById(local_id).style.color=error_color;
			  valid = false;
		 }
    }
	/*if(id == "band_name" || id == "form")
    {
    	var local_id = "band_name";
    	var input = document.contact_form.band_name.value;
    	if(!(standard_display_status(input, local_id))){ valid = false; }
    }
	if(id == "website" || id == "form")
	{
		var local_id = "website";
    	var input = document.contact_form.website.value;
    	if(!(standard_display_status(input, local_id))){ valid = false; }
  	} 
	if(id == "myspace" || id == "form")
	{
		var local_id = "myspace";
    	var input = document.contact_form.myspace.value;
    	if(!(standard_display_status(input, local_id))){ valid = false; }
  	} */
	if(id == "brand_model" || id == "form")
    {
    	var local_id = "brand_model";
    	var input = document.contact_form.brand_model.value;
		
		if(!(standard_display_status(input, local_id))){valid = false; }
		if(document.contact_form.brand_model.value == "Other" && is_empty(document.contact_form.other.value))
		{	
			document.getElementById("other").style.color = error_color;
		}
		else
		{
			document.getElementById("other").style.color = correct_color;
		}
    }
	
	if(id == "other" || id == "form")
    {
    	var local_id = "other";
    	var input = document.contact_form.other.value;
		
		if(document.contact_form.brand_model.value == "Other")
		{
    		if(!(standard_display_status(input, local_id))){valid = false; }
		}
    }
	
	if(id == "head_size" || id == "form")
    {
    	var local_id = "head_size";
    	var input = document.contact_form.head_size.value;
    	if(!(standard_display_status(input, local_id))){ valid = false; }
    }
	
	if(id == "mic_holes" || id == "form")
	{
	     var local_id = "mic_holes";
	     var input = document.contact_form.mic_holes.value;
		 
		 if(validate_isNumber(input))
		 {
			 if(input > 0)
			 {
				 if(is_empty(document.contact_form.mic_size.value))
				 {
			  		document.getElementById("mic_size").style.color = error_color;
			  	 }
				 if(is_empty(document.contact_form.mic_location.value))
				 {
				   	document.getElementById("mic_location").style.color = error_color;
			  	 }
				 document.getElementById(local_id).style.color=correct_color;
			 }
			 if(input == 0)
			 {
				document.contact_form.mic_size.value = "";
				document.contact_form.mic_location.value = "";
				document.getElementById("mic_size").style.color = correct_color;
				document.getElementById("mic_location").style.color = correct_color;
				document.getElementById(local_id).style.color=correct_color;
			 }
		}
		else
		{
			if(!is_empty(input))
			{
				document.getElementById("mic_holes").style.color = error_color;
			}
		}
	}
   
	
	if(id == "mic_size" || id == "form")
    {
    	var local_id = "mic_size";
    	var input = document.contact_form.mic_size.value;
		if(validate_isNumber(document.contact_form.mic_holes.value) && document.contact_form.mic_holes.value > 0)
		{
			if(!(standard_display_status(input, local_id))){ valid = false; }
		}
		if((is_empty(document.contact_form.mic_holes.value) || validate_isNumber(document.contact_form.mic_holes.value) == false) && is_empty(input) == false)
		{
			document.getElementById("mic_holes").style.color = error_color;
		}
		else
		{
			document.getElementById("mic_holes").style.color = correct_color;
		}
		if(is_empty(input)==false && document.contact_form.mic_holes.value == "0")
		{
			document.getElementById("mic_holes").style.color = error_color;
			valid = false; 
			error_msg = error_msg + "You cannot have 0 number of mic holes and have a size selected";
		}
    }
	
	if(id == "mic_location" || id == "form")
    {
    	var local_id = "mic_location";
    	var input = document.contact_form.mic_location.value;
		if(validate_isNumber(document.contact_form.mic_holes.value) && document.contact_form.mic_holes.value > 0)
		{
			if(!(standard_display_status(input, local_id))){ valid = false; }
		}
		if((is_empty(document.contact_form.mic_holes.value) || validate_isNumber(document.contact_form.mic_holes.value) == false) && is_empty(input) == false)
		{
			document.getElementById("mic_holes").style.color = error_color;
		}
		else
		{
			document.getElementById("mic_holes").style.color = correct_color;
		}
		if(is_empty(input)==false && document.contact_form.mic_holes.value == "0")
		{
			document.getElementById("mic_holes").style.color = error_color;
			valid = false; 
			error_msg = error_msg + "You cannot have 0 number of mic holes and have a size selected";
		}
    }
	
	if(id == "form")
	{
	     if(valid == true)
		 {
	         return true;
	     }
		 else
		 {
		  	 alert(error_msg);
		 	 return false;
		 }
	}	
	
}

<!-- METHODS BELOW !-->


//INTERNAL METHOD
function special_chars(input)
{
  var iChars = "!@#$%^&*()+=~`_-[]\\\';,./{}|\":<>?";

  for (var i = 0; i < input.length; i++) 
  {
  	if (iChars.indexOf(input.charAt(i)) != -1) 
  	{
  		return true;
  	}
  }
  return false;
}

//INTERNAL METHOD
function is_empty(input)
{
	if(input == "") { return true; }
	return false;
}

//INTERNAL METHOD
function isempty_msg()
{
	if(msg_counter == 0)
	{
		error_msg = error_msg + "-One or more fields were left empty.\n";
	}
	msg_counter++;
}

//METHOD
function validate_department()
{
	if(!document.contact_form.department[0].checked && !document.contact_form.department[1].checked && 
       !document.contact_form.department[2].checked && !document.contact_form.department[3].checked) 
    {
     	error_msg = error_msg + "-You didn't select the department for which you are applying.\n";
     	return false;
    }
    return true;
}

//METHOD
function validate_schedule()
{
	if(!document.contact_form.schedule[0].checked && !document.contact_form.schedule[1].checked && 
       !document.contact_form.schedule[2].checked && !document.contact_form.schedule[3].checked &&
       !document.contact_form.schedule[4].checked && is_empty(document.contact_form.schedule_other.value))
    {
    	error_msg = error_msg + "-You didn't specify your availability.\n";
     	return false;
	}
	return true;
}

//METHOD
function validate_notZero(input)
{
	if(!validate_isNumber(input)) 
	{
		return false;
	}
	if(input < 1) 
	{
		error_msg = error_msg + "-You must enter a number greater than 0 for number of mic. holes\n";
		return false
	}
	return true;
}

//METHOD 
function validate_isNumber(input)
{
	if(is_empty(input))
	{
		isempty_msg();
		return false;
	}
	if(isNaN(input))
	{
		error_msg = error_msg + "-contains non-numeric characters.\n";
		return false;
	}
	return true;
}

//METHOD
function validate_phone(input, required)
{ 
  phone_exception = "";
	var stripped = input.replace(/[\(\)\.\-\\* ]/g, '');
	//strip out acceptable non-numeric characters
	if(is_empty(input)) 
	{
		if(required == true) { isempty_msg(); return false; }
		else { return true; }
	}
	if (isNaN(parseInt(stripped))) 
	{
        error_msg = error_msg + "-Your phone/fax number contains non-numeric characters. " + phone_exception + "\n";
        return false;
	}
	if (!(stripped.length == 10)) 
	{	
		if(stripped.length < 10)
		{
	    	error_msg = error_msg + "-Your phone/fax number does not contain enough digits. Please don't forget to include your area code. " + phone_exception + "\n";
	    	return false;
	    }
	    if(stripped.length > 10)
	    {
	    	error_msg = error_msg + "-Your phone/fax number has too many digits. " + phone_exception + "\n";
	    	return false;
	    }
    }
    
    return true;
}

function validate_UsCa_zip(zip)
{
	if (zip.match(/^[0-9]{5}$/)) {
	return true;
	}
	zip=zip.toUpperCase();
	if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
	return true;
	}
	if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
	return true;
	}
	error_msg = error_msg + "-Your zip code was invalid";
	return false;
}

//METHOD
function validate_zip(field) 
{
	var valid = "0123456789-";
	var hyphencount = 0;
	if(is_empty(field)) 
	{
		isempty_msg();
		return false;
	}
	if (field.length!=5 && field.length!=10) 
	{
		error_msg = error_msg + "-Your zip code was invalid. Please enter your 5 digit (e.g. '12345') or 5 digit+4 zip code (e.g. '12345-6789').\n";
		return false;
	}
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			error_msg = error_msg + "-Your zip code contained invalid characters.\n";
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 	
		{
			error_msg = error_msg + "-Your zip code was invalid. The hyphen character should be used with a properly formatted 5 digit+four zip code (e.g. '12345-6789').\n";
			return false;
		}
	}
	return true;
}

//METHOD
function validate_state(input)
{ 
	if(is_empty(input)) { return false; }
	if(isNaN(input) == false)
	{
		error_msg = error_msg + "-The State provided contains numbers. Please use a two letter abbreviation or leave blank if this field does not apply to you.\n";
		return false;
	}
	if(special_chars(input))
	{
		error_msg = error_msg + "-The State provided contains special characters. Please use a two letter abbreviation for the state or leave blank if this field does not apply to you..\n";	
		return false;
	}
	if(input.length > 2) 
	{ 
		error_msg = error_msg + "-The State provided has too many characters. Please use a two letter abbreviation for the state or leave blank if this field does not apply to you..\n";
		return false;
	}
	if(input.length < 2)
	{
		error_msg = error_msg + "-The State provided has too few characters. Please use a two letter abbreviation for the state or leave blank if this field does not apply to you..\n";
		return false;
	}
	return true;
}

//METHOD
function validate_city(input)
{
	if(is_empty(input))
	{	
		isempty_msg();
		return false;	
	}
	if(!(isNaN(input)))
	{
		error_msg = error_msg + "-Your city of residence contains no letters.\n";
		return false;
	}
	return true;
}

//METHOD
function validate_address(input)
{
	if(is_empty(input)) 
	{ 
		isempty_msg();
		return false; 
	}
	
	return true;
}

//METHOD
function validate_country(input)
{
	if(is_empty(input)) 
	{ 
		isempty_msg();
		return false; 
	}
	return true;
}

//METHOD
function validate_website(input)
{
	var urlStr = input;
	if(is_empty(urlStr)) 
	{
		isempty_msg();
		return false;
	}
    
	if (urlStr.indexOf(" ") != -1) {
	error_msg = error_msg + "-Your website URL does and cannot contain spaces";
	return false;
	}
	
	urlStr=urlStr.toLowerCase();
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var atom=validChars + '+';
	var urlPat=/^http:\/\/(\w*)\.([\-\+a-z0-9]*)\.(\w*)/;
	var matchArray=urlStr.match(urlPat);
	
	if (matchArray==null) {
	error_msg = error_msg + "-Your webiste URL seems incorrect \ncheck it begins with http://\n and it has 2 .'s";
	return false;
	}
	
	var user=matchArray[2];
	var domain=matchArray[3];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
		error_msg = error_msg + "-Your website URL contains invalid characters.";
		return false;
		}
	}
	
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
		error_msg = error_msg + "-Your website URL contains invalid characters.";
		return false;
		}
	}
	
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat) == -1) {
		error_msg = error_msg + "-Your website URL does not seem to be valid.";
		return false;
		}
	}
	
	return true;
	
} 

//METHOD
function validate_email(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if(is_empty(str)) 
	{
		isempty_msg();
		return false;
	}
	if (str.indexOf(at)==-1)
	{
	    error_msg = error_msg + "-Your e-mail address as written is not a valid e-mail address.\n";
	    return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
	    error_msg = error_msg + "-Your e-mail address as written is not a valid e-mail address.\n";
	    return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
	   	error_msg = error_msg + "-Your e-mail address as written is not a valid e-mail address.\n";
	    return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
    {
	    error_msg = error_msg + "-Your e-mail address as written is not a valid e-mail address.\n";
	    return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
	    error_msg = error_msg + "-Your e-mail address as written is not a valid e-mail address.\n";
	    return false;
	}


	if (str.indexOf(dot,(lat+2))==-1)
    {
	     error_msg = error_msg + "-Your e-mail address as written is not a valid e-mail address.\n";
	     return false;
	}
		
	if (str.indexOf(" ")!=-1)
    {

     error_msg = error_msg + "-Your e-mail address as written is not a valid e-mail address.\n";
	 	 return false;
	}

    return true;					
}

//METHOD
function validate_name(input)
{
	if(is_empty(input))
	{
	     isempty_msg();
	     return false;
	}
	if(!(isNaN(input)))
	{
		error_msg = error_msg + "-Your name contains only numbers.\n";
		return false;
	}
	return true;
}

//METHOD
function validate_userinput(input)
{	
	if(is_empty(input))
	{	
		isempty_msg();
		return false;	
	}
	return true;	
}

//METHOD
function standard_display_status(input, local_id)
{
	
		if(validate_userinput(input) == true)
		{
		      document.getElementById(local_id).style.color=correct_color;
			  return true;
		}
		else
		{	
			  isempty_msg();
		      document.getElementById(local_id).style.color=error_color;
			  return false;
		}
}
