// Global variables
//
   var mthnames = new Array(12);
	mthnames[0] = "Jan";
	mthnames[1] = "Feb";
	mthnames[2] = "Mar";
	mthnames[3] = "Apr";
	mthnames[4] = "May";
	mthnames[5] = "Jun";
	mthnames[6] = "Jul";
	mthnames[7] = "Aug";
	mthnames[8] = "Sep";
	mthnames[9] = "Oct";
	mthnames[10] = "Nov";
	mthnames[11] = "Dec";

/* The functions in this file primarily work with IE5.0+ and NN6.0+.
 *
 * These functions will also work for IE4.0 due to a 
 * document.getELemenById remapping script that is executed in layers.js
*/

function checkdate(objName) {
   var datefield = objName;
   if (chkdate(objName) == false) {
	return false;
   }
   else {
	return true;
   }
}


function chkdate(objName) {
   var strDatestyle = "US"; //United States date style
 //var strDatestyle = "EU";  //European date style
   var strDate;
   var strDateArray;
   var strDay;
   var strMonth;
   var strYear;
   var intday;
   var intMonth;
   var intYear;
   var booFound = false;
   var datefield = objName;
   var strSeparatorArray = new Array("-"," ","/",".");
   var intElementNr;
   var err = 0;
   strDate = datefield.value;

   if (strDate.length < 1) {
	return true;
   }

   for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
	   strDateArray = strDate.split(strSeparatorArray[intElementNr]);
	   if (strDateArray.length != 3) {
		err = 1;
		return false;
	   }
	   else {
		strDay = strDateArray[0];
		strMonth = strDateArray[1];
		strYear = strDateArray[2];
	   }

	   booFound = true;
	}
   }

   if (booFound == false) {
	if (strDate.length>5) {
	   strDay = strDate.substr(0, 2);
	   strMonth = strDate.substr(2, 2);
	   strYear = strDate.substr(4);
	}
   }

   if (strYear.length == 2) {
	strYear = '20' + strYear;
   }

   // US style
   if (strDatestyle == "US") {
	strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;
   }
   intday = parseInt(strDay, 10);
   if (isNaN(intday)) {
	err = 2;
	return false;
   }

   intMonth = parseInt(strMonth, 10);
   if (isNaN(intMonth)) {
	for (i = 0;i<12;i++) {
	   if (strMonth.toUpperCase() == mthnames[i].toUpperCase()) {
		intMonth = i+1;
		strMonth = mthnames[i];
		i = 12;
	   }
	}

	if (isNaN(intMonth)) {
	   err = 3;
	   return false;
	}
   }

   intYear = parseInt(strYear, 10);
   if (isNaN(intYear)) {
	err = 4;
	return false;
   }

   if (intMonth>12 || intMonth<1) {
	err = 5;
	return false;
   }

   if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
	err = 6;
	return false;
   }

   if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
	err = 7;
	return false;
   }

   if (intMonth == 2) {
	if (intday < 1) {
	   err = 8;
	   return false;
	}

	if (LeapYear(intYear) == true) {
	   if (intday > 29) {
		err = 9;
		return false;
	   }
	}
	else {
	   if (intday > 28) {
		err = 10;
		return false;
	   }
	}
   }

   if (strDatestyle == "US") {
	datefield.value = mthnames[intMonth-1] + " " + intday+" " + strYear;
   }
   else {
	datefield.value = intday + " " + mthnames[intMonth-1] + " " + strYear;
   }

   return true;
}


function LeapYear(intYear) {
   if (intYear % 100 == 0) {
	if (intYear % 400 == 0) { return true; }
   }
   else {
	if ((intYear % 4) == 0) { return true; }
   }

   return false;
}


// Check for valid date. If valid, format the date.
// This one is a more generic version of the above function.
// This will eventuall replace the other routines.
//
function valiDate(dateFld, dateName, dateEmsg) {
  if (dateFld.value == "" || dateFld.value == " ") return true;
  var checkStr = "";
  var checkLen = 0;
  var year, month, date, ampm, monthtext;

  checkStr = document.getElementById(dateName).value;
  checkLen = document.getElementById(dateName).value.length;
  document.getElementById(dateEmsg).innerHTML = "";

  year  = parent.left_frame.document.getElementById('birthyear_id').value;
  year  = parseInt(year);
  year  = isNaN(year) ? 0 : tooLong(year);
  monthtext = parent.left_frame.document.getElementById('birthmonth_id').options[parent.left_frame.document.getElementById('birthmonth_id').selectedIndex].innerText;
  month = parent.left_frame.document.getElementById('birthmonth_id').options[parent.left_frame.document.getElementById('birthmonth_id').selectedIndex].value;
  month = isNaN(month) ? 0 : parseInt(month);
  date  = parent.left_frame.document.getElementById('birthdate_id').options[parent.left_frame.document.getElementById('birthdate_id').selectedIndex].value;
  date  = isNaN(date) ? 0 : parseInt(date);
  ampm  = parent.left_frame.document.getElementById('ampm_id').options[parent.left_frame.document.getElementById('ampm_id').selectedIndex].value;
  var bdate = new Date(year, month, date);

  if (checkdate(dateFld)) {
    var fromDate = new Date(bdate);
        fromDate.setHours(0);
        fromDate.setHours(0);
        fromDate.setMinutes(0);
        fromDate.setSeconds(0);
    var workDate = new Date(dateFld.value);
        workDate.setHours(0);
        workDate.setMinutes(0);
        workDate.setSeconds(0);

    if (workDate >= fromDate) {
    }
    else {
	document.getElementById(dateEmsg).innerHTML = 
	  "You must choose a date after " + monthtext + " " + date + " " + year;;
      return (false);
    }
  }
  else {
    document.getElementById(dateEmsg).innerHTML = "Invalid Date. Please try again.";
    return (false);
  }

  return (true);
}


function tooLong(val) {
  if (val >= 0) {
    return Math.floor(val);
  }
  else {
    return Math.ceil(val);
  }
}

