//setCal();
function date_picker(){
	this.ie = (navigator.appName)=="Netscape"?false:true;
	this.month_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	this.month_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	this.nos = "00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59".split(",");
//	this.month_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	this.week_days = new Array("Sn","Mn","Tu","Wd","Th","Fr","St");
	if(this.ie){
		this.ar = new Array("&uarr;","&darr;");
	} else {
		this.ar = new Array("&uArr;","&dArr;");
	}
	this.border = 0;
	this.cellspacing = 1;
	this.cellpadding = 0;
	this.CSSmain="calmain";
	this.CSSarrow="calarrow";
	this.CSSinput="calinput";
	this.CSSdays="caldays1";
	this.CSSdaynames="caldays2";
	this.CSSday="calday";

	this.leap_year = leap_year;
	this.get_days = get_days;
	this.get_month_name = function(month) { return this.month_names[month]; }
	this.draw_cal = draw_cal;
	this.get_date = get_date;
	this.set_year = set_year;
	this.set_month = set_month;
	this.hide_it = hide_it;
	this.close_it = close_it;
	this._getY = _getY;
	this._getX = _getX;
	var now = new Date();
	this.year = now.getFullYear();
	this.month = now.getMonth();
	this.date = now.getDate();
	now = null;
	this.first_day = 0;
//	this.set_date('2000-02-21');
}
function _getY( oElement ){ var iReturnValue = 0; while( oElement != null ) { iReturnValue += oElement.offsetTop; oElement = oElement.offsetParent; } return iReturnValue; }
function _getX( oElement ){ var iReturnValue = 0; while( oElement != null ) { iReturnValue += oElement.offsetLeft; oElement = oElement.offsetParent; } return iReturnValue; }

function leap_year(){ return (this.year % 4 == 0) ? true : false; }
function get_days (){
		this.month_days[1] = (this.leap_year(this.year)) ? 29 : 28;
		return this.month_days[this.month];
}

function get_date(v1, v2, v3, v4){
	var now = new Date();
	this.year = now.getFullYear();
	this.month = now.getMonth();
	this.date = now.getDate();
	this.v4 = v4;
	now = null;
	var v = '';
	if(v3!='' && v3.indexOf('-')>0){
		v = v3.split("-");
		v[2] = v[2].substr(0,2);
		if(v[0]>999){ this.year = v[0]; }
		if(v[1]>0 && v[1]<13){this.month = v[1]-1; }
		if(v[2]>0 && v[2]<32){ this.date = v[2]; }
	}
	if(v4==1 && v3.indexOf(':')>0){
		v = v3.split(":");
		v[0] = v[0].substr(v[0].length - 2,2);
		if(v[0]>=0 && v[0]<=23){this.hour = v[0]; }
		if(v[1]>=0 && v[1]<=59){this.minute = v[1]; }
		if(v[2]>=0 && v[2]<=59){this.second = v[2]; }
	} else {
		this.hour = '00';
		this.minute = '00';
		this.second = '00';
	}
	this.input = v1;
	this.input.style.position = 'relative';
	this.input.readOnly = true;
	if(!document.getElementById(v2)){
		this.div = document.createElement("div");
		this.div.setAttribute("id", v2)
		this.div.style.display = 'none';
		this.div.style.position = 'absolute';
		document.body.appendChild(this.div);
	} else {
		this.div = document.getElementById(v2);
	}
//	document.f1.t1.value = dump_props(this.input,"d");
	this.div.style.top=this._getY(this.input)+this.input.offsetHeight + "px";
	this.div.style.left=this._getX(this.input) + "px";
	this.div.innerHTML = this.draw_cal();
	this.div.style.display = "block";
}

function set_year(v1){ this.year = v1; this.div.innerHTML = this.draw_cal(); }

function set_month(v1){
	if(v1==-1){ this.year--; this.month = 11; } else if(v1==12){ this.year++; this.month = 0; } else { this.month = v1; }
	this.div.innerHTML = this.draw_cal();
}

function hide_it(v1){
	this.input.value = v1;
	if(this.v4==1){ this.input.value += ' ' + this.hour + ':' + this.minute + ':' + this.second; }
	this.div.innerHTML = ""; this.div.style.display = "none";
}
function close_it(v1){ this.div.innerHTML = ""; this.div.style.display = "none"; }

