/**************************************************************************
Del.icio.us Direc.tor
(c) Copyright 2005 Johnvey Hwang
http://johnvey.com/features/deliciousdirector/

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**************************************************************************/


/*************************************************************************
 * JS prototyperrific object extensions
 ************************************************************************/
String.prototype.toDOM = function() {
	if (document.implementation.createDocument){
		var parser = new DOMParser();
		return parser.parseFromString(this, "text/xml");
	} else if (window.ActiveXObject){
		var dom = new ActiveXObject("Msxml2.DOMDocument")
		dom.async="false";
		dom.loadXML(this);
		return dom;
	}
}

Array.prototype.indexOf = function(q) {
	var i = this.length;
	var output = -1;
	for(var i=0, x=this.length; i < x; i++) {
		if(this[i][0] == q) {
			output = i;
			break;
		}
	}
	return output;
}

Hash = function() {
}


/*************************************************************************
 * XML/XSL utility classes (and no, Safari can't play)
 ************************************************************************/
jvXmlRequest = function() {
	this.adapter = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
		try {
			this.adapter = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.adapter = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				this.adapter = false;
			}
		}
	@end @*/
	if (!this.adapter && typeof(XMLHttpRequest) != 'undefined') {
		this.adapter = new XMLHttpRequest();
	}
}

jvXmlRequest.prototype.adapter = null;

jvXmlRequest.prototype.getDocument = function(sUrl) {
	this.adapter.open("GET", sUrl, false);
	this.adapter.send(null);
	return this.adapter.responseXML;
	
}


jvXslProcessor = function() {
}

jvXslProcessor.prototype._proc = null;

jvXslProcessor.prototype.importStylesheet = function(sXsl) {
	
	if(document.implementation.createDocument) {
		this._proc = new XSLTProcessor();
		this._proc.importStylesheet(sXsl.toDOM());

	} else {
		var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
		var xslTemplate = new ActiveXObject("Msxml2.XSLTemplate");
		xslDoc.async = false;
		xslDoc.loadXML(sXsl);
		xslTemplate.stylesheet = xslDoc;
		this._proc = xslTemplate.createProcessor();
		
	}
	
}

jvXslProcessor.prototype.transformToFragment = function(xmlSource, domDocument) {
	if(xmlSource == null) return "";
	if(document.implementation.createDocument) {
		var fragment = this._proc.transformToFragment(xmlSource, domDocument);
		// create container and insert offline
		var tmpBox = document.createElement("div");
		tmpBox.appendChild(fragment);
		return tmpBox.innerHTML;
		
	} else {
		this._proc.input = xmlSource;
		this._proc.transform();
		var rawXML = this._proc.output;
		return rawXML;
		
	}
}

jvXslProcessor.prototype.getParameter = function(paramName, paramValue) {
	this._proc.getParameter(paramName, paramValue);

}

jvXslProcessor.prototype.setParameter = function(namespace, paramName, paramValue) {
	if(document.implementation.createDocument) {
		this._proc.setParameter(namespace, paramName, paramValue);
	} else {
		this._proc.addParameter(paramName, paramValue);
	}

}


/*************************************************************************
 * Del.icio.us Direc.tor global super awesom-o class
 ************************************************************************/

jvDeliciousFinder = function() {
}

jvDeliciousFinder.prototype.tableBody = null;
jvDeliciousFinder.prototype.sortOrder = null;
jvDeliciousFinder.prototype.sortCol = null;
jvDeliciousFinder.prototype.sortType = null;
jvDeliciousFinder.prototype.pageNumber = null;
jvDeliciousFinder.prototype.recordsPerPage = null;
jvDeliciousFinder.prototype.query = null;
jvDeliciousFinder.prototype.xslProc = null;
jvDeliciousFinder.prototype.xmlReq = null;
jvDeliciousFinder.prototype.allPosts = null;
jvDeliciousFinder.prototype.hilite = false;
jvDeliciousFinder.prototype.retailerindex = null;
jvDeliciousFinder.prototype.discountindex = null;
//jvDeliciousFinder.prototype.description = null;
jvDeliciousFinder.prototype.productindex = null;
jvDeliciousFinder.prototype.productretailerindex = null;
jvDeliciousFinder.prototype.productpriceindex = null;
jvDeliciousFinder.prototype.productsubcategoryindex = null;
jvDeliciousFinder.prototype.discountedpriceindex = null;
jvDeliciousFinder.prototype.whichone = null;
jvDeliciousFinder.prototype.whichretailer = null;
jvDeliciousFinder.prototype.browser = null;
jvDeliciousFinder.prototype.dataBrowser = [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	];
	jvDeliciousFinder.prototype.dataOS = [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	];
	
// START HERE WITH THE MODIFICATIONS
// First, give some inputs to the init function - this way we can differentiate
// between two of these controls on the page?
jvDeliciousFinder.prototype.init = function(whichone,whichRetailer) {
	var start = new Date();
	var msg = "";

	// init vars
	this.browserDetect();
	if (whichone == "coupons") {
		this.tableBody = document.getElementById("ps");
		this.sortOrder = "ascending";
		this.sortCol = "postdate";
		this.sortType = "text";
		this.pageNumber = 1;
		this.recordsPerPage = 15;
		this.xslProc = new jvXslProcessor();
		this.xmlReq = new jvXmlRequest();
		msg += "Init XML objects: " + ((new Date()) - start) + "\n";
		this.retailerindex = 0;
		this.discountindex = 0;
		//this.description = "";
		this.whichone = "coupons";
		this.whichretailer = whichRetailer;
		// get posts
		//	this.allPosts = this.xmlReq.getDocument("/api/posts/all");
		this.allPosts = this.xmlReq.getDocument("../photocoupons.asp"); 
		this.generateXsl('' + this.whichretailer + '|All|All' );
		}
	else
		{
		this.tableBody = document.getElementById("productlist");
		this.sortOrder = "descending";
		this.sortCol = "discountedprice";
		this.sortType = "number";
		this.pageNumber = 1;
		this.recordsPerPage = 20;
		this.productretailerindex = 0;
		this.productpriceindex = 0;
		this.productsubcategoryindex = 0;
		this.discountedpriceindex = 0;
		this.productindex = 0;
		this.xslProc = new jvXslProcessor();
		this.xmlReq = new jvXmlRequest();
		this.whichone = "products";
		this.whichretailer = whichRetailer;
		msg += "Init XML objects: " + ((new Date()) - start) + "\n";
	
		// get posts
		//	this.allPosts = this.xmlReq.getDocument("/api/posts/all");
		this.allPosts = this.xmlReq.getDocument("../photoproducts.asp"); 
		this.generateXsl('' + this.whichretailer + '|All|All|All' );
		}
	msg += "Get XML doc: " + ((new Date()) - start) + "\n";
	
	// get stylesheet
	//this.generateXsl('' + this.whichretailer + '|All|All' );
	
	msg += "Generate XSL: " + ((new Date()) - start) + "\n";
	
	// apply stylesheet to posts
	if (this.whichone == "coupons") {
		this.sort("postdate");
	}
	else {
		this.sort("discountedprice");
	}
	msg += "Apply stylesheet: " + ((new Date()) - start) + "\n";
}


