// JavaScript Document
var xmlHttp;
var aff_num;
var kad_w;
var kad_font;
var kad_font_size;
var kad_img_border_color;
var kad_table_border_color;
var kad_table_background_color;
var kad_price_color;
var kad_titre;
var kad_other;
var kssq;
var kcatId;
var krdr;
var kimage;
var knbcol;
var knbp;
var kbutton;
var kbuttonUrl;
var kbuttonSearchUrl;
var kpower;
var ksearchfield;
var kepi;
var flagLoadDataFirstTime = 0;
var DivID = 0;
var activeDivID = 0;
var flagFindSearch = 0; 
var kssq_1 = "";
var kcatId_1 = "";
var knbp_1 = "";
var mydomain, mydomain_client;
mydomain = "www.comparebuyandsave.com/ns";
mydomain_client=window.location.href.match(/:\/\/(.[^/]+)/)[1];
if((mydomain_client != "") && (mydomain_client.toLowerCase().indexOf("comparebuyandsave",0) >=0))
{
	mydomain = mydomain_client + "/ns";
}


document.write("<form name='form"+ DivID +"' onsubmit='return false;'><table border='0' cellpadding='2' cellspacing='0' width='100%' style='font-family:Verdana;font-size:12;'><tr><td colspan='3' style='font-family:Verdana;font-size:14;'><label id=\"lblRefine"+ DivID +"\" name=\"lblRefine"+ DivID +"\"><b>What are you shopping for?</b></label>"
			   +"</td><tr><td width='330px'><input type='text' id='txtRefine"+ DivID +"' name='txtRefine"+ DivID +"' value='' style='font-family:Verdana;font-size:12;width:150px;'>"
			   +"&nbsp;<span id='spanSubCategory' style='font-family:Verdana;font-size:12;'><select id='selSubCategory' style='font-family:Verdana;font-size:12;width:170px;'><option value='0'> -- Select Category -- </option></select></span></td>"
			   +"<td width=\"50\"><a id='abtnSearch"+ DivID +"' href=\"javascript:findSearch('"+ kssq +"','"+ kcatId +"','"+ knbp +"','"+ DivID +"');\"><img id='btnSearch"+ DivID +"' border=\"0\" src='http://www.comparebuyandsave.com/images/Buttons/go.gif' width=\"52\" height=\"21\" title=\"Search within results\" alt=\"Go\"></img></a></td>"
			   +"<td style='font-family:Verdana;font-size:12;' align='right'><b>Sort By:</b>&nbsp;<select id='selSortBy"+ DivID +"' name='selSortBy"+ DivID +"' style='font-family:Verdana;font-size:12;width:150px;' onchange=\"javascript:findSearch('"+ kssq +"','"+ kcatId +"','"+ knbp +"','"+ DivID +"');\"><option value='default_ranking'>Popularity</option>"
			   +"<option value='price_ascending'>Price Ascending</option><option value='price_descending'>Price Descending</option>"
			   +"<option value='totalprice_ascending'>Total Price Ascending</option><option value='totalprice_descending'>Total Price Descending</option></select>"
			   +"<input type='hidden' id='hdFlagState"+ DivID +"' name='hdFlagState"+ DivID +"' value='Load_ipod'><input type='hidden' id='hdCatId"+ DivID +"' name='hdCatId"+ DivID +"' value=''></td></tr></table><div id='divID"+ DivID +"' width='100%'></div></form>");

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function findSearch(varkssq,varkcatId,varknbp,vardivID)
{
	var strFlagSTR = trim(document.getElementById("txtRefine" + vardivID).value);
	var strkcatId = varkcatId;
	if(strFlagSTR == "")
	{
		strFlagSTR = varkssq;
	}
	else
	{
		strFlagSTR = varkssq + " " + strFlagSTR;
	}
	
	if(document.getElementById("selSubCategory").options[document.getElementById("selSubCategory").selectedIndex].value != "0")
	{
		strkcatId = document.getElementById("selSubCategory").options[document.getElementById("selSubCategory").selectedIndex].value;
		if(trim(document.getElementById("txtRefine" + vardivID).value) != "")
		{
			//strFlagSTR = document.getElementById("selSubCategory").options[document.getElementById("selSubCategory").selectedIndex].text + " " + trim(document.getElementById("txtRefine" + vardivID).value);
			strFlagSTR = trim(document.getElementById("txtRefine" + vardivID).value);
		}
		else
		{
			strFlagSTR = document.getElementById("selSubCategory").options[document.getElementById("selSubCategory").selectedIndex].text;
		}
	}
	else
	{
		alert("Please Select Category.");
		document.getElementById("divID" + vardivID).innerHTML = "&nbsp;";
		return;
	}
	
	if(document.getElementById("hdFlagState" + vardivID).value == "Load_ipod")
	{
		Load_ipod(strFlagSTR,strkcatId,"1",varknbp,document.getElementById("selSortBy" + vardivID).options[document.getElementById("selSortBy" + vardivID).selectedIndex].value,vardivID);	
	}
	else if(document.getElementById("hdFlagState" + vardivID).value == "Load_ComparePrice")
	{
		Load_ComparePrice(strFlagSTR,strkcatId,document.getElementById("hdCatId" + vardivID).value,"1",varknbp,document.getElementById("selSortBy" + vardivID).options[document.getElementById("selSortBy" + DivID).selectedIndex].value,vardivID);
	}
}

