function valiDate(obj) {
	var fld = obj, sMsg, iFormat;

	// if no format argument is supplied default to 1 (dd-mmm-yyyy)
	if (arguments.length==2) iFormat=arguments[1];
  else iFormat=1;

	// set up default date restrictions
	if (arguments.length==3) min_year=arguments[2];
	else min_year=1990;
	if (arguments.length==4) min_year=arguments[3];
	else max_year=2020;

	ret_val = isValidDate(obj, iFormat, min_year, max_year);

  if(ret_val==11){
    err_message = "The date is outside of the acceptable date range.\nPlease choose a date between 01-Jan-" + min_year + " and 31-Dec-" + max_year;
  }
  else if (ret_val > 1){
    err_message = obj.value + " is not a valid date.  Please try again.\n\nSome examples of valid date formats:\n12\/31\/00     12\/31\/2000\n12.31.00       12.31.2000\n12\-31\-00       12\-31\-2000\n31\-Dec\-00   31\-Dec\-2000";
  }

  if(ret_val > 1){
    fld.select();
    fld.focus();
    alert (err_message);
    return false
  }
  else return true;


}

function isValidDate(obj, iFormat, min_year, max_year) {
	var sDate, asTmp, sOutput, iFormat;
	var sDay, sMonth, sYear;
	var iDay, iMonth, iYear;
	var sStyle = "US", bExists = false, fld = obj, bAlphaMonth = false;
	var i, err = 0;
	var asMonth = new Array ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var sSeparator = new Array("-"," ","/",".");

	sDate = fld.value;

	// if the field is empty stop now
	if (sDate.length < 1) {
		return true;
	}

	// look for valid separators and try to parse date string
	for (i = 0; i < sSeparator.length; i++) {
		if (sDate.indexOf(sSeparator[i]) != -1) {
			asTmp = sDate.split(sSeparator[i]);
			if (asTmp.length != 3) {
				return 13;
			}
			else {
				sDay = asTmp[0];
				sMonth = asTmp[1];
				sYear = asTmp[2];
			}
			bExists = true;
		}
	}

	// if no valid separators are found, check to see if date is in ddmmyyyy format
	if (bExists == false) {
		if (sDate.length>5) {
			sDay = sDate.substr(0, 2);
			sMonth = sDate.substr(2, 2);
			sYear = sDate.substr(4);
	   	}
	   	else {
	   		return 12;
	   	}
	}

	// do we have a d*-mmm-**yy format?
	for (i = 0;i<12;i++) {
			if (sMonth.toUpperCase() == asMonth[i].toUpperCase()) {
				bAlphaMonth = true
				sMonth = asMonth[i];
				i = 12;
	   		}
	}

	// if we're parsing US date style input, flip the day and month values, unless we know we've got a format that shouldn't be flipped
	if ((sStyle == "US")&&(bExists==true)&&(bAlphaMonth==false)) {
		strTemp = sDay;
		sDay = sMonth;
		sMonth = strTemp;
	}

	// check to make sure that the day portion of the string is a valid integer
	iDay = parseInt(sDay, 10);
	if (isNaN(iDay)) {
		return 2;
	}

	// check to make sure that the month portion of the string is a valid integer or a valid three letter month code
	iMonth = parseInt(sMonth, 10);
	if (isNaN(iMonth)) {
		for (i = 0;i<12;i++) {
			if (sMonth.toUpperCase() == asMonth[i].toUpperCase()) {
				iMonth = i+1;
				sMonth = asMonth[i];
				i = 12;
	   		}
		}
		if (isNaN(iMonth)) {
		  return 3;
	   	}
	}

  // get rid of any extraneous characters in the year string
  sYear = sYear.replace(/[^0-9]/g, "");

	// check to make sure that the year portion of the string is a valid integer
	iYear = parseInt(sYear, 10);
	if (isNaN(iYear)) {
		return 4;
	}

	// window any 2 digit years
	if (sYear.length == 2){
		if (sYear<95){
			sYear = '20' + sYear;
		}
		else
			sYear = '19' + sYear;
	}

	// pad one digit days with leading zeros
	if (iDay<10)
		sDay = "0" + iDay;
	else
		sDay = iDay + "";

	// check to make sure that month falls between 1 and 12
	if (iMonth>12 || iMonth<1) {
		return 5;
	}

	// check day in 31 day months
	if ((iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7 || iMonth == 8 || iMonth == 10 || iMonth == 12) && (iDay > 31 || iDay < 1)) {
		return 6;
	}

	// check day in 30 day months
	if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && (iDay > 30 || iDay < 1)) {
		return 7;
	}

	// check February
	if (iMonth == 2) {
		if (iDay < 1) {
			return 8;
		}
		if (isLeapYear(iYear) == true) {
			if (iDay > 29) {
			return 9;
		}
	}
	else {
		if (iDay > 28) {
			return 10;
		}
	}
	}

	// restrict year
	iYear = parseInt(sYear, 10);
	if (iYear < min_year || iYear > max_year){
		return 11;
	}

	// format the output
	switch(arguments[1]){
		case 2:
			sOutput = iMonth + "/" + iDay + "/" + sYear;
			break;
		default:
			sOutput = sDay + "-" + asMonth[iMonth-1] + "-" + sYear;
			break;
	}

	fld.value = sOutput;
	return true;
}

function isLeapYear(iYear) {
	if (iYear % 100 == 0) {
		if (iYear % 400 == 0) { return true; }
	}
	else {
		if ((iYear % 4) == 0) { return true; }
	}
	return false;
}
