
<!--  Author: 	   Maria Geary  -->
<!--  Description: Stores all generic Date functions needed by an application  -->
<!--  Name:	   DateUtilities -->


function isYearMth(textObj)
      { 
          yearMth=textObj.value
          lenYearMth=textObj.value.length
          
          var month = parseInt(yearMth.substr(4,6),10)
          var year  = parseInt(yearMth.substr(0,4),10)
          
          var today   = new Date()
	  var currMonth = today.getMonth()+1
	  var currYear  = today.getYear()
	       
	   if (currMonth<10)
	         {
	          currMonth= "0" + currMonth;
	           }
	          
	   if(currYear<1000) 
	          { currYear+=1900;
	           }
	           
	  var year = currYear.toString();
	  var month = currMonth.toString();
	          
	  var currYearMth = year+month;
	  
          
          if (textObj.value == "") 
                {    
	          return true;
	        }
          else
          {  
            if (lenYearMth != null && lenYearMth != 6 )
               {
                if(textObj.value!=textObj.lastvalue)
                  {
	           alert("Please enter Year Month in the format YYYYMM.");
	           textObj.focus();
                   textObj.select();
                   textObj.lastvalue = textObj.value;
                   return false;
	           } 
	        }
	  else
	      {   
	       if( isNaN(year))
	                 {  
	                   if(textObj.value!=textObj.lastvalue)
	                        {
	                         alert("Not a valid Year.Please enter Year Month in the format YYYYMM.")
	                         textObj.focus();
	                         textObj.select();
	                         textObj.lastvalue = textObj.value;
	                         return false;
	                        }
                         }   
          else 
             {  
               if ( isNaN(month) || month < 1 || month > 12 )
	                  {   
	                   if(textObj.value!=textObj.lastvalue)
	                         {
	                          alert("Invalid Month Entered.Must be between 1 and 12." )
	                          textObj.focus();
	                          textObj.select();
	                          textObj.lastvalue = textObj.value;
	                          return false;
	                         }
                           }   
	      } 
	    }
	  }
	  
	  
	  
	  if (yearMth > currYearMth)
	  	         {
	  	         if (textObj.value!=textObj.lastvalue)
	  	              {
	  	              alert("Year/Month cannot be greater than current Year/Month.");
	  	              textObj.focus();
	  	              textObj.select();
	  	              textObj.lastvalue = textObj.value;
	  	              return false;
	  	              }
	  	         else
	  	             { return true;
	  	             }
	               }  
	  
	  
    }
    
 function isMinYearMth(textObj,minYearMth) {
     var date = textObj.value;
     var year = date.substring(0,4);
     var mon  = date.substring(4,6);
     
     var minYear = minYearMth.substring(0,4);
     var minMon  = minYearMth.substring(4,6);
     
     
     var yyyymm = year + mon;
     var minDate = minYear + minMon;
        
     if (yyyymm < minDate)
            {
            if (textObj.value!=textObj.lastvalue)
                 {
                 alert("YearMonth must be later than "+minDate);
                 textObj.focus();
                 textObj.select();
                 textObj.lastvalue = textObj.value;
                 return false;
                 }
            else
                { return true;
                }
       }  
     
     }
 


function isTime(textObj)
      { 
          time=textObj.value
          lenTime=textObj.value.length
          
          var hour   = parseInt(time.substr(0,2),10)
          var minute = parseInt(time.substr(2,4),10)
          
          if (textObj.value == "" ||textObj.value =="24HH") 
                {    
	          return true;
	        }
          else
          {  
            if (lenTime != null && lenTime != 4 )
               {
                if(textObj.value!=textObj.lastvalue)
                  {
	           alert("Please enter Time in 24 Hour clock format HHMM.");
	           textObj.focus();
                   textObj.select();
                   textObj.lastvalue = textObj.value;
                   return false;
	           } 
	         }
	  else
	      {  
	       if ( isNaN(hour) || hour < 0 || hour > 23 )
                  {   
                   if(textObj.value!=textObj.lastvalue)
                     {
                      alert("Invalid Hour Entered.Must be between 00 and 23." )
                      textObj.focus();
                      textObj.select();
                      textObj.lastvalue = textObj.value;
                      return false;
                     }
                  }   
          else 
             {  
               if( isNaN(minute) || minute < 0 || minute > 59 )
                  {  
                   if(textObj.value!=textObj.lastvalue)
                     {
                      alert("Invalid minute. Must be between 00 and 59.")
                      textObj.focus();
                      textObj.select();
                      textObj.lastvalue = textObj.value;
                      return false;
                     }
                   }   
	      } 
	    }
	  }
    }

  
  
