
/*****************************************
*** Form validation and error handling ***
*****************************************/
  



/************* ERROR CHECKING ****************/

  function resetAlert(elem){
      errorNameToReset = elem + '_error';
	  if (document.getElementById(errorNameToReset)){
		document.getElementById(errorNameToReset).style.backgroundImage = 'none';
      }
	  else{
        alert("! Tamar Debug\r\r" + "Check document errors for: " + errorNameToReset)
      }
  }
 
  function resetAlerts(oarrInput){ 
    for (i=0; i < oarrInput.length; i++){
      errorNameToReset = oarrInput[i] + '_error';
	  if (document.getElementById(errorNameToReset)){
		document.getElementById(errorNameToReset).style.backgroundImage = 'none';
      }
	  else{
        alert("! Tamar Debug\r\r" + "Check document errors for: " + errorNameToReset)
      }
    }
  }
  
 
  function checkMail(oStringToCheck){
    var mailError = 0;
    var charError = 0;
    var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.@";
    //alert('checkIllegalChars')
	//alert(oStringToCheck);
    for (n = 0; n < oStringToCheck.length; n++){
      //alert("checking: " + checkOK.indexOf(oStringToCheck.charAt(n)) + ", val: " + oStringToCheck.charAt(n));
      if (checkOK.indexOf(oStringToCheck.charAt(n)) == -1){
        charError++;
      }
    }

    if (charError > 0){
      return true;
    } 

    var atSymbol = oStringToCheck.indexOf('@');
    var period = oStringToCheck.lastIndexOf('.')
    if (atSymbol == -1 || period == -1 || period==oStringToCheck.length){
      mailError++;
    }
    if (mailError > 0) {
      return true;
    } 
	else {
      return false;
    }
  }
 
 
 function checkRadio(oRadioToCheck){
    var radioError = 1
    for (r=0; r < oRadioToCheck.length; r++){
      if (oRadioToCheck[r].checked){
        radioError = 0;
        return;
      }
    }
    if (radioError) return true;
  }
  
 function checkRadioAgree(oRadioToCheck){
    var radioError = 1
    for (r=0; r < oRadioToCheck.length; r++){
      if (oRadioToCheck[r].checked){
	//alert(oRadioToCheck[r].value);
        if (oRadioToCheck[r].value == 'agree') {
          radioError = 0;
          return;
	} else {
	  return true;
	}
      }
    }
    if (radioError) return true;
  }

  function checkNumber(oStringToCheck){
    var myNumber = Number(oStringToCheck.replace(/(,|\.)/g, ""));
    if (isNaN(myNumber)) return true;
  }
  
  function checkIllegalChars(oStringToCheck){
    var charError = 0;
    var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'- @";
    //alert('checkIllegalChars')
      for (n = 0; n < oStringToCheck.length; n++){
        if (checkOK.indexOf(oStringToCheck.charAt(n)) == -1){
          charError++;
        }
      }
    if (charError > 0){
      return true;
    }else{
      return false;
    }
  }
  
  function checkPhoneNumber(oStringToCheck){
    var charError = 0;
    var checkOK = "0123456789";
    var myNumber = oStringToCheck.replace(/(,|\-|\(|\)|\+|\s)/g, "");
//    alert("Phone Numer : " + oStringToCheck + " : " + myNumber);
    for (n = 0; n < myNumber.length; n++){
      if (checkOK.indexOf(myNumber.charAt(n)) == -1 || myNumber.length < 10 || myNumber.length > 20){
        charError++;
      }
    }
    if (charError > 0){
      return true;
    }
	else{
      return false;
    }
  }
 
  function checkPasswordLength(oStringToCheck){
    var myNumber = oStringToCheck;
    if (myNumber.length < 5 || myNumber.length > 15){
        return true;
    }
    else {
        return false;
    }
  }

 
  function checkPostcodeError(oStringToCheck){
    var postcodeError = 0;

    // strip spaces & convert to upper case
    oStringToCheck = oStringToCheck.replace(/ /g, ""); 
    oStringToCheck = oStringToCheck.toUpperCase();

    size = oStringToCheck.length;

    // incode is the last 3 chars
    var incode  = oStringToCheck.substr(size-3, 3);
 
    // outcode is everything except the last 3 chars
    var outcode = oStringToCheck.substr(0, size-3);

    // incode must equal 9AA format
    if (incode.match(/[0-9][A-Z][A-Z]/)) { 
        //alert("incode success");
    } 
    else {
        //alert("incode failure");
        postcodeError++;
    }

    // incode must equal A9, A99, AA9, AA99, AA9A, A9A or AAA format
    if (outcode.match(/^[A-Z][0-9]$/)           ||
        outcode.match(/^[A-Z][0-9][0-9]$/)      ||
        outcode.match(/^[A-Z][A-Z][0-9]$/)      ||
        outcode.match(/^[A-Z][A-Z][0-9][0-9]$/) ||
        outcode.match(/^[A-Z][A-Z][0-9][A-Z]$/) ||
        outcode.match(/^[A-Z][0-9][A-Z]$/)      ||
        outcode.match(/^[A-Z][A-Z][A-Z]$/) ) {

        //alert("outcode success");
    }  
    else {
        //alert("outcode failure");
        postcodeError++;
    }

    if (postcodeError > 0){
      return true;
    } else{
      return false;
    }
  }
 
  function checkDay(oStringToCheck){
    var myNumber = Number(oStringToCheck)
    if (isNaN(myNumber) || myNumber <= 0 || myNumber > 31) return true;
  }
  
  function checkMonth(oStringToCheck){
    var myNumber = Number(oStringToCheck)
    if (isNaN(myNumber) || myNumber < 0 || myNumber > 12) return true;
  }
  
  function checkYear(oStringToCheck){
    var myNumber = Number(oStringToCheck)
    if (isNaN(myNumber) || myNumber < 00 || myNumber > 99) return true;
  }
  
  function checkFullYear(oStringToCheck){
    var myNumber = Number(oStringToCheck)
    if (isNaN(myNumber) || myNumber < 1700 || myNumber > 2010) return true;
  }

