// JavaScript Document
var txt;
var selectedCat;
txt="";
var CountryCode = "0";
var searchHistory = new Array();
function setCountry(code) {
	CountryCode = code;	
}

function getCats(parent_cat) {
	var xmlHttp;
	txt="";
	var browser;
	selectedCat=parent_cat;
	document.getElementById("menu").innerHTML = "<div><img src='spinner.gif' alt='spinner'></img></div>";
	$(".menu").corner();
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
		browser=0;
	}
	catch (e) {
  		// Internet Explorer
  		try {
  		  	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			browser=1;
  		}
  		catch(e) {
    		try {
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				browser=2;
    		}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
    		}
  		}
	}
	xmlHttp.onreadystatechange=function() 
	{
		if(xmlHttp.readyState==4) 
		{
    		x=xmlHttp.responseXML.documentElement.getElementsByTagName("category");
        	for (i=0;i<x.length;i++) {
			x1=x[i].getElementsByTagName("cat_id");
            	x2=x[i].getElementsByTagName("cat_name");
				x3=x[i].getElementsByTagName("cat_parent_id");
				txt = txt + "\n<div class='menuItem' onmouseover='this.style.backgroundColor=\"#FEFEFF\"' onmouseout='this.style.backgroundColor=\"#DEDEFF\"' onmousedown='this.style.backgroundColor=\"#FFFF00\"' onmouseup='this.style.backgroundColor=\"#DEDEFF\"' onclick='getParentCat(this.id); getCats(this.id);' id='" + x1[0].firstChild.nodeValue + "'>" + x2[0].firstChild.nodeValue + "</div>\n";
			}
	       	document.getElementById("menu").innerHTML = txt;
			$(".menu").corner();
     	}
	}	
	xmlHttp.open("GET","category.php?parent_cat="+parent_cat+"&site_id="+CountryCode,true);
	xmlHttp.send(null);
}

function getParentCat(cat) {
	var xmlHttp;
	var browser;
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
		browser=0;
	}
	catch (e) {
  		// Internet Explorer
  		try {
  		  	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			browser=1;
  		}
  		catch(e) {
    		try {
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				browser=2;
    		}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
    		}
  		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			txt = "";
			y=xmlHttp.responseXML.documentElement.getElementsByTagName("category");
        	for (i=0;i<y.length;i++) {
        		y1=y[i].getElementsByTagName("cat_parent_id");
				y2=y[i].getElementsByTagName("cat_name_path");
				txt = txt + "\n<div><a onclick='getParentCat(this.id); getCats(this.id);' id='" + y1[0].firstChild.nodeValue + "'><img src='up-directory.gif' alt='up arrow' height='24px' width='24px' /></a></div>\n";
			}
			//document.getElementById("menu").innerHTML = txt;
			if(cat!="-1") {
					var temp = y2[0].firstChild.nodeValue;
					temp = temp.replace(/:/g, " --> ");
					document.getElementById("currentCat").innerHTML = "<span style='font-family: courier; font-size: .90em;'><b>Current Category:</b> " + temp + "&nbsp;<img class='searchButton' onclick='getSearchedWords(this)' src='search.gif' alt='search' height='24px' width='24px' /></span>";
			}
			
			else {
					document.getElementById("currentCat").innerHTML = "<span style='font-family: courier; font-size: .90em;'><b>Current Category:</b> No Category Selected</span>";
			}
			
			$(".menu").corner();
     	}
	}	
	xmlHttp.open("GET","category.php?cat_id="+cat+"&site_id="+CountryCode,true);
	xmlHttp.send(null);
}

	txt = "";
