var baynoteDefaultGuides = {
	"forced_links" : {
		0 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1010520.html",
			"faq" : "1010520",
			"title" : "Renewing Your Payroll Service"
		},
		1 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1008593.html",
			"faq" : "1008593",
			"title" : "Setting Up QuickBooks Payroll Service"
		},
		2 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1010233.html",
			"faq" : "1010233",
			"title" : "Understanding a Charge in Your Account"
		},
		3 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1011976.html",
			"faq" : "1011976",
			"title" : "Finding, Entering and Editing Your Service Key"
		},
		4 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1008577.html",
			"faq" : "1008577",
			"title" : "Errors Updating Payroll"
		},		
		5 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1008521.html",
			"faq" : "1008521",
			"title" : "Cancelling Your Payroll Subscription"
		},
		6 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1008593.html",
			"faq" : "1008593",
			"title" : "Activating or Unlocking Your Payroll Service"
		},
		7 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1009983.html",
			"faq" : "1009983",
			"title" : "Received a tax notice"
		},
		8 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1010558.html",
			"faq" : "1010558",
			"title" : "Payroll Administrator Email Address Change"
		},
		9 : {
			"url" : "http://payroll.intuit.com/support/kb/kbitem/1012188.html",
			"faq" : "1012188",
			"title" : "Stimulus 2009"
		}
	}
}

function getDefaultBaynoteGuides(numLinksToReturn) {
	var defaultLinks = [];
	var count = 0;
	for (var o in baynoteDefaultGuides["forced_links"]) {
		var obj = baynoteDefaultGuides["forced_links"][o];
		defaultLinks.push(new Array(obj["url"], obj["faq"], obj["title"]));
		count++;
		if (count == numLinksToReturn) break;
	}
	defLinksT10 = defaultLinks;
	return defaultLinks;
}
function stringToXML(xmlString)
{
	// Trim the string
	xmlString = $.trim(xmlString);
	
	xmlString = xmlString.replace(/&quot;/g, '"');
	xmlString = xmlString.replace(/&amp;amp;/g, "&amp;");
	xmlString = xmlString.replace(/&amp;quot;/g, '&quot;');
	xmlString = xmlString.replace(/&lt;/g, '<');
	xmlString = xmlString.replace(/&gt;/g, '>');
	
	var xml = xmlString;

	if (window.ActiveXObject) { // IE
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.async = "false";
		xml.loadXML(xmlString);
		
	} else if (document.implementation && document.implementation.createDocument) { // Practically everyone else
		// Basically just Opera needs this
		var xmlParser = new DOMParser();
		xml = xmlParser.parseFromString(xmlString, "text/xml");
	}
	return xml;
}
function baynoteGuidesXMLDisplay(xml, resultsPerPage){
	var orderedListElement = document.createElement('ol');
	var anchorElement, linkTextElement, listItemElement, title;
	var count = 0;
	// Loop through all the r nodes and add them to the ordered list
	$(xml).find('r').each(function(){
		anchorElement = document.createElement('a');
		anchorElement.setAttribute("href", $(this).attr("u"));
		
		linkTextElement = document.createTextNode($(this).attr('t'));
		anchorElement.appendChild(linkTextElement);
		
		listItemElement = document.createElement('li');
		listItemElement.appendChild(anchorElement);
		
		orderedListElement.appendChild(listItemElement);
		count ++;
		
	});
	guidesT10 = '';
	guidesT10Col = ''
	//resultsPerPage = '15';
	if (count < resultsPerPage) {
		numLinksToReturnT10 = resultsPerPage - count
		getDefaultBaynoteGuides(numLinksToReturnT10)
		
		if (defLinksT10.length > 0) {
			for (i=0; i<defLinksT10.length; i++) {
				//url = defLinksT10[i][0];
				//faq = defLinksT10[i][1];
				//title = defLinksT10[i][2];
				guidesT10 = "<li><a href='" + defLinksT10[i][0] + "'>" + defLinksT10[i][2] + "</a></li>";
				guidesT10Col = guidesT10Col + guidesT10;
			}
		}
	}
	$("#baynote_guides").html(orderedListElement);
	$(orderedListElement).addClass("numbered_links");
	$("#baynote_guides").css("display", "block");
	if (guidesT10Col=='') {} 
	else {
		$("#baynote_guides .numbered_links").append(guidesT10Col);
	}
	
}


