/*
	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, this is IMPORTANT!
// -------------------------------------
//var sNetworkID = '71.135';	// Test Network.
var sNetworkID = '701.1';	// Live Ad Network.

var sFallbackID = '0';
var sAlias = 'default_tower';
var disabled = false; // Set this to true for no banner to display

// Debug info:
// -----------
var debug_adtech = 0;

// Find Zone based on keywords
function smartZones3( regioncode ) {	
	var qString = new QueryString();
	
	var sKVLoc = 'KVLoc=';
	var sKVPos = 'KVPos=';
	var sKVInd = 'KVInd=';
	var sKVMan = 'KVMan=';
	var sKeywords = 'key=';
	
	var sKVs = '';

	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;
	}
	
	//set management, non-management or all
	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 += '_tower;';

	// Lets populate KVLoc with the top 8 location codes. 
	if( qString.Length('localAreaCodes') > 1 || 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/154/ADTECH;alias=' + sAlias + sKVs + 'loc=100;target=_blank;' + sKeywords + 'misc='+new Date().getTime()+'></scri'+'pt>';
	}

// Fallback placements for regions
if(sPath.lastIndexOf('hcareers.co.uk') > 0) {
	sAlias = 'UK-fallback-160x600';
	sFallbackID = '1469084';
} else if (sPath.lastIndexOf('hcareers.ca') > 0) {
	sAlias = 'CA-fallback-160x600';
	sFallbackID = '1475414';
} else {
	sAlias = 'US-fallback-160x600';
	sFallbackID = '1422227';
}

// US - general placements
if ( sPath.lastIndexOf('hcareers.com/') > 0 ) {
	sAlias = 'US-Homepage_tower'; /* new ad */
}
if ( sPath.lastIndexOf('hcareers.com/employer') > 0 ) {
	sAlias = 'US-EmployerHome_tower';
}
if ( sPath.lastIndexOf('hcareers.com/employer/rates') > 0 ) {
	sAlias = 'US-RecruitmentProducts_tower';
}
if ( sPath.lastIndexOf('hcareers.com/us/employer/faq') > 0 || sPath.lastIndexOf('hcareers.com/us/employers/faq') > 0 ||
		sPath.lastIndexOf('hcareers.com/us/seeker/faq') > 0 || sPath.lastIndexOf('hcareers.com/us/jobseekers/faq') > 0) {
	sAlias = 'US-FAQ_tower';
}		
if ( sPath.lastIndexOf('hcareers.com/us/abouthospitalityjobs') > 0 ) {
	sAlias = 'US-AboutHospitalityJobs_tower';
}
if ( sPath.lastIndexOf('hcareers.com/us/seeker/employerprofiles') > 0 ) {
	sAlias = 'US-EmployerProfiles_tower';
}
if ( sPath.lastIndexOf('hcareers.com/us/resourcecenter') > 0 ) {
	sAlias = 'US-ResourceCenter_tower';
}
if ( sPath.lastIndexOf('hcareers.com/us/seeker/careerevents') > 0 || sPath.lastIndexOf('hcareers.com/us/jobseekers/careerevents') > 0 ) {
	sAlias = 'US-Career-Events_tower';
}
if ( sPath.lastIndexOf('hcareers.com/us/salary-tools.html') > 0 ) {
	sAlias = 'US-ResourceCenter_tower';	// US-Salary-Center_tower
}
if ( sPath.lastIndexOf('hcareers.com/newsletter') > 0 ) {
	disabled = true;
}