function getSearchedWords(sender) {
	document.getElementById("contentBody").innerHTML = "<img src='spinner.gif' alt='spinner' />";
	var category = "";
	var searchWords = "";
	var searchResults;
	var country;
	
	country = "&siteid=" + CountryCode;
	if(selectedCat=='-1') {
		category = "";
	}
	else {
		category = "catID="+selectedCat;
	}
	if(document.getElementById("searchWords").value=="" || document.getElementById("searchWords").value=="Search terms...") {
		searchWords = "";
	}
	
	else {
		// Add words to search array for back button code.
		if(sender.className!="goBack") {
			searchHistory.push(document.getElementById("searchWords").value);
		}
		searchWords = "&keyword=" + document.getElementById("searchWords").value;
	}
	if(searchHistory) {
		document.getElementById("backButton").innerHTML = "<button onclick='goBack(this);' class='goBack'>Back</button>";
	}
	else {
		document.getElementById("backButton").innerHTML = "";
	}
	var xmlHttp = null;
	var browser;
	var contentText="";
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
		browser=0;
	}
	catch (e) {
  		// Internet Explorer
  		try {
  		  	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			browser=1;
  		}
  		catch(e) {
    		try {
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				browser=2;
    		}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
    		}
  		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var alternativeWord;
			var relatedWord;
    	  	searchResults = "<table class='searchTable'>\n";
			searchResults += "<thead><th>Alternatives</th><th>Related</th></thead>";
			searchResults += "<tbody>";
			a=xmlHttp.responseXML.documentElement.getElementsByTagName("altWord");
			r=xmlHttp.responseXML.documentElement.getElementsByTagName("relatedWord");
			searchResults += "<tr><td>";
			if(a[0].firstChild!=null) {
				searchResults += "<table>";
				for(count=0;count<a.length;count++) {
					alternativeWord = a[count].firstChild.nodeValue;
					searchResults += "<tr><td><a href=\"#\" onclick=\"setSearchBox('" + alternativeWord + "'); getCats('-1');getSearchedWords(this);\">" + alternativeWord + "</a></td>\n";
					searchResults += "<td><a target=\"new_\" class=\"resultButton\" href=\"http://whats-hot-weekly.com/static.php?keyword=" + alternativeWord + "\"><img class=\"resultButton\" src=\"whw_button.jpg\" alt=\"most popular items button\" /></a>&nbsp;&nbsp;&nbsp;&nbsp;";
					searchResults += "<a target=\"new_\" class=\"resultButton\" href='http://www.google.com/search?q=\"" + alternativeWord + "\"'><img class=\"resultButton\" src=\"g.jpg\" alt=\"google button\" /></a>&nbsp;&nbsp;&nbsp;&nbsp;";
					searchResults += "<a target=\"new_\" class=\"resultButton\" href='http://search.yahoo.com/search?p=\"" + alternativeWord + "\"'><img class=\"resultButton\" src=\"y.jpg\" alt=\"yahoo button\" /></a>&nbsp;&nbsp;&nbsp;&nbsp;";
					searchResults += "<a target=\"new_\" class=\"resultButton\" href='http://www.bing.com/search?q=\"" + alternativeWord + "\"'><img class=\"resultButton\" src=\"b.jpg\" alt=\"bing button\" /></a></td></tr>";		
				}
				searchResults += "</table>\n";				
			}
			searchResults += "</td><td>\n";
			
			if(r[0].firstChild!=null) {
				searchResults += "<table>";
				for(count=0;count<r.length;count++) {
					relatedWord = r[count].firstChild.nodeValue;
					searchResults += "<tr><td><a href=\"#\" onclick=\"setSearchBox('" + relatedWord + "'); getCats('-1'); getSearchedWords(this); \">" + relatedWord + "</a>&nbsp;&nbsp;\n";
					searchResults += "<td><a target=\"new_\" class=\"resultButton\"href=\"http://whats-hot-weekly.com/static.php?keyword=" + relatedWord + "\"><img class=\"resultButton\" src=\"whw_button.jpg\" alt=\"most popular items button\" /></a>&nbsp;&nbsp;&nbsp;&nbsp;";
					searchResults += "<a target=\"new_\" class=\"resultButton\" href='http://www.google.com/search?q=\"" + relatedWord + "\"'><img class=\"resultButton\" src=\"g.jpg\" alt=\"google button\" /></a>&nbsp;&nbsp;&nbsp;&nbsp;";
					searchResults += "<a target=\"new_\" class=\"resultButton\" href='http://search.yahoo.com/search?p=\"" + relatedWord + "\"'><img class=\"resultButton\" src=\"y.jpg\" alt=\"yahoo button\" /></a>&nbsp;&nbsp;&nbsp;&nbsp;";
					searchResults += "<a target=\"new_\" class=\"resultButton\" href='http://www.bing.com/search?q=\"" + relatedWord + "\"'><img class=\"resultButton\" src=\"b.jpg\" alt=\"bing button\" /></a></td></tr>";		
				}
				searchResults += "</table>\n";
			}
			searchResults += "</td></tr>\n";
			searchResults += "</table>\n";
			document.getElementById("contentBody").innerHTML = searchResults;
		  
		}
   	}
		xmlHttp.open("GET","searches.php?"+category+searchWords+country+"&MaxEntries=1000" , true);
		xmlHttp.send(null);	
	
}
function setSearchBox(word) {
	document.getElementById("searchWords").value = word;
}

function goBack(a) {
	var oldSearchWords = searchHistory[searchHistory.length -2];
	searchHistory.pop();
	document.getElementById("searchWords").value = oldSearchWords;
	getSearchedWords(a);
}

$(function() {
	$(".menu").corner();
	$("div.corner").corner();
	$("#searchForm").submit(function(event){ event.preventDefault(); });
});
