/*
	Copyright (c) 2005 Virtual Softworks, Inc. All rights reserved.
	http://www.vsworks.com 
	Do not duplicate or redistribute in any form.
*/

var dpArrDaysOfWeek = new Array ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var dpArrMonthes = new Array ('January','February','March','April','May','June','July','August','September','October','November','December');
var dpArrPickers = new Array ();

//detect Internet Explorer
var ie = (document.all) ? true : false;
var opera = (navigator.userAgent.indexOf("Opera") > 0) ? true : false;

Date.prototype.getRealYear = function (){
	//alert(this.getYear());
	if (opera)
		return this.getYear() + 1900;
	else if (ie && this.getYear() >= 2000) 
		return this.getYear();
	else 
		return this.getYear() + 1900;
}

function datepicker(ev, dateInput)
{
	//register onclick handler
	if (typeof document.onclick == 'undefined' || document.onclick == null)
		document.onclick = dpHideAllPickers;
		
	if (ie){
		ev = window.event;
		pickerImg = ev.srcElement;
	}
	else{
		pickerImg = ev.currentTarget;
	}
		
	ev.cancelBubble = true;
	//alert(pickerImg.tagName);

	//alert('datepicker');
	//alert(pickerImg.offsetTop);
	
	var picker = dpFindInstance(pickerImg, dateInput);
	if (picker == null) 
		dpCreateInstance(pickerImg, dateInput);
	else {
		if (picker.isVisible)
			picker.hide();
		else{
			picker.getDate();
			picker.update();
			picker.reposition();
			picker.show();
		}
	}
}

function dpHideAllPickers(){
	for (var i=0; i<dpArrPickers.length; i++)
		if (dpArrPickers[i].isVisible)
			dpArrPickers[i].hide();
}

function dpFindInstance(pickerImg, dateInput){
	for (var i=0; i<dpArrPickers.length; i++){
		if (	dpArrPickers[i].pickerImg === pickerImg
			&& 	dpArrPickers[i].dateInput === dateInput)
			return dpArrPickers[i];
	}
	return null;
}

function dpCreateInstance(pickerImg, dateInput){
	var picker = new DatePickerObj (pickerImg, dateInput);
	dpArrPickers.push(picker);
	picker.getDate();
	picker.update();
	picker.reposition();
}

function DatePickerObj(pickerImg, dateInput){
	this.instanceId = dpArrPickers.length;
	this.date = new Date();
	this.pickerImg = pickerImg;
	this.dateInput = dateInput;
	this.isVisible = true;

	//methods	
	this.reposition = dpReposition;
	this.pickerObj = dpCreateHTMLObj();

	this.update = dpUpdate;
	this.getDate = dpGetDate;
	this.pasteDate = dpPasteDate;
	this.hide = dpHide;
	this.show = dpShow;

	this.generateHTML = dpGenerateHTML;
	this.generateHeaderHTML = dpGenerateHeaderHTML;
	this.generateBodyHTML = dpGenerateBodyHTML;
}

function dpCreateHTMLObj(){
	var pickerObj = document.createElement("span");
	pickerObj.className  = "dp";
	document.body.appendChild(pickerObj);
	return pickerObj;
}

function dpReposition(){
	var o = this.pickerImg;
	var left = o.offsetLeft;
	while (o = o.offsetParent){
		left += o.offsetLeft;
	}
   
	o = this.pickerImg;
	var top = o.offsetTop;
	while (o = o.offsetParent){
		top += o.offsetTop;
	}
	
	//alert(document.body.clientHeight);
	//alert(window.pageYOffset);
	//alert(document.body.scrollTop);
	
    var win_height = document.body.clientHeight + document.body.scrollTop;
    if (
    		(top + this.pickerImg.offsetHeight + this.pickerObj.offsetHeight) > win_height 
    		|| opera
    	)
    {
    	//open on top
		top -= this.pickerObj.offsetHeight;
	}
	else{
		//open on bottom
		top += this.pickerImg.offsetHeight;
	}
	
    var win_width = document.body.clientWidth + document.body.scrollLeft;
    //alert(win_width - (left + this.pickerObj.offsetWidth));
    if ((left + this.pickerObj.offsetWidth) > win_width ){
    	left += win_width - (left + this.pickerObj.offsetWidth);
    }

	this.pickerObj.style.left  = left;
	this.pickerObj.style.top  = top;
	
}

function dpUpdate(){
	this.pickerObj.innerHTML = this.generateHTML();
}


function dpGenerateHTML(){
	return 	"<TABLE cellpadding=0 cellspacing=0 border=0><TR><TD>"
	 		+ this.generateHeaderHTML() 
	 		+ "<DIV class='dpHeaderSeparator'></DIV>"
	 		+ this.generateBodyHTML() 
	 		+ "</TD></TR></TABLE>";
}

