/******************************************************************************
* validation.js                                                               *
*                                                                             *
* Provides functions for data validation                                      *
*                                                                             *
*                                                                             *
******************************************************************************/



// ********** checks for empty field
function FieldIsEmpty(ThisObject, ObjectString)
{
var strFieldIsEmpty = "You have not entered all the information required. Please enter";
 
  // field is blank
  if (ThisObject.value == "") 
  {
    alert(strFieldIsEmpty + " " + ObjectString);
    ThisObject.focus();
    return true;
  }
  return false;
}  // DataEmpty


// ********* checks for certain string in field
function FieldHasString(ThisObject, ObjectString, SearchString)
{
  // field is not blank
  if (ThisObject.value != "")
  {
    // contains the string???
    if (ThisObject.value.indexOf(SearchString) == -1)
    {
      alert(ObjectString + " does not contain " + SearchString + "!");
      ThisObject.focus();
      return false;
    } // if
  } // if
  return true;
} // FieldHasString

// ********* checks for number field
function FieldIsNumber(ThisObject, ObjectString)
{
  // field is not blank
  if (ThisObject.value != "")
  {
    // is it a number?
    if (isNaN(ThisObject.value))
    {
      alert(ObjectString + " is not a number!");
      ThisObject.focus();
      return false;
    }

  } // if
  return true;
}  // FieldIsNumber


// ********** checks field for max length
function MemoIsTooBig(ThisObject, ObjectString, ObjectMaxLength)
{
  var strLast80 = "";       
  // field is blank
  if (ThisObject.value.length > ObjectMaxLength)
  {
    strLast80 = ThisObject.value.substring(ObjectMaxLength - 80, ObjectMaxLength); 
    alert(ObjectString + " has exceeded " + ObjectMaxLength + " characters!" + 
          "\nLast 80 characters: " + strLast80);
    ThisObject.focus();
    return true;
  }
  return false;
}  // DataEmpty

// ***************** checks for date field
// ***************** format is dd/mm/yyyy
function FieldIsDate(ThisObject, ObjectString)
{
  // field is blank
  if (ThisObject.value != "")
  {
    // -------------------- dd/mm/yyyy has 10 chars
    if (ThisObject.value.length != 10)
    {
      alert(ObjectString + " [" + ThisObject.value + "] is not in dd/mm/yyyy format!");
      ThisObject.focus();
      return false;
    }

    // --------------------- dd/mm/yyyy has '/' char
    if (ThisObject.value.indexOf("/") == -1)
    {
      alert(ObjectString + " '/' missing, not in dd/mm/yyyy format!");
      ThisObject.focus();
      return false;
    }
    else
    {
      var tempstr = ThisObject.value;
      var pos = ThisObject.value.indexOf("/");

      // ------------------------ checks for the first '/'
      if (tempstr.indexOf("/") != 2)
      {
        alert(ObjectString + " '/' is not dd/mm/yyyy format!");
        ThisObject.focus();
        return false;
      }

      // strip the '/' and find the second one
      tempstr = tempstr.substring(3,11);
      pos = ThisObject.value.indexOf("/");

      // ---------------------- checks for the second '/'
      if (tempstr.indexOf("/") != 2)
      {
        alert(ObjectString + " '/' is not dd/mm/yyyy format!");
        ThisObject.focus();
        return false;
      }

    }

    // ------------------ yyyy needs to be a number and less than 3000
    var YearString = ThisObject.value.substring(6,11);
    if (isNaN(YearString))
    {
      alert(ObjectString + " [" + YearString + "] number of years (yyyy) is not a valid number!");
      ThisObject.focus();
      return false;
    }
    else
    if ((parseInt(YearString,10) < 1900) || (parseInt(YearString,10) > 3000))
    {
      alert(ObjectString + " [" + YearString + "] number of years (yyyy) is not valid!");
      ThisObject.focus();
      return false;
    }

    // ---------------------- mm needs to be a number and less than 13
    var MonthString = ThisObject.value.substring(3,5);
    if (isNaN(MonthString))
    {
      alert(ObjectString + " [" + MonthString + "] number of months (mm) is not a valid number!");
      ThisObject.focus();
      return false;
    }
    else
    if ((parseInt(MonthString,10) < 1) || (parseInt(MonthString,10) > 12))
    {
      alert(ObjectString + " [" + MonthString +"] number of months (mm) is not valid!");
      ThisObject.focus();
      return false;
    }

    // ---------------------- dd needs to be a number and less than 32
    var DayString = ThisObject.value.substring(0,2);
    if (isNaN(DayString))
    {
      alert(ObjectString + " [" + DayString + "] number of days (dd) is not a valid number!");
      ThisObject.focus();
      return false;
    }
    else
    if ((parseInt(DayString,10) < 1) || (parseInt(DayString,10) > 31))
    {
      alert(ObjectString + " [" + DayString + "] number of days (dd) is not valid!");
      ThisObject.focus();
      return false;
    }

    // ---------------------- IS THIS A VALID DATE???? e.g. 30/02/2000
    var MonthInt = parseInt(MonthString,10);
    var DayInt = parseInt(DayString,10);
    var YearInt = parseInt(YearString,10);

    // look at the month
    switch(MonthInt)
    {
      case 4:
      case 6:
      case 9:
      case 11:
        if (DayInt > 30)
        {
          alert(ObjectString + " [" + DayString + "] number of days (dd) is greater than 30 days!");
          ThisObject.focus();
          return false;
        }
        break;
      case 2:
        if (IsLeapYear(YearInt))
        {
          // leap year
          if (DayInt > 29)
          {
            alert(ObjectString + " [" + DayString + "] number of days (dd) is greater than 29 days!");
            ThisObject.focus();
            return false;
          }
        }
        else
        {
          // standard
          if (DayInt > 28)
          {
            alert(ObjectString + " [" + DayString + "] number of days (dd) is greater than 28 days!");
            ThisObject.focus();
            return false;
          }
        }
        break;
      default: // the rest are 31 days
    } // switch
  }
  return true;
}  // DataEmpty


