/* Date functions 
   Written by wjg ,Last modified :2000-6-23
*/
/* function monthdays
    returns the number of days of certain month 
    determined by parameter p_year,p_month;
*/
 function monthdays(p_year,p_month)
  {
    if(p_month==1 || p_month==3 || p_month==5 || p_month==7 || p_month==8 ||p_month==10 || p_month==12 )
      return 31;
    if(p_month==4 || p_month==6 || p_month==9 || p_month==11 )
      return 30;
    if(p_month==2 && ((p_year%4==0 && p_year%100!=0) || p_year%400==0 ))
      return 29;
    return 28;
 }
 function nextmonth(p_year,p_month) {
   p_month=parseInt(p_month);
   if(p_month<12)  
    {p_month +=1; }
   else  {
      p_year +=1;
      p_month =1;
   }
   return(p_year+"-"+p_month);
  }
     
 function nextdate(p_year,p_month,p_day)  {
   if(p_day<monthdays(p_year,p_month)) 
      {p_day +=1; }
   else{
     if(p_month<12) {
         p_month +=1;
         p_day = 1;
      }
     else {
         p_year +=1;
         p_month =1;
         p_day   =1;
      }
    }
   return(p_year+"-"+p_month+"-"+p_day);
  }

/* function validdate 
   return false if inputs is not a valid date,
   return true if it is.

   Parameter :p_year,p_month,p_day  integer;
*/
 function validdate(p_year,p_month,p_day)
  {
   if(!(!isNaN(p_year) && p_year>1000 && p_year<=9999))
        return false;
   if(!(!isNaN(p_month) && p_month>=1 && p_month<=12))
        return false;
   if(!(!isNaN(p_day) && p_day>=1 && p_day<=monthdays(p_year,p_month)))
        return false;
   return true;
  }
 /* function isDates 
    return false if input is a valid date
    return true if it is
    
    Parameter :p_date a string with delimeter formatted like year-mon-day
               for example : 2000-2-2,2000/2/2 
 */   
 function isDates(p_date) {
    deli ='';
    for(i=0;i<=p_date.length;i++)
      { c=p_date.substring(i,i+1);
     switch (c) {
        case '-' : deli = '-'
                   break;
        case '/' : deli = '/'
                   break;  
        case ':' : deli = ':'
                   break;  
        case '.' : deli = '.'
                   break;  
        case ',' : deli = ','
                   break;  
        case "'" : deli = "'"
                   break;  
        }
      if (deli!="") break;
     }
    if(deli=="") return false;
    l_date=p_date.split(deli);
    if(l_date.length>3) return false;
    if(!validdate(l_date[0],l_date[1],l_date[2]))
       return false;
    return true;
  }

