// JavaScript Document

function changeBG(obj){
	obj.style.backgroundColor="#FFCC80";
}
function nullBG(obj){
	obj.style.backgroundColor="";
}
function mainmenuOver(obj){
	obj.style.color="#FFCC66";
}
function mainmenuOut(obj){
	obj.style.color="#FFFFFF";
}
function menuOver(obj){
	obj.style.color="#333333";
}
function menuOut(obj){
	obj.style.color="#666666";
}
function subMenuClick(menu, subMenu){
	
	document.getElementById("menuDetail").innerHTML="";
	subMenu=parseInt(subMenu)+1;
	file=menu+"_"+subMenu;
	//alert(file);
	url="Data/menu"+file+".xml";
	url=url+"?sid="+Math.random();
	//action="menu"+menu;
	
	ajaxSend('get', url, 0, 'getDetail', 1, 'menuHeader', 'Loading...');
}

function highlightMenu(menu){
	
	//alert(menu);
	for(i=1;i<=7;i++){
		document.getElementById("menu"+i).style.backgroundColor="#666666";
		if(i==7) document.getElementById("menu"+i).style.backgroundColor="";
	}
	document.getElementById("menu"+menu).style.backgroundColor="#4282A8";

}

function menuClick(menu, submenu){
	
	document.getElementById("header").className="header"+menu;
	
	urlSubmenu="Data/subMenu.xml";
	urlSubmenu=urlSubmenu+"?sid="+Math.random();
	action="menu"+menu;
	
	ajaxSend('get', urlSubmenu, 0, action, 1, 'subMenu', 'Loading...');
	
	document.getElementById("menuHeader").innerHTML="";
	document.getElementById("menuDetail").innerHTML="";
	
	if(submenu) subMenuClick(menu, submenu);
	else subMenuClick(menu, 0);
	
	highlightMenu(menu);
}

function scDetail(obj, dpObj){

	x=obj.responseXML.documentElement.getElementsByTagName("data");
	
	for (i=0;i<x.length;i++){

	  	xx=x[i].getElementsByTagName("title");
		try{
			txtHead= xx[0].firstChild.nodeValue;
		}
		catch (er){

		}
		
	  	xx=x[i].getElementsByTagName("detail");
		try{
			txtDetail= xx[0].firstChild.nodeValue;
		}
		catch (er){

		}
		xx=x[i].getElementsByTagName("moredetail");
		try{
			txtDetail+= xx[0].firstChild.nodeValue;
		}
		catch (er){

		}
		
	}
	
	document.getElementById(dpObj).innerHTML=txtHead;
	document.getElementById("menuDetail").innerHTML=txtDetail;

}

function scSubMenu(obj, dpObj, action){
	
	txt="<table border='0' width='100%' cellpadding='5'>";
	x=obj.responseXML.documentElement.getElementsByTagName(action);
	mainMenu=action.substr(4,1);
	//alert(mainMenu);
	txt+="<tr><td style='text-decoration:underline;'>Menu</td></tr>";
	for (i=0;i<x.length;i++){
	  	txt=txt + "<tr>";
	  	xx=x[i].getElementsByTagName("title");
		var subMenu=mainMenu;
		//subMenu=subMenu;
		//alert(subMenu);
		try{
			txt=txt + "<td onmouseover='changeBG(this)' onmouseout='nullBG(this)'>";
			txt=txt + "<a onclick='subMenuClick("+mainMenu+","+i+")' onmouseover='menuOver(this)' onmouseout='menuOut(this)'>" + xx[0].firstChild.nodeValue + "</a></td>";
		}
		catch (er){
			txt=txt + "<td> </td>";
		}
	}
	
	txt=txt + "</tr></table>";
	//txt=txt + "<br /><table align='center'><tr><td align='center' valign='top' height='180'>";
	//txt=txt + "<img src='images/undp_logo.jpg' /><img src='images/po_logo.gif' />";
	//txt=txt + "<br /><img src='images/Logo_ILGI.jpg' /></td></tr></table>";
	document.getElementById(dpObj).innerHTML=txt;
}

function responseAction(action, obj, dpObj){
	
	if(action.substring(0,4)=="menu") scSubMenu(obj, dpObj, action) ;
	else if(action=="getDetail") scDetail(obj, dpObj) ;
	
}

function ajaxSend(method, url, parameters, responseHandler, asynchronous, dpObj, waitText)
{
	if (typeof method != 'string'){
		alert("Invalid request method.");
		return false;
	}
	method = method.toUpperCase();
	if (method != 'POST' && method != 'GET'){
		alert("Invalid request method.");
		return false;
	}
	if (typeof parameters != 'string') parameters = '';
	if (typeof asynchronous == 'undefined') asynchronous = true;
	
	// build the request
	var ajaxRequest = null;
	if (window.XMLHttpRequest) ajaxRequest = new XMLHttpRequest();
	else if (window.ActiveXObject) ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

	// bind the callback.
	function ajaxBindCallback(){
		
		if (ajaxRequest.readyState == 4){
			if (ajaxRequest.status == 200 || ajaxRequest.status == 0) // is either valid XML or no
			{
				//alert(responseHandler);
				if (responseHandler && dpObj) responseAction(responseHandler, ajaxRequest, dpObj);
				else if (responseHandler && !dpObj) responseAction(responseHandler, ajaxRequest);
			}
			else{
				if (ajaxRequest.status == 404){
					alert("The requested service could not be found.");
					if(dpObj) document.getElementById(dpObj).innerHTML="Data Not Found !"
				}
				else{
					alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
				}
			}
		}
		else{
			if(waitText) document.getElementById(dpObj).innerHTML=waitText;
			else document.getElementById(dpObj).innerHTML="Loading";
		}
	}		

	// send the request
	if (ajaxRequest){
		
		if (asynchronous) ajaxRequest.onreadystatechange = ajaxBindCallback;
		if (method == "GET"){
			url = url + '?' + parameters
			parameters = null;
		}
		ajaxRequest.open(method, url, asynchronous);
		if (method == 'POST'){
			ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		}
		ajaxRequest.send(parameters);

		if (!asynchronous) ajaxBindCallback();
	}
	else alert("Unable to build XMLHttpRequest");
}