/*
	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 = [];
	}
}

// Variables defining global AdTech settings.
// ------------------------------------------
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_top';

var debug_adtech = 0;

// Find Zone based on keywords
function smartZones1( 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 += '_top;';

	// 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/225/ADTECH;alias=' + sAlias + sKVs + 'loc=100;target=_blank;' + sKeywords + 'misc='+new Date().getTime()+'></scri'+'pt>';
}


// Set the fallback placement for each region
if(sPath.lastIndexOf('hcareers.co.uk') > 0) {
	sAlias = 'UK-fallback-728x90-top';
	sFallbackID = '1475412';
} else if (sPath.lastIndexOf('hcareers.ca') > 0) {
	sAlias = 'CA-fallback-728x90-top';
	sFallbackID = '1475413';
} else {
	sAlias = 'US-fallback-728x90-top';
	sFallbackID = '1475411';
}

// hospitality job results for US
if ( sPath.lastIndexOf('hcareers.com/employer') > 0 ) {
	sAlias = 'US-EmployerHome_top';
}
if ( sPath.lastIndexOf('hcareers.com/seeker/search/advanced') > 0 ) {
	sAlias = 'US-Advanced-Search-Page_top';
}
if ( sPath.lastIndexOf('hcareers.com/seeker/search/diversity') > 0 ) {
	sAlias = 'US-Diversity-Search_top';
}
if ((sPath.lastIndexOf('hcareers.com/seeker/search/?') > 0) ||
		(sPath.lastIndexOf('hcareers.com/seeker/search/advanced?') > 0)) 
{
	// All
	sAdUrl = smartZones1( 'US' );
}
// hospitality job results for CA
if ( sPath.lastIndexOf('hcareers.ca/employer') > 0 ) {
	sAlias = 'CA-EmployerHome_top';
}
if ( sPath.lastIndexOf('hcareers.ca/seeker/search/advanced') > 0 ) {
	sAlias = 'CA-Advanced-Search-Page_top';
}
if ( sPath.lastIndexOf('hcareers.ca/seeker/search/diversity') > 0 ) {
	sAlias = 'CA-Diversity-Search_top';
}
if ((sPath.lastIndexOf('hcareers.ca/seeker/search/?') > 0) ||
		(sPath.lastIndexOf('hcareers.ca/seeker/search/advanced?') > 0))
{
	sAdUrl = smartZones1( 'CA' );
}
// hospitality job results for UK
if ( sPath.lastIndexOf('hcareers.co.uk/seeker/search') > 0 ) {
	sAlias = 'UK-Advanced-Search-Page_top';		
}
if ( sPath.lastIndexOf('hcareers.co.uk/seeker/search/advanced') > 0 ) {
	sAlias = 'UK-Advanced-Search-Page_top';
	sFallbackID = '1402599';
}
if ((sPath.lastIndexOf('hcareers.co.uk/seeker/search/?') > 0) ||
		(sPath.lastIndexOf('hcareers.co.uk/seeker/search/advanced?') > 0)) 
{
	sAdUrl = smartZones1( 'UK' );
}

if(debug_adtech) { alert('DEBUG: [headerAd->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/225/ADTECH;alias=' + sAlias + ';loc=100;target=_blank;misc='+new Date().getTime()+'></scri'+'pt>';
	
	if(debug_adtech) { alert('DEBUG: [headerAd->sAdUrl]: ' + sAdUrl) }
}

// Show ads if there is a alias
if (sAdUrl != '') {
	document.write(sAdUrl);
}
