function AmountGreaterThanEquals(fValue1, fValue2) 
{
	
	// checks if the first value is less than the second by asking confirmation from
	// the user - special case scenario
	
	if (fValue1.length == 0)
	{
		return true;
	}

	if (fValue2.length == 0)
	{
		return true;
	}

	var fV1 = parseFloat(fValue1);
	var fV2 = parseFloat(fValue2);
	
	if ( fV2 > fV1)
		{
		var result;
		result = confirm('Invoice amount greater than run rate. Is this correct?');
	
		if (result == true)
			{
			return true;
			}
		else 
			{
			return false;
			}
		}
		
	return true;
}


function IsPresent(a_sCheckString)
{
   return a_sCheckString.length > 0;
}

function IsFilledIn(a_sCheckString)
{
	return Trim(a_sCheckString).length > 0;
}

function LengthBetween(a_sCheckString, a_iMinLength, a_iMaxLength)
{
  return a_sCheckString.length >= a_iMinLength && a_sCheckString.length <= a_iMaxLength;
}

function ContainsCharacters(a_sCheckString, a_sValidCharacters)
{
  return true;
}

function MatchExpression(a_sCheckString, re)
{
	if (a_sCheckString.length == 0)
	{
		return true;
	}

	var arr = re.exec(a_sCheckString);
	if (arr == null)
	{    return false;
	}
	else
	{    return true;
	}
}

function Trim(a_sText)
{
	var retValue = "" + a_sText;
	var ch = retValue.substring(0, 1);

	
	while (ch == " ") 
	{ 
	  // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
	}
    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == " ") 
    { 
      // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
    }
    

	return retValue;
}


// BOI, followed by one or more digits, followed by EOI.
var reInteger = /^\d+$/

function IsInteger(a_fldField)
{
    var sValue = Trim(a_fldField.value);
	var bIsOK = reInteger.test(sValue);
	return bIsOK;
}
// d1 Keep Seneschal Planning change.

function IsNumeric(str) {
	var j=0;
	if (str.substring(0,1)=="-"){
		j=1;
		}
	for (var i = j; i < str.length; i++) {
		var ch = str.substring(i, i+1);
		if (ch < "0" || ch > "9" || str.length == null) {
			return false;
		}
	}
	return true;
}

function IsNumber(a_sCheckString, a_bIsDecimal, a_iMin, a_iMax)
{

  var checkOK = "0123456789.";
  var bIsValid = true;
  var bHasDecimal = false;
  var iDigitsAfterDecimal = 0;

if (a_sCheckString.length != 0)
  {
  for (i = 0;  i < a_sCheckString.length;  i++)
  {
    var ch = a_sCheckString.charAt(i);

    for (j = 0;  j < checkOK.length;  j++)
    {
      if (ch == checkOK.charAt(j))
        break;
    }

    if (ch == ".")
    {
      if (bHasDecimal || !a_bIsDecimal)
      {
        bIsValid = false;
        break;
      }
      else
      {
        bHasDecimal = true;
      }
    }

    if (j == checkOK.length)
    {
      if (ch == "-" && i == 0)
        bIsValid = true;
      else
      {
        bIsValid = false;
        break;
      }
    }
    else
    {
      if (bHasDecimal)
      {
        iDigitsAfterDecimal++;
      }
    }
   }
  
  

  // If the number is an integer then test to see it is between the min and max.

  if (bIsValid)
  {
    var iValue = parseInt(a_sCheckString);

    if (a_iMin && iValue < parseInt(a_iMin))
    {
      bIsValid = false;
    }

    if (a_iMax && iValue > parseInt(a_iMax))
    {
      bIsValid = false;
    }
  }

  return bIsValid;
}
return bIsValid;
}

function IsSelected(index)
//Checks a selection has been made in a drop down list
{
if (index <= 0)
  {
  return false;
  }
return true;
}

///////// Date Validation //////////

function IsDate(s)
// Returns true if a string can be converted to a date or if the string is blank

{
	if (s.length == 0)
	{
		return true;
	}
	else
	{
		// Convert the string to a javascript date.
		var iIndexSlash1 = s.indexOf('/');
		if ( iIndexSlash1 < 1 )
		{
			return false;
		}
		// Have X../..
		
		var iIndexSlash2 = s.indexOf('/', iIndexSlash1 + 1);
		if ( iIndexSlash2 == -1 || (iIndexSlash2 - iIndexSlash1) < 2 )
		{
			return false;
		}
		// Have X../X../..
		
		var iIndexEnd = s.length - 1;
		if ( iIndexEnd - iIndexSlash2 < 1 )
		{
			return false;
		}
		// Have X../X../X..
		
		// Substring works from start index to end index - 1
		var sDay = s.substring( 0, iIndexSlash1 );
		var sMonth = s.substring( iIndexSlash1 + 1, iIndexSlash2 );
		var sYear = s.substring( iIndexSlash2 + 1, iIndexEnd + 1 );
		
		return calendarDate(sDay, sMonth, sYear);
	}			
}

