function GetLocalCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;

	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {return getCookieVal (j); }
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) { break; }
	}
	return null;
}

var favoritequova = GetLocalCookie('q');

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {endstr = document.cookie.length;}
	return unescape(document.cookie.substring(offset, endstr));
}

function toUnhexedString(value){
	// 'hexString' is a copy of 'value'
	var hexString = new String( value );
	// 'result' will be the "unhexed" string
	var result = "";

	// add a "0" to the front of the hex string if it does not have an even
	// number of hex characters
	if( hexString.length % 2 != 0 )
		hexString = "0" + hexString;

	// "unhex" the string
	for( var index = 0; index < hexString.length; index += 2 )
		result += String.fromCharCode( parseInt( hexString.substring( index, index + 2 ), 16 ) );

	return result;
}

//****************  Code starts

var agt=navigator.userAgent.toLowerCase();
if(agt.indexOf("aol") != -1) {
	var partner = "AOL"; 
}
else {
	var partner = "UNIVISION";
}

var isCobrand = GetLocalCookie('aoluolsite');

if(isCobrand == 'AOL') {
	var client = "&CLIENT=AOL"; 
}
else {
	var client = "";
}

var favorite = GetLocalCookie('d');
var unhexed = toUnhexedString(favorite);
var arr1 = unhexed.split('|');

var unhexedquova = toUnhexedString(favoritequova);
var arr2 = unhexedquova.split('|');

var favoritezip = GetLocalCookie('z');
var unhexedzip = toUnhexedString(favoritezip);
var arr3 = unhexedzip.split('|');

var gender = '';
var ageGroup = '';
var countryResidence = '';
var countryOrigin = '';
var dma = '';
var zipCode2 = '';
var zipCode3 = '';
var state = '';
var timeZone = '';
var areaCode = '';
var majorMetroCity = '';
var aolIndicator = '';
var daypart = '';
var lineSpeed = '';

if (favoritezip!=null)
{
	for(var k=0; k < arr3.length; k++){
		var tempzip = arr3[k];

		//ZIP code -- 3-digit
		if (tempzip!=null || tempzip!='') {
			var zipCode3 = '&Z3=' + tempzip.substring(0, 3);
		}

		//ZIP code -- 2-digit
		if (tempzip!=null || tempzip!='') {
			var zipCode2 = '&Z2=' + tempzip.substring(0, 2);
		}

	}
}

for(var i=0; i < arr1.length; i++){
	var temp = arr1[i];
	//Gender
	if(temp.indexOf('P=')!=-1) {
		var rawGender = temp.substring(2, temp.length); 
		var gender = '&P=' + rawGender;
	}

	//Age group
	if(temp.indexOf('A=')!=-1) {
		var rawAgeGroup = temp.substring(2, temp.length); 
		var ageGroup = '&A=' + rawAgeGroup;
	}

	//Country of residence
	if(temp.indexOf('R=')!=-1) {
		if(temp.substring(2, temp.length) != 'XX') {		
			var rawCountryResidence = temp.substring(2, temp.length);
			//var countryResidence = '&CR=' + rawCountryResidence;
		}
	}

	//Country of origin
	if(temp.indexOf('C=')!=-1) {
		if(temp.substring(2, temp.length) != 'XX') {		
			var rawCountryOrigin = temp.substring(2, temp.length);
			var countryOrigin = '&CO=' + rawCountryOrigin;
		}
	}

	//DMA
	if(temp.indexOf('D=')!=-1 && temp.substring(2, temp.length)!='0') {	
			var rawDma = temp.substring(2, temp.length);
			var dma = '&D=' + rawDma;
	}

	//Area code
	if(temp.indexOf('H=')!=-1) {
		var rawAreaCode = temp.substring(2, temp.length); 
		var areaCode = '&H=' + rawAreaCode;
	}

	//Major Metro City
	if(temp.indexOf('M=')!=-1) {
		var rawMajorMetroCity = temp.substring(2, temp.length).split(' ').join(''); 
		var majorMetroCity = '&MM=' + rawMajorMetroCity;
	}
}

for(var i=0; i < arr1.length; i++){
	var temp = arr1[i];

	//State
	if(temp.indexOf('S=')!=-1) {
		if (rawCountryResidence=='223' && temp.substring(2, temp.length)!='XX') {
			var rawState = temp.substring(2, temp.length);
			var state = '&S=' + rawState;
		}
	}	

	//ZIP code 3-digit
	if(zipCode3=='' && rawCountryResidence=='223') {
		if(temp.indexOf('Z=')!=-1) {
			var rawZipCode3 = temp.substring(2, 5);
			var zipCode3 = '&Z3=' + rawZipCode3;
		}
	}

	//ZIP code 2-digit
	if(zipCode2=='' && rawCountryResidence=='223') {
		if(temp.indexOf('Z=')!=-1) {
			var rawZipCode2 = temp.substring(2, 4);
			var zipCode2 = '&Z2=' + rawZipCode2;

		}
	}	

}