/**
	 * This class will query the Baynote server
	 * 
	 * By default, it will query against the MostPopular guides
	 */
function BaynoteGuides(guide)
	{	
		this.baynoteDiv = "baynote_guides";
		this.requests = 0;
		this.baynoteGuide = "MostPopular";
		this.baynoteDivClass = "baynote_guides";
		this.callback = null;
		this.callbackParams = null;
		// Omniture Tracking
		this.omnitureTrack = true;
		this.omniturePageName = "Home";
		this.omniturePageSubName = "Top 10 Answers";
		this.omniturePageValue = "FAQ";
	}
	
	/**
	 * This will allow you to specify the div that will be populated with the result set
	 * 
	 * @param {Object} div
	 */
	BaynoteGuides.prototype.setDiv = function(div)
	{
		this.baynoteDiv = div;
	}
	
	/**
	 * This will apply the supplied class to the out ol tag
	 * 
	 * @param {Object} class
	 */
	BaynoteGuides.prototype.setDivClass = function(cssClass)
	{
		this.baynoteDivClass = cssClass;
	}
	
	/**
	 * This will allow you to specify the guide to query
	 * 
	 * @param {Object} guide
	 */
	BaynoteGuides.prototype.setGuide = function(guide)
	{
		this.baynoteGuide = guide;		
	}
	
	/**
	 * This will make a GET request to the Baynote server and return the response
	 * 
	 * @param {Object} attrFilter
	 * @param {Object} resultsPerPage
	 * @param {Object} hardCodedValues
	 */
	BaynoteGuides.prototype.get = function(attrFilter, resultsPerPage, hardCodedValues)
	{   
		var obj = this;
		obj.requests++;
		if (hardCodedValues == null) hardCodedValues = [];	
		$.ajax({
			type: "GET",
			url: "/support/fragments/baynote/baynote_content.jsp?baynoteFilterValue=" + attrFilter,
			dataType: "html",
			//async: false,		// This allows for multiple concurrent requests
			cache: true,
			success: function(xml) {
				
				// Turn the xml string into an xml DOM
				if (typeof xml != "object") {
					xml = stringToXML(xml);
				}
				
				if (obj.callback != null) {
					eval(obj.callback)(xml,obj.callbackParams);
				} else {
					obj.display(xml, resultsPerPage, hardCodedValues);
				}
			},
			error: function(){
				if (obj.callback != null) {
					// This callback function should account for null
					eval(obj.callback)(null,obj.callbackParams);
				} else {
					// Display the default top 10 on error
					obj.display(null, resultsPerPage, getDefaultBaynoteGuides(resultsPerPage));
				}
			}
		});
	}
	
	/**
	 * This receives the response from the GET requests, parses it out and populates the specified div with the results.
	 * 
	 * If any hard coded values are specified, it will force them to the top and remove them from the results.
	 * 
	 * @param {Object} xml
	 * @param {Object} resultsPerPage
	 * @param {Object} hardCodedValues
	 */
	BaynoteGuides.prototype.display = function(xml, resultsPerPage, hardCodedValues)
	{	
		// If the xml is null or contains no children we will need to get the default top 10 from the global.js file
		if (xml == null && hardCodedValues.length < resultsPerPage) {
			// Populate the hard-coded value with the default values
			if (hardCodedValues.length < resultsPerPage){
				var missingNum = resultsPerPage - hardCodedValues.length;
				var defaultGuides = getDefaultBaynoteGuides(missingNum);
				for (var i=0; i<defaultGuides.length; i++) {
					hardCodedValues.push(defaultGuides[i])
				}
			}
		}

		var obj = this;
     	var guides = "<ol class='" + this.baynoteDivClass + "'>";
     	var list = "";
     	var max = resultsPerPage - hardCodedValues.length;
     	var count = 0;
     	var found = false;
     	var attr, url, title, i, faq, omnitureCode;

		// Loop through all the r nodes
		$(xml).find('r').each(function(){
			found = false;
			faq = "";
			omnitureCode = "";
			
			// Get the FAQ
			$(this).find('a').each(function(){
				attr = $(this).attr('n');
	         	if (attr == "content-id") {
					faq = $(this).attr('v');
				}
			});
			
			// Check if the FAQ exists in the hard coded values and if so, ignore it!
			if (hardCodedValues.length > 0) {
	         	for (i=0; i<hardCodedValues.length; i++) {
	         		if (faq == hardCodedValues[i][1]) {
	         			found = true;
	         		}
	         	}
	         }

			if (!found) {
				url = $(this).attr('u');
				title = $(this).attr('t');
				
			// Omniture Tracking
				if (obj.omnitureTrack) {
					if (faq == "") {
						obj.omniturePageValue = obj.omniturePageSubName + " - " + title;
					}
					else {
						obj.omniturePageValue = obj.omniturePageSubName + " - FAQ " + faq + " - " + title;
					}
					obj.omniturePageValue = obj.omniturePageValue.replace(/'/ig, "\\'");
					obj.omniturePageValue = obj.omniturePageValue.replace(/"/ig, "");
					omnitureCode = " onclick=\"trackPageClick('" + obj.omniturePageName + "', '" + obj.omniturePageValue + "')\"";
				}
				
				list += "<li><a href='" + url + "'" + omnitureCode + ">" + title + "</a></li>";
				count ++; 
			}

			if (count == max) return false;
		}); //close each(

		// Add the hard-coded values
		if (hardCodedValues.length > 0) {
			for (i=0; i<hardCodedValues.length; i++) {
				faq = hardCodedValues[i][1];
				title = hardCodedValues[i][2];
				
				// Omniture Tracking
				if (obj.omnitureTrack) {
					if (faq == "") {
						obj.omniturePageValue = obj.omniturePageSubName + " - " + title;
					}
					else {
						obj.omniturePageValue = obj.omniturePageSubName + " - FAQ " + faq + " - " + title;
					}
					obj.omniturePageValue = obj.omniturePageValue.replace(/'/ig, "\\'");
					obj.omniturePageValue = obj.omniturePageValue.replace(/"/ig, "");
					omnitureCode = " onclick=\"trackPageClick('" + obj.omniturePageName + "', '" + obj.omniturePageValue + "')\"";
				}
				
				guides += "<li><a href='" + hardCodedValues[i][0] + "'" + omnitureCode + ">" + title + "</a></li>";
			}
		}
		guides += list + "</ol>";
		$("#" + obj.baynoteDiv).html(guides);
	}
	 if ($.trim($("#baynote_guides").text()) != "") {
			// Get the the xml string and replace the escaped characters
			var baynoteGuidesXML = stringToXML($.trim($("#baynote_guides").html()));		
			baynoteGuidesXMLDisplay(baynoteGuidesXML, 10);
		}
		
	/**
	 * Instantiate the guides
	 */
	var bnGuides = new BaynoteGuides();
	
	/* $(document).ready(function(){
		$(".numbered_links li a:contains('&amp;')").each(function(){
			mystrrep = $(this).html();
			repl22 = mystrrep.replace(/&amp;amp;/g, "&");
			$(this).html(repl22);
		})
	}); */
	 
	/*
	########## EXAMPLES ##########
	// Simple request:
	bnGuides.get("DC.coverage.temporal:2007", 10);

	// Advanced request with forced FAQs
	var hardCodedValues = [
		["/support/go/1086", "1086", "-----2007 State Forms for Personal Returns"],
		["/support/go/607", "607", "-----TurboTax Orders, Charges, and Statements"]
	];
	bnGuides.get("DC.coverage.temporal:2007", 10, hardCodedValues);
	
	// How to make another request to populate another div
	bnGuides.setDiv("baynote_div");
	bnGuides.get("", 5);
	##############################
	*/
