/////////////////// Global Vars ///////////////////
var eidValue_Global='';
var bidValue_Global='';
var ECNstepname = '';
//////////// Basic cookie functions ///////////////
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var ck = name + "=" + value + expires + ";path=/; domain=" + window.document.domain.toString();
	document.cookie = ck;
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function eraseCookie(name)
{
	createCookie(name,"",-1);
}

//////////// Converstion functions ////////////////
function CreateCookieForEmailID(value) {
	createCookie("eid", value, 7);
}

function CreateCookieForCampaignID(value) {
	createCookie("bid", value, 7);
}

function CreateCookieForRevenue(value) {
	createCookie("revTotal", value, 7);
}


function CreateConversionCookies() {
	getConversionParams();	

	if((eidValue_Global.length > 0) && (bidValue_Global.length > 0)){
		CreateCookieForEmailID(eidValue_Global);
		CreateCookieForCampaignID(bidValue_Global);
	}else{
		//eidValue_Global and bidValue_Global are NULL so use the existing Cookie Value
	}		
}

/////////// Get Conversion param data from URL ///////////
function getConversionParams(){
    var sURL = window.document.URL.toString();
	var bidValue = "";
	var eidValue = "";

	if (sURL.indexOf("?") > 0) {
		var arrParams = sURL.split("?");

		var arrURLParams = arrParams[1].split("&");

		var arrParamNames = new Array(arrURLParams.length);
		var arrParamValues = new Array(arrURLParams.length);

		var i = 0;
		for (i=0;i<arrURLParams.length;i++) {
			var sParam =  arrURLParams[i].split("=");
			arrParamNames[i] = sParam[0];
			if (sParam[1] != "")
				arrParamValues[i] = unescape(sParam[1]);
			else
				arrParamValues[i] = "No Value";
		}

		for (i=0;i<arrURLParams.length;i++) {
			if(arrParamNames[i] == "bid"){
				bidValue_Global = arrParamValues[i];
			}else if(arrParamNames[i] == "eid"){
				eidValue_Global = arrParamValues[i];
			}
		}				
	}
}

/////////// POST Conversion Data ///////////
function PostConversionData()  {
    var sURL = window.document.URL.toString();
    //alert(window.document.domain.toString());
	var conversionURL = "";
	var bidValue = readCookie("bid");
	var eidValue = readCookie("eid");
	if (bidValue == null || eidValue == null){
	    //If the cookie values are null, recreate them from the URL params
		CreateConversionCookies();
	}else{
		//If the cookie values are not null, Check'em agains the Query string 
		//Values. 
		getConversionParams();
		
		//if there are URL param values, check if they are the same.
		//If same continue, If not, reset the values again for the current Blast, EmailID
		if(bidValue_Global.length > 0 &&  eidValue_Global.length > 0){
			if(!(bidValue == bidValue_Global) || !(eidValue == eidValue_Global)){
				CreateConversionCookies();
			}
		}else{
			//bidValue / eidValue are null / no params. so use existing cookies
		}
	}
	//Get the cookie values again. They might be different.
	bidValue = readCookie("bid");
	eidValue = readCookie("eid");
	//alert(bidValue + ', ' + eidValue);
	if (bidValue != null && eidValue != null && bidValue!="" && eidValue!="") {	
		var origURL = sURL.split("bid=");
		
		if (ECNstepname != ''){
			origURL[0] = origURL[0] + "&step=" + ECNstepname;
		}

		conversionURL = "https://www.ecn5.com/ecn.communicator/engines/conversion.aspx?b="+bidValue+"&e="+eidValue+"&oLink="+origURL[0];
	} else {
		conversionURL = "https://www.ecn5.com/1x1.jpg";
	}
	return conversionURL;
}

/////////// POST Value from Hidden directly////////
function PostRevenueConversionData_Direct()  {		
	var sURL = window.document.URL.toString();
	var conversionURL = "";
	var bidValue = readCookie("bid");
	var eidValue = readCookie("eid");	
	var totalValue = getobjValue('ecnConv_total');
	if (bidValue != null && eidValue != null && bidValue!="" && eidValue!="") {	
		var origURL = sURL.split("bid=");
		conversionURL = "https://www.ecn5.com/ecn.communicator/engines/conversion.aspx?b="+bidValue+"&e="+eidValue+"&total="+totalValue+"&oLink="+origURL[0];
	} else {
		conversionURL = "https://www.ecn5.com/1x1.jpg";
	}
	return conversionURL;
}

/////////// POST Value from COOKIE directly////////
function PostRevenueConversionData_Cookie()  {		
	var sURL = window.document.URL.toString();
	var conversionURL = "";
	var bidValue = readCookie("bid");
	var eidValue = readCookie("eid");	
	var totalValue = readCookie("revTotal");
	if (bidValue != null && eidValue != null && bidValue!="" && eidValue!="") {	
		var origURL = sURL.split("bid=");
		conversionURL = "https://www.ecn5.com/ecn.communicator/engines/conversion.aspx?b="+bidValue+"&e="+eidValue+"&total="+totalValue+"&oLink="+origURL[0];
	} else {
		conversionURL = "https://www.ecn5.com/1x1.jpg";
	}
	return conversionURL;
}

/////////// Convertion Value from Hidden///////////
function getobjValue(id) {
	if (document.all && !document.getElementById) 
		obj = eval('document.all.' + id);
	else if (document.layers) 
		obj = eval('document.' + id);
	else if (document.getElementById) 
		obj = document.getElementById(id);

	return obj.value;
}