var partnerTermsParams = '';
var shopKeyword = "";

function readID() {
	var id = GetCookie('trackingid');
	if (id == null || id == undefined) {
		var expDays = 365; // number of days the cookie should last
		var ran_number=Math.floor(Math.random()*999);
		var nowDate = new Date();
		id = nowDate.getTime()+""+addZeros(ran_number,3);
		setCookie('trackingid', id, expDays);
	}
	return id;
}
function addZeros(number,length){
	number=number.toString();
	while (number.length<length)
	{
		number = "0"+number;
	}
	return number;
}
function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
		endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0; 
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;   
	}  
	return null;
}
function setCookie(name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime(today.getTime());

	if (expires) {
		expires = expires * 1000 * 60;
	}

	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape(value) +
	((expires) ? ';expires='+expires_date.toGMTString() : '') + 
	((path) ? ';path=' + path : '') +
	((domain) ? ';domain=' + domain : '') +
	((secure) ? ';secure' : '');
}
function deleteCookie( name ) {
	setCookie(name, null, -9999);
}

function overtureResponse(xmlDoc){
	
	//logger("xmlDoc:"+xmlDoc);

	//returns the market code, example: us
	this.market = or_getMarket(xmlDoc);
	//logger("market:"+this.market);

	if(this.market != "us" && this.market != "uk" && this.market != "au"){
		showYahooLander = true;
	}

	//returns the keyword used
	this.keyword = or_getKeyword(xmlDoc);
	//logger("keyword:"+this.keyword);

	//returns true or false	
	this.d2r = or_getFeedType(xmlDoc);
	//logger("d2r:"+this.d2r);

	//returns an array of related terms and it's associated query params in the form of [name][queryparams]
	this.relatedTerms = or_getRelatedTerms(xmlDoc);
	//logger("relatedTerms:"+this.relatedTerms);

	//returns an array of popular categories and it's associated query params in the form of [category_name][name][queryparams]
	//[category_name]['params'] returns the queryParams just for the category
	this.popularCategories = or_getPopularCategories(xmlDoc);
	//logger("popularCategories:"+this.popularCategories);

	//returns the qs parameter used for partner term searches.
	this.partnerTerms = or_getPartnerTerms(xmlDoc);
	partnerTermsParams = this.partnerTerms;
	setCookie('partnerTermsParams',partnerTermsParams,30);
	
	//returns a string of the queryParams to be used in future searches
	this.searchBoxParams = or_getSearchBoxParams(xmlDoc);
	searchBoxParams = this.searchBoxParams;
	//logger("searchBoxParams:"+searchBoxParams);

	this.adListings = or_getAdListings(xmlDoc);
	this.webListings = or_getWebListings(xmlDoc);
	this.nextArgs = or_getNextArgs(xmlDoc);
	this.prevArgs = or_getPrevArgs(xmlDoc);
	

	return this;
}

function getLander(){
	feedURL = '/shopsfeed.php';
	new Ajax.Request(feedURL,{asynchronous:true, evalScripts:true, onSuccess:parseLander, onFailure:errFeed});
}
function parseLander(ajx){
	var xmlDoc = ajx.responseXML;
	responseObj = new overtureResponse(xmlDoc);
	getResults(shopKeyword,partnerTermsParams);
}
function getResults(q,qs,maxListings){
	
	maxListings = maxListings || 6;
	feedURL = '/shopsfeed.php?q='+q+'&qs='+qs+'&maxListings='+maxListings;
	new Ajax.Request(feedURL,{asynchronous:true, evalScripts:true, onSuccess:parseResults, onFailure:errFeed});
}
function getResults2(q,qs,maxListings){
	
	maxListings = maxListings || 6;
	feedURL = '/shopsfeed.php?q='+q+'&qs='+qs+'&maxListings='+maxListings;
	new Ajax.Request(feedURL,{asynchronous:true, evalScripts:true, onSuccess:parseResults2, onFailure:errFeed});
}
function errFeed(ajx){
	alert(ajx);
}
function loadresults(kwd){
	shopKeyword = kwd || "";
	//document.write("<div id='results'></div>");
	try{
		contentdiv = document.getElementById('insidecontent');
		//contentdiv.innerHTML = "<div id='results_top'></div>\n"+contentdiv.innerHTML+"\n<div id='results'></div>\n";
		contentdiv.innerHTML = contentdiv.innerHTML+"\n<div id='results'></div>\n";
	}catch(err){
	
	}
	if(GetCookie("partnerTermsParams")==null){
		getLander();
	}else{
		if(shopKeyword!=""){
			getResults(shopKeyword,GetCookie("partnerTermsParams"));
		}
	}
}
function loadresults2(kwd){
	shopKeyword = kwd || "";
	if(GetCookie("partnerTermsParams")==null){
		getLander();
	}else{
		if(shopKeyword!=""){
			getResults2(shopKeyword,GetCookie("partnerTermsParams"));
		}
	}
}

