// JavaScript Document

function val_email (email, c)
  {	
   var apos=email.value.indexOf("@");
   var dotpos=email.value.lastIndexOf(".");
    if (apos<1 || dotpos-apos <2 || email.value == null || email.value =="")
    {
	  var c = c + "Not a valid Email." + "\n";
	  email.style.background = 'Yellow';
	}else{
	  email.style.background = 'White';	
	}
	return c;
  }
  
function val_home_cell_phone (phone, c)
  {	
    var error = "";
    var stripped = phone.value.replace(/[\(\)\.\-\ ]/g, '');    
   if (phone.value == "") {
        error = "You didn't enter a phone number.";
        phone.style.background = 'Yellow';
		var c = c + error + "\n";
    }else if(isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.";
        phone.style.background = 'Yellow';
		var c = c + error + "\n";
    }else if((stripped.length < 10) || (stripped.length > 11)) {
        error = "The phone number is the wrong length.";
        phone.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		phone.style.background = 'White';	
	}
	return c;
  }
  
  
function val_work_phone (phone, c , sponsor)
  {	
    var error = "";
	if (sponsor = "sponsor"){
		var mid = "sponsors";
	}else if(sponsor = "work"){
		var mid = "work";
	}else if(sponsor = "supervisor"){
		var mid = "supervisor";
	}
    var stripped = phone.value.replace(/[\(\)\.\-\ ]/g, '');    
   if (phone.value == "") {
        error = "You didn't enter a " + mid + " phone number.";
        phone.style.background = 'Yellow';
		var c = c + error + "\n";
    }else if(isNaN(parseInt(stripped))) {
        error = "The " + mid + " phone number contains illegal characters.";
        phone.style.background = 'Yellow';
		var c = c + error + "\n";
    }else if((stripped.length < 10) || (stripped.length > 15)) {
        error = "The " + mid + " phone number is the wrong length. Make sure you included an area code.";
        phone.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		phone.style.background = 'White';	
	}
	return c;
  }

function val_name (name, c, error)
  {	
    if ((name.value.length < 2) || (name.value == null) || (name.value == ""))
    {
        name.style.background = 'Yellow';
		var c = c + error + "\n";
	}else{
		name.style.background = 'White';	
	}
	return c;
  }
  
  function val_enter (name, c, error)
  {	
    if ((name.value.length == 0) || (name.value == null) || (name.value == ""))
    {
        name.style.background = 'Yellow';
		var c = c + error + "\n";
	}else{
		name.style.background = 'White';	
	}
	return c;
  }
   
  function val_date(month, day, year, c)
  {	
  
    var error = "";
    var m_str = month.value.replace(/[\(\)\.\-\ ]/g, ''); 
	var d_str = day.value.replace(/[\(\)\.\-\ ]/g, ''); 
	var y_str = year.value.replace(/[\(\)\.\-\ ]/g, ''); 

   if ((month.value == "")||(isNaN(parseInt(m_str)))||(month.value >= 13)||(month.value <= 0)){
        error = "You didn't enter a valid month.";
        month.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		month.style.background = 'White';	
	}
	if ((day.value == "")||(isNaN(parseInt(d_str)))||(day.value >= 32)||(day.value <= 0)){
        error = "You didn't enter a valid day.";
        day.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		day.style.background = 'White';	
	}
	if ((year.value == "")||(isNaN(parseInt(y_str)))||(!(year.value.length == 4))){
         error = "You didn't enter a valid year.";
        year.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		year.style.background = 'White';	
	}
	return c;
  } 

  function val_state(state, c)
  {	
   if(state == ""){
	  var c = c + "Please select state." + "\n";	
	}
	return c;
  }
  
    function val_country(country, c, error)
  {	
   if(country == ""){
	  var c = c + error + "\n";	
	}
	return c;
  }

   function val_state_new(state, c, error)
  {	
    if ((state.value.length < 2) || (state.value == null) || (state.value == ""))
    {
        state.style.background = 'Yellow';
		var c = c + error + "\n";
	}else{
		state.style.background = 'White';	
	}
	return c;
  }
  

