//26APR10 : WebTrends : Added custom exchange rate conversions

// This is where we include the exchange rates file and standard WebTrends tag
// Set these file references as appropriate to the site
function imInclude(filename) {
	document.write('<script src="' + filename + '" type="text/javascript"></script>');
}

// EDIT THESE PATHS AS APPROPRIATE
imInclude('/_scripts/conversion.js');
imInclude('/_scripts/webtrends.js');

// This effectively extends the standard WebTrends object, taking in currency info
function IMWebTrends(imExRates, imCurrDefaults) {
	// Create the standard tag in here
	this.wtTag = new WebTrends();
	
	// Add escaping of other chars to WebTrends' own escaping RegExp
	// UNCOMMENT IF = SIGN IN DATA PROVES A PROBLEM
	/*if (typeof(this.wtTag.re) == 'object') {
		this.wtTag.re['%3D'] = new RegExp(/=/g);
	}*/

	// Reference all tag properties/methods into this custom object so they can all be used as normal. 
	// (Although be aware simple data types are passed by value, so will not reflect any changes made inside wtTag)
	for (var n in this.wtTag) {
		this[n] = this.wtTag[n];
	}

	// Add in the currency info
	this.exRates = imExRates;
	this.currDefaults = imCurrDefaults;
	
	// This overrides the standard dcsTag to pre-process all transaction currency conversions (works on load and dcsMultiTrack()
	this.dcsTag = function() {
    	this.populateCurrencies();
    	this.wtTag.dcsTag(arguments);
    	this.clearZValues();
	}

	// This overrides the standard dcsCollect to extract WT params from URL (don't want to happen with dcsMultiTrack()
	this.dcsCollect = function () {
	    if (this.enabled){
	        this.dcsVar();
	        this.dcsMeta();
	        this.dcsAdv();
			this.setParamsFromUrl();
	        this.dcsTag();
	    }
	}
	
	// This overrides the standard dcsCleanUp to ensure references are maintained
	this.dcsCleanUp = function() {
    	this.wtTag.dcsCleanUp(arguments);
    	this.DCS = this.wtTag.DCS;
    	this.DCSext = this.wtTag.DCSext;
    	this.WT = this.wtTag.WT;
	}
}

// Adds in a method to our custom object for formatting currency figures
IMWebTrends.prototype.formatMoney = function(amount) {
	// Round to 2 decimal places
	amount = Math.round(amount * 100) / 100;
	// Format as a string, padding zeros
	var aAmounts = amount.toString().split('.');
	if (aAmounts.length == 1) {
		aAmounts[1] = '00';
	}
	else {
		aAmounts[1] += '00';
		aAmounts[1] = aAmounts[1].substr(0,2);
	}
	return aAmounts.join('.');
}

// Adds in a method to our custom object for converting between currencies
IMWebTrends.prototype.convert = function(amount, sourceCurrency, targetCurrency) {
	var rate = this.exRates[sourceCurrency + '-' + targetCurrency];
	// If no rate is specified, will use zero, so amount will come out to zero
	if (typeof(rate) == 'undefined') {
		rate = 1;
	}
	return this.formatMoney(amount * rate);
}