// CA - general placements
if ( sPath.lastIndexOf('hcareers.ca') > 0 ) {
	sAlias = 'CA-Homepage_tower'; /* new ad */
}
if ( sPath.lastIndexOf('hcareers.ca/employer') > 0 ) {
	sAlias = 'CA-EmployerHome_tower';
}
if ( sPath.lastIndexOf('hcareers.ca/employer/rates') > 0 ) {
	sAlias = 'CA-RecruitmentProducts_tower';
}
if ( sPath.lastIndexOf('hcareers.com/ca/employer/faq') > 0 || sPath.lastIndexOf('hcareers.com/ca/employers/faq') > 0 ||
		sPath.lastIndexOf('hcareers.com/ca/seeker/faq') > 0 || sPath.lastIndexOf('hcareers.com/ca/jobseekers/faq') > 0 ) {
	sAlias = 'CA-FAQ_tower';
}
if ( sPath.lastIndexOf('hcareers.com/ca/abouthospitalityjobs') > 0 ) {
	sAlias = 'CA-AboutHospitalityJobs_tower';
}
if ( sPath.lastIndexOf('hcareers.ca/seeker') > 0 ) {
	sAlias = 'CA-JobSeekerHome_tower';
}
if ( sPath.lastIndexOf('hcareers.com/ca/seeker/employerprofiles') > 0 ) {
	sAlias = 'CA-EmployerProfiles_tower';
}
if ( sPath.lastIndexOf('hcareers.com/ca/resourcecenter') > 0 ) {
	sAlias = 'CA-ResourceCenter_tower';
}
if ( sPath.lastIndexOf('hcareers.com/ca/seeker/careerevents') > 0 || sPath.lastIndexOf('hcareers.com/ca/jobseekers/careerevents') > 0 ) {
	sAlias = 'CA-Career-Events_tower';
}
if ( sPath.lastIndexOf('hcareers.com/ca/salary-tools.html') > 0 ) {
	sAlias = 'CA-ResourceCenter_tower';	// CA-Salary-Center_tower
}
if ( sPath.lastIndexOf('hcareers.ca/newsletter') > 0 ) {
	disabled = true;
}

// UK - general placements
if ( sPath.lastIndexOf('hcareers.co.uk') > 0 ) {
	sAlias = 'UK-Homepage_tower';
}
if ( sPath.lastIndexOf('hcareers.co.uk/employer') > 0 ) {
	sAlias = 'UK-EmployerHome_tower';
}
if ( sPath.lastIndexOf('hcareers.co.uk/employer/rates') > 0 ) {
	sAlias = 'UK-RecruitmentProducts_tower';
}
if ( sPath.lastIndexOf('hcareers.com/uk/employer/faq') > 0 || sPath.lastIndexOf('hcareers.com/uk/employers/faq') > 0 ||
		sPath.lastIndexOf('hcareers.com/uk/seeker/faq') > 0 || sPath.lastIndexOf('hcareers.com/uk/jobseekers/faq') > 0 ) {
	sAlias = 'UK-FAQ_tower';
}
if ( sPath.lastIndexOf('hcareers.com/uk/abouthospitalityjobs') > 0 ) {
	sAlias = 'UK-AboutHospitalityJobs_tower';
}
if ( sPath.lastIndexOf('hcareers.co.uk/seeker') > 0 ) {
	sAlias = 'UK-JobSeekerHome_tower';
}
if ( sPath.lastIndexOf('hcareers.com/uk/seeker/employerprofiles') > 0 ) {
	sAlias = 'UK-EmployerProfiles_tower';
}
if ( sPath.lastIndexOf('hcareers.com/uk/resourcecenter') > 0 ) {
	sAlias = 'UK-ResourceCenter_tower';
}
if ( sPath.lastIndexOf('hcareers.com/uk/seeker/careerevents') > 0 || sPath.lastIndexOf('hcareers.com/uk/jobseekers/careerevents') > 0 ) {
	sAlias = 'UK-Career-Events_tower';
}
if ( sPath.lastIndexOf('hcareers.co.uk/newsletter') > 0 ) {
	disabled = true;
}

// US, CA, UK - post jobs
if (sPath.lastIndexOf('hcareers.com/employer/content/employer/jobs/demo_us_en.html') > 0) {
	sAlias='US-PostJobs_tower';
	sFallbackID = '1402781';
} else if (sPath.lastIndexOf('hcareers.ca/employer/content/employer/jobs/demo_ca_en.html') > 0) {
	sAlias='CA-PostJobs_tower';
	sFallbackID = '1399213';
} else if (sPath.lastIndexOf('hcareers.co.uk/employer/content/employer/jobs/demo_uk_en.html') > 0) {
	sAlias='UK-PostJobs_tower';
	sFallbackID = '1402814';
}