function val_zip(zip, c, error) 
{
     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
     if (!reZip.test(zip.value)){
	  var c = c + error + "\n";
	  zip.style.background = 'Yellow';
     }else{
		zip.style.background = 'White';	
	}
   return c;
}


function val_zip_new(zip, c) 
  {	

    if ((zip.value.length < 3) || (zip.value == null) || (zip.value == ""))
    {     
        zip.style.background = 'Yellow';
		var c = c + "Please enter correct zip code." + "\n";
	}else{
		zip.style.background = 'White';	
	}
	return c;
  }

  function val_k9sex(k9_sex, c)
  {	
    var dog = k9_sex.value.toLowerCase();
   if((dog != "m")&&(dog != "f")&&(dog != "male")&&(dog != "female")){
	  var c = c + "Please type sex of dog." + "\n";
	  k9_sex.style.background = 'Yellow';
	}else{
		k9_sex.style.background = 'White';	
	}
	return c;
  }
  
  function val_age(k9_age, c)
  {	
    var error = "";
    var age = k9_age.value.replace(/[\(\)\.\-\ ]/g, '');   
   if ((k9_age.value == "")||(isNaN(parseInt(age)))||(age.length >= 3)||(k9_age.value >= 20)||(k9_age.value <= 0)){
        error = "You didn't enter a valid K-9 age.";
        k9_age.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		k9_age.style.background = 'White';	
	} 
	return c;
  }  
  
  
   function val_add(add, c)
  {	
    var error = "";
	
   if ((add.value == "")||(add.value.length <= 2)){
        error = "You didn't not enter a valid address.";
        add.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		add.style.background = 'White';	
	}
	return c;
  } 
  
  function val_chk(check, c)
  {	
      var error = "";
      if((check.checked == false)||(check.checked == 0))
      {
        error = "Please check the Terms & Conditions box.";
        check.style.background = 'Yellow';
		var c = c + error + "\n";
      }
	  return c;
  }
  
    function val_radio(radio, c, error)
  {	
	 var chosen = "";
	 len = radio.length;
	 for (i = 0; i <len; i++) {
		if (radio[i].checked) {
		  chosen = radio[i].value
		}
     }
	  if (chosen == "") { 
		var c = c + error + "\n";
	  }
	  return c;
  }
  
  
   function val_ssn(ssn, c) {	
    var error = "";
    var stripped = ssn.value.replace(/[\(\)\.\-\ ]/g, '');   
   if (ssn.value == "") {
        error = "You didn't enter a social security number.";
        ssn.style.background = 'Yellow';
		var c = c + error + "\n";
    }else if(isNaN(parseInt(stripped))) {
        error = "The social security number contains illegal characters.";
        ssn.style.background = 'Yellow';
		var c = c + error + "\n";
    }else if(stripped.length != 9) {
        error = "The social security number is the wrong length..";
        ssn.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		ssn.style.background = 'White';	
	}
	return c;
  }
  
   function val_exp(y_exp, c, error) {	 
	var texp = y_exp.value.replace(/[\(\)\.\-\ ]/g, ''); 
   if ((texp.value == "")||(isNaN(parseInt(texp)))||(texp.value > 70)||(texp.value < 0)) {
        y_exp.style.background = 'Yellow';
		var c = c + error + "\n";
    }else{
		y_exp.style.background = 'White'
	}
	return c;
  }
  
  function val_confirm(confirm_f, c)
  {	
	  var noalpha = /^[a-zA-Z0-9]*$/;
	  
	if(confirm_f.value == ""){
        confirm_f.style.background = 'Yellow'; 
         c = c +"You didn't enter a confirmation #.\n -Enter only letters or numbers (no special characters).";
    }else if (!noalpha.test(confirm_f.value)){
        confirm_f.style.background = 'Yellow'; 
      c = c + "The confirmation # has wrong values.\n -Enter only letters or numbers (no special characters).";
    }else if(confirm_f.value.length <= 5){
        confirm_f.style.background = 'Yellow'; 
         c = c + "The confirmation # needs a minimum of six letters and/or numbers.\n -Enter only letters or numbers (no special characters).";
    } else {
		confirm_f.style.background = 'White'
	}
	return c;
  }
  
  
  
