//********************************************************************************
//********************************************************************************
// Version 01.00 - 31/05/2006
//
// This file contains some routines specific to this application
//********************************************************************************
//********************************************************************************

//********************************************************************************
//    Author: Steve Betts
//      Date: May 2006
//      Desc: Validate the form fields.
//            This master function calls a series of sub-functions, each of which
//            checks a single form field.
//
// Amendment:
//********************************************************************************
function validate_form(p_oForm, p_sAction) {

 var nPtr

 if (p_sAction == "20SUBMIT") {
    var why = "";

    why += checkCountry(p_oForm.eCountry.value);
    why += checkTitle(p_oForm.eTitle.value);
    why += checkForename(p_oForm.eForename.value);
    why += checkSurname(p_oForm.eSurname.value);
    why += checkAddr1(p_oForm.eAddr1.value);
    why += checkAddr2(p_oForm.eAddr2.value);

    if ( (p_oForm.eCountry.value == 1) || (p_oForm.eCountry.value == 2) ) {
         why += checkPostcode(p_oForm.ePostcode.value);
    }

    why += checkHomePhone(p_oForm.eHomePhone.value);
    why += checkMobilePhone(p_oForm.eMobilePhone.value);
    why += checkWorkPhone(p_oForm.eWorkPhone.value);
    why += checkEmail(p_oForm.eEmail.value);
    why += checkGender(p_oForm.eGender[0].checked, p_oForm.eGender[1].checked);
    why += checkDOB(p_oForm.eDOBDD.value, p_oForm.eDOBMM.value, p_oForm.eDOBYYYY.value);
    why += checkFullTime(p_oForm.eFullTime[0].checked, p_oForm.eFullTime[1].checked);
    why += checkJob(p_oForm.eJob[0].checked, p_oForm.eJob[1].checked, p_oForm.eJob[2].checked, p_oForm.eJob[3].checked, p_oForm.eJob[4].checked, p_oForm.eJob[5].checked, p_oForm.eJob[6].checked, p_oForm.eJob[7].checked);
    why += checkInternet(p_oForm.eInternet[0].checked, p_oForm.eInternet[1].checked);
    why += checkHhSize(p_oForm.eHhSize[0].checked, p_oForm.eHhSize[1].checked, p_oForm.eHhSize[2].checked, p_oForm.eHhSize[3].checked, p_oForm.eHhSize[4].checked);
    why += checkMainShopper(p_oForm.eMainShopper[0].checked, p_oForm.eMainShopper[1].checked);
    why += checkAboutUs(p_oForm.eAboutUs.value, p_oForm.eAboutUsText.value);

    if (why != "") {
       alert(why);
       return false;
    }
 }
 return true;
};

function checkCountry (p_sOption) {
 var sError = "";

 if (p_sOption == "0") {
    sError = "Country has not been selected.\n";
 }
 return sError;
};       

function checkTitle (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = "Title is missing.\n";
 }
 else if (sValue.length < 2) {
   sError = "Title is insufficient.\n";
 }
 return sError;
};       

function checkForename (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = "Forename is missing.\n";
 }
 else if (sValue.length < 2) {
   sError = "Forename is insufficient.\n";
 }
 return sError;
};       

function checkSurname (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = "Surname is missing.\n";
 }
 else if (sValue.length < 2) {
   sError = "Surname is insufficient.\n";
 }
 return sError;
};       

function checkAddr1 (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = "Address Line 1 is missing.\n";
 }
 else if (sValue.length < 3) {
   sError = "Address Line 1 is insufficient.\n";
 }
 return sError;
};       

function checkAddr2 (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = "Address Line 2 is missing.\n";
 }
 else if (sValue.length < 3) {
   sError = "Address Line 2 is insufficient.\n";
 }
 return sError;
};       

function checkPostcode (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = "Postcode is missing.\n";
 }
 else if (sValue.length < 5) {
   sError = "Postcode is incomplete.\n";
 }
 return sError;
};       

function checkHomePhone (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = "Home Telephone Number is missing.\n";
 }
 else if (sValue.length < 10) {
   sError = "Home Telephone Number is incomplete.\n";
 }
 return sError;
};       

function checkMobilePhone (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if ((sValue != "") && (sValue.length < 10)) {
   sError = "Mobile Phone is incomplete.\n";
 }
 return sError;
};       

function checkWorkPhone (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if ((sValue != "") && (sValue.length < 10)) {
   sError = "Work Telephone Number is incomplete.\n";
 }
 return sError;
};       

function checkEmail (p_sValue) {
 var nValid = 0;
 var sError = "";
 var sValue = Trim(p_sValue);
  
 if (sValue == "") {
    sError = "Email address is missing.\n";
 }
 else {
    nValid = VerifyEmailFormat(p_sValue);
    if (nValid != 0) sError = "Email address has an invalid format.\n";
 }
 return sError;
};       

function checkGender (p_bValue1, p_bValue2) {
 var sError = "";

 if (!(p_bValue1) && !(p_bValue2)) {
    sError = "Gender question has not been answered.\n";
 }
 return sError;
};       