// US, CA, UK - search resumes
if (sPath.lastIndexOf('hcareers.com/employer/content/employer/search/demo_us_en.html') > 0) {
	sAlias='US-SearchResumes_tower';
	sFallbackID = '1402766';
} else if (sPath.lastIndexOf('hcareers.ca/employer/content/employer/search/demo_ca_en.html') > 0) {
	sAlias='CA-SearchResumes_tower';
	sFallbackID = '1399466';
} else if (sPath.lastIndexOf('hcareers.co.uk/employer/content/employer/search/demo_uk_en.html') > 0) {
	sAlias='UK-SearchResumes_tower';
	sFallbackID = '1402810';
}

// US, CA, UK - resume detective
if (sPath.lastIndexOf('hcareers.com/employer/content/employer/detective/demo_us_en.html') > 0) {
	sAlias='US-ResumeDetective_tower';
	sFallbackID = '1402768';
} else if (sPath.lastIndexOf('hcareers.ca/employer/content/employer/detective/demo_ca_en.html') > 0) {
	sAlias='CA-ResumeDetective_tower';
	sFallbackID = '1399223';
} else if (sPath.lastIndexOf('hcareers.co.uk/employer/content/employer/detective/demo_uk_en.html') > 0) {
	sAlias='UK-ResumeDetective_tower';
	sFallbackID = '1402804';
}

// US, CA, UK - inboxes
if (sPath.lastIndexOf('hcareers.com/employer/content/employer/inbox/demo_us_en.html') > 0) {
	sAlias='US-Inboxes_tower';
	sFallbackID = '1402773';
} else if (sPath.lastIndexOf('hcareers.ca/employer/content/employer/inbox/demo_ca_en.html') > 0) {
	sAlias='CA-Inboxes_tower';
	sFallbackID = '1399097';
} else if (sPath.lastIndexOf('hcareers.co.uk/employer/content/employer/inbox/demo_uk_en.html') > 0) {
	sAlias='UK-Inboxes_tower';
	sFallbackID = '1402807';
}

// US - hospitality job search pages
if ( sPath.lastIndexOf('hcareers.com/seeker/') > 0 ||
		sPath.lastIndexOf('hcareers.com/seeker/search/') > 0 ) {
	sAlias = 'US-JobSeekerHome_tower';
}	
if ( sPath.lastIndexOf('hcareers.com/seeker/search/advanced') > 0 ) {
	sAlias = 'US-Advanced-Search-Page_tower';
}
if ( sPath.lastIndexOf('hcareers.com/seeker/search/diversity') > 0 ) {
	sAlias = 'US-Diversity-Search_tower';
}
if ((sPath.lastIndexOf('hcareers.com/seeker/search/?') > 0) ||
(sPath.lastIndexOf('hcareers.com/seeker/search/advanced?') > 0)) {
	// All
	sAdUrl = smartZones3( 'US' );
}
if (sPath.lastIndexOf('hcareers.com/seeker/search/view') > 0) {
	sAlias = 'US-JobDetails_tower';
}

// CA - hospitality job results pages
if ( sPath.lastIndexOf('hcareers.ca/seeker/search') > 0 ) {
		sAlias = 'CA-JobSeekerHome_tower';
}
if ( sPath.lastIndexOf('hcareers.ca/seeker/search/advanced') > 0 ) {
		sAlias = 'CA-Advanced-Search-Page_tower';
}
if ( sPath.lastIndexOf('hcareers.ca/seeker/search/diversity') > 0 ) {
		sAlias = 'CA-Diversity-Search_tower';
}
if ((sPath.lastIndexOf('hcareers.ca/seeker/search/?') > 0) ||
(sPath.lastIndexOf('hcareers.ca/seeker/search/advanced?') > 0) ) {
	// All
	sAdUrl = smartZones3( 'CA' );
}
if (sPath.lastIndexOf('hcareers.ca/seeker/search/view') > 0) {
	sAlias = 'CA-JobDetails_tower';
}

