
  // Call chkdate() method defined below
  function checkdate(objName) 
  {
     var datefield = objName;
     if ( datefield.value.length != 0 )
     {  
        if (chkdate(objName) == false) 
        {
          datefield.select();
          alert("That date is invalid.  Please try again.");
          datefield.focus();
          return false;
        }
        else 
        {
          return true;
        }
      }
      else
         return true;
  }

  // Date Format Validation
  function chkdate(objName) 
  {
     var strDate;
     var strDay;
     var strMonth;
     var strYear;
     var intday;
     var intMonth;
     var intYear;
     var datefield = objName;
     var dateFormat = (document.form.date_format.value).toUpperCase();
     var dayIndex;
     var monthIndex;
     var yearIndex;
     var strDatestyle = "";  
     datFormat = dateFormat.substr(1,dateFormat.length-2);
     
     dayIndex   =  datFormat.indexOf("DD");
     if ( datFormat.length == 6 || datFormat.length == 9 || datFormat.length == 11  )
        monthIndex =  datFormat.indexOf("MON");
     else
        monthIndex =  datFormat.indexOf("MM");
     yearIndex  =  datFormat.indexOf("YYYY");

     var err = 0;

     var strMonthArray = new Array(12);
     if ( datFormat.length == 9 || datFormat.length == 11 )
     { 
       strMonthArray[0] = "Jan";
       strMonthArray[1] = "Feb";
       strMonthArray[2] = "Mar";
       strMonthArray[3] = "Apr";
       strMonthArray[4] = "May";
       strMonthArray[5] = "Jun";
       strMonthArray[6] = "Jul";
       strMonthArray[7] = "Aug";
       strMonthArray[8] = "Sep";
       strMonthArray[9] = "Oct";
       strMonthArray[10] = "Nov";
       strMonthArray[11] = "Dec";
     }
     else if ( datFormat.length == 6 || datFormat.length == 8 || datFormat.length == 10 )
     {
       strMonthArray[0] = "01";
       strMonthArray[1] = "02";
       strMonthArray[2] = "03";
       strMonthArray[3] = "04";
       strMonthArray[4] = "05";
       strMonthArray[5] = "06";
       strMonthArray[6] = "07";
       strMonthArray[7] = "08";
       strMonthArray[8] = "09";
       strMonthArray[9] = "10";
       strMonthArray[10] = "11";
       strMonthArray[11] = "12";

     }
       strDate = datefield.value;
       if( strDate.length != datFormat.length )
       {
          alert("Enter the Valid Date ");
          objName.focus();
       }
 
        if (strDate.length>5) 
        {
           strDay = strDate.substr(dayIndex, 2);
           if ( datFormat.length == 6 || datFormat.length == 9 || datFormat.length == 11 )
           {
              strMonth = strDate.substr(monthIndex, 3);
              strMonth = findIntMonth(strMonth);
           }
              
           else 
              strMonth = strDate.substr(monthIndex, 2);
           strYear = strDate.substr(yearIndex,4);
        }
        if ( datFormat.length == 6 || datFormat.length == 9 || datFormat.length == 11 || datFormat.length == 10 )
        {
          if((dayIndex+2) == (datFormat.length))
             strSeparator = datFormat.substr(yearIndex+4,1);
          if( (yearIndex+4) == (datFormat.length) )
             strSeparator = datFormat.substr(dayIndex+2,1);
        }
        else
          strSeparator = "";
 
     
     if (strYear != "" && strYear.length == 2) 
     {
        strYear = '20' + strYear;
     }
   
     intday = parseInt(strDay, 10);
     if (isNaN(intday)) {
     err = 2;
     return false;
   }

   intMonth = parseInt(strMonth, 10);
   if (isNaN(intMonth)) 
   {
      for (i = 0;i<12;i++) 
      {
         if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
         {
            intMonth = i+1;
            strMonth = strMonthArray[i];
            i = 12;
         }
      }
    
      if (isNaN(intMonth)) 
      {
         err = 3;
         return false;
      }
   }
   intYear = parseInt(strYear, 10);
   if (isNaN(intYear)) 
   {
      err = 4;
      return false;
   }
   
   if (intMonth>12 || intMonth<1) 
   {
      err = 5;
      return false;
   }
   
   if ((intMonth == 1 || 
        intMonth == 3 || 
        intMonth == 5 || 
        intMonth == 7 || 
        intMonth == 8 || 
        intMonth == 10 || 
        intMonth == 12)     && (intday > 31 || intday < 1)) 
   {
      err = 6;
      return false;
   }
   
   if ((intMonth == 4 || 
        intMonth == 6 || 
        intMonth == 9 || 
        intMonth == 11) 
    && (intday > 30 || intday < 1)) 
   {
      err = 7;
      return false;
   }
   
   if (intMonth == 2) 
   {
      if (intday < 1) 
      {
         err = 8;
         return false;
      }
      if (LeapYear(intYear) == true) 
      {
         if (intday > 29) 
         {
            err = 9;
            return false;
         }
      }
      else 
      {
         if (intday > 28) 
         {
            err = 10;
            return false;
         }
      }
   }
   if (strDatestyle == "US") 
   {
      strDay == intday.toString();
      if ( strDay.length == 1 )
         strDay = "0"+ strDay ;
      else 
        strDay = strDay;

      if ( strSeparator == "" ) 
         datefield.value = strMonthArray[intMonth-1] + strDay + strYear;
      else                                
         datefield.value = strMonthArray[intMonth-1] + strSeparator + strDay + strSeparator + strYear;
   }
   else 
   { 
      strDay == intday.toString();
      if ( strDay.length == 1 )
         strDay = "0"+ strDay ;
      else
        strDay = strDay;

      if ( (yearIndex < monthIndex) && (monthIndex < dayIndex ) )
      { 
         if ( strSeparator == "" ) 
            datefield.value = strYear + strMonthArray[intMonth-1] + strDay ;
         else                                
            datefield.value = strYear + strSeparator + strMonthArray[intMonth-1] + strSeparator + strDay;
      }
      if ( (yearIndex < dayIndex) && (dayIndex < monthIndex) )
      { 
         if ( strSeparator == "" ) 
            datefield.value = strYear + strDay + strMonthArray[intMonth-1];
         else                                
            datefield.value = strYear + strSeparator + strDay + strSeparator + strMonthArray[intMonth-1];
      }
      if ( (monthIndex < yearIndex) && (yearIndex < dayIndex) )
      { 
         if ( strSeparator == "" ) 
            datefield.value = strMonthArray[intMonth-1] + strYear + strDay;
         else                                
            datefield.value = strMonthArray[intMonth-1] + strSeparator + strYear + strSeparator + strDay;
      }
      if ( (monthIndex < dayIndex) && (dayIndex < yearIndex) )
      { 
         if ( strSeparator == "" ) 
            datefield.value = strMonthArray[intMonth-1] + strDay + strYear;
         else                                
            datefield.value = strMonthArray[intMonth-1] + strSeparator + strDay + strSeparator + strYear;
      }
      if ( (dayIndex < yearIndex) && (yearIndex < monthIndex) )
      { 
         if ( strSeparator == "" ) 
            datefield.value = strDay + strYear + strMonthArray[intMonth-1];
         else                                
            datefield.value = strDay + strSeparator + strYear + strSeparator + strMonthArray[intMonth-1];
      }
      if ( (dayIndex < monthIndex) && (monthIndex < yearIndex) )
      { 
         if ( strSeparator == "" ) 
            datefield.value = strDay + strMonthArray[intMonth-1] + strYear;
         else                                
            datefield.value = strDay + strSeparator + strMonthArray[intMonth-1] + strSeparator + strYear;
      }
    }
   return true;
}

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

