// actionTags.js

function writeDynamicTag( tagCode ) {
	if ( tagCode ) {
		$('#dynamicTagScript').html( eval(tagCode) );
	}
}
//overture parameters 
var pm_tagname = "universalTag.txt";
var pm_accountid;
var pm_customargs;

//shopzilla parameters
var mid;
var cust_type;
var order_value;
var order_id;
var units_ordered;

//google parameters
var google_conversion_id;
var google_conversion_language = "en_GB";
var google_conversion_format = "1";
var google_conversion_color = "FFFFFF";
var google_conversion_value;
var google_conversion_label;


//doubleclick parameters
var axel = Math.random()+"";
var a = axel * 10000000000000;

//shopping.com
var merchant_id;
var order_id;
var order_amt;	
var category_id = "";
var category_name = "";
var product_id = "";
var product_name = "";

//nexTag
var id;
var rev;
var order;
var cats = "";
var prods = "";
var units = ""

// mediaplex CACHEBUSTER value
var mpt = new Date();
var mpts = mpt.getTimezoneOffset() + mpt.getTime();

//Inserts underscores between the parameters seperated by a |
function insertUnderscores(src) {
	
	var productList = "";
	
	if(typeof (src) != "undefined") {
		var splitValues = src.split("|");
		for(x = 0; x < splitValues.length; x++) {
			productList += splitValues[x];
			if(x + 1 < splitValues.length) {
				productList += "_";
			}
		}
	}
	return productList;
}

//nexTag
function nexTag(merchId) {
	id = merchId;
	rev = jTotalValue;
	order = jOrderid;
	prods = escape(jProductUids);
}

//web loyalty
function webLoyalty(title) {
 	  var tag1 = '<div id="webLoyalty"><a href="' + getProtocol() + 'ad.doubleclick.net/jump/N3446.jdwilliams/' + title +
		';sz=300x130;ord=' + escape(jTimestamp) + '?" target=_blank>' +
		'<img src="' + getProtocol() + 'ad.doubleclick.net/ad/N3446.jdwilliams/' + title +
		';sz=300x130;ord=' + escape(jTimestamp) + '?"BORDER=3 WIDTH=300 HEIGHT=130 ALT="Claim your Special Reward"></a></div>';

 	  $("#webLoyaltyContainer").append(tag1);
}

//web loyalty pop up
function webLoyaltyPopUp(merchantId_first_part, merchantId_second_part) {
	$(document).ready(function() {
	   	var tag1 = getProtocol() + 'one-time-offer.com/sg/gateway.aspx?v=' + merchantId_first_part + '&p=' + merchantId_second_part + '&cl=F6E606';
	   	webloyaltyPopup.popUpWin(tag1, true);
	});
}

//kelkoo
function kelkoo(programmeId, eventId) {
	var pStr = programmeId != null ? 'p=' + programmeId + '&' : '';
	
	document.writeln('<img src="' + getProtocol() + 'tbs.tradedoubler.com/report?' +
	pStr +
	'organization=1381977&event=' + eventId +
	'&orderNumber=' + escape(jOrderid) +
	'&orderValue=' + escape(jTotalValue) + 
	'&currency=' + escape(jCurrency) +
	'&checksum=v04' + escape(jChecksum) + 
	'&reportInfo=' +
	'&tduid=' + escape(jTDUID) +
	'&review=">');

}

//pricerunner
function pricerunner(actionId) {
	document.writeln('<img src="' + getProtocol() + 'www.emjcd.com/u?' +
	'AMOUNT=' + escape(jTotalValue) + '&CID=1512973' +
	'&OID=' + escape(jOrderid) +
	'&TYPE=' + actionId +
	'&CURRENCY=' + escape(jCurrency) +
	'&METHOD=IMG" height="1" width="20">');
}

//shopping.com checkout complete
function shopping(mercId) {
	merchant_id = mercId;
	order_id = jOrderid;
	order_amt = jTotalValue;	
}

//channel advisor checkout complete
function channel_adv_cc(clientId) {
	//channel advisor checkout complete
	document.writeln('<IMG SRC="https://tracking.searchmarketing.com/thankyou.asp?' +
		'SMCID='+ clientId +'&oVal='+ escape(jTotalValue) + 
		'&OrderID=' + escape(jOrderid) + 
		'&ProductID=' +	escape(jProductUids) + '" ' +
		'WIDTH="1" HEIGHT="1" BORDER="0">');		
}

