var mouse = {x:0, y:0};
var div = {x:0, y:0};
if(window.Event) {document.onmousemove = getMousePos;}

function getMousePos(e) {
	if (!e) e = window.event || window.Event;
	if(e.pageY) {
		mouse.x = e.pageX;
		mouse.y = e.pageY;
	}
}

function loc() {
	if (document.layers) {getMousePos;}
	else if (document.all) {1
		mouse.x = window.event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - (document.documentElement.clientLeft || 0);
		mouse.y = window.event.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - (document.documentElement.clientTop || 0);
	}
}

var showing;
var allowclose = 0;

function AssignPosition(d) {
	if (!d) {
		d = document.getElementById(showing);
	}
	if (allowclose) {
		if(document.body.offsetWidth) {
			var wid = document.body.offsetWidth;
		} else if(window.innerWidth) {
			var wid = window.innerWidth;
		} else {
			var wid = 1024;
		}
		if (d.style.display != "block") {
			d.style.top = (div.y + 10) + "px";
		}
		d.style.display = "block";
		div.x = div.x - (wid-956)/2 - 154;  //cursor less left margin (half screen width less the page width) less left menu div (154)
		if (div.x > wid - 540 - (wid-956)/2 + document.documentElement.scrollLeft) {
			d.style.left = wid - 540 - (wid-956)/2 + document.documentElement.scrollLeft + "px";
		} else {
			d.style.left = div.x + 10 + "px";
		}
		var hig = window.innerHeight;
		if (!window.innerHeight) {
			var hig = (document.body.offsetHeight || document.documentElement.offsetHeight);
				//prevents IE from expanding window rather than moving d up -- body is IE6, docEl is IE7
		}
		if (div.y < hig + document.documentElement.scrollTop - 40 - d.offsetHeight) {
			d.style.top = div.y - 10 + "px";
		} else {
			d.style.top = hig + document.documentElement.scrollTop - d.offsetHeight - 40 + "px";
		}
	}
}

function hideContent(force) {
	if(allowclose|force && showing) {
		document.getElementById(showing).style.display = "none";
		allowclose = 0;
		document.onclick = "";
	}
}

function ShowContent(d) {
	if(showing) {
		allowclose=1;
		hideContent();
	}
	showing=d;
	if(d.length > 0) {
		div.x = mouse.x;
		div.y = mouse.y;
		var dd = document.getElementById(d);
		allowclose = 1;
		AssignPosition(dd);
		setTimeout("document.onclick = hideContent",50);
	}
}

function closePopUp(delay) {
	if (!delay) {
		delay = 0;
	}
	if (document.getElementById('popupdiv')) {
		document.getElementById('popupdiv').style.display = 'none';
		document.getElementById('popupdiv').innerHTML = '<p class="head2" style="text-align:center">Loading<br /><img src="/shared/loading.gif" alt="..."/></p>'; 
		setTimeout("document.getElementById('greyout').style.display = 'none';",delay);
	}
}

function popUp(el) {
	document.getElementById('greyout').style.display = 'block';
	document.getElementById('popupdiv').style.display = 'block';
	ajax(el,'popupdiv');
}


var closecal=1;
var showmini=0;

function showminical() {
	closecal=0;
	document.getElementById('minicaldiv').style.display = 'block';
	ajax('minical.php','minicaldiv');
}
	
function hideminical(forcemini) {
	if (closecal==1 || forcemini==1) {
		setTimeout("document.getElementById('minicaldiv').style.display = 'none'",100);
	} else if (document.getElementById('minicaldiv').style.display == 'block') {
		document.getElementById('date').focus();
	}
}


function reloadcal(el) {
	value = "";
	allowed = "0123456789";
	for (i=0; i < el.value.length; i++) {
		char = el.value.charAt(i);
		if (allowed.indexOf(char,0) != -1) {
			value += char;
		}
	}
	el.value = value;
	if (parseInt(el.value)) {
		weeks = parseInt(value);
		wks = '&printweeks='+weeks;
	} else {
		weeks = 5;
		wks = '';
	}
	calstartdate = parseInt(document.getElementById('calstartdate').value);
	ajax('/calendar/cal.php?calstartdate='+calstartdate+wks,'calendar');
}

function assignpn() {
	calstartdate = parseInt(document.getElementById('calstartdate').value);
	wks = '&printweeks='+document.getElementById('weeks').value;
	prevstart = calstartdate-(weeks*604800);
	nextstart = calstartdate+(weeks*604800);
	document.getElementById('prevlink').innerHTML =
		"<a href='#' onclick=\"ajax('/calendar/cal.php?calstartdate="+prevstart+wks+"','calendar'); return false;\">previous "+weeks+" weeks</a>";
	document.getElementById('nextlink').innerHTML =
		"<a href='#' onclick=\"ajax('/calendar/cal.php?calstartdate="+nextstart+wks+"','calendar'); return false;\">next "+weeks+" weeks</a>";
}


