/*
===============================================================================
PAGE:			RealMedia.js 

AUTHOR:			Gary Winter

DESCRIPTION:	Javascript Routines for RealMedia calls

INCLUDES:		
	
NOTES:			

START DATE:		October 04, 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
06/19/2006		Debbie Martindale added function fullCityName to city OAS_QUERY section to filter
								out partial abbreviations of city names (st., ft., pt., mt.)
9/18/2006		Gary Winter		Removed references to Left1 from all position lists
===============================================================================
*/
/* Function for parsing citynames */

function fullCityName(cityname){
var rExp1 = /mt. /gi
var rExp2 = /st. /gi
var rExp3 = /ft. /gi
var rExp4 = /pt. /gi
var returnstring = ""
returnstring=cityname.replace(rExp1,"mount ")
returnstring=returnstring.replace(rExp2,"saint ")
returnstring=returnstring.replace(rExp3,"fort ")
returnstring=returnstring.replace(rExp4,"port ")
return returnstring;
}

/* 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/calculatorsarticles.cfm",
	"/content/competitivemarketanalysisrequest.cfm",
	"/content/competitivemarketanalysisthanks.cfm",
	"/content/custombuildersearch.cfm",
	"/content/faq.cfm",
	"/content/feedback.cfm",
	"/content/financecenter.cfm",
	"/content/financecenterresults.cfm",
	"/content/guestbook.cfm",
	"/content/homescomtogo.cfm",
	"/content/listingcityoptions.cfm",
	"/content/listingcityselect.cfm",
	"/content/listingcriteria.cfm",
	"/content/listingdetail.cfm",
	"/content/listingstatemap.cfm",
	"/content/listingusmap.cfm",
	"/content/neighborhoodcityoptions.cfm",
	"/content/neighborhoodcityselect.cfm",
	"/content/neighborhoodcountyselect.cfm",
	"/content/neighborhoodcrime.cfm",
	"/content/neighborhooddemos.cfm",
	"/content/neighborhoods.cfm",
	"/content/neighborhoodsearchmain.cfm",
	"/content/neighborhoodstatemap.cfm",
	"/content/neighborhoodzipcompare.cfm",
	"/content/neighborhoodzipdetail.cfm",
	"/content/schoolneighborhoodsearchresults.cfm",
	"/content/schooldetail.cfm",	
	"/content/rentalcriteria.cfm",
	"/rentals",
	"/content/rentalsearch.cfm",
	"/content/rentalstatemap.cfm",
	"/content/schoolreports.cfm",
	"/content/sitemap.cfm",
	"/content/articles/details.cfm",
	"/content/articles/glossary.cfm",
	"/content/articles/index.cfm",
	"/content/calculators/index.cfm",
	"/content/promotions/marketconditions.cfm",
	"/content/searchoptions.cfm",
	"/content/foreclosuresearch.cfm",
	"/my/emailreminder.cfm",
	"/my/nobookmarks.cfm",
	"/my/signoff.cfm",
	"/my/signon.cfm",
	"/my/startup.cfm",
	"/my/signupconfirmation.cfm",
	"/content/agentsearch.cfm");
	// RealMedia server
	//var OAS_url = 'http://ads.traderonline.com/RealMedia/ads/';
	var OAS_url = 'http://oasc05139.247realmedia.com/RealMedia/ads/';
	
	// RealMeadia page - this is the full location
	//OAS_sitepage = 'www.homes.com/homepage.html';
	if (typeof(OAS_sitepage) == 'undefined')
		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
	if (typeof(page) == 'undefined')
		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 = '';
	var google_kw_type = 'google_kw_type=broad';
	var google_kw = 'google_kw=';
		
//OAS Position List Setup Begin
	// This checks which page we are on and sets the proper position list
	if ( page == '/index.cfm' || page == '/'){
		OAS_listpos = 'Top,Right1';
		if (page == '/') {
			OAS_sitepage = OAS_sitepage + "index.cfm";
		}
	} else if ( page == '/content/listing.cfm' || page == '/content/cmasearchresults.cfm') {
		OAS_listpos = 'Top,Bottom1'
	} else if ( page == '/content/agentofficesearchresults.cfm' || page == '/content/listingsearchresults.cfm') {
		OAS_listpos = 'Top,Right1,Bottom1'
	} else if ( page == '/content/inter.cfm' || page == '/content/nhcinter.cfm') {
		OAS_listpos = 'Middle'
	} else if ( page == '/content/agentsearch.cfm' ) {
		OAS_listpos = 'Top,Right1'		
	} else if ( page == '/content/schoolneighborhoodsearchresults.cfm' || page == '/content/schooldetail.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';
	}
	//alert(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);
				//OAS_query = 'city=' + fullCityName(GetParam('city',search));
			else
				OAS_query = OAS_query + '&city=' + GetParam('city',search);
				//OAS_query = OAS_query + '&city=' + fullCityName(GetParam('city',search));
			google_kw = google_kw + GetParam('city',search);
		}
		if ( GetParam('county',search) ) {
			if (OAS_query.length == 0)
				OAS_query = 'county=' + GetParam('county',search);
			else
				OAS_query = OAS_query + '&county=' + GetParam('county',search);
			google_kw = google_kw + GetParam('county',search);
		}	
		if ( GetParam('state',search) ) {
			if (OAS_query.length == 0){
				OAS_query = 'state=' + GetParam('state',search);
			} else {
				OAS_query = OAS_query + '&state=' + GetParam('state',search);
			}	
			var stateFix = GetParam('state',search);
			stateFix = stateFix.toLowerCase();
			if ( stateFix.length > 0 && ( page == '/content/listingsearchresults.cfm' 
									|| page == '/content/inter.cfm'
									|| page == '/content/listingcriteria.cfm' 									
									|| page == '/content/schoolneighborhoodsearchresults.cfm' 
									|| page == '/content/agentofficesearchresults.cfm' ) ) { 
				OAS_sitepage += '/' + stateFix;
			}
			google_kw = google_kw + ' ' + GetParam('state',search);
		}
		if ( GetParam('zipcode',search) ) {
			if (OAS_query.length == 0) {
				OAS_query = 'zipcode=' + GetParam('zipcode',search);
			} else {
				OAS_query = OAS_query + '&zipcode=' + GetParam('zipcode',search);
			}
			//alert ( stateFix );
			if (  page == '/content/listingsearchresults.cfm' 
									|| page == '/content/inter.cfm'
									|| page == '/content/listingcriteria.cfm' ) {
				if ( stateFix && stateFix.length > 0 ) {
					OAS_sitepage += '/' +  stateFix.toLowerCase();
				}				
				
			}
			google_kw = google_kw + GetParam('zipcode',search);
		}
		if (  page == '/content/listingsearchresults.cfm' 
									|| page == '/content/inter.cfm'
									|| page == '/content/listingcriteria.cfm' ) {
			if (GetParam('foreclosure',search).toLowerCase() == 'yes')
			{
				OAS_query += '&proptype=foreclosure';
			}
			else
			{
				OAS_query += '&proptype=' + GetParam('proptype',search);	
			}		
		}
		if (page == '/content/listing.cfm')
		{
			OAS_query += '&proptype=' + LDarchtype;
		}
		/* BEGIN _RM_HTML */
		if (page == '/content/agentofficesearchresults.cfm' || page == '/content/inter.cfm' || page == '/content/nhcinter.cfm' || page == '/content/listingsearchresults.cfm' || page == '/content/schoolneighborhoodsearchresults.cfm' || page == '/content/schooldetail.cfm')
		{
			OAS_query += '&_RM_HTML_CITY_=' + GetParam('city',search).replace(/[ ]+/g, '_').replace(/%20/g, '_');
			OAS_query += '&_RM_HTML_STATE_=' + GetParam('state',search).replace(/[ ]+/g, '_').replace(/%20/g, '_');
			OAS_query += '&_RM_HTML_ZIP_=' + GetParam('zipcode',search).replace(/[ ]+/g, '_').replace(/%20/g, '_');
		}
		
	}

//Make sure everything is lower case

OAS_query = OAS_query.toLowerCase();
//alert(OAS_query);
//make sure query string doesn't have pluses or spaces
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);
//alert(google_kw);
//if ( google_kw.length > 0 )
//		OAS_query = OAS_query + '&' + google_kw + ' Homes for Sale Real Estate Mortgage Home Loan&' + google_kw_type;
//alert(OAS_listpos);
//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 */