//affiliate window checkout completed
function affiliateWindow_cc(merchantId) {
	(jNewWebCustomer == 'Y') ? customerType="new" : customerType="existing";
	affiliateWindow_cc_product(merchantId, customerType);

	document.writeln('<script type="text/javascript">');
	document.writeln('var AWIN={};');
	document.writeln('AWIN.Tracking={};');
	document.writeln('AWIN.Tracking.Sale={};');
	document.writeln('AWIN.Tracking.Sale.amount="' + escape(jTotalValue) + '";');
	document.writeln('AWIN.Tracking.Sale.currency="' + escape(jCurrency) + '";');
	document.writeln('AWIN.Tracking.Sale.orderRef="' + escape(jOrderid) + '";');
	document.writeln('AWIN.Tracking.Sale.parts="' + customerType + ':' + escape(jTotalValue) + '";');
	document.writeln('AWIN.Tracking.Sale.voucher="";');
	document.writeln('AWIN.Tracking.Sale.test="0";');
	document.writeln('</script>');
	document.writeln('<script src="https://www.dwin1.com/'+merchantId+'.js" type="text/javascript" defer="defer"></script>');
}

function affiliateWindow_cc_product(merchantId, customerType) {
	document.writeln('<form style="display:none;" name="aw_basket_form">');
	document.writeln('<textarea wrap="physical" id="aw_basket">');
	
	var productIds = jProductUidsMT.split("|");
	var productNames = jProductNamesMT.split("|");
	var productPrices = jProductPricesMT.split("|");	
	var productQuantities = jProductQuantitiesMT.split("|");		
	var d = '|';
		
	for (i=0; i<productIds.length; i++ ) {
		var s = 'AW:P' + d 
			+ merchantId + d 
			+ jOrderid + d 
			+ productIds[i] + d 
			+ productNames[i] + d 
			+ productPrices[i] + d 
			+ productQuantities[i] + d 
			+ '' + d 
			+ customerType + d
			+ '';
		document.writeln(s);
	}
	document.writeln('</textarea>');
	document.writeln('</form>');
}

function affiliateWindow_makeLine (merchantId, orderRef, productId, productName, unitPrice, quantity, skuData, commissionGroup, category) {
	var d = '|';
	var line = 'AW:P' + d + merchantId + d + orderRef + d + productId + d + productName + d + unitPrice + d + quantity + d + skuData + d + commissionGroup + d + category;
	return line;
}

