var NS = (navigator.appName == "Netscape") ? 1 : 0;
var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var ver = parseInt(navigator.appVersion);
var IE = (navigator.appName == "Microsoft Internet Explorer") ? 1 : 0;
var cid = 'cal';
var ct = 0;

function isLeapYear (x_year) {
	if (((x_year % 4)==0) && ((x_year % 100)!=0) || ((x_year % 400)==0)) {
		return (true);
		}
	else {
		return (false);
		}
	}

function getDOW(date) {
	dow = date.getDay();
	return (dow == 0 ? 7 : dow);
	}

function getMonthDays(date) {
	var month = date.getMonth()+1;
	if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)	return 31;
	else if (month==4||month==6||month==9||month==11) return 30;
	else if (month==2) {
		if (isLeapYear(date.getYear()))	return 29;
		else return 28;
		}
	}

function setDateValues(formname, control, d, m, y) {
	m = (m+1);
	document.forms[formname].elements[control].value = (d < 10 ? '0' + d : d ) + '/' + (m < 10 ? '0' + m : m ) + '/' + y;
	if (control == 'start_date')
	update_end_date(document.forms[formname].elements[control].value);
	hideCalendar();
	}

function hideCalendar() {
	if (NS) {
		var obj = document.getElementById(cid);
		obj.style.visibility = 'hidden';
		obj.style.display = 'none';
		}
	else {
		document.all[cid].style.visibility = 'hidden';
		document.all[cid].style.display = 'none';
		}
	clvis = false;
	document.body.oncontextmenu = function(){ return true; };
	}


function buildCalendar(y, m, img, formname, control) {
	var html = '';
	var cDate = new Date();
	var template_root = '/sites/knexus/template/uk/';
	var y = parseInt(y, 10);
	var m = parseInt(m, 10);

	var fDate = new Date (y, (m-1), 1);
	var fDow = getDOW(fDate);
	var fDays = getMonthDays(fDate);

	html += '<div class="tooltip" style="width:100%;"><div class="calendar_box" style="width:100%;"><div class="calendar_top" style="width:100%;"><div></div></div><div class="nav">';
	html += '<a href="javascript:showCalendar('+ (y-1) + ',' + m + ", '" + img + "', '" + formname + "', '" + control + '\');"><img src="'+template_root+'/gfx/icons/prev_end.gif" width="20" height="9" alt="" class="float_left" style="float:left;"></a>';
	html += '<a href="javascript:showCalendar('+((m-1)>=0?y:y-1)+', '+((m-1)>=0?m-1:12)+", '" + img + "',  '" + formname + "', '" + control + '\');"><img src="' + template_root+'/gfx/icons/prev_small.gif" width="20" height="9" alt="" style="float:left;" class="float_left"></a>';
	html += '<a href="javascript:showCalendar('+(y+1)+', '+m+", '" + img + "',  '" + formname + "', '" + control + '\');"><img src="' + template_root+'/gfx/icons/next_end.gif" width="20" height="9" alt="" class="float_right"></a>';
	html += '<a href="javascript:showCalendar('+((m+1)!=13?y:y+1)+', '+((m+1)!=13?(m+1):1)+", '" + img + "',  '" + formname + "', '" + control + '\');"><img src="' + template_root+'/gfx/icons/next_small.gif" width="20" height="9" alt="" class="float_right"></a>';
	html += '<div style="cursor:' + (NS ? 'pointer' : 'hand') + ';' + (fDate.getMonth()==cDate.getMonth()&&fDate.getYear()==cDate.getYear()? '' : '') + '" OnClick="hideCalendar();" title=" Close ">' + months[fDate.getMonth()] + ' ' + fDate.getFullYear() + '</div>';
//	html += " " + months[fDate.getMonth()] + ' ' + fDate.getFullYear() + "";

	html += '</div><div class="indent"><table><tr><th>Mon</th><th>Thu</th><th>Wed</th><th>Thi</th><th>Fri</th><th>Sat</th><th>Sun</th></tr>';
	html += '<tr>';
	var fDate = new Date (((m-2)>=0?y:(y-1)), ((m-2)>=0?(m-2):12), 1);
	var fDays = getMonthDays(fDate);
	var pDow = getDOW(fDate);

	d = fDays+2-fDow;

	for (c = 1; c < fDow; c++) {
		html += '<td class="gday">'+d+'</td>';
		d++;
		}

	var fDate = new Date (y, (m-1), 1);
	var fDow = getDOW(fDate);
	var fDays = getMonthDays(fDate);
	var skipDOW = new Array();
	for (d = 1; d <= fDays;) {
		cday = ( (fDate.getYear() == cDate.getYear()) &&	(fDate.getMonth() == cDate.getMonth()) &&	(cDate.getDate() == d ) ) ? 1 : 0 ;
		fDow = getDOW(fDate);
		cl = (fDow == 7 ? 'day7' : (fDow == 6 ? 'day6' : 'day' ));
		html += '<td' + (cday ? ' class="cday"':' class="day"')+(!cday ? ' onMouseOver="this.style.backgroundColor=\'#cccccc\';" onMouseOut="this.style.backgroundColor=\'#ffffff\'"':'') + '>';
		if (!skipDOW[fDow]){
			html += '<a' + (cday ? ' class="cday nobg"':' class="'+cl+' nobg"') +' href="javascript:' + "setDateValues('" + formname + "', '" + control + "', " + d + ", " + fDate.getMonth() + ", " + fDate.getFullYear() + ');">';
			html += d;
			html += '</a>';
		}else
			html += d;
		html += '</td>' + (fDow == 7 ? '</tr><tr bgcolor="#EEEEEE" align="center">' : '' );
		d++;
		fDate.setDate(d);
		}

	var fDate = new Date (((m+1)!=13?(y+1):y), ((m+1)!=13?(m+1):1), 1);
	var fDays = getMonthDays(fDate);
	var d = 1;

	for (c = fDow; c < 7; c++) {
		var fDow = getDOW(fDate);
		html += '<td class="gday">'+d+'</td>';
		d++;
		fDate.setDate(d);
		}
	html += '</tr></table></div><div class="calendar_bottom"><div></div></div></div></div>';
	return html;
	}

