/**
 * Created on 	: July 22 2008.
 * Author 		: Sachin Kumar K.T.
 */
var merchantId = "70176";
var varb = new getVariables();
/* SALES/ORDER BLOCK */
function xmlPost() {
	var img = new Image();
	img.src = varb.xmlOrderURL + "?xmlOrder=" + getXML();
}
function getXML() {
	var xmlOrder = null;
	var total = 0;
	var xmlProduct = "";
	var affiliateId = 0;
	var _domain_domain = "-";
	if (GetCookie(varb.asm) !== null) {
		affiliateId = GetCookie(varb.asm);
	}
	if (GetCookie(varb.asmReferral) !== null) {
		_domain_domain = GetCookie(varb.asmReferral);
	}
	var xmlOpen = "";
	var _productId_productId = document.getElementsByName(varb.productId);
	var _price_price = document.getElementsByName(varb.price);
	var _quantity_quantity = document.getElementsByName(varb.quantity);
	//Please make sure that for each product the hidden fields id, price, and quantity are mandatory
	//if not, it wont create any xml, and that order wont be considered   
	if (_price_price.length !== 0 && _productId_productId.length !== 0 && _quantity_quantity.length !== 0 && _price_price.length === _productId_productId.length && _productId_productId.length === _quantity_quantity.length) {
		for (var i = 0; i < _price_price.length; i++) {
			total = total + (_quantity_quantity[i].value * _price_price[i].value);
			var temp = varb.subProduct + _productId_productId[i].value.replace(/[^0-9]/g,"") + varb.subPrice + _price_price[i].value + varb.subQuantity + _quantity_quantity[i].value + varb.clsQuantity;
			xmlProduct = xmlProduct + temp;
		}
		var _ipq_ipq = document.getElementById(varb.ipq);
		var token = new StringTokenizer(_ipq_ipq.value, varb.colan);
		while (token.hasMoreElements()) {
			xmlOpen = varb.xmlOpnTag + token.nextElement() + varb.merchant + merchantId + varb.affiliate + affiliateId + varb.domains + _domain_domain + varb.subTotal + total + varb.discount + token.nextElement() + varb.customer + token.nextElement() + varb.country + token.nextElement() + varb.status + token.nextElement() + varb.products;
		}
		xmlOrder = xmlOpen + xmlProduct + varb.order;
	}
	return xmlOrder;
}
function StringTokenizer(str, delim) {
	this.str = str;
	this.delim = delim;
	this.getTokens = getTokens;
	this.hasMoreElements = hasMoreElements;
	this.nextElement = nextElement;
	this.countTokens = countTokens;
	this.tokens = this.getTokens();
	this.actuel = 0;
}
function getTokens() {
	var tokens = new Array();
	var nextToken;
	if (this.str.indexOf(this.delim) < 0) {
		tokens[0] = this.str;
		return tokens;
	}
	start = 0;
	end = this.str.indexOf(this.delim, start);
	var i = 0;
	while (this.str.length - start >= 1) {
		nextToken = this.str.slice(start, end);
		start = end + 1;
		if (this.str.indexOf(this.delim, start + 1) < 0) {
			end = this.str.length;
		} else {
			end = this.str.indexOf(this.delim, start + 1);
		}
		nextToken = trim(nextToken);
		while (nextToken.slice(0, this.delim.length) == this.delim) {
			nextToken = nextToken.slice(this.delim.length);
		}
		nextToken = trim(nextToken);
		if (nextToken === "") {
			continue;
		}
		tokens[i++] = nextToken;
	}
	return tokens;
}
function trim(strToTrim) {
	return (strToTrim.replace(/^\s+|\s+$/g, ""));
}
function countTokens() {
	return this.tokens.length;
}
function nextElement() {
	if (this.actuel >= this.tokens.length) {
		return null;
	} else {
		var r = this.tokens[this.actuel];
		this.actuel++;
		return r;
	}
}
function hasMoreElements() {
	return this.actuel < this.tokens.length;
}
/* CAPTURING CLICKS BLOCK */
var affSoft_kbId = 0;
var affSoft_queryString = window.location.search.substring(1);
var affSoft_imgdata = "";
var affSoft_subdata = "";
var affSoft_overwrite = 0;
var affSoft_refdata = document.referrer;
var affSoft_daysToLive = 365;
var multiDomain = false;
if (affSoft_queryString.length > 0) {
	var pairs = affSoft_queryString.split(varb.ampsend);
	for (var i = 0; i < pairs.length; i++) {
		var pairs2 = pairs[i].split(varb.equals);
		switch (pairs2[0].toLowerCase()) {
		  case varb.affid:
			affSoft_kbId = pairs2[1];
			break;
		  case varb.id:
			if (affSoft_kbId == 0) {
				affSoft_kbId = pairs2[1];
			}
			break;
		  case varb.img:
			affSoft_imgdata = pairs2[1];
			break;
		  case varb.sub:
			affSoft_subdata = pairs2[1];
			break;
		}
	}
}

