
// Global Javascript Variables For HBX Campaign vars
var StrAidVal;
var strADV;

function insertParam(whichHC, param, value, prefix) {
	//set string for pipe or comma for use later
	pipe = "|";
	comma = ",";
	
	//test prefix and set default value if needed
	if(prefix == "" || prefix == undefined)
		prefix = "hbx.";
		
	//test to make sure HBX var exists
	if(eval(prefix + whichHC) == undefined)
		return false;
	
	
	//first test string for illegal chars
	if(validate(value) == true)
	{
		//now test if param is empty
		if(param == "")
		{ // we are not inserting a custom metric so just assign the value
			eval(prefix + whichHC +" = value");
		}
		else {
			// we are inserting a custom metric now
			
			//test to see if this is the first param to put in
			if(eval(prefix + whichHC) == "")
			{		
				eval(prefix + whichHC +" = param+pipe+value");
			} else {
				//other params have been put in so add a comma on insert
				eval(prefix + whichHC +" = comma+param+pipe+value");
			}			
		}
		return true;
	}
	if(whichHC == constHBXmlc)
	{
		if(validate_mlc(value) == true)
		{
			//now test if param is empty
			if(param == "")
			{ // we are not inserting a custom metric so just assign the value
				eval(prefix + whichHC +" = value");
			}
			else {
				// we are inserting a custom metric now
				
				//test to see if this is the first param to put in
				//test for undefined as well
				if(eval(prefix + whichHC) == "")
				{		
					eval(prefix + whichHC +" = param+pipe+value");
				} else {
					//other params have been put in so add a comma on insert
					eval(prefix + whichHC +" = comma+param+pipe+value");
				}			
			}
			return true;
		}
	}
	return false;
}

function validate(value) {
	// create array to hold all illegal chars
	regexp = new Array("!","#","$","%","&","\'","*","\/",":",";","<",">","\\","^","|","~");
	// at first instance of invalid char false is returned
	for(i = 0; i < regexp.length; i++) {
		if(value.indexOf(regexp[i]) >= 0) {
			//illegal char found
			return false;
		}
	}	
	// everything is ok so return true
	return true;
}

function validate_mlc(value) {
	// create array to hold all illegal chars
	regexp = new Array("!","#","$","%","&","\'","*",":",";","<",">","\\","^","|","~");
	// at first instance of invalid char false is returned
	for(i = 0; i < regexp.length; i++) {
		if(value.indexOf(regexp[i]) >= 0) {
			//illegal char found
			return false;
		}
	}	
	// everything is ok so return true
	return true;
}



function getHBXorderid() {
	return 1;
}

// this function sets the account info according to the bIsProduction var in constHBXvar.js file
function setAccountNumber(prefix) {
	//test prefix and set default value if needed
	if(prefix == "" || prefix == undefined)
		prefix = "hbx.";
	//test to make sure HBX var exists
	if(eval(prefix + constHBXacct) == undefined)
		return false;
	if(bIsProduction == true) {
		eval(prefix + constHBXacct + " = constProductionAccountNumber");
		if(eval(prefix + constHBXcacct) != undefined) {
			eval(prefix + constHBXcacct + " = constCommerceProductionAccountNumber");
		}
	} else {
		eval(prefix + constHBXacct + " = constTestAccountNumber");
		if(eval(prefix + constHBXcacct) != undefined) {
			eval(prefix + constHBXcacct + " = constCommerceTestAccountNumber");
		}
	}
}