jvDeliciousFinder.prototype.sort = function(column) {
	var samecol;
	
	samecol = 0;
	if (this.sortCol == column) {
	}
	else {
		// If going to a different column, want to always start with descending sort order
		this.sortOrder = "ascending";
	}
	this.sortCol = column;
	this.xslProc.setParameter(null, "sortColumn", column);
	if (this.sortOrder == "descending")	{
		this.xslProc.setParameter(null, "sortOrder", "ascending");
		this.sortOrder = "ascending";
	} else {
		this.xslProc.setParameter(null, "sortOrder", "descending");
		this.sortOrder = "descending";
	}
	// Also need to input the datatype
	if (this.sortCol == "discount" || this.sortCol == "productprice" || this.sortCol == "discountedprice") {
		this.xslProc.setParameter(null, "sortType", "number");
		this.sortType = "number";
	}
	else {
		this.xslProc.setParameter(null, "sortType", "text");
		this.sortType = "text";
	}
	// If we are sorting, we need to go back to the first page.
	this.pageNumber = 1;
	this.xslProc.setParameter(null, "pageNumber", 1);
	this.xslProc.setParameter(null, "recordsPerPage", this.recordsPerPage);
	this.xslProc.setParameter(null, "browserType", this.browser);
	this.xslProc.setParameter(null, "whichRetailer", this.whichretailer);
	// apply transform	
	var fragment = this.xslProc.transformToFragment(this.allPosts, document);
// CHANGE THE BELOW...	
	if(fragment == "") fragment = "<p>Unable to load the listings.  If you continue to see this error, please contact the system administrator.</p>";
	this.tableBody.innerHTML = fragment; 
	if(document.getElementById("c_" + this.sortCol) != null) {
		var sortColName = this.sortCol;
		//alert('This is the sort column name: ' + sortColName);
		//if (sortColName != "productname" | sortColName != "retailername" | sortColName != "productprice") {
			document.getElementById("c_" + sortColName).style.backgroundColor = "#AFC4F4";
		//}
		//if (sortColName == "productname") {
		//	document.getElementById("productname").style.backgroundColor = "#AFC4F4";
		//}
		//if (sortColName == "productprice") {
		//	document.getElementById("productprice").style.backgroundColor = "#AFC4F4";
		//}
		//if (sortColName == "retailername") {
		//	document.getElementById("retailername").style.backgroundColor = "#AFC4F4";
		//}
		if (this.sortOrder == "ascending") {
			document.getElementById("arrow" + this.sortCol).src = "images/up.gif";
		}
		else {
			document.getElementById("arrow" + this.sortCol).src = "images/down.gif";
		}
		document.getElementById("arrow"  + this.sortCol).style.visibility = "visible";
	}
	this.setBackgrounds();

}
	
	
jvDeliciousFinder.prototype.setBackgrounds = function() {
		if (this.whichone == "coupons") {
			document.getElementById("RetailerDisplaySelect").selectedIndex = this.retailerindex;
			document.getElementById("DiscountSelect").selectedIndex = this.discountindex;
			if (this.discountindex != 0) {
				document.getElementById("DiscountSelect").style.background = "#AFC4F4";
			}
			else {
				document.getElementById("DiscountSelect").style.background = "none";
			}
			if (this.retailerindex != 0)
			{
				document.getElementById("RetailerDisplaySelect").style.backgroundColor = "#AFC4F4";
			}
			else {
				document.getElementById("RetailerDisplaySelect").style.backgroundColor = "white";
			}
		}
		else
		{	
			document.getElementById("ProductRetailerSelect").selectedIndex = this.productretailerindex;
			//document.getElementById("ProductPriceSelect").selectedIndex = this.productpriceindex;
			document.getElementById("ProductSelect").selectedIndex = this.productindex;
			document.getElementById("DiscountedPriceSelect").selectedIndex = this.discountedpriceindex;
			document.getElementById("ProductSubcategorySelect").selectedIndex = this.productsubcategoryindex;
			if (this.productretailerindex != 0) {
				document.getElementById("ProductRetailerSelect").style.background = "#AFC4F4";
			}
			else {
				document.getElementById("ProductRetailerSelect").style.background = "none";
			}
			if (this.productindex != 0)
			{
				document.getElementById("ProductSelect").style.backgroundColor = "#AFC4F4";
			}
			else {
				document.getElementById("ProductSelect").style.backgroundColor = "white";
			}
			if (this.productsubcategoryindex != 0)
			{	
				document.getElementById("ProductSubcategorySelect").style.backgroundColor = "#AFC4F4";
			}
			else {
				document.getElementById("ProductSubcategorySelect").style.backgroundColor = "white";
			}
			if (this.discountedpriceindex != 0)
			{	
				document.getElementById("DiscountedPriceSelect").style.backgroundColor = "#AFC4F4";
			}
			else {
				document.getElementById("DiscountedPriceSelect").style.backgroundColor = "white";
			}
		}	

}

	
jvDeliciousFinder.prototype.cT = function(idValue,textValue) {
	var link;	
	link = document.getElementById(idValue);
	link.innerHTML = textValue;
	//link.style.display = 'block';
	//link.style.visibility = 'visible';
	//alert('Calling first fade');
	//Fat.fade_element(idValue, 30, 5000, "#FFFFFF", "#FFFF33");
   	//setTimeout("Fat.fade_element(" + idValue + ", 30, 5000, '#FFFF33', '#FFFFFF')",6000);
	// Set the background color back to normal after all of this...
	//setTimeout("document.getElementById(" + idValue + ").style.backgroundColor = ''",12000); 
	setTimeout("self.focus()",100);

}

jvDeliciousFinder.prototype.resetCriteria = function() {
	var i, val;
	if (this.whichone == "coupons")
	{
		this.retailerindex = 0;
		this.discountindex = 0;
		//this.description = "";
		document.getElementById("RetailerDisplaySelect").selectedIndex = this.retailerindex;
		document.getElementById("DiscountSelect").selectedIndex = this.discountindex;
		//document.getElementById("DescriptionInput").value = this.description;		
	}
	else
	{
		this.productretailerindex = 0;
		this.productpriceindex = 0;
		this.productindex = 0;
		this.productsubcategoryindex = 0;
		this.discountedpriceindex = 0;
		document.getElementById("ProductRetailerSelect").selectedIndex = this.productretailerindex;
		document.getElementById("ProductSelect").selectedIndex = this.productindex;
		document.getElementById("DiscountedPriceSelect").selectedIndex = this.discountedpriceindex;
		document.getElementById("ProductSubcategorySelect").selectedIndex = this.productsubcategoryindex;
	}
	this.filter('--');
}


