//<!--
function compareDate(pDate1,pDate2,pFormat)
{
	intyear1 = validateDate1(pDate1,false,pFormat,"yyyy");
	intmonth1 = validateDate1(pDate1,false,pFormat,"MM");
	intdate1 = validateDate1(pDate1,false,pFormat,"dd");
	
	
	intyear2 = validateDate1(pDate2,false,pFormat,"yyyy");
	intmonth2 = validateDate1(pDate2,false,pFormat,"MM");
	intdate2 = validateDate1(pDate2,false,pFormat,"dd");

	if (intyear1 > intyear2)
		return 1;
	else if (intyear1 < intyear2)
		return -1;
	
	if (intmonth1 > intmonth2)
		return 1;
	else if (intmonth1 < intmonth2)
		return -1;

	if (intdate1 > intdate2)
		return 1;
	else if (intdate1 < intdate2)
		return -1;
	return 0;
}


function compareDateSys(pDate1,pDate2,pFormat)
{
	intyear1 = validateDate1(pDate1,false,pFormat,"yyyy");
	intmonth1 = validateDate1(pDate1,false,pFormat,"MM");
	intdate1 = validateDate1(pDate1,false,pFormat,"dd");
	intdate1=intdate1+1;
	
	intyear2 = validateDate1(pDate2,false,pFormat,"yyyy");
	intmonth2 = validateDate1(pDate2,false,pFormat,"MM");
	intdate2 = validateDate1(pDate2,false,pFormat,"dd");

	if (intyear1 > intyear2)
		return 1;
	else if (intyear1 < intyear2)
		return -1;
	
	if (intmonth1 > intmonth2)
		return 1;
	else if (intmonth1 < intmonth2)
		return -1;

	if (intdate1 > intdate2)
		return 1;
	else if (intdate1 < intdate2)
		return -1;
	return 0;
}

/**
  * This method gets the correct AM/PM text based on the hour
  */

function getCorrectAMPM (pHour, pAMPM)
{
   var lAMPM = pAMPM;
   if (lAMPM == null)
   {
      if (pHour > -1 && pHour < 12) lAMPM = "AM";
      else if (pHour > 11 && pHour < 24) lAMPM = "PM";
   }
   else if (pHour > 12) lAMPM = "PM";
   return lAMPM;
}
function validateDate (pField, pNullAllowed, pFormat, pOutputFormat, pImage)
{
   return(validateDate1 (pField.value, pNullAllowed, pFormat, pOutputFormat, pImage))
}
/**
  * This method validates the specified date and optionally returns
  * the date in the new format specified.
  */