function dpGenerateHeaderHTML(){
	return 	"<TABLE width='100%' class='dpHeader' cellpadding=0 cellspacing=0 border=0><TR><TD>"

			+ "<TABLE class='dpHeader' id='dpHeaderMonth' cellpadding=0 cellspacing=0 border=0><TR>"
			+ "<TD width='10' align=center onclick='dpPrevMonth(" + this.instanceId + ");' style='cursor: hand'>"
			+ "<IMG src='Images/Site/arrow_prev.png' alt='<' width=6 height=11>"
			+ "</TD><TD>"
			+ dpArrMonthes[this.date.getMonth()]
			+ "</TD><TD width='10' align=center onclick='dpNextMonth(" + this.instanceId + ");' style='cursor: hand'>"
			+ "<IMG src='Images/Site/arrow_next.png' alt='>' width=6 height=11>"
			+ "</TD></TR></TABLE>"

			+ "</TD><TD align=right>"

			+ "<TABLE class='dpHeader' id='dpHeaderYear' cellpadding=0 cellspacing=0 border=0><TR>"
			+ "<TD width='10' align=center onclick='dpPrevYear(" + this.instanceId + ");' style='cursor: hand'>"
			+ "<IMG src='Images/Site/arrow_prev.png' alt='<' width=6 height=11>"
			+ "</TD><TD>"
			+ this.date.getRealYear()
			+ "</TD><TD width='10' align=center onclick='dpNextYear(" + this.instanceId + ");' style='cursor: hand'>"
			+ "<IMG src='Images/Site/arrow_next.png' alt='>' width=6 height=11>"
			+ "</TD></TR></TABLE>"

			+ "</TD></TR></TABLE>";
}

function dpGenerateBodyHTML(){
	var str = "<TABLE border=0 cellpadding=2 cellspacing=0 class='dpBody'>" + dpGenerateDaysOfWeek() + "<TR>";
	
	var month_start = new Date (this.date.getYear(), this.date.getMonth(),1);
	var month_end = new Date (this.date.getYear(), this.date.getMonth()+1,1);
	month_end.setDate(month_end.getDate()-1);
	var days_in_month = month_end.getDate();
	var curr_date = new Date();
	var is_curr_month = (this.date.getYear()==curr_date.getYear() && this.date.getMonth()==curr_date.getMonth());
	
	//alert(month_start);
	//alert(month_end);
	//alert(this.date.getDate());
	
	//alert(month_start.getDay());
	var skip_days = month_start.getDay();
	if (!ie) skip_days += 2;
	if (skip_days >= 7) skip_days -= 7;
	//alert(skip_days);
	for (var i=0; i<skip_days; i++){
		str += "<TD class='dpDayCells'></TD>";
	}
	
	var day_of_week = skip_days;
	for (var i=1; i<=days_in_month; i++){
		if (day_of_week == 7){
			day_of_week = 1;
			str += '</TR><TR>';
		}
		else 
			day_of_week++;

		var cell_id = (day_of_week == 1 || day_of_week == 7) ? 'dpWeekEnd' : '';
		if (is_curr_month && i == curr_date.getDate()) cell_id = 'dpToday';
		if (i == this.date.getDate()) cell_id = 'dpSelectedDay';

		str += 	"<TD class='dpDayCells' id='" + cell_id + "'>"
				+ "<A href='' id='" + cell_id + "' class='dpNumbersLink'"
				+ " onclick='dpDateSelect(" +this.instanceId+ "," +i+ "); return false;'>"
				+ i
				+ "</A></TD>";
	}
	str += '</TR></TABLE>';
	return str;
}

function dpGenerateDaysOfWeek(){
	var str = '<TR>'
	for (var i=0; i<dpArrDaysOfWeek.length; i++){
		str += "<TD class='dpDayCells' id='dpDaysOfWeekCells'>" + dpArrDaysOfWeek[i] + '</TD>';
	}
	str += '</TR>';
	return str;
}

function dpNextMonth(instanceId){
	if (opera) window.event.cancelBubble = true;
	with (dpGetPickerById(instanceId)){
		date.setMonth(date.getMonth()+1);
		update();
	}
}

function dpPrevMonth(instanceId){
	if (opera) window.event.cancelBubble = true;
	with (dpGetPickerById(instanceId)){
		date.setMonth(date.getMonth()-1);
		update();
	}
}

function dpNextYear(instanceId){
	if (opera) window.event.cancelBubble = true;
	with (dpGetPickerById(instanceId)){
		date.setYear(date.getRealYear()+1);
		update();
	}
}

function dpPrevYear(instanceId){
	if (opera) window.event.cancelBubble = true;
	with (dpGetPickerById(instanceId)){
		date.setYear(date.getRealYear()-1);
		update();
	}
}

function dpDateSelect(instanceId, d){
	with (dpGetPickerById(instanceId)){
		date.setDate(d);
		pasteDate();
		hide();
	}
}

function dpGetPickerById(instanceId){
	return dpArrPickers[instanceId];
}


function dpPasteDate(){
	this.dateInput.value = dpFormatDate(this.date);
}


function dpFormatDate(d){
	return d.getMonth()+1 + '/' + d.getDate() + '/' + (d.getRealYear());
}

function dpHide(){
	this.pasteDate();
	this.pickerObj.style.visibility = 'hidden';
	this.isVisible = false;
}

function dpShow(){
	dpHideAllPickers();
	this.pickerObj.style.visibility = '';
	this.isVisible = true;
}

function dpGetDate(){
	var date_str = this.dateInput.value;
	var date;
	if (!date_str.length)
		date = new Date();
	else{
		var d = Date.parse(this.dateInput.value);
		if (isNaN(d)){
			//invalid date
			alert('Invalid date.\nPlease enter date in format mm/dd/yyyy.');
			date = new Date();
		}
		else
			date = new Date (d);
	}
	this.date = date;
}

