/***********************************************************
* Author Chris Hobden <chris@impera.co.uk>                *
* Copyright (c) Oshun Ltd, 2003-2005. All rights reserved.*
* See http://www.imperal.co.uk/licence/                   *
***********************************************************/

function isisDatePicker(fid, date_format){
	datepicker=this;
	this.date_format=date_format;
	this.days = new Array(7);
	this.days[0] = "Sun";
	this.days[1] = "Mon";
	this.days[2] = "Tue";
	this.days[3] = "Wed";
	this.days[4] = "Thu";
	this.days[5] = "Fri";
	this.days[6] = "Sat";

	this.months = new Array(12);
	this.months[0]  = "January";
	this.months[1]  = "February";
	this.months[2]  = "March";
	this.months[3]  = "April";
	this.months[4]  = "May";
	this.months[5]  = "June";
	this.months[6]  = "July";
	this.months[7]  = "August";
	this.months[8]  = "September";
	this.months[9]  = "October";
	this.months[10] = "November";
	this.months[11] = "December";

	this.field=document.getElementById(fid);
	this.setDate(datepicker.field.value, this.date_format);
	this.selectedDay=this.date.getDate();
	this.selectedMonth=this.date.getMonth()+1;
	this.selectedYear=this.date.getFullYear();

	tr=getTrueTopRight(this.field);
	this.holder=document.createElement('div');
	document.body.appendChild(this.holder);
	this.holder.style.position='absolute';
	this.holder.style.width=200+'px';
	this.holder.style.height=165+'px';
	this.holder.style.top=tr[1]+this.field.offsetHeight+"px";
	this.holder.style.left=tr[0]+"px";
	this.holder.style.zIndex=9;
	this.holder.className='isisDialogBox';
	maintable=document.createElement('table');
	isisAddEventHandler(maintable, 'keydown', function(e){datepicker.keyHandler(e);});
	maintable.appendChild(document.createElement("tbody"));
	this.holder.appendChild(maintable);
	maintable.width=this.holder.style.width;
	maintable.height=this.holder.style.height;
	tr=maintable.insertRow(-1);

	prev_button=document.createElement('input');
	prev_button.type='button';
	prev_button.value='<';
	td=tr.insertCell(-1);
	td.style.width=16;
	td.appendChild(prev_button);

	isisToggleSelect(parseInt(datepicker.holder.style.left),parseInt(datepicker.holder.style.top), parseInt(datepicker.holder.offsetWidth), parseInt(datepicker.holder.offsetHeight));

	isisAddEventHandler(prev_button, 'click', function(e){
		datepicker.addNMonths(-1);
		month_day_cell.replaceChild(document.createTextNode(datepicker.months[datepicker.date.getMonth()]+" "+datepicker.date.getFullYear()), month_day_cell.firstChild);
		calander_cell.replaceChild(datepicker.getTable(datepicker.date.getMonth()+1, datepicker.date.getFullYear()), calander_cell.firstChild);
	});

	month_day_cell=tr.insertCell(-1);
	month_day_cell.align='center';
	month_day_cell.appendChild(document.createTextNode(datepicker.months[datepicker.date.getMonth()]+" "+datepicker.date.getFullYear()));

	next_button=document.createElement('input');
	next_button.type='button';
	next_button.value='>';
	td=tr.insertCell(-1);
	td.style.width=16;
	td.appendChild(next_button);

	isisAddEventHandler(next_button, 'click', function(e){
		datepicker.addNMonths(1);
		month_day_cell.replaceChild(document.createTextNode(datepicker.months[datepicker.date.getMonth()]+" "+datepicker.date.getFullYear()), month_day_cell.firstChild);
		calander_cell.replaceChild(datepicker.getTable(datepicker.date.getMonth()+1, datepicker.date.getFullYear()), calander_cell.firstChild);
	});

	isisAddEventHandler(this.holder, 'keydown', function(e){datepicker.keyHandler(e);});
	tr=maintable.insertRow(-1);

	calander_cell=tr.insertCell(-1);
	calander_cell.colSpan=3;
	calander_cell.appendChild(datepicker.getTable(datepicker.date.getMonth()+1, datepicker.date.getFullYear()));
	next_button.focus();
}