function isDate(textObj,format) {
    var date = textObj.value;
    var day  = date.substring(0,2);
    var mon  = date.substring(2,4);
    var year;
    var maxdays;
    var errormess = "Date format should be DDMMYYYY or DDMM.";
    
    var today   = new Date();
    var mymonth = today.getMonth()+1;
    var myyear  = today.getYear();
    var myday   = today.getDate();
     
    
        if(myday<10) 
          { myday = "0" + myday;
          }   
     
         if (mymonth<10)
            {
             mymonth= "0" + mymonth;
            }
        
         if(myyear<1000) 
           { myyear+=1900;
           }
         
    myyear  =  myyear.toString();
    mymonth = mymonth.toString();
    myday   = myday.toString();
    
    var mytoday = myyear+mymonth+myday;

    if (textObj.value == "") {    // nulls dont count
       return true;
    }

    
    if (date.length==4)
    {
      if (mon > mymonth) //default year to last year if month submitted is later than current month
        { year = today.getFullYear()-1;
        }
     else 
       { year = today.getFullYear();
        }
    }
    else
      if (date.length!=8)
      {
        if (textObj.value!=textObj.lastvalue)
            {
            alert(errormess);
            textObj.focus();
            textObj.select();
            textObj.lastvalue = textObj.value;
            return false;
            }
        else
           { return true;
            }
      }
      else
      {
        year = date.substring(4,8);
      }

  // validate month
    switch(mon.toUpperCase())
    {
      case "01" :
          maxdays=31
          break;
      case "02" :
          maxdays=29
          break;
      case "03" :
          maxdays=31
          break;
      case "04" :
          maxdays=30
          break;
      case "05" :
          maxdays=31
          break;
      case "06" :
          maxdays=30
          break;
      case "07" :
          maxdays=31
          break;
      case "08" :
          maxdays=31
          break;
      case "09" :
          maxdays=30
          break;
      case "10" :
          maxdays=31
          break;
      case "11" :
          maxdays=30
          break;
      case "12" :
          maxdays=31
          break;
      default :
        if (textObj.value!=textObj.lastvalue)
            { alert("Invalid month. " + errormess);
             textObj.lastvalue = textObj.value;
             textObj.focus();
             textObj.select();
             return false;
             }
         else
          { return true;
           }
    }

// validate day

  var daynumeric = parseInt(day,10);
  if (daynumeric < 1 || daynumeric > parseInt(maxdays) ||
      day.substring(0,1) < "0" || day.substring(0,1) > "3" ||
      day.substring(1,2) < "0" || day.substring(1,2) > "9" ||
      day.length < 2 )
  {
    if (textObj.value!=textObj.lastvalue)
      {alert("Invalid day. " + errormess);
       textObj.focus();
       textObj.select();
       textObj.lastvalue = textObj.value;
       return false;
       }
    else
      { return true;
       }
  }   

// validate 29th of Feb

  if (mon.toUpperCase() == "02" && day == "29" && year % 4 != 0)
  {
    if (textObj.value!=textObj.lastvalue)
      {
      alert("29th February is not valid for this year.");
      textObj.lastvalue = textObj.value;
      textObj.focus();
      textObj.select();
      return false;
      }
     else 
       { return true;
       }
  }
  
  
  
 // If a 4-digit numeric was entered, convert to standard format
    if (date.length==4)
    {
      textObj.value = day + mon + year;
     }
     
   
   // validate todays date
   var yyyymmdd = year + mon + day;
   
   
   if (yyyymmdd > mytoday)
       {
       if (textObj.value!=textObj.lastvalue)
            {
            alert("Date Cannot be later than Todays Date.");
            textObj.focus();
            textObj.select();
            textObj.lastvalue = textObj.value;
            return false;
            }
       else
           { return true;
           }
       }  
  
  
  // validate year
  
  if (year.length < 4 )
    {
      if (textObj.value!=textObj.lastvalue)
        { alert("Invalid year. " + errormess);
          textObj.focus();
          textObj.select();
          textObj.lastvalue = textObj.value;
          return false;
         }
      else
        { return true;
         }
  }   
   
 else  
   return true;
}


