var xmlHttp;
var xmlHttp2 = false;
var value_str;

function catprod(str, str2, str3)
{
	location.href="/products/cartridge/"+str+".html/"+str2+"|_|"+str3;
}

function getmodel(str, str2)
{
	document.getElementById('result').innerHTML = "";
	document.getElementById('cat_1').style.display = "none";
	document.getElementById('cat_2').style.display = "none";
	if(str != "")
	{
		document.getElementById('model_1').style.display = "block";
		document.getElementById('model_2').style.display = "block";
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		var url="/global/cartridge.html"
		var url=url+"?sect="+(str.replace(/ /g, "_"))+"&get=model&model="+str2.replace(/ /g, "_");
		
		xmlHttp.onreadystatechange=stateChanged1
 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

	}
}

function searchprod(str, str2)
{
	if(str != "")
	{
		document.getElementById('cat_1').style.display = "none";
		document.getElementById('cat_2').style.display = "none";
		document.getElementById('result').style.display = "block";
		document.getElementById('result').innerHTML = "";

		xmlHttp2=GetXmlHttpObject();
		if (xmlHttp2==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		var url="/global/cartridge.html";
		var url=url+"?model="+(str.replace(/ /g, "_"))+"&manu="+(str2.replace(/ /g, "_"))+"&get=search";
		xmlHttp2.open("GET",url,true)
		xmlHttp2.setRequestHeader('Content-Type', 'text/xml');
		xmlHttp2.setRequestHeader('Cache-Control', 'no-cache');
		xmlHttp2.setRequestHeader('Cache-Control', 'must-revalidate');
		xmlHttp2.setRequestHeader('Cache-Control', 'no-store');
		xmlHttp2.setRequestHeader('Pragma', 'no-cache');
		xmlHttp2.setRequestHeader('Expires', '0');
		xmlHttp2.onreadystatechange=stateChanged2;
		xmlHttp2.send(null);
		value_str = str;
		value_str2 = str2;
		halt = "ok";
	}
 
}



function stateChanged1() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		xmlDoc=xmlHttp.responseText;
		
		document.getElementById("model_2").innerHTML="<select name=\"Model\" id=\"Model\" class=\"cartridgeselect\" onchange='searchprod(this.value, Manufacturer.value);'>"+xmlDoc+"</select>";
		halt = "ok";
		if(document.getElementById("Model").value != ""){ searchprod(document.getElementById("Model").value, document.getElementById("Manufacturer").value);  halt="please_stop"; }
	}
} 

function stateChanged2() 
{
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
	{
		var xmlDoc = xmlHttp2.responseXML.documentElement;
		var log = xmlDoc.getElementsByTagName('LOG')[0].firstChild.nodeValue;
		var category = xmlDoc.getElementsByTagName('category')[0].firstChild.nodeValue;
		var part = xmlDoc.getElementsByTagName('part')[0].firstChild.nodeValue;
		if(log == "colour")
		{
			document.getElementById('cat_1').style.display = "block";
			document.getElementById('cat_2').style.display = "block";
			document.getElementById("cat_2").innerHTML="<select name='Cat' id='Cat' class='cartridgeselect' onchange='catprod(this.value, \""+part+"\", \""+value_str+"\");'><option value=''>Select a category</option>"+category.replace(/#/g, "<")+"</select>";
			
		}
		else if(log == "no")
		{
			document.getElementById("result").innerHTML="<p style=\"color: #ff0000; font-weight: bold; test-align: center;\">Unfortunately at present we do not have any cartridges/toners loaded on the site for the "+value_str+" printer you selected.</p>";
		}
		else
		{
			if(halt!="please_stop")
			{
				location.href="/products/cartridge/"+category+".html/"+part+"|_|"+value_str;
			}
			else
			{
				document.getElementById("Model").selectedIndex = 0;
			}
		}
	}
}

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