function findIntMonth( strMonth )
{
  //alert( 'Input-'+strMonth );
  var numMonth = 0;
  if ( strMonth == "Jan" ) numMonth = 1;
  else
  if ( strMonth == "Feb" ) numMonth = 2;
  else
  if ( strMonth == "Mar" ) numMonth = 3;
  else
  if ( strMonth == "Apr" ) numMonth = 4;
  else
  if ( strMonth == "May" ) numMonth = 5;
  else
  if ( strMonth == "Jun" ) numMonth = 6;
  else
  if ( strMonth == "Jul" ) numMonth = 7;
  else
  if ( strMonth == "Aug" ) numMonth = 8;
  else
  if ( strMonth == "Sep" ) numMonth = 9;
  else
  if ( strMonth == "Oct" ) numMonth = 10;
  else
  if ( strMonth == "Nov" ) numMonth = 11;
  else
  if ( strMonth == "Dec" ) numMonth = 12;
  //alert( 'Output-'+numMonth );
  return numMonth;
}


function doDateCheck(evt, from, to) 
{
   //--------------------------------------------------------
   // SET DATE_FORMAT in hidden field DATE_FORMAT
   // SET TODAY_DATE in hidden field TODAY_DATE
   // Give Label name of each date field as per SUNVISION STD
   // for > futute date, <to) param must by doc object of today_date
   // for < futute date, <from) param must by doc object of today_date
   //--------------------------------------------------------

   //--------------------------------------------------------
   // ADDED BY RV
   if ( !from || !to )
   {
     jAlert('Invalid date field', 'Sunvision Alert Says');
     return false;
   }
   var lDateFormat = document.getElementById('date_format');
   if ( !lDateFormat )
   {
     jAlert('Missing standard date format', 'Sunvision Alert Says');
     return false;
   }
   //--------------------------------------------------------

   //--------------------------------------------------------
   // ADDED BY RV
   //alert( from.value+ ' ' + to.value );
   //alert( from.name+ ' ' + to.name );
   var fromLabel  = '';
   var toLabel    = '';
   if ( from.name == 'today_date' )
     fromLabel  = 'Today date';
   else
   {
     if ( document.getElementById('lable_'+from.name) )
       fromLabel  = document.getElementById('lable_'+from.name).abbr;
     else
       fromLabel  = from.name;
   }
   if ( to.name == 'today_date' )
     toLabel    = 'Today date';
   else
   {
     if ( document.getElementById('lable_'+to.name) )
       toLabel    = document.getElementById('lable_'+to.name).abbr;
     else
       toLabel    = to.name;
   }
   //alert( fromLabel+ ' ' + toLabel );
   //--------------------------------------------------------

   if (from.value.length != 0 && to.value.length != 0)
   {
      //datFormat = (document.form.date_format.value).toUpperCase();
      datFormat = (lDateFormat.value).toUpperCase();
      datFormat = datFormat.substr(1,datFormat.length-2);
      dayIndex   =  datFormat.indexOf("DD");
      if ( datFormat.length == 6 || datFormat.length == 9 || datFormat.length == 11 )
         monthIndex =  datFormat.indexOf("MON");
      else
        monthIndex =  datFormat.indexOf("MM");
      yearIndex  =  datFormat.indexOf("YYYY");

      fromDay   = parseInt((from.value).substr(dayIndex,2),10);
      //alert('fromDay=>'+fromDay);
      if ( datFormat.length == 6 || datFormat.length == 9 || datFormat.length == 11 )
        //fromMonth = parseInt(findIntMonth((from.value).substr(monthIndex,3)));
        fromMonth = findIntMonth((from.value).substr(monthIndex,3));
      else
        fromMonth =  parseInt((from.value).substr(monthIndex,2),10);
      fromYear  =  parseInt((from.value).substr(yearIndex,4));
       
      toDay   = parseInt(to.value.substr(dayIndex,2),10);
      //alert('toDay=>'+to.value+' '+' '+dayIndex+' '+to.value.substr(dayIndex,2)+' '+parseInt(to.value.substr(dayIndex,2))+' '+parseInt('08',10)+' '+toDay);
      if ( datFormat.length == 6 || datFormat.length == 9 || datFormat.length == 11 )
      {
        toMonth = findIntMonth((to.value).substr(monthIndex,3));
        //alert( 'datFormat.length '+datFormat.length+' '+toMonth );
      }
      else
        toMonth =  parseInt((to.value).substr(monthIndex,2),10);
      toYear  =  parseInt((to.value).substr(yearIndex,4));


      var fromDayStr   = '';
      var toDayStr     = '';
      var fromMonthStr = '';
      var toMonthStr   = '';
      //alert('(Num) From =>'+fromDay+'-'+fromMonth+'-'+fromYear+' To =>'+toDay+'-'+toMonth+'-'+toYear)
      if ( fromMonth < 10 ) fromMonthStr = '0'+fromMonth; else fromMonthStr = ''+fromMonth;
      if ( toMonth < 10 ) toMonthStr = '0'+toMonth; else toMonthStr = ''+toMonth;
      if ( fromDay < 10 ) fromDayStr = '0'+fromDay; else fromDayStr = ''+fromDay;
      if ( toDay < 10 ) toDayStr = '0'+toDay; else toDayStr = ''+toDay;
      //alert('(Str) From =>'+fromDayStr+'-'+fromMonthStr+'-'+fromYear+' To =>'+toDayStr+'-'+toMonthStr+'-'+toYear)
      if ( parseInt(fromYear+''+fromMonthStr+''+fromDayStr) <= parseInt(toYear+''+toMonthStr+''+toDayStr) )
        ;
      else
      if ( ( fromDay <= toDay ) && ( ( fromMonth >= toMonth ) && ( fromYear >= toYear ) ) ) 
      {
        jAlert(toLabel+" must occur after "+fromLabel);
        to.focus();
        //------------------------------------------------------------
        var lBrowserName = getBrowserName();
        if ( lBrowserName == 'Microsoft Internet Explorer' )
          window.event.returnValue=false;
        else
        if ( lBrowserName == 'Netscape' )
        {
          evt.preventDefault();
          evt.stopPropagation();
        }
        //------------------------------------------------------------
        return false;
      }
      else 
      {
        if (to.name == 'today_date')
        {
          jAlert(fromLabel+" should not be future date");
          from.focus();
          //------------------------------------------------------------
          var lBrowserName = getBrowserName();
          if ( lBrowserName == 'Microsoft Internet Explorer' )
            window.event.returnValue=false;
          else
          if ( lBrowserName == 'Netscape' )
          {
            evt.preventDefault();
            evt.stopPropagation();
          }
          //------------------------------------------------------------
          return false;
        }
        else
        if (from.name == 'today_date')
        {
          jAlert(toLabel+" should be future date");
          to.focus();
          //------------------------------------------------------------
          var lBrowserName = getBrowserName();
          if ( lBrowserName == 'Microsoft Internet Explorer' )
            window.event.returnValue=false;
          else
          if ( lBrowserName == 'Netscape' )
          {
            evt.preventDefault();
            evt.stopPropagation();
          }
          //------------------------------------------------------------
          return false;
        }
        else
        {
          jAlert(toLabel+" must occur after "+fromLabel);
          to.focus();
          //------------------------------------------------------------
          var lBrowserName = getBrowserName();
          if ( lBrowserName == 'Microsoft Internet Explorer' )
            window.event.returnValue=false;
          else
          if ( lBrowserName == 'Netscape' )
          {
            evt.preventDefault();
            evt.stopPropagation();
          }
          //------------------------------------------------------------
          return false;
        } 
      }
   }
   return true;
}

