/*
	Copyright (c) 2000, Derek Petillo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without 
	modification, are permitted provided that the following conditions are
	met:

	Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer. 
	
	Redistributions in binary form must reproduce the above copyright 
	notice, this list of conditions and the following disclaimer in the 
	documentation and/or other materials provided with the distribution. 
	
	Neither the name of Praxis Software nor the names of its contributors 
	may be used to endorse or promote products derived from this software 
	without specific prior written permission.
	 
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
	IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
	OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
function QueryString() {
	var data = [];
	this.Read = function() 
	{
		var aPairs, aTmp;
		var queryString = new String(window.location.search);
		queryString = queryString.substr(1, queryString.length); //remove "?"
		aPairs = queryString.split("&");	
		
		for (var i=0 ; i<aPairs.length; i++)
		{
			aTmp = aPairs[i].split("=");
			if(data[aTmp[0]] == undefined) {
				data[aTmp[0]] = aTmp[1];
			} else {
				data[aTmp[0]] += ',' + aTmp[1];
			}
		}
	}
	
	this.GetValue = function( key )
	{
		return data[key];
	}
	
	this.GetValue = function( key, index )
	{
		if( index == undefined ) {
			return data[key];	
		} else  {
			if( data[key] != undefined ) {
				return data[key].split(',')[index];
			}
			
			return undefined;
		}
	}
	
	this.hasValue = function( key )
	{
		var aTemp; 
		if( data[key] != undefined ) {
			aTemp = data[key].split("=").toString();
			if (aTemp.length > 1) {
				return true;
			}
		}
		return false;
	}
	
	this.IsMulti = function ( key ) {		
		if ( key != undefined && data[key] != undefined && data[key].indexOf(',') != -1 ) {
			return true;		
		}
		
		return false;
	}
	
	this.Length = function ( key ) {
		if ( this.IsMulti( key ) ) {
			return data[key].split(',').length;
		} else if ( data[key] != undefined ) {
			return 1;
		} else {
			return 0;
		}
	}
	
	this.SetValue = function( key, value )	
	{
		if (value == null)
			delete data[key];
		else 
			data[key] = value;
	}
		
	this.ToString = function()
	{
		var queryString = new String(""); 
		
		for (var key in data)
		{	
			if (queryString != "")
				queryString += "&"
			if (data[key])
				queryString += key + "=" + data[key];		
		}
		if (queryString.length > 0)
			return "?" + queryString;
		else
			return queryString;
	}
	this.Clear = function()
	{
		delete data;
		data = [];
	}
}


// Global AdTech settings for this banner position.
// ------------------------------------------------
var sPath = window.location.href.toLowerCase();
var searchPath = window.location.search.toLowerCase();
var sAdUrl = ''

// AdTech network ID, very IMPORTANT!
// ----------------------------------
//var sNetworkID = '71.135';	// Test Network.
var sNetworkID = '701.1';	// Live Network.

var sAlias = 'CA-Homepage-default-250 x 250';
var sFallbackID = '0';

var debug_adtech = 0;

// Find Zone based on keywords
function smartZones2( regioncode ) {	
	var qString = new QueryString();
	
	var sKVLoc = 'KVLoc=';
	var sKVPos = 'KVPos=';
	var sKVInd = 'KVInd=';
	var sKVMan = 'KVMan=';
	
	var sKVs = '';

	var sKeywords = 'key=';

	qString.Read();
	
	if (qString.GetValue('localAreaCodes') == '_true') {
		sAlias = 'L'+regioncode;
	} else if (qString.GetValue('intlAreaCodes') == '_true') {
		sAlias = 'LALL';
	} else if (qString.hasValue('localAreaCodes') && qString.GetValue('localAreaCodes', 0) != null) {
		sAlias = qString.GetValue('localAreaCodes', 0);
	} else if (qString.GetValue('intlAreaCodes', 0) != null) {
		sAlias = qString.GetValue('intlAreaCodes', 0);
	} else {
		sAlias = 'L'+regioncode;
	}
	
     if (qString.GetValue('management') == 't') {
		sAlias += '_mgmt';
		sKVMan += 'yes;';
	} else if (qString.GetValue('management') == 'f') {
		sAlias += '_non-mgmt';
		sKVMan += 'no;';
	//Quick search parameters
	} else if ( (qString.Length('management') == 0 && qString.hasValue('managementPositions') == true && qString.hasValue('hourlyPositions') == false ) ){
		sAlias += '_mgmt';
		sKVMan += 'yes;';
	} else if ( (qString.Length('management') == 0 && qString.hasValue('hourlyPositions') == true && qString.hasValue('managementPositions') == false ) ) {
		sAlias += '_non-mgmt';
		sKVMan += 'no;';
	} else {
		sAlias += '_all-mgmt';
		sKVMan += 'all;';
	}
	
	sAlias += '_bottom;';

	// Lets populate KVLoc with the top 8 location codes.
	if( qString.Length('localAreaCodes') > 0 || qString.Length('intlAreaCodes') > 0 ) {
		var lLen = qString.Length( 'localAreaCodes');				
		var iLen = qString.Length('intlAreaCodes');
		
		if (qString.GetValue('localAreaCodes') == '_true') {
			sKVLoc += 'L'+regioncode+';';
		}
		else if (qString.GetValue('intlAreaCodes') == '_true') {
			sKVLoc += 'LALL;'
		}
		else
		{
			for ( var i = 0; i < 8; i++ ) {
				if ( i < lLen ) {
					sKVLoc += qString.GetValue( 'localAreaCodes', i) + ':';						
				} else if ( iLen > 0 && i < lLen + iLen ) {					
					sKVLoc += qString.GetValue( 'intlAreaCodes', i - lLen ) + ':';
				} else {
					break;
				}
			}
			
			sKVLoc = sKVLoc.substring( 0, sKVLoc.length - 1) + ';';
		}
	}	
	
	// Lets populate the top 8 industry codes.
	if( qString.Length('industryCodes') > 0 && qString.GetValue('industryCodes',0) > 0) {
		var indLength = 8;
		var keyLength = 4;
		var rawCodes = 'I' + qString.GetValue('industryCodes').replace( /,/g, ',I' );
		var indCodes = rawCodes.split(',');

		if ( qString.Length('industryCodes') < indLength ) {
			indLength = qString.Length('industryCodes');
		}

		if ( qString.Length('industryCodes') < keyLength ) {
			keyLength = qString.Length('industryCodes');
		}
		
		for ( i = 0; i < indLength; i++ ) {
			sKVInd += indCodes[i];

			if ( i < indLength - 1 ) {
				sKVInd += ':';
			}

			if ( i < keyLength ) {
				sKeywords += indCodes[i];

				if ( i < keyLength - 1 ) {
					sKeywords += '+';
				}
			}
		}

		sKVInd += ';';
		sKeywords += ';';
	} else { // no industry codes = 0
		sKVInd += 'I0;'; 
	}
	
	// Lets populate the KVPos with the top 8 position codes.
	if( qString.Length('allPositionCodes') > 0 || qString.Length('managementPositions') > 0 || qString.Length('hourlyPositions') > 0 ) {
		var aLen = qString.Length('allPositionCodes');				
		var mLen = qString.Length('managementPositions');
		var hLen = qString.Length('hourlyPositions');
		
		for ( var i = 0; i < 8; i++ ) {
			if ( aLen > 0 && i < aLen ) {
				sKVPos += 'P' + qString.GetValue( 'allPositionCodes', i).substring(1) + ':';						
			} else if ( mLen > 0 && i < aLen + mLen ) {
				sKVPos += 'P' + qString.GetValue( 'managementPositions', i - aLen ).substring(1) + ':';
			} else if ( hLen > 0 && i < aLen + mLen + hLen ) {
				sKVPos += 'P' + qString.GetValue( 'hourlyPositions', i - (aLen  + mLen)).substring(1) + ':';
			} else {
				break;
			}
		}
		
		sKVPos = sKVPos.substring( 0, sKVPos.length - 1) + ';';
	}
	else // no position codes = 0
	{
		sKVPos += 'P0;';
	}
	
	sKVs += sKVLoc + sKVMan + sKVInd + sKVPos;
	
	return '<scr'+'ipt language="javascript1.1" src=http://atimages.hcareers.com/addyn/3.0/' + sNetworkID + '/0/0/165/ADTECH;alias=' + sAlias + sKVs + 'loc=100;target=_blank;' + sKeywords + 'misc='+new Date().getTime()+'></scri'+'pt>';
}

// Lets set our catch all, and then see if we can make it more specific.
if(sPath.lastIndexOf('hcareers.co.uk') > 0) {
	sAlias = 'US-TOP_Zones_Attractions_bottom_250';
	sFallbackID = '1469805';
} else if (sPath.lastIndexOf('hcareers.ca') > 0) {
	sAlias = 'CA-TOP_Zones_Attractions_bottom_250';
	sFallbackID = '1475415';
} else {
	sAlias = 'US-TOP_Zones_Attractions_bottom_250';
	sFallbackID = '1422225';
}

// US - general pages
if ( sPath.lastIndexOf('hcareers.com') > 0 ) {
	sAlias = 'US-TOP_Zones_Attractions_bottom_250'; /* new ad */
}

