
function DatePicker(name) {
     this.linkcursor = document.all ? "hand" : "pointer";
     this.name = name;
     document.write('<span id="' + name + 
        '" style="z-index:3; position:absolute;visibility:hidden;"' +
        'style="background-color:#f0f0f0" class="DatePicker" onmouseover="oDatePicker.mouseover(event)" onmouseout="oDatePicker.mouseout(event)"></span>');
     document.write('<span id="' + name + 
        '_iframe" style="z-index:2; position:absolute;visibility:hidden;"' +
        'style="background-color:#ffffff"><iframe src="javascript:false;" frameborder=0 style="width:160;height:135"></iframe></span>');
}

DatePicker.prototype.showUpper = function(obj, givendt, width, height) {
  this.show(obj, givendt, width, height, true);
}

DatePicker.prototype.show = function(obj, givendt, width, height, isUpper) {
	datepicker_hideitnow = false;
	obj = eval(obj);
	this.client = obj;
	if(!isUpper || isUpper == "" || isUpper == null){
		this.isUpper = false;
	} else {
	  this.isUpper = true;
	}
	if(!givendt || givendt == "" || givendt == null){
		givendt = obj.value;
	}
	
	var mydt = isValidDate(givendt, this.getFormat());
	if(mydt && mydt.getTime() > 0){
	  this.dt = mydt;
	}else{
	  this.dt = new Date();
	}
    this.orgdt = new Date();
    this.orgdt.setTime(this.dt.getTime());
    // if not rendered yet, do so
    if ( !this.oSpan ) this.render();
    // set coordinates
    this.oFrame = document.getElementById(this.name + "_iframe");
    var xOffset = 0;
    var navAppName = navigator.appName.toLowerCase();
    var w = 200; // for != Netscape
    if(navAppName.indexOf("netscape") >= 0) {
      w = 180
    }
    // will it go outside of the window?
    if(document.body.offsetWidth < findPosXInContainer(obj)+w) {
      xOffset = findPosXInContainer(obj) + w - document.body.offsetWidth;
    }
    this.oFrame.style.left = findPosXInContainer(obj)-xOffset;
    this.oFrame.style.top= findPosYInContainer(obj) + 20;
    
    var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") == -1 && agt.indexOf("msie 5") == -1) // not Opera & IE 5.x
	{
    	this.oFrame.style.visibility = "visible";
	}
	
    if(!height){
    	height = 135;
    }
    if(!width){
    	width = 160;
    }
    this.oFrame.style.width = width;
    this.oFrame.style.height = height;    
    this.oSpan.style.left = findPosXInContainer(obj)-xOffset;
    this.oSpan.style.top= findPosYInContainer(obj) + 20;
    this.fill();
    this.oSpan.style.visibility = "visible";
    this.oMonth.focus();
	
}
DatePicker.prototype.callback = function(dt) {
	 var format = this.getFormat();
	 var d = dt.getDate();
	 if(d < 10){
	 	d = "0" + d;
	 }
	 format = format.replace("dd", d);
	 var m = dt.getMonth()+1;
	 if(m < 10){
	 	m = "0" + m;
	 }
	 format = format.replace("MM", m);
	 format = format.replace("yyyy", dt.getFullYear());
	 var shortyear = dt.getFullYear()-1900;
	 if(shortyear >= 100)
	 	shortyear -= 100;
	 if(shortyear < 10){
	 	shortyear = "0" + shortyear;
	 }
	 format = format.replace("yy", shortyear);

   if (this.isUpper) {
		 format = format.replace("HH", "23");
		 format = format.replace("hh", "11");
		 format = format.replace("mm", "59");
		 format = format.replace("ss", "59");
		 format = format.replace("a", "PM");
   } else {
		 format = format.replace("HH", "00");
		 format = format.replace("hh", "00");
		 format = format.replace("mm", "00");
		 format = format.replace("ss", "00");
		 format = format.replace("a", "AM");
   }

     this.client.value = format;
	 //signal the element
     this.client.select();
}
DatePicker.prototype.mouseout = function(evt) {
	if(needToHide(this.oSpan.id,evt)){
		this.hide(500);
	}
}
DatePicker.prototype.mouseover = function(evt) {
	datepicker_hideitnow = false;
}

