//--------------------------------------------------------------------------------------------

//General Javascript Form Validator

//What it should do

//Check every input when focus is changed to next input
//If error - Alert box with error instruction followed by highlighted background in text field after 2 attempts; Change background colour if mandatory and empty
//Check all fields again for values on submit and check for at least one subscription
//If error - Alert box with "no subscription" error and background color change

//INSTRUCTIONS

//1. Set the formname variable to the name of your form in the USER SET VARIABLES section at top of head section (See sample below)
//2. Set the highlight colour for incorrectly filled fields in hex with the variable "errorback" at top of head section
//3. Set the checkboxes that are NOT subscriptions into the subscription comparison loop (Search for INSTRUCTION1:) (Probably unnecessary)
//4. For every field that you want to check, insert the requisite call to the function that applies.
//   ie. in the fname text field you would put,

//   <input type="text" name="FNAME" size="20" ONBLUR="validateFname(this, true);">

//   The "this" parameter passes the form element info and the "true" means it's required, "false" for elective. The ONBLUR
//   method must be used.

//   Below are the function names and what they check for:

//   validateEmail(this,true)  -- Checks via a standard email pattern
//   validateFname(this,true)  -- Checks for 2 letters minimum, no numbers, puctuation
//   validateLname(this,true)  -- Checks for 2 letters minimum, no numbers, puctuation
//   validateAddress(this,true)  -- Checks for any value
//   validatepCode(this,false) -- Checks for Canada, UK and US 5 digit or 9 digit postal codes
//   validatePhone(this,false) -- Checks for minimum of 10 numbers allowing dashes, periods, brackets and spaces
//   validateDay(this,false)   -- Checks for value other than "0", " <option value=0 selected>DD " must be present
//   validateMonth(this,false) -- Checks for value other than "0", " <option value=0 selected>MM " must be present
//   validateYear(this,true)   -- Checks for 4 digits only
//   
//5. Set up "manfields" array variable with the mandatory field name like "LNAME" then add a comma, then the statement you would like to display
//   in the alert box if the field is empty in quotes. Finally, for each field, add the default value even if it's null but with 2 quotes. 
//   Don't forget about combo boxes etc or default strings such as "YYYY" for year. YOU MUST HAVE A DEFAULT VALUE THAT IS NOT LEGITIMATE, otherwise
//   there is no way to confirm input. Numeric values must be in quotes.

//6. For the submit button, <input type="submit" name="submit" value="Subscribe" onClick="return CheckCheckBoxes();"> ensuring onClick is used and return is there.

//--------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------

//USER SET VARIABLES (

//var formname = "EA_TR_WEB_FULL"; // Input name of form here (case sensitive)
//var errorback = "#FFCCCC"; // Input error highlight colour here

// Input all mandatory fields followed by their alert message and default value even if null, look for "0" or "YYYY" etc. eg.("fieldname","Your alert message","Defaultvalue")

//var manfields = new Array("BIRTHYEAR","You need to type in your year of birth","YYYY","BIRTHMONTH","You need to select your //birthmonth","0","PCODE","You need to type in your Postal/Zip code","","LNAME","You need to type in your last //name","","FNAME","You need to type in your first name","","EMAIL","You need to type in your email","")

//------------------------------------------------------------------------------------------


var regularback = "#FFFFFF";
var global_fieldval;	// retain field for timer thread
var countemailfocus = 0;
var countlnamefocus = 0;
var countfnamefocus = 0;
var countpcodefocus = 0;
var countphonefocus = 0;
var countyearfocus = 0;
var addfocus = 0;
var checksum = 0;
var curdate = new Date() // get this year
var year = curdate.getYear() // put year in a variable
if (year < 300){
	year = year + 1900;
	}



function CheckCheckBoxes()
{
countemailfocus = 2;
countlnamefocus = 2;
countfnamefocus = 2;
countpcodefocus = 2;
countphonefocus = 2;
countyearfocus = 2;
 if (checksum == 0)
 {


  for(var t = 0; t < manfields.length; t+=3)
  { 
   if (document.forms[formname].elements[manfields[t]].value == "" || document.forms[formname].elements[manfields[t]].value == manfields[t+2]) {
    alert(manfields[t+1]);
    document.forms[formname].elements[manfields[t]].style.backgroundColor = errorback;
    document.forms[formname].elements[manfields[t]].focus();
    return false;
    break;
   }
  }


 var a = 0
  for(var i = 0; i < document.forms[formname].length; i++)
  {
   var formElement = document.forms[formname].elements[i];

   // INSTRUCTION1: Input the name of Checkboxes that are NOT subscriptions below so the script doesn't count them as such

   if (formElement.type == "checkbox" && !(formElement.name == "SKIER" || formElement.name == "BOARDER" || formElement.name == "GOLFER" || formElement.name == "EMAILTYPE"))
   {
       if (formElement.checked == true)
   	 {
    	   a = 1;
   	 }
   }
  }
 if (a == -1)
   {
    alert("You must subscribe to at least one thing");
    return false;
   }
 return true;
} else {
 return false;
}
}

