/**
 *
 * Picabo AjaxFramework
 *
 * @category   PcbJs
 * @package    PcbJs_Calendar
 * @copyright  Copyright (c) 2008 Picabo s.r.o.(http://www.picabo.cz)
 * @author     Lukas Musalek(musalek@picabo.cz)
 * @version    006 0007 01.03.2011 16:45
 * 
 *  
 */

/* ************************************
 *
 *  Description: Array - popup objects holder 
 *
 * ************************************ */

var PCB_Calendar_holder = null;

/* ************************************
 *
 *  Description: simulate object creation and display
 *
 *  see function (constructor) TinyPopup
 *  see TinyPopup.prototype.showPopup()
 *
 * ************************************ */
function setDateUp(el, popup, y_from, y_to, alt_date) {
  PCB_Calendar_holder = new PCB_Calendar(2, el, popup, y_from, y_to, alt_date);
  PCB_Calendar_holder.setDateUp();
}
function setDateDown(el, popup, y_from, y_to, alt_date) {
  PCB_Calendar_holder = new PCB_Calendar(2, el, popup, y_from, y_to, alt_date);
  PCB_Calendar_holder.setDateDown();
}

function setCalendar(el, popup, y_from, y_to, alt_date) {
   PCB_Calendar_holder = new PCB_Calendar(1, el, popup, y_from, y_to, alt_date);
   PCB_Calendar_holder.init();
}

function PCB_Calendar(type, el, popup, y_from, y_to, alt_date) {
  this._type = type;
  this._monthVisible = 0;
  this._popup = popup;
  this._el = el;
  this._altDate=alt_date;
  this._cDate=new Date();
  this._cYear=this._cDate.getFullYear();
  this._cMonth=this._cDate.getMonth();
  this._cDay=this._cDate.getDate();
  this._day = 1;
  this._blankDate = 0;
  if (y_from) this._y_from = y_from;
  else this._y_from = 1910;
  if (y_to) this._y_to = y_to;
  else this._y_to = 2100;
  var datum = document.getElementById(this._el).value.split(".");
  this._cYear = datum[2];
  if (datum[1]>=1 && datum[1]<=12 && datum[2]>=this._y_from && datum[2]<=this._y_to) {
    if (datum[0]>=1 && datum[0]<=this.getDaysPerMonth(datum[1]-1)) this._day=datum[0];
    else this._day=this.getDaysPerMonth(datum[1]-1);
    if (datum[0]>=1 && datum[0]<=this.getDaysPerMonth(datum[1]-1)) this._cDay=datum[0];
    else this._cDay=this.getDaysPerMonth(datum[1]-1);
    this._cYear = datum[2];
    this._cMonth = datum[1]-1;
    datum[0] = this._day;
    var pomDate = new Date(datum[2],datum[1]-1,datum[0]);
    this._cYear=pomDate.getFullYear();
    this._cMonth=pomDate.getMonth();
    this._cDay=pomDate.getDate();
  }  
  else {
    if (this._altDate) {
      var datum = this._altDate.split(".");
      this._cYear = datum[2];
      if (datum[1]>=1 && datum[1]<=12 && datum[2]>=this._y_from && datum[2]<=this._y_to) {
        if (datum[0]>=1 && datum[0]<=this.getDaysPerMonth(datum[1]-1)) this._day=datum[0];
        else this._day=this.getDaysPerMonth(datum[1]-1);
        if (datum[0]>=1 && datum[0]<=this.getDaysPerMonth(datum[1]-1)) this._cDay=datum[0];
        else this._cDay=this.getDaysPerMonth(datum[1]-1);
        this._cYear = datum[2];
        this._cMonth = datum[1]-1;
        datum[0] = this._day;
        var pomDate = new Date(datum[2],datum[1]-1,datum[0]);
        this._cYear=pomDate.getFullYear();
        this._cMonth=pomDate.getMonth();
        this._cDay=pomDate.getDate();
      } else {
        this._blankDate = 1;
        datum = new Date();
        this._cDay = datum.getDate();
        this._day = datum.getDate();
        this._cMonth = datum.getMonth();
        this._cYear = datum.getFullYear();
      }
    } else {
      this._blankDate = 1;
      datum = new Date();
      this._cDay = datum.getDate();
      this._day = datum.getDate();
      this._cMonth = datum.getMonth();
      this._cYear = datum.getFullYear();
    }  
  }
}

PCB_Calendar.prototype.init = function() {
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}

PCB_Calendar.prototype.send = function(day) {
  if (day && day != ' ') {
    this.mM = ((this._cMonth+1)<10)? "0"+(this._cMonth+1) : (this._cMonth+1);
    if (day!="") {
      document.getElementById(this._el).value=day+'.'+(this.mM)+'.'+this._cYear;
      
      eval(this._popup);
      if (document.getElementById(this._el).onchange) {
        document.getElementById(this._el).onchange();
      }
    }
  }  
}