isisDatePicker.prototype.keyHandler=function(e){
	switch(e.keyCode){
		case 27:
		isisToggleSelect(parseInt(this.holder.style.left),parseInt(this.holder.style.top), parseInt(this.holder.offsetWidth), parseInt(this.holder.offsetHeight));
		this.holder.parentNode.removeChild(this.holder);
		break;
		default:
		break;
	}
};
isisDatePicker.prototype.setCellClass=function(cell, status){
	switch(status){
		case "over":
		cell.className="isisButtonOver";
		break;
		case "pressed":
		cell.className="isisButtonPressed";
		break;
		default:
		cell.className="isisButton";
		break;
	}
};
isisDatePicker.prototype.daysInMonth=function(month, year){
	with (new Date(year, month, 1, 12)) {
		setDate(0);
		return getDate();
	}
};
isisDatePicker.prototype.createButton=function(day, month, year){
	var td=document.createElement("td");
	td.align='center';
	var tid='TT'+Math.floor(Math.random()*99999);
	td.id=tid;
	//		td.style.width='32px';
	//		td.style.height='20px';
	var btnDiv=document.createElement("div");
	btnDiv.style.className='isisButton';

	
	if(this.selectedDay==day&&this.selectedMonth==month&&this.selectedYear==year){
		btnDiv.innerHTML='<b>'+ i +'</b>';		
	}else{
		btnDiv.appendChild(document.createTextNode(i));
	}
	isisAddEventHandler(btnDiv, "click", function(e){
		//			alert(this.innerHTML);
		if(datepicker.date_format=='UK'){
			newdate=btnDiv.innerHTML.removeHTML()+'/'+month+'/'+year;
		}else{
			newdate=month+'/'+btnDiv.innerHTML.removeHTML()+'/'+year;
		}
		datepicker.field.value=newdate;
		isisToggleSelect(parseInt(datepicker.holder.style.left),parseInt(datepicker.holder.style.top), parseInt(datepicker.holder.offsetWidth), parseInt(datepicker.holder.offsetHeight));
		datepicker.holder.parentNode.removeChild(datepicker.holder);
	});
	this.setCellClass(btnDiv);
	isisAddEventHandler(btnDiv, "mouseover", function(e){datepicker.setCellClass(btnDiv, "over");});
	isisAddEventHandler(btnDiv, "mouseout", function(e){datepicker.setCellClass(btnDiv);});
	isisAddEventHandler(btnDiv, "mousedown", function(e){datepicker.setCellClass(btnDiv, "pressed");});
	isisAddEventHandler(btnDiv, "mouseup", function(e){datepicker.setCellClass(btnDiv);});
	isisAddEventHandler(btnDiv, 'keydown', function(e){datepicker.keyHandler(e);});
	isisAddEventHandler(td, 'keydown', function(e){datepicker.keyHandler(e);});
	td.appendChild(btnDiv);
	return td;
};
isisDatePicker.prototype.getTable=function(month, year){
	datepicker=this;
	total_days=this.daysInMonth(month, year);

	var table=document.createElement('table');
	table.style.width='100%';
	table.appendChild(document.createElement("tbody"));
	var tr=table.insertRow(-1);

	for(i=0;i<this.days.length;i++){
		var td=tr.insertCell(-1);
		td.appendChild(document.createTextNode(this.days[i]));
		this.setCellClass(td);
	}
	tdate = new Date(year, month-1, 1, 12);
	start=tdate.getDay();
	var tr=table.insertRow(-1);
	for(i=0;i<start;i++){
		tr.insertCell(-1);
	}
	for(i=1;i<=total_days;i++){
		if(start % 7 == 0){
			var tr=table.insertRow(-1);
		}
		start++;
		tr.appendChild(this.createButton(i,month, year));
	}
	return table;
};
isisDatePicker.prototype.addNMonths=function(months){
	this.date.setMonth(this.date.getMonth()+months);
};
isisDatePicker.prototype.setDate=function(sdate, date_format){
	if(sdate.length>0){
		if(date_format=='UK'){
			format='$3/$1/$5';
		}else{
			format='$1/$3/$5';
		}
		sdate=sdate.replace(/(\d+)(\/|-|.)(\d+)(\/|-|.)(\d+)/, format);
		this.date = new Date(sdate);
		if(isNaN(this.date))this.date = new Date();
	}else{
		this.date = new Date();
	}
};