// referral tracking
if (document.referrer != '') {
	if (document.referrer.toString().indexOf(window.location.hostname.toString()) < 0)
		createCookie('referer',document.referrer,14);
}


// src tracking
if (getQsItem('PROMOCODE') != '')
	createCookie('PromoCode',getQsItem('PROMOCODE'),14);
if (getQsItem('SOURCEID') != '')
	createCookie('PromoCode',getQsItem('SOURCEID'),14);
if (getQsItem('SRC') != '')
	createCookie('PromoCode',getQsItem('SRC'),14);
if (getQsItem('P') != '')
	createCookie('PromoCode',getQsItem('P'),14);


// agent id tracking
if (getQsItem('AGENTID') != '')
	createCookie('AgentID',getQsItem('AGENTID'),14);
if (getQsItem('TELID') != '')
	createCookie('TelID',getQsItem('TELID'),14);
if (getQsItem('COGID') != '')
	createCookie('CogID',getQsItem('COGID'),14);
if (getQsItem('REFID') != '')
	createCookie('RefID',getQsItem('REFID'),14);
if (getQsItem('CUSTOMERID') != '')
	createCookie('CustomerID',getQsItem('CUSTOMERID'),14);
	
/****** Worker functions ******/
	// cookie functions
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else
			var expires = "";
		
		document.cookie = name.toLowerCase() + "=" + value.toLowerCase() + expires + "; domain=" + getTLD() + "; path=/";
	}
	
	function readCookie(name) {
		var nameEQ = name.toLowerCase() + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	function eraseCookie(name) {
		createCookie(name.toLowerCase(), '', -1);
	}
	
	
	// query string functions
	function PageQuery(q) {
		if(q.length > 1)
			this.q = q.substring(1, q.length);
		else
			this.q = null;
		
		this.keyValuePairs = new Array();
		
		if(q) {
			for(var i=0; i < this.q.split("&").length; i++) {
				this.keyValuePairs[i] = this.q.split("&")[i];
			}
		}
	
		this.getKeyValuePairs = function() { return this.keyValuePairs; }
	
		this.getValue = function(s) {
			for(var j=0; j < this.keyValuePairs.length; j++) {
				if(this.keyValuePairs[j].split("=")[0].toUpperCase() == s.toUpperCase())
					return this.keyValuePairs[j].split("=")[1];
			}
			
			return false;
		}
	
		this.getParameters = function() {
			var a = new Array(this.getLength());
			for(var j=0; j < this.keyValuePairs.length; j++) {
				a[j] = this.keyValuePairs[j].split("=")[0];
			}
			
			return a;
		}
	
		this.getLength = function() { return this.keyValuePairs.length; }
	}
	
	function queryString(key) {
		var page = new PageQuery(window.location.search);
		
		return unescape(page.getValue(key));
	}
	
	function getQsItem(key) {
		if(queryString(key)=='false')
			return '';
		else
			return(queryString(key));
	}
	
/*	function getTLD() {
		var tld = '';
		var tmp = new Array();
		var i = 0;
		
		tmp = document.domain.split('.');
		
		if (tmp.length >=2) {
			for (i=tmp.length-2; i<tmp.length; i++) {
				tld = tld + tmp[i] + '.'
			}
			
		}
		return tld.substring(0, tld.lastIndexOf('.'));
	}
*/
	function getTLD() {
		var tld = '';
		var tmp = new Array();
		var i = 0;
		
		tmp = document.domain.split('.');
		
		if (tmp.length >=2) {
			for (i=1; i<tmp.length; i++) {
				tld = tld + tmp[i] + '.'
			}
		}
		else if (tmp.length == 2) {
			tld = domain + '.';
		}
		
		return tld.substring(0, tld.lastIndexOf('.'));
	}
/*****************************/