function doDateCheckByName(evt, from_field_name, to_field_name, today_date_field_name, is_from_future, is_to_future )
{
  var lReturnValue = 0;
  var DATE_OK = false;
  var lTodayDate = document.getElementById( today_date_field_name );
  var lStartDate = document.getElementById( from_field_name );
  var lEndDate   = document.getElementById( to_field_name );
  //alert( from_field_name+'#'+to_field_name+'#'+today_date_field_name+'#'+is_from_future+'#'+is_to_future )
  //alert( lTodayDate.value+'###'+lStartDate.value+'#'+lEndDate.value )
  if ( is_from_future )
  {
    if ( ( lStartDate && lTodayDate ) && ( lStartDate.value.length > 0 && lTodayDate.value.length > 0 ) )
    {
      DATE_OK = doDateCheck( evt, lTodayDate, lStartDate );
      if ( !DATE_OK ) return -1;
    }
  }
  //alert( lTodayDate.value+'###'+lStartDate.value+'#'+lEndDate.value )
  if ( is_to_future )
  {
    if ( ( lEndDate && lTodayDate ) && ( lEndDate.value.length > 0 && lTodayDate.value.length > 0 ) )
    {
      DATE_OK = doDateCheck( evt, lTodayDate, lEndDate );
      if ( !DATE_OK ) return -1;
    }
  }
  //alert( lTodayDate.value+'###'+lStartDate.value+'#'+lEndDate.value )
  if ( ( DATE_OK ) && ( lStartDate && lEndDate ) && ( lStartDate.value.length > 0 && lEndDate.value.length > 0 ) )
  {
    doDateCheck( evt, lStartDate, lEndDate );
    if ( !DATE_OK ) return -1;
  }
  //alert( lTodayDate.value+'###'+lStartDate.value+'#'+lEndDate.value )
  return lReturnValue;
}


function doReverseDateCheck(from, to) 
{
  if ( !doDateCheck(from, to) ) from.focus(); 
} 
//  End -->

