var xstylesheetlink="";
var xmlDoc;
var XSLTProcessor = new XSLTProcessor();
var xStyleSheet;
buybuttonN= new Image(100,30);
buybuttonH= new Image(100,30);

function init(file)
{
	importXML(file);
}
function getstylesheet()
{
	var txt=location.search.toLowerCase();
	temp=txt.split("?");
	txt=temp[1];
	var link = ""
	if(!txt)
	{
		link="designs.xsl"
		return link;
	}
	var expression = new Array();
	expression = txt.split("&"); 
	
	var x;
	for(x in expression)
	{
		if(expression[x].match("xstylesheet"))
		{
			temp=expression[x].split("=");
			link=temp[1];
		}
	}
	switch(link)	//place all valid xsl files here.  If not one, change to products.xsl
	{
		case "products.xsl":
		case "designs.xsl":
			break;
		default:
			link="designs.xsl"
			break;
	}
	return link
}
function assignparams()
{
	var txt=location.search;
	temp=txt.split("?");
	txt=temp[1];
	if(!txt)
	{
		return;
	}

	var expression = new Array();
	expression = txt.split("&"); 
	//example URL products.xml?generaltype=apparel+signs&customertype=male+female
	//sample here: expression[0]="generaltype=apparel+signs" - exp[1]="customertype=male+female"

	var x;
	for(x in expression)
	{
		comparefilter(expression[x])
	}
	return ;
}
function comparefilter(expression)
{
	if(expression.indexOf("=")==-1)
	{
		return;
	}
	temp=expression.split("=");

	if(temp[0].indexOf("generaltype")==0)
	{
		changeParameter("generaltype", temp[1]);
	}
	if(temp[0].indexOf("category")==0)
	{
		changeParameter("category", temp[1]);
	}
	if(temp[0].indexOf("customertype")==0)
	{
		changeParameter("customertype", temp[1]);
	}
	if(temp[0].indexOf("type")==0)
	{
		temp[1]=temp[1].replace(/%20/gi, " ");
		temp[1]=temp[1].replace(/%27/gi, "'");
		temp[1]=temp[1].replace(/%22/gi, '"');
		if(temp[1].indexOf("+")!=-1)
		{
			var temptype = "";
			var sectemp=temp[1].split("+");
			temptype=sectemp.join(" ");
			temp[1]=temptype;
		}
		
		changeParameter("type", temp[1]);
	}
	if(temp[0].indexOf("designer")==0)
	{
		changeParameter("designer", temp[1]);
	}
	if(temp[0].indexOf("storeid")==0)
	{
		changeParameter("storeid", temp[1]);
	}
	if(temp[0].indexOf("pricelow")==0)
	{
		changeParameter("pricelow", temp[1]);
	}
	if(temp[0].indexOf("pricehigh")==0)
	{
		changeParameter("pricehigh", temp[1]);
	}
	if(temp[0].indexOf("season")==0)
	{
		changeParameter("season", temp[1]);
	}
	if(temp[0].indexOf("holiday")==0)
	{
		changeParameter("holiday", temp[1]);
	}
	if(temp[0].indexOf("searchterms")==0)
	{
		changeParameter("searchterms", temp[1].toLowerCase());
	}
	if(temp[0].indexOf("page")==0)
	{
		if(temp[1]<1 || temp[1]=="")
		{
			temp[1]=1;
		}
		changeParameter("page", temp[1]);

	}
	if(temp[0].indexOf("numresults")==0)
	{
		if(temp[1]<5)
			temp[1]=5;
		changeParameter("numresults", temp[1]);
	}
	return
}
function changeParameter(variable, value)
{
	var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
	var ie = (typeof window.ActiveXObject != 'undefined');
	if(moz)
	{
		XSLTProcessor.setParameter(null, variable, value);
		return
	}
	else if(ie)
	{
		//Change parameter
		xStyleSheet.selectSingleNode("//xsl:param[@name='"+variable+"']").setAttribute("select","'"+value+"'");
		return
	}
	else
	{
		alert("Filtering Not Available! Sorry! Please email webmaster@brezeegerland.com with browser information");
	}
	return
}
function importXML(file)
{
	var ie = (typeof window.ActiveXObject != 'undefined');
	var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
	
	xstylesheetlink=getstylesheet();
	if (moz)
	{
		xStyleSheet = document.implementation.createDocument("", "", null);
		xStyleSheet.async = false;
		xStyleSheet.load(xstylesheetlink);
		XSLTProcessor.importStylesheet(xStyleSheet);
		assignparams();	//modify params
		xmlDoc = document.implementation.createDocument("","", null);
		xmlDoc.async = false;
		xmlDoc.load(file);	//file=products.xml
		var result = XSLTProcessor.transformToDocument(xmlDoc);
		var xmls = new XMLSerializer();
		var output = xmls.serializeToString(result);
		var alteredoutput=output;
	//	alteredoutput=output.replace(/</gi,"&lt;");
		document.write(alteredoutput);
      
	}
	else if (ie)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		while(xmlDoc.readyState != 4) {};
		xmlDoc.load(file);
		
	    // Load XSL
		xStyleSheet = new ActiveXObject("Msxml2.DomDocument")
		xStyleSheet.async = false
		xStyleSheet.load(xstylesheetlink)
		assignparams(); // modify params
		// Transform
		document.write(xmlDoc.transformNode(xStyleSheet))	//replace serialize and final doc.write
	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
}