//Adds in a method to our custom object for populating all multi-currency transaction details
IMWebTrends.prototype.populateCurrencies = function () {
	try {
		// We only care about all this is there is a WT.tx_e and it isn't blank or a view
		if (typeof(this.WT.tx_e) != 'undefined' && this.WT.tx_e != '' && this.WT.tx_e != 'v') {
			// Want to make sure this is lowercase
			this.WT.tx_e = this.WT.tx_e.toLowerCase();
			
			// If no sitecode, set to UNKNOWN 
			if (typeof(this.WT.z_sitecode) == 'undefined') this.WT.z_sitecode = 'UNKNOWN';
	    	this.WT.z_sitecode = this.WT.z_sitecode.toUpperCase();
			
	    	// Find the site currency (default to UNKNOWN)
	    	var siteCurr = 'UNKNOWN';
			if (typeof(this.currDefaults[this.WT.z_sitecode]) != 'undefined') siteCurr = this.currDefaults[this.WT.z_sitecode].toUpperCase();
	
			// If no transaction currency, then set to UNKNOWN
			if (typeof(this.WT.z_tx_cur) == 'undefined') this.WT.z_tx_cur = 'UNKNOWN';
	    	this.WT.z_tx_cur = this.WT.z_tx_cur.toUpperCase();
	
	    	// If no tx_s, set to zero
	    	if (typeof(this.WT.tx_s) == 'undefined') this.WT.tx_s = '0.00';

	    	// If no tx_u, set to zero
	    	if (typeof(this.WT.tx_u) == 'undefined') this.WT.tx_u = '0';
	    		    	
	    	// Now we have everything we need
    		var aValueTrans = this.WT.tx_s.split(';');
    		var aValueSite = new Array(aValueTrans.length);
    		var aValueEur = new Array(aValueTrans.length);
    		
    		// First, make sure passed transaction values are properly formatted
    		for (var i=0; i<aValueTrans.length; i++) {
    			aValueTrans[i] = this.formatMoney(aValueTrans[i]);
    		}
    		
    		// If site currency is same as transaction, then no conversion needed
    		if (this.WT.z_tx_cur == siteCurr && siteCurr != 'UNKNOWN') {
    			aValueSite = aValueTrans;
    		}
    		else {
    			// Convert transaction values to site currency
	    		for (i=0; i<aValueTrans.length; i++) {
	    			aValueSite[i] = this.convert(aValueTrans[i], this.WT.z_tx_cur, siteCurr);
	    		}
    		}

    		// If transaction currency is EUR then no conversion needed
    		if (this.WT.z_tx_cur == 'EUR') {
    			aValueEur = aValueTrans;
    		}
    		else {
    			// Or if site currency is EUR, then no conversion needed
    			if (siteCurr == 'EUR') {
    				aValueEur = aValueSite;
    			}
    			else {
    				// Convert transaction values to EUR
    	    		for (i=0; i<aValueTrans.length; i++) {
    	    			aValueEur[i] = this.convert(aValueTrans[i], this.WT.z_tx_cur, 'EUR');
    	    		}
    			}
    		}
    		
    		// Now put the values into WebTrends tags
    		if (this.WT.tx_e == 'p') {
    			this.WT.tx_s = aValueSite.join(';');
    			this.WT.z_tx_eur = aValueEur.join(';');
    			this.WT.z_tx_txn = aValueTrans.join(';');
				this.WT.tx_u = this.WT.tx_u;
    		}
    		else {
    			var txe = this.WT.tx_e;
    			if (txe == 'i') txe = 'a'; 
    			this.WT['z_tx_' + txe] = aValueSite.join(';');
    			this.WT['z_tx_eur' + txe] = aValueEur.join(';');
    			this.WT['z_tx_tx' + txe] = aValueTrans.join(';');
    			this.WT['z_tx_u' + txe] = this.WT.tx_u;
				this.WT.tx_u = this.WT.tx_u;
    		}
		}
		}
	catch (e) {
		// Do nothing so we can still log whatever we have
	}
}

//Adds in a method to our custom object for setting parameters extracted from URL
IMWebTrends.prototype.setParamsFromUrl = function () {
	var qry = location.search.substring(1);
	if (qry != '') {
		var pair = new Array();
		var params = new Array();
		qry = qry.split('&');
		for (var i=0; i<qry.length; i++) {
			pair = qry[i].split('=');
			if (pair[0] != '') {
				params.push(pair[0]);
				if (pair.length == 1) {
					params.push('');
				}
				else {
					params.push(pair[1]);
				}
			}
		}
		this.dcsSetProps(params);
	}
}

//Adds in function to clear out unwanted WT.z_tx_* params and others
IMWebTrends.prototype.clearZValues = function() {
	for (var n in this.WT) {
		if ((n.substr(0, 5) == 'z_tx_' || n == 'z_load_basket') && n != 'z_tx_cur') this.WT[n] = null;
	}
}


