var xmlDoc, level1, level2, level1index, level2index;

var i=0;
for(x in menu) {
	document.menuform.select1.options[i] = new Option(x, x, false, false);
	i++;
}

var cookie1 = getCookie("l1");
var cookie2 = getCookie("l2");
	
if(cookie1 != null && cookie2 != null) {
	document.menuform.select1.options[cookie1].selected = true;
	change1(cookie1);

	document.menuform.select2.options[cookie2].selected = true;
	change2(cookie2);
}


function change1(selectedIndex) {
	clearMenu("select2");
	showMenu("select2", "heading2");
	
	//hideMenu("select3", "heading3");
	clearResults();
	
	level1index = selectedIndex;
	level1 = document.menuform.select1.options[selectedIndex].value;
	if(!level1) return;

	var preloadstr = "";	
	var i=0;
	for(y in menu[level1]) {
		document.menuform.select2.options[i] = new Option(y, y, false, false);
		document.menuform.select2.options[i].selected = false;
		
		preloadstr += menu[level1][y] + "--";
		i++;
	}

	var url = "/cgi-bin/fetch-hotkeys.pl/"+escape(preloadstr); 
	var fakeimg = new Image();
	fakeimg.src = url;
}

function change2(selectedIndex) {
	clearResults();
	showMenu("select3", "heading3");
	
	level2index = selectedIndex;
	level2 = document.menuform.select2.options[selectedIndex].value;
	if(!level2) return;
	
	document.getElementById("select3").appendChild(document.createTextNode("Loading, please wait..."));
	
	importXML(menu[level1][level2]);
	
	setCookie("l1", level1index);
	setCookie("l2", level2index);
}

function change2_xmlloaded() {
	if(xmlDoc == null) { alert("No XML"); return }
	
	clearResults();
	var listings = xmlDoc.getElementsByTagName('LISTING');
	
	var doc = document.getElementById('select3');
	
	for(var i=0; i < listings.length; i++) {
		var title = stripEntities(listings[i].getElementsByTagName('TITLE')[0].firstChild.nodeValue);
		var description = stripEntities(listings[i].getElementsByTagName('DESCRIPTION')[0].firstChild.nodeValue);
		var link_url = listings[i].getElementsByTagName('LINK_URL')[0].firstChild.nodeValue;
		var display_url = listings[i].getElementsByTagName('DISPLAY_URL')[0].firstChild.nodeValue;
		
		var item = document.createElement('P');
		var link = document.createElement('A');
		link.href = link_url;
		link.appendChild(document.createTextNode(title));
		item.appendChild(link);
		item.appendChild(document.createElement('BR'));
		item.appendChild(document.createTextNode(description));
		item.appendChild(document.createElement('BR'));
		var url = document.createElement('SPAN');
		url.appendChild(document.createTextNode(display_url));
		url.className = "displayURL";
		item.appendChild(url);
		doc.appendChild(item);
	}
}	

function hideMenu(menu, heading) {
	//document.getElementById(menu).style.visibility = "hidden";
	//document.getElementById(heading).style.visibility = "hidden";
}

function showMenu(menu, heading) {
	document.getElementById(menu).style.visibility = "visible";
	document.getElementById(heading).style.visibility = "visible";
}

function clearMenu(menu) {
	for(i=document.menuform.elements[menu].options.length-1; i>=0; i--) {
		document.menuform.elements[menu].options[i] = null;
	}
}

function clearResults() {
	var doc = document.getElementById('select3');
	for(var i=doc.childNodes.length-1; i>=0; i--) {
		doc.removeChild(doc.childNodes[i]);
	}
}

function importXML(search_string) {

	var xmlurl = "/cgi-bin/fetch-hotkeys.pl/"+escape(search_string)+".xml"; 

	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = change2_xmlloaded;
		xmlDoc.load(xmlurl);
	}
	else if (window.ActiveXObject) {
		var req = new ActiveXObject("Microsoft.XMLHTTP");
		req.open("GET", xmlurl, false);
		req.send();
		xmlDoc = req.responseXML;
		change2_xmlloaded();
 	}
	else {
		alert('Your browser does not support this shopping menu');
		return;
	}
}

function stripEntities(str) {
	str = str.replace(/&amp;/g, "&");
	str = str.replace(/&#39;/g, "'");
	str = str.replace(/&#xB4;/g, "'");
	str = str.replace(/&#x2019;/g, "'");
	str = str.replace(/&#x2122;/g, "™");
	str = str.replace(/&quot;/g, "\"");

	return str;
}

/**
 * Sets a Cookie with the given name and value.
 * http://www.netspade.com/articles/2005/11/16/javascript-cookies/
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

