/*
===============================================================================
PAGE:			RealMediaNHC.js 

AUTHOR:			Gary Winter

DESCRIPTION:	Javascript Routines for RealMedia calls

INCLUDES:		/HomesCom/Resources/RealMediaNHC.js
	
NOTES:			

START DATE:		December 02, 2005

MODIFICATIONS:
MM/DD/YYYY      Author          Change
04/07/206		Gary Winter		Added replace for OAS_query to replace
								plus, space and %20 with a dash
04/19/206		Gary Winter		Added code to replace single quotes with no space in OAS_QUERY
9/18/2006		Gary Winter		Removed references to Left1 from all position lists
===============================================================================
*/

/* Function for parsing querystring */

function GetParam(name,search)
{
	var start=search.indexOf("?"+name+"=");
	if (start < 0) start = search.indexOf("&" + name + "=");
	if (start < 0) return '';
	start += name.length + 2;
	var end = search.indexOf("&",start) - 1;
	if (end < 0) end = search.length;
	var result = location.search.substring(start,end);
	var result = '';
	for( var i = start; i <= end; i++ ) {
		var c = location.search.charAt(i);
		result = result + (c=='+'?' ':c);
	}
	return unescape(result);
}

/* OAS SETUP begin */

//Configuration
// Initilaize variables
	//Array of page names the use the Middle1 Position
		//If adding to this, make sure the page name is in lower case
		//  and full script_name is used.
	var page_array = new Array(
	"/content/nhccriteria.cfm",
	"/content/nhccommunity.cfm",
	"/content/nhccontact.cfm",
	"/content/nhccontactthanks.cfm",
	"/content/nhcthankyou.cfm",
	"/content/getlisted.cfm",
	"/content/nhcarticles.cfm",
	"/content/nhcusmap.cfm",
	"/content/nhcstatemap.cfm",
	"/content/nhccityoptions.cfm");
	// RealMedia server
	var OAS_url = 'http://ads.traderonline.com/RealMedia/ads/';
	// RealMeadia page - this is the full location
	//OAS_sitepage = 'www.homes.com/homepage.html';
	var OAS_sitepage = 'www.homes.com' + window.location.pathname.toLowerCase();
	// variable to use so we can compare and not worry about case in the pathname
	var page = window.location.pathname.toLowerCase();
	// Initialize OAS_listpos ( RealMedia Position List )
	var OAS_listpos = "";
	// Initialize OAS_target
	var OAS_target = '_top';
	// Initialize OAS_query ( RealMedia Targeting )
	if ( ! OAS_query )
		var OAS_query = '';
	//alert (page);
	
//OAS Position List Setup Begin
	// This checks which page we are on and set the proper position list
	if ( page == '/content/newhomessearch.cfm' || page == '/'){
		OAS_listpos = 'Top,Right1';
		if ( page == '/' ){
			OAS_sitepage = OAS_sitepage + '/content/newhomessearch.cfm';
		}
	} else if ( page == '/content/nhcsearchresults.cfm') {
		OAS_listpos = 'Top,Right1,Bottom1';
	} else if ( page == '/content/nhcmodel.cfm') {
		OAS_listpos = 'Top,Right1,Bottom1';
	} else {
		for ( var i = 0; i < page_array.length; i++ ) {
			if ( page_array[i] == page )
				OAS_listpos = 'Top,Right1,Bottom1';
		}
		if ( ! OAS_listpos.length )
			OAS_listpos = 'Top,Right1';
	}
//OAS Position List Setup End
//alert ('OAS_listpos = '+OAS_listpos);
//OAS_query setup start
	// This sets the targeting parameters passed through the URL variables
	if ( location.search.length > 0 ) {
	// set the url string to all lower case for ease of comparison
	var search = location.search.toLowerCase();
		if ( GetParam('city',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'city=' + GetParam('city',search);
			else
				OAS_query = OAS_query + '&city=' + GetParam('city',search);
			OAS_query += "&_RM_HTML_CITY_=" + GetParam('city',search).replace(/[ ]+/g, '_').replace(/%20/g, '_');
		}
		if ( GetParam('state',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'state=' + GetParam('state',search);
			else
				OAS_query = OAS_query + '&state=' + GetParam('state',search);
			OAS_query += "&_RM_HTML_STATE_=" + GetParam('state',search).replace(/[ ]+/g, '_').replace(/%20/g, '_');
		}
		if ( GetParam('zipcode',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'zipcode=' + GetParam('zipcode',search);
			else
				OAS_query = OAS_query + '&zipcode=' + GetParam('zipcode',search);
			OAS_query += "&_RM_HTML_ZIP_=" + GetParam('zipcode',search).replace(/[ ]+/g, '_').replace(/%20/g, '_');
		}
		if ( GetParam('county',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'county=' + GetParam('county',search);
			else
				OAS_query = OAS_query + '&county=' + GetParam('county',search);
		}
		if ( GetParam('communityid',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'communityid=' + GetParam('communityid',search);
			else
				OAS_query = OAS_query + '&communityid=' + GetParam('communityid',search);
		}
		if ( GetParam('price',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'price=' + GetParam('price',search);
			else
				OAS_query = OAS_query + '&price=' + GetParam('price',search);
		}
		/*if ( GetParam('builderid',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'builderid=' + GetParam('builderid',search);
			else
				OAS_query = OAS_query + '&builderid=' + GetParam('builderid',search);
		}*/
		if ( GetParam('article',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'article=' + GetParam('article',search);
			else
				OAS_query = OAS_query + '&article=' + GetParam('article',search);
		}
	}
//Make sure everything is lower case
OAS_query = OAS_query.toLowerCase();
//make sure query string doesn't have pluses
OAS_query = OAS_query.replace(/[\+]+/g, '-');
OAS_query = OAS_query.replace(/%20/g, '-');
OAS_query = OAS_query.replace(/[ ]+/g, '-');
OAS_query = OAS_query.replace(/[\']+/g, '');
//alert (OAS_query);
//OAS_query setup end
//End Configuration

OAS_version = 10;
OAS_rn = '00123456789';
OAS_rns = '00123456789';
OAS_rn = new String (Math.random());
OAS_rns = OAS_rn.substring (2, 11);
// this is the nonconforming version of the RealMedia call
function OAS_NORMAL(pos) {
	document.write('<a href="' + OAS_url + 'click_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@'
		+ OAS_listpos + '!' + pos + '?' + OAS_query + '" TARGET=' + OAS_target + '>');
	document.write('<img src=""="' + OAS_url + 'adstream_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@'
		+ OAS_listpos + '!' + pos + '?' + OAS_query + '" TARGET=' + '" border=0></a>');
}	
// Here we check for which navigator is used and show the tags accordingly
OAS_version = 11;
if (( navigator.userAgent.indexOf('Mozilla/3') != -1) || (navigator.userAgent.indexOf('Mozilla/4,0 WebTV') != -1))
	OAS_version = 10;
// This loads in the OAS_RICH function from ad server if the navigator is a standard one
if ( OAS_version >= 11 )
	document.write('<scr' + 'ipt language=JavaScript1.1 src="' + OAS_url + 'adstream_mjx.ads/' + OAS_sitepage + '/1' + 
	OAS_rns + '@' + OAS_listpos + '?' + OAS_query + '">' + OAS_target + '"></script>');
 
document.write('');
// Function called in code to display RealMedia - Must be cased exactly like it is in the position list
function OAS_AD(pos) {
	if ( OAS_version >= 11 )
		OAS_RICH(pos);
	else
		OAS_NORMAL(pos);
}
//-->
/* OAS SETUP end */

