
// this is the common file for the turkey website


// check content of form, the full name, e-mail address and
// phone number and url, etc
function form_verified(da_form) {

  var content_req_array = new Array();
  content_req_array[0] = da_form.guest_full_name.value;
  content_req_array[1] = da_form.guest_email.value;

/*
  content_req_array[1] = da_form.email.value;
  content_req_array[2] = da_form.registrar_title.value;
  content_req_array[3] = da_form.registrar_city.value;
  content_req_array[4] = da_form.state_province.value;
  content_req_array[5] = da_form.country.value;
  content_req_array[6] = da_form.home_Phone_Group.value;
  content_req_array[7] = da_form.home_Phone_Local.value;
  content_req_array[8] = da_form.area_Code.value;
  content_req_array[9] = da_form.state_province.value;
  content_req_array[10] = da_form.country.value;
  content_req_array[11] = da_form.registrar_last_name.value;
  content_req_array[12] = da_form.years.value;
*/

  for (i = 0; i < content_req_array.length; i++) {
    if (content_req_array[i] == "") {
      alert('You have not filled in all the required fields, please check and rekey.');
	  document.man_tky_guestbook_form.guest_full_name.focus();	      
	  return false;
    }
  }


  // recheck to make sure that the e-mail is correctly formated
  var this_valid_email = checkEMail(da_form.guest_email.value,1);
  if (!this_valid_email){
    return false;
  }

  // recheck the phone number to see if any error are still there
  var this_valid_area_code = checkAreaPhone(da_form.guest_area_Code.value);
  if (!this_valid_area_code) {
	return false;  
  }

  // recheck the phone number to see if any error are still there
  var this_valid_phone = checkPhone(da_form.guest_home_Phone_Group.value,1);
  if (!this_valid_phone) {
	return false;  
  }

  // recheck the phone number to see if any error are still there
  var this_valid_phone_local = checkPhoneLocal(da_form.guest_home_Phone_Local.value,1);
  if (!this_valid_phone_local) {
	return false;  
  }

  // recheck the fax phone number to see if any error are still there
  var this_valid_area_code = checkAreaPhone(da_form.guest_fax_area_Code.value);
  if (!this_valid_area_code) {
	return false;  
  }

  // recheck the fax phone number to see if any error are still there
  var this_valid_phone = checkPhone(da_form.guest_fax_home_Phone_Group.value,1);
  if (!this_valid_phone) {
	return false;  
  }

  // recheck the fax phone number to see if any error are still there
  var this_valid_phone_local = checkPhoneLocal(da_form.guest_fax_home_Phone_Local.value,1);
  if (!this_valid_phone_local) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.guest_full_name.value.toUpperCase(),1);
  if (!this_valid_text_entry) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.guest_address.value.toUpperCase(),1);
  if (!this_valid_text_entry) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.guest_city_prompt.value.toUpperCase(),1);
  if (!this_valid_text_entry) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.guest_comments_info.value.toUpperCase(),1);
  if (!this_valid_text_entry) {
	return false;  
  }

  // check the postal code, zip code area for proper data
  var this_valid_postal_zip = checkPostal(da_form.guest_postal_zip.value, da_form.guest_country.value);
  if (!this_valid_postal_zip) {
	return false;  
  }


/*
  // recheck the state province to see if any error are still there
  var this_valid_state_province = checkStateProvince(da_form.guest_state_province.value);
  if (!this_valid_state_province) {
	return false;  
  }

  // recheck the country to see if any error are still there
  var this_valid_country = checkStateProvince(da_form.guest_country.value);
  if (!this_valid_country) {
	return false;  
  }

  // recheck the year to make sure that it is entered correctly
  var this_valid_year = checkYears(da_form.years.value);
  if (!this_valid_year) {
	return false;  
  }
*/

}