//vypisu kalendar                
PCB_Calendar.prototype.setCalendar = function(dt) {
  
  this._cYear=dt.getFullYear();
  this._cMonth=dt.getMonth();
  this._cDay=dt.getDate();
  this._firstDay = dt.getDay();
  
  if (this._cYear>this._y_to) {
    this._cYear = this._y_to;
    dt = new Date(this._cYear,this._cMonth,this._cDay);
    this._cYear=dt.getFullYear();
    this._cMonth=dt.getMonth();
    this._cDay=dt.getDate();
    this._firstDay = dt.getDay();
  }
  if (this._cYear<this._y_from) {
    this._cYear = this._y_from;
    dt = new Date(this._cYear,this._cMonth,this._cDay);
    this._cYear=dt.getFullYear();
    this._cMonth=dt.getMonth();
    this._cDay=dt.getDate();
    this._firstDay = dt.getDay();
  }
  if (this._type==1) {
    for(i = 1; i < 43 ; i++) {
      document.getElementById("cal_td"+i).innerHTML="<span>&nbsp;</span>";
      document.getElementById("cal_td"+i).className = 'empty';
    }  
    if (this._firstDay==0)
      this._firstDay +=7;
    this._dayspermonth = this.getDaysPerMonth(this._cMonth);
    this._tyden = this._firstDay - 1;
    for(i = 1; i < this._dayspermonth+1; i++){
      i1=(i<10)? "0"+i : i;
      pom = i+this._firstDay-1;
      this._tyden++; 
      document.getElementById("cal_td"+pom).innerHTML='<a href="javascript:PCB_Calendar_holder.send(\''+i1+'\');">'+i1+'</a>';
      if (this._tyden<6) document.getElementById("cal_td"+pom).className = 'normal';
      else {
        document.getElementById("cal_td"+pom).className = 'weekend';
        if (this._tyden==7) this._tyden = 0;
      } 
      if (this._day == i) document.getElementById("cal_td"+pom).className = 'dnes';  
    }
    for(i = pom+1; i < 43; i++) {
      //document.getElementById("td"+i).style.display = 'none;';
    }
    if (document.getElementById("month_from")) {
      document.getElementById("month_from").value = this._cMonth;
      for (var i = 1;i<=12;i++) {
        if (document.getElementById("m_"+i)) {
          if (i==this._cMonth+1) {
            document.getElementById("m_"+i).style.display = "block";
          } else {
            document.getElementById("m_"+i).style.display = "none";
          }
        }
      }
    }
    if (document.getElementById("year_from")) {
      document.getElementById("year_from").value = this._cYear;
      document.getElementById("y_set").innerHTML = this._cYear; 
      /*for (var y = this._y_from;y<=this._y_to;y++) {
        if (document.getElementById("y_"+y)) {
          if (y==this._cYear) {
            document.getElementById("y_"+y).style.display = "block";
          } else {
            document.getElementById("y_"+y).style.display = "none";
          }
        }
      }*/
    }
  }  
  //document.getElementById("month_from").selectedIndex=this._cMonth; //nastavim mesic v selektu
  //document.getElementById("year_from").selectedIndex=this._cYear-parseInt(document.getElementById("year_from").options[0].text);
}

//prevedu rok ze selektu na spravny tvar (template)              
PCB_Calendar.prototype.setNYear = function(y) {
  this._cYear=parseInt(document.getElementById("year_from").options[0].text)+y;
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}

//prevedu mesic ze selektu na spravny tvar (mesic beze zmeny)  (template)         
PCB_Calendar.prototype.setNMonth = function(m) {
  this._cMonth=m;
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}