function mvi_doubleclick(src, type, cat) {
	
	var productList = "";
	
	if(typeof (jProductUidsMT) != "undefined") {
		productList = insertUnderscores(jProductUidsMT);
	}
	
	document.write('<IFRAME SRC="' + getProtocol() + 'fls.uk.doubleclick.net/activityi' +
	';src=' + src + ';type=' + type + ';cat=' + cat + ';u2=' + escape(jTitle) + 
	';u11=' + escape(productList) + ';ord=1;num='+ a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>');
}


//Should be used for overlays (e.g. minibag) where document.write does not work correctly.
//divid must be the id of a <div> tag with style="display:none"
//into which the iframe html is placed

function mvi_overlayDoubleclick(divid, src, type, cat) {
	
	var productList = "";
	
	if(typeof (jProductUidsMT) != "undefined") {
		productList = insertUnderscores(jProductUidsMT);
	}
	
	var html = '<IFRAME SRC="' + getProtocol() + 'fls.uk.doubleclick.net/activityi' +
	';src=' + src + ';type=' + type + ';cat=' + cat + ';u2=' + escape(jTitle) + 
	';u11=' + escape(productList) + ';ord=1;num='+ a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>';
	
	$('#' + divid).html(html); 
}



//doubleclick checkout completed
function doubleclick_cc() {
	document.writeln('<IFRAME SRC="' + getProtocol() + 'fls.doubleclick.net/activityi' +
	';src=1792610;type=sales842;cat=011or841;u1=' + escape(jOrderid) + 
	';u2=' + escape(jTitle) + ';u3=' + escape(jNewWebCustomer) + 
	';u4=' + escape(jAccountid) + ';u5=' + escape(jQuantity) + 
	';u6=' + escape(jTotalValue) + ';u7=' + escape(jBillingPostCode) + 
	';qty=' + escape(jQuantity) + ';cost=' + escape(jTotalValue) + 
	';ord=' + escape(jOrderid) + '" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>');
}

//doubleclick checkout completed
function doubleclick_cc_extrafields() {
	var quantities = "";
	if(typeof (jProductQuantitiesMT) != "undefined") {
		quantities = insertUnderscores(jProductQuantitiesMT);
	}
	
	var product_ids = "";
	if(typeof (jProductUidsMT) != "undefined") {
		product_ids = insertUnderscores(jProductUidsMT);
	}
	
	var prices = "";
	if(typeof (jProductPricesMT) != "undefined") {
		prices = insertUnderscores(jProductPricesMT);
	}
	
	document.writeln('<IFRAME SRC="' + getProtocol() + 'fls.doubleclick.net/activityi' +
	';src=1792610;type=sales842;cat=011or841;u1=' + escape(jOrderid) + 
	';u2=' + escape(jTitle) + ';u3=' + escape(jNewWebCustomer) + 
	';u4=' + escape(jAccountid) + ';u5=' + escape(jQuantity) + 
	';u6=' + escape(jTotalValue) + ';u7=' + escape(jBillingPostCode) + 
	';u9=' + escape(quantities) + ';u11=' + escape(product_ids) + 
	';u15=' + escape(prices) + 
	';qty=' + escape(jQuantity) + ';cost=' + escape(jTotalValue) + 
	';ord=' + escape(jOrderid) + '" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>');
}

//doubleclick catalogue request
function doubleclick_catReqConf() {
	document.write('<IFRAME SRC="' + getProtocol() + 'fls.uk.doubleclick.net/activityi;' +
	'src=1792610;type=count825;cat=10cat270' +
	';u2=' + escape(jTitle) +
	';u3=' + escape(jNewWebCustomer) +
	';u4=' + escape(jAccountid) +
	';u7=' + escape(jBillingPostCode) +
	';u8=;ord=1;num='+ a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>');
}

//doubleclick registration complete
function doubleclick_rc() {
	document.write('<IFRAME SRC="' + getProtocol() + 'fls.uk.doubleclick.net/activityi;' +
	'src=1792610;type=count825;cat=012regst' +
	';u2=' + escape(jTitle) +
	';u4=' + escape(jAccountid) +
	';u7=' + escape(jBillingPostCode) +
	';u8=;ord=1;num='+ a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>');
}

//doubleclick floodlight home

function doubleclick_fl_home(src, type, cat) {
	document.write('<iframe src="//fls.doubleclick.net/activityi;' + 
	'src='+ src +';type=' + type + ';cat=' + cat +
	';ord=' + a + '?" width="1" height="1" frameborder="0"></iframe>');
}

//doubleclick floodlight account

function doubleclick_fl_acct(src, type, cat) {
	document.write('<iframe src="//fls.doubleclick.net/activityi;' + 
	'src='+ src +';type=' + type + ';cat=' + cat +
	';ord=' + a + '?" width="1" height="1" frameborder="0"></iframe>');
}

// doubleclick floodlight checkout completed
function doubleclick_fl_cc(src, type, cat) {
	document.write('<iframe src="//fls.doubleclick.net/activityi;' + 
	'src='+ src +';type=' + type + ';cat=' + cat +
	';u1=' + escape(jProductUids) +';ord=' + a + '?" width="1" height="1" frameborder="0"></iframe>');
}

/*
 * Doubleclick floodlight product details page.
 * This function is for a dynamic tag, so it does not itself attempt to document.write anything into the page.
 * Instead it returns a string to be written into the page by the writeDynamicTag function.
 */
function doubleclick_fl_pd(src, type, cat) {
	$(document).ready(function() {
		var tag = '<iframe src="http://fls.doubleclick.net/activityi;' +
			'src=' + src + 
			';type=' + type + 
			';cat=' + cat + 
			';u11=' + jProductId + 
			';u2=' + jTitle + 
			';ord=1;num=' + a + 
			'?" width="1" height="1" frameborder="0"></iframe>';
		$('#dynamicTagScript').append(tag);
	  });  
}

function image(path) {
    document.write('<img height="1" width="1" src="' + getProtocol() + path+'">');
} 

function cj_sale(eid,aid,currency) {
    document.write('<img src="https://www.emjcd.com/u?AMOUNT='+jTotalValue+'&CID='+
    eid+'&OID='+jOrderid+';'+jQuantity+'&TYPE='+aid+'&CURRENCY='+currency+'&METHOD=IMG" height="1" width="20">');
}

function cj_lead(eid,aid,currency) {
    document.write('<img src="' + getProtocol() + 'www.emjcd.com/u?AMOUNT=00.00&CID='+eid+
    '&TYPE='+aid+'&CURRENCY='+currency+'&METHOD=IMG" height="1" width="20">');
}

function cj_saleConfirmationItems(eid,aid,currency) {
	document.write('<img src="https://www.emjcd.com/u?' +
		'CID=' + eid +
		'&amp;OID=' + jOrderid +
		'&amp;TYPE=' + aid +
		cj_orderLines(jProductUidsMT, jProductPricesMT, jProductQuantitiesMT) +
		'&amp;CURRENCY=' + currency +
		'&METHOD=IMG" height="1" width="20">');
}

function cj_orderLines( itemStr, amtStr, qtyStr ) {
	var ret = '';
	var items = itemStr.split("|");
	var amts = amtStr.split("|");
	var qtys = qtyStr.split("|");
	
	for ( i = 0; i < items.length -1; i++ ) {
		ret += '&amp;ITEM' + (i+1) + '=' + items[i] + '&amp;AMT' + (i+1) + '=' + escape(amts[i]) + '&amp;QTY' + (i+1) + '=' + qtys[i];
	}
	ret += '&amp;ITEM' + (i+1) + '=' + items[i] + '&amp;AMT' + (i+1) + '=' + escape(amts[i]) + '&amp;QTY' + (i+1) + '=' + qtys[i];

	return ret;
}

function shopzilla_cc(merchantId) {
    mid            = merchantId;
    if(jNewCustomer == "Y") {
	    cust_type      = '1';
	} else {
	    cust_type      = '0';
	}
	order_value    = jTotalValue;
	order_id       = jOrderid;
	units_ordered  = jQuantity;
}

function google(conversion_id, label, conversion_format, conversion_value) {
    google_conversion_id =  conversion_id;
    google_conversion_value = conversion_value ? conversion_value : 1.0;
	google_conversion_label =  label;
	google_conversion_format = conversion_format;
}

function googleUrchin(code) {
    //_uacct = "UA-245925-1"; VLD
    //_uacct = "UA-245925-2"; PMA
    //_uacct = "UA-245925-3"; NAT    
    _uacct = code;
	urchinTracker();
}

function getProtocol() {
	return isSecureProtocol() ? "https://" : "http://";
}

function isSecureProtocol() {
	return location.href.indexOf("https") === 0;
}

/* URL-encodes the urlStr, but leaves those parts of the string that match exludeStr intact.
*/
function escapeExcluding( urlStr, excludeStr ) {
	var ret = "";
	var sections = urlStr.split( excludeStr );
	for ( i = 0 ; i < sections.length - 1; i++ ) {
		ret += ( escape(sections[i]) + excludeStr );
	}
	ret += escape(sections[i]);
	return ret;
}


function facebook_tracking(id, h) {
	try {  FB.Insights.impression({
	     'id': id,
	     'h' : h  });
	} catch (e) {}
}


function mediaplex_cc(title) {
  $(document).ready(function() {
      var mediaTag = "<div id=\"mediaplexCC\"><a href=\"" + getProtocol() + "altfarm.mediaplex.com/ad/ck/" + title+ "?" + "mpt=" + mpts
	    + "\"><img src=\"" + getProtocol() + "altfarm.mediaplex.com/ad/bn/" + title+ "?" + "mpt=" + mpts
	    + "\" alt=\"Click Here\" border=\"0\"></a></div>";   
     $("#affiliateMedia").append(mediaTag);
  });
}

function letsgorewards_cc(ref) {
  $(document).ready(function() {
      var mediaTag = "<div id=\"letsgorewardsCC\"><a href=\"" + getProtocol() + "www.letsgorewards.co.uk/global/scripts/promo.asp?ref=" + ref
	    + "\"><img src=\"" + IMG_SERVER + "/content/common/images/letsgorewards.gif"
	    + "\" alt=\"Click Here\" border=\"0\"></a></div>";   
    $("#affiliateMedia").append(mediaTag);
  });  
  
}

function doubleclick_ns(arg1, arg2){
	if (document.referrer) {
		document.write('<script language="JavaScript1.1" src="' + getProtocol() + 'images2.drct2u.com/content/javascript/naturalSearch/' + arg1 + '_dcadv' + arg2 + '.js?ord=' + a + '"></script>');
		// Original double click implementation
		// document.write('<script language="JavaScript1.1" src="' + getProtocol() + 'ad.doubleclick.net/adj/N4378.N10463.nso.codesrv/' + arg1 +';dcadv=' + arg2 + ';sz=1x2;ord=' + a + '?"></script>');
	}
}