// check the turkey contest entries for valid data
// certain fields have to be present
function tky_form_verified(da_form) {

  var tky_contest_req_array = new Array();
  tky_contest_req_array[0] = da_form.teacher_name.value;
  tky_contest_req_array[1] = da_form.school_name.value;
  tky_contest_req_array[2] = da_form.school_address.value;
  tky_contest_req_array[3] = da_form.phone_group.value;
  tky_contest_req_array[4] = da_form.phone_local.value;
  tky_contest_req_array[5] = da_form.student_email.value;
  tky_contest_req_array[6] = da_form.student_name.value;
  tky_contest_req_array[7] = da_form.student_grade.value;
  tky_contest_req_array[8] = da_form.recipe_name.value;
  tky_contest_req_array[9] = da_form.recipe_content.value;
  


  for (i = 0; i < tky_contest_req_array.length; i++) {
    if (tky_contest_req_array[i] == "") {
      alert('You have not filled in all the required fields, please check and rekey.');
	  document.turkey_contest.teacher_name.focus();	      
	  return false;
    }
  }
	
  // recheck the phone number to see if any error are still there
  var this_valid_grade = checkGrade(da_form.student_grade.value);
  if (!this_valid_grade) {
	return false;  
  }


  if (da_form.school_email.value != '') {
    // recheck to make sure that the e-mail is correctly formated
    var this_valid_email = checkEMail(da_form.school_email.value,2);
    if (!this_valid_email){
      return false;
    }
  }

  // recheck to make sure that the e-mail is correctly formated
  var this_valid_email = checkEMail(da_form.student_email.value,3);
  if (!this_valid_email){
    return false;
  }

  // check the postal code, zip code area for proper data
  var this_valid_postal_zip = check_Tky_postal(da_form.postal_code.value);
  if (!this_valid_postal_zip) {
	return false;  
  }

  // recheck the phone number to see if any error are still there for the phone group
  var this_valid_phone = checkPhone(da_form.phone_group.value,2);
  if (!this_valid_phone) {
	return false;  
  }

  // recheck the phone number to see if any error are still there for the phone local
  var this_valid_phone_local = checkPhoneLocal(da_form.phone_local.value,2);
  if (!this_valid_phone_local) {
	return false;  
  }
 
// check to make sure that the fax number has been entered 
  if (da_form.fax_group.value != '') {
  
    // recheck the phone number to see if any error are still there for the fax 
    var this_valid_phone = checkPhone(da_form.fax_group.value,2);
    if (!this_valid_phone) {
	  return false;  
    }

    // recheck the phone number to see if any error are still there
    var this_valid_phone_local = checkPhoneLocal(da_form.fax_local.value,2);
    if (!this_valid_phone_local) {
	  return false;  
    }
  }



// this area of the verification will check to make sure that no php commands have been
// sent down the pipe
  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.teacher_name.value.toUpperCase(),2);
  if (!this_valid_text_entry) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.school_name.value.toUpperCase(),2);
  if (!this_valid_text_entry) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.school_address.value.toUpperCase(),2);
  if (!this_valid_text_entry) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.student_name.value.toUpperCase(),2);
  if (!this_valid_text_entry) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.recipe_name.value.toUpperCase(),2);
  if (!this_valid_text_entry) {
	return false;  
  }

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextData(da_form.recipe_content.value.toUpperCase(),2);
  if (!this_valid_text_entry) {
	return false;  
  }

