////////////////////////////////////////////////////////////////////////////////////////
//**************************************************************************************
//**************************************************************************************
// Date Picker
// (c)2004 Be-in, All Right Reserved
//**************************************************************************************
//**************************************************************************************
//Constructor
//**************************************************************************************
DatePicker = function(_name,_calendarDIV,_target){
    this.isIE = navigator.appName.indexOf("Microsoft") != -1;
    this.name = _name;
    this.calendarDIV = _calendarDIV;
    this.target = _target;
    this.row = 7;
    this.colWidth = "30";
    this.colHeight = "30";
    this.yearColWidth = "40";
    this.yearColHeight = "15";
    this.colBackColor="#242424";
    this.colDayBackColor="#242424";
    this.fontSize="10px";
    this.fontName="Verdana";
    this.DayNames=new Array("Po","Út","St","Čt","Pá","So", "Ne");
    this.MonthNames=new Array("Leden","Únor","Březen","Duben","Květen","Červen", "Červenec","Srpen","Září","Říjen","Listopad","Prosinec");
    this.calendarBackColor="#242424";
    this.tempDate = new Date();
    this.oldTargetDate="";
    this.Day; 
    this.Month; 
    this.Year;
}
//**************************************************************************************
//Show Calendar 
//**************************************************************************************
DatePicker.prototype.Show = function( event ){
    //save actual date
    this.oldTargetDate = document.getElementById(this.target).value;
    var tempDate = new Date();
    this.Day =  tempDate.getDate();
    this.Month = tempDate.getMonth();
    if(this.isIE){
        this.Year = tempDate.getYear();
    }else{
        this.Year = tempDate.getYear() + 1900;
    }
    //show date
    document.getElementById(this.name + "_comboMonth").value =  this.Month;
    this.ShowMonth();
    this.ShowYear();
    //start of set position
    //
    /*
    document.getElementById(this.calendarDIV).style.left=event.clientX+20;
    document.getElementById(this.calendarDIV).style.top=event.clientY+20;
    */
    document.getElementById(this.calendarDIV).style.left=mouseX(event)-200+'px';
    document.getElementById(this.calendarDIV).style.top=mouseY(event)-5+'px';
    //end of set position
    document.getElementById(this.calendarDIV).style.display="block";
}
//**************************************************************************************
//Hide Calendar 
//**************************************************************************************
DatePicker.prototype.Hide = function(){
    document.getElementById(this.calendarDIV).style.display="none";
}
//**************************************************************************************
//Create Calendar Table
//**************************************************************************************
DatePicker.prototype.Create = function(){
       //hide
       document.getElementById(this.calendarDIV).style.display="none";
       var strCalendar="<table class=\"dpick-table\" cellpadding=\"0px\" cellspacing=\"0px\">";
       strCalendar+="<tr><td style=\"background-color: #242424;\" colspan=\"7\"><table class=\"dpick-table\" cellpadding=\"2px\" cellspacing=\"2px\"><tr><td align=\"left\">";
       strCalendar+="<select id=\"" +this.name + "_comboMonth\" onchange=\"" +this.name + ".comboMonthOnChanged();\">";
       for(var m=0;m<this.MonthNames.length;m++){
           strCalendar+="<option value=\"" +m + "\">" +this.MonthNames[m] + "</option>";
       }
       strCalendar+="</select></td>";
       strCalendar+="<td style=\"background-color: #242424;\" width=\"100%\" valign=\"middle\" align=\"right\">";
       strCalendar+="<input type=\"button\" id=\"" +this.name + "_btnPrevYear\" style=\"width:22px;height:18px\" value=\"<\"  onclick=\"" +this.name + ".btnPrevYearOnClick();\">";
       strCalendar+="<span style=\"width:2px\"> </span>";
       strCalendar+="<input type=\"text\" id=\"" +this.name + "_textYear\"  style=\"height:14px\" size=\"4\"  readonly value>";
       strCalendar+="<span style=\"width:2px\"> </span>";
       strCalendar+="<input type=\"button\" id=\"" +this.name + "_btnNextYear\" style=\"width:22px;height:18px\" value=\">\" onclick=\"" +this.name + ".btnNextYearOnClick();\">";
       strCalendar+="</td></tr></table></td></tr>";
       for(var y=0;y<this.row;y++){
           strCalendar+="<tr>";
            for(var x=0;x<7;x++){
                if(y==0){
                     strCalendar+="<th align=\"center\" style=\"width:" +this.colWidth + "px; height:" +this.colHeight + "px; background-color:" + this.colDayBackColor +";\" id=\""+this.name+"_c" + y + "_" + x + "\">" +this.DayNames[x] + "</td>"; 
                }else{
                      //strCalendar+="<td onclick=\"" + this.name + ".DayOnClick(this);\" align=\"center\" style=\"width:" +this.colWidth + "px; height:" +this.colHeight + "px; background-color:" + this.colBackColor +"\" id=\""+this.name+"_c" + y + "_" + x + "\"></td>";
                      strCalendar+="<td onclick=\"" + this.name + ".DayOnClick(this);\" align=\"center\" style=\"width:" +this.colWidth + "px; height:" +this.colHeight + "px;\" id=\""+this.name+"_c" + y + "_" + x + "\"></td>"; 
                 }
            }
            strCalendar+="</tr>";
        }
        //strCalendar+="<tr><td colspan=\"7\"><table cellpadding=\"2px\" cellspacing=\"2px\" style=\"font-size:" + this.fontSize + "; font-family:" +this.fontName + "; background-color:" +  this.calendarBackColor +"  \" width=\"100%\"><tr><td align=\"left\">";
        //strCalendar+="<a id=\"" +this.name + "_btnCancel\" href=\"javascript:" +this.name + ".btnCancelOnClick();\"><span class=\"back\">Storno</span></a>";
        //strCalendar+="</td><td width=\"100%\" align=\"right\">";
       // strCalendar+="<a id=\"" +this.name + "_btnOK\" href=\"javascript:" +this.name + ".btnOKOnClick();\"><span class=\"bnext\">OK</span></a>";
        //strCalendar+="</td></tr></table></td></tr>";
        strCalendar+="</table>";
        document.getElementById(this.calendarDIV).innerHTML=strCalendar;
}
//**************************************************************************************
//Get Count of Days in Month
//parameters:   _month - actual month
//                   _year - actual year
//**************************************************************************************
DatePicker.prototype.GetCountOfDays = function(_month, _year){
    var MonthDays =  new Array(31,28,31,30,31,30,31,31,30,31,30,31); 
	if (((0 == (_year%4)) && ( (0 != (_year%100)) || (0 == (_year%400)))) && _month == 1) {
		return 29;
	} else {
		return MonthDays[_month];
	} 
}
//**************************************************************************************
//Show Days in Month
//parameters:   _month - actual month
//                   _year - actual year
//**************************************************************************************
DatePicker.prototype.ShowMonth = function (){

    var tempDate = new Date( this.Year,this.Month,1);
    var xDayPosition = tempDate.getDay() - 1;
    if(xDayPosition == -1){
        xDayPosition = 6;
    }
    var yDayPosition = 1;
    this.Clean();
    for(var d=1;d<(this.GetCountOfDays(this.Month,this.Year)+1);d++){
        document.getElementById(this.name + "_c" + yDayPosition + "_" + xDayPosition).innerHTML = d;
        document.getElementById(this.name + "_c" + yDayPosition + "_" + xDayPosition).style.cursor="pointer";
        xDayPosition++;
        if(xDayPosition>6){
            xDayPosition = 0;
            yDayPosition++;
        }
    }
}
//**************************************************************************************
//Show  Year
//parameters:    _year - actual year
//**************************************************************************************
DatePicker.prototype.ShowYear = function (){
        document.getElementById(this.name + "_textYear").value = this.Year;
}
//**************************************************************************************
//Clean
//**************************************************************************************
DatePicker.prototype.Clean = function (){
       for(var y=1;y<this.row;y++){
            for(var x=0;x<7;x++){
                    document.getElementById(this.name + "_c" + y + "_" + x).innerHTML = "";
                    document.getElementById(this.name + "_c" + y + "_" + x).style.cursor="auto";
            }
        }
}
//**************************************************************************************
//User use month combo
//**************************************************************************************
DatePicker.prototype.comboMonthOnChanged = function (){
       /*
       this.sMonth = document.getElementById(this.name + "_comboMonth").value;
       this.Month = ((this.sMonth.length == 1) ? "0"+this.sMonth : this.sMonth);
       */
       this.Month = document.getElementById(this.name + "_comboMonth").value;
       this.ShowMonth();
       this.SetTarget();
}
//**************************************************************************************
//Day on click handler
//**************************************************************************************
DatePicker.prototype.DayOnClick = function (_obj){
    if(_obj.innerHTML != ""){
        /*
        this.sDay = _obj.innerHTML;
        this.Day = ((this.sDay.length == 1) ? "0"+this.sDay : this.sDay);
        */
        this.Day = _obj.innerHTML;
        this.SetTarget();
        this.Hide();
    }
}
//**************************************************************************************
//Prev year on click handler
//**************************************************************************************
DatePicker.prototype.btnPrevYearOnClick = function (){
    if(this.Year>1902){
        this.Year --;
        this.ShowYear();
        this.ShowMonth();
        this.SetTarget();
    }
}
//**************************************************************************************
//Next year on click handler
//**************************************************************************************
DatePicker.prototype.btnNextYearOnClick = function (){
    if(this.Year<2050){
        this.Year ++;
        this.ShowYear();
        this.ShowMonth();
        this.SetTarget();
    }
}
//**************************************************************************************
//OK on click handler
//**************************************************************************************
DatePicker.prototype.btnOKOnClick = function (){
        this.Hide();
}
//**************************************************************************************
//Cancel on click handler
//**************************************************************************************
DatePicker.prototype.btnCancelOnClick = function (){
        document.getElementById(this.target).value = this.oldTargetDate; 
        this.Hide();
}
//**************************************************************************************
//Set Target Date
//**************************************************************************************
DatePicker.prototype.SetTarget = function (){
        var tmonth = parseInt(this.Month) + 1;
        document.getElementById(this.target).value = this.Day + "." + tmonth + "." + this.Year;
}
//**************************************************************************************
//**************************************************************************************
////////////////////////////////////////////////////////////////////////////////////////

function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}

