/* Copyright 2004: Eric Tessau */

var dtMDYFormat;

function requiredString(a, b)
{
  var re = /^\s[ \t]*$/;
  var myval = a.value;
  if (re.test(myval) || myval == '')
  {
	alert("The following field is required: " + b);
	//myval.focus();
	return false;
  }
  return true;
}

function requiredNumeric(a, b)
{
  var boolRetVal;
  boolRetVal = requiredString(a, b) && validateNumeric(a, b);
  return boolRetVal;
}

function requiredPositiveNumeric(a, b)
{
  var boolRetVal;
  boolRetVal = requiredString(a, b) && validatePositiveNumeric(a, b) && validateNumeric(a, b);
  return boolRetVal;
}

function requiredDate(a, b)
{
  var boolRetVal;
  boolRetVal = requiredString(a, b) && validateDate(a, b);
  return boolRetVal;
}

function checkString(a)
{
  var re = /^\s[ \t]*$/;
  var myval = a.value;
  if (re.test(myval) || myval == '')
  {
	return false;
  }
  return true;
}

function validateNumeric(a, b)
{
  var numTemp = a.value;
  if (numTemp == '') {return true;}

  //strip out commas
  numTemp = numTemp.replace(/,/g,"");
  if (!(/^[-+]?\d*\.?\d*$/.test(numTemp)))
  {
	alert("The following field must be numbers only (eg. 10, 2.25, etc.) " + b);
	return false;
  }
  return true;
}

function validatePositiveNumeric(a, b)
{
  var myval = a;
  if (myval.value == '0' || /^-/.test(myval.value)) 
  {
    alert("The following field must have a value greater than 0: " + b);
    return false;
  }
  return true;
}

function requiredSelectBox(a, b)
{
  var myval = a;
  if (myval.value == '0' || myval.value == '') 
  {
    alert("You must make a selection from the following field: " + b);
    return false;
  }
  return true;
}

// BEGIN ERIC'S VERSION
function validateDate(objName, b) 
{
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intDay;
var intMonth;
var intYear;
var datefield = objName;
var intElementNr;
// var err = 0;
strDate = objName.value;

// OK if zero-length string (required would be handled elsewhere)
if (strDate.length < 1) 
{
  return true;
}

if (strDate.indexOf("/") != -1) 
{
 strDateArray = strDate.split("/");
}
else
{
  alert("Date must be in month/day/year, such as 12/31/2004");
  return false;
}

// Need to be exactly three elements: month, day, year
if (strDateArray.length != 3) {
  alert("Date must be in month/day/year, such as 12/31/2004");
  return false;
}
else {
  // switch these only in multilingual versions
  strMonth = strDateArray[0];
  strDay = strDateArray[1];
  strYear = strDateArray[2];
}

//Adjustment for short years entered
if (strYear.length == 2) {
  strYear = '20' + strYear;
}

intDay = parseInt(strDay, 10);
if (isNaN(intDay)) 
{
  alert("Day is not a number"); 
  return false;
}

intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) 
{
  alert("Month is not a number");
  return false;
}

intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
  alert("Year is not a number");
  return false;
}

// Now, check that all of the month, day, years are in valid ranges
if (intMonth > 12 || intMonth < 1) 
{
  alert("Month should be between 1 and 12: " + intMonth);
  return false;
}

if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)) {
  alert("Day provided is out of range for the given month: " + intDay);
  return false;
}

if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1)) {
  alert("Day provided is out of range for the given month: " + intDay);
  return false;
}

if (intMonth == 2) 
{
  if (intDay < 1) 
  {
    alert("Day provided is out of range for the given month: " + intDay);
    return false;
  }

  if (LeapYear(intYear) == true) 
  {
    if (intDay > 29) 
    {
      alert("Day provided is out of range for the given month: " + intDay);
      return false;
    }
  }
  else 
  {
    if (intDay > 28) 
    {
      alert("Day provided is out of range for the given month: " + intDay);
      return false;
    }
  }
}
dtMDYFormat = new Date(intYear, intMonth-1, intDay);
objName.value = intMonth + '/' + intDay + '/' + intYear
return true;
}

function LeapYear(intYear) 
{
  if (intYear % 100 == 0) 
  {
    if (intYear % 400 == 0) { return true; }
  }
  else 
  {
    if ((intYear % 4) == 0) { return true; }
  }
  return false;
}

// a = Date 1 should be less than
// b = Date 2 
// c = String of field name of Date 1
// d = String of field name of Date 2
// e = 0: neither required; 1 = Date 1 required; 2 = Date 2 required; 3 = Both required
function DoDateLTE(a, b, c, d, e)
{
  var validate1;
  var validate2;
  var date1;
  var date2;

  if (e == 0)
  {
    if (a.value == '' || b.value == '') {return true;}

    validate1 = validateDate(a, c);
    date1 = dtMDYFormat;
    validate2 = validateDate(b, d);
    date2 = dtMDYFormat;
  }
  else if (e == 1)
  {
    if (b.value == '') {return true;}
    if (a.value == '')
    {
	alert("Required date " + c + " is missing");
	return false;
    }

    validate1 = requiredDate(a, c);
    date1 = dtMDYFormat;
    validate2 = validateDate(b, d);
    date2 = dtMDYFormat;
  }
  else if (e == 2)
  {
    if (a.value == '') {return true;}
    if (b.value == '')
    {
	alert("Required date " + d + " is missing");
	return false;
    }

    validate1 = validateDate(a, c);
    date1 = dtMDYFormat;
    validate2 = requiredDate(b, d);
    date2 = dtMDYFormat;
  }
  else //if (e == 3)
  {
    if (a.value == '')
    {
	alert("Required date " + c + " is missing");
	return false;
    }
    if (b.value == '')
    {
	alert("Required date " + d + " is missing");
	return false;
    }

    validate1 = requiredDate(a, c);
    date1 = dtMDYFormat;
    validate2 = requiredDate(b, d);
    date2 = dtMDYFormat;
  }

  // success!
  if (date1 <= date2)
  {
	return true;
  }
  // uh oh -- date 2 was greater!
  else
  {
	alert(d + " should not be a later date than " + c);
	return false;
  }
}

// a = Date 1 should be less than
// b = String of field name of Date 1
// c = 0: date not required; 1 = date required
function DoDateLTEToday(a, b, c)
{
  var validate1;
  var date1;
  var date2 = new Date();

  if (c == 0)
  {
    if (a.value == '') {return true;}

    validate1 = validateDate(a, b);
    date1 = dtMDYFormat;
  }
  else if (c == 1)
  {
    if (a.value == '')
    {
	alert("Required date " + b + " is missing");
	return false;
    }

    validate1 = requiredDate(a, b);
    date1 = dtMDYFormat;
  }

  // success!
  if (date1 <= date2)
  {
	return true;
  }
  // uh oh -- date was greater than today!
  else
  {
	
	var confirmret;
	confirmret = confirm(b + " is later than today's date. Do you wish to continue?");
	if (confirmret)
	{
	  return true;
	}
	else
	{
	  return false;
	}
	
	//alert(b + " should not be a later date than today.");
	//return true;
  }
}