var IE = false;
if (window.ActiveXObject) IE = true;

function ajaxExecFunc(url, func) {
	var httpRequest;
	try {
		httpRequest = new XMLHttpRequest();
	} catch(e) {
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				alert("Could not load page!");
			}
		}
	}
	httpRequest.onreadystatechange = function () {
		if (httpRequest.readyState!=4) return;
		if (httpRequest.status!=200) return;
		func(httpRequest);
	}
	httpRequest.open("GET", url, true);
	if (typeof(httpRequest.overrideMimeType)=="function")
		httpRequest.overrideMimeType('text/xml');
	else
		httpRequest.setRequestHeader("Content-Type", "text/xml");
	httpRequest.send(null);
}

function ajaxLoadIntoId(id, url) {
	var httpRequest;
	try {
		httpRequest = new XMLHttpRequest();
	} catch(e) {
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				alert("Could not load page!");
			}
		}
	}
	httpRequest.onreadystatechange = function () {
		if (httpRequest.readyState!=4) return;
		if (httpRequest.status!=200) return;
		var obj = document.getElementById(id);
		obj.innerHTML = httpRequest.responseText;
	}
	httpRequest.open("GET", url, true);
	httpRequest.setRequestHeader("Content-Type", "text/xml");
	httpRequest.send(null);
}

function loadScript(url) {
	var newScript = document.createElement('script');
	newScript.type="text/javascript";
	newScript.src=url;
	document.getElementsByTagName('head')[0].appendChild(newScript);
}

function loadXML(url) {
	var xmlDoc = null;
	if (IE) xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	else xmlDoc = document.implementation.createDocument("","",null);
	xmlDoc.async = false;
	xmlDoc.load(url);
	return xmlDoc;
}

function base64encode(input) {
	var lookup = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".split('');
	var output = "";
	var in1, in2, in3, out1, out2, out3, out4;
	var i=0;
	while (i < input.length) {
		in1 = input.charCodeAt(i++);
		in2 = input.charCodeAt(i++);
		in3 = input.charCodeAt(i++);
		
		out1 = in1 >> 2;
		out2 = ((in1 & 3) << 4) | (in2 >> 4);
		out3 = ((in2 & 15) << 2) | (in3 >> 6);
		out4 = in3 & 63;
		
		if (isNaN(in2)) { out3 = out4 = 64; }
		else if (isNaN(in3)) { out4 = 64; }

		output += lookup[out1] + lookup[out2] + lookup[out3] + lookup[out4];
	}
	return output;
}

function getObjectPosition(object) {
	var left = 0;
	var top = 0;
	
	while (object.offsetParent) {
		left += object.offsetLeft;
		top += object.offsetTop;
		object = object.offsetParent;
	}
	
	left += object.offsetLeft;
	top += object.offsetTop;
	
	return {x:left, y:top};
}

function doCalendar() {
	location.href = "calendar.php";
}

var emailNotificationBox = null;
var emailNotificationBoxReady = false;

function initEmailNotification() {
	emailNotificationBox = document.createElement("div");
	emailNotificationBox.style.position = "absolute";
	emailNotificationBox.style.backgroundColor = "white";
	emailNotificationBox.style.border = "3px solid black";
	emailNotificationBox.style.display = "none";
	emailNotificationBox.style.padding = "10px";
	document.body.appendChild(emailNotificationBox);
	$(emailNotificationBox).load("constantcontact.html?rand="+base64encode(""+Math.random()), function(){emailNotificationBoxReady = true;});
}

function doEmail() {
	var callerPosition = getObjectPosition(document.getElementById("emailNotificationHeaderCell"));
	//alert(callerPosition.y);
	emailNotificationBox.style.top = "" + (callerPosition.y+25) + "px";
	emailNotificationBox.style.left = "" + (callerPosition.x) + "px";
	$(emailNotificationBox).slideToggle();
}