function isMinDate(textObj,minStartDate) {
     var date = textObj.value;
     var day  = date.substring(0,2);
     var mon  = date.substring(2,4);
     var year = date.substring(4,8);
     
     var minDay  = minStartDate.substring(0,2);
     var minMon  = minStartDate.substring(2,4);
     var minYear = minStartDate.substring(4,8);
     
    
     var yyyymmdd = year + mon + day;
     var minDate  = minYear + minMon + minDay;
        
     if (yyyymmdd < minDate)
            {
            if (textObj.value!=textObj.lastvalue)
                 {
                 alert("Date must be later than "+minStartDate);
                 textObj.focus();
                 textObj.select();
                 textObj.lastvalue = textObj.value;
                 return false;
                 }
            else
                { return true;
                }
       }  
     
     }


function isExpryDate(textObj) {
    var date = textObj.value;
    var day  = date.substring(0,2);
    var mon  = date.substring(2,4);
    var year;
    var maxdays;
    var errormess = "Date format should be DDMMYYYY or DDMM.";
    
    var today   = new Date();
    var mymonth = today.getMonth()+1;
    var myyear  = today.getYear();
    var myday   = today.getDate();
     
    
        if(myday<10) 
          { myday = "0" + myday;
          }   
     
         if (mymonth<10)
            {
             mymonth= "0" + mymonth;
            }
        
         if(myyear<1000) 
           { myyear+=1900;
           }
         
        
    //var mytoday = myyear+mymonth+myday;

    if (textObj.value == "") {    // nulls dont count
       return true;
    }

    
    if (date.length==4)
    {
       year = today.getFullYear();
     }
    else
      if (date.length!=8)
      {
        if (textObj.value!=textObj.lastvalue)
            {
            alert(errormess);
            textObj.focus();
            textObj.select();
            textObj.lastvalue = textObj.value;
            return false;
            }
        else
           { return true;
            }
      }
      else
      {
        year = date.substring(4,8);
      }

  // validate month
    switch(mon.toUpperCase())
    {
      case "01" :
          maxdays=31
          break;
      case "02" :
          maxdays=29
          break;
      case "03" :
          maxdays=31
          break;
      case "04" :
          maxdays=30
          break;
      case "05" :
          maxdays=31
          break;
      case "06" :
          maxdays=30
          break;
      case "07" :
          maxdays=31
          break;
      case "08" :
          maxdays=31
          break;
      case "09" :
          maxdays=30
          break;
      case "10" :
          maxdays=31
          break;
      case "11" :
          maxdays=30
          break;
      case "12" :
          maxdays=31
          break;
      default :
        if (textObj.value!=textObj.lastvalue)
            { alert("Invalid month. " + errormess);
             textObj.lastvalue = textObj.value;
             textObj.focus();
             textObj.select();
             return false;
             }
         else
          { return true;
           }
    }

// validate day

  var daynumeric = parseInt(day,10);
  if (daynumeric < 1 || daynumeric > parseInt(maxdays) ||
      day.substring(0,1) < "0" || day.substring(0,1) > "3" ||
      day.substring(1,2) < "0" || day.substring(1,2) > "9" ||
      day.length < 2 )
  {
    if (textObj.value!=textObj.lastvalue)
      {alert("Invalid day. " + errormess);
       textObj.focus();
       textObj.select();
       textObj.lastvalue = textObj.value;
       return false;
       }
    else
      { return true;
       }
  }   

// validate 29th of Feb

  if (mon.toUpperCase() == "02" && day == "29" && year % 4 != 0)
  {
    if (textObj.value!=textObj.lastvalue)
      {
      alert("29th February is not valid for this year.");
      textObj.lastvalue = textObj.value;
      textObj.focus();
      textObj.select();
      return false;
      }
     else 
       { return true;
       }
  }
  
  
  
 // If a 4-digit numeric was entered, convert to standard format
    if (date.length==4)
    {
      textObj.value = day + mon + year;
     }
     
  
  
  // validate year
  
  if (year.length < 4 )
    {
      if (textObj.value!=textObj.lastvalue)
        { alert("Invalid year. " + errormess);
          textObj.focus();
          textObj.select();
          textObj.lastvalue = textObj.value;
          return false;
         }
      else
        { return true;
         }
  }   
   
 else  
   return true;
}