for(var j=0; j < arr2.length; j++){
	var tempquova = arr2[j];

	//Country of residence
	if(tempquova.indexOf('C=')!=-1) {
		var cIndicator = tempquova.substring(2, tempquova.length);
		//if(countryResidence=='') {
			var countryResidence = '&CR=' + tempquova.substring(2, tempquova.length);
		//}
	}

	//Time zone
	if(tempquova.indexOf('T=')!=-1) {
		if(tempquova.indexOf('SRT=')==-1) {
		var tempTime = tempquova.substring(2, tempquova.length);

		if (partner=='AOL')
		{
		if(tempTime=='-5.0') {var zoneValue = 'EAOL';}
		else if(tempTime=='-6.0') {var zoneValue = 'CAOL';}
		else if(tempTime=='-7.0') {var zoneValue = 'MAOL';}
		else if(tempTime=='-8.0') {var zoneValue = 'PAOL';}
		else if((tempTime=='9999') && (rawCountryResidence == '223')) { var zoneValue = 'EAOL'; }
		else {var zoneValue = tempTime; }
		}
		else
		{
		if(tempTime=='-5.0') {var zoneValue = 'E';}
		else if(tempTime=='-6.0') {var zoneValue = 'C';}
		else if(tempTime=='-7.0') {var zoneValue = 'M';}
		else if(tempTime=='-8.0') {var zoneValue = 'P';}
		else {var zoneValue = tempTime; }
		}

		var timeZone = '&T=' + zoneValue;
		}
	}

	//Linespeed
	if((tempquova.indexOf('L=')!=-1) && (tempquova.indexOf('AO')==-1) && (tempquova.indexOf('TL')==-1) && (tempquova.indexOf('SL')==-1)) {
		var lineSpeed = '&L=' + tempquova.substring(2, tempquova.length); 
	}


	//DMA
	if(dma=='' && (cIndicator=='US' || cIndicator=='223')) {
		if(tempquova.indexOf('D=')!=-1) {
			var dma = '&D=' + tempquova.substring(2, tempquova.length); 			
		}
	}

	//State
	if (state=='' && (cIndicator=='US' || cIndicator=='223')) {
		if(tempquova.indexOf('S=')!=-1) {
			var state = '&S=' + tempquova.substring(2, tempquova.length).toUpperCase();
		}
	}

	//Area code
	if (areaCode=='') {
		if(tempquova.indexOf('H=')!=-1) {
			var areaCode = '&H=' + tempquova.substring(2, tempquova.length);
		}
	}




	//Major Metro City
	if (majorMetroCity=='') {
		if(tempquova.indexOf('M=')!=-1) {
			var mm = tempquova.substring(2, tempquova.length);
			mm = mm.split(' ').join('');
			mm = mm.toUpperCase();
			var majorMetroCity = '&MM=' + mm;
		}
	}

	//AOL indicator
	if(tempquova.indexOf('AOL=')!=-1) {
		if(tempquova.substring(4, tempquova.length) == '0') {
			var rawAolIndicator = '0';
			var aolIndicator = '&AOL=0';
		}
		else {
			var aoli = tempquova.substring(4, tempquova.length);
			aoli = parseInt(aoli);
			if(aoli >= 1) {
				var rawAolIndicator = '1';
				var aolIndicator = '&AOL=1';
			}
		}
	}

	//ZIP code 3-digit
	if(zipCode3=='' && (cIndicator=='US' || cIndicator=='223')) {
		if(tempquova.indexOf('Z=')!=-1) {
			var zipCode3 = '&Z3=' + tempquova.substring(2, 5); 			
		}
	}

	//ZIP code 2-digit
	if(zipCode2=='' && (cIndicator=='US' || cIndicator=='223')) {
		if(tempquova.indexOf('Z=')!=-1) {
			var zipCode2 = '&Z2=' + tempquova.substring(2, 4); 			
		}
	}

}


//Daypart
var currentTime = new Date();
var currentHour = currentTime.getHours();

if((currentHour >= 6) && (currentHour < 8)) {var rawDaypart = 'EM'; var daypart = '&DP=EM';}
if((currentHour >= 8) && (currentHour < 11)) {var rawDaypart = 'MN'; var daypart = '&DP=MN';}
if((currentHour >= 11) && (currentHour < 13)) {var rawDaypart = 'LM'; var daypart = '&DP=LM';}
if((currentHour >= 13) && (currentHour < 17)) {var rawDaypart = 'AN'; var daypart = '&DP=AN';}
if((currentHour >= 17) && (currentHour < 20)) {var rawDaypart = 'EV'; var daypart = '&DP=EV';}
if((currentHour >= 20) && (currentHour < 23)) {var rawDaypart = 'NT'; var daypart = '&DP=NT';}
if(currentHour == 23) {var rawDaypart = 'LN'; var daypart = '&DP=LN';}
if((currentHour >= 0) && (currentHour < 3)) {var rawDaypart = 'LN'; var daypart = '&DP=LN';}
if((currentHour >= 3) && (currentHour < 6)) {var rawDaypart = 'ON'; var daypart = '&DP=ON';}


//AOL
if (partner=='AOL')
{
	var dma = '&D=991';
	var zipCode2 = '&Z2=AOL';
	var zipCode3 = '&Z3=AOL';
	var state = '&S=AOL';
	var areaCode = '&H=AOL';
	var majorMetroCity = '&MM=AOL';
}

var personalTag = gender + ageGroup + countryResidence + countryOrigin + dma + zipCode2 + zipCode3 + state + timeZone + daypart + areaCode + majorMetroCity + lineSpeed + aolIndicator;