//********************************************************************************
//********************************************************************************
// Version 01.00 - 13/11/2007
//
// This file contains some routines specific to this application
//********************************************************************************
//********************************************************************************


//********************************************************************************
//    Author: Steve Betts
//      Date: Oct 2007
//      Desc: Validate the form fields.
//            This master function calls a series of sub-functions, each of which
//            checks a single form field.
//********************************************************************************
function ValidateForm(p_oForm, p_sAction) {

 if ( p_sAction == "10CHECK" ) {
    var why = "";
    why += CheckPanelNo(p_oForm.ePanelNo.value);
    why += CheckReason(p_oForm.eReason.value);
    why += CheckFromDate(p_oForm.eExcludeFrom.value);
    why += CheckToDate(p_oForm.eExcludeTo.value);
    why += CheckDateRange(p_oForm.eExcludeFrom.value, p_oForm.eExcludeTo.value);

    if (why != "") {
       alert(why);
       return false;
    };
 };


 if ( p_sAction == "30CHECK" ) {
    var why = "";
    why += checkDOB(p_oForm.eDOB1DD.value,p_oForm.eDOB1MM.value,p_oForm.eDOB1YY.value);

    if (why != "") {
       alert(why);
       return false;
    };
 };


 return true;
};


function CheckPanelNo (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);
 
 if ( sValue == "" ) {
    sError = "No panel number has been entered.\n";
 }
 else {
    if ( IsNumeric(sValue) ) {
       if ( ! IsInteger(sValue) ) {
          sError = "Panel number is not a valid format.\n";
       };
    } else {
       sError = "Panel number is not a valid format.\n";
    };
 };

 return sError;
};

function CheckReason (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = "Please choose an appropriate reason.\n";
 };

 return sError;
};

function CheckFromDate (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "dd/mm/yyyy") {
    sError = "No 'from date' has been selected.\n";
 };

 return sError;
};

function CheckToDate (p_sValue) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "dd/mm/yyyy") {
    sError = "No 'to date' has been selected.\n";
 };

 return sError;
};

function CheckDateRange (p_sFrom, p_sTo) {
 var sError = "";
 var sFrom = Trim(p_sFrom);
 var sTo = Trim(p_sTo);

 if ( (sFrom != "dd/mm/yyyy") && (sTo != "dd/mm/yyyy") ) {
    sFrom = sFrom.substr(6,4) + sFrom.substr(3,2) + sFrom.substr(0,2)
    sTo = sTo.substr(6,4) + sTo.substr(3,2) + sTo.substr(0,2)

    if ( parseInt(sTo, 10) < parseInt(sFrom, 10) ) {
       sError = "The 'to date' cannot be before the 'from date'.\n";
    };
 };

 return sError;
};

function checkDOB (p_sDD, p_sMM, p_sYY) {
 var sError   = "";
 var sDay;
 var sMonth;
 var sYear;

 sDay=Trim(p_sDD);
 sMonth=Trim(p_sMM);
 sYear=Trim(p_sYY);

 if ( (sDay.length==0) || (sMonth.length==0) || (sYear.length==0) ) {
    sError = "Date Of Birth has not been entered.\n";
 } else {
   if ( !(IsDate(p_sDD,p_sMM,p_sYY)) ) {
      sError = "Date Of Birth is not a valid format.\n";
   };
 };

 return sError;
}; 


//********************************************************************************
//    Author: Steve Betts
//      Date: Oct 2005
//      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){

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;
return true;
};


//********************************************************************************
//    Author: Steve Betts
//      Date: Oct 2005
//      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: Oct 2005
//      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: Nov 2007
//      Desc: BirthYear224
//            Converts a 2 digit birth year to 4 digits
//
// Amendment:
//********************************************************************************
function BirthYear224(obj){
 if ( obj.value.length == 2 ) {
    if ( IsInteger(obj.value) ) {
       obj.value='19'+obj.value;
       return;
    };
 };
};