// A leap year is divisible by 4 and (not divisible by 100 or divisible by 400)
function IsLeapYear (year)
{
    return (year % 4 == 0) &&
           ((year % 100 != 0) || (year % 400 == 0));
}


// find ' and " in the string and replace with `
function ReplaceQuotes(txt)
{
   var str = txt;
   var singlequote = /'/g;
   var doublequote= /"/g;
   str = str.replace(singlequote,"''");
   str = str.replace(doublequote,"`");
   return(str);
}

// check valid phone
function FieldIsPhone(ThisObject, ObjectString)
{
//var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
var strFieldIsPhone = "The phone number entered contains invalid characters. Please try again";
   
    // is it a number?
    if (isNaN(ThisObject.value))
    {
       var plusstr = /\+/g;
       var minusstr = /\-/g;
       var lbstr = /\(/g;
       var rbstr = /\)/g;
       var spstr = /\s/g;
       
       var stripstr = ThisObject.value;

       // remove all occurance of +, -, (, ), spaces 
       stripstr = stripstr.replace(plusstr, "");
       stripstr = stripstr.replace(minusstr, "");
       stripstr = stripstr.replace(lbstr, "");
       stripstr = stripstr.replace(rbstr, "");
       stripstr = stripstr.replace(spstr, "");

       // we are left with only numbers to check
       if (isNaN(stripstr) == false)
       {
          return true;
       }
       else
       {
        alert(strFieldIsPhone);
          ThisObject.focus();
          return false;
       }
    }
    
  return true;
}

// ********* checks for certain invalid string in field
function FieldIsEmail(ThisObject, ObjectString)
{
var strFieldIsEmail = "Entered information does not contain a recognised email address";

	var TempStr = ThisObject.value.toUpperCase();
	//Regular Expression
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;	
	if (filter.test(TempStr)) 
	{
		return true;	
	}
	else
	{
		alert(strFieldIsEmail);
		ThisObject.focus();
		return false;	
	}  
}

// ********* checks for certain string in field
function FieldsAreIdentical(FromObject, ToObject, ObjectString)
{
var strFieldsAreIdentical = "Entered information are not identical";
  // field is not blank
  if (FromObject.value != "")
  {
    // contains the string???
    if (FromObject.value != ToObject.value)
    {
      alert(ObjectString + " " + strFieldsAreIdentical);
      FromObject.focus();
      return false;
    } // if
  } // if
  return true;
} // FieldHasString