//findSearch(kssq,kcatId,knbp,DivID);
document.getElementById("spanSubCategory").innerHTML = document.getElementById("FillSubCategory").innerHTML;
document.getElementById("FillSubCategory").innerHTML = "&nbsp;";

function Load_ipod(varkssq,varkcatId,varstart,varknbp,strsort,vardivID)
{ 
	if(parseInt(vardivID,10) == 0)
		window.scrollTo(0,0);
		
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	document.getElementById("hdFlagState" + vardivID).value = "Load_ipod";
	document.getElementById("lblRefine" + vardivID).style.display="inline";
	document.getElementById("txtRefine" + vardivID).style.display="inline";
	document.getElementById("btnSearch" + vardivID).style.display="inline";
	document.getElementById("abtnSearch" + vardivID).style.display="inline";
	document.getElementById("spanSubCategory").style.display="inline";
	var url="http://" + mydomain + "/test_link.php?flag=FindItems&query="+escape(varkssq)+"&category="+varkcatId+"&start="+varstart+"&results="+varknbp + "&sort="+escape(strsort) + "&divID=" + vardivID;
	//var url="test.php?flag=FindItems&query="+escape(varkssq)+"&category="+varkcatId+"&start="+varstart+"&results="+varknbp + "&sort="+escape(strsort) + "&divID=" + vardivID;
	url=url+"&sid="+Math.random();
	activeDivID = vardivID;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	document.body.style.cursor = 'wait';
	try
    {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			var outPut=xmlHttp.responseText; 
			document.getElementById("divID" + activeDivID).innerHTML = outPut;
			document.body.style.cursor = 'auto'; 
			if(document.getElementById("hdFlagState" + activeDivID).value == "Load_ipod")
			{
				document.getElementById("txtRefine" + activeDivID).focus();
			}
		}
	}
	catch(e)
	{
		document.body.style.cursor = 'auto'; 
	}
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp; 
}

function Load_ComparePrice(varkssq,varkcatId,varCatId,varstart,varknbp,strsort,vardivID)
{ 
	if(parseInt(vardivID,10) == 0)
		window.scrollTo(0,0);
		
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	document.getElementById("hdFlagState" + vardivID).value = "Load_ComparePrice";
	document.getElementById("hdCatId" + vardivID).value = varCatId;
	document.getElementById("lblRefine" + vardivID).style.display="none";
	document.getElementById("txtRefine" + vardivID).style.display="none";
	document.getElementById("btnSearch" + vardivID).style.display="none";
	document.getElementById("abtnSearch" + vardivID).style.display="none";
	document.getElementById("spanSubCategory").style.display="none";
	activeDivID = vardivID;
	var url="http://" + mydomain + "/test_link.php?flag=ComparePrice&CatalogId="+varCatId+"&query="+escape(varkssq)+"&category="+varkcatId+"&start="+varstart+"&results="+varknbp + "&sort="+escape(strsort) + "&divID=" + vardivID;
	//var url="ComparePrice_CatalogListing.php?flag=ComparePrice&CatalogId="+varCatId+"&query="+escape(varkssq)+"&category="+varkcatId+"&start="+varstart+"&results="+varknbp + "&sort="+escape(strsort) + "&divID=" + vardivID;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

document.onkeyup = KeyCheck;       
function KeyCheck(e)
{
   	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	if(KeyID == 13) 
	{
		findSearch(kssq,kcatId,knbp,DivID);
		return false;
	}
}