// checking for the click Id and same Click or not 
if (aff_IsNewCustomer() == true) { //aff_IsAffiliateClick()==true && 
	InitializeTimer();
}
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
function InitializeTimer() {
    // Set the length of the timer, in seconds
	secs = 4;
	StopTheClock();
	StartTheTimer();
}
function StopTheClock() {
	if (timerRunning) {
		clearTimeout(timerID);
	}
	timerRunning = false;
}
function StartTheTimer() {
	if (secs == 0) {
		StopTheClock();
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
		postFunction();
	} else {
       // self.status = secs;
		secs = secs - 1;
		timerRunning = true;
		timerID = self.setTimeout("StartTheTimer()", delay);
	}
}
function postFunction() {
	aff_Post();
		// Store a flag in the form of a cookie
	if (affSoft_kbId != null) {
		SetCookie(varb.asm, affSoft_kbId, affSoft_daysToLive);
	}
	SetCookie(varb.asmReferral, affSoft_refdata, affSoft_daysToLive);
}
/*
function aff_IsAffiliateClick() { 
	if (affSoft_kbId!=null && affSoft_kbId!=0) {
		return true;
	} else {
		return false;
	}
 }
*/
function aff_IsNewCustomer() {
	if (GetCookie(varb.asm) == null && GetCookie(varb.asmReferral) == null) {
		return true;
	} else {
		return false;
	}
}
		
//Posting Click
function aff_Post() {
	var img = new Image();
	var url = varb.clicksURL + varb.ampId + affSoft_kbId;
	if (affSoft_imgdata != "") {
		url += varb.ampImgId + affSoft_imgdata + varb.ampBanner;
	} else {
		url += varb.ampText;
	}
	if (affSoft_subdata != "") {
		url += varb.ampSub + affSoft_subdata;
	}
	if (affSoft_refdata != "") {
		url += varb.ampReferrer + affSoft_refdata;
	}
	img.src = url;
}
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(varb.semiColon, offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie(name) {
	var arg = name + varb.equals;
	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;
}
function WriteCookie(name, value, expires) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	//inserted by Andrew Herron to fix cross-sub-domain cookie compatibility, 
	if (multiDomain) {
		rootDomain = document.domain;
	} else {
		domain = document.domain;
		dparts = domain.split(varb.dot);
		if (dparts.length == 3) {
			rootDomain = dparts[1] + varb.dot + dparts[2];
		} else {
			rootDomain = domain;
		}
	}
	var secure = (argc > 5) ? argv[5] : false;
	var cookie = name + varb.equals + escape(value) + ((expires == null) ? "" : (varb.expires + expires.toGMTString())) + ((varb.slash == null) ? "" : (varb.path + varb.slash)) + ((rootDomain == null) ? "" : (varb.semiDomain + rootDomain)) + ((secure == true) ? varb.semiSecure : "");
	document.cookie = cookie;
}
function DeleteCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1000000000);  // This cookie is history (changed -1 to make it previous time)
	var cval = GetCookie(name);
	document.cookie = name + varb.equals + cval + varb.expires + exp.toGMTString();
}
function SetCookie(name, value, expiredays) {
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * expiredays));
	WriteCookie(name, value, expdate);
}
/* submit action */
function submitAction(form) {
	form.referedBy.value = GetCookie(varb.asm);
	form.submit();
}
/* VARIABLES DECLARATION BLOCK */
function getVariables() {
	/* SALES VARIABLES START */
	/* PLEASE DO NOT CHANGE ANYTHING IN THIS BLOCK */
	this.xmlOrderURL = "http://ca.clickinc.com/sales/servlet/SalesXML";
	this.productId = "_productId_productId";
	this.price = "_price_price";
	this.quantity = "_quantity_quantity";
	this.ipq = "_ipq_ipq";
	this.hash = "#";
	this.colan = ":";
	this.subProduct = "<product>\n<id>";
	this.subPrice = "</id>\n<price>";
	this.subQuantity = "</price>\n<quantity>";
	this.clsQuantity = "</quantity>\n</product>\n";
	this.xmlOpnTag = "<?xml version=" + "\"" + "1.0" + "\"" + "?>\n<order>\n<id>";
	this.merchant = "</id>\n<merchant>";
	this.affiliate = "</merchant>\n<affiliateid>";
	this.domains = "</affiliateid>\n<domain>";
	this.subTotal = "</domain>\n<subtotal>";
	this.discount = "</subtotal>\n<discount>";
	this.customer = "</discount>\n<customer>";
	this.country = "</customer>\n<country>";
	this.status = "</country>\n<status>";
	this.products = "</status>\n<products>\n";
	this.order = "</products>\n</order>";
	/* SALES VARIABLES END */
	/* CLICKS VARIABLES START */
	/* PLEASE DO NOT CHANGE ANYTHING IN THIS BLOCK */
	this.clicksURL = "http://ca.clickinc.com/clicks/servlet/Click?merchant=" + merchantId;
	this.ampsend = "&";
	this.equals = "=";
	this.id = "id";
	this.affid = "affid";
	this.img = "img";
	this.sub = "sub";
	this.asm = "asm";
	this.asmReferral = "asmReferral";
	this.ampId = "&affId=";
	this.ampImgId = "&img=";
	this.ampSub = "&sub=";
	this.ampReferrer = "&referrer=";
	this.semiColon = ";";
	this.slash = "/";
	this.dot = ".";
	this.expires = "; expires=";
	this.path = "; path=";
	this.semiDomain = "; domain=";
	this.semiSecure = "; secure";
	this.ampBanner = "&type=banner";
	this.ampText = "&type=text";
	/* CLICKS VARIABLES END */
}