// CA - general pages
if( sPath.lastIndexOf('hcareers.ca') > 0 ) {
	sAlias = 'CA-TOP_Zones_Attractions-bottom-250'; /* new ad */
}


// UK - general pages (no UK yet)
//if ( sPath.lastIndexOf('hcareers.co.uk') > 0 ) {
//	sAlias = 'UK-Homepage_bottom';
//}

// US - industry pages
if (sPath.lastIndexOf('hcareers.com/us/industry/attractions') > 0) {
	sAlias = 'US-TOP_Zones_Attractions_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/us/industry/foodservice') > 0) {
	sAlias = 'US-TOP_Zones_Foodservice_bottom_250'; /* new ad */
}		
if (sPath.lastIndexOf('hcareers.com/us/industry/hotel') > 0) {			//Changed from /hotelcasino
	sAlias = 'US-TOP_Zones_Hotel_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/us/industry/recruitment') > 0) {
	sAlias = 'US-TOP_Zones_Recruitment_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/us/industry/restaurantbarnightclub') > 0) {
	sAlias = 'US-TOP_Zones_Restaurant_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/us/industry/timeshare') > 0) {
	sAlias = 'US-TOP_Zones_Timeshare_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/us/industry/travel') > 0) {
	sAlias = 'US-TOP_Zones_Travel_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/us/industry/retirement') > 0) {
	sAlias = 'US-TOP_Zones_Retirement_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/us/industry/clubindustry') > 0) {
	sAlias = 'US-TOP_Zones_Golf_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/us/industry/casino') > 0) {
	sAlias = 'US-TOP_Zones_Casino_bottom_250'; /* new ad */
}

