//		DATE VALIDATION AND CONVERSION ROUTINE
//		Automatically validates dates and converts to dd mmm yyyy format
//		USAGE:
//		To validate and convert dates upon field.lostfocus
//		<INPUT TYPE="Text" SIZE="11" MAXLENGTH="11" NAME="DateFrom" onBlur="checkdate(this)" >

//		To verify that one date field is later than the other
//		<INPUT TYPE="submit" VALUE="Go!" NAME="Action" onClick="doDateCheck(this.form.DateFrom, this.form.DateTo);" >
//		(Substitute field references as appropriate)

//		PLACE FOCUS ON FIRST ENTRY FIELD
//		Sets focus to the first form field on a page.
//		USAGE:
//		Include in body tag - <BODY OnLoad="placeFocus()">


function checkdate(objName) {
   if (!chkdate(objName)) {
      objName.select();
      alert("That date is invalid.  Please try again.");
      objName.focus();
      return false;
   } else {
      return true;
   }
}
function chkdate(objName) {
   //var strDatestyle = "US"; // United States date style (month, day, year)
   var strDatestyle = "EU";  // European date style (day, month, year)
   var strMonthArray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
   var strDate = objName.value;

   // A blank field is a valid date, as far as we're concerned
   if (strDate.length < 1)
      return true;

   var strDateParts = splitDateParts(strDate);
   if (!strDateParts)
      return false;

   var strDay = strDateParts[0];
   var strMonth = strDateParts[1];
   var strYear = strDateParts[2];

   // If we're doing US style, the month was first, so swap the values
   if (strDatestyle == "US") {
      strDay = strDateParts[1];
      strMonth = strDateParts[0];
   }

   // We learnt our lesson from Y2k :)
   if (strYear.length == 2)
      strYear = (parseInt(strYear) < 30 ? '20' : '19') + strYear;

   // Get the numeric day
   var intDay = parseInt(strDay, 10);
   if (isNaN(intDay))
      return false;

   // Try for a numeric month
   var intMonth = parseInt(strMonth, 10);

   // If the month isn't numeric, maybe it's a name?
   if (isNaN(intMonth)) {
      for (var i = 0; i < 12; i++) {
         if (strMonth.substr(0, 3).toUpperCase() == strMonthArray[i].toUpperCase()) {
            intMonth = i + 1;
            strMonth = strMonthArray[i];
            break;
         }
      }
      if (isNaN(intMonth))
         return false;
   }

   // Is the year a valid number?
   var intYear = parseInt(strYear, 10);
   if (isNaN(intYear))
      return false;

   if (!IsValidDate(intDay, intMonth, intYear))
      return false;

   // Okay, reformat the date in the input field, and return
   if (strDatestyle == "US") {
      objName.value = strMonthArray[intMonth-1] + " " + intDay + " " + strYear;
   } else {
      objName.value = intDay + " " + strMonthArray[intMonth-1] + " " + strYear;
   }

   return true;
}
function IsLeapYear(intYear) {
   if (intYear % 100 == 0) {
      return (intYear % 400) == 0;
   } else {
      return (intYear % 4) == 0;
   }
}
function IsValidMonth(intMonth) {
   return (intMonth >= 1 && intMonth <= 12);
}
function IsValidDate(intDay, intMonth, intYear) {
   if (!IsValidMonth(intMonth))
      return false;

   // If day is outside 1-31, there's no way it's valid
   if (intDay > 31 || intDay < 1)
      return false;
   // Check the 30-day months
   if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11)
      return intDay <= 30;
   // Check February's funky days
   if (intMonth == 2)
      return intDay <= 28 || (IsLeapYear(intYear) && intDay == 29);
   return true;
}
function splitDateParts(strDate) {
   var strSeparatorArray = new Array("-"," ","/",".");
   // Try the various separators out
   for (var intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
      if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
         var strDateArray = strDate.split(strSeparatorArray[intElementNr]);
         if (strDateArray.length == 3)
            return strDateArray;
      }
   }
   // If none were good, assume it's just all numeric
   if (strDate.length >= 6)
      return new Array(strDate.substr(0, 2), strDate.substr(2, 2), strDate.substr(4));
}


function doDateCheck(from, to) {
   if (Date.parse(from.value) > Date.parse(to.value)) {
      if (from.value == "" || to.value == "")
         alert("Both dates must be entered.");
      else
         alert("To date must occur after the from date.");
   }
}



function placeFocus() {
	var acceptableElements = 'text select-one select-many';
   if (document.forms.length > 0) {
      var fields = document.forms[0];
      for (i = 0; i < fields.length; i++)
      	if(fields.elements[i].type)
      		if(acceptableElements.toString().indexOf(fields.elements[i].type) >= 0)
      			return document.forms[0].elements[i].focus();
   }
}


