function include(file) {  
	var script  = document.createElement('script');  
	script.src  = file;  
	script.type = 'text/javascript';  
	script.defer = true;  
	document.getElementsByTagName('head').item(0).appendChild(script);  
}

function get_url_var(urlVarName) {
	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]) {
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++) {
			if(urlVars[i]) {
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}


function SetCookie() {
	if(arguments.length < 2) { return; }
	var n = arguments[0];
	var v = arguments[1];
	var d = 0;
	if(arguments.length > 2) { d = parseInt(arguments[2]); }
	var exp = '';
	if(d > 0) {
		var now = new Date();
		then = now.getTime() + (d * 24 * 60 * 60 * 1000);
		now.setTime(then);
		exp = '; expires=' + now.toGMTString();
		}
	document.cookie = n + "=" + escape(String(v)) + '; path=/' + exp;
} // function SetCookie()

function GetCookie(n) {
	var cookiecontent = new String();
	if(document.cookie.length > 0) {
		var cookiename = n+ '=';
		var cookiebegin = document.cookie.indexOf(cookiename);
		var cookieend = 0;
		if(cookiebegin > -1) {
			cookiebegin += cookiename.length;
			cookieend = document.cookie.indexOf(";",cookiebegin);
			if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
			cookiecontent = document.cookie.substring(cookiebegin,cookieend);
			}
		}
	return unescape(cookiecontent);
} // function ReadCookie()


function get_domain_name(url) {
	var urlHalves = String(url).split('?');

	if(urlHalves[1]) {
		temp_url = urlHalves[0];
	} else {
		temp_url = url;
	}

	local_file = temp_url.indexOf('file://');
	if (local_file != -1) {
		return '';
	}
	var urlHalves = String(temp_url).split('/');

	Domain = '';

	if (urlHalves.length >= 3) {
		temp_url = urlHalves[2];
		
	} else {
		return '';
	}

	urlHalves = String(temp_url).split('.');
	if (urlHalves[0] != 'www') {
		Domain += urlHalves[0]+'.';
	}

	for(i=1; i<(urlHalves.length); i++) {
		Domain += urlHalves[i];
		if (i != (urlHalves.length-1)) {
			Domain += '.';
		}
	}

	return Domain;
	
}

// http://www.phpbuilder.com/board/showthread.php?t=10318476
function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

// http://jehiah.cz/archive/javascript-isdefined-function
function is_defined( variable ) {
	//return (typeof(window[variable]) == "undefined")?  false: true;
	if(typeof(variable) != "undefined"){ 
		return true;
	}
	return false;
}


//document.write(document.location);
//document.write(location.href);

var CookieVisitorID = '';
var PreviousCookie = GetCookie('CookieVisitorID');

if(PreviousCookie.length == 0) {
	CookieVisitorID = '';
} else {
	CookieVisitorID = PreviousCookie;
}


//alert(Action);
//if (!is_defined(Action)) {
if(typeof(Action) == "undefined"){ 
//if (typeof(window[Action]) == "undefined") {
	//alert('Action not defined');
	var Action = '';
}
if (!is_defined(UniqueID)) {
	var UniqueID = '';
}
//var Action = get_url_var('Action');
//var UniqueName = get_url_var('UniqueName');

//document.write(location.href);
Domain = get_domain_name(location.href);
//Domain = 'improv2.com';

Referral = urlencode(document.referrer);
RequestURI = urlencode(location.href);

if (Action == '') {
	Action = 'TrackVisitor';
}
//document.write(visitor_tracker_action);
//PageName = '';

//url = 'http://analytics.findersmedia.com/analytics.php?Action=' + Action;
url = 'http://analytics.ecomicbranding.com/analytics.php?Action=' + Action;
url += '&Domain=' + Domain;
url += '&CookieVisitorID=' + CookieVisitorID;
url += '&Referral=' + Referral;
url += '&RequestURI=' + RequestURI;
//alert(Action);
if (Action == 'TrackVisitor') {
	//url = 'analytics.php?Action=TrackVisitor';
	//url = 'http://www.h.dev/dc_projects/ecbanalytics/analytics.php?Action=TrackVisitor';
	// for ecb
	//url = 'http://analytics.ecomicbranding.com/analytics.php?Action=TrackVisitor';
	// for findersmedia
	//if (is_defined(analytics_page_name)) {
	if (typeof(analytics_page_name) != "undefined"){ 
	//if (typeof(window[analytics_page_name]) == "undefined") {
		url += '&PageName=' + analytics_page_name;
	}
	//document.write(url);
	include(url);
} else if (Action == 'TrackClick') {
	url += '&UniqueName=' + UniqueID;
	//alert(url);
	include(url);
	//document.write(url);
}
//alert(url);
