/* 
COPYRIGHT EC-SERVER ALL RIGHT RESERVED
Author : Carl 
$Id: date.js,v 1.1.1.1 2007/09/14 08:49:02 carl Exp $

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 dateUtil() {
	return this;	
}

/**
*   get total days about specify month;

*   @param integer p_year
*   @param integer p_month
*   @return integer total days.
*/
dateUtil.prototype.monthdays = function (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;
}

/**
*   get specify month's next month;

*   @param integer p_year
*   @param p_month
*   @return integer next month
*/
dateUtil.prototype.nextmonth = function(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);
}

/**
*  get specify date's next date;

*  @param integer p_year
*  @param integer p_month
*  @param integer p_day
*  @return integer next day;
*/
dateUtil.prototype.nextdate = function(p_year,p_month,p_day)  {
   if(p_day<this.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);
}

/**
*  check if inputs is  valid date ;

*  @param integer p_year
*  @param integer p_month
*  @param integer p_date
*  @return bool
*/

dateUtil.prototype.validdate = function (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<=this.monthdays(p_year,p_month)))
      return false;
   return true;
}
 /* function isDates 
    
  *  @param string p_date a string with delimeter formatted like year-mon-day
               for example : 2000-2-2,2000/2/2 
  *  @return bool
*/   
dateUtil.prototype.isDates = function(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;
	try {
   	if(!this.validdate(l_date[0],l_date[1],l_date[2]))
      	return false;
	}catch(e){return false;}
   return true;
}