function draw_cal() {
	var firstDayInstance = new Date(this.year, this.month, 1);
	this.first_day = firstDayInstance.getDay();
	firstDayInstance = null;
	var text = "";
	text += '<form><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0"><TR><TD class="'+ this.CSSmain +'">';
	text += '<TABLE BORDER="' + this.border + '" CELLSPACING="' + this.cellspacing + '" CELLPADDING="' + this.cellpadding + '">';
	text += '<TH COLSPAN="7" nowrap class="'+ this.CSSmain +'"><select onChange="pick_date.set_month(this.options[this.selectedIndex].value)" class="'+ this.CSSinput +'" size="1" name="cmonth" title="Select Month">'
	for(var i in this.month_names){
		text += '<option onClick="pick_date.set_month(this.value)" value="'+ i +'"'+ ((this.month==i)?' selected':'')  +'>' + this.month_names[i] + '</option>';
	}
	text += '</select>'
	text += '<span onClick="pick_date.set_month('+(this.month+1)+')" class="'+ this.CSSarrow +'" title="Last Month">'+this.ar[0]+'</span>';
	text += '<span onClick="pick_date.set_month('+(this.month-1)+')" class="'+ this.CSSarrow +'" title="Next Month">'+this.ar[1]+'</span>';
	text += '<select onChange="pick_date.set_year(this.options[this.selectedIndex].value)" class="'+ this.CSSinput +'" size="1" name="cyear" title="Select Year">'
	for(i=1950; i<2035; i++){
		text += '<option value="'+ i +'"'+ ((this.year==i)?' selected':'')  +' >' + i + '</option>';
	}
	//onClick="pick_date.set_year(this.value)"
	text += '</select>';
	text += '<span onClick="pick_date.set_year('+(this.year+1)+')" class="'+ this.CSSarrow +'" title="Last Year">'+this.ar[0]+'</span>';
	text += '<span onClick="pick_date.set_year('+(this.year-1)+')" class="'+ this.CSSarrow +'" title="Next Year">'+this.ar[1]+'</span>';
	text += '&nbsp;<span onClick="pick_date.close_it()" class="'+ this.CSSarrow +'" title="Close Calendar">X</span>';
	text += '</TH></TR><TR>';
	for (var dayNum = 0; dayNum < 7; ++dayNum) { text += '<td class="'+ this.CSSdaynames +'">' + this.week_days[dayNum] + '</td>'; }
	text += '</TR><TR title="Click any Day to Choose">';
	var dayOfMonth = 1;
	if(!nosofcells){ var nosofcells = null; }
	if((this.first_day+this.get_days())%7==0){
		nosofcells = this.first_day+this.get_days();
	} else {
		nosofcells = this.first_day+this.get_days()+(7-(this.first_day+this.get_days())%7);
	}
	for(i =0; i< nosofcells; i++){
		if(i<this.first_day || dayOfMonth>this.get_days()){
			text += '<TD class="'+ this.CSSdays +'">&nbsp;</TD>';
		} else {
			var j = this.year+'-'+((this.month<9)?'0':'')+eval(parseInt(this.month)+1)+'-'+((dayOfMonth<10)?'0'+dayOfMonth:dayOfMonth);
			if(this.date==dayOfMonth){
				text += '<TD onClick="pick_date.hide_it(\''+j+'\')" class="'+ this.CSSday +'">'+(i-this.first_day+1)+'</TD>';
			} else {
				text += '<TD onClick="pick_date.hide_it(\''+j+'\')" class="'+ this.CSSdays +'">'+(i-this.first_day+1)+'</TD>';
			}
			dayOfMonth++;
		}
		if(i%7==6){ text += '</TR><TR title="Click any Day to Choose">'; }
	}
	text += '</TR></TABLE>';
	if(this.v4==1){
		text += '<select onChange="pick_date.hour = this.options[this.selectedIndex].value" size"1" name="hour" class="'+ this.CSSinput +'" title="Select Hour" style="width:38px; border:0px; margin:0px; padding:0px;">';
		for(i=0; i<24; i++){ text += '<option value="'+ this.nos[i] +'"'+ ((this.hour==i)?' selected':'')  +' >' + this.nos[i] + '</option>'; }
		text += '</select>';
		text += '<span onClick="pick_date.set_hour('+(this.hour+1)+')" class="'+ this.CSSarrow +'" title="Last Hour">'+this.ar[0]+'</span>';
		text += '<span onClick="pick_date.set_hour('+(this.hour-1)+')" class="'+ this.CSSarrow +'" title="Next Hour">'+this.ar[1]+'</span>';
		text += '<span>:</spand>';
		text += '<select onChange="pick_date.minute =this.options[this.selectedIndex].value" size"1" name="minute" class="'+ this.CSSinput +'" title="Select Minute" style="width:38px; border:0px; margin:0px; padding:0px;">';
		for(i=0; i<60; i++){ text += '<option value="'+ this.nos[i] +'"'+ ((this.minute==i)?' selected':'')  +' >' + this.nos[i] + '</option>'; }
		text += '</select>';
		text += '<span onClick="pick_date.set_minute('+(this.minute+1)+')" class="'+ this.CSSarrow +'" title="Last Minute">'+this.ar[0]+'</span>';
		text += '<span onClick="pick_date.set_minute('+(this.minute-1)+')" class="'+ this.CSSarrow +'" title="Next Minute">'+this.ar[1]+'</span>';
		text += '<span>:</spand>';
		text += '<select onChange="pick_date.second = this.options[this.selectedIndex].value" size"1" name="second" class="'+ this.CSSinput +'" title="Select Second" style="width:38px; border:0px; margin:0px; padding:0px;">';
		for(i=0; i<60; i++){ text += '<option value="'+ this.nos[i] +'"'+ ((this.second==i)?' selected':'')  +' >' + this.nos[i] + '</option>'; }
		text += '</select>';
		text += '<span onClick="pick_date.set_second('+(this.second+1)+')" class="'+ this.CSSarrow +'" title="Last Second">'+this.ar[0]+'</span>';
		text += '<span onClick="pick_date.set_second('+(this.second-1)+')" class="'+ this.CSSarrow +'" title="Next Second">'+this.ar[1]+'</span>';
	}
	text += '</form>';
	text += '</TD></TR></table>';
	return text; 
}

var pick_date = new date_picker();