/*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Author: Dov Goldberg
'' Function Name: getParamPriortyHBX
'' Function Params: QueryString as String
'' Function Returns: Name of param with highest priority or "" if no params are found
'' Logic:  Function retrieves querystring array
''         uses a 2 dim array 
''             first dim is name of params
''             second dim is priorty value
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
function getParamPriortyHBX(QueryString) {
	
	//'' Dim our Priority Array
	var arrPriority = new Array(2);
	var currentPriority = -1;
	var arrQueryString;
	var delim = "|";
	var rtValue;
	var i,j;
	
	
	//'' Set function to return empty string
	rtValue = "";
	
	//'' Store names of params
	arrPriority[0] = new Array(2);
	arrPriority[1] = new Array(2);
	arrPriority[0][0] = "AID";
	arrPriority[0][1] = "SUAID";
	
	//'' Store priority levels
	arrPriority[1][0] = "2";
	arrPriority[1][1] = "1";
	
	//'' Check to make sure QueryString is not empty or null
	if(QueryString != "" && (QueryString != null || QueryString != undefined)) {
		//'' Convert QueryString to Uppercase
		QueryString = QueryString.toUpperCase();
		arrQueryString = QueryString.split(delim);
		//'' Loop through array and search for any params
		for(i=0;i < arrPriority.length; i++) {
		//For i = 1 to UBound(priority)
			for(j=0;j < arrQueryString.length; j++) {
			//For j = 0 to UBound(arrQueryString)
				if(arrQueryString[j] == arrPriority[0][i]) { 
					if(parseInt(arrPriority[1][i]) < currentPriority || currentPriority == -1) {
						currentPriority = parseInt(arrPriority[1][i]);
						rtValue = arrPriority[0][i];
						//alert('Priority: ' + rtValue);
					}
				}
			}
		}				
	}
	return rtValue;
}

function setCampParams() {
	
	var sAgent = "";
	var strParam;
	
	StrAidVal = GetDefaultAID();
	
	if(unescape(Trim(utils_RetrieveCookieValue(unescape(document.cookie),"PersonalInfo","AID"))) != "") {
		sAgent = "aid";
	}
	
	if(unescape(Trim(utils_RetrieveCookieValue(unescape(document.cookie),"PersonalInfo","SUAID"))) != "") {
		sAgent = sAgent + "|SUAID";
	}
	
	strParam = getParamPriortyHBX(sAgent);	
	//alert("priority " + strParam);
	if(strParam != "") {
		//'Consts_ClickThru_COOKIECOLLECTION check if this has a value
		if(unescape(Trim(utils_RetrieveCookieValue(unescape(document.cookie),"DL3CLKTHRU",strParam))) != "") {
			StrAidVal = unescape(Trim(utils_RetrieveCookieValue(unescape(document.cookie),"DL3CLKTHRU",strParam)));
		} else {
			StrAidVal = unescape(Trim(utils_RetrieveCookieValue(unescape(document.cookie),"PersonalInfo",strParam)));
		}		
	}
	
	strADV = unescape(Trim(utils_RetrieveCookieValue(unescape(document.cookie),"DL3CLKTHRU","ADV")));
	if(strADV == "") {
		strADV = unescape(Trim(utils_RetrieveCookieValue(unescape(document.cookie),"PersonalInfo","ADV")));
		if(strADV == "") {
			strADV = GetDefaultADV();
		}
	}
	//alert(StrAidVal);
	//alert(strADV);
}

function GetDefaultAID() {
	return "3";
}

function GetDefaultADV() {
	return "N/A";
}

function Trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function utils_RetrieveCookieValue(CookiesStr,NameOfCollection,NameOfValue){

	//LOGIC:FUNCTION EXPECTS TO RECEIVE:
	//		[1]unescaped COOKIE STR
	//		[2]NO NAME-VALUE PAIRS WITH EMPTY STRING

	//DEFAULTS
	var counter = 0;
	var bHasEnd = false;
	var valCookie = "";
	var intPosValue = 0;

	//DATA
	var searchStr = CookiesStr;
	var lenSearchStr = searchStr.length;

	//IF COLLECTION REPLACE searchStr WITH COLLECTION SUBSTRING
	if(NameOfCollection != ""){
		searchStr = utils_RetrieveCollString(searchStr,NameOfCollection)
		lenSearchStr = searchStr.length;
	}

	//IF VALUE EXISTS
	if(searchStr.indexOf(NameOfValue) != -1){
		intPosValue = searchStr.indexOf(NameOfValue) + (NameOfValue.length+1);
		for(counter=intPosValue;counter<=lenSearchStr;counter++){
			bHasEnd = (counter>intPosValue) && ((searchStr.charAt(counter)=="&") || (searchStr.charAt(counter)==";"));
			if(bHasEnd){
				break;
			}
			else{
				valCookie=valCookie + searchStr.charAt(counter);
			}
		}
	}
	return valCookie;
}

function utils_RetrieveCollString(strCookies,strNameOfCollection){

	var strRetVal = "";	
	var strTemp = "";
	var intNameLen = strNameOfCollection.length;
	var intPosCol;
	var intPosOfEndOfCol;

	//NAME IS IN STRING
	if((intPosCol = strCookies.indexOf(strNameOfCollection)) != -1){
		//NAME IS NOT MEMBER OF OTHER COLLECTION
		if((intPosCol!=0) && (strCookies.charAt(intPosCol-1) == "=" || strCookies.charAt(intPosCol-1) == "&")){
			strRetVal = "";
		}
		else{
			//GET FROM BEGINNING OF NAME TO END OF COOKIE
			strRetVal = strCookies.substr(intPosCol,strCookies.length - intPosCol);
			//IF END OF COOKIE ISN'T END OF COLLECTION
			if((intPosOfEndOfCol = strRetVal.indexOf(";")) != -1){
				strRetVal = strRetVal.substr(0,intPosOfEndOfCol);
			}
			//IF HAS NO MEMBERS THEN ISN'T COLLECTION
			strTemp = strRetVal.substr(intNameLen + 1,strRetVal.length);
			if(strTemp.indexOf("=") == -1){
				strRetVal = "";
			}
		}
	}
	return strRetVal;
}


