/* 
 * This javascript file is to capture the page referrer so that promotools
 * that are on the other pages can use this referrer.
 *
 * example: you get to a website disclaimer from goolge, on this disclaimer you don't have any promotool
 * but you want the promotools on the site to use this referrer.
 */
if(eval("typeof TC_promo_Base64") == "undefined")
	var TC_promo_Base64 = {
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = TC_promo_Base64._utf8_encode(input);
		while (i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
		}
		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = TC_promo_Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}
}
//parse a URL to form an object of properties
function tc_promo_parseURL(url)
{
    //save the unmodified url to href property
    //so that the object we get back contains
    //all the same properties as the built-in location object
	if(url!="") {
		var loc = { 'href' : url };

		//split the URL by single-slashes to get the component parts
		var parts = url.replace('//', '/').split('/');

		//store the protocol and host
		loc.protocol = parts[0];
		loc.host = parts[1];

		//extract any port number from the host
		//from which we derive the port and hostname
		parts[1] = parts[1].split(':');
		loc.hostname = parts[1][0];
		loc.port = parts[1].length > 1 ? parts[1][1] : '';

		//splice and join the remainder to get the pathname
		parts.splice(0, 2);
		loc.pathname = '/' + parts.join('/');

		//extract any hash and remove from the pathname
		loc.pathname = loc.pathname.split('#');
		loc.hash = loc.pathname.length > 1 ? '#' + loc.pathname[1] : '';
		loc.pathname = loc.pathname[0];

		//extract any search query and remove from the pathname
		loc.pathname = loc.pathname.split('?');
		loc.search = loc.pathname.length > 1 ? '?' + loc.pathname[1] : '';
		loc.pathname = loc.pathname[0];

		//return the final object
		return loc;
	}
	return false;
}

function tc_promo_setCookie(c_name,value,expiredays) {
	var url = 'http://promo.vador.com/ref/ref=';
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name+ '=' + escape(value) + ((expiredays==null) ? '' : (';expires=' + exdate.toUTCString()));
	own = location.host;
	own = encodeURIComponent(TC_promo_Base64.encode(own));

	var f = '<iframe width="0" height="0" frameborder="0" src="';
		f+= url;
		f+= value;
		f+= '"></iframe>';
	document.write(f);
}

function tc_promo_getCookie(c_name) {
if (document.cookie.length>0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1) {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function tc_promo_encode(str) {
	var result = str;
	if(result != '') {
		result = TC_promo_Base64.encode(result);
		result = result.replace('/','_');
		result = result.replace('+','-');
		result = result.replace(/=/g,'');
		result = encodeURIComponent(result);
	}
	
	return result;
}

function tc_promo_decode(str) {
	var result = str;
	if(result != '') {
		result = decodeURIComponent(result);
		result.replace('_','/');
		result.replace('-','+');
		var len = result.length();
		while(len % 4 != 0) {
			len++;
			result += "=";
		}
		result = TC_promo_Base64.decode(result);
	}
	return result;
}


if(tc_ref == undefined) {
	var tc_ref;
	// gets referrer hostname
	var tc_ref_host = tc_promo_parseURL(window.document.referrer);
	// compares referrer with actual page host name, if different, save referrer
	if (location.host != tc_ref_host.hostname) {
		tc_ref = window.document.referrer;
		if (tc_ref != "") {
			tc_ref = tc_promo_encode(tc_ref);
		}
	} else { // if equal, return saved referrer, if does not have one, save current
		tc_ref = tc_promo_getCookie('tc_referrer');
		if (tc_ref == "") {
			tc_ref = window.document.referrer;
			if (tc_ref != "") {
				tc_ref = tc_promo_encode(tc_ref);
			}
		}
	}

	tc_promo_setCookie('tc_referrer',tc_ref,null);
}
