/*
EverestTechTrack.js.
This file makes a request for a 1x1px tracking image on either the email capture page or the order results page.

The query string variable ev_transid takes this format:

020080818092416948429952_264805997
^\---------------/\----/ \-------/
|        ^           ^       ^
|        |           |       |
|        |           |       - customer UID taken from Fool cookie (will be blank if cookies are disabled)
|        |           |
|        |           - random number between one and one million
|        |
|        - Current time (year, month , date, hours, minutes, seconds, 1/1000th of a second)
|
- Use 0 for email capture page, 1 for order page

Can't include the Ecap function on WWW\Usmf\Shop\Newsletters\Landing\Template\EcapTemplate.xslt
as our XSLT processor chokes on "&", even within the CDATA section.

The second query string variable takes this format:
ev_ecap_<product number>=1

*/

// Tracking pixel for Everest Tech on email capture pages
function EverestTechTrack(sPageType) {
	var sTimeString = Fool.Util.getTimeString();
	var sRandomNum = (parseInt(Math.random() * 1000000)).toString();
	var sUid = Fool.Cookie.getCookieValue("Fool", "Uid", "&");
	var sProductNum = -1;
	var sUrl = window.location.href;
	var sOpt;
	var sPageTypeNum;

	if(sPageType == "ecap") {
		sPageTypeNum = "0";
		sSaleType = "ecap";
		if(/\/shop\/newsletters\//i.test(sUrl)) {
			sProductNum = sUrl.replace(/^.*\/shop\/newsletters\//i, "").replace(/\/.*$/i, ""); // 2-digit product number
			sProductNum = sProductNum.length == 1 ? "0" + sProductNum : sProductNum;
		}
		else if(/\/TMF\/welcome.aspx/i.test(sUrl)) {
			sProductNum = sUrl.replace(/^.*\?category=/i, "").replace(/&.*$/, ""); 
			sProductNum = sProductNum.length == 1 ? "0" + sProductNum : sProductNum;
		}
	}
	else if (sPageType = "order") {
		var sSaleType;
		var sSaleTypeNum;
		sPageTypeNum = "1";
		if(/\&opt=[0-9]{4}-[0-9]+-[0|1]/i.test(sUrl)) {
			sOpt = sUrl.replace(/^.*\&opt=/i, "").replace(/&.*$/i, "");
			sProductNum = sOpt.replace(/-.*$/i, ""); // 4-digit product number
			sSaleTypeNum = sOpt.charAt(sOpt.length -1);
			//console.log("sOpt: " + sOpt + ", sProductNum: " + sProductNum + ", sPageTypeNum: " + sPageTypeNum);
			if(sSaleTypeNum == 1) {
				sSaleType = "FreeTrial";
			}
			else {
				sSaleType = "PaidSub";
			}
		}
	}

	// In the case of customer service looking up an order, the URL for orderresults.aspx will include
	// oid=<order number> but nothing else so sProductNum will still be -1. Do not call pixel in this case.
	if (sProductNum != -1) {
		var imageUrl = document.location.protocol + "//pixel1833.everesttech.net/1833/p?ev_transid=" + sPageTypeNum + sTimeString + sRandomNum + "_" + sUid + "&ev_" + sSaleType + "_" + sProductNum + "=1";
		//alert("ImageUrl: " + imageUrl);

		var doPing = new Image();
		doPing.src = imageUrl;
	}

	return true;
}

