function isProper(string) {

   if (!string) return false;
   var iChars = "*|\":<>[]{}\;@$%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;
}

function ContactFormValidator(form){
	if (isProper(form.FName.value) == false) {
    	alert("Please enter your first name.");
    	form.FName.focus();
    return false;
    }
	if (isProper(form.LName.value) == false) {
    	alert("Please enter your last name.");
    	form.LName.focus();
    return false;
    }
	if (isProper(form.Clientid.value) == false) {
    	alert("Please enter your client id.");
    	form.Clientid.focus();
    return false;
    }
	
	return ValidateEmail();
}

function isNotMichigan(){
	if (document.forms[0].State[document.forms[0].State.selectedIndex].value == "MI"){
		alert('Note: To be eligible for this program you must be a resident \nof the United States excluding the state of Michigan.');
	}
}

function FormatPhoneNumber(string){
	var valid;
	if ((document.forms[0].State[document.forms[0].State.selectedIndex].value == "AA")||(document.forms[0].State[document.forms[0].State.selectedIndex].value == "AE")||(document.forms[0].State[document.forms[0].State.selectedIndex].value == "AP")||(document.forms[0].State[document.forms[0].State.selectedIndex].value == "AS")||(document.forms[0].State[document.forms[0].State.selectedIndex].value == "FM")||(document.forms[0].State[document.forms[0].State.selectedIndex].value == "GU")||(document.forms[0].State[document.forms[0].State.selectedIndex].value == "MH")){
	} else {
		if ((string.value.slice(3,4) == "-")||(string.value.slice(3,4) == ".")){
			valid = isNumber(string.value.slice(0,3));
			if (valid){
				valid = isNumber(string.value.slice(4,8));
			}
			if (string.value.length<8){
				valid = false;
			}
			if (valid){
				string.value = string.value.slice(0,3) + "-" + string.value.slice(4);
			}
		}else{
				valid = isNumber(string.value.slice(0,7));
				if (string.value.length < 7){
					valid = false;
				}
				if (valid==true){
					string.value = string.value.slice(0,3) + "-" + string.value.slice(3);
				}
		}
		if((string.length < 7)||(string.length > 9)){
			alert('Please provide a valide 7 digit phone number in this field.')
			string.focus();
		}
	}
}

function isNumber(sValue){
	var ret;
	var i;
	for (i=0; i <= sValue.length; i++){
		ret = isNaN(sValue.slice(i,i+1));

		if (ret == true){
			return false ;
		}
	}
	return true;

}

function ValidateEmail(){
		var form = document.forms[0].Email;
		var a;
		var i;
		var w;
		var x;
		var y;
		var z;
		var v;
		var u;
		var t;
		var s;
		var ichars;
		if (document.forms[0].Email){
			w = document.forms[0].Email.value.length - 1;
			x = document.forms[0].Email.value;
		}
		y = x.indexOf("@",0)
		z = x.indexOf(".",y)
		v = x.indexOf(".",y-1)
		u = x.indexOf("@",y+1)
		t = x.indexOf(".",0)
		s = x.indexOf(".",t+1)
		//First I check for invalid characters
		ichars = "~`!#$%^&*()=+|]}[{;:'?,<>*";
		for (i=0; i<=w; i++) {
			if (ichars.indexOf(x.charAt(i)) != -1){
				alert ('Please provide a valid e-mail address. Invalid characters found.')
				return false;
			}
		}
		
		//I check for White spaces
		if((x.indexOf(" ",0)==-1)&&(x.indexOf("@_"))){
			//Then I check to see if the @ sign and the period is present AND they are not the first character and it not duplicate
			if((y!=-1)&&(y!=0)&&(z!=-1)&&(u==-1)){
				//Now we check to see if the email address is the form of "jorge@.com"
				if((z-y!=1)&&(x.indexOf("..",0)==-1)){
					//We check to see if the email addres is the form of "jorge.@hello.net"
					if((y>v)||(t==s+1)){
						alert('Please provide a valid e-mail address.');
						form.focus();
						return false;
					}
					//Lastly we check to see if the period is the last part of the address given
					if(z != w){
					}
					else{
						alert('Please provide a valid e-mail address.');
						form.focus();
						return false;
					}
				}
				else{
					alert('Please provide a valid e-mail address.');
					form.focus();
					return false;
				}
			}
			else {
				alert('Please provide a valid e-mail address.');
				form.focus();
				return false;
			}
		}
		else {
			alert('Please remove any blank spaces from your email address.');
			form.focus();
			return false;
		}
		return true;
}