DatePicker.prototype.hide = function(timeout) {
     if (this.oSpan){
     	if(!timeout)
     		timeout = 0;
		datepicker_hideitnow = true;
		window.setTimeout('if(datepicker_hideitnow){document.getElementById("'+this.oSpan.id+'").style.visibility="hidden";document.getElementById("'+this.oFrame.id+'").style.visibility="hidden"}', timeout);
     }	
}
DatePicker.prototype.render = function() {
     var oT1, oTR1, oTD1, oTH1;
     var oT2, oTR2, oTD2;
     this.oSpan = document.getElementById(this.name);
     this.oSpan.appendChild(oT1 = document.createElement("table"));
     oT1.border = 1;
     oT1.cellPadding=0;
     oT1.cellSpacing=0;
     oTR1 = oT1.insertRow(oT1.rows.length);
     oTD1 = oTR1.insertCell(oTR1.cells.length);
     oTD1.colSpan = 7;
     oTD1.className = 'DatePickerHdr';
     oT2 = document.createElement("table");
     oTD1.appendChild(oT2);
     oT2.border = 0;
     oT2.cellPadding=0;
     oT2.cellSpacing=0;
     oTR2 = oT2.insertRow(oT2.rows.length);

     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.onclick = function() { this.oDatePicker.onPrev(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "&lt;&lt;";
     oTD2.style.cursor=this.linkcursor;
     oTD2.className = 'DatePickerHdrBtn';
     oTD2 = oTR2.insertCell(oTR2.cells.length);

     this.oMonth = document.createElement("select");
     oTD2.appendChild(this.oMonth);
     oTD2.className = 'DatePickerHdrBtn';
     this.oMonth.oDatePicker = this;
     this.oMonth.onchange = this.oMonth.onkeyup = 
       function() { this.oDatePicker.onMonth(); }
     for ( var i = 0; i < 12; i++ ) {
		var oOption = document.createElement("option");
		oOption.text = convertNls(this.texts.months[i]);
		oOption.value = i;
        if (navigator.appName == "Netscape")
          this.oMonth.add(oOption, null);
        else
          this.oMonth.add(oOption);
     }
     
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.onclick = function() { this.oDatePicker.onNext(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "&gt;&gt;";
     oTD2.style.cursor=this.linkcursor;
     oTD2.className = 'DatePickerHdrBtn';
     oTR1 = oT1.insertRow(oT1.rows.length);
     for ( i = 0; i < 7; i++ ) {
        oTH1 = document.createElement("th");
        oTR1.appendChild(oTH1);
        oTH1.innerHTML = convertNls(this.texts.days[i]);
        oTH1.className = 'DatePicker';
     }
     this.aCells = new Array;
     for ( var j = 0; j < 6; j++ ) {
        this.aCells[j] = new Array;
        oTR1 = oT1.insertRow(oT1.rows.length);
        for ( i = 0; i < 7; i++ ) {
           this.aCells[j][i] = oTR1.insertCell(oTR1.cells.length);
           this.aCells[j][i].oDatePicker = this;
	   	   var oSpan = document.createElement("span");
		   this.aCells[j][i].appendChild(oSpan);
		   this.aCells[j][i].oSpan = oSpan;
           this.aCells[j][i].oSpan.onclick = 
              function() { this.oDatePicker.onDay(this); }
		   this.aCells[j][i].oSpan.oDatePicker = this;
		   this.aCells[j][i].oSpan.style.width = 20;
        }
     }


     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.innerHTML = "&nbsp;&nbsp;";
     oTD2.className = 'DatePickerHdrBtn';


     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.onclick = function() { this.oDatePicker.onPrevYear(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "&lt;&lt;";
     oTD2.style.cursor=this.linkcursor;
     oTD2.className = 'DatePickerHdrBtn';

     this.oYear = document.createElement("select");
     oTD2.appendChild(this.oYear);
     oTD2.className = 'DatePickerHdrBtn';
     this.oYear.oDatePicker = this;
     this.oYear.onchange = this.oYear.onkeyup = 
       function() { this.oDatePicker.onYear(); }
       syear = new Date().getFullYear();
     for ( var i = 0; i < 99; i++ ) {
	var oOption = document.createElement("option");
	oOption.text = syear-i;
	oOption.value = syear-i;
        if (navigator.appName == "Netscape")
          this.oYear.add(oOption, null);
        else
          this.oYear.add(oOption);
     }     
     
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.onclick = function() { this.oDatePicker.onNextYear(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "&gt;&gt;";
     oTD2.style.cursor=this.linkcursor;
     oTD2.className = 'DatePickerHdrBtn';
     
}
DatePicker.prototype.fill = function() {
     // first clear all
     this.clear();
     // place the dates in the calendar
     var nRow = 0;
     var d = new Date(this.dt.getTime());
     var m = d.getMonth();
     for ( d.setDate(1); d.getMonth() == m; d.setTime(d.getTime() + 86400000) ) {
        var nCol = d.getDay();
        var myclass = d.getTime() == this.orgdt.getTime() ? "DatePickerBtnSelect" : "DatePickerBtn";
        this.aCells[nRow][nCol].oSpan.className = myclass;
		this.aCells[nRow][nCol].oSpan.innerHTML = d.getDate();
		this.aCells[nRow][nCol].style.cursor = this.linkcursor;
        if ( nCol == 6 ) nRow++;
     }
     // set the month combo
     this.oMonth.value = m;
     // set the year text
     this.oYear.value = this.dt.getFullYear();
}
DatePicker.prototype.clear = function() {
     for ( var j = 0; j < 6; j++ )
        for ( var i = 0; i < 7; i++ ) {
           this.aCells[j][i].oSpan.innerHTML = "&nbsp;"
           this.aCells[j][i].oSpan.className = 'DatePickerBtn';
           this.aCells[j][i].className = 'DatePickerBtn';
   	   	   this.aCells[j][i].style.cursor = 'auto';
        }
}
DatePicker.prototype.onPrev = function() {
	 this.dt.setDate(1);
	 if ( this.dt.getMonth() == 0 ) {
        this.dt.setFullYear(this.dt.getFullYear() - 1);
        this.dt.setMonth(11);
     } else {
        this.dt.setMonth(this.dt.getMonth() - 1);
     }
     this.fill();
}
DatePicker.prototype.onNext = function() {
	 this.dt.setDate(1);
     if ( this.dt.getMonth() == 11 ) {
        this.dt.setFullYear(this.dt.getFullYear() + 1);
        this.dt.setMonth(0);
     } else {
        this.dt.setMonth(this.dt.getMonth() + 1);
     }
     this.fill();
}

DatePicker.prototype.onNextYear = function() {
     this.dt.setDate(1);
     this.dt.setFullYear(this.dt.getFullYear() + 1);
     this.fill();
}

DatePicker.prototype.onNext5Years = function() {
     this.dt.setDate(1);
     this.dt.setFullYear(this.dt.getFullYear() + 5);
     this.fill();
}

DatePicker.prototype.onPrev5Years = function() {
     this.dt.setDate(1);
     this.dt.setFullYear(this.dt.getFullYear() - 5);
     this.fill();
}

DatePicker.prototype.onPrevYear = function() {
     this.dt.setDate(1);
        this.dt.setFullYear(this.dt.getFullYear() - 1);
     this.fill();
}

DatePicker.prototype.onYear = function() {
     this.dt.setDate(1);
     this.dt.setFullYear(this.oYear.value*1+1);
     this.fill();
}

DatePicker.prototype.onMonth = function() {
	 this.dt.setDate(1);
     this.dt.setMonth(this.oMonth.value);
     this.fill();
}
DatePicker.prototype.onDay = function(oCell) {
     var d = parseInt(oCell.innerHTML);
     if ( d > 0 )
     {
        this.dt.setDate(d);
        this.hide();
        this.callback(this.dt);
     }
}

DatePicker.prototype.texts = {
     months: [
        "January", "February", "March", 
        "April", "May", "June", 
        "July", "August", "September", 
        "October", "November", "December"
     ],
     days: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
     years: []
};
DatePicker.prototype.getFormat = function() {
	return "dd.MM.yyyy";
}

var oDatePicker = new DatePicker('theDatePicker');

DatePicker.prototype.getFormat = function() {
	format = "dd.MM.yyyy";
	return format;
}

DatePicker.prototype.texts = {
     months: [
        "January"
      , "February"
      , "March"
      , "April"
      , "May"
      , "June"
      , "July"
      , "August"
      , "September"
      , "October"
      , "November"
      , "December"
     ],
     days: [
     	  "Su"
     	, "Mo"
     	, "Tu"
     	, "We"
     	, "Th"
     	, "Fr"
     	, "Sa"
     ]
};