function checkDOB (p_sDD, p_sMM, p_sYYYY) {
 var sError = "";
 var sDD      = Trim(p_sDD);
 var sMM      = Trim(p_sMM);
 var sYYYY    = Trim(p_sYYYY);
 var dNow     = new Date();
 var nMinYear = 1900;
 var nMaxYear = dNow.getFullYear();

 if ((sDD == "") && (sMM == "") && (sYYYY == "")) {
    sError = "Date of Birth is missing.\n";
 }
 else {
    if ((sDD == "") || (sMM == "") || (sYYYY == "")) {
       sError = "Date of Birth is incomplete.\n";
    }
    else if (!(IsDate(sDD,sMM,sYYYY,nMinYear,nMaxYear))) {
       sError = "Date of Birth is invalid.\n";
    } 
 }
 return sError;
};       

function checkFullTime (p_bValue1, p_bValue2) {
 var sError = "";

 if (!(p_bValue1) && !(p_bValue2)) {
    sError = "Full-time education question has not been answered.\n";
 }
 return sError;
};       

function checkInternet (p_bValue1, p_bValue2) {
 var sError = "";

 if (!(p_bValue1) && !(p_bValue2)) {
    sError = "Internet access question has not been answered.\n";
 }
 return sError;
};       

function checkJob (p_bValue1, p_bValue2, p_bValue3, p_bValue4, p_bValue5, p_bValue6, p_bValue7, p_bValue8) {
 var sError = "";

 if (!(p_bValue1) && !(p_bValue2) && !(p_bValue3) && !(p_bValue4) && !(p_bValue5) && !(p_bValue6) && !(p_bValue7) && !(p_bValue8)) {
    sError = "Occupation question has not been answered.\n";
 }
 return sError;
};       

function checkHhSize (p_bValue1, p_bValue2, p_bValue3, p_bValue4, p_bValue5) {
 var sError = "";

 if (!(p_bValue1) && !(p_bValue2) && !(p_bValue3) && !(p_bValue4) && !(p_bValue5)) {
    sError = "Household Size question has not been answered.\n";
 }
 return sError;
};       

function checkMainShopper (p_bValue1, p_bValue2) {
 var sError = "";

 if (!(p_bValue1) && !(p_bValue2)) {
    sError = "Main shopper question has not been answered.\n";
 }
 return sError;
};       

function checkAboutUs (p_sOption, p_sText) {
 var sError = "";
 var sText  = Trim(p_sText);

 if (p_sOption == "X") {
    sError = "Hear about TNS question has not been answered.\n";
 }
 else {
    if (p_sOption != "3") {
       if (sText == "") {
          sError = "Hear about TNS (please specify) is missing.\n";
       }
       else if (sText.length < 5) {
          sError = "Hear about TNS (please specify) is insufficient.\n";
       }
    }
 }
 return sError;
};       


//********************************************************************************
//    Author: Steve Betts
//      Date: May 2006
//      Desc: NoEnter
//            Traps the [Enter] key to prevent a form from being submitted
//
// Amendment:
//********************************************************************************
function NoEnter() {
  return !(window.event && window.event.keyCode == 13); 
};


//********************************************************************************
//    Author: Steve Betts
//      Date: May 2006
//      Desc: IsDate
//            Returns true if the three elements of the date make up a valid date
//
// Amendment:
//********************************************************************************
function IsDate(p_sDay, p_sMonth, p_sYear, p_nMinYear, p_nMaxYear){

var nDaysInMonth=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
var sDay;
var sMonth;
var sYear;
var nDay;
var nMonth;
var nYear;


// --------------------------------------
// All elements must exist and be numeric
// --------------------------------------
sDay=Trim(p_sDay);
sMonth=Trim(p_sMonth);
sYear=Trim(p_sYear);
if ((sDay.length==0) || !(IsInteger(sDay)) || (sMonth.length==0) || !(IsInteger(sMonth)) || (sYear.length==0) || !(IsInteger(sYear))) return false;

// ------------------------
// Convert string to number
// ------------------------
nDay=parseInt(sDay,10);
nMonth=parseInt(sMonth,10);
nYear=parseInt(sYear,10);
if ((nDay==0) || (nMonth==0) || (nYear==0)) return false;

// --------------------------------------
// Adjust days in February for leap years
// --------------------------------------
if ((nYear % 4 == 0) && ((!(nYear % 100 == 0)) || (nYear % 400 == 0))) nDaysInMonth[2]=29;

// -----------------
// Validate elements
// -----------------
if (nMonth<1 || nMonth>12) return false;
if (nDay > nDaysInMonth[nMonth]) return false;
if (nYear<p_nMinYear || nYear>p_nMaxYear) return false;
return true;
}


//********************************************************************************
//    Author: Steve Betts
//      Date: May 2006
//      Desc: IsInteger
//            Returns true if string value is an integer
//
// Amendment:
//********************************************************************************
function IsInteger(p_sValue){
 var nPtr;
 var sChar;

 for (nPtr = 0; nPtr < p_sValue.length; nPtr++){   
   sChar = p_sValue.charAt(nPtr);
   if (((sChar < "0") || (sChar > "9"))) return false;
 }
 return true;
}


//********************************************************************************
//    Author: Steve Betts
//      Date: 11 Dec 2009
//      Desc: On a change in country hide/show postcode where appropriate
//
// Amendment:
//********************************************************************************
function onchange_eCountry(p_nCountry)
{
  var nElement;

  if ( (p_nCountry == 1) || (p_nCountry == 2) )
  {
    nElement = document.getElementById('ePostCodeCell');
    nElement.style.display = "inline";
    nElement.style.visibility = "visible";
  }
  else
  {
    nElement = document.getElementById('ePostCodeCell');
    nElement.style.display = "none";
    nElement.style.visibility = "hidden";
  };
};

