/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg */
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}
// ###################################
function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}
// ###################################
function qString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}
// ###################################
function displayItem(key){
	if(qString(key)=='false')
	{return null;}
	else{return(qString(key));}}
// ###################################


/************
* Google AFC
*************/

/*
* Define required globals 
*/

// Content for site targeted ads
var gafcOniAd;
var gafcOasuText = "Advertise through Google AdWords on this site.";


// Google params
var google_ad_client = 'ca-ottaway-middletown_js';
//var google_ad_channel = 'middletown';
var google_ad_output = 'js';
var google_ad_type = 'text,image,flash'; 
//var google_ad_type = 'image'; 
var google_max_num_ads = '3';
var google_image_size='300x250';
var google_encoding = 'latin1';
var google_language = 'en';
var google_safe = 'high';

// DEBUG
//var google_kw = 'googletestadflashvideo';
//var google_kw_type='exact';

var gQs = new Querystring();
if(typeof(gQs.get("ovrAFCType")) != "undefined")
{
	google_ad_type = gQs.get("ovrAFCType");
}	
	
function google_ad_request_done(google_ads) 
{ 
	if(typeof(google_ads) != "undefined" && google_ads.length > 0)
	{
		gafcOniAd = new Object();
		gafcOniAd.type = google_ads[0].bidtype;
			
		// debug
		if(gQs.get("ovrAFCbidtype"))
		{
			gafcOniAd.type = gQs.get("ovrAFCbidtype");
		}	
    
    if (google_ads[0].type == "flash") 
    {         
    	// we have to remove the .swf extension from the URL for use in theAC_FL_RUNContent method
    	var flashUrlNoExt = google_ad.image_url.replace(/\.swf\?/i,"?");
    	
    	gafcOniAd.content = '<div id="gafcFlashAd"><div class="gafcBranding">Ads By Google</div>' +
    											'<scr' + 'ipt language="JavaScript" type="text/javascript">' +
						  '<!--\n' +
						  'AC_FL_RunContent("codebase","http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' +
						  ',"id","shv","width","' + google_ad.image_width + '","height","' + google_ad.image_height + '","align","middle","src","' +
						  flashUrlNoExt + '","quality","high","wmode","opaque","scale","name","shv","noscale","movie","' + flashUrlNoExt + '"' + 
						  ',"pluginspage","http://www.macromedia.com/go/getflashplayer")\;' +
						  '\n// -->\n' +
  						'</scr' + 'ipt><div class="gafcSignUp"><a href="https://adwords.google.com/select/OnsiteSignupLandingPage?client=' + google_ad_client + '&referringUrl=http://' + document.location.hostname + '&hl=en">' + gafcOasuText + '</a></div></div>';
    } 
    else if (google_ads[0].type == "image") 
    {
      gafcOniAd.content = '<div id="gafcImgAd"><div class="gafcBranding">Ads By Google</div>' +
      									  '<a href="' + 
      				google_ads[0].url + '" target="_top" title="go to ' + google_ads[0].visible_url +
              '"><img border="0" src="' + google_ads[0].image_url +
              '"width="' + google_ads[0].image_width +
              '"height="' + google_ads[0].image_height + '" id="gafcImg"></a><div class="gafcSignUp"><a href="https://adwords.google.com/select/OnsiteSignupLandingPage?client=' + google_ad_client + '&referringUrl=http%3A%2F%2F' + document.location.hostname + '&hl=en">' + gafcOasuText + '</a></div></div>';
			
    } 
    else 
    {      
      if (google_ads.length == 1) 
      {
      	var adBeginLink = '<a href="' + google_ads[0].url + '" ' +
                          'onmouseout="window.status=\'\'" ' +
                          'onmouseover="window.status=\'go to ' +
                          google_ads[0].visible_url + '\'">';
      	var adEndLink = "</a>";
        //Adjust text sizes when 1 ad is returned.
				gafcOniAd.content= '<div id="gafcSingleTxtAd"><div class="gafcBranding">Ads By Google</div>'+
													'<div id="gafcTxtAd">' +
                          '<div class="gafcAdLine1">' + adBeginLink +
                          google_ads[0].line1 + adEndLink + '</div>' +
                          '<div class="gafcAdLine2">' + adBeginLink +
                          google_ads[0].line2  + adEndLink +
                          '</div><div class="gafcAdLine3">' + adBeginLink +
                          google_ads[0].line3 + '</div>' +
                          '<div class="gafcAdVisUrl">' + adBeginLink +
                          google_ads[0].visible_url + '</div></a></div><div class="gafcSignUp"><a href="https://adwords.google.com/select/OnsiteSignupLandingPage?client=' + google_ad_client + '&referringUrl=http%3A%2F%2F' + document.location.hostname + '&hl=en">' + gafcOasuText + '</a></div></div>';
      } 
      else if (google_ads.length > 1) 
      {
        /*
         * For text ads, append each ad to the string.
         */
        gafcOniAd.content = '<div id="gafcMultiTxtAd"><div class="gafcBranding">Ads By Google</div>';
        for(i=0; i < google_ads.length; ++i) 
        {
        	var adBeginLink = '<a href="' + google_ads[i].url + '" ' +
                          'onmouseout="window.status=\'\'" ' +
                          'onmouseover="window.status=\'go to ' +
                          google_ads[i].visible_url + '\'">';
      		var adEndLink = "</a>";
        	gafcOniAd.content += '<div class="gafcTxtAd">' +
                          '<div class="gafcAdLine1">' + adBeginLink +
                          google_ads[i].line1 + adEndLink + '</div>' +
                          '<div class="gafcAdLine2">' + adBeginLink +
                          google_ads[i].line2  + adEndLink +
                          '</div><div class="gafcAdLine3">' + adBeginLink +
                          google_ads[i].line3 + '</div>' +
                          '<div class="gafcAdVisUrl">' + adBeginLink +
                          google_ads[i].visible_url + '</div></a></div>';
        }
        gafcOniAd.content += '<div class="gafcSignUp"><a href="https://adwords.google.com/select/OnsiteSignupLandingPage?client=' + google_ad_client + '&referringUrl=http%3A%2F%2F' + document.location.hostname + '&hl=en">' + gafcOasuText + '</a></div></div>';
      }
    }
	}
  return;	
}  

function writeCPCAd()
{
	if(gafcOniAd && gafcOniAd.type == "CPC")
	{
		document.write(gafcOniAd.content);
	}
}

function writeCPMAd()
{
	if(gafcOniAd && gafcOniAd.type == "CPM")
	{
		document.write(gafcOniAd.content);
	}
}

function writeAnyAd()
{
	if(gafcOniAd)
	{
		document.write(gafcOniAd.content);
	}
}