function parseResults(ajx){
	var xmlDoc = ajx.responseXML;
	responseObj = new overtureResponse(xmlDoc);

	
	
	adListings = responseObj.adListings;
	
	resultcount=0;
	var results_div1 = document.getElementById("results_top");
	results_div1.innerHTML = "";

	
				var lastListingType="true";
				for (x=0;x<3;x++ )
				{
					//logger("line:"+x+"");
					
					var title = adListings[resultcount][0];
					var descriptionstr = adListings[resultcount][1];
					var siteHost = adListings[resultcount][2];
					var clickurl = adListings[resultcount][3];
					var biddedListing = adListings[resultcount][4];



					var dom_listing = document.createElement("div");
					dom_listing.setAttribute("id","listing");
					var dom_url = document.createElement("div");
					dom_url.setAttribute("id","url");
					var dom_description = document.createElement("div");
					dom_description.setAttribute("id","description");
					var dom_sitehost = document.createElement("div");
					dom_sitehost.setAttribute("id","host");
					
					var dom_link = document.createElement("a");
					dom_link.setAttribute("href",clickurl);
					dom_link.setAttribute("target","_blank");
					
					if(biddedListing=="true"){
						eval("dom_link.onclick = function(){track_action='clicked_sponsored_result';track_domaindest='"+siteHost+"';pageview('"+clickurl+"');return true;}");
					}else{
						eval("dom_link.onclick = function(){track_action='clicked_web_result';track_domaindest='"+siteHost+"';pageview('"+clickurl+"');return true;}");
					}



					dom_link.innerHTML=title;
					dom_sitehost.innerHTML=siteHost;
					dom_description.innerHTML=descriptionstr;
					dom_url.appendChild(dom_link);
					dom_listing.appendChild(dom_url);
					dom_listing.appendChild(dom_description);
					dom_listing.appendChild(dom_sitehost);

					if(x==3){
						
						//var results_div = document.getElementById("results");
					}

					if((x==0) && biddedListing=="true"){
						var sponsored_listings = document.createElement("div");
						sponsored_listings.setAttribute("id","resultsHeader");
						sponsored_listings.innerHTML='Sponsored Results';
						results_div1.appendChild(sponsored_listings);
					}
					if((x==0) && biddedListing=="false"){
						var sponsored_listings = document.createElement("div");
						sponsored_listings.setAttribute("id","resultsHeader");
						sponsored_listings.innerHTML='Web Results';
						results_div1.appendChild(sponsored_listings);
					}
					if (x!=0 && lastListingType!=biddedListing){
						var sponsored_listings = document.createElement("div");
						sponsored_listings.setAttribute("id","resultsHeader");
						sponsored_listings.innerHTML='Web Results';
						results_div1.appendChild(sponsored_listings);
					}
					lastListingType = biddedListing;
					results_div1.appendChild(dom_listing);
					//logger("line:"+x+" complete");
					resultcount+=1;
				}			
	var results_div = document.getElementById("results");
	results_div.innerHTML = "";

	
				var lastListingType="true";
				for (x=0;x<5;x++ )
				{
					//logger("line:"+x+"");
					
					var title = adListings[resultcount][0];
					var descriptionstr = adListings[resultcount][1];
					var siteHost = adListings[resultcount][2];
					var clickurl = adListings[resultcount][3];
					var biddedListing = adListings[resultcount][4];



					var dom_listing = document.createElement("div");
					dom_listing.setAttribute("id","listing");
					var dom_url = document.createElement("div");
					dom_url.setAttribute("id","url");
					var dom_description = document.createElement("div");
					dom_description.setAttribute("id","description");
					var dom_sitehost = document.createElement("div");
					dom_sitehost.setAttribute("id","host");
					
					var dom_link = document.createElement("a");
					dom_link.setAttribute("href",clickurl);
					dom_link.setAttribute("target","_blank");
					
					if(biddedListing=="true"){
						eval("dom_link.onclick = function(){track_action='clicked_sponsored_result';track_domaindest='"+siteHost+"';pageview('"+clickurl+"');return true;}");
					}else{
						eval("dom_link.onclick = function(){track_action='clicked_web_result';track_domaindest='"+siteHost+"';pageview('"+clickurl+"');return true;}");
					}



					dom_link.innerHTML=title;
					dom_sitehost.innerHTML=siteHost;
					dom_description.innerHTML=descriptionstr;
					dom_url.appendChild(dom_link);
					dom_listing.appendChild(dom_url);
					dom_listing.appendChild(dom_description);
					dom_listing.appendChild(dom_sitehost);

					if(x==3){
						
						//var results_div = document.getElementById("results");
					}

					if((x==0) && biddedListing=="true"){
						var sponsored_listings = document.createElement("div");
						sponsored_listings.setAttribute("id","resultsHeader");
						sponsored_listings.innerHTML='Sponsored Results';
						results_div.appendChild(sponsored_listings);
					}
					if((x==0) && biddedListing=="false"){
						var sponsored_listings = document.createElement("div");
						sponsored_listings.setAttribute("id","resultsHeader");
						sponsored_listings.innerHTML='Web Results';
						results_div.appendChild(sponsored_listings);
					}
					if (x!=0 && lastListingType!=biddedListing){
						var sponsored_listings = document.createElement("div");
						sponsored_listings.setAttribute("id","resultsHeader");
						sponsored_listings.innerHTML='Web Results';
						results_div.appendChild(sponsored_listings);
					}
					lastListingType = biddedListing;
					results_div.appendChild(dom_listing);
					//logger("line:"+x+" complete");
					resultcount+=1;
				}
	
}
function parseResults2(ajx){
	var xmlDoc = ajx.responseXML;
	responseObj = new overtureResponse(xmlDoc);

	
	
	adListings = responseObj.adListings;

				resultcount3=0;
				var results_div3 = document.getElementById("productppc");
				results_div3.innerHTML = "";


							var lastListingType="true";
							for (x=0;x<5;x++ )
							{
								//logger("line:"+x+"");

								var title = adListings[resultcount3][0];
								var descriptionstr = adListings[resultcount3][1];
								var siteHost = adListings[resultcount3][2];
								var clickurl = adListings[resultcount3][3];
								var biddedListing = adListings[resultcount3][4];



								var dom_listing = document.createElement("div");
								dom_listing.setAttribute("id","listing");
								var dom_url = document.createElement("div");
								dom_url.setAttribute("id","url");
								var dom_description = document.createElement("div");
								dom_description.setAttribute("id","description");
								var dom_sitehost = document.createElement("div");
								dom_sitehost.setAttribute("id","host");

								var dom_link = document.createElement("a");
								dom_link.setAttribute("href",clickurl);
								dom_link.setAttribute("target","_blank");

								if(biddedListing=="true"){
									eval("dom_link.onclick = function(){track_action='clicked_sponsored_result';track_domaindest='"+siteHost+"';pageview('"+clickurl+"');return true;}");
								}else{
									eval("dom_link.onclick = function(){track_action='clicked_web_result';track_domaindest='"+siteHost+"';pageview('"+clickurl+"');return true;}");
								}



								dom_link.innerHTML=title;
								dom_sitehost.innerHTML=siteHost;
								dom_description.innerHTML=descriptionstr;
								dom_url.appendChild(dom_link);
								dom_listing.appendChild(dom_url);
								dom_listing.appendChild(dom_description);
								dom_listing.appendChild(dom_sitehost);

								if(x==3){

									//var results_div = document.getElementById("results");
								}

								if((x==0) && biddedListing=="true"){
									var sponsored_listings = document.createElement("div");
									sponsored_listings.setAttribute("id","resultsHeader");
									sponsored_listings.innerHTML='Sponsored Results';
									results_div3.appendChild(sponsored_listings);
								}
								if((x==0) && biddedListing=="false"){
									var sponsored_listings = document.createElement("div");
									sponsored_listings.setAttribute("id","resultsHeader");
									sponsored_listings.innerHTML='Web Results';
									results_div3.appendChild(sponsored_listings);
								}
								if (x!=0 && lastListingType!=biddedListing){
									var sponsored_listings = document.createElement("div");
									sponsored_listings.setAttribute("id","resultsHeader");
									sponsored_listings.innerHTML='Web Results';
									results_div3.appendChild(sponsored_listings);
								}
								lastListingType = biddedListing;
								results_div3.appendChild(dom_listing);
								//logger("line:"+x+" complete");
								resultcount3+=1;
							}
	
}
