var CalendarAPI = Class.create();

CalendarAPI.prototype =
{
    /**
     * @param site Site identifierare
     * @param acitvity Aktivitet
     */
    initialize: function(site,lang,activity)
    {
    	this.site = site;
    	this.lang = lang;
    	this.activity = activity;

    	this.callResult = null;
    	this.callResult2 = null;
    	
    	this.ageCache = $H();
    },
    /**
     * @param date Datum att kolla
     *
     * @return 0 = OK
     *         1 = Ogiltigt datum format
     *         2 = Brott mot kalender regler
     *         3 = Fel vid koll
     */
    validate: function(date,opt)
    {
      if(!this.isDate(date))
          return 1;

      return this.isValidperiod(date,opt);

    },

    /**
     * @param val Ett datum YYYY-MM-DD
     *
     * @return true om gilltigt datum annars falskt
     */
    isDate: function(val)
    {
      return isDate(val);
    },
    isValidperiod: function(date,opt)
    {
       this.callResult = null;

       var options;

       if(opt == null)
          options = $H();
       else
            options = opt;

       var p = $H({site: this.site, lang: this.lang, activity: this.activity, date: date, daysForwardKey: options['daysForwardKey'], closeDaysForwardKey: options['closeDaysForwardKey'],limitsStartOverride: options['limitsStartOverride'], limitsEndOverride: options['limitsEndOverride']});

       new Ajax.Request('/app/projects/common/templates/calendar/api/validate.php',{method: 'get',parameters: p,asynchronous: false,onSuccess: this.onSuccess.bind(this)});

       if(this.callResult == 'VALID')
           return 0;
       else if(this.callResult == 'INVALID')
           return 2;
       else
           return 3;
    },
    onSuccess: function(transport)
    {
        this.callResult = transport.responseText;
    },
    /**
     * Kolla att en mängd datum ligger inom samma period,
     * d.v.s. inte är för utspridda i tiden.
     *
     * @param p en lista med datum par
     */
    isWithinSamePeriod: function(list)
    {
        var p = $H({site: this.site, lang: this.lang, activity: this.activity});
        // OK, serialisera
        for(var i = 0; i < list.length;i++)
        {
            p['dates[' + i + '][arrdate]'] = list[i].arrdate;
            p['dates[' + i + '][depdate]'] = list[i].depdate;
        }
       new Ajax.Request('/app/projects/common/templates/calendar/api/validatePeriod.php',{method: 'get',parameters: p,asynchronous: false,onSuccess: this.onSuccess.bind(this)});

       if(this.callResult == 'VALID')
           return true;
       else
           return false;
    },
    onSucces2: function(transport)
    {
        this.callResult = transport.responseText;
    },
    getAge: function(birthdate,date)
    {
       // OK, har vi cachat ?
       var cachedAge = this.ageCache[birthdate + ':' + date];
       if(cachedAge)
           return cachedAge;

       this.callResult = null;
       var p = $H({site: this.site, lang: this.lang, activity: this.activity, date: date, birthdate: birthdate});

       new Ajax.Request('/app/projects/common/templates/calendar/api/getAge.php',{method: 'get',parameters: p,asynchronous: false,onSuccess: this.onSuccess.bind(this)});

       if(this.callResult > 0)
       {
         // OK, save in cache ..
         this.ageCache[birthdate + ':' + date] = this.callResult;
       }
       return this.callResult;
    }
};

/**
 * @param val Ett datum YYYY-MM-DD
 *
 * @return true om gilltigt datum annars falskt
 */
isDate=function(val)
{
  if(val == null)
      return false;

  // Vi kollar "grovt"
  if(!val.match(/^\d{4}-[01]{1}\d{1}-[0123]{1}\d{1}$/))
      return false;

  // Kolla exakt ..
  var yyyy = parseInt(val.substr(0,4),10);
  var mm = parseInt(val.substr(5,2),10);
  var dd = parseInt(val.substr(8,2),10);

  var leap = (yyyy == (parseInt(yyyy/4) * 4)); // && !(yyyy == (parseInt(yyyy/100) * 100)));

  if(leap && (yyyy == (parseInt(yyyy/100) * 100)))
    leap = (yyyy == (parseInt(yyyy/400) * 400));

  if (!((mm >= 1) && (mm <= 12)))
     return false;

  if ((mm == 2) && (leap))
     dom = 29;
  if ((mm == 2) && !(leap))
     dom = 28;

  if ((mm == 1) || (mm == 3) || (mm == 5) || (mm == 7) || (mm == 8) || (mm == 10) || (mm == 12))
     dom = 31;

  if ((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11))
     dom = 30;

  if (dd > dom)
     return false;
  return true;
}

function DatePair(arrdate,depdate)
{
  this.arrdate = arrdate;
  this.depdate = depdate;
}


