function chkDateValid(obj, dtFormat, dtSep)
{
  if(obj.value!="")
  {
	if(dtFormat.value!="")
	{
		var boolFlagDate = isDate(obj, dtFormat, dtSep);
		if(! boolFlagDate)
		{									
			obj.focus();
			obj.select();
			return false;
		}
		else{return true;}			
	}
  }
}
function isDate(gField, gFormat, dtSep)
{

     var inputStr = gField.value;
     // convert hyphen delimiters to slashes
     while (inputStr.indexOf("-") != -1)
     {
          inputStr = replaceString(inputStr,"-","/");
     }
     while (inputStr.indexOf(" ") != -1)
     {
          inputStr = replaceString(inputStr," ","/");
     }
     var delim1 = inputStr.indexOf("/");
     var delim2 = inputStr.lastIndexOf("/");
     if (delim1 != -1 && delim1 == delim2)
     {
          // there is only one delimiter in the string
          alert("Enter a Valid Date");
          gField.focus();
          gField.select();
          return false;
     }
	if (delim1 != -1)
	{
        // there are delimiters; extract component values
        if(gFormat=='dmy' || gFormat=='DMY')
        {
			var dd = parseInt(inputStr.substring(0,delim1),10);
			var mm = parseInt(inputStr.substring(delim1 + 1,delim2),10);
			var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10);
        }
        else if (gFormat=='mdy' || gFormat=='MDY')
        {
			var mm = parseInt(inputStr.substring(0,delim1),10);
			var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10);
			var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10);
        }
    }
     else
     {
          // there are no delimiters; extract component values
          var dd = parseInt(inputStr.substring(0,2),10);
          var mm = parseInt(inputStr.substring(2,4),10);
          var yyyy = parseInt(inputStr.substring(4,inputStr.length),10);
     }
     if (isNaN(mm) || isNaN(dd) || isNaN(yyyy))
     {
          // there is a non-numeric character in one of the component values
          alert("Enter a Valid Date");
          gField.focus();
          gField.select();
          return false;
     }
     if (mm < 1 || mm > 12)
     {
          // month value is not 1 thru 12
          alert("Enter a Valid Date");
          gField.focus();
          gField.select();
          return false;
     }
     if (dd < 1 || dd > 31)
     {
          // date value is not 1 thru 31
          alert("Enter a Valid Date");
          gField.focus();
          gField.select();
          return false;
     }

     // validate year, allowing for checks between year ranges
     // passed as parameters from other validation functions
     if (yyyy < 100)
     {
          // entered value is two digits, which we allow for 1930-2029
          if (yyyy >= 30)
          {
               yyyy += 1900;
          }
          else
          {
               yyyy += 2000;
          }
     }

     var today = new Date();
     var tmpYYYY = today.getFullYear();
     var minYear = tmpYYYY - 100;
     var maxYear = tmpYYYY + 60;
     // function called with specific year range parameters
	 
	 if (gField.name!="pass_iss_date" && gField.name!="pass_iss_exp_date" && gField.name!="txt_arr_date" && gField.name!="txt_int_date")
	 {
		if (yyyy <= minYear || yyyy >= maxYear)
	     {
			  // entered year is outside of range passed from calling function
			  alert("Enter a Valid Date");
	          gField.focus();
		      gField.select();
			  return false;
	     }
	 }
	 else
	{
		yyyystring=yyyy.toString();
		 if(yyyystring.length!=4)
		{
			  alert("Enter a Valid Date");
	          gField.focus();
		      gField.select();
			  return false;	
		}
	}
		 
     if (!checkMonthLength(mm,dd))
     {
          gField.focus();
          gField.select();
          return false;
     }
     if (mm == 2)
     {
          if (!checkLeapMonth(mm,dd,yyyy))
          {
               gField.focus();
               gField.select();
               return false;
          }
     }
     // put the Informix-friendly format back into the field
	if(gFormat=='dmy' || gFormat=='DMY')
	{
		inputStr = monthDayFormat(dd) + dtSep + monthDayFormat(mm) + dtSep +  yyyy ;
	}
	else if(gFormat == 'mdy' || gFormat == 'MDY')
	{
		inputStr = monthDayFormat(mm) + dtSep + monthDayFormat(dd) + dtSep +  yyyy ;
	}
	gField.value = inputStr;
     return true;
}
function checkMonthLength(mm,dd)
{
     var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December")
     if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30)
     {
          alert(months[mm] + " has only 30 days.");
          return false;
     }
     else
     {
          if (dd > 31)
          {
          alert(months[mm] + " has only 31 days.");
          return false;
          }
     }
     return true;
}
function monthDayFormat(str)
{
     while (str.length <=2)
     {
          str = "0" + str;
     }
     return str;
}

// insert insertString immediately before searchString
function insertString(mainStr,searchStr,insertStr)
{
     var front = getFront(mainStr,searchStr);
     var end = getEnd(mainStr,searchStr);
     if (front != null && end != null)
     {
          return front + insertStr + searchStr + end;
     }
     return null;
}
// remove deleteString
function deleteString(mainStr, deleteStr)
{
     return replaceString(mainStr,deleteStr,"");
}
// replace searchString with replaceString
function replaceString(mainStr, searchStr, replaceStr)
{
     var front = getFront(mainStr, searchStr);
     var end = getEnd(mainStr,searchStr);
     if (front != null && end != null)
     {
          return front + replaceStr + end;
     }
     else
     {
          return mainStr;
     }
     return null;
}

// extract front part of string prior to searchString
function getFront(mainStr,searchStr)
{
     foundOffset = mainStr.indexOf(searchStr);
     if (foundOffset == -1)
     {
          return null;
     }
     return mainStr.substring(0,foundOffset);
}

// extract back end of string after searchString
function getEnd(mainStr,searchStr)
{
     foundOffset = mainStr.indexOf(searchStr);
     if (foundOffset == -1)
     {
          return null;
     }
     return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
}

// check the entered February date for too high a value 
function checkLeapMonth(mm,dd,yyyy)
{
     if (yyyy % 4 > 0 && dd > 28)
     {
          alert("February of " + yyyy + " has only 28 days.");
          return false;
     }
     else
     {
          if (dd > 29)
          {
               alert("February of " + yyyy + " has only 29 days.");
               return false;
          }
     }
     return true;
}