
var username = "pools";
var password = "goodducks";
var baseLink = "http://www.pools.com/live/softRelay.php?xml=";	
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";


var global_debug = 0; //Global Debug Variable

var xmlHttp;
var xmlHttp2;
var xmlHttp3;
var mode;

var categoryID;
var partID;
var brandID;
var modelID;

var quantity = 1;
var itemidF;
var priceF;
var descriptionF;
var imgsrcF;
var dimGA;
var dimGB;






<!-- requires rebuild asap to clean up -->

	function initXHttp()
	{
	
	//Populate the categories...
	
	if(global_debug == 1)
		window.alert("Initializing : InitXHttp...");

	try{	
	
		
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Above Ground Pools", "23");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Automatic Cleaners", "1");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Chemical Feeders", "2");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Filters - Cartridge Type", "8");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Replacement Cartridges (Center Holes)", "18");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Replacement Cartridges (Others)", "19");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("D.E. Filters", "4");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Sand Filters (Side Valve)", "6");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Sand Filters (Top Valve)", "7");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Filter Valves", "3");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Underwater Lights", "10");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Pumps", "13");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Salt Chlorine Generators", "25");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Skimmers", "14");
		document.myForm.Category.options[document.myForm.Category.options.length] = new Option("Solar Reels", "24");
	
	}catch(e)
	{
		
		window.alert(e);
	}
	
	

		try{
			if(global_debug == 1)
				window.alert("creating xmlhttp objects...");
			
			
			xmlHttp2 = initAJAX();
				
		} catch(e)
		{
		
		}	
	
	
	}
	
	function initAJAX()
	{
	var temp;
	//We are ie friendly right now, i haven't built in firefox compatibility into 
	//this yet fully....
	try
    	{   
			temp = new ActiveXObject("Microsoft.XMLHTTP")
			return temp;     
		}catch(e)
		{
			try{
				temp = new ActiveXObject("Msxml2.XMLHTTP");
				return temp;
			}catch(e){
					//window.alert("i have not made this firefox/opera/safari compatible yet...");
					//5-22 adding firefox/opera/safari compatibility
				try{
					temp = new XMLHttpRequest();
					return temp;
					
					}catch(e){
								//Unsupported Browser...
								window.alert("debug" + e.toString());
							 }
			}
		}

	}


	function grabType(theId)
	{
	
	if(global_debug == 1)
		window.alert("getting types...");
	
		categoryID = theId;
	
	try{
	
	
		mode = 1;

		

		var XML =  "<?productname Optimus Parts?>";
		XML = XML + "<?customer " + username + "?>";
		XML = XML + "<?password " + password + "?>";
		XML = XML + "<root><parttypes action='get'>";
		XML = XML + "<customername>pools</customername>";
		XML = XML + "<categoryid>" + theId + "</categoryid>";
		XML = XML + "</parttypes></root>";

	
		
		XML = escape(XML);
		XML = encode64(XML);
		
	
		
		xmlHttp = initAJAX();
		xmlHttp.onreadystatechange=dataReceived;
		xmlHttp.open("GET", baseLink + XML, true);
		xmlHttp.send(null);
		
		
		
	}catch(e)
		{
			if(global_debug == 1)
				window.alert("Error in gettypes..." + e.description);
		}
		
	}
	
	function grabBrand(pTID)
	{

	if(global_debug == 1)
		window.alert("getting brands");
	
		partID = pTID;
		
		try{
		
			mode = 2;
			
			var XML = "<?productname Optimus Parts?>";
			XML = XML + "<?customer " + username + "?>";
			XML = XML + "<?password " + password + "?>";
			XML = XML + "<root><brands action = 'get'>";
			XML = XML + "<customername>pools</customername>";
			XML = XML + "<parttypeid>" + pTID + "</parttypeid>";
			XML = XML + "</brands></root>";
			
			XML = escape(XML);
			XML = encode64(XML);
			
			xmlHttp = initAJAX();
			xmlHttp.onreadystatechange=dataReceived;
			xmlHttp.open("GET", baseLink + XML, true);
			xmlHttp.send(null);
		
		
		
		}catch(e)
		{
				if(global_debug == 1)
					window.alert(e);	
		}
	
	
	}
	
	function grabModel(bID)
	{
	
	if(global_debug == 1)
		window.alert("gettings models");
		
		brandID = bID;
		
		try{
		
			mode = 3;
			
			var XML = "<?productname Optimus Parts?>";
			XML = XML + "<?customer " + username + "?>";
			XML = XML + "<?password " + password + "?>";
			XML = XML + "<root><models action = 'get'>";
			XML = XML + "<customername>pools</customername>";
			XML = XML + "<brandid>" + bID + "</brandid>";
			XML = XML + "</models></root>";
			
			XML = escape(XML);
			XML = encode64(XML);
			
			
			xmlHttp = initAJAX();
			xmlHttp.onreadystatechange=dataReceived;
			xmlHttp.open("GET", baseLink + XML, true);
			xmlHttp.send(null);
			
		
		
		}catch(e)
		{
			if(global_debug == 1)
				window.alert(e);	
		}
	
	}
		
	function listProducts()
	{
		
	if(global_debug == 1)
		window.alert("getting products...");	
		
		try{
		
		document.getElementById("loadScreen").style.visibility = "visible";
		
		xmlHttp2 = initAJAX();
		
			
			var XML = "<?productname Optimus Parts?>";
			XML = XML + "<?customer " + username + "?>";
			XML = XML + "<?password " + password + "?>";
			XML = XML + "<root><items action = 'get'>";
			XML = XML + "<parttypeid>" + partID + "</parttypeid>";
			if (brandID != 0)
				XML = XML + "<brandid>" + brandID + "</brandid>";
			if (modelID != 0)
				XML = XML + "<modelid>" + modelID + "</modelid>";
			XML = XML + "<customername>pools</customername>";
			XML = XML + "</items></root>";
			
			XML = escape(XML);
			XML = encode64(XML);
			
			xmlHttp2.onreadystatechange=prodReceived;
			xmlHttp2.open("GET", baseLink + XML, true);
			xmlHttp2.send(null);
		
		
		
		}catch(e)
		{
				if(global_debug == 1)
					window.alert("error in listproducts:" + e);	
		}		
		
		
		
		
	}
	
	function prodReceived()
	{
	
		if(xmlHttp2.readyState==4)
		{
			if(global_debug == 1)
				window.alert("products recieved...");
		
	
		//get listings... (... kludge parse out data from lame xml)
		
		var rData = xmlHttp2.responseText;
		
		var itemid;
		var dima;
		var dimb;
		var description;
		var retailprice;
		var mfid;
		var brand;
		var imgsrc;
		var rif;
		var lastID;
		
		var count = 0;
		
		var hotlink;
		var html = "<table><th>Item ID</th><th>Description</th><th>Brand</th><th>Dimensions</th><th>View Details / Purchase</th>";
		
		var i = rData.indexOf("itemid");
		
			while(i != -1)
			{
				
				i = rData.indexOf(">",i);
				itemid = rData.substring(i + 1, rData.indexOf("<", i));
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
				
				i = rData.indexOf("dimensiona");
				i = rData.indexOf(">", i);
				dima = rData.substring(i + 1, rData.indexOf("<",i));
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
				dimAA = dima;  //Global Variable Set.
				
				i = rData.indexOf("dimensionb");
				i = rData.indexOf(">", i);
				dimb = rData.substring(i + 1, rData.indexOf("<",i));
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
				dimBB = dimb; //Global Variable Set.
				
				i = rData.indexOf("description");
				i = rData.indexOf(">", i);
				description = rData.substring(i + 1, rData.indexOf("<",i));
				description = description.toLowerCase();
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
				
				i = rData.indexOf("retailprice");
				i = rData.indexOf(">", i);
				retailprice = rData.substring(i + 1, rData.indexOf("<",i));
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
				
				i = rData.indexOf("imagesrc");
				i = rData.indexOf(">", i);
				imgsrc = rData.substring(i + 1, rData.indexOf("<",i));
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
				
				i = rData.indexOf("brand");
				i = rData.indexOf(">", i);
				brand = rData.substring(i + 1, rData.indexOf("<",i));
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
												
				i = rData.indexOf("manufacturid");
				i = rData.indexOf(">", i);
				mfid = rData.substring(i + 1, rData.indexOf("<",i));
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
				
				i = rData.indexOf("relateditemsfile");
				i = rData.indexOf(">", i);
				rif = rData.substring(i + 1, rData.indexOf("<",i));
				rData = rData.substring(rData.indexOf("<", i) + 5, rData.length);
				
				//Do a replace - fix for global in and floating single quotes
				description = description.replace("\"", " inch");
				description = description.replace("\"", " inch");
				description = description.replace("\"", " inch");
				
				description = description.replace(/'/g, "");
				
				
								
				hotlink = "<a style='text-decoration:none' href='javascript:showPurchase(\"" + itemid + "\",\"" + retailprice + "\",\"" + description + "\",\"" + imgsrc + "\")'><img border='0' src='http://www.pools.com/images/button_view-details.gif'</a>";
				
				
				if(itemid != lastID)
				{
					if(description.indexOf('obsolete') == -1)
					{
					
					html = html + "<tr>";
					html = html + "<td>" + itemid + "</td><td>" + description + "</td><td>" + brand + "</td><td>" + dima + "x" + dimb + "</td><td>" + hotlink + "</td>";
					html = html + "</tr>";
					count++;
					}
				}
				
				
				
				lastID = itemid;
				i = rData.indexOf("itemid");
				
		
			}
		
	
		html = html + "</table>";
		WriteLayer("partslist", null, html);
		document.getElementById("loadScreen").style.visibility = "hidden"; 
		
		
		}
						
	}
	
	function getBasketID()
	{
	
	if(global_debug == 1)
		window.alert("retrieving cookies");
		
			
		var bid = readCookie("BID");
		
	if(global_debug == 1)
		window.alert("cookie val for bid: " + bid);
		
		return bid;
		
		
	
	}
	
	function readCookie(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) 
		{
			var c = ca[i];
				while (c.charAt(0)==' ') 
					c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
	
	return null;
	}
	
	
	function referNLA(description)
	{
		window.alert("The part you have selected has been replaced by a newer model. We will automatically retrieve it for display below.");
		document.searchBox.pNum.value = description.substring(description.indexOf('w/') + 2);
		searchInv();
		
	}

	function showPurchase(itemid, price, description, imgsrc)
	{
	
	if(global_debug == 1)
		window.alert("setting up purchase");
		

	
		//Set our global variables, show data in the dash, and allow customer to confirm
		itemidF = itemid;
		priceF = price;
		descriptionF = description;
		imgsrcF = imgsrc;
		
		
		var html;
		var ihtml;
		
		html = "<p align='right'><b>Item Number</b><br/>";
		html = html + itemidF;
		html = html + "<br/><b>Description</b><br/>";
		html = html + descriptionF;
		html = html + "<br/><b>Price</b><br/>" + "$" + priceF + "</p>";
		
		
		ihtml = "<img src='http://www.optimusparts.com/Graphics/Catalog/" + imgsrcF + "' width='175' height='115'></img>";
		
		if(imgsrcF == "")
			ihtml = "<img src='http://www.pools.com/images/products/noimage.jpg'></img>";
		
		
		WriteLayer("pDataF", null, html);
		WriteLayer("pImage", null, ihtml);

		document.getElementById("lQuan").style.visibility = "visible";
		document.getElementById("addlayer").style.visibility = "visible";
		document.getElementById("frontImage").style.visibility = "hidden";
		document.location="#Top";
		
	}
		
		
	
	function addToCart()
	{
	
	if(global_debug == 1)
		window.alert("adding to pools.com cart");
	
	
		if((descriptionF.indexOf('nla') != -1) || (descriptionF.indexOf('bad#') != -1))
		{
			referNLA(descriptionF);
			document.getElementById("lQuan").style.visibility = "hidden";
			document.getElementById("addlayer").style.visibility = "hidden";
			WriteLayer("pDataF", null, "<p class='descriptionred'>The product you have attempted to buy was replaced by a newer model, and your replacement options are available below</p>");
			return;	
		}
	
	
	
			
		//shabuoy
		xmlHttp3 = initAJAX();
		var basketID = getBasketID();
		var postData;
		var args;

	
		postData = "http://www.pools.com/live/addtocart.asp?";
		args = "basketID=" + basketID;
		args = args + "&itemID=" + itemidF;
		args = args + "&description=" + escape(descriptionF);
		args = args + "&quantity=" + quantity;
		args = args + "&price=" + priceF;
		args = args + "&dima=" + escape(dimAA);
		args = args + "&dimb=" + escape(dimBB);

		postData = postData + args;

		if(global_debug == 1)
			window.alert("query formed: posting to cart: will escape/encode: " + postData);


						
		xmlHttp3.onreadystatechange=readyPost;
		xmlHttp3.open("GET", postData, false);
		xmlHttp3.send(null);
		
		
		
		
		
	}

	function readyPost()
	{
		if(xmlHttp3.readyState==4)
		{
			//window.alert(xmlHttp3.responseText);
			window.location = "http://www.pools.com/catalog/index.cfm?fuseaction=basket";
		}

	}
	

	function dataReceived()
	{
	var rData;


	
		if(xmlHttp.readyState==4)
			{
			
			if(global_debug == 1)
				window.alert("data received in drill down...");
				
			
				if(mode==1)
						{
						//populate the partType
												
						rData = xmlHttp.responseText;
						
						var category_id = new Array();
						var names = new Array();
												
						category_id = xmlParse(rData, "id");
						names = xmlParse(rData, "name");
						
						
						document.myForm.pType.options[0] = new Option("Select a Part Type", 0);	
											
							for(var a = 0; a < category_id.length - 1;a++)
							{
								document.myForm.pType.options[document.myForm.pType.options.length] = new Option(names[a].replace(/&quot;/g,"\""),category_id[a]);						
							}
						
						window.status = "Loading Complete";
							
							
    					}
						
				if(mode==2)
						{
						//populate the brands
						
						
						
						rData = xmlHttp.responseText;
						
						var brand_id = new Array();
						var names = new Array();
												
						brand_id = xmlParse(rData, "id");
						names = xmlParse(rData, "name");
						
						document.myForm.brand.options[0] = new Option("Select a Brand", 0);	
											
							for(var a = 0; a < brand_id.length - 1;a++)
							{
								document.myForm.brand.options[document.myForm.brand.options.length] = new Option(names[a],brand_id[a]);						
							}
						
						window.status = "Loading Complete";
						
						}
						
					if(mode==3)
					{
					//populate the models
						
						
						
						rData = xmlHttp.responseText;
						
						var model_id = new Array();
						var names = new Array();
												
						model_id = xmlParse(rData, "id");
						names = xmlParse(rData, "name");
						
						document.myForm.model.options[0] = new Option("Select a Model", 0);	
											
							for(var a = 0; a < model_id.length - 1;a++)
							{
								document.myForm.model.options[document.myForm.model.options.length] = new Option(names[a],model_id[a]);						
							}
						
						window.status = "Loading Complete";
						
					
					
					}
					
						 		 
		 		xmlHttp = initAJAX();
		 
		 	}

	}
	

	
	
	function xmlParse(xml, parsetag)
	{
	
	if(global_debug == 1)
		window.alert("parsing xml..");
		
		//0.4 - the fourth implementation is the charm
		var retVal = new Array();
		
		var i = 0;
		var step = 0;
		var temp;
		
		parsetag = parsetag + "=\"";
		
		
		while(i != -1)
		{
			
						
			i = xml.indexOf(parsetag);
			
			xml = xml.substring(i + parsetag.length, xml.length);
			temp = xml.substring(0, xml.indexOf("\""));
			xml = xml.substring(temp.length, xml.length);
			
			retVal[step] = temp;
			step++;
					
		}
		
		return retVal;
				
	}
	

	
	
	
	
	function cChange()
	{
		var theId = document.myForm.Category.value;
		
		//reset subs
		document.myForm.pType.length = 0;
		document.myForm.brand.length = 0;
		document.myForm.model.length = 0;
		
		partID = 0;
		brandID = 0;
		modelID = 0;
		
		window.status = "Loading...";
		
		grabType(theId);
		
	}
	
	function pChange()
	{
		var thePID = document.myForm.pType.value;
		document.myForm.brand.length = 0;
		document.myForm.model.length = 0;
			
		brandID = 0;
		modelID = 0;
		
		
		window.status = "Loading...";
		grabBrand(thePID);
		listProducts();	
	}
	
	function bChange()
	{
		var theBID = document.myForm.brand.value;
		document.myForm.model.length = 0;
		
		modelID = 0;
			
		window.status = "Loading..."
		grabModel(theBID);
		listProducts();	
	}
	
	function mChange()
	{
		modelID = document.myForm.model.value;
		listProducts();	
	}
	
	function uQuantity()
	{
		quantity = form1.select.value;
		//window.alert(quantity);
	}
		
	function WriteLayer(ID,parentID,sText) 
	{ 
				
		 if (document.layers)
		 { 
   			var oLayer; 
   			if(parentID)
			{ 
     			oLayer = eval('document.' + parentID + '.document.' + ID + '.document'); 
   			}else{ 
     		oLayer = document.layers[ID].document; 
   			} 
   				oLayer.open(); 
   				oLayer.innerHTML = sText; 
   				oLayer.close(); 
 			} 
 			else if (parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape") { 
   			document.getElementById(ID).innerHTML = sText; 
 			} 
 			else if (document.all) document.all[ID].innerHTML = sText 
	} 
	
   function encode64(input) 
   {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

//

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function searchInv()
{
if(global_debug == 1)
		window.alert("getting products...");	
		
		try{
		
		document.getElementById("loadScreen").style.visibility = "visible";
		
		xmlHttp2 = initAJAX();
		
			
			var XML = "<?productname Optimus Parts?>";
			XML = XML + "<?customer " + username + "?>";
			XML = XML + "<?password " + password + "?>";
			XML = XML + "<root><altitems action = 'get'>";
			XML = XML + "<itemid>" + document.searchBox.pNum.value + "</itemid>";
			XML = XML + "<customername>pools</customername>";
			XML = XML + "</altitems></root>";
			
						
			
			XML = escape(XML);
			XML = encode64(XML);
			
			xmlHttp2.onreadystatechange=prodReceived;
			xmlHttp2.open("GET", baseLink + XML, true);
			xmlHttp2.send(null);
		
		
		
		}catch(e)
		{
				if(global_debug == 1)
					window.alert("error in listproducts:" + e);	
		}		
		

}

function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			searchInv();
			return false;
		} 
		else
		return true;
	}      