function setTodaysDate(textObj)
{
        var d  = new Date();
        var retval=""
        
        if ( d.getDate() > 9 )
                retval += d.getDate();
        else
                retval += "0" + d.getDate()
        if ( d.getMonth() > 8 )
                retval += d.getMonth()+1;
        else
        {
                retval += "0" + (d.getMonth()+1);
        }
        retval += d.getFullYear();
        textObj.value = retval;
}


function incrDate(textObj,n) {
    var date = textObj.value;
    var day  = date.substring(0,2);
    var mon  = date.substring(2,4);
    var year;
    var maxdays;
    
    var d = new Date();
    
    if (textObj.value == "") {    // nulls dont count
       return true;
    }

    
    if (date.length==4)
       { year = d.getFullYear();
       }
    else
      { year = date.substring(4,8);
      }

  // validate month
    switch(mon.toUpperCase())
    {
      case "01" :
          maxdays=31
          break;
      case "02" :
          //maxdays=29
          maxdays=28
          break;
      case "03" :
          maxdays=31
          break;
      case "04" :
          maxdays=30
          break;
      case "05" :
          maxdays=31
          break;
      case "06" :
          maxdays=30
          break;
      case "07" :
          maxdays=31
          break;
      case "08" :
          maxdays=31
          break;
      case "09" :
          maxdays=30
          break;
      case "10" :
          maxdays=31
          break;
      case "11" :
          maxdays=30
          break;
      case "12" :
          maxdays=31
          break;        
    }


  var daynumeric   = parseInt(day,10);
  var monthnumeric = parseInt(mon,10);
  var yearnumeric  = parseInt(year,10);
  
  
  if (mon.toUpperCase() == "02" && (year % 4) == "0") 
      { maxdays=29;
      }
  
  
  if (daynumeric >= parseInt(maxdays))
     { 
       day = "1";
       
        if (mon == "12")
           { mon = "1";
             yearnumeric = yearnumeric+1;
             year=yearnumeric;
            }    
       else
          { monthnumeric = monthnumeric+1;
            mon          = monthnumeric; 
           } 
           
       if (day < 9)
	   { day = "0"+day;
           }  
           
       if (mon < 9)
           { mon = "0"+mon;
           }          
     }
  else 
     {  
        daynumeric = daynumeric+n; 
        day = daynumeric; 
        
        if (day < 9)
	     { day = "0"+day;
             }  
      }
      
    textObj.value = day + mon + year;    
 
}


//jtc
function ddmmyyyy(d)
{
        var retval=""
        if ( d.getDate() > 9 )
                retval += d.getDate()
        else
                retval += "0" + d.getDate()
        if ( d.getMonth() > 8 )
                retval += d.getMonth()+1
        else
        {
                retval += "0" + (d.getMonth()+1)
        }
        retval += d.getFullYear()
        return retval
}
function incrDate(d,n)
{
        var d1 = new Date(d.getFullYear(),d.getMonth(),d.getDate()+n)
        return d1
}
function misDate(df,fmt)
{
        if ( !df.value )
                return false;
        else
                return isDate(df,fmt)
}
function nextDate(df1,df2)
{
        if( misDate(df1,"default") )
        {

                var d1 = convertToDate(df1.value,"default")

                df2.value = ddmmyyyy(incrDate(d1,1))
                df2.select()
        }
}

// from mtsi0008.js
function getMonthNum(monthAbbrev)
{
  monVal = 0
  switch (monthAbbrev)
  {
    case "JAN" :
      monVal = 1
      break
    case "FEB" :
      monVal = 2
      break
    case "MAR" :
      monVal = 3
      break
    case "APR" :
      monVal = 4
      break
    case "MAY" :
      monVal = 5
      break
    case "JUN" :
      monVal = 6
      break
    case "JUL" :
      monVal = 7
      break
    case "AUG" :
      monVal = 8
      break
    case "SEP" :
      monVal = 9
      break
    case "OCT" :
      monVal = 10
      break
    case "NOV" :
      monVal = 11
      break
    case "DEC" :
      monVal = 12
      break
  }
  return monVal
}




function convertToDate(inDateString, picFormat)
{
  var dayVal = inDateString.substring(0,2)
  var monVal
  var yearVal
  if (picFormat == "default" || picFormat == "ddmmyyyy")
  {
    monVal = inDateString.substring(2,4) - 1
    yearVal = inDateString.substring(4,8)
  }
  else
  {
    if (picFormat == "dd-MON-yyyy")
    {
      monVal = getMonthNum(inDateString.substring(3,6)) - 1
      yearVal = inDateString.substring(7,11)
    }
  }
  outDate = new Date(yearVal, monVal, dayVal)
  return outDate
}



