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.visibility != "visible") {
			d.style.top = (div.y - 110) + "px";
		}
		d.style.visibility = "visible";
		d.style.opacity = 1;
		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
		}
//		alert("div.y: "+div.y+"\nhig: "+hig+"\ndocument.documentElement.scrollTop: "+document.documentElement.scrollTop+"\nd.offsetHeight: "+d.offsetHeight);
		if (document.documentElement.scrollTop + hig > div.y + 30 + d.offsetHeight) {
			d.style.top = div.y - 120 + "px";
		} else {
			d.style.top = hig + document.documentElement.scrollTop - d.offsetHeight - 150 + "px";
		}
	}
}

/*
hig: 839
document.documentElement.scrollTop: 0

div.y: 336
d.offsetHeight: 431

top=268
*/

function hideContent(force) {
	if(allowclose|force && showing) {
		document.getElementById(showing).style.visibility = "hidden";
		document.getElementById(showing).style.opacity = 0;
		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);
	}
}


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) {
	if (el) {
		value = "";
		allowed = "0123456789";
		for (i=0; i < el.value.length; i++) {
			chara = el.value.charAt(i);
			if (allowed.indexOf(chara,0) != -1) {
				value += chara;
			}
		}
		el.value = value;
		if (parseInt(el.value)) {
			weeks = parseInt(value);
			wks = '&printweeks='+weeks;
		} else {
			weeks = 5;
			wks = '';
		}
		calstartdate = parseInt(document.getElementById('calstartdate').value);
		ajax('cal.php?calstartdate='+calstartdate+wks+'&printweeks='+weeks,'calendar');
	} else {
		ajax('cal.php','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('cal.php?calstartdate="+prevstart+wks+"','calendar'); return false;\">previous "+weeks+" weeks</a>";
	document.getElementById('nextlink').innerHTML =
		"<a href='#' onclick=\"ajax('cal.php?calstartdate="+nextstart+wks+"','calendar'); return false;\">next "+weeks+" weeks</a>";
}

function setOrgInfo(idselect) {
	//make orginfo div visible
	//document.getElementById('orginfo').style.display = 'block';
	//look for selected option in list
	document.getElementById('orginfo').style.display = 'table-row-group'; 
	orgID = idselect.options[idselect.selectedIndex].value;
	if (orgID == 'new') {
		document.getElementById('unlock').style.display = 'none'; 
		//clear form, including invisible orgID field  and clear readonly settings
		document.getElementById('orgname').value = "";
		document.getElementById('orgbackground').value = "";
		document.getElementById('orglocation').value = "";
		document.getElementById('orgphone').value = "";
		document.getElementById('orgemail').value = "";
		document.getElementById('orgurl').value = "";
		unlockOrgInfo();
	} else if (orgID != 'none') {  //'none' is the value of the default "please select" option
		//as long as something's selected, call php page to query info on that option and fill in form
		ajax('/hrap/calendar/orgsearch.php?orgID='+orgID,'ajaxactiondiv');
		lockOrgInfo();
		document.getElementById('unlock').style.display = 'table-row-group'; 
	} else {
		document.getElementById('unlock').style.display = 'none'; 
		document.getElementById('orginfo').style.display = 'none'; 
	}
}

function unlockOrgInfo() {
		document.getElementById('orgname').removeAttribute('readOnly'); 
		document.getElementById('orgbackground').removeAttribute('readOnly'); 
		document.getElementById('orglocation').removeAttribute('readOnly'); 
		document.getElementById('orgphone').removeAttribute('readOnly'); 
		document.getElementById('orgemail').removeAttribute('readOnly'); 
		document.getElementById('orgurl').removeAttribute('readOnly'); 
}

function lockOrgInfo() {
		document.getElementById('orgname').setAttribute('readOnly','readonly');    
		document.getElementById('orgbackground').setAttribute('readOnly','readonly');     
		document.getElementById('orglocation').setAttribute('readOnly','readonly');     
		document.getElementById('orgphone').setAttribute('readOnly','readonly');     
		document.getElementById('orgemail').setAttribute('readOnly','readonly');     
		document.getElementById('orgurl').setAttribute('readOnly','readonly');    
}
   

