/*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 = "Oops! Your submission contains one or more errors. Please correct the fields in the form that are highlighted in red and resubmit your form.\n\nError Details:\n\n"; 

    	msg_counter = 0;

    }

	

	/*the id represents whether only one field ('name') is being checked or all fields ('form')*/

	if(id == "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 = "name";

	     var input = document.contact_form.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 == "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 == "referral" || id == "form")

    {

    	var local_id = "referral";

    	var input = document.contact_form.referral.value;

    	if(!(standard_display_status(input, local_id))){ valid = false; }

    }

    if(id == "message" || id == "form")

    {

    	var local_id = "message";

    	var input = document.contact_form.message.value;

    	if(!(standard_display_status(input, local_id))){ valid = false; }

    }

    

	if(id == "form")

	{

	     if(valid == true)

		 {

	         return true;

	     }

		 else

		 {

		  	 alert(error_msg);

		 	 return false;

		 }

	}	

	

}







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;

}



function is_empty(input)

{

	if(input == "") { return true; }

	return false;

}



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;

}



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;

}



function validate_phone(input)

{ 

	var stripped = input.replace(/[\(\)\.\-\\* ]/g, '');

	//strip out acceptable non-numeric characters

	if(is_empty(input)) 

	{

		isempty_msg();

		return false;

	}

	if (isNaN(parseInt(stripped))) 

	{

        error_msg = error_msg + "-Your phone number contains non-numeric characters.\n";

        return false;

	}

	if (!(stripped.length == 10)) 

	{	

		if(stripped.length < 10)

		{

	    	error_msg = error_msg + "-Your phone number does not contain enough digits. Please don't forget to include your area code.\n";

	    	return false;

	    }

	    if(stripped.length > 10)

	    {

	    	error_msg = error_msg + "-Your phone number has too many digits.\n";

	    	return false;

	    }

    }

    

    return true;

}



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;

}



function validate_state(input)

{ 

	if(is_empty(input)) { return true; }

	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;

}



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;

}



function validate_address(input)

{

	if(is_empty(input)) 

	{ 

		isempty_msg();

		return false; 

	}

	return true;

}



function validate_country(input)

{

	if(is_empty(input)) 

	{ 

		isempty_msg();

		return false; 

	}

	return true;

}



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;					

}



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;

}





function validate_userinput(input)

{

	if(is_empty(input))

	{	

		isempty_msg();

		return false;	

	}

	return true;	

}



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;

		}

}



function isempty_msg()

{

	if(msg_counter == 0)

	{

		error_msg = error_msg + "-One or more fields were left empty.\n";

	}

	msg_counter++;

}

