var gXMLHTTPHOST = "hotlist_ajaxhost.php";
var gIESHOWHIDE = "block";

function classXmlHTTP() {
	function createXMLHttpRequest() {
		if(window.XMLHttpRequest) { try { xmlHttpRequest = new XMLHttpRequest(); } catch(e) { return null; } }
		else if(window.ActiveXObject) {
			try { 
				xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) { try { xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) { return null; } }
		}
		else { return null; }
		
		return xmlHttpRequest;
	}
	
	var o_XmlHttp = createXMLHttpRequest(); 
	this.CallBack = function() {}
	this.readyState    = function() { return o_XmlHttp.readyState; }
	this.status		   = function() { return o_XmlHttp.status; }
	this.responseText  = function() { return o_XmlHttp.responseText; }
	this.responseXML  = function() { return o_XmlHttp.responseXML; }
	this.XmlHttp       = function() { return o_XmlHttp; }
	
	this.Get = function(sUrl) {	
		o_XmlHttp.onreadystatechange = this.CallBack;
		o_XmlHttp.open("GET", sUrl, true);
		o_XmlHttp.send(null);
	}
	
	this.Post = function(sUrl, sPostData) {
		if (sPostData == undefined) sPostData = "";
		o_XmlHttp.onreadystatechange = this.CallBack;
		o_XmlHttp.open("POST", sUrl, true);
		//o_XmlHttp.setRequestHeader("Content-Type", "text/xml");
		o_XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//o_XmlHttp.setRequestHeader("Content-length", sPostData.length);
		o_XmlHttp.setRequestHeader("Connection", "close");
		o_XmlHttp.send(sPostData);
	}
	
	this.Abort = function() { o_XmlHttp.abort(); }
}

function sendXmlhttpData(postData) {
	var xmlhttp = new classXmlHTTP();
	xmlhttp.CallBack = function() {
		if (xmlhttp.readyState() == 4)
			if (xmlhttp.status() == 200)
				onXmlhttpReceive(xmlhttp);
	}

	xmlhttp.Post(gXMLHTTPHOST, postData);
}

function onXmlhttpReceive(xmlhttp) {
	var txtResponse = xmlhttp.responseText();
	if (txtResponse.substring(0,1) == "<") { processReturnData(xmlhttp); }
	else { /*alert("onXmlhttpReceive: " + txtResponse);*/ }
}

function processReturnData(xmlhttp) {
	var xmlResponse = xmlhttp.responseXML();
	var HotlistCheck = xmlResponse.getElementsByTagName("Message");
	if(HotlistCheck[0].childNodes.length > 0) {
		switch(HotlistCheck[0].childNodes[0].nodeValue) {
			case "Hotlist":
				updateHotlistItems(xmlResponse);
				break;
			default:
				break;
		}
	}
	else { document.getElementById("hotlistDiv").style.display = "none"; }
}

function updateHotlist(queryType, userID, productID) {
	var xmlPost = "<?xml version='1.0' encoding='UTF-8'?><postxml>";
	xmlPost += "<querytype>" + queryType + "</querytype>";
	xmlPost += "<userID>" + userID + "</userID>";
	xmlPost += "<productID>" + productID + "</productID>";
	xmlPost += "</postxml>";
	xmlPost = escape(xmlPost);
	sendXmlhttpData("xmlPost="+xmlPost);
}

