var fnMap = Function;
fnMap["processContact"] = processContact;
fnMap["processPoll"] = processPoll;

function aO(elemId){if(document.getElementById){return document.getElementById(elemId);}else{return false;}}
function sendRequest(uri,sendParams,fnRef){
	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}else if(window.ActiveXObject) { 
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e) {
				req = false;
			}
		}
	}
	if(req) {
		if (window.XMLHttpRequest){
			try {
					if (netscape.security.PrivilegeManager.enablePrivilege) {
						 netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
					}
			}catch(e) {}
		}
		req.onreadystatechange = fnMap[fnRef];
		req.open("POST", uri, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(sendParams);
	}else {}
}

var doc, req;
function parseXMLDoc(response){
	if (window.XMLHttpRequest) {
		if (window.ActiveXObject){
			doc = new ActiveXObject("Msxml2.DOMDocument.3.0");
			doc.async = "false";
			doc.loadXML(response);
		}else{
			doc = (new DOMParser()).parseFromString(response, "text/xml");
		}
	}else if (window.ActiveXObject){
		doc = new ActiveXObject("Microsoft.XMLDOM")
		doc.async = "false";
		doc.loadXML(response);
	}
}

function processContact(){
	if (req.readyState == 4) {
		if (req.status == 200) {
			aO("contactBtn").value = "Contact me";
			aO("contactBtn").removeAttribute("disabled");
			var response = req.responseText;
			parseXMLDoc(response);
			xmlObj=doc.documentElement;
			var msg = xmlObj.getElementsByTagName("message")[0].firstChild.nodeValue;
			var color = xmlObj.getElementsByTagName("color")[0].firstChild.nodeValue;
			msg = "<font color='" + color + "'>" + msg + "</font>";
			aO("message").style.display = "";
			aO("message").innerHTML = msg;   
		}
	}
}

function processPoll(){
	if (req.readyState == 4) {
		if (req.status == 200) {
			var response = req.responseText;
			parseXMLDoc(response);
			xmlObj=doc.documentElement;
			var msg = xmlObj.getElementsByTagName("message")[0].firstChild.nodeValue;
			var color = xmlObj.getElementsByTagName("color")[0].firstChild.nodeValue;
			msg = "<font color='" + color + "'>" + msg + "</font>";
			aO("pollQuestion").innerHTML = msg;
			aO("pollContent").innerHTML = '<a href="?forcePoll=true">Take this poll again</a>';
		}
	}
}

function sendMail(k){
	aO("message").style.display = "none";
	aO("contactBtn").value = "Processing ...";
	aO("contactBtn").setAttribute("disabled","disabled");
	var email = escape(k.email.value);
	var msg = escape(k.comments.value);
	var url = "/scripts/contact.php";
	var sendVal = "email=" + email + "&msg=" + msg;
	sendRequest(url, sendVal, "processContact");
}

function highlite(elem){
	elem.style.backgroundColor = "#FFFF66";
	elem.onblur = function(){
		this.style.backgroundColor = "#fff";
	}
}

_alreadyInProcess = false;
function submitVote(option){
	aO("pollQuestion").style.border = "1px solid #ccc";
	aO("pollQuestion").style.padding = "3px";
	aO("pollQuestion").style.backgroundColor = "#fff";
	aO("pollQuestion").innerHTML = "Processing ...";
	if(!_alreadyInProcess){
		_alreadyInProcess = true;
		var url = "/scripts/poll.php";
		var sendVal = "optionId=" + option;
		sendRequest(url, sendVal, "processPoll");
	}
}

function moreLess(which, elem){
	if("less" == which){
		elem.style.display = 'none';
		aO(elem.id + "moreLink").style.display = "";
	}else{
		elem.style.display = "";
		aO(elem.id + "moreLink").style.display = "none";
	}
}

function showSubMenu(linkObject){
	var subMenu = aO(linkObject.id + "Menu");
	if(subMenu){
		subMenu.style.display = "";
	}
}

var fadeInstances = [];
var Fade = function(elementId){
	var elem = aO(elementId);
	if(!elem)return;
	if(elem.underFade)return;
	this.element = elem;
	this.fadeInterval = 200;
	this.currentInstance = fadeInstances.length;
	fadeInstances[this.currentInstance] = this;
	var currentBackgroundColor = elem.style.backgroundColor ? elem.style.backgroundColor : "";
	this.element.underFade = true;
  this.doFade(this.currentInstance,Fade.Constants.startFadeAt,currentBackgroundColor);
};

Fade.Constants = {
	fadeInterval:150,
	fadeSteps: new Array("ff","ee","dd","cc","bb","aa","99","88","77","66","55","44","33","22","11","00"),
	startFadeAt:10			/* use all the fade colors in the above array, cap to a desired extent */
}

Fade.prototype.doFade = function(whichInstance,colorId,currentBackgroundColor){
	try{
		if(colorId >= 0) {
			fadeInstances[whichInstance].element.style.backgroundColor = "#ffff" + Fade.Constants.fadeSteps[colorId];
			if(colorId == 1){
				if(currentBackgroundColor){
					fadeInstances[whichInstance].element.style.backgroundColor = currentBackgroundColor;	
				}
				fadeInstances[whichInstance].element.underFade = false;
      }
			colorId--;
			var fadeFunction = "fadeInstances["+whichInstance+"].doFade("+whichInstance+","+colorId+",'"+currentBackgroundColor+"')"; 
			setTimeout(fadeFunction, Fade.Constants.fadeInterval);
		}
	}catch(e){}
};

function trim(str){return str.replace(/^\s*|\s*$/g,"");}

function expandNav(clickOrigin){
	if(!clickOrigin.currentNavState){
		clickOrigin.currentNavState = "close";
	}
	if(clickOrigin.currentNavState == "close"){
		aO("leftNavMenu").style.display = '';
		clickOrigin.innerHTML = "&lsaquo;";
		clickOrigin.title = "Collapse menu";
		clickOrigin.currentNavState = "open";
	}else{
		aO("leftNavMenu").style.display = 'none';
		clickOrigin.innerHTML = "&rsaquo;";
		clickOrigin.title = "Expand menu";
		clickOrigin.currentNavState = "close";
	}
}