// this process will strip all of the double quotes off of the textarea
  var length_recipe_content = da_form.recipe_content.value.length;

  for(i=0;i<length_recipe_content;i++) {
    da_form.recipe_content.value = da_form.recipe_content.value.replace(/[\">]/,"&rdquo;");
  }

  for(i=0;i<length_recipe_content;i++) {
    da_form.recipe_content.value = da_form.recipe_content.value.replace(/[\'>]/,"&rsquo;");
  }

}




// check content of form, the full name, e-mail address and
// phone number and url, etc
function search_verified(search_form) {

  // recheck to see that they have not entered specific database commands in some fields
  var this_valid_text_entry = checkTextSearchData(search_form.search_info.value.toUpperCase());
  if (!this_valid_text_entry) {
	return false;  
  }

}


// this function will check the area phone number against a regular Expression
// to see if it is valid.  
function checkAreaPhone(phone_num) {
  var msg = "That is not a valid area code for the phone number. Please re-type it.";

  // Check to see if the phone number has been entered properly
  var checked_phone = new RegExp("[0-9][0-9][0-9]");

  if (phone_num != '') {
    // checking the phone number against a regular expression
    if (phone_num.match(checked_phone) != null) {
      return true;  
    } else {
      alert(msg);
	  document.man_tky_guestbook_form.guest_area_Code.focus();	
      return false;
    }
  }
  return true;
}


// this function will check the phone number group against a regular Expression
// to see if it is valid.  
function checkPhone(phone_num, phone_type) {
  var msg = "That is not a valid 3 number group phone number. Please re-type it.";

  // Check to see if the phone number has been entered properly
  var checked_phone = new RegExp("[0-9][0-9][0-9]");

  if (phone_num != '') {
    // checking the phone number against a regular expression
    if (phone_num.match(checked_phone) != null) {
      return true;  
    } else {
      alert(msg);
	  if (phone_type == 1) {
		document.man_tky_guestbook_form.guest_home_Phone_Group.focus();  
	  } else {
		document.turkey_contest.phone_group.focus(); 
	  }
	  	
      return false;
    }
  }
  return true;
}

// this function will check the phone number local against a regular Expression
// to see if it is valid.  
function checkPhoneLocal(phone_num, phone_type) {
  var msg = "That is not a valid 4 number local phone number. Please re-type it.";

  // Check to see if the phone number has been entered properly
  var checked_phone_Local = new RegExp("[0-9][0-9][0-9][0-9]");

  if (phone_num != '') {
    // checking the phone number against a regular expression
    if (phone_num.match(checked_phone_Local) != null) {
      return true;  
    } else {
      alert(msg);
	  if (phone_type == 1) {
	    document.man_tky_guestbook_form.guest_home_Pone_Local.focus();
	  } else {
		document.turkey_contest.phone_local.focus();  
	  }
      return false;
    }
  }
  return true;
}



// check to make sure that the person is in grades 5, 6, 7, 8 only.
// field already checked to make sure is not empty
function checkGrade(grade_entered) {
  var msg = "You have to be either in grade 5, 6, 7, or 8 to participate.";

  // Check to see if the phone number has been entered properly
  var checked_grade = new RegExp("[5-8]");

  if (grade_entered != '') {
    // checking the grade number against a regular expression
    if (grade_entered.match(checked_grade) != null) {
      return true;  
    } else {
      alert(msg);
	  document.turkey_contest.student_grade.focus();	
      return false;
    }
  }
  return true;
}



// this function will check the email information for the @ sign and the 
// period.  Also the @ sign could not be at the start of the email address
// or the period be anywhere near the end of the address.
function checkEMail(email_address, email_group) {
  // set up message variables
  var msg1 = 'You have entered an e-mail address with an improper "@", please rekey.';
  var msg2 = 'You have entered an e-mail address with an improper ".", please rekey.';

  if (email_address != '') {
    // check for the @ sign in the field
    if ((email_address.indexOf('@') == -1)||
        (email_address.charAt(0) == '@')||
	    (email_address.charAt(email_address.length-1) == '@')) {
      alert(msg1);

      switch (email_group) {
        case 1:
          document.man_tky_guestbook_form.guest_email.focus();
          break;
        case 2:
          document.turkey_contest.school_email.focus();
          break;
        default:
          document.turkey_contest.student_email.focus();
      }
/*
      if (email_group == 1) {
        document.man_tky_guestbook_form.guest_email.focus();
	  } else {
        document.turkey_contest.school_email.focus();
      }
*/
      return false;
    }
	
    // check for the "period" in the second portion of the info
    if ((email_address.indexOf('.') == -1)||
        (email_address.charAt(0) == '.')||
	    (email_address.charAt(email_address.length-1) == '.')||
  	    (email_address.charAt(email_address.length-2) == '.')) {
      alert(msg2);

      switch (email_group) {
        case 1:
          document.man_tky_guestbook_form.guest_email.focus();
          break;
        case 2:
          document.turkey_contest.school_email.focus();
          break;
        default:
          document.turkey_contest.student_email.focus();
      }


/*
	  if (email_group == 1) {
        document.man_tky_guestbook_form.guest_email.focus();
	  } else {
        document.turkey_contest.school_email.focus();
      }
*/
      return false;
    }
  }
  return true;	
}


// check to make sure that state province has been selected
// cannot leave it as the first entry in the select area
function checkStateProvince(state_Province) {

  // set up message variables
  var msg1 = 'You must select a state or province or country, please re-select.';
  var incorrect_entry = 'incorrect';

  if (state_Province == 'incorrect') {
	alert(msg1);
	return false;
  } else {
	return true;
  }

}


// check to make sure that the data entered in some text fields does not contain
// any commands that can be used against databases in mysql
function checkTextData(sText, return_page) {
	
  var error_holder = 0;

  var msg1 = 'You may not use certain command words such as SELECT in these fields, please re-key.';   
  var msg2 = 'You may not use certain command words such as DROP in these fields, please re-key.'; 
  var msg3 = 'You may not use certain command words such as DELETE in these fields, please re-key.'; 
  var msg4 = 'You may not use certain command words such as INSERT in these fields, please re-key.'; 
  var msg5 = 'You may not use certain command words such as LOAD DATA in these fields, please re-key.'; 
  var msg6 = 'You may not use certain command words such as REPLACE in these fields, please re-key.'; 
  var msg7 = 'You may not use certain command words such as UPDATE in these fields, please re-key.'; 
  var msg8 = 'You may not use certain command words such as SHOW in these fields, please re-key.'; 
  var msg9 = 'You may not use certain command words such as DESCRIBE in these fields, please re-key.'; 
  var msg10 = 'You may not use certain command words such as EXPLAIN in these fields, please re-key.'; 
  var msg11 = 'You may not use certain command words such as CHECK in these fields, please re-key.'; 
  var msg12 = 'You may not use certain command words such as ANALYZE in these fields, please re-key.'; 
  var msg13 = 'You may not use certain command words such as REPAIR in these fields, please re-key.';
  var msg14 = 'You may not use certain command words such as OPTIMIZE in these fields, please re-key.';  

  var select_text = new RegExp("SELECT");
  var drop_text = new RegExp("DROP");
  var delete_text = new RegExp("DELETE");
  var insert_text = new RegExp("INSERT");
  var load_text = new RegExp("LOAD DATA");
  var replace_text = new RegExp("REPLACE");
  var update_text = new RegExp("UPDATE");
  var show_text = new RegExp("SHOW");
  var describe_text = new RegExp("DESCRIBE");
  var explain_text = new RegExp("EXPLAIN");
  var check_text = new RegExp("CHECK");
  var analyze_text = new RegExp("ANALYZE");
  var repair_text = new RegExp("REPAIR");
  var optimize_text = new RegExp("OPTIMIZE");


// check to make sure that there is data in the field
  if (sText != '') {

    if (select_text.test(sText)) {
        alert(msg1);
		error_holder = 1;
	}
    if (drop_text.test(sText)) {
        alert(msg2);
		error_holder = 1;
	}
    if (delete_text.test(sText)) {
        alert(msg3);
		error_holder = 1;
	}
    if (insert_text.test(sText)) {
        alert(msg4);
		error_holder = 1;
	}
    if (load_text.test(sText)) {
        alert(msg5);
		error_holder = 1;
	}
    if (replace_text.test(sText)) {
        alert(msg6);
		error_holder = 1;
	}
    if (update_text.test(sText)) {
        alert(msg7);
		error_holder = 1;
	}
    if (show_text.test(sText)) {
        alert(msg8);
		error_holder = 1;
	}
    if (describe_text.test(sText)) {
        alert(msg9);
		error_holder = 1;
	}
    if (explain_text.test(sText)) {
        alert(msg10);
		error_holder = 1;
	}
    if (check_text.test(sText)) {
        alert(msg11);
		error_holder = 1;
	}
    if (analyze_text.test(sText)) {
        alert(msg12);
		error_holder = 1;
	}
    if (repair_text.test(sText)) {
        alert(msg13);
		error_holder = 1;
	}
    if (optimize_text.test(sText)) {
        alert(msg14);
		error_holder = 1;
	}

  }
  
// an error has occured, return to specific page
  if (error_holder != 0) {
	if (return_page == 1) {
      document.man_tky_guestbook_form.guest_full_name.focus();
	} else {
	  document.turkey_contest.teacher_name.focus();
	}
    return false;  
  } else {
    return true;
  }

  
}

// check to make sure that the data entered in some text fields does not contain
// any commands that can be used against databases in mysql
function checkTextSearchData(sText) {

  var msg1 = 'You may not use certain command words such as SELECT in these fields, please re-key.';   
  var msg2 = 'You may not use certain command words such as DROP in these fields, please re-key.'; 
  var msg3 = 'You may not use certain command words such as DELETE in these fields, please re-key.'; 
  var msg4 = 'You may not use certain command words such as INSERT in these fields, please re-key.'; 
  var msg5 = 'You may not use certain command words such as LOAD DATA in these fields, please re-key.'; 
  var msg6 = 'You may not use certain command words such as REPLACE in these fields, please re-key.'; 
  var msg7 = 'You may not use certain command words such as UPDATE in these fields, please re-key.'; 
  var msg8 = 'You may not use certain command words such as SHOW in these fields, please re-key.'; 
  var msg9 = 'You may not use certain command words such as DESCRIBE in these fields, please re-key.'; 
  var msg10 = 'You may not use certain command words such as EXPLAIN in these fields, please re-key.'; 
  var msg11 = 'You may not use certain command words such as CHECK in these fields, please re-key.'; 
  var msg12 = 'You may not use certain command words such as ANALYZE in these fields, please re-key.'; 
  var msg13 = 'You may not use certain command words such as REPAIR in these fields, please re-key.';
  var msg14 = 'You may not use certain command words such as OPTIMIZE in these fields, please re-key.';  

  var select_text = new RegExp("SELECT");
  var drop_text = new RegExp("DROP");
  var delete_text = new RegExp("DELETE");
  var insert_text = new RegExp("INSERT");
  var load_text = new RegExp("LOAD DATA");
  var replace_text = new RegExp("REPLACE");
  var update_text = new RegExp("UPDATE");
  var show_text = new RegExp("SHOW");
  var describe_text = new RegExp("DESCRIBE");
  var explain_text = new RegExp("EXPLAIN");
  var check_text = new RegExp("CHECK");
  var analyze_text = new RegExp("ANALYZE");
  var repair_text = new RegExp("REPAIR");
  var optimize_text = new RegExp("OPTIMIZE");


// check to make sure that there is data in the field
  if (sText != '') {

    if (select_text.test(sText)) {
        alert(msg1);
        document.search_site.search_info.focus();
        return false;
	}
    if (drop_text.test(sText)) {
        alert(msg2);
        document.search_site.search_info.focus();
        return false;
	}
    if (delete_text.test(sText)) {
        alert(msg3);
        document.search_site.search_info.focus();
        return false;
	}
    if (insert_text.test(sText)) {
        alert(msg4);
        document.search_site.search_info.focus();
        return false;
	}
    if (load_text.test(sText)) {
        alert(msg5);
        document.search_site.search_info.focus();
        return false;
	}
    if (replace_text.test(sText)) {
        alert(msg6);
        document.search_site.search_info.focus();
        return false;
	}
    if (update_text.test(sText)) {
        alert(msg7);
        document.search_site.search_info.focus();
        return false;
	}
    if (show_text.test(sText)) {
        alert(msg8);
        document.search_site.search_info.focus();
        return false;
	}
    if (describe_text.test(sText)) {
        alert(msg9);
        document.search_site.search_info.focus();
        return false;
	}
    if (explain_text.test(sText)) {
        alert(msg10);
        document.search_site.search_info.focus();
        return false;
	}
    if (check_text.test(sText)) {
        alert(msg11);
        document.search_site.search_info.focus();
        return false;
	}
    if (analyze_text.test(sText)) {
        alert(msg12);
        document.search_site.search_info.focus();
        return false;
	}
    if (repair_text.test(sText)) {
        alert(msg13);
        document.search_site.search_info.focus();
        return false;
	}
    if (optimize_text.test(sText)) {
        alert(msg14);
        document.search_site.search_info.focus();
        return false;
	}

  }
  return true;

  
}

// this function will check the postal code to make sure that they have
// entered the correct sequence of letters and numbers
function checkPostal(postal_Info, country) {


  // set up message variables
  var postal_mess2 = 'You have entered an incorrect postal code, please rekey.';
  var zip_code_mess = 'You have entered an incorrect zip code, please rekey';
  var country_mess = 'You have to select a country to process the postal code/zip code, please rekey';

  // set up the regular expression
  var checked_Postal_End = new RegExp("[A-Z][0-9][A-Z] [0-9][A-Z][0-9]");
  var checked_Start = new RegExp("[A-Z][0-9][A-Z]");
  var checked_End = new RegExp("[0-9][A-Z][0-9]");
  var checked_Zip_Code = new RegExp("[0-9][0-9][0-9][0-9][0-9]");

  postal_Info = postal_Info.toUpperCase();

  postal_type = postal_type.toUpperCase();

  postal_start = postal_type.substr(0,3);

  postal_middle = postal_type.substr(3,1);
  
// make sure that entry has data
  if (postal_middle != " ") {
    postal_end = postal_type.substr(3,3);
  } else {
	postal_end = postal_type.substr(4,3);  
  }

  if (postal_Info != '') { 


    if (country == 'Canada') {


	  if (postal_start.match(checked_Start) != null && postal_end.match(checked_End) != null ) {
	    return true;  
      } else {
        alert(postal_mess2);
        document.man_tky_guestbook_form.guest_postal_zip.focus();
        return false;
	  }	

 
	} else if (country == 'USA') {

      // checking the start postal against a regular expression
      if (postal_Info.match(checked_Zip_Code) != null) {
	    return true;  
      } else {
        alert(zip_code_mess);
        document.man_tky_guestbook_form.guest_postal_zip.focus();
        return false;
	  }

	} else {

	  // the country has not been selected 
      alert(country_mess);
      document.man_tky_guestbook_form.guest_country.focus();
      return false;		
	}

  }

  return true;
}


// check postal code entered in turkey contest 
function check_Tky_postal(postal_type) {

  // set up message variables
  var postal_mess2 = 'You have entered an incorrect postal code, please rekey.';

  // set up the regular expression
  var checked_Postal_End = new RegExp("[A-Z][0-9][A-Z] [0-9][A-Z][0-9]");
  var checked_Start = new RegExp("[A-Z][0-9][A-Z]");
  var checked_End = new RegExp("[0-9][A-Z][0-9]");
  
  postal_type = postal_type.toUpperCase();

  postal_start = postal_type.substr(0,3);

  postal_middle = postal_type.substr(3,1);
  
// make sure that entry has data
  if (postal_middle != " ") {
    postal_end = postal_type.substr(3,3);
  } else {
	postal_end = postal_type.substr(4,3);  
  }

  if (postal_type != '') {
	if (postal_start.match(checked_Start) != null && postal_end.match(checked_End) != null ) {
	  return true;  
    } else {
      alert(postal_mess2);
      document.turkey_contest.postal_code.focus();
      return false;
	}	
	
  }

  return true;  

}


// build a footer so that I only have to update this to make the footer the same on all pages
function create_footer() {

    var current_date = new Date().getFullYear();

    document.write('<p>Copyright '+current_date+'. Manitoba Turkey Producers. All rights Reserved.<br />Web site design and integration by <a href="http://www.swj.cc" target="_blank">SWJ Strategic Marketing &amp; Advertising</a></p>');
}