function validate_form_assoc(thisform)
{
with (thisform)
  {
   var c = "";  
   
   c = val_date(b_month, b_day, b_year, c);
   c = val_date(t_month, t_day, t_year, c);
   c = val_name(n_last, c, "Last name not valid.");
   c = val_name(n_first, c, "First name not valid.");
   c = val_add(h_add, c);
   c = val_name (h_city, c, "City name not valid.");
   c = val_state_new(h_state, c, "Home State/Province/Region code is not valid.");
   c = val_country($("#h_country").val(), c, "Select a home country.");
   c = val_zip_new(h_zip, c, "Home zip code is not valid.");

   c = val_email(email, c);
   c = val_name(sponsor, c, "Sponsor's name not entered.");

   c = val_name(an_last, c, "Assocciate last name not valid.");
   c = val_name(an_first, c, "Assocciate first name not valid.");
   c = val_add(ah_add, c);
   c = val_name(ah_city, c, "Assocciate city name not valid.");
   c = val_state_new(ah_state, c, "Associate State/Province/Region is not valid.");
   c = val_country($("#ah_country").val(), c, "Select an assocciate country.");
   c = val_zip_new(ah_zip, c, "Associate zip code is not valid.");  

   c = val_name(k9_name, c, "K-9's name not valid.");
   c = val_age(k9_age, c);
   c = val_k9sex(k9_sex, c);
   c = val_name(k9_breed, c, "K-9's breed not valid.");
   c = val_name(k9_color, c, "K-9's color not valid.");
   c = val_name(specialty, c, "Specialty needs to be entered.");
   c = val_confirm(confirm_f, c);
   c = val_chk(bk_chk, c);	
  }
  
    if(c != "") {
	alert(c);
	return false;
   }
}

function validate_form_reg(thisform)
{
with (thisform)
  {
   var c = "";  
   c = val_date(b_month, b_day, b_year, c);
   c = val_date(t_month, t_day, t_year, c);
   c = val_name(n_last, c, "Last name not valid.");
   c = val_name(n_first, c, "First name not valid.");
   c = val_add(h_add, c);
   c = val_name (h_city, c, "City name not valid.");
   c = val_state_new(h_state, c, "Home State/Province/Region is not valid.");
   c = val_country($("#h_country").val(), c, "Select a home country.");
   c = val_zip_new(h_zip, c, "Home zip code is not valid.");

   c = val_email(email, c);
   c = val_radio(RadioGroup1, c, "Check wether you are a Law Enforcement Officer.");
   c = val_radio(RadioGroup2, c, "Check wether you have arrest powers.");
   c = val_radio(RadioGroup3, c, "Check wether you and your K-9 USED by your Law Enforcement Employer.");
   c = val_name (le_dept, c, "Law Enforcement Department Employer Name not valid.");
   c = val_add(w_add, c);
   c = val_name (w_city, c, "City name not valid."); 
   c = val_state_new(w_state, c, "Work State/Province/Region is not valid."); 
   c = val_country($("#w_country").val(), c, "Select a work country.");
   c = val_zip_new(w_zip, c, "Associate zip code is not valid.");

   c = val_name(k9_name, c, "K-9's name not valid.");
   c = val_age(k9_age, c);
   c = val_k9sex(k9_sex, c);
   c = val_name(k9_breed, c, "K-9's breed not valid.");
   c = val_name(k9_color, c, "K-9's color not valid.");
   c = val_name(specialty, c, "Specialty needs to be entered.");
   c = val_confirm(confirm_f, c);
   
  }
  
    if(c != "") {
	alert(c);
	return false;
   }
   
 }

