// (c) 2004 Copyright Nedstat BV Netherlands.
// ALL RIGHTS RESERVED
// version v4.6 2004-02-17 TP added resend check with session cookie check
// version v4.7 2004-02-22 TP added ns_ prefix and ns_undefined validation

function ns_order(ns_customerID,ns_client_id,ns_order_id) {
this.id = ns_customerID;
this.ns_client_id = ns_client_id;
this.ns_order_id = ns_order_id;
this.orders = new Array();
this.addLine = ns_addLine;
this.sendOrder = ns_sendOrder;
}

function ns_addLine(prod_id,brand,prod_grp,shop,qty,prod_price) {
	this.orders[this.orders.length] = new Array(prod_id,brand,prod_grp,shop,qty,prod_price);
}

function ns_cookieVal(cookieName) {
	var thisCookie = document.cookie.split("; ");
		for (var i=0; i<thisCookie.length; i++) {
			if (cookieName == thisCookie[i].split("=")[0]) {
				return thisCookie[i].split("=")[1];
			}
	  }
	}

function ns_isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function ns_isSignedFloat (s)	{
	var s = "" + s + "";
	var i=0;
  var seenDecimalPoint = false;
  if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") ) i++;
	if (s.charAt(i) == ".") return false;
  for (i ; i < s.length; i++)	{
  	var c = s.charAt(i);
		if ((c == ".") && !seenDecimalPoint) seenDecimalPoint = true;
		else if ( (c > "9") || (c < "0") ) return false;
		}
	if (s.charAt(i-1)==".") return false;
	return true;
}

function ns_sendOrder() {
	if (ns_cookieVal("ns_order_id_"+this.ns_order_id)!="true") {
		document.cookie = "ns_order_id_"+this.ns_order_id+"=true";
		var keys = new Array('ns_prod_id','ns_brand','ns_prod_grp','ns_shop','ns_qty','ns_prod_price');
		var ns__t="&ns__t="+(new Date()).getTime();
		for (var i in this.orders) {
			var start = this.id;
			start += ns__t + '&ns_commerce=true&ns_type=hidden&ns_client_id=' + this.ns_client_id + '&ns_order_id=' + this.ns_order_id ;
			start += '&ns_orderlines=' + this.orders.length;
			start += '&ns_orderline_id=' + ((i*1)+1);
			for (var t in keys) {
				if (ns_isEmpty(this.orders[i][t])) this.orders[i][t]="ns_undefined";
				if ( (keys[t]=="ns_qty") || (keys[t]=="ns_prod_price") )	{
					if (!ns_isSignedFloat(this.orders[i][t])) this.orders[i][t]="ns_undefined";
					}
				eval("start += '&"+keys[t]+"="+escape(this.orders[i][t])+"';");
			}
			eval("nedstat"+i+" = new Image();");
			eval("nedstat"+i+".src = start;");
		}
	}
}
//--------------------------------------------------------------
//Commented out the statements that are not needed for homepage 
//tagging. 
//The first input parameter is not neccesarily an anchor. It 
//could be an asp.net button (e.g. Add to my wallet) button.
//--------------------------------------------------------------
function ns_onclick (a, url, name, type, winopt) {
    //var ns_l="http://countrycode.sitestat.com/bcs/bcs/s?";
    var ns_l="http://int.sitestat.com/travelex/travelexuk/s?";
    
    // Inside purchase funnel, we have to use https instead of http
    if (typeof(requestUrl) != 'undefined' && requestUrl.indexOf('wallet/') != -1){
        ns_l = "https://int.sitestat.com/travelex/travelexuk/s?";
    }
    
    ns_l+=name;
    ns_l+='&ns_type='+type+'&ns_action=view';
    ns_l+='&ns__t='+(new Date()).getTime();
    
//    if(!url) {           //An asp.net button does not have a "href" property.
//        url=a.href;
//    }
    
    var ns_0=document.referrer;
    
    if(ns_0.lastIndexOf('/')==ns_0.length-1)
        ns_0=ns_0.substring(ns_0.lastIndexOf('/'),0);
    
    if (ns_0.length>0)
        ns_l+='&amp;ns_referrer='+escape(ns_0);
    
//    winopt=winopt||"";    //window open options are handled by our own code
    
    var target=(a&&a.target&&a.target!="")?(a.target.substring(0,1)=="_")?a.target.substring(1):a.target:"self";
    
    var ns_i=new Image();
    
    if(target) {//&&url) {
        if(window[target]) {
            window.ns_softclick_timer=function(target) { //,url) {
                return function() {
                    ns_i.onload=ns_i.onerror=function(){return;};
//                    window[((window[target])?target:"self")].location.href=url;
                }
            }
            (target)//,url);
            window.setTimeout('ns_softclick_timer()',5000);
            ns_i.onload=ns_i.onerror=window.ns_softclick_timer;
        }
//        else {
//            window.open(url,target,winopt);
//        }
    }
    ns_i.src=ns_l;
    return false;
}