//////////////////////////////////////////////////////////////////////////////////////////////

function revertColor()
{
document.forms[formname].elements[global_fieldval.name].style.backgroundColor = regularback;
}

//////////////////////////////////////////////////////////////////////////////////////////////



function setFocus(addfocus)
{
	global_fieldval.style.backgroundColor = errorback;
	if (addfocus == 1) {
	  document.forms[formname].elements[global_fieldval.name].select();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////

function errorEmail() {

	  if (countemailfocus >= 2) {
	     countemailfocus = 0;
	     alert("There seems to be an error with your email address.\nThere seems to be an error with your email address. Please review it.");
	  }
 return true;
}



function validateEmail  (fieldval,   // element to be validated
                         required)   // true if required
{
  var emailpat = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i ;
  global_fieldval = fieldval;

  if (fieldval.value == "" && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     setTimeout( 'setFocus(0)', 100 );
    return false;
  }
    if (!emailpat.test(fieldval.value) && fieldval.value != "") {
     global_fieldval = fieldval;
     countemailfocus++;
     checksum = 1;
     errorEmail();
     setTimeout( 'setFocus(1)', 100 );
    return false;
  }
 revertColor();
 countemailfocus = 0;
 checksum = 0;
 return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////


function errorFname() {

	  if (countfnamefocus >= 2) {
	     countfnamefocus = 0;
	     alert("Make sure there are no spaces or periods.\nYou should your first name only.");
	  }
 return true;
}

function validateFname  (fieldval,   // element to be validated
                         required)   // true if required
{
  var fnamepat = /^([A-Za-z]{2,})$/;

  global_fieldval = fieldval;

  if (fieldval.value == "" && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     setTimeout( 'setFocus(0)', 100 );
    return false;
  }

  if (!fnamepat.test(fieldval.value) && fieldval.value != "") {
     global_fieldval = fieldval;
     countfnamefocus++;
     checksum = 1;
     errorFname();
     setTimeout( 'setFocus(1)', 100 );
    return false;
  }
 revertColor();
 countfnamefocus = 0;
 checksum = 0;
 return true;
}


/////////////////////////////////////////////////////////////////////////////////////////////


function errorLname() {

	  if (countlnamefocus >= 2) {
	     countlnamefocus = 0;
	     alert("Make sure there are no spaces or periods.\nYou should your last name only.");
	  }
 return true;
}

function validateLname  (fieldval,   // element to be validated
                         required)   // true if required
{
  var lnamepat = /^([A-Za-z]{2,})$/;

  global_fieldval = fieldval;

  if (fieldval.value == "" && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     setTimeout( 'setFocus(0)', 100 );
    return false;
  }

  if (!lnamepat.test(fieldval.value) && fieldval.value != "") {
     global_fieldval = fieldval;
     countlnamefocus++;
     checksum = 1;
     errorLname();
     setTimeout( 'setFocus(1)', 100 );
    return false;
  }
 revertColor();
 countlnamefocus = 0;
 checksum = 0;
 return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////



function validateAddress  (fieldval,   // element to be validated
                         required)   // true if required
{

  global_fieldval = fieldval;

  if (fieldval.value == "0" && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     global_fieldval.style.backgroundColor = errorback
    return false;
  }

 revertColor();
 checksum = 0;
 return true;
}


/////////////////////////////////////////////////////////////////////////////////////////////

function errorpCode() {

	  if (countpcodefocus >= 2) {
	     countpcodefocus = 0;
	     alert("Please double check your Zip/Postal code");
	  }
 return true;
}

function validatepCode  (fieldval,   // element to be validated
                         required)   // true if required
{
  var uspat = /^(\d{5}|(\d{5}(( |-)*\d{4}){1}))$/;
  var canpat = /^([A-Za-z]{1})([0-9]{1})([A-Za-z]{1})( |-)*([0-9]{1})([A-Za-z]{1})([0-9]{1})$/;
  var ukpat = /^([A-Za-z]{1,2})([0-9]{1,2})([A-Za-z]{0,1})( |-)*(\d{1}[A-Za-z]{2})$/;

  global_fieldval = fieldval;

  if (fieldval.value == "" && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     setTimeout( 'setFocus(0)', 100 );
    return false;
  }

  if (!uspat.test(fieldval.value) && !canpat.test(fieldval.value) && !ukpat.test(fieldval.value) && fieldval.value != "") {
     global_fieldval = fieldval;
     countpcodefocus++;
     checksum = 1;
     errorpCode();
     setTimeout( 'setFocus(1)', 100 );
    return false;
  }
 revertColor();
 countpcodefocus = 0;
 checksum = 0;
 return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////


function errorphone() {

	  if (countphonefocus >= 2) {
	     countphonefocus = 0;
	     alert("Please double check your phone number");
	  }
 return true;
}

function validatePhone  (fieldval,   // element to be validated
                         required)   // true if required
{
  var phonepat = /^(([0-9]|-| |\(|\)|\.){10,})$/;

  global_fieldval = fieldval;

  if (fieldval.value == "" && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     setTimeout( 'setFocus(0)', 100 );
    return false;
  }

  if (!phonepat.test(fieldval.value) && fieldval.value != "") {
     global_fieldval = fieldval;
     countphonefocus++;
     checksum = 1;
     errorphone();
     setTimeout( 'setFocus(1)', 100 );
    return false;
  }
 revertColor();
 countphonefocus = 0;
 checksum = 0;
 return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////


function validateDay  (fieldval,   // element to be validated
                         required)   // true if required
{

  global_fieldval = fieldval;

  if (fieldval.value == "0" && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     global_fieldval.style.backgroundColor = errorback
    return false;
  }

 revertColor();
 checksum = 0;
 return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////


function validateMonth  (fieldval,   // element to be validated
                         required)   // true if required
{

  global_fieldval = fieldval;

  if (fieldval.value == "0" && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     global_fieldval.style.backgroundColor = errorback
    return false;
  }

 revertColor();
 checksum = 0;
 return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////

function errorYear() {

	  if (countyearfocus >= 2) {
	     countyearfocus = 0;
	     alert("Please type the 4 digits of your year of birth.\nYou must be at least 14 to submit.");
	  }
 return true;
}

function validateYear  (fieldval,   // element to be validated
                         required)   // true if required
{
  var yearpat = /^(\d{4})$/;

  global_fieldval = fieldval;

  if ((fieldval.value == "" || fieldval.value == "YYYY") && checksum != 1 && required == true) {
     global_fieldval = fieldval;
     setTimeout( 'setFocus(0)', 100 );
    return false;
  }

  if ((!yearpat.test(fieldval.value) && (fieldval.value != "" && fieldval.value != "YYYY")) || ((year - (fieldval.value * 1)) > 200 || (year - (fieldval.value * 1)) < 15)) {
     global_fieldval = fieldval;
     countyearfocus++;
     checksum = 1;
     errorYear();
     setTimeout( 'setFocus(1)', 100 );
    return false;
  }

 revertColor();
 countyearfocus = 0;
 checksum = 0;
 return true;
}


/////////////////////////////////////////////////////////////////////////////////////////////

function errorMatureYear() {

    if (countyearfocus >= 2) {
        countyearfocus = 0;
        alert("Please type the 4 digits of your year of birth.\nYou must be at least 19 to submit.");
    }
    return true;
}

function validateMatureYear(fieldval,   // element to be validated
                         required)   // true if required
{
    var yearpat = /^(\d{4})$/;

    global_fieldval = fieldval;

    if ((fieldval.value == "" || fieldval.value == "YYYY") && checksum != 1 && required == true) {
        global_fieldval = fieldval;
        setTimeout('setFocus(0)', 100);
        return false;
    }

    if ((!yearpat.test(fieldval.value) && (fieldval.value != "" && fieldval.value != "YYYY")) || ((year - (fieldval.value * 1)) > 200 || (year - (fieldval.value * 1)) < 19)) {
        global_fieldval = fieldval;
        countyearfocus++;
        checksum = 1;
        errorMatureYear();
        setTimeout('setFocus(1)', 100);
        return false;
    }

    revertColor();
    countyearfocus = 0;
    checksum = 0;
    return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////

/*
Fahim Kanji Addition - 10/21/09.  
Validation needs more to it!
Extend it to include all-fields validation.
this function must be called onClick of the submit button.
These fields were identified and confirmed by Melanie Day (WB) on Oct. 21, 2009
as the complete set of regularly-required fields.

*/

function validateFormObjectIDs() {
    if (document.getElementById("EMAIL")) {
        if ((document.getElementById("EMAIL").value == "") ||
            (document.getElementById("FNAME").value == "") ||
            (document.getElementById("LNAME").value == "") ||
            (document.getElementById("PCODE").value == "") ||
            (document.getElementById("BIRTHMONTH").options[document.getElementById("BIRTHMONTH").selectedIndex].value == "MM") ||
            (document.getElementById("BIRTHYEAR").options ? document.getElementById("BIRTHYEAR").options[document.getElementById("BIRTHYEAR").selectedIndex].value == "YYYY" : false) ||
            (document.getElementById("BIRTHYEAR").value == "" || document.getElementById("BIRTHYEAR").value == "YYYY")) {
            alert("Please complete all required fields in your profile marked with an asterisk(*). Thank you.");
            return false;
        }
        else {
            return true;
        }
    }
    else {
        if ((document.getElementById("email").value == "") ||
            (document.getElementById("fname").value == "") ||
            (document.getElementById("lname").value == "") ||
            (document.getElementById("pcode").value == "") ||
            (document.getElementById("birthmonth").options[document.getElementById("birthmonth").selectedIndex].value == "MM") ||
            (document.getElementById("birthyear").options ? document.getElementById("birthyear").options[document.getElementById("birthyear").selectedIndex].value == "YYYY" : false) ||
            (document.getElementById("birthyear").value == "" || document.getElementById("birthyear").value == "YYYY")) {
            alert("Please complete all required fields in your profile marked with an asterisk(*). Thank you.");
            return false;
        }
        else {
            return true;
        }
    }
}