// UK - hospitality job results pages
if ( sPath.lastIndexOf('hcareers.co.uk/seeker/search') > 0 ) {
	sAlias = 'UK-Advanced-Search-Page_tower';
}
if ( sPath.lastIndexOf('hcareers.co.uk/seeker/search/advanced') > 0 ) {
	sAlias = 'UK-Advanced-Search-Page_tower';
}
if ((sPath.lastIndexOf('hcareers.co.uk/seeker/search/?') > 0) ||
		(sPath.lastIndexOf('hcareers.co.uk/seeker/search/advanced?') > 0) ) {
	sAdUrl = smartZones3( 'UK' );
}
if (sPath.lastIndexOf('hcareers.co.uk/seeker/search/view') > 0) {
	sAlias = 'UK-JobDetails_tower';
}

// US - industry pages - don't display a side banner
if (sPath.lastIndexOf('hcareers.com/us/industry/') > 0) {	// Any page with /us/industry/ is caught here
	disabled = true;
}

// CA - industry pages - don't display a side banner
if (sPath.lastIndexOf('hcareers.com/ca/industry/') > 0) {	// Any page with /ca/industry/ is caught here
	disabled = true;
}


// UK - industry pages
if (sPath.lastIndexOf('hcareers.com/uk/industry/attractions') > 0) {
	sAlias = 'UK-TOP-Zones-Attractions_tower';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/foodservice') > 0) {
	sAlias = 'UK-TOP-Zones-Foodservice_tower';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/hotelcasino') > 0) {
	sAlias = 'UK-TOP-Zones-Casino_tower';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/restaurantbarnightclub') > 0) {
	sAlias = 'UK-TOP-Zones-Restaurant-Bar-Nightclub_tower';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/recruitment') > 0) {
	sAlias = 'UK-TOP-Zones-Recruitment-Companies_tower';
}
if (sPath.lastIndexOf('hcareers.com/uk/industry/timeshare') > 0) {
	sAlias = 'UK-TOP-Zones-Timeshare_tower';
}
if (sPath.lastIndexOf('www.hcareers.com/uk/industry/travel') > 0) {
	sAlias = 'UK-TOP-Zones-Travel_tower';
}

// HcareersCampus
if (sPath.lastIndexOf('hcareerscampus.com') > 0) {
	sAlias = 'US-Campus-Homepage_tower';	//If there's no subdomain then use the US banner
}
if (sPath.lastIndexOf('us.hcareerscampus.com') > 0) {
	sAlias = 'US-Campus-Homepage_tower';
}
if (sPath.lastIndexOf('ca.hcareerscampus.com') > 0) {
	sAlias = 'CA-Campus-Homepage_tower';
}
if (sPath.lastIndexOf('uk.hcareerscampus.com') > 0) {
	sAlias = 'UK-Campus-Homepage_tower';
}

if(debug_adtech) { alert('DEBUG: [rightAd->sPath]: ' + sPath); }

// Build the url if it's one of our static aliases, otherwise it's a dynamic one.
if ( (sAdUrl == '') && (disabled == false) ) {
	sAdUrl = '<scr'+'ipt language="javascript1.1" src=http://atimages.hcareers.com/addyn/3.0/' + 
		sNetworkID + '/' + 
		sFallbackID + '/0/154/ADTECH;alias=' + 
		sAlias + ';loc=100;target=_blank;misc='+ 
		new Date().getTime()+'></scri'+'pt>';
	
	if(debug_adtech) { alert('DEBUG: [rightAd->sAdUrl]: ' + sAdUrl) }
}

// Show the ad if it exists.
if ((sAdUrl != '') && (disabled == false)) {
	document.write(sAdUrl);
}

