// Function to get search term
var searchtermarr = new Array();
var query = window.location.search.substring(1);
var params = query.split('&');
for (var i=0; i<params.length; i++) {
	var pos = params[i].indexOf('=');
	if (pos > 0) {
		var key = params[i].substring(0,pos);
		var val = params[i].substring(pos+1);
		searchtermarr[key] = val;
	}
}

if(searchtermarr['w']){ searchterm = searchtermarr['w']; }
else{ searchterm = "no search";  }

// Converts the array into an object so that we can test if a value is in the array
function oc(a){
  var o = {};
  for(var i=0;i<a.length;i++){ o[a[i]]=''; }
  return o;
}

//Adult Search Term Array
var adulttermsarr = new Array();
var adulttermsarr = ["auckland+massage+parlours", "escorts", "prostitutes", "nzr18", "viagra", "strip", "transsexual+escorts", "transsexual", "brothel", "escorts hamilton", "christchurch massage parlours", "transexual", "peaches and cream", "escorts nz"];

//Google Ads for Search Javascript
hide_ads = false;
restricted = false;
adultsearchterm = false;
function google_afs_request_done(google_ads) {
/* This function is required and is used to display the ads that are returned from the JavaScript request. You should modify the document.write commands so that the HTML they write out fits with your desired ad layout. */
var s = '';
var i;
var visURL = "";
var advert_number_to_display = 6; // Sets Max Number of ads for a page 
// If Search Term is Adult and in our array display our ads
if((searchterm in oc(adulttermsarr)) && !adultsearchterm){
	adultsearchterm = true; // Sets adultsearchterm = true so that when it comes back with ads it won't repeat this process
	restricted = true; // This tells the script that the ads displayed will not be ads from google
	theads = restricted_ads(); // restricted_ads() is a function set in nzs-zeus.js that returns our adult ads
	google_afs_request_done(theads); // Recalls this script with the returned ads
} else if (google_ads.length == 0) { /* Verify that there are actually ads to display. */
	restricted = true; // This tells the script that the ads displayed will not be ads from google
	theads = search_ads(); // search_ads() is a function set in nzs-zeus.js that returns our ads
	google_afs_request_done(theads); // Recalls this script with the returned ads
} else if(google_ads.length < 3 && custom_type == "main-bottom"){
		// If there are less than 3 google ads and there need to be ads displayed at the bottom aswell then we need to call our function to display our ads
		restricted = true; // This tells the script that the ads displayed will not be ads from google
		custom_type = "main-bottom" // Sets custom_type to main-bottom again
		theads = search_ads(); // search_ads() is a function set in nzs-zeus.js that returns our ads
		google_afs_request_done(theads); // Recalls this script with the returned ads
} else {  // it should be noted that we are intentionally ignoring the one ad case, we want either the first N adverts or the whatever Google gives us whichever is the smaller
	if(google_ads.length < advert_number_to_display){ numberOfAdverts = google_ads.length; }
	else{ numberOfAdverts = advert_number_to_display; }
	/* For text ads, append each ad to the string. */
		startnum = 0;
		
		main_number = 3; // number of ads to display in the main section
		main_bottom = 3 // number of ads to display in the bottom section
		right_number = 4; // number of ads to display in the right section
		
		if(custom_type=="right-sidebar"){
			
			s+='<div class="google-right">';
			
			if(!restricted){ // If restricted has not been set to true then display the Ads by Google link, otherwise do not display it
				s+='				<span class="srnd-border-blue-grad-heading"><a href="http://services.google.com/feedback/online_hws_feedback"><img src="/img/headings/surrounds/ads_by_google_grey.gif" width="100" height="13" /></a></span>';
			} else{
				s+='				<span class="srnd-border-blue-grad-heading"><a href="/advertising/"><img src="/img/headings/surrounds/ads_by_nzs.gif" /></a></span>';
			}
			
		} else{
			
			s+='<div class="directory">';
			
			if(!restricted){ // If restricted has not been set to true then display the Ads by Google link, otherwise do not display it
				s+='<div class="googlelink"><a href="http://services.google.com/feedback/online_hws_feedback"><img src="/img/headings/surrounds/ads_by_google_green.gif" width="89" height="13" class="googlelinkimg" /></a></div>';
			} else {
				s+='<div class="googlelink"><img src="/img/headings/surrounds/ads_by_nzs_green.gif" width="95" height="13" class="googlelinkimg" /></div>';
			}
			
		}
		
		switch(custom_type){
			// Setting the start and end ad numbers
			case "main-wide":
			startnum = 0;
			endnum = main_number;
			break;
			case "main-bottom":
			startnum = startnum + main_number; //This will make the first ad in the bottom the next ad from the array after the last ad displayed in the top section.
			endnum = startnum + main_bottom;
			break;
			case "right-sidebar":
			startnum = 0 + main_number + main_bottom;
			endnum = startnum + right_number;
			break;
		}
		// we want to ensure that we show the correct ads for the bottom, even though google may not send the correct number  
		if(endnum > (google_ads.length)){ // look for whether google sent us a lesser number
			endnum = google_ads.length; // set the endnum back to get the end of the list sent
			if(startnum > (google_ads.length - 1)){
				switch(custom_type){
					case "main-bottom":
					startnum = endnum-main_bottom; // try to get the right number of ads
					break;
					case "right-sidebar":
					startnum=endnum-right_number; // try to get the right number of ads
					break;
				}
			}
			if(startnum < 0) { startnum=0; } // if all else fails start at 0
		}
		s+='<ul class="google-ads">';
		for(i = startnum; i < endnum; ++i) { //Runs through each ad that has been sent
		// we need to process the visible url so that we don't have it spilling out over the side
			rawvisURL = google_ads[i].visible_url;
			if(custom_type=="right-sidebar"){
				if(!wide_ad){ numchars = 24; }
				else{ numchars = 24; }
				if(rawvisURL.length > numchars){
					visURLsections = Math.ceil(rawvisURL.length/numchars);
					visURLarr = new Array();
					for(j=0; j< visURLsections; j++){
						visURLarr[j] = rawvisURL.slice(0,numchars);
						rawvisURL = rawvisURL.slice(numchars);
					}
					
					thevisURL = '';
					for(j=0; j< visURLarr.length; j++){
						thevisURL += visURLarr[j] + '<br />';
					}
					visURL = thevisURL.substring(0,(thevisURL.length - 6));
					
					//visURL=rawvisURL.slice(0,18);
					//visURL += "...";
				} else { visURL=rawvisURL; }
			} else{ visURL=rawvisURL; }
			if(custom_type=="right-sidebar"){
				s+='<li style="list-style:none; background:none;">';
				s+='<a href="' + google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' + visURL + '\'"><strong>' + google_ads[i].line1 + '</strong></a>';
				s+='<p>' + google_ads[i].line2 + '&nbsp;' + google_ads[i].line3 + '</p>';
				s+='<a href="' + google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' + visURL + '\'" class="url">' + visURL + '</a>';
				s+='</li>';
			} else {
				s+='<li>';
				s+='<a href="' + google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' + visURL + '\'"><strong>' + google_ads[i].line1 + '</strong></a>';
				s+=' <a href="' + google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' + visURL + '\'" class="url">(' + visURL + ')</a>';
				s+='<p>' + google_ads[i].line2 + '&nbsp;' + google_ads[i].line3 + '</p>';
				s+='</li>';
			}
		}
		s+="</ul>";
		
		if(custom_type=="right-sidebar"){
			s+='</div>';
		} else{
			s+='</div>';
		} 
	}
	//document.write(s);
	if(!hide_ads){document.write(s);}
	return;
}
custom_type="main-wide"; // set global value to the ad-type that is the ones in the middle section with the listings
global_s="";