/*
  function checkFullHouseYear(oStringToCheck){
    var myNumber = Number(oStringToCheck)
    if (isNaN(myNumber) || myNumber < 1500 || myNumber > 2010) return true;
  }

  function checkWeek(oStringToCheck){
    var myNumber = Number(oStringToCheck)
    if (isNaN(myNumber) || myNumber < 0 || myNumber > 52) return true;
  }

  function checkCreditCardNumber(oStringToCheck){
    var myNumber = Number(oStringToCheck.replace(/(\s)/g, ""));
    if (isNaN(myNumber)) return true;
  }

  function checkTotalNumber(string1, string2, string3){
    var myNumber1 = Number(string1.replace(/(,|\.)/g, ""));
    var myNumber2 = Number(string2.replace(/(,|\.)/g, ""));
    var myNumber3 = Number(string3.replace(/(,|\.)/g, ""));
    var total = myNumber1 + myNumber2 + myNumber3;
    if (total < 1) return true;
  }

*/  
  function checkFutureDate(day,month,year) {
    var today = new Date();
    var strToday = today.getDate();
    var strMonth = today.getMonth() + 1;
    var strYear = today.getYear();
    if (strYear < 1000) {
      strYear = strYear + 1900;
    }

    if (year < strYear) {
      return true;
    } else if (strYear == year) {
      if (month < strMonth) {
        return true;
      } else if (strMonth == month) {
        if (day < strToday) {
          return true;
        }
      }
    }

    return false;
  }


  function checkPastDate(day,month,year) {
    var today = new Date();
    var strToday = today.getDate();
    var strMonth = today.getMonth() + 1;
    var strYear = today.getYear();
    if (strYear < 1000) {
      strYear = strYear + 1900;
    }

    if (year > strYear) {
      return true;
    } else if (strYear == year) {
      if (month > strMonth) {
        return true;
      } else if (strMonth == month) {
        if (day > strToday) {
          return true;
        }
      }
    }

    return false;
  }
