var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
   var inDtStr = dtStr.value;
   var wDtStr = Trim(dtStr.value);
   if ((wDtStr == null) || (wDtStr == "")) {
      dtStr.value = wDtStr;
      alert("The date is blank");
      setTimeout("focusElement('" + dtStr.form.name + "', '" + dtStr.name + "')",0);
      return false;
   }
	var daysInMonth = DaysArray(12)
   var mm = ''; dd = ''; yyyy = ''
   wDigits = '';
// Try and find month, day, year
   if (isInteger(wDtStr)) {
      if (wDtStr.length == 6) {
         mm = wDtStr.substr(0,2);
         dd = wDtStr.substr(2,2);
         yyyy = '20' + wDtStr.substr(4,4);
      }
      if (wDtStr.length == 8) {
         mm = wDtStr.substr(0,2);
         dd = wDtStr.substr(2,2);
         yyyy = wDtStr.substr(4,4);
      }
      wDigits = new Array(mm,dd,yyyy);
   }
   else {
      wDigits = wDtStr.split(/\D/);
      if (wDigits[2] < 100) {
         wDigits[2] = ((wDigits[2] - 0) + 2000).toString();
      }
      if (wDigits[0].length == 1) {wDigits[0] = "0" + wDigits[0];}
      if (wDigits[1].length == 1) {wDigits[1] = "0" + wDigits[1];}
   }
// Validate month, day, year
   if (wDigits.length !== 3) {
      alert("The date format should be : mm-dd-yyyy");
      setTimeout("focusElement('" + dtStr.form.name + "', '" + dtStr.name + "')",0);
      return false;
   }
// Convert to numeric for testing
	month=wDigits[0]-0;
	day=wDigits[1]-0;
	year=wDigits[2]-0;
	if (wDigits[0].length<1 || month<1 || month>12){
		alert("Please enter a valid month. Current value(" + month + ")")
		dtStr.value = inDtStr;
		setTimeout("focusElement('" + dtStr.form.name + "', '" + dtStr.name + "')",0);
		return false
	}
	if (wDigits[1].length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day. Current value(" + wDigits[1] + ")");
		setTimeout("focusElement('" + dtStr.form.name + "', '" + dtStr.name + "')",0);
		dtStr.value = inDtStr;
		return false
	}
	if (wDigits[2].length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+". Current value(" + wDigits[2] + ")");
		setTimeout("focusElement('" + dtStr.form.name + "', '" + dtStr.name + "')",0);
		dtStr.value = inDtStr;
		return false
	}
	wDate = wDigits.join("-");
   dtStr.value = wDate;
return true
}