jvDeliciousFinder.prototype.filter = function(pageValue,lastPage) {
	
	// In our case, we need to construct the filter from scratch.  This means going through each of the input
	// and select boxes and getting the values.  We need to take these values and put them into a string that
	// we can parse later in the generateXsl function.  For now though, just get things to display and we'll worry
	// about the filtering later...
	
	// If we change the filter criteria, we need to go back to the first page from whatever page we are currently viewing.
	// store current query
	var q;
	var catvalue, discountvalue, expvalue, postedvalue, numberperpage, retailervalue, descriptionvalue, productretailer, product, productprice, discountedprice;
	var productsubcategory, productold;
	// For the photo case, only going to allow to filter by retailer, discount, description?
	if (this.whichone == "coupons") {
		retailervalue = document.getElementById("RetailerDisplaySelect").options[document.getElementById("RetailerDisplaySelect").selectedIndex].text;
		discountvalue = document.getElementById("DiscountSelect").options[document.getElementById("DiscountSelect").selectedIndex].text;
		this.retailerindex = document.getElementById("RetailerDisplaySelect").selectedIndex;
		this.discountindex = document.getElementById("DiscountSelect").selectedIndex;
		this.setBackgrounds();	
		// Have to do the same thing with the description filter...
		q = retailervalue;
		q += '|' + discountvalue;
	}
	else
	{
		productold = document.getElementById("ProductSelect").options[this.productindex].text;
		productretailer = document.getElementById("ProductRetailerSelect").options[document.getElementById("ProductRetailerSelect").selectedIndex].text;
		product = document.getElementById("ProductSelect").options[document.getElementById("ProductSelect").selectedIndex].text;
		discountedprice = document.getElementById("DiscountedPriceSelect").options[document.getElementById("DiscountedPriceSelect").selectedIndex].text;
		if (productold == product) {
			productsubcategory = document.getElementById("ProductSubcategorySelect").options[document.getElementById("ProductSubcategorySelect").selectedIndex].text;
		}
		else {
			productsubcategory = "All";
			this.productsubcategoryindex = 0;
			document.getElementById("ProductSubcategorySelect").selectedIndex = 0;
		}
		this.productretailerindex = document.getElementById("ProductRetailerSelect").selectedIndex;
		this.productindex = document.getElementById("ProductSelect").selectedIndex;
		this.discountedpriceindex = document.getElementById("DiscountedPriceSelect").selectedIndex;
		this.productsubcategoryindex = document.getElementById("ProductSubcategorySelect").selectedIndex;
		this.setBackgrounds();	
		// Have to do the same thing with the description filter...
		q = productretailer;
		q += '|' + product;
		q += '|' + discountedprice;
		q += '|' + productsubcategory;
	}
	switch (pageValue) {
		case "+":
			this.pageNumber = this.pageNumber + 1;
			break;
		case "-":
			this.pageNumber = this.pageNumber - 1;
			break;
		case "--":
			this.pageNumber = 1;
			break;
		case "++":
			this.pageNumber = lastPage;
			break;
		case "+2":
			this.pageNumber = this.pageNumber + 2;
			break;
		case "-2":
			this.pageNumber = this.pageNumber - 2;
			break;
		default:
			this.pageNumber = 1;
	}
	this.query = q;
	if(this.query.length != -1) {
		// create new stylesheet based on query

		this.generateXsl(q);
	
		// re-establish stylesheet pararms
		this.xslProc.setParameter(null, "sortOrder", this.sortOrder);
		this.xslProc.setParameter(null, "sortColumn", this.sortCol);
		this.xslProc.setParameter(null, "sortType", this.sortType);
		this.xslProc.setParameter(null, "pageNumber", this.pageNumber);
		this.xslProc.setParameter(null, "recordsPerPage", this.recordsPerPage);
		this.xslProc.setParameter(null, "whichRetailer", this.whichretailer);
	    
		// apply transform
		var fragment = this.xslProc.transformToFragment(this.allPosts, document);
		
		if(fragment == "") fragment = "No coupons found.";
		this.tableBody.innerHTML = fragment;
		
		if(document.getElementById("c_" + this.sortCol) != null) {
			var sortColName = this.sortCol;
			//if (sortColName != "productname" | sortColName != "retailername" | sortColName != "productprice") {
				document.getElementById("c_" + sortColName).style.backgroundColor = "#AFC4F4";
			//}
			//if (sortColName == "productname") {
			//	document.getElementById("productname").style.backgroundColor = "#AFC4F4";
			//}
			//if (sortColName == "productprice") {
			//	document.getElementById("productprice").style.backgroundColor = "#AFC4F4";
			//}
			//if (sortColName == "retailername") {
			//	document.getElementById("retailername").style.backgroundColor = "#AFC4F4";}
		if (this.sortOrder == "ascending") {
			document.getElementById("arrow" + this.sortCol).src = "images/up.gif";
		}
		else {
			document.getElementById("arrow" + this.sortCol).src = "images/down.gif";
		}
		document.getElementById("arrow"  + this.sortCol).style.visibility = "visible";
		}
		this.setBackgrounds();
	}
}