function validateDate1 (pFieldValue, pNullAllowed, pFormat, pOutputFormat, pImage)
{
   var lCount = 0;
   var lYear = 1900;
   var lMonth = 0;
   var lDay = 1;
   var lHour = 0;
   var lMinute = 0;
   var lSecond = 0;
   var lMilliSecond = 0;
   var lAMPM = null;
   var lOutputDate = null;
   var lDateCount = 0;
   var lTempString;
   var lAMPMStrings = new Array(2);
   var lMinimumHour = 0;
   var lInDate = pFieldValue;
   var lDate;
   lAMPMStrings[0] = "AM";
   lAMPMStrings[1] = "PM";
   lInDate = pFieldValue;
   if (!pNullAllowed)
   {
      if (lInDate == null)
	  {
	     errMsg = "1 Please enter a valid date in " + pFormat + " format.";
         if (pImage != null)
         {
            pImage.src = "/scripts/error.gif"
            pImage.alt = errMsg;
         }
		 return false;
	  }
	  else
	  {
		 lInDate = trim(lInDate);
		 if (lInDate.length == 0)
		 {
		    errMsg = "2 Please enter a valid date in " + pFormat + " format."
            if (pImage != null)
            {
               pImage.src = "/scripts/error.gif"
               pImage.alt = errMsg;
            }
	        return false;
		 }
	  }
   }
   //if (lInDate == null) return true;
   lInDate = trim(lInDate);
   if (lInDate.length > 0)
   {
      lDate = parseDate(lInDate);
      lFormat = parseFormat(pFormat, false);
      if (lFormat.length != lDate.length)
      {
   	     errMsg = "3 Please enter a valid date in " + pFormat + " format.";
         if (pImage != null)
         {
            pImage.src = "/scripts/error.gif"
            pImage.alt = errMsg;
         }
		 return false;
	 }
	
	  lCount = 0;
      while(lFormat[lCount] && lCount < lFormat.length)
      {
         if (lFormat[lCount] == "yyyy" ||
             lFormat[lCount] == "yy")
            lYear = getYear(lDate[lDateCount++]);
         else if (lFormat[lCount] == "MM" ||
                  lFormat[lCount] == "MMM" ||
                  lFormat[lCount] == "MMMM")
            lMonth = getMonth(lDate[lDateCount++]);
         else if (lFormat[lCount] == "dd")
            lDay = getDay(lDate[lDateCount++]);
         else if(lFormat[lCount] == "hh" ||
                 lFormat[lCount] == "HH")
		 {
            lHour = getHour(lDate[lDateCount++]);
			if(lFormat[lCount] == "hh") lMinimumHour = 1
			else lMinimumHour = 0;
		 }
         else if (lFormat[lCount] == "mm")
            lMinute = getMinute(lDate[lDateCount++]);
         else if (lFormat[lCount] == "ss")
            lSecond = getSecond(lDate[lDateCount++]);
         else if (lFormat[lCount] == "SS")
            lMilliSecond = getMilliSecond(lDate[lDateCount++]);
         else if (lFormat[lCount] == "a")
            lAMPM = getAMPM(lDate[lDateCount++]);
         lCount++;
      }
      lAMPM = getCorrectAMPM(lHour, lAMPM);
      if (lYear > 0 && lMonth >= 0 && lDay >= 1 &&
          lHour >= lMinimumHour && lMinute >= 0 && lSecond >= 0 &&
          lMilliSecond >= 0)
      {
         if (lAMPM == "PM" && lHour <= 11) lHour = lHour + 12;
		 else if (lAMPM == "AM" && lHour == 12) lHour = 0; 
         if (isDayValid (lYear, lMonth, lDay))
         {
            lOutputDate = formatDate(lYear, lMonth, lDay, lHour, lMinute, lSecond, lMilliSecond, lAMPM, pOutputFormat);
         }
         else
         {
            errMsg = "Invalid number of days for the month. Please enter a valid date in " + pFormat + " format."
            if (pImage != null)
            {
               pImage.src = "/scripts/error.gif"
               pImage.alt = errMsg;
            }
            return false;
         }
      }
      else
      {
         errMsg = "Enter a valid date in " + pFormat + " format.";
	     if (lYear <= 0) errMsg = "Enter a valid year";
		 else if (lMonth < 0) errMsg = "Enter a valid month";
		 else if (lDay < 1) errMsg = "Enter a valid day";
		 else if (lHour < lMinimumHour) errMsg = "Enter a valid hour";
		 else if (lMinute < 0) errMsg = "Enter valid minutes";
		 else if (lSecond < 0) errMsg = "Enter valid seconds";
		 else if (lMilliSeconds < 0) errMsg = "Enter valid milli seconds";
         if (pImage != null)
         {
            pImage.src = "/scripts/error.gif"
            pImage.alt = errMsg;
         }
		 return false;
      }
   }
   if (pImage != null)
   {
      pImage.src = "/scripts/transparent.gif"
      pImage.alt = "";
   }
   errMsg = "";
   return lOutputDate;
}

/**
  * This method formats the date in the specified format
  */