PCB_Calendar.prototype.setNextMonth = function() {
  this._cMonth = this._cMonth+1;
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}
PCB_Calendar.prototype.setPrewMonth = function() {
  this._cMonth = this._cMonth-1;
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}
PCB_Calendar.prototype.setNextYear = function() {
  this._cYear = this._cYear+1;
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}
PCB_Calendar.prototype.setPrewYear = function() {
  this._cYear = this._cYear-1;
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}
var lastDateTimeChange;
PCB_Calendar.prototype.setDateUp = function() {
  var pom_date = new Date();
  if (!lastDateTimeChange || (pom_date-lastDateTimeChange)>100) {
    lastDateTimeChange = new Date();
    if (this._blankDate!=1) this._cDay = parseInt(this._cDay) + 1;
    this._blankDate = 0;
    this.setCalendar(new Date(this._cYear,this._cMonth,this._cDay));
    this.mM = ((this._cMonth+1)<10)? "0"+(this._cMonth+1) : (this._cMonth+1);
    this.pomDay = ((this._cDay)<10)? "0"+(this._cDay) : (this._cDay);
    document.getElementById(this._el).value=(this.pomDay)+'.'+(this.mM)+'.'+this._cYear;
  }
}
PCB_Calendar.prototype.setDateDown = function() {
  var pom_date = new Date();
  if (!lastDateTimeChange || (pom_date-lastDateTimeChange)>100) {
    lastDateTimeChange = new Date();
    if (this._blankDate!=1) this._cDay = parseInt(this._cDay) - 1;
    this._blankDate = 0;
    this.setCalendar(new Date(this._cYear,this._cMonth,this._cDay));
    this.mM = ((this._cMonth+1)<10)? "0"+(this._cMonth+1) : (this._cMonth+1);
    this.pomDay = ((this._cDay)<10)? "0"+(this._cDay) : (this._cDay);
    document.getElementById(this._el).value=(this.pomDay)+'.'+(this.mM)+'.'+this._cYear;
  }  
}
function setTimeUp(el) {
  var pom_date = new Date();
  if (!lastDateTimeChange || (pom_date-lastDateTimeChange)>100) {
    lastDateTimeChange = new Date();
    if (document.getElementById(el)) {
      var currentTime = new Date();
      var day = currentTime.getDate();
      var month = currentTime.getMonth();
      var year = currentTime.getFullYear();
      var hours = currentTime.getHours();
      var minutes = 0;
      var seconds = currentTime.getSeconds();
      var cas = document.getElementById(el).value.split(":");
      if (cas[0].length==2) {
        if (cas[0].substring(0,1)=='0') {
          hours = parseInt(cas[0].substring(1,2));
        } else {
          hours = parseInt(cas[0]);
        }
      } else if (cas[0].length==1) {
        hours = parseInt(cas[0]);
      }
      hours = hours + 1;
      if (cas[1]) {
        if (cas[1].length==2) {
          if (cas[1].substring(0,1)=='0') {
            minutes = parseInt(cas[1].substring(1,2));
          } else {
            minutes = parseInt(cas[1]);
          }
        } else if (cas[1].length==1) {
          minutes = parseInt(cas[1]);
        }
      } else {
        minutes = 0;
      }   
      var newTime = new Date(year, month, day, hours, minutes, seconds);
      var hours = newTime.getHours();
      var minutes = newTime.getMinutes();
      var seconds = newTime.getSeconds();
      seconds = ((seconds)<10)? "0"+(seconds) : (seconds);
      minutes = ((minutes)<10)? "0"+(minutes) : (minutes);
      hours = ((hours)<10)? "0"+(hours) : (hours);
      document.getElementById(el).value = hours+':'+minutes;
    }
  }  
}
function setTimeDown(el) {
  var pom_date = new Date();
  if (!lastDateTimeChange || (pom_date-lastDateTimeChange)>100) {
    lastDateTimeChange = new Date();
    if (document.getElementById(el)) {
      var currentTime = new Date();
      var day = currentTime.getDate();
      var month = currentTime.getMonth();
      var year = currentTime.getFullYear();
      var hours = currentTime.getHours();
      var minutes = 0;
      var seconds = currentTime.getSeconds();
      var cas = document.getElementById(el).value.split(":");
      if (cas[0].length==2) {
        if (cas[0].substring(0,1)=='0') {
          hours = parseInt(cas[0].substring(1,2));
        } else {
          hours = parseInt(cas[0]);
        }
      } else if (cas[0].length==1) {
        hours = parseInt(cas[0]);
      }
      hours = hours - 1;
      if (cas[1]) {
        if (cas[1].length==2) {
          if (cas[1].substring(0,1)=='0') {
            minutes = parseInt(cas[1].substring(1,2));
          } else {
            minutes = parseInt(cas[1]);
          }
        } else if (cas[1].length==1) {
          minutes = parseInt(cas[1]);
        }
      } else {
        minutes = 0;
      }   
      var newTime = new Date(year, month, day, hours, minutes, seconds);
      var hours = newTime.getHours();
      var minutes = newTime.getMinutes();
      var seconds = newTime.getSeconds();
      seconds = ((seconds)<10)? "0"+(seconds) : (seconds);
      minutes = ((minutes)<10)? "0"+(minutes) : (minutes);
      hours = ((hours)<10)? "0"+(hours) : (hours);
      document.getElementById(el).value = hours+':'+minutes;
    }
  }
}
function checkTime(el) {
  if (document.getElementById(el)) {
    var cas = document.getElementById(el).value.split(':');
    var cas2 = document.getElementById(el).value;
    var splitter = ':';
    var minutes = '';
    var hours = '';
    //alert(cas+' '+cas[0][1]+' '+cas[0][2]+' '+cas[0][3]+' '+cas[0][4]);
    if (!cas[1] && cas[0].substring(2,3)) {
      if (cas[0].substring(1,2)) {
        hours = cas[0].substring(0,1)+cas[0].substring(1,2);
      } else {
        hours = cas[0].substring(0,1);
      }
      if (parseInt(hours)>23) {
        hours = parseInt(cas[0].substring(0,1));
        if (!cas[0].substring(2,3)) {
          minutes = cas[0].substring(1,2);
        } else {
          minutes = cas[0].substring(1,2) + cas[0].substring(2,3);
        }
      }
      if (minutes=='') {
        if (!cas[0].substring(3,4) && cas[0].substring(2,3)>5) {
          minutes = '0' + cas[0].substring(2,3);
        } else {
           if (cas[0].substring(3,4)) {
              minutes = cas[0].substring(2,3) + cas[0].substring(3,4);
            } else {
              minutes = cas[0].substring(2,3);
            }
        }  
      }
      document.getElementById(el).value = hours+':'+minutes;
    } else if (cas[1]) {
      if (cas[0].substring(1,2)) {
        hours = cas[0].substring(0,1) + cas[0].substring(1,2);
      } else {
        hours = cas[0].substring(0,1);
      }  
      if (hours != parseInt(hours)) {
        if (cas[0].substring(0,1)==0 && cas[0].substring(1,2)==parseInt(cas[0].substring(1,2))) {
          hours = cas[0].substring(0,1)+cas[0].substring(1,2);
        } else {
          hours = 1;
        }  
      } else {
        if (cas[0].substring(0,1)!=0) hours = parseInt(hours);
      }
      if (hours>23) hours = 23;
      if (cas[1].substring(0,1)) {
        if (cas[1].substring(0,1)!=parseInt(cas[1].substring(0,1))) {
          minutes = 0;
        } else {
          minutes = cas[1].substring(0,1);
        } 
        if (cas[1].substring(1,2)) {
          if (cas[1].substring(1,2)==parseInt(cas[1].substring(1,2))) minutes = minutes + cas[1].substring(1,2);
        }  
        else {
          if (cas[1].substring(0,1)>5) minutes = '0'+cas[1].substring(0,1);
          else minutes = cas[1].substring(0,1);
        }  
      } else {
        minutes = '';
      }  
      document.getElementById(el).value = hours+':'+minutes;
    } else if (cas[0]) {
      minutes = '';
      if (cas[0].substring(1,2)) {
        hours = cas[0].substring(0,1) + cas[0].substring(1,2);
        if (parseInt(hours)>23) {
          hours = cas[0].substring(0,1);
          if (cas[0].substring(2,3)) {
            minutes = cas[0].substring(1,2) + cas[0].substring(2,3);
          } else {
            if (cas[0].substring(1,2)>5) minutes = '0'+cas[0].substring(1,2);
            else minutes = cas[0].substring(1,2);
          }
          document.getElementById(el).value = hours+':'+minutes;
        }  
      } else {
        hours = cas[0].substring(0,1);
        if (cas2.substring(1,2)==':' || cas2.substring(2,1)==':') document.getElementById(el).value = hours+':';
        else document.getElementById(el).value = hours;
      }
      if (hours != parseInt(hours)) {
        if (cas[0].substring(0,1) == 0) hours = '0'+parseInt(cas[0].substring(1,2));
        if (minutes!='') document.getElementById(el).value = hours+':'+minutes;
        else {
          if (cas2.substring(1,2)==':' || cas2.substring(2,3)==':') document.getElementById(el).value = hours+':';
          
        }  
      }
      
    }
  }
}

PCB_Calendar.prototype.showMonth = function() {
  if (document.getElementById('month-selection')) {
    if (this._monthVisible == 0) {
      this._monthVisible = 1;
      document.getElementById('month-selection').style.display = "block";
    } else {
      this._monthVisible = 0;
      document.getElementById('month-selection').style.display = "none";
    }  
  }
}
PCB_Calendar.prototype.hideMonth = function() {
  if (document.getElementById('month-selection')) {
    this._monthVisible = 0;
    document.getElementById('month-selection').style.display = "none";
  }
}
PCB_Calendar.prototype.setNewMonth = function(newMonth) {
  this.setCalendar(new Date(this._cYear, newMonth, this._cDay));
  this.showMonth();
}

//kolik dni je ve kterem mesici          
PCB_Calendar.prototype.getDaysPerMonth = function(m){
  daysArray=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  days=daysArray[m];
  if (m==1){
    if((this._cYear% 4) == 0) {
      if(((this._cYear% 100) == 0) && (this._cYear% 400) != 0)
        days = 28;
      else
        days = 29;
    }
  }
  return days;
}