jvDeliciousFinder.prototype.generateXsl = function(query) {
	
	var xslSelect, retailerval, retailer2Val, categoryVal, discountVal, expirationVal, postedVal, expDate, expirationDate, monthVal, dayVal, currentDate, postedDate;
	var terms, descriptionval, productretailer, product, productprice, discountedprice, xslSelectForSubcategory, productsubcategory;
	var xslSelectForCategory;
	terms = new Array();
	if(query.length > 0) {
		// Will really need to split this out.  We are going to assume that the whole query contains each and every filter item, even if it is null or "All".
		// Each filter items will be separated from the other by a "|" character, so this is what we will split on (use javascript split function).
		xslSelect = "";
		terms = query.split("|");
		if (this.whichone == "coupons") {
			retailerval = terms[0];
			//xslSelect += " and (contains(translate(@description,$uc,$lc),''))";
		
			if (retailerval != "All") {
				xslSelect += " and (starts-with(translate(@retailer,$uc,$lc),'" + retailerval.toLowerCase() + "'))";
				//alert('This.whichretailer is: ' + this.whichretailer.toLowerCase());
				//xslSelect += " and (starts-with(translate(@retailer,$uc,$lc),'" + this.whichretailer.toLowerCase() + "'))";
			}
			// Need to go through the other filter items
			discountVal = terms[1];
			if (discountVal != "All") {
				xslSelect += " and (@discount " + discountVal + ")";
			}
		}
		else {
			productretailer = terms[0];
			if (productretailer != "All") {
				xslSelect += " and (contains(translate(@retailername,$uc,$lc),'" + productretailer.toLowerCase() + "'))";
			}
			else {
				xslSelect += " and (contains(translate(@retailername,$uc,$lc),''))";
			}
			product = terms[1];
			if (product != "All") {
				xslSelect += " and (starts-with(translate(@productcategory,$uc,$lc),'" + product.toLowerCase() + "'))";
			}
			discountedprice = terms[2];
			if (discountedprice != "All") {
				discountedprice = discountedprice.replace(/</,"");
				xslSelect += " and (@discountedprice &lt; " + discountedprice + ")";
			}
			productsubcategory = terms[3];
			if (productsubcategory != "All") {
				xslSelect += " and (starts-with(translate(@productsubcategory,$uc,$lc),'" + productsubcategory.toLowerCase() + "'))";
			}
			//xslSelectForSubcategory = "post[(contains(translate(@retailername,$uc,$lc),'" + productretailer.toLowerCase() + "')) and (starts-with(translate(@productcategory,$uc,$lc),'" + product.toLowerCase() + "')) and (count(. | key(\'subcategories-for-product\', @productsubcategory)[1]) = 1)  ]";
			xslSelectForCategory = "post[(contains(translate(@retailername,$uc,$lc),'" + productretailer.toLowerCase() + "')) and (count(. | key(\'products-by-category\',concat(@retailername,\'+\',@productcategory))[1]) = 1)]";
			xslSelectForSubcategory = "post[(contains(translate(@retailername,$uc,$lc),'" + productretailer.toLowerCase() + "')) and (starts-with(translate(@productcategory,$uc,$lc),'" + product.toLowerCase() + "')) and (count(. | key(\'subcategories-for-product\', concat(@retailername,\'+\',concat(@productcategory,\'+\',@productsubcategory)))[1]) = 1)]";

		}
		xslSelect = "post[" + xslSelect.substring(5) + "]";
	} else {
		xslSelect = "post";
	}
	// Also need to worry about the number of records to display.  For this, we need to look at the number of records to display, plus what page to display.
	// This can give us an idea of where we need to start and where we need to end.  Create new parameters?  recordsPerPage, pageNumber, recordCount
    
	// This will be a lot of repetition of code, but will probably be worth it to seperate the coupon xslt from the product xslt...
	var output = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">';
	if (this.whichone == "coupons") 
	{	
		output += '<xsl:param name="sortOrder" />';
		output += '<xsl:param name="sortColumn" />';
		output += '<xsl:param name="sortType" />';
		output += '<xsl:param name="recordsPerPage" />';
		output += '<xsl:param name="pageNumber" />';
		output += '<xsl:param name="recordCount" />';
		output += '<xsl:param name="browserType" />';
		output += '<xsl:param name="whichRetailer" />';
		output += '<xsl:param name="lc" select="\'abcdefghijklmnopqrstuvwxyz\'" />';
		output += '<xsl:param name="uc" select="\'ABCDEFGHIJKLMNOPQRSTUVWXYZ\'" />';
		output += '	<xsl:key name="posts-by-retailer" match="post" use="@retailer" />';
		output += '<xsl:template match="posts">';
		output += '		<a name="coupontable"></a>';
		output += '		<table border="0" class="c">';
		output += '		<thead>';
		output += '		<th class="blue" colspan="3" align="left"> Filter the coupon codes below by retailer and/or discount amount</th></thead>';
		output += '		<tr><td class="c" align="left" width="125"><label>Retailer:</label><br/><select  name="Retailer Display_select" onChange="rd.filter(\'--\')" id="RetailerDisplaySelect" disabled="true">';
		//output += '		<option value="none">All</option>';
		output += '		<xsl:for-each select="post[count(. | key(\'posts-by-retailer\', @retailer)[1]) = 1]">';
		output += '			<xsl:sort select="@retailer" order="ascending" data-type="text" />';
		output += '			<xsl:if test="$whichRetailer = @retailer">';
		output += '				<option value=\'{@retailer}\'><xsl:value-of select="@retailer"/></option>';
		output += '			</xsl:if>';
		output += '		</xsl:for-each>';
		output += '		</select></td>';
		output += '		<td class="c" align="left" width="100"><label>Discount:</label><br/><select name="Discount_select" onChange="rd.filter(\'--\')" id="DiscountSelect"><option VALUE="none">All</option>';
		output += '		<option >>5</option>';
		output += '		<option >>10</option>';
		output += '		<option >>15</option>';
		output += '		<option >>20</option>';
		output += '		<option >>25</option>';
		output += '		<option >>50</option>';
		output += '		<option >>75</option>';
		output += '		</select></td>';
		output += '		<td class="c" align="left" width="550"><br/><input type="button" value="Reset" onclick="rd.resetCriteria();"></input></td>';
		output += '		</tr></table>';
		output += '		<table class="c">';
		output += '		<colgroup><col style="width: 125px" /><col style="width: 90px" /><col style="width: 125px" /><col style="width: 85px" /><col style="width: 85px" /><col style="width: 265px" /></colgroup>';
		//output += '		<colgroup><col style="width: 15%" /><col style="width: 15%" /><col style="width: 15%" /><col style="width: 15%" /><col style="width: 15%" /><col style="width: 25%" /></colgroup>';
		output += '		<thead>';
		output += '		<tr>';
		output += '			<th class="c" id="c_retailer" onclick="rd.sort(\'retailer\')" title="Click to sort by retailer">Retailer  ';
		output += '			<img class="no-border" id="arrowretailer" src="images/up.gif" width="10" height="10" style="visibility:hidden"/>';
		output += '			</th>';
		output += '			<th class="c" id="c_discount" onclick="rd.sort(\'discount\')" title="Click to sort by discount">Discount  ';
		output += '			<img class="no-border" id="arrowdiscount" src="images/up.gif" width="10" height="10" style="visibility:hidden"/>';
		output += '			</th>';
		output += '			<th class="c" id="c_discountcode">Coupon</th>';
		output += '			<th class="c" id="c_expdate" onclick="rd.sort(\'expdate\')" title="Click to sort by expiration date">Expires  '; 
		output += '			<img class="no-border" id="arrowexpdate" src="images/up.gif" width="10" height="10" style="visibility:hidden"/>';
		output += '			</th>';
		output += '			<th class="c" id="c_postdate" onclick="rd.sort(\'postdate\')" title="Click to sort by date posted">Posted  ';
		output += '			<img class="no-border" id="arrowpostdate" src="images/up.gif" width="10" height="10" style="visibility:hidden"/>';
		output += '			</th>';
		output += '			<th class="c" id="c_description">Description</th>';
		output += '		</tr>';
		output += '		</thead>';
		output += '		<tbody>';
		output += '<xsl:for-each select="' + xslSelect + '">';
		output += '		<xsl:sort select="@*[name() = $sortColumn]" order="{$sortOrder}" data-type="{$sortType}" />';
		output += '		<xsl:if test="(position() &gt; ($pageNumber - 1)*$recordsPerPage) and (position() &lt; ($pageNumber*$recordsPerPage)+1)">';
		output += '		<tr onmouseover="hl(this)" onmouseout="nl(this)">';
		output += '			<td class="c">';
		//output += '				<xsl:value-of select="@retailer"/>';
		output += '			<img src="{@retailerimage}" alt="{@retailer}" class="no-border"/>';
		output += '			</td>';
		output += '			<td class="c"><xsl:value-of select="@discountdisp"/></td>';
		output += '		<xsl:if test="$browserType = \'Safari\'" >';
		output += '			<td class="c"><a href="offers.asp" onclick="submitit(\'{@discountlink}\',\'{@retailerlink}\'); return false;"><xsl:value-of select="@discountcode"/></a></td>';
		output += '		</xsl:if>';
		output += '		<xsl:if test="$browserType != \'Safari\'" >';
		output += '			<td class="c" align="center"><a href="offers.asp" class="view" onclick="rd.cT(\'coupon{@id}\',\'{@discountcode}\'); submitit(\'{@discountlink}\',\'{@retailerlink}\'); return false;">Get Code</a><div class="ccode">Code:<br><b><div class="ccode2" id="coupon{@id}"></div></b></br><div class="ccode2">&#160;</div></div></td>';
		output += '		</xsl:if>';
		//output += '			<td class="c"><a href="offers.asp" onclick="submitit(\'{@discountlink}\',\'{@retailerlink}\'); rd.cT(\'coupon{@id}\',\'{@discountcode}\'); return false;" id="coupon{@id}" target="_blank" onmouseout="window.status=\' \'; return true;"><xsl:value-of select="@discountcode"/></a></td>'; 
		output += '			<td class="c"><xsl:value-of select="@expdatedisplay"/></td>';
		output += '			<td class="c"><xsl:value-of select="@postdatedisplay"/></td>';
		output += '			<td class="c"><xsl:value-of select="@description"/></td>';
		output += '		</tr>';
		output += ' 	</xsl:if>';
		output += '</xsl:for-each>';
		output += '		</tbody>';
		output += '	</table>';
		// New paging
		output += '<xsl:for-each select="' + xslSelect + '">';  
		output += '		<xsl:if test="position() = last()">';
		output += '			<xsl:variable name="numpages"><xsl:value-of select="ceiling(last() div $recordsPerPage)"/></xsl:variable>';
		output += '			<xsl:variable name="lastpage"><xsl:value-of select="last()"/></xsl:variable>';
		output += '			<div class="pagenav_box">';
		output += '			<nobr><strong>Page <xsl:value-of select="$pageNumber"/> of <xsl:value-of select="$numpages"/></strong> (Results <xsl:value-of select="($pageNumber - 1)*$recordsPerPage + 1"/> through ';
		output += '			<xsl:choose>';
		output += '				<xsl:when test="$lastpage &lt; ($pageNumber*$recordsPerPage)"><xsl:value-of select="$lastpage"/></xsl:when>';
		output += '				<xsl:when test="$lastpage &gt; ($pageNumber*$recordsPerPage)"><xsl:value-of select="$pageNumber*$recordsPerPage"/></xsl:when>';
		output += '				<xsl:when test="$lastpage = ($pageNumber*$recordsPerPage)"><xsl:value-of select="$lastpage"/></xsl:when>';
		output += '			</xsl:choose>';
		output += '			of <xsl:value-of select="$lastpage"/>)';
		output += '			<xsl:if test="$pageNumber = 1">';
		output += '				<a class="search_pagenav search_pagenav_current" href="#">1</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber = 2">';
		output += '				<a class="search_pagenav_text" href="#coupontable" onclick="rd.filter(\'-\')">Prev</a>';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'-\')">1</a>';
		output += '				<a class="search_pagenav search_pagenav_current" href="#">2</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber = 3">';
		output += '				<a class="search_pagenav_text" href="#coupontable" onclick="rd.filter(\'-\')">Prev</a>';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'-2\')">1</a>';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'-\')">2</a>';
		output += '				<a class="search_pagenav search_pagenav_current" href="#">3</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber &gt; 3">';
		output += '				<a class="search_pagenav_text" href="#coupontable" onclick="rd.filter(\'--\')">First</a>';
		output += '				<a class="search_pagenav_text" href="#coupontable" onclick="rd.filter(\'-\')">Prev</a>';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'-2\')"><xsl:value-of select="$pageNumber - 2"/></a>';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'-\')"><xsl:value-of select="$pageNumber - 1"/></a>';
		output += '				<a class="search_pagenav search_pagenav_current" href="#"><xsl:value-of select="$pageNumber"/></a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber &lt; ($numpages - 2)">';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'+\')"><xsl:value-of select="$pageNumber + 1"/></a>';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'+2\')"><xsl:value-of select="$pageNumber + 2"/></a>';
		output += '				<a class="search_pagenav_text" href="#coupontable" onclick="rd.filter(\'+\')">Next</a>';
		output += '				<a class="search_pagenav_text" href="#coupontable" onclick="rd.filter(\'++\',{$numpages})">Last</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber = ($numpages - 2)">';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'+\')"><xsl:value-of select="$pageNumber + 1"/></a>';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'+2\')"><xsl:value-of select="$pageNumber + 2"/></a>';
		output += '				<a class="search_pagenav_text" href="#coupontable" onclick="rd.filter(\'+\')">Next</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber = ($numpages - 1)">';
		output += '				<a class="search_pagenav" href="#coupontable" onclick="rd.filter(\'+\')"><xsl:value-of select="$pageNumber + 1"/></a>';
		output += '				<a class="search_pagenav_text" href="#coupontable" onclick="rd.filter(\'+\')">Next</a>';
		output += '			</xsl:if>';
		output += '			</nobr>';
		output += '			</div>';
		// Paging
		//output += '<xsl:for-each select="' + xslSelect + '">';
		//output += '		<xsl:if test="position() = last()">';
		//output += '			<xsl:variable name="numpages"><xsl:value-of select="ceiling(last() div $recordsPerPage)"/></xsl:variable>';
		//output += '			<xsl:variable name="lastpage"><xsl:value-of select="last()"/></xsl:variable>';
		//output += '		<xsl:if test="$pageNumber &gt; 1">';
		//output += '			<span class="pageNav" title="Go to the first page"><a href="#coupontable" onclick="rd.filter(\'--\')">&lt;&lt;</a></span><span class="pageNav" title="Go to the previous page"><a href="#coupontable" onclick="rd.filter(\'-\')">&lt;</a></span>';
		//output += '		</xsl:if>';
		//output += '		<xsl:if test="$pageNumber &lt; $numpages">';
		//output += '			<span class="pageNav" title="Go to the next page"><a href="#coupontable" onclick="rd.filter(\'+\')">&gt;</a></span><span class="pageNav" title="Go to the last page"><a href="#coupontable" onclick="rd.filter(\'++\',{$numpages})">&gt;&gt;</a></span>';
		//output += '		</xsl:if>';
		//output += '		<table class="c2"><tr><td class="text"><b><xsl:value-of select="$lastpage" /></b> ';
		//output += '		total items found</td></tr>';
		//output += '		<xsl:if test="$lastpage &gt; 0">';
		//output += '		<tr><td class="text">Items <b><xsl:value-of select="($pageNumber - 1)*$recordsPerPage + 1"/></b> through ';
		//output += '		<xsl:choose>';
		//output += '		<xsl:when test="$lastpage &lt; ($pageNumber*$recordsPerPage)"><b><xsl:value-of select="$lastpage"/></b></xsl:when>';
		//output += '		<xsl:when test="$lastpage &gt; ($pageNumber*$recordsPerPage)"><b><xsl:value-of select="$pageNumber*$recordsPerPage"/></b></xsl:when>';
		//output += '		<xsl:when test="$lastpage = ($pageNumber*$recordsPerPage)"><b><xsl:value-of select="$lastpage"/></b></xsl:when>';
		//output += '		</xsl:choose>';
		//output += '		shown</td></tr>';
		//output += '		<tr><td class="text">Page <b><xsl:value-of select="$pageNumber"/></b> of <b><xsl:value-of select="$numpages"/></b></td></tr>';
		//output += '		</xsl:if>';
		//output += '		</table>';
		// end paging
		output += '</xsl:if>';
		output += '</xsl:for-each>';
		output += '</xsl:template>';
		output += '</xsl:stylesheet>';
	}
	else
	{
		output += '<xsl:param name="sortOrder" />';
		output += '<xsl:param name="sortColumn" />';
		output += '<xsl:param name="sortType" />';
		output += '<xsl:param name="recordsPerPage" />';
		output += '<xsl:param name="pageNumber" />';
		output += '<xsl:param name="recordCount" />';
		output += '<xsl:param name="browserType" />';
		output += '<xsl:param name="whichRetailer" />';
		output += '<xsl:param name="lc" select="\'abcdefghijklmnopqrstuvwxyz\'" />';
		output += '<xsl:param name="uc" select="\'ABCDEFGHIJKLMNOPQRSTUVWXYZ\'" />';
		output += '<xsl:key name="products-by-retailer" match="post" use="@retailername" />';
		//output += '<xsl:key name="products-by-category" match="post" use="@productcategory" />';
		output += '<xsl:key name="products-by-category" match="post" use="concat(@retailername,\'+\',@productcategory)" />';
		//output += '<xsl:key name="subcategories-for-product" match="post" use="@productsubcategory" />';
		//output += '<xsl:key name="subcategories-for-product" match="post" use="concat(@productcategory,\'+\',@productsubcategory)" />';
		output += '<xsl:key name="subcategories-for-product" match="post" use="concat(@retailername,\'+\',concat(@productcategory,\'+\',@productsubcategory))" />';
		output += '<xsl:template match="posts">';
		output += '		<a name="producttable"></a>';
		output += '		<table border="0" class="c">';
		output += '		<thead>';
		output += '		<th class="blue" colspan="4" align="left">Filter the product list by selecting values for the retailer, product and/or product price</th></thead>';
		output += '		<tr><td width="125" class="c"><label>Retailer:</label><select  name="Product Retailer_select" onChange="rdproducts.filter(\'--\')" id="ProductRetailerSelect" disabled="true">';
		output += '		<xsl:for-each select="post[count(. | key(\'products-by-retailer\', @retailername)[1]) = 1]">';
		output += '			<xsl:sort select="@retailername" order="ascending" data-type="text" />';
		output += '				<xsl:if test="$whichRetailer = @retailername">';
		output += '					<option value=\'{@retailername}\'><xsl:value-of select="@retailername"/></option>';
		output += '				</xsl:if>';
		output += '		</xsl:for-each>';
		output += '		</select></td>';
		output += '		<td width="145" class="c"><label>Product:</label><select name="Product_select" onChange="rdproducts.filter(\'--\')" id="ProductSelect"><option VALUE="none">All</option>'; 
		output += '     <xsl:for-each select="' + xslSelectForCategory + '">';
		output += '         <xsl:sort select="@productcategory" order="ascending" data-type="text" />';
		output += '			<option value=\'{@productcategory}\'><xsl:value-of select="@productcategory"/></option>';
		output += '     </xsl:for-each>';
		//output += '		<xsl:for-each select="post[count(. | key(\'products-by-category\', @productcategory)[1]) = 1]">';
		////output += '			<xsl:for-each select="post">';
		//output += '			<xsl:sort select="@productcategory" order="ascending" data-type="text" />';
		//output += '			<xsl:if test="$whichRetailer = @retailername">';
		//output += '			<option value=\'{@productcategory}\'><xsl:value-of select="@productcategory"/></option>';
		//output += '			</xsl:if>';
		//output += '		</xsl:for-each>';
		output += '		</select></td>';
		output += '		<td width="170" class="c"><label>Type:</label><select name="Product_Subcategory_select" onChange="rdproducts.filter(\'--\')" id="ProductSubcategorySelect"><option VALUE="none">All</option>';	
		output += '		<xsl:for-each select="' + xslSelectForSubcategory + '">';		
		output += '				<xsl:sort select="@productsubcategory" order="ascending" data-type="text" />';
		// New
		output += '		<xsl:if test="@productsubcategory != \' \'">';
		output += '				<option value=\'{@productsubcategory}\'><xsl:value-of select="@productsubcategory"/></option>';
		output += '		</xsl:if>';
		output += '		</xsl:for-each>';
		output += '		</select></td>';
		output += '		<td width="100" class="c"><label>Price:</label><select name="DiscountedPrice_select" onChange="rdproducts.filter(\'--\')" id="DiscountedPriceSelect"><option VALUE="none">All</option>';
		output += '		<option >&lt;1</option>';
		output += '		<option >&lt;5</option>';
		output += '		<option >&lt;10</option>';
		output += '		<option >&lt;15</option>';
		output += '		<option >&lt;20</option>';
		output += '		<option >&lt;25</option>';
		output += '		<option >&lt;30</option>';
		output += '		<option >&lt;40</option>';
		output += '		<option >&lt;50</option>';
		output += '		</select></td>';
		output += '		<td width="50" class="c"><br/><input type="button" value="Reset" onclick="rdproducts.resetCriteria();" style="position: relative; top: 10px;"></input></td>';
		output += '		</tr></table>';
		output += '		<table class="c">';
		output += '		<colgroup><col style="width: 125px;" /><col style="width: 300px;" /><col style="width: 115px;" /><col style="width: 115px;" /><col style="width:120px;" /></colgroup>';
		output += '		<thead>';
		output += '		<tr>';
		// Modify the columns displayed.  Columns should be: retailer, category, discountdisp, discountcode, expdatedisplay, postdatedisplay, description, email.
		// Note that some of these will eventually be links and not what is currently displayed (that's for later).
		output += '			<th class="c" id="c_retailername" onclick="rdproducts.sort(\'retailername\')" title="Click to sort by retailer">Retailer  ';
		output += '			<img class="no-border" id="arrowretailername" src="images/up.gif" width="10" height="10" style="visibility:hidden"/>';
		output += '			</th>';
		output += '			<th class="c" id="c_productname" onclick="rdproducts.sort(\'productname\')" title="Click to sort by product">Product  '; 
		output += '			<img class="no-border" id="arrowproductname" src="images/up.gif" width="10" height="10" style="visibility:hidden"/>';
		output += '			</th>';
		output += '			<th class="c" id="c_productprice" onclick="rdproducts.sort(\'productprice\')" title="Click to sort by price">List Price  '; 
		output += '			<img class="no-border" id="arrowproductprice" src="images/up.gif" width="10" height="10" style="visibility:hidden"/>';
		output += '			</th>';
		output += '			<th class="c" id="c_discountedprice" onclick="rdproducts.sort(\'discountedprice\')" title="Click to sort by discounted price">Your Price  '; 
		output += '			<img class="no-border" id="arrowdiscountedprice" src="images/up.gif" width="10" height="10" style="visibility:hidden"/>';
		output += '			</th>';
		output += '			<th class="c" id="c_view">View</th>';
		output += '		</tr>';
		output += '		</thead>';
		output += '		<tbody>';
		output += '<xsl:for-each select="' + xslSelect + '">';
		output += '		<xsl:sort select="@*[name() = $sortColumn]" order="{$sortOrder}" data-type="{$sortType}" />';
		// Need to make sure that we only display the "right" items (number per page, records per page etc.)  This matches up with the /xsl:if below
		output += '		<xsl:if test="(position() &gt; ($pageNumber - 1)*$recordsPerPage) and (position() &lt; ($pageNumber*$recordsPerPage)+1)">';
		output += '		<tr onmouseover="hl(this)" onmouseout="nl(this)">';
		// Modify the columns displayed.
		output += '			<td class="c">';
		//output += '				<a href="#producttable" onclick="submitit(\'{@productlink}\',\'{@retailerdefaultlink}\'); return false;" target="_blank"><img src="{@retailerimage}" alt="{@retailername}" class="no-border"/></a>';
		output += '			<img src="{@retailerimage}" alt="{@retailername}" class="no-border"/>';			
		output += '			</td>';
//		output += '			<td class="c"><table><tr valign="top"><td><img src="{@productimage}" alt="{@productname}" id="imageleft" /></td><td><b><xsl:value-of select="@productname"/></b><br><xsl:value-of select="@productdescription"/></br></td></tr></table></td>';
//		output += '			<td class="c"><table><colgroup><col style="width: 110px;" /><col style="width: 190px;"/></colgroup><tr valign="top"><td><img src="{@productimage}" alt="{@productname}" id="imageleft" /></td><td><b><xsl:value-of select="@productname"/></b><br><xsl:value-of select="@productdescription"/></br></td></tr></table></td>';
        output += '     <xsl:if test="$browserType = \'Safari\'" >';
		output += '			<td class="c"><table><colgroup><col style="width: 115px;" /><col style="width: 185px;"/></colgroup><tr valign="top"><td><img src="{@productimage}" alt="{@productname}" id="imageleft" /></td><td><b><xsl:value-of select="@productname"/></b><br></br><xsl:value-of select="@productdescription"/></td></tr></table></td>';
		output += '     </xsl:if>';
		output += '     <xsl:if test="$browserType != \'Safari\'" >';
		output += '			<td class="c"><table><colgroup width="115px"></colgroup><colgroup width="185px"></colgroup><tr valign="top"><td><img src="{@productimage}" alt="{@productname}" id="imageleft" /></td><td><b><xsl:value-of select="@productname"/></b><br><xsl:value-of select="@productdescription"/></br></td></tr></table></td>';
		output += '     </xsl:if>';
		output += '			<td class="c"><xsl:value-of select="@productpricedisplay"/></td>';
		output += '			<td class="c">$<xsl:value-of select="@discountedprice"/><xsl:if test="@discount &gt; 0"><br/><span class="green">Save $<xsl:value-of select="round((@productprice - @discountedprice)*100) div 100"/> with <xsl:value-of select="@discountdisplay"/> off coupon</span></xsl:if><xsl:if test="@couponnewcustomer = \'Y\'"><br/><span class="green">(New customers)</span></xsl:if></td>';
		output += '		<xsl:if test="$browserType = \'Safari\'" >';
		output += '			<td class="c" align="center"><xsl:choose><xsl:when test="@discount &gt; 0"><a href="offers.asp" class="view" onclick="submitit(\'{@productlink}\',\'{@retailerdefaultlink}\'); return false;">View</a><div class="ccode">Coupon Code:<br></br><b><div id="product{@productid}"><xsl:value-of select="@couponcode"/></div></b></div></xsl:when><xsl:otherwise><a href="offers.asp" onclick="submitit(\'{@productlink}\',\'{@retailerdefaultlink}\'); return false;" class="view" target="_new">View</a></xsl:otherwise></xsl:choose></td>';
		output += '		</xsl:if>';
		output += '		<xsl:if test="$browserType != \'Safari\'" >';
		output += '			<td class="c" align="center"><xsl:choose><xsl:when test="@discount &gt; 0"><a href="offers.asp" class="view" onclick="submitit(\'{@productlink}\',\'{@retailerdefaultlink}\'); rdproducts.cT(\'product{@productid}\',\'{@couponcode}\'); return false;">View</a><div class="ccode">Coupon Code:<br><b><span id="product{@productid}"></span></b></br></div></xsl:when><xsl:otherwise><a href="offers.asp" onclick="submitit(\'{@productlink}\',\'{@retailerdefaultlink}\'); return false;" class="view" target="_new">View</a></xsl:otherwise></xsl:choose></td>';
		output += '		</xsl:if>';
		output += '		</tr>';
		output += ' 	</xsl:if>';
		output += '</xsl:for-each>';
		output += '		</tbody>';
		output += '	</table>';
		// New paging
		output += '<xsl:for-each select="' + xslSelect + '">';
		output += '		<xsl:if test="position() = last()">';
		output += '			<xsl:variable name="numpages"><xsl:value-of select="ceiling(last() div $recordsPerPage)"/></xsl:variable>';
		output += '			<xsl:variable name="lastpage"><xsl:value-of select="last()"/></xsl:variable>';
		output += '			<div class="pagenav_box">';
		output += '			<nobr><strong>Page <xsl:value-of select="$pageNumber"/> of <xsl:value-of select="$numpages"/></strong> (Results <xsl:value-of select="($pageNumber - 1)*$recordsPerPage + 1"/> through ';
		output += '			<xsl:choose>';
		output += '				<xsl:when test="$lastpage &lt; ($pageNumber*$recordsPerPage)"><xsl:value-of select="$lastpage"/></xsl:when>';
		output += '				<xsl:when test="$lastpage &gt; ($pageNumber*$recordsPerPage)"><xsl:value-of select="$pageNumber*$recordsPerPage"/></xsl:when>';
		output += '				<xsl:when test="$lastpage = ($pageNumber*$recordsPerPage)"><xsl:value-of select="$lastpage"/></xsl:when>';
		output += '			</xsl:choose>';
		output += '			of <xsl:value-of select="$lastpage"/>)';
		output += '			<xsl:if test="$pageNumber = 1">';
		output += '				<a class="search_pagenav search_pagenav_current" href="#">1</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber = 2">';
		output += '				<a class="search_pagenav_text" href="#producttable" onclick="rdproducts.filter(\'-\')">Prev</a>';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'-\')">1</a>';
		output += '				<a class="search_pagenav search_pagenav_current" href="#">2</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber = 3">';
		output += '				<a class="search_pagenav_text" href="#producttable" onclick="rdproducts.filter(\'-\')">Prev</a>';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'-2\')">1</a>';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'-\')">2</a>';
		output += '				<a class="search_pagenav search_pagenav_current" href="#">3</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber &gt; 3">';
		output += '				<a class="search_pagenav_text" href="#producttable" onclick="rdproducts.filter(\'--\')">First</a>';
		output += '				<a class="search_pagenav_text" href="#producttable" onclick="rdproducts.filter(\'-\')">Prev</a>';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'-2\')"><xsl:value-of select="$pageNumber - 2"/></a>';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'-\')"><xsl:value-of select="$pageNumber - 1"/></a>';
		output += '				<a class="search_pagenav search_pagenav_current" href="#"><xsl:value-of select="$pageNumber"/></a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber &lt; ($numpages - 2)">';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'+\')"><xsl:value-of select="$pageNumber + 1"/></a>';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'+2\')"><xsl:value-of select="$pageNumber + 2"/></a>';
		output += '				<a class="search_pagenav_text" href="#producttable" onclick="rdproducts.filter(\'+\')">Next</a>';
		output += '				<a class="search_pagenav_text" href="#producttable" onclick="rdproducts.filter(\'++\',{$numpages})">Last</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber = ($numpages - 2)">';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'+\')"><xsl:value-of select="$pageNumber + 1"/></a>';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'+2\')"><xsl:value-of select="$pageNumber + 2"/></a>';
		output += '				<a class="search_pagenav_text" href="#producttable" onclick="rdproducts.filter(\'+\')">Next</a>';
		output += '			</xsl:if>';
		output += '			<xsl:if test="$pageNumber = ($numpages - 1)">';
		output += '				<a class="search_pagenav" href="#producttable" onclick="rdproducts.filter(\'+\')"><xsl:value-of select="$pageNumber + 1"/></a>';
		output += '				<a class="search_pagenav_text" href="#producttable" onclick="rdproducts.filter(\'+\')">Next</a>';
		output += '			</xsl:if>';
		output += '			</nobr>';
		output += '			</div>';
		// Paging
		//output += '<xsl:for-each select="' + xslSelect + '">';
		//output += '		<xsl:if test="position() = last()">';
		//output += '			<xsl:variable name="numpages"><xsl:value-of select="ceiling(last() div $recordsPerPage)"/></xsl:variable>';
		//output += '			<xsl:variable name="lastpage"><xsl:value-of select="last()"/></xsl:variable>';
		//output += '		<xsl:if test="$pageNumber &gt; 1">';
		//output += '			<span class="pageNav" title="Go to the first page"><a href="#producttable" onclick="rdproducts.filter(\'--\')">&lt;&lt;</a></span><span class="pageNav" title="Go to the previous page"><a href="#producttable" onclick="rdproducts.filter(\'-\')">&lt;</a></span>';
		//output += '		</xsl:if>';
		//output += '		<xsl:if test="$pageNumber &lt; $numpages">';
		//output += '			<span class="pageNav" title="Go to the next page"><a href="#producttable" onclick="rdproducts.filter(\'+\')">&gt;</a></span><span class="pageNav" title="Go to the last page"><a href="#producttable" onclick="rdproducts.filter(\'++\',{$numpages})">&gt;&gt;</a></span>';
		//output += '		</xsl:if>';
		//output += '		<table class="c2"><tr><td class="text"><b><xsl:value-of select="$lastpage" /></b> ';
		//output += '		total items found</td></tr>';
		//output += '		<xsl:if test="$lastpage &gt; 0">';
		//output += '		<tr><td class="text">Items <b><xsl:value-of select="($pageNumber - 1)*$recordsPerPage + 1"/></b> through ';
		//output += '		<xsl:choose>';
		//output += '		<xsl:when test="$lastpage &lt; ($pageNumber*$recordsPerPage)"><b><xsl:value-of select="$lastpage"/></b></xsl:when>';
		//output += '		<xsl:when test="$lastpage &gt; ($pageNumber*$recordsPerPage)"><b><xsl:value-of select="$pageNumber*$recordsPerPage"/></b></xsl:when>';
		//output += '		<xsl:when test="$lastpage = ($pageNumber*$recordsPerPage)"><b><xsl:value-of select="$lastpage"/></b></xsl:when>';
		//output += '		</xsl:choose>';
		//output += '		shown</td></tr>';
		//output += '		<tr><td class="text">Page <b><xsl:value-of select="$pageNumber"/></b> of <b><xsl:value-of select="$numpages"/></b></td></tr>';
		//output += '		</xsl:if>';
		//output += '		</table>';
		// end paging
		output += '</xsl:if>';
		output += '</xsl:for-each>';
		output += '</xsl:template>';
		output += '</xsl:stylesheet>';
	}
	this.xslProc.importStylesheet(output);

}

jvDeliciousFinder.prototype.th = function() {
	if(this.hilite) this.hilite = false;
	else this.hilite = true;
}

jvDeliciousFinder.prototype.browserDetect = function() {
	    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
}

jvDeliciousFinder.prototype.searchString = function(data) {
		for (var i=0; i < data.length; i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
}

jvDeliciousFinder.prototype.searchVersion = function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
}


/*************************************************************************
 * View controller stuff, that really should go somewhere else
 ************************************************************************/

/* the mouseover highlighting */
function hl(what) {	what.style.background = "#eee"; }
function nl(what) {	what.style.background = ""; }


var rd = new jvDeliciousFinder();
var rdproducts = new jvDeliciousFinder();

function start() {
	// Have two different recordsets: 1) one for the coupons and 2) one for the products.
	rd.init("coupons",varRetailer);
	// For products, we will allow them to filter by retailer or product.
	rdproducts.init("products",varRetailer);
}

window.onload = start;