function formatDate(pYear, pMonth, pDay, pHour, pMinute, pSecond,
                    pMilliSecond, pAMPM, pOutputFormat)
{
   var lCount = 0;
   var lOutputDate = "";
   var lTempString ="";
   var lHour = 0;
   var lOutputFormat = parseFormat(pOutputFormat, true);

	//alert("output Format Length = " + lOutputFormat.length);
   while(lOutputFormat[lCount] && lCount < lOutputFormat.length)
   {
		//alert(lCount + ":" + lOutputFormat[lCount]);
      if (lOutputFormat[lCount] == "yyyy")
         lOutputDate = lOutputDate + formatNumber(pYear, 4)
      else if (lOutputFormat[lCount] == "yy")
      {
         lTempString = "";
         lTempString = lTempString + pYear; 
         lOutputDate = lOutputDate + lTempString.substr(2,4);
      }
      else if (lOutputFormat[lCount] == "MM")
         lOutputDate = lOutputDate + formatNumber(pMonth + 1, 2);
      else if (lOutputFormat[lCount] == "MMM")
         lOutputDate = lOutputDate + getShortMonths()[pMonth];
      else if (lOutputFormat[lCount] == "MMMM")
         lOutputDate = lOutputDate + getLongMonths()[pMonth];
      else if (lOutputFormat[lCount] == "dd")
         lOutputDate = lOutputDate + formatNumber(pDay, 2);
      else if(lOutputFormat[lCount] == "hh")
      {
         if (pHour > 12) lHour = pHour - 12;
		 // This is to handle the case of zero hours as 12 AM
		 else if (pHour == 0) lHour = 12;
		 else if (pHour == 0) lHour = 12;
		 else lHour = pHour;
         lOutputDate = lOutputDate + formatNumber(lHour, 2);
      }
      else if(lOutputFormat[lCount] == "HH")
      {
         if (pHour < 12 && pAMPM == "PM") lHour = pHour + 12;
		 else lHour = pHour;
         lOutputDate = lOutputDate + formatNumber(lHour, 2);
      }
      else if (lOutputFormat[lCount] == "mm")
         lOutputDate = lOutputDate + formatNumber(pMinute, 2);
      else if (lOutputFormat[lCount] == "ss")
         lOutputDate = lOutputDate + formatNumber(pSecond, 2);
      else if (lOutputFormat[lCount] == "SS")
         lOutputDate = lOutputDate + formatNumber(pMilliSecond, 3);
      else if (lOutputFormat[lCount] == "a")
      {
         if (pAMPM == null)
         {
            if (lHour <=12) lAMPM = "AM";
            else lAMPM = "PM";
         }
		 else
		    lAMPM = pAMPM;
         lOutputDate = lOutputDate + lAMPM;
      }
      else
         lOutputDate = lOutputDate + lOutputFormat[lCount];
      lCount++;
	  //alert(lCount + ":" + lOutputFormat[lCount]);
   }
   //alert("Return Date " + lOutputDate);
   return lOutputDate;
}
function formatNumber(pNumber, pLength)
{
   var lTempString = "";
   lTempString = lTempString + pNumber;
   while (lTempString.length < pLength)
   {
      lTempString = '0' + lTempString;
   }
   return lTempString;
}
/**
 * This method checks if the given string is a valid year and
 * returns the correct year else it returns a value of -1.
 * The function assumes that all values from 0 to 79 (both
 * inclusive) are in the 21st century and years from 80 to 99
 * (both inclusive) are considered to be in the 20th century.
 */
function getYear(pYear)
{
   if (isNumeric(pYear))
   {
      while (pYear.charAt(0) == '0')
	  {
         pYear = pYear.substr(1,(pYear.length - 1))
		 if (pYear.length == 0) break;
	  }
	  if (pYear.length == 0 ) lYear = 0;
	  else lYear = parseInt(pYear);
      if (lYear >= 0)
      {
         if (lYear >= 0 && lYear <= 79)
         {
            lYear = 2000 + lYear;
         }
         else if (lYear < 100)
         {
            lYear = 1900 + lYear;
         }
      }
      else
      {
         lYear = -1;
      }
   }
   else
   {
      lYear = -1;
   }
   return lYear;
}

/**
 * This method checks if the given string is a valid month and
 * returns the correct year else it returns a value of -1.
*/
function getMonth(pMonth)
{
   var lLongMonths = getLongMonths();
   var lShortMonths = getShortMonths();

   if (isNumeric(pMonth))
   {
      while (pMonth.charAt(0) == '0')
	  {
         pMonth = pMonth.substr(1,(pMonth.length - 1))
		 if (pMonth.length == 0) break;
	  }
	  if (pMonth.length == 0 ) lMonth = 0;
	  else lMonth = parseInt(pMonth);
      lMonth = lMonth - 1;
      if ( lMonth < 0 || lMonth > 11 )
      {
         lMonth = -1;
      }
   }
   else
   {
      for (lCount=0;lCount < 12; lCount++)
      {
         if ((lLongMonths[lCount]).toUpperCase() == pMonth.toUpperCase())
         {
            break;
         }
      }
      if (lCount == 12)
      {
         for (lCount=0;lCount < 12; lCount++)
         {
            if ((lShortMonths[lCount]).toUpperCase()  == pMonth.toUpperCase())
            {
               break;
            }
         }
      }
      if (lCount == 12) lMonth = -1;
      else lMonth = lCount;
   }
   return lMonth;
}

