// JavaScript Document
var xmlHttp;
var productID='';
var IfNoResults, ReviewSelection;
var flagIfNoResults = 0, knbp;
var mydomain, mydomain_client;
mydomain = "www.astuteshopping.com/v2_to_v3";
mydomain_client=window.location.href.match(/:\/\/(.[^/]+)/)[1];
if((mydomain_client != "") && (mydomain_client.toLowerCase().indexOf("astuteshopping",0) >=0))
{
	mydomain = mydomain_client + "/v2_to_v3";
}

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

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

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

function findSearch()
{ 
	window.location = (document.getElementById('hdSearchString').value == "?" ? "?txtRefine=" : document.getElementById('hdSearchString').value + "&txtRefine=") + escape(document.getElementById('txtRefine').value) + "&btnSearch=On&productID="+productID; 
}
function findSearchNo()
{ 
	window.location = (IfNoResults) ;
}

function sortSearch(str)
{
	var str1 = (str == "?" ? "?sort=" : str + "&sort=") + escape(document.getElementById('selSortBy').options[document.getElementById('selSortBy').selectedIndex].value)+"&productID="+productID; 
	window.location = str1; 
}

function Check_CheckedBoxes()
{
	if(productID != "")
	{
		productID = productID.substring(0,productID.length-1); 
		if(productID.indexOf(",") > 0)
		{
			var arr = productID.split(",");
			for(j=0;j<arr.length;j++)
			{
				for (i=0; i<document.form0.checkgroup.length; i++)
				{
					if (document.form0.checkgroup[i].value==arr[j])
					{
						document.form0.checkgroup[i].checked = true;
						break;
					}
				}
			}
		}
		else
		{
			for (i=0; i<document.form0.checkgroup.length; i++)
			{
				if (document.form0.checkgroup[i].value==productID)
				{
					document.form0.checkgroup[i].checked = true;
					break;
				}
			}
		}	
		productID = productID + ",";
	}
}

function getProductId()
{
	if(productID != "")
	{
		productID = productID.substring(0,productID.length-1); 
		ViewSavedList(productID);
		productID = productID + ",";
	}
	else
	{
		alert("Please select at least one product(s) to view.");
	}
}

/////////////////////////// Compare Products ////////////////////////////////////////
function ViewSavedList(productID)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	document.getElementById("lblRefine").style.display="none";
	document.getElementById("txtRefine").style.display="none";
	document.getElementById("btnSearch").style.display="none";
	document.getElementById("abtnSearch").style.display="none";
	
	var url="http://" + mydomain + "/View_Saved_List.php?productId="+productID;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedCP;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChangedCP() 
{ 
	document.body.style.cursor = 'wait';
	try
    {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			var outPut = xmlHttp.responseText; 
			outPut = "<table width='100%' border='0' cellpadding='2' cellspacing='0'><tr><td align='right'><a href=\"" + (ReviewSelection == "?" ? "?productID=" : ReviewSelection + "&productID=") + productID + "\" style='font-family:Verdana;font-size:12;'>Return To Search</a></td></tr></table>" + outPut;
			document.getElementById("divID").innerHTML = outPut;
			document.body.style.cursor = 'auto';
		}
	}
	catch(e)
	{
		document.body.style.cursor = 'auto'; 
		document.getElementById("lblRefine").style.display="inline";
		document.getElementById("txtRefine").style.display="inline";
		document.getElementById("btnSearch").style.display="inline";
		document.getElementById("abtnSearch").style.display="inline";
	}
} 


///////////////////////////////////////////////////////////////////
function chkproducts(e)
{
	if (e.checked==true)
	{
		productID = productID + e.value + ",";		
	}
	else
	{
		if(productID.indexOf(e.value + ",") >= 0)
		{
			productID = productID.replace(e.value + ",","");
		}
		else if(productID.indexOf("," + e.value) > 0)
		{
			productID = productID.replace("," + e.value,"");
		}
	}
}



function fnShowDetails(count)
{
	for(i=0;i<knbp;i++)
	{
		if(count == i)
		{
			document.getElementById('show_details'+i).style.display = 'inline';
			document.getElementById('hide_details'+i).style.display = 'none';
			break;
		}
	}
}
 
function fnhideDetails(count)
{
	for(i=0;i<knbp;i++)
	{
		if(count == i)
		{
			document.getElementById('show_details'+i).style.display = 'none';
			document.getElementById('hide_details'+i).style.display = 'inline';
			break;
		}
	}
}

function ReturnSearch()
{
	flagIfNoResults = 1;
	document.getElementById("txtRefine").value = "";
	if(IfNoResults){
		findSearchNo();
	}else{
		findSearch();
	}
}

function addProductID(str)
{
	var str1 = (str == "?" ? "?productID=" : str + "&productID=") + productID; 
	window.location = str1;
}