function IsSmallDate(s)
// Returns true if a string can be converted to a date or if the string is blank

{
	if (s.length == 0)
	{
		return true;
	}
	else
	{
		// Convert the string to a javascript date.
		var iIndexSlash1 = s.indexOf('/');
		if ( iIndexSlash1 < 1 )
		{
			return false;
		}
		// Have X../..
		
		var iIndexSlash2 = s.indexOf('/', iIndexSlash1 + 1);
		if ( iIndexSlash2 == -1 || (iIndexSlash2 - iIndexSlash1) < 2 )
		{
			return false;
		}
		// Have X../X../..
		
		var iIndexEnd = s.length - 1;
		if ( iIndexEnd - iIndexSlash2 < 1 )
		{
			return false;
		}
		// Have X../X../X..
		
		// Substring works from start index to end index - 1
		var sDay = s.substring( 0, iIndexSlash1 );
		var sMonth = s.substring( iIndexSlash1 + 1, iIndexSlash2 );
		var sYear = s.substring( iIndexSlash2 + 1, iIndexEnd + 1 );
		
		sYear = "20" + sYear;
		
		return calendarDate(sDay, sMonth, sYear);
	}			
}

function Month()
{
	this.name = "";
	this.selected = "";
	this.days = 0;
}

// Set up the array to hold the month objects
month = new Object();

for(var i=0; i<12; i++)
{
	month[i] = new Month();
}

month[0].name  = "Jan"; month[0].days  = 31;
month[1].name  = "Feb"; month[1].days  = 28;
month[2].name  = "Mar"; month[2].days  = 31;
month[3].name  = "Apr"; month[3].days  = 30;
month[4].name  = "May"; month[4].days  = 31;
month[5].name  = "Jun"; month[5].days  = 30;
month[6].name  = "Jul"; month[6].days  = 31;
month[7].name  = "Aug"; month[7].days  = 31;
month[8].name  = "Sep"; month[8].days  = 30;
month[9].name  = "Oct"; month[9].days  = 31;
month[10].name = "Nov"; month[10].days = 30;
month[11].name = "Dec"; month[11].days = 31;


function calendarDate(sDay, sMonth, sYear)
// Function to check legal calendar dates
{
	// Check they are numbers
	if ( !isInt(sDay) || !isInt(sMonth) || !isInt(sYear) )
	{
		return false;
	}

	// d2 Chnaged to use parseInt.
	var intDay = parseInt(sDay, 10);
	var intMonth = parseInt(sMonth, 10);
	var intYear = parseInt(sYear, 10);

	// Check the year and month range
	// Arbitrary year range, desinged to pick up typos, not get sensible data.
	if ( intMonth < 1 || intMonth > 12 || intYear < 1800 || intYear > 3000 )
	{
		return false;
	}

	// Check day range
	if ( intDay < 1 || ( intDay > month[intMonth-1].days && intMonth != 2 ) )
	{
		return false;
	}

	// Leap year - February Days.
	if ( intMonth == 2 )
	{
		// Work out number of days in Feb.
		var intFebDays;
		
		// Generally leap years are divisible by 4.
		if ( intYear % 4 == 0 )
		{
			// Years divisible by 100 are not leap years, unless divisible by 400.
			if ( intYear % 100 == 0 && ! ( intYear % 400 == 0 ))
			{
				intFebDays = 28;
			}
			else
			{
				intFebDays = 29;
			}
		}
		else
		{
			intFebDays = 28;
		}

		if ( intDay > intFebDays )
		{
			return false;
		}
	}

	return true;
}


function isInt(s)
{
	if (s.length == 0)
	{
		return true;
	}
	else
	{
		var i = parseInt(s, 10);

		if (isNaN(i))
		{
			return false;
		}
		else
		{
			// Check if the parsed int is the same as original
			if ( s == i )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}


function addValidation(a_bTest, a_sMessage, a_ctlField)
// Add the given validation.
// d1 PDT 16/01/1999
{
  if (!a_bTest)
  {
    //If no previous field failed then set the focus to this field
    if (this.bPassedValidation)
    {
      try
      {     a_ctlField.focus();
      }
      catch(e)
      {}
    }
    
    this.sMessage += "  " + a_sMessage + "\n";
    this.bPassedValidation = false;
  }
}

function validate()
// Perform the validation.
// If the validation failed then put and alert message up.
// d1 PDT 16/01/1999
{
  if (!this.bPassedValidation)
  {
    alert(this.sMessage);
  }

  return this.bPassedValidation;
}

function FormValidator(theForm)
// Constructor for the FormValidator
// d1 PDT 16/01/1999
{
  //Initialise member variables
  this.bPassedValidation = true;
  this.sMessage = "The following fields are invalid:\n\n";

  //Add methods
  this.addValidation = addValidation;
  this.validate = validate;
}

function CheckAlpha(a_sExpression){
	var PassReg = new RegExp("[^a-zA-Z0-9_]");
	
	if (PassReg.test(a_sExpression))
	{   return false;   }
	else
	{   return true;    }
					
}

