// JavaScript Document
/*
* This function parses comma separated name=value
* argument pairs from the query string of the URL.
* It stores the name=value pairs in
* properties of an object and then returns that object
*
* Jim K - From Orielly JSB pp 244
*/

function getArgs() {
	var args = new Object();
	// Get Query String
	var query = location.search.substring(1);
	// Split query at the comma
	var pairs = query.split("&");

	// Begin loop through the querystring
	for(var i = 0; i < pairs.length; i++) {

		// Look for "name=value"
		var pos = pairs[i].indexOf('=');
		// if not found, skip to next
		if (pos == -1) continue;
		// Extract the name
		var argname = pairs[i].substring(0,pos);

		// Extract the value
		var value = pairs[i].substring(pos+1);
		// Store as a property
		args[argname] = unescape(value);
	}
	return args; // Return the Object
}

var client;
createClient();

function createClient() {
	try {
		client = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) { 
		alert("Sorry, your browser is not AJAX-enabled!"); 
	}
}

/************************************************************************************************/
/*******************************************Menu*************************************************/
/************************************************************************************************/
var nFirst;

function getMenu() {
	
	var myDocument = document;
	//seleciona o select que tem o menu
	var selectElement = myDocument.getElementById("sMenu");
	
	//altera o nome da primeira opção
	//Create a TextNode
	var textNode = document.createTextNode("Loading menu...");
	//Create an Element of type option	
		nFirst = myDocument.createElement("option");
		nFirst.setAttribute("value", "0");
		//coloca o text dentro a opção
		nFirst.appendChild(textNode);
		//coloca o elemento select que criei dentro da minha div
		selectElement.appendChild(nFirst);
		

	
	client.open("get","./menu.xml",true);
   	client.setRequestHeader('Content-Type',  "text/xml");
	client.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	client.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	client.setRequestHeader("Pragma", "no-cache");
	client.onreadystatechange = callBackMenu;
	client.send(null);
}

function callBackMenu() {
	
	if (client.readyState == 4) {
		//if (client.status == 200) {
			//texto recebido 
			var response = client.responseXML;
			
			//chama a função que queria a combo
			comboMenu(response);

			//cria um novo objecto
			createClient();
		/*} else {
			alert("There was a problem retrieving the response:\n" + client.statusText);
			//cria um novo objecto
			createClient();
		}*/
	}
}

function comboMenu(aMenus) {

	
	// Get the values back
	var args = getArgs();
	
	//verifica qt menus existem 
	var response = aMenus.documentElement;
	var nTotal = response.getElementsByTagName('name').length;

	var myDocument = document;
	//seleciona o select que tem o menu
	var selectElement = myDocument.getElementById("sMenu");
	

	//ciclo que percorre o xml e cria a combo
	for(i=0;i<nTotal;i++){

		var nome = response.getElementsByTagName('name')[i].firstChild.nodeValue;
		var link = response.getElementsByTagName('link')[i].firstChild.nodeValue;
		
		//se tiver link acrescenta o id do menu
		if(link != "" && link != 0){
			//calcula a posição do ? no link
			var pos = link.indexOf('?');
			//se nao existir coloca o id do link com o ?
			if(pos == "-1"){
				link = link + "?id=" + i;	
			//se existir coloca o id com o &
			}else{
				link = link + "&id=" + i;	
			}
		}
		
		//Create a TextNode
		var textNode = document.createTextNode(nome);
		//Create an Element of type option	
		var optinElement = myDocument.createElement("option");
			optinElement.setAttribute("id", i);
			optinElement.setAttribute("value", link);
			
			if(args.id == i){
				optinElement.setAttribute("selected", "true");
			}
			
			
			//coloca o text dentro a opção
			optinElement.appendChild(textNode);
			//coloca o elemento select que criei dentro da minha div
			selectElement.appendChild(optinElement);
	}

	//altera o nome da primeira opção
	//Create a TextNode
	var textNode = document.createTextNode("Selected Portfolio");
	//Create an Element of type option	
	var optinElement = myDocument.createElement("option");
		optinElement.setAttribute("id", "option");
		optinElement.setAttribute("value", "0");
		//optinElement.setAttribute("selected", "true");		
		//coloca o text dentro a opção
		optinElement.appendChild(textNode);
		//coloca o elemento select que criei dentro da minha div
		selectElement.replaceChild(optinElement,nFirst);
		
}

//////jump combo
function MM_jumpMenu(targ,selObj,restore){ //v3.0
	
	if(selObj.options[selObj.selectedIndex].value != 0){
		eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	}
	
	if (restore) selObj.selectedIndex=0;
}