function validate_form_awards(thisform)
{
with (thisform)
  {
   var c = "";  
   c = val_date(t_month, t_day, t_year, c);
   c = val_name(n_last, c, "Last name not valid.");
   c = val_name(n_first, c, "First name not valid.");
   c = val_add(h_add, c);
   c = val_name (h_city, c, "City name not valid.");
   c = val_state_new(h_state, c, "Home State/Province/Region is not valid.");
   c = val_country($("#h_country").val(), c, "Select a home country.");
   c = val_zip_new(h_zip, c, "Home zip code is not valid.");

   c = val_email(email, c);
   
   c = val_name(k9_name, c, "K-9's name not valid.");
   c = val_age(k9_age, c);
   c = val_name(k9_breed, c, "K-9's breed not valid.");
   
   c = val_name(agency, c, "Need to enter agency name.");
   c = val_name(rank, c, "Need to enter agency rank.");
   c = val_add(a_add, c);
   c = val_name (a_city, c, "City name not valid.");
   c = val_state_new(a_state, c, "Agency State/Province/Region is not valid.");
   c = val_country($("#a_country").val(), c, "Select the agency's country.");
   c = val_zip_new(a_zip, c, "Agency zip code is not valid.");

   
   c = val_name(nom_last, c, "Last name not valid.");
   c = val_name(nom_first, c, "First name not valid.");

   c = val_add(nom_add, c);
   c = val_name (nom_city, c, "City name not valid.");
   c = val_state_new(nom_state, c, "Nominating person State/Province/Region is not valid.");
   c = val_country($("#nom_country").val(), c, "Select the nominating person's country.");
   c = val_zip_new(nom_zip, c, "Nominating person zip code is not valid.");
   c = val_email(nom_email, c);
   
   c = val_name(nom_agency, c, "Need to enter agency name.");
   c = val_name(nom_rank, c, "Need to enter agency rank.");
   c = val_add(nom_a_add, c);
   c = val_name (nom_a_city, c, "City name not valid.");
   c = val_state_new(nom_a_state, c, "Agency's State/Province/Region is not valid.");
   c = val_country($("#nom_a_country").val(), c, "Select the agency's country.");
   c = val_zip_new(nom_a_zip, c, "Agency's zip code is not valid.");

   
   c = val_add(del_add, c);
   c = val_name (del_city, c, "City name not valid.");
   c = val_state_new(del_state, c, "Delivery State/Province/Region is not valid.");
   c = val_country($("#del_country").val(), c, "Select a delivery country.");
   c = val_zip_new(del_zip, c);
   
   c = val_name(award, c, "Need to enter nominating award.");
   
   c = val_chk(bk_chk, c);
  }
  
    if(c != "") {
	alert(c);
	return false;
   }
 }
 
 
 function validate_form_explosives(thisform)
{
with (thisform)
  {
   var c = "";  
   
   c = val_name(name, c, "Name not valid.");
   c = val_radio(RadioGroup0, c, "Check wether you are an evaluator or handler.");
   c = val_ssn(ssn, c);
   c = val_name(agency, c, "Must enter an agency name.");
   c = val_name(sup_name, c, "Must enter supervisors name.");

   c = val_add(h_add, c);
   c = val_name (h_city, c, "City name not valid.");
   c = val_state_new(h_state, c, "Home State/Province/Region is not valid.");
   c = val_country($("#h_country").val(), c, "Select a home country.");
   c = val_zip_new(h_zip, c, "Home zip code is not valid.");
   c = val_email(email, c);


   c = val_radio(RadioGroup1, c, "Check shirt size..");
   c = val_name(k9_name, c, "K-9's name not valid.");
   c = val_age(k9_age, c);
   c = val_name(att_date, c, "Class date not entered.");
   c = val_name(alt_date, c, "Alternate date not entered.");
   c = val_exp(k9_exp, c, "You didn't enter years of experience.");
   c = val_enter(k9_work, c, "Types of K9 worked not entered.");
   c = val_enter(k9_train, c, "Types of K9 trained not entered.");
   c = val_exp(k9_num, c, "You didn't enter approx. # of K-9's trained.");
   c = val_date(c_month, c_day, c_year, c);
   c = val_date(n_month, n_day, n_year, c); 
   c = val_chk(bk_chk, c);
  }
  
    if(c != "") {
	alert(c);
	return false;
   }
 }
