/*************************************************************************
 * 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.retailerindex = null;
jvDeliciousFinder.prototype.discountindex = null;
jvDeliciousFinder.prototype.categoryindex = null;
jvDeliciousFinder.prototype.expdateindex = null;
jvDeliciousFinder.prototype.postdateindex = null;
jvDeliciousFinder.prototype.retailertext = null;
jvDeliciousFinder.prototype.numrecordsindex = null;
jvDeliciousFinder.prototype.query = null;
jvDeliciousFinder.prototype.xslProc = null;
jvDeliciousFinder.prototype.xmlReq = null;
jvDeliciousFinder.prototype.allPosts = null;
jvDeliciousFinder.prototype.hilite = false;
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
	
jvDeliciousFinder.prototype.init = function() {
	var start = new Date();
	var msg = "";

	// init vars
	this.browserDetect();
	this.tableBody = document.getElementById("ps");
	this.sortOrder = "ascending";
	this.sortCol = "postdate";
	this.sortType = "text";
	this.pageNumber = 1;
	this.recordsPerPage = 30;
	this.retailertext = retailerval;
	this.categoryindex = 0;
	if (categoryval == "Apparel")
	{   this.categoryindex = 1; }
	if (categoryval == "Auctions")
	{   this.categoryindex = 2; }
	if (categoryval == "Automotive")
	{   this.categoryindex = 3; }
	if (categoryval == "Baby and Children")
	{   this.categoryindex = 4; }
	if (categoryval == "Books")
	{   this.categoryindex = 5; }
	if (categoryval == "Computers")
	{   this.categoryindex = 6; }
	if (categoryval == "Electronics")
	{   this.categoryindex = 7; }
	if (categoryval == "Flowers and Gifts")
	{   this.categoryindex = 8; }
	if (categoryval == "Food and Cooking")
	{   this.categoryindex = 9; }
	if (categoryval == "Health and Beauty")
	{   this.categoryindex = 10; }
	if (categoryval == "Home and Garden")
	{   this.categoryindex = 11; }
	if (categoryval == "Jewelry")
	{   this.categoryindex = 12; }
	if (categoryval == "Magazines")
	{   this.categoryindex = 13; }
	if (categoryval == "Movies")
	{   this.categoryindex = 14; }
	if (categoryval == "Music")
	{   this.categoryindex = 15; }
	if (categoryval == "Office")
	{   this.categoryindex = 16; }
	if (categoryval == "Pets")
	{   this.categoryindex = 17; }
	if (categoryval == "Photo")
	{   this.categoryindex = 18; }
	if (categoryval == "Sports")
	{   this.categoryindex = 19; }
	if (categoryval == "Toys")
	{   this.categoryindex = 20; }
	if (categoryval == "Travel")
	{   this.categoryindex = 21; }
	this.retailerindex = 0;
    this.discountindex = 0;
	//this.categoryindex = 0;
    this.expdateindex = 0;
    this.postdateindex = 0;
    this.numrecordsindex = 3;
	this.xslProc = new jvXslProcessor();
	this.xmlReq = new jvXmlRequest();
	msg += "Init XML objects: " + ((new Date()) - start) + "\n";
	
	// get posts
//	this.allPosts = this.xmlReq.getDocument("/api/posts/all");
	this.allPosts = this.xmlReq.getDocument("TestListnew.asp");
	msg += "Get XML doc: " + ((new Date()) - start) + "\n";
	
	// get stylesheet
	this.generateXsl("");
	msg += "Generate XSL: " + ((new Date()) - start) + "\n";
	
	// apply stylesheet to posts
	this.sort("postdate");
	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.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);
	// apply transform	
	var fragment = this.xslProc.transformToFragment(this.allPosts, document);
// CHANGE THE BELOW...	
	if(fragment == "") fragment = "<p>Unable to load the coupon listing.  If you continue to see this error, please contact the system administrator.</p>";
	// hilite terms on offline node
	//if(this.hilite) this.tableBody.innerHTML = hlTerms(fragment, this.query, false);
	//else this.tableBody.innerHTML = fragment;
	this.tableBody.innerHTML = fragment; 
	if(document.getElementById("c_" + this.sortCol) != null) {
		document.getElementById("c_" + this.sortCol).style.background = "#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.cT = function(idValue,textValue,linkval,defaultlink) {
	var link;	
	var date = new Date();
    var curDate = null;
    
	link = document.getElementById(idValue);
	link.innerHTML = textValue;
	link.style.visibility = "visible";
    document.getElementById("dlink").value = linkval;
	document.getElementById("address").value = defaultlink; 
	//this.submitall();
	do { curDate = new Date(); } 
    while(curDate-date < 10);
    this.submitall();
	setTimeout("self.focus()",50);
	//setTimeout("this.submitall()",10);
}

jvDeliciousFinder.prototype.submitall = function() {
    document.redir.submit();
}



jvDeliciousFinder.prototype.resetCriteria = function() {
	var i, val;
	this.retailertext = "";
	this.retailerindex = 0;
    this.discountindex = 0;
	this.categoryindex = 0;
    this.expdateindex = 0;
    this.postdateindex = 0;
    document.getElementById("RetailerDisplayInput").value = this.retailertext;
    document.getElementById("RetailerDisplaySelect").selectedIndex = this.retailerindex;
    document.getElementById("CategorySelect").selectedIndex = this.categoryindex;
	document.getElementById("DiscountSelect").selectedIndex = this.discountindex;
	document.getElementById("ExpirationDateSelect").selectedIndex = this.expdateindex;
	document.getElementById("PostedDateSelect").selectedIndex = this.postdateindex;
	//document.getElementById("NumRecordsSelect").selectedIndex = this.numrecordsindex;
	//form1.RetailerDisplayInput.value = '';
	this.filter('--');
}


jvDeliciousFinder.prototype.filter = function(pageValue,lastPage) {
	
	// If we change the filter criteria, we need to go back to the first page from whatever page we are currently viewing.
	//alert('PageValue is: ' + pageValue);
	// store current query
	var q;
	var catvalue, discountvalue, expvalue, postedvalue, numberperpage;
	var retailertextvalue;
	catvalue = document.getElementById("CategorySelect").options[document.getElementById("CategorySelect").selectedIndex].text;
	discountvalue = document.getElementById("DiscountSelect").options[document.getElementById("DiscountSelect").selectedIndex].text;
	expvalue = document.getElementById("ExpirationDateSelect").value;
	postedvalue = document.getElementById("PostedDateSelect").value;
	numberperpage = document.getElementById("NumRecordsSelect").value;
	retailervalue = document.getElementById("RetailerDisplaySelect").options[document.getElementById("RetailerDisplaySelect").selectedIndex].text;
    discountvalue = document.getElementById("DiscountSelect").options[document.getElementById("DiscountSelect").selectedIndex].text;
    retailertextvalue = document.getElementById("RetailerDisplayInput").value;
    this.retailertext = retailertextvalue;
    this.retailerindex = document.getElementById("RetailerDisplaySelect").selectedIndex;
	this.discountindex = document.getElementById("DiscountSelect").selectedIndex;
    this.categoryindex = document.getElementById("CategorySelect").selectedIndex;
	this.expdateindex =  document.getElementById("ExpirationDateSelect").selectedIndex;
	this.postdateindex = document.getElementById("PostedDateSelect").selectedIndex;
	this.numrecordsindex = document.getElementById("NumRecordsSelect").selectedIndex;
	this.recordsPerPage = numberperpage;
	this.setBackgrounds();
	q = document.getElementById("RetailerDisplayInput").value;
	q += '|' + document.getElementById("RetailerDisplaySelect").value;
	//q += '|' + 'none';
	q += '|' + catvalue;
	q += '|' + discountvalue;
	q += '|' + expvalue;
	q += '|' + postedvalue;
	q += '|' + document.getElementById("NumRecordsSelect").value;
	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);
		// If we are filtering, we need to go back to the first page, which is taken care of in the switch statement above
		//alert('The page number is: ' + this.pageNumber);
		this.xslProc.setParameter(null, "pageNumber", this.pageNumber);
		this.xslProc.setParameter(null, "recordsPerPage", this.recordsPerPage);
		// apply transform
		var fragment = this.xslProc.transformToFragment(this.allPosts, document);
		if(fragment == "") fragment = "No bookmarks found.";
		//if(this.hilite) this.tableBody.innerHTML = hlTerms(fragment, this.query, false);
		//else this.tableBody.innerHTML = fragment;
		this.tableBody.innerHTML = fragment;
		if(document.getElementById("c_" + this.sortCol) != null) {
			document.getElementById("c_" + this.sortCol).style.background = "#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() {
    document.getElementById("RetailerDisplayInput").value = this.retailertext;
    document.getElementById("RetailerDisplaySelect").selectedIndex = this.retailerindex;
	document.getElementById("DiscountSelect").selectedIndex = this.discountindex;
    document.getElementById("CategorySelect").selectedIndex = this.categoryindex;
	document.getElementById("ExpirationDateSelect").selectedIndex = this.expdateindex;
	document.getElementById("PostedDateSelect").selectedIndex = this.postdateindex;
	document.getElementById("NumRecordsSelect").selectedIndex = this.numrecordsindex;
	if (this.retailertext != "")
	{
	    document.getElementById("RetailerDisplayInput").style.backgroundColor = "#AFC4F4";
	}
	else {
	    document.getElementById("RetailerDisplayInput").style.backgroundColor = "white";
	}
	if (this.retailerindex != 0)
	{
        document.getElementById("RetailerDisplaySelect").style.backgroundColor = "#AFC4F4";
	}
	else {
	    document.getElementById("RetailerDisplaySelect").style.backgroundColor = "white";
    }
	if (this.categoryindex != 0) {
		document.getElementById("CategorySelect").style.background = "#AFC4F4";
	}
	else {
		document.getElementById("CategorySelect").style.background = "none";
	}
	if (this.discountindex != 0) {
		document.getElementById("DiscountSelect").style.background = "#AFC4F4";
	}
	else {
		document.getElementById("DiscountSelect").style.background = "none";
	}
	if (this.expdateindex != 0) {
		document.getElementById("ExpirationDateSelect").style.background = "#AFC4F4";
	}
	else {
		document.getElementById("ExpirationDateSelect").style.background = "none";
	}
	if (this.postdateindex != 0) {
		document.getElementById("PostedDateSelect").style.background = "#AFC4F4";
	}
	else {
		document.getElementById("PostedDateSelect").style.background = "none";
	}
	if (document.getElementById("RetailerDisplayInput").value != "")
	{
		document.getElementById("RetailerDisplayInput").style.backgroundColor = "#AFC4F4";
		document.cf.RetailerDisplayInput.focus();
	}
	else {
		document.getElementById("RetailerDisplayInput").style.backgroundColor = "white";
	}
	//document.cf.RetailerDisplayInput.select();
	//setTimeout("document.cf.RetailerDisplayInput.focus()",.001);
	
}


jvDeliciousFinder.prototype.generateXsl = function(query) {
	
	var xslSelect, retailerval, retailer2Val, categoryVal, discountVal, expirationVal, postedVal, expDate, expirationDate, monthVal, dayVal, currentDate, postedDate;
	var terms;
	terms = new Array();
	//alert('This is the query: ' + query);
	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).
		// Replace bad characters with correct representation
		terms = query.split("|");
		retailerval = terms[0];
		retailerval = retailerval.replace(/&/g,"&amp;");  
		retailerval = retailerval.replace(/</g,"&lt;");
		retailerval = retailerval.replace(/>/g,"&gt;");
		retailerval = retailerval.replace(/"/g,"&quot;");
		//terms = terms.replace(/'/g,"&apos;");
		//terms = query.split(" ");
		xslSelect = "";
		// As long as the query is the empty string, we don't have to worry about the filtering going on below.  Translate is a function in XSLT and can be
		// found on the web.  More than likely, we go through all filters and get their values each time so the below is always the same... and not as difficult
		// as below.  Try out part of the below in filtering the input tag for the retailer?  Filtering would be on: retailer input, retailer select, category select,
		// discount amount, exp date, posted date, number of coupons... Can we try to use the same type of contains function for retailer and category?  Obviously, the discount
		// amount, exp date, posted date would have to be done elsewhere in the code (or not?)
		//for(var i=0; i < terms.length; i++) {
		// Still having issues with the single quote... work on later?
		xslSelect += " and (starts-with(translate(@retailer,$uc,$lc),'" + retailerval.toLowerCase() + "'))";
		retailer2Val = terms[1];
		if (retailer2Val != "none") {
			xslSelect += " and (starts-with(@retailer,'" + retailer2Val + "'))";
		}
		categoryVal = terms[2];
		if (categoryVal != "All") {
			xslSelect += " and (contains(@category,'" + categoryVal + "'))";
		}
		discountVal = terms[3];
		if (discountVal != "All") {
			xslSelect += " and (@discount " + discountVal + ")";
		}
		expirationVal = terms[4];
		if (expirationVal != "none") {
			expDate = new Date();
			expDate.setDate(expDate.getDate()+parseInt(expirationVal));
			monthVal = expDate.getMonth();
			monthVal += 1;
			dayVal = expDate.getDate();
			if (dayVal < 10) {
				dayVal = '0' + dayVal.toString();
			}
			else {
				dayVal = dayVal.toString();
			}
			if (monthVal < 10) {
				monthVal = '0' + monthVal.toString();
			}
			else
			{
				monthVal.toString();
			}
			expirationDate = '' + expDate.getYear() + '' + monthVal + '' + dayVal;
			xslSelect += " and (" + expirationDate + "&gt; @expdate)";
		}
		postedVal = terms[5];
		if (postedVal != "none") {
			currentDate = new Date();
			currentDate.setDate(currentDate.getDate()-parseInt(postedVal));
			monthVal = currentDate.getMonth();
			monthVal += 1;
			dayVal = currentDate.getDate();
			if (dayVal < 10) {
				dayVal = '0' + dayVal.toString();
			}
			else {
				dayVal = dayVal.toString();
			}
			if (monthVal < 10) {
				monthVal = '0' + monthVal.toString();
			}
			else
			{
				monthVal.toString();
			}
			postedDate = '' + currentDate.getYear() + '' + monthVal + '' + dayVal;
			xslSelect += " and (@postdate &gt;" + postedDate + ")";
		}
		xslSelect = "post[" + xslSelect.substring(5) + "]";
		//alert('This is now xslSelect: ' + xslSelect);
	} else {
		xslSelect = "post";
	} 
	var output = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">';
	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="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="cform">';
		output += '		<thead>';
		output += '		<th class="blue" colspan="3" align="left">Search Coupons</th></thead>';
		output += '		<tr><td class="c" align="left" width="195"><label>Retailer:</label><br/>';
		output += '     <input id="RetailerDisplayInput" size="27" onfocus="SetEnd(this);" name="RetailerDisplay_Input" type="text" onKeyPress="return checkEnter(event);" onKeyUp="rd.filter(\'--\');"></input><br/>';
		output += '     <select  name="Retailer Display_select" onChange="rd.filter(\'--\')" id="RetailerDisplaySelect">';
		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 += '			<option value=\'{@retailer}\'><xsl:value-of select="@retailer"/></option>';
		output += '		</xsl:for-each>';
		output += '		</select></td>';
		output += '     <td class="c" align="left" width="140"><label>Category:</label><br/><select  name="Category_select" onChange="rd.filter(\'--\')" id="CategorySelect">';
		output += '		<option value="none">All</option>';
		output += '		<option value="=">Apparel</option>';
		output += '		<option value="=">Auctions</option>';
		output += '		<option value="=">Automotive</option>';
		output += '		<option value="=">Baby and Children</option>';
		output += '		<option value="=">Books</option>';
		output += '		<option value="=">Computers</option>';
		output += '		<option value="=">Electronics</option>';
		output += '		<option value="=">Flowers and Gifts</option>';
		output += '		<option value="=">Food and Cooking</option>';
		output += '		<option value="=">Health and Beauty</option>';
		output += '		<option value="=">Home and Garden</option>';
		output += '		<option value="=">Jewelry</option>';
		output += '		<option value="=">Magazines</option>';
		output += '		<option value="=">Movies</option>';
		output += '		<option value="=">Music</option>';
		output += '		<option value="=">Office</option>';
		output += '		<option value="=">Pets</option>';
		output += '		<option value="=">Photo</option>';
		output += '		<option value="=">Sports</option>';
		output += '		<option value="=">Toys</option>';
		output += '		<option value="=">Travel</option>';
		output += '		</select></td>';
		output += '		<td class="c" align="left" width="60"><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="125"><label>Expiration Date:</label><br/><select  name="Expiration Date_select" onChange="rd.filter(\'--\')" id="ExpirationDateSelect">';
		output += '		<option value="none">All</option>';
		output += '		<option value="1">Expiring Today!</option>';
		output += '		<option value="3">Within 3 days</option>';
		output += '		<option value="7">Within 7 days</option>';
		output += '		<option value="14">Within 14 days</option>';
		output += '		<option value="30">Within 30 days</option>';
		output += '     </select></td>';
		output += '     <td class="c" align="left" width="105"><label>Date Posted:</label><br/><select  name="Posted Date_select" onChange="rd.filter(\'--\')" id="PostedDateSelect">';
		output += '		<option value="none">All</option>';
		output += '		<option value="1">New Today</option>';
		output += '		<option value="3">Last 3 days</option>';
		output += '		<option value="5">Last 5 days</option>';
		output += '		<option value="7">Last 7 days</option>';
		output += '		<option value="14">Last 14 days</option>';
		output += '		<option value="30">Last 30 days</option>';
		output += '     </select></td>';
		output += '     <td class="c" align="left" width="130"><label>Coupons per page:</label><br/><select  name="Num_Records_select" onChange="rd.filter(\'--\')" id="NumRecordsSelect">';
		output += '		<option value="10">10</option>';
		output += '		<option value="15">15</option>';
		output += '		<option value="20">20</option>';
		output += '		<option value="30">30</option>';
		output += '		<option value="40">40</option>';
		output += '		<option value="50">50</option>';
		output += '     </select></td></tr>';
		output += '		<tr><td class="c" align="left" width="132"><input type="button" value="Reset Criteria" onclick="rd.resetCriteria();"></input></td>';
		output += '		</tr></table>';
	output += '		<table class="c">';
	output += '		<colgroup><col style="width: 15%" /><col style="width: 15%" /><col style="width: 10%" /><col style="width: 15%" /><col style="width: 10%" /><col style="width: 10%" /><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_category">Category</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 Code</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}" />';
	// 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 += '				<xsl:value-of select="@retailer"/>';
	output += '			</td>';
	output += '			<td class="c">';
	output += '				<xsl:value-of select="@category"/>';
	output += '			</td>';
	output += '			<td class="c"><xsl:value-of select="@discountdisp"/></td>';
    	output += '		<xsl:if test="$browserType = \'Safari\'" >';
		output += '			<td class="c"><a href="offers2.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="cspec"><a href="offers2.asp" class="view" onclick="rd.cT(\'coupon{@id}\',\'{@discountcode}\',\'{@discountlink}\',\'{@retailerlink}\'); return false;">Get Code</a><div class="ccode">Code:<br><b><div class="ccode2" id="coupon{@id}" onmouseover="this.className=\'ccode3\';" onmouseout="this.className=\'ccode2\';" onclick="window.clipboardData.setData(\'text\',\'{@discountcode}\');" title="Click to copy code" style="visibility:hidden"></div></b></br><div class="ccode2" onmouseover="document.body.style.cursor=\'default\';">&#160;</div></div></td>';
		output += '		</xsl:if>';
	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>';
	// End paging
	output += '</xsl:if>';
	output += '</xsl:for-each>';
	output += '</xsl:template>';
	output += '</xsl:stylesheet>';
	
	this.xslProc.importStylesheet(output);

}

// END OF STUFF THAT WE NEED TO CHANGE (INITIALLY)

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));
}

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

/*************************************************************************
 * ...and finally, page initialization stuff that has been stuffed to the
 * bottom of this page.
 ************************************************************************/

var rd = new jvDeliciousFinder();

function start() {
	rd.init();
    rd.filter();
    //document.cf.RetailerDisplayInput.focus();
	//paintBrowser(null, 1);
	// Uncomment below later....
	//document.getElementById("q").focus();
	//document.getElementById("h").checked = rd.hilite;
}

window.onload = start;