/**
 * This method checks if the given string is a valid month and
 * returns the correct year else it returns a value of -1.
 * This method does not check for number of days with respect to
 * the month. It just validates it to be between 1 and 31.
*/
function getDay(pDay)
{
   var lDay;
   if (isNumeric(pDay))
   {
      while (pDay.charAt(0) == '0')
	  {
         pDay = pDay.substr(1,(pDay.length - 1))
		 if (pDay.length == 0) break;
	  }
	  if (pDay.length == 0 ) lDay = 0;
	  else lDay = parseInt(pDay);
      if (lDay < 0 || lDay > 31)
      {
         lDay = -1;
      }
   }
   else
   {
      lDay = -1;
   }
   return lDay;
}

/**
 * This method checks if the given string is a valid Hour and
 * returns the correct year else it returns a value of -1.
*/
function getHour(pHour, pFormat)
{
   if (pHour != null)
   {
      if (isNumeric(pHour))
      {
         while (pHour.charAt(0) == '0')
	     {
            pHour = pHour.substr(1,(pHour.length - 1))
			if (pHour.length == 0) break;
	     }
         if (pHour.length == 0 ) lHour = 0;
         else lHour = parseInt(pHour);
         if (lHour < 0 || lHour > 23)
         {
            lHour = -1;
         }
      }
      else
      {
         lHour = -1;
      }
   }
   else
   {
      lHour = 0;
   }
   return lHour;
}

/**
 * This method checks if the given string is a valid Minute and
 * returns the correct year else it returns a value of -1.
*/
function getMinute(pMinute)
{
   var lMinute;
   if (pMinute != null)
   {
      if (isNumeric(pMinute))
      {
         while (pMinute.charAt(0) == '0')
	     {
            pMinute = pMinute.substr(1,(pMinute.length - 1))
			if (pMinute.length == 0) break;
	     }
         if (pMinute.length == 0 ) lMinute = 0;
         else lMinute = parseInt(pMinute);
         if (lMinute < 0 || lMinute > 59)
         {
            lMinute = -1;
         }
      }
      else
      {
         lMinute = -1;
      }
   }
   else
   {
      lMinute = 0;
   }
   return lMinute;
}

/**
 * This method checks if the given string is a valid Second and
 * returns the correct year else it returns a value of -1.
*/
function getSecond(pSecond)
{
   var lSecond;
   if (pSecond != null)
   {
      if (isNumeric(pSecond))
      {
         while (pSecond.charAt(0) == '0')
	     {
            pSecond = pSecond.substr(1,(pSecond.length - 1))
			if (pSecond.length == 0) break;
	     }
         if (pSecond.length == 0 ) lSecond = 0;
         else lSecond = parseInt(pSecond);
         if (lSecond < 1 || lSecond > 59)
         {
            lSecond = -1;
         }
      }
      else
      {
         lSecond = -1;
      }
   }
   else
   {
      lSecond = 0;
   }
   return lSecond;
}
/**
 * This method checks if the given string is a valid Millisecond and
 * returns the correct year else it returns a value of -1.
*/
function getMilliSecond(pMilliSecond)
{
   var lMilliSecond;
   if (pMilliSecond != null)
   {
      if (isNumeric(pMilliSecond))
      {
         while (pMilliSecond.charAt(0) == '0')
	     {
            pMilliSecond = pMilliSecond.substr(1,(pMilliSecond.length - 1))
			if (pMilliSecond.length == 0) break;
	     }
         if (pMilliSecond.length == 0 ) lMilliSecond = 0;
         else lMilliSecond = parseInt(pMilliSecond);
         if (lMilliSecond < 1 || lMilliSecond > 999)
         {
            lMilliSecond = -1;
         }
      }
      else
      {
         lMilliSecond = -1;
      }
   }
   else
   {
      lMilliSecond = 0;
   }
   return lMilliSecond;
}
/**
 * This method checks of the given string is a valid AM/PM string
 * for the given locale.
 */
function getAMPM (pAMPM)
{
   var lAMPMStrings = new Array(2);

   lAMPMStrings[0] = "AM";
   lAMPMStrings[1] = "PM";

   if (pAMPM == null) return "AM";
   pAMPM = trim(pAMPM);
   if (pAMPM.toUpperCase() == (lAMPMStrings[0]).toUpperCase()) return "AM";
   else if (pAMPM.toUpperCase() == (lAMPMStrings[1]).toUpperCase()) return "PM";
   else return null;
}

/**
 * This method parse a given date string and returns an array of
 * the different substrings. It delimits the string by any non
 * alpha numeric characters and any change of numeric to alpha.
 */