// CA - industry pages
if (sPath.lastIndexOf('hcareers.com/ca/industry/attractions') > 0) {
	sAlias = 'CA-TOP_Zones_Attractions-bottom-250'; /* new ad */
}			
if (sPath.lastIndexOf('hcareers.com/ca/industry/foodservice') > 0) {
	sAlias = 'CA-TOP_Zones_Foodservice-bottom-250'; /* new ad */
}	
if (sPath.lastIndexOf('hcareers.com/ca/industry/hotel') > 0) {	//Changed from /hotelcasino
	sAlias = 'CA-TOP_Zones_Hotel_bottom_250'; /* new ad */
}			
if (sPath.lastIndexOf('hcareers.com/ca/industry/restaurantbarnightclub') > 0) {
	sAlias = 'CA-TOP_Zones_Restaurant-bottom-250'; /* new ad */
}	
if (sPath.lastIndexOf('hcareers.com/ca/industry/timeshare') > 0) {
	sAlias = 'CA-TOP_Zones_Timeshare-bottom-250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/ca/industry/travel') > 0) {
	sAlias = 'CA-TOP_Zones_Travel-bottom-250'; /* new ad */
}	
if (sPath.lastIndexOf('hcareers.com/ca/industry/recruitment') > 0) {
	sAlias = 'CA-TOP_Zones_Recruitment-bottom-250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/ca/industry/retirement') > 0) {
	sAlias = 'CA-TOP_Zones_Retirement-bottom-250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/ca/industry/clubindustry') > 0) {
	sAlias = 'CA-TOP_Zones_Golf_bottom_250'; /* new ad */
}
if (sPath.lastIndexOf('hcareers.com/ca/industry/casino') > 0) {
	sAlias = 'CA-TOP_Zones_Casino-bottom-250'; /* new ad */
}

// UK - industry pages (no support yet)
/*
if (sPath.lastIndexOf('hcareers.com/uk/industry/attractions') > 0) {
	sAlias = 'UK-TOP-Zones-Attractions_bottom';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/foodservice') > 0) {
	sAlias = 'UK-TOP-Zones-Foodservice_bottom';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/hotelcasino') > 0) {
	sAlias = 'UK-TOP-Zones-Casino_bottom';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/recruitment') > 0) {
	sAlias = 'UK-TOP-Zones-Recruitment-Companies_bottom';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/timeshare') > 0) {
	sAlias = 'UK-TOP-Zones-Timeshare_bottom';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/restaurantbarnightclub') > 0) {
	sAlias = 'UK-TOP-Zones-Restaurant-Bar-Nightclub_bottom';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/travel') > 0) {
	sAlias = 'UK-TOP-Zones-Travel_bottom';
	*/

if(debug_adtech) { alert('DEBUG: [footerAd->sPath]: ' + sPath); }

// Build the url if it's one of our static aliases, otherwise it's a dynamic one.
if ( sAdUrl == '' ) {
	sAdUrl = '<scr'+'ipt language="javascript1.1" src=http://atimages.hcareers.com/addyn/3.0/' + sNetworkID +  '/0/0/165/ADTECH;alias=' + sAlias + ';loc=100;target=_blank;misc='+new Date().getTime()+'></scri'+'pt>';

	if(debug_adtech) { alert('DEBUG: [footerAd->sAdUrl]: ' + sAdUrl) }
}

// Show ads if there is a alias
if (sAdUrl != '') {
	document.write(sAdUrl);
}