function chgDate(df)
{
        var kc = event.keyCode
        if( (kc==38 || kc==40) && isDate(df,"default") )
        {
                switch( kc )
                {
                        case 38: // arrow up
                                df.value = ddmmyyyy(incrDate(convertToDate(df.value,"default"),-1))
                                break
                        case 40: // arrow down
                                df.value = ddmmyyyy(incrDate(convertToDate(df.value,"default"),1))
                                break
                }
        }
}

function isFutureDate(textObj,format) {
    var date = textObj.value;
    var day  = date.substring(0,2);
    var mon  = date.substring(2,4);
    var year;
    var maxdays;
    var errormess = "Date format should be DDMMYYYY or DDMM.";
    
    var today   = new Date();
    var mymonth = today.getMonth()+1;
    var myyear  = today.getYear();
    var myday   = today.getDate();
     
    
        if(myday<10) 
          { myday = "0" + myday;
          }   
     
         if (mymonth<10)
            {
             mymonth= "0" + mymonth;
            }
        
         if(myyear<1000) 
           { myyear+=1900;
           }
         
    myyear  =  myyear.toString();
    mymonth = mymonth.toString();
    myday   = myday.toString();
    
    var mytoday = myyear+mymonth+myday;

    if (textObj.value == "") {    // nulls dont count
       return true;
    }

    
    if (date.length==4)
    {
      if (mon > mymonth) //default year to last year if month submitted is later than current month
        { year = today.getFullYear()-1;
        }
     else 
       { year = today.getFullYear();
        }
    }
    else
      if (date.length!=8)
      {
        if (textObj.value!=textObj.lastvalue)
            {
            alert(errormess);
            textObj.focus();
            textObj.select();
            textObj.lastvalue = textObj.value;
            return false;
            }
        else
           { return true;
            }
      }
      else
      {
        year = date.substring(4,8);
      }

  // validate month
    switch(mon.toUpperCase())
    {
      case "01" :
          maxdays=31
          break;
      case "02" :
          maxdays=29
          break;
      case "03" :
          maxdays=31
          break;
      case "04" :
          maxdays=30
          break;
      case "05" :
          maxdays=31
          break;
      case "06" :
          maxdays=30
          break;
      case "07" :
          maxdays=31
          break;
      case "08" :
          maxdays=31
          break;
      case "09" :
          maxdays=30
          break;
      case "10" :
          maxdays=31
          break;
      case "11" :
          maxdays=30
          break;
      case "12" :
          maxdays=31
          break;
      default :
        if (textObj.value!=textObj.lastvalue)
            { alert("Invalid month. " + errormess);
             textObj.lastvalue = textObj.value;
             textObj.focus();
             textObj.select();
             return false;
             }
         else
          { return true;
           }
    }

// validate day

  var daynumeric = parseInt(day,10);
  if (daynumeric < 1 || daynumeric > parseInt(maxdays) ||
      day.substring(0,1) < "0" || day.substring(0,1) > "3" ||
      day.substring(1,2) < "0" || day.substring(1,2) > "9" ||
      day.length < 2 )
  {
    if (textObj.value!=textObj.lastvalue)
      {alert("Invalid day. " + errormess);
       textObj.focus();
       textObj.select();
       textObj.lastvalue = textObj.value;
       return false;
       }
    else
      { return true;
       }
  }   

// validate 29th of Feb

  if (mon.toUpperCase() == "02" && day == "29" && year % 4 != 0)
  {
    if (textObj.value!=textObj.lastvalue)
      {
      alert("29th February is not valid for this year.");
      textObj.lastvalue = textObj.value;
      textObj.focus();
      textObj.select();
      return false;
      }
     else 
       { return true;
       }
  }
  
  
  
 // If a 4-digit numeric was entered, convert to standard format
    if (date.length==4)
    {
      textObj.value = day + mon + year;
     }
     


  
  // validate year
  
  if (year.length < 4 )
    {
      if (textObj.value!=textObj.lastvalue)
        { alert("Invalid year. " + errormess);
          textObj.focus();
          textObj.select();
          textObj.lastvalue = textObj.value;
          return false;
         }
      else
        { return true;
         }
  }   
   
 else  
   return true;
}