/*

  function checkBirthDate(day,month,year) {
    
    var today = new Date();
    var strToday = today.getDate();
    var strMonth = today.getMonth() + 1;
    var strYear = today.getYear();
    if (strYear < 1000) {
      strYear = strYear + 1900;
    }
    
   
    
    if (year > strYear) {
      return true;
      
    } else if (strYear == year) {
      if (month > strMonth) {
        return true;
        
      } else if (strMonth == month) {
        if (day > strToday) {
          return true;
          
        }
      }
    }
    return false;
    
  }

  function checkOver21(day,month,year) {
    var today = new Date();
    var strToday = today.getDate();
    var strMonth = today.getMonth() + 1;
    var strYear = today.getYear();
    if (strYear < 1000) {
      strYear = strYear + 1900;
    }

    strYear = strYear - 21;
    if (year > strYear) {
      return true;
    } else if (strYear == year ) {
      if (month > strMonth) {
        return true;
      } else if (strMonth == month) {
        if (day > strToday) {
          return true;
        }
      }
    }

    return false;
  }

*/
  function checkValidDate(strToday,strMonth,strYear) {

    if (strMonth > 12 || strMonth < 1) {
      return true;
    }
    if ((strMonth == 1 || strMonth == 3 || strMonth == 5 
         || strMonth == 7 || strMonth == 8 || strMonth == 10 
         || strMonth == 12) && (strToday > 31 || strToday < 1)) {
      return true;
    }
    if ((strMonth == 4 || strMonth == 6 || strMonth == 9 
         || strMonth == 11) && (strToday > 30 || strToday < 1)) {
      return true;
    }
    if (strMonth == 2) {
      if (strToday < 1) {
         return true;
      }
      if (LeapYear(strYear) == true) {
        if (strToday > 29) {
          return true;
        }
      } else {
        if (strToday > 28) {
          return true;
        }
      }
    }
    return false;
  }


  function LeapYear(intYear) {
    if (intYear % 100 == 0) {
      if (intYear % 400 == 0) { return true; }
    } else {
      if ((intYear % 4) == 0) { return true; }
    }
    return false;
  } 


  function checkPastYear(year) {
    var today = new Date();
    var strYear = today.getYear();
    if (strYear < 1000) {
      strYear = strYear + 1900;
    }

    if (year > strYear) {
      return true;
    } else {
      return false;
    }
  }

/*

  function checkCreditCardExpiry(month, year) {
    // convert to int and add 2000
    year1 = Number(year);
    year1 = year1 + 2000;

    month1 = Number(month);

    var today = new Date();
    var todayMonth = Number(today.getMonth() + 1);
    var todayYear  = Number(today.getYear());

    if (todayYear < 1000) {
      todayYear = todayYear + 1900;
    }

    if (year1 > todayYear) {
      return false;
    } else if (todayYear == year1) {
      if (month1 > todayMonth) {
        return false;
      }
    }

    return true;
  }


  function checkEmpty(oStringToCheck){
    var charError = 0;
    if (oStringToCheck == ''){
      charError++;
    }
    if (charError > 0){
      return true;
    }
	else{
      return false;
    }
  }
 
  function checkNumberError(oStringToCheck){
    var charError = 0;
    var checkOK = "0123456789";
      for (n = 0; n < oStringToCheck.length; n++){
        if (checkOK.indexOf(oStringToCheck.charAt(n)) == -1 || oStringToCheck.length < 10 || oStringToCheck.length > 12){
          charError++;
        }
      }
    if (charError > 0){
      return true;
    }
	else{
      return false;
    }
  }
  
  function checkPastMonthYear(month, year) {
    var today = new Date();
    var strMonth = today.getMonth() + 1;
    var strYear = today.getYear();
    if (strYear < 1000) {
      strYear = strYear + 1900;
    }

    if (year > strYear) {
      return true;
    } else if (strYear == year) {
      if (month > strMonth) {
        return true;
      }
    }

    return false;
  }

  */
  
  
function showErrors(myErrorArray){
    for (t=0;t<myErrorArray.length; t++){
        var myErrorName = myErrorArray[t];
        if (document.getElementById){
       document.getElementById(myErrorName).style.visibility = 'visible';
       // document.getElementById(myErrorName).style.backgroundImage = 'url(/images/quote/error.gif)';
        }
    }
}

function showError(errorName){
    var myErrorName = errorName + '_error';
    //alert(myErrorName);
    if (document.getElementById){
        document.getElementById(myErrorName).style.visibility = 'visible';
        //document.getElementById(myErrorName).style.backgroundImage = 'url(/images/quote/error.gif)';
    }
}


  
   
  
  
 