function showCalendar(y, m, img, formname, control) {

	var html = buildCalendar(y, m, img, formname, control);

	var obj = (NS ? document.getElementById(cid) : document.all[cid]);
	if (NS) {
		var newNode = document.createElement('span');
		obj.replaceChild(newNode, obj.firstChild);
		newNode.innerHTML = html;
		}
	else {
		obj.innerHTML = html;
	}

    
	TopCoord  = ObjTop(document.images[img], 2);
	LeftCoord = ObjLeft(document.images[img], 0);
	
	

	obj.style.top  = (1*TopCoord - (IE ? 19 : 20))+'px';
	obj.style.left = (1*LeftCoord - (IE ? 110 : 10))+'px';
//	obj.style.top  = (img=='cl_start_date' || img=='cl_end_date')?(TopCoord - 40) +'px' : TopCoord -15 + 'px' ;//TopCoord+'px';
//	obj.style.left = (img=='cl_start_date'  || img=='cl_end_date')?-45 +'px':(LeftCoord  -290) +'px'; //(img=='cl_img2'?LeftCoord-210+14:LeftCoord+0)+'px';

	obj.style.visibility = 'visible';
	obj.style.display = 'inline';
	document.body.oncontextmenu=function(){ return false; };
}
	
function test_dates( start, stop ){
 start_date = new Date(start.split('/')[2], start.split('/')[1], start.split('/')[0]);
 stop_date = new Date(stop.split('/')[2], stop.split('/')[1], stop.split('/')[0]);
  if (start_date > stop_date) {
 	alert("The End date is older then the Start date. Enter correct dates, please!");
 	return false;
 }
 return true;
}	

function update_end_date(start) {
	y = start.split('/')[2] * 1;
	m = start.split('/')[1] * 1;
	d = start.split('/')[0] * 1;
	
	m1 = ((1+m)!=13?(1+m):1); 
	y1 = (1+m)!=13?y:1+y;
	if (typeof document.forms.db_form.elements['end_date'].value != 'undefined')
		document.forms.db_form.elements['end_date'].value = (d < 10 ? '0' + d : d ) + '/' + (m1 < 10 ? '0' + m1 : m1 ) + '/' + y1
}