function updateHotlistItems(xmlResponse) {
	var tblHotlist = document.getElementById("hotlist_items");
	while(tblHotlist.childNodes.length > 0)
        tblHotlist.removeChild(tblHotlist.firstChild);

	if(xmlResponse.getElementsByTagName("Product")) {
		var Hotlist = xmlResponse.getElementsByTagName("Product");

		var n = 0;
		while(n < Hotlist.length) {
			tblHotlist.appendChild(document.createElement("tr"));
			n += 3;
		}

		for(var i = 0; i < Hotlist.length; i++) {
			if(checkIE() > -1) {
				var userID = Hotlist[i].childNodes[0].text;
				var productID = Hotlist[i].childNodes[1].text;
				var productImage = Hotlist[i].childNodes[2].text;
			}
			else {
				var userID = Hotlist[i].childNodes[0].textContent;
				var productID = Hotlist[i].childNodes[1].textContent;
				var productImage = Hotlist[i].childNodes[2].textContent;
			}

			var td1 = document.createElement("td");
			td1.style.width = "49%";
			td1.setAttribute("align", "center");
			
			var a1 = document.createElement("a");
			a1.setAttribute("href", 'http://www.superav.com/details_final.php?productID=' + productID + '&ci=' + userID);
			a1.setAttribute("target", "_blank");
			var img1 = document.createElement("img");
			img1.setAttribute("src", "./image/" + productImage);
			img1.style.width = "40px";
			img1.style.height = "50px";
			img1.style.border = "0px solid black";
			a1.appendChild(img1);
			
			var a2 = document.createElement("a");
			a2.setAttribute("href", 'http://www.superav.com/details_final.php?productID=' + productID + '&ci=' + userID);
			a2.setAttribute("target", "_blank");
			var img2 = document.createElement("img");
			img2.setAttribute("src", "./image/button/magnifier-small.png");
			img2.style.border = "0px solid black";
			a2.appendChild(img2);

			var img3 = document.createElement("img");
			img3.setAttribute("src", "./image/button/minus-small-circle.png");
			img3.style.cursor = "pointer";
			img3.style.cursor = "hand";
			img3.setAttribute("onClick", "updateHotlist('deleteHotlist', '" + userID + "', '" + productID + "');");

			td1.appendChild(a1);
			td1.appendChild(document.createElement("br"));
			td1.appendChild(a2);
			td1.appendChild(img3);
			
			//var n = 0;
			//if(i%3 == 0) { n = i/3 } else { n = (i-1)/3 };
			var n = Math.floor(i/3);
			tblHotlist.childNodes[n].appendChild(td1);
		}
		
		if(checkIE() > -1) {
			var innerH1 = '<table width="100%" border="0" cellpadding="1" cellspacing="0" align="center"><tr><td bordercolor="#333333" bgcolor="#000000"><div align="center"><img src="./image/button/boobs-hotlist.gif" style="text-decoration:none;" /><br /><img src="./image/button/boobs-hotlist-icon.jpg" style="text-decoration:none;" /><br /></div></td></tr><tr><td><div id="hotlist_items_container" style="display:' + gIESHOWHIDE + ';"><table id="hotlist_items" width="100%">';
			
			var innerH2 = '</table></div></td></tr><tr><td align="center" bgcolor="#000000"><a id="imageDivLink" href="javascript:showhide(\'hotlist_items_container\', \'imageDivLink\');"><img src="./image/button/button-close.jpg" name="showhideDivLink" border="0" onMouseOver="swap(\'showhideDivLink\',\'mouseover_close\')" onMouseOut="swap(\'showhideDivLink\',\'normal_close\')" /></a></td></tr></table>';
			
			if(gIESHOWHIDE == "none") {
				var innerH2 = '</table></div></td></tr><tr><td align="center" bgcolor="#000000"><a id="imageDivLink" href="javascript:showhide(\'hotlist_items_container\', \'imageDivLink\');"><img src="./image/button/button-open.jpg" name="showhideDivLink" border="0" onMouseOver="swap(\'showhideDivLink\',\'mouseover_open\')" onMouseOut="swap(\'showhideDivLink\',\'normal_open\')" /></a></td></tr></table>';
			}
			
			document.getElementById("hotlist_box").innerHTML = innerH1 + tblHotlist.innerHTML + innerH2;
		}

		if(Hotlist.length > 0) {
			document.getElementById("hotlist_box").style.display = "block";
			document.getElementById("hotlist_items").style.display = "block";
		}
		else {
			document.getElementById("hotlist_box").style.display = "none";
			document.getElementById("hotlist_items").style.display = "none";
		}
	}
	else {
		document.getElementById("hotlist_box").style.display = "none";
		document.getElementById("hotlist_items").style.display = "none";
	}
}

function checkIE()
{
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