function parseDate(pDate)
{
   var lDate = new Array();
   var lCount = 0;
   var lAllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   var lStringsCount = 0;
   var lIsNumeric;
   var lIsAlpha;

   lDate[lStringsCount] = "";
   lIsNumeric = isNumeric(pDate.charAt(lCount));
   lIsAlpha = isAlpha(pDate.charAt(lCount));
   while (lCount < pDate.length)
   {
      lTempChar = (pDate.charAt(lCount)).toUpperCase();
      if (lAllowedChars.indexOf(lTempChar) >= 0)
      {
         if (lIsAlpha && isAlpha(lTempChar) ||
             lIsNumeric && isNumeric(lTempChar) )
         {
            lDate[lStringsCount] = lDate[lStringsCount] + lTempChar;
         }
         else
         {
            lStringsCount++;
            lDate[lStringsCount] = "";
            lIsNumeric = isNumeric(pDate.charAt(lCount + 1));
            lIsAlpha = isAlpha(pDate.charAt(lCount + 1));
         }
      }
      else
      {
         lStringsCount++;
         lDate[lStringsCount] = "";
         lIsNumeric = isNumeric(pDate.charAt(lCount + 1));
         lIsAlpha = isAlpha(pDate.charAt(lCount + 1));
      }
      lCount++;
   }
   return lDate;
}

/**
 * This method parse a given date format string and returns an 
 * array of the different substrings. It delimits the string by 
 * any non alpha numeric characters.
 */
function parseFormat(pFormat, pRetain)
{
   var lFormat = new Array();
   var lCount = 0;
   var lTempChar;
   var lAllowedCharacters = "yMdhHmsSa";
   var lStringsCount = 0;

   lFormat[lStringsCount] = "";
   while (lCount < pFormat.length)
   {
      lTempChar = pFormat.charAt(lCount);
      if (lAllowedCharacters.indexOf(lTempChar) >= 0)
      {
         lFormat[lStringsCount] = lFormat[lStringsCount] + lTempChar;
      }
      else
      {
         lStringsCount++;
         lFormat[lStringsCount] = lTempChar;
		 if (pRetain) lFormat[++lStringsCount] = "";
		 else lFormat[lStringsCount] = "";
      }
      lCount++;
   }
   //alert(pFormat + " " + pRetain + " Number in array" + lFormat.length);
   return lFormat;
}

/**
 * This method checks if the day is valid with respect to
 * the number of days in the month in the year specified.
 */
function isDayValid (pYear, pMonth, pDay)
{
   var lIsDayValid = false;

   switch (pMonth)
   {
   case 0:
      if (pDay <= 31) lIsDayValid = true;
         break;
   case 1:
      if ((pYear % 100 == 0 && pYear % 400 == 0) ||
          (pYear % 100 != 0 && pYear % 4 == 0))
	  {
         if (pDay <= 29) lIsDayValid = true;
	  }
      else if (pDay <= 28)
      {
         lIsDayValid = true;
      }
         break;
   case 2:
      if (pDay <= 31) lIsDayValid = true;
         break;
   case 3:
      if (pDay <= 30) lIsDayValid = true;
         break;
   case 4:
      if (pDay <= 31) lIsDayValid = true;
         break;
   case 5:
      if (pDay <= 30) lIsDayValid = true;
         break;
   case 6:
      if (pDay <= 31) lIsDayValid = true;
         break;
   case 7:
      if (pDay <= 31) lIsDayValid = true;
         break;
   case 8:
      if (pDay <= 30) lIsDayValid = true;
         break;
   case 9:
      if (pDay <= 31) lIsDayValid = true;
         break;
   case 10:
      if (pDay <= 30) lIsDayValid = true;
         break;
   case 11:
      if (pDay <= 31) lIsDayValid = true;
         break;
   }
   return lIsDayValid;
}

/**
 * This method checks if the day is valid with respect to
 * the number of days in the month in the year specified.
 */
function getNumberOfDaysInMonth (pYear, pMonth)
{

   switch (pMonth)
   {
   case 0: return 31;
   case 1:
      if ((pYear % 100 == 0 && pYear % 400 == 0) ||
          (pYear % 100 != 0 && pYear % 4 == 0)) return 29;
	  else return 28;
   case 2: return 31;
   case 3: return 30;
   case 4: return 31;
   case 5: return 30;
   case 6: return 31;
   case 7: return 31;
   case 8: return 30;
   case 9: return 31;
   case 10: return 30;
   case 11: return 31;
   }
}

//-->
