function submit(clientID,stepID)
	{
//Checking and setting cookies:
var user_hash = "";
var cookie_string = "";
var step_string = "";
var landing = 0;
var unique = 1;


var browser = "";
var version = "-99";
var platform = "";
var referrer = "";
var language = "";
var time_zone = "";

var total = navigator.userAgent.toLowerCase();   //Gets all the information about the browser and the operating system	
var url = (window.location + "").substring(0,250);    //Get the current page's URL
var url_s = get_domain(url);
var url_short = url_s[2];

//Checking for the browser type:	
if((version = get_version(total,"chrome")) != -99)
	browser = "Chrome";
else if((version = get_version(total,"safari")) != -99)
	browser = "Safari";
else if((version = get_version(total,"opera")) != -99)
	browser = "Opera";
else if((version = get_version(total,"firefox")) != -99)
	browser = "FF";
else if((version = get_version(total,"msie")) != -99)
	browser = "IE";
else
	browser = "Other";


//Checking for platform:
if (total.indexOf("linux")+1)
	platform = "Linux";
else if (total.indexOf("mac")+1 || total.indexOf("ppc")+1)
	platform = "Mac";
else if (total.indexOf("win")+1)
	platform = "Windows";
else
	platform = "Other";


// find referrer
if (parent.document.referrer != null)
	referrer = parent.document.referrer;
else if(document.referrer != null)
	referrer = document.referrer;	
if (referrer == "blockedReferrer")
	referrer = "";

var base_domain = "";
var referrer_domain = "";
var keywords = "";
var search_engine = "";
var ppc = 0;
var social = 0;
var landing_time;
//Checking to see whether the referrer is a search engine:
if (referrer != "")
	{
	var engines = new Array();
	engines["google"] = ["Google","q"];
	engines["yahoo"] = ["Yahoo","p"];
	engines["bing"] = ["Bing","q"];
	engines["msn"] = ["MSN","q"];
	engines["aol"] = ["AOL","q"];
	engines["lycos"] = ["Lycos","query"];
	engines["ask"] = ["Ask","q"];
	engines["altavista"] = ["AltaVista","q"];
	engines["search"] = ["Search.com","q"];
	engines["netscape"] = ["Netscape","s"];
	engines["cnn"] = ["CNN","query"];
	engines["looksmart"] = ["LookSmart","qt"];
	engines["about"] = ["About","terms"];
	engines["mamma"] = ["Mamma","query"];
	engines["alltheweb"] = ["AlltheWeb","q"];
	engines["gigablast"] = ["Gigablast","q"];
	engines["voila"] = ["Voila","kw"];
	engines["virgilio"] = ["Virgilio","qs"];
	engines["live"] = ["Live","q"];
	engines["baidu"] = ["Baidu","wd"];
	engines["seznam"] = ["Seznam","w"];
	engines["yandex"] = ["Yandex","text"];
	engines["najdi"] = ["Najdi","q"];

	var socialr = new Array();
	socialr["facebook"] = 1;
	socialr["myspace"] = 1;
	socialr["twitter"] = 1;

	try
		{
		var domain_aspects = get_domain(referrer);
		if(domain_aspects[0] != "")
			{
			referrer_domain = domain_aspects[2];
			base_domain = domain_aspects[1];

			var social_hit = socialr[base_domain];
			if(social_hit == 1)
				social = 1;

			var engine_aspects = engines[base_domain];  //engines[0] is the search engine name; engines[1] is the digits it uses before '='
			if (engine_aspects != null)
				{
				var pos = referrer.indexOf("?"+engine_aspects[1]+"=");
				if (pos == -1)
					pos = referrer.indexOf("&"+engine_aspects[1]+"=");
					
				if (pos > -1)
					{
					keywords = referrer.substr(pos+engine_aspects[1].length+2);
					pos = keywords.indexOf("&");
					if (pos > -1)
						keywords = keywords.substr(0,pos);
					keywords = decodeURIComponent(keywords.replace(/\+/g," "));
					}
				}
			}
		
		if (keywords != "")
			{
			search_engine = engine_aspects[0];
			}
		} 
	catch (_e) {}

	//Was the referrer a google ppc?
	var pos = url.indexOf("gclid=");
	if (pos > 0)
		ppc = 1;
	else
		ppc = 0;

	//If the source was doubleclick get the actual referrer URL:
	if(referrer_domain.indexOf("doubleclick") > -1 || referrer_domain.indexOf("googlesyndication") > -1)
		{
		ppc = 1; //just making sure
		var url_pos = referrer.indexOf("&url=");
		if(url_pos == -1)
			url_pos = referrer.indexOf("?url=");
		var source_url = referrer.substr(url_pos+5);
		var end_pos = source_url.indexOf("&");
		if (end_pos > -1)
			source_url = decodeURIComponent(source_url.substr(0,end_pos));
		var source_aspects = get_domain(source_url);
		referrer_domain = source_aspects[2];
		}
	}

if(ppc == 1)
	social = 0;

//Getting Language, Time Zone and Time:
var d_obj = new Date();
time_zone = d_obj.getTimezoneOffset()/60;
var time = d_obj.getTime()/1000;
var landing_ago;
if (navigator.language)  			// For FF/Opera/Safari
	language = navigator.language.toLowerCase();
else if(navigator.userLanguage) 		//For IE
        language = navigator.userLanguage.toLowerCase();



//Doing all the cookie work:
cookie_string = get_cookie("funnel-tr_"+clientID);
if(cookie_string == null) //Checking if it's a landing
	{
	landing = 1;
	landing_ago = 0;
	user_hash = create_user_string();
	step_string = stepID;
	cookie_string = user_hash+'_+_'+referrer_domain+'_+_'+ppc+'_+_'+time+'_+_'+search_engine+'_+_'+keywords+'*+*'+stepID;
	set_cookie("funnel-tr_"+clientID, cookie_string, 180);
	}
else
	{
	var TW = cookie_string.split('*+*');
	var user_info = TW[0];
	step_string = TW[1];
	var US = user_info.split('_+_');
	user_hash = US[0];
	referrer_domain = US[1];
	ppc = US[2];
	landing_time = US[3];
	search_engine = US[4];
	keywords = US[5];
	landing_ago = time-landing_time;

	var CC = step_string.split('_');
	old_step_string = step_string;
	step_string = "";
	for(var i = 0; i < CC.length; i++)
		{
		//Remove Previous instances of this step:
		if(CC[i] == stepID) //They've already been on this step
			unique = 0;
		else
			step_string = step_string + '_' + CC[i];
		}
	step_string = step_string + "_" + stepID;
	step_string = step_string.substr(1);
	cookie_string = user_info + "*+*" + step_string;
	if(old_step_string != step_string)
		set_cookie("funnel-tr_"+clientID, cookie_string, 180);
	}

//Creating the return string:
var variables =  "stepid=" + encodeURIComponent(stepID) + "&";
variables +=  "steps=" + encodeURIComponent(step_string) + "&";
variables +=  "landing=" + encodeURIComponent(landing) + "&";
variables +=  "unique=" + encodeURIComponent(unique) + "&";
variables +=  "url=" + encodeURIComponent(url) + "&";
variables +=  "url_short=" + encodeURIComponent(url_short) + "&";
variables +=  "total=" + encodeURIComponent(total) + "&";
variables +=  "browser=" + encodeURIComponent(browser) + "&";
variables +=  "version=" + encodeURIComponent(version) + "&";
variables +=  "platform=" + encodeURIComponent(platform) + "&";
variables +=  "referrer=" + encodeURIComponent(referrer) + "&";
variables +=  "referrer_domain=" + encodeURIComponent(referrer_domain) + "&";
variables +=  "language=" + encodeURIComponent(language) + "&";
variables +=  "engine=" + encodeURIComponent(search_engine) + "&";
variables +=  "keywords=" + encodeURIComponent(keywords) + "&";
variables +=  "social=" + encodeURIComponent(social) + "&";
variables +=  "ppc=" + encodeURIComponent(ppc) + "&";
variables +=  "timezone=" + encodeURIComponent(time_zone) + "&";
variables +=  "landing_ago=" + encodeURIComponent(landing_ago) + "&";
variables +=  "user_hash=" + encodeURIComponent(user_hash);

var security = document.location.protocol == "https:" ? "https" : "http";
var send_string = security + "://www.travelground.com/funnel_track/funnel_test.php?" + variables;
var img = new Image(1,1);
img.src = send_string;

	}



//return_array[0] is the referrer's domain, while return_array[1] is the base part of that domain, return_array[2] is short version of domain
function get_domain(referrer_string)
	{

	var return_array = new Array();
	var extensions = ["ac","ad","ae","af","ag","ai","al","am","an","ao","aq","ar","as","at","au",
"aw","az","ax","ba","bb","bd","be","bf","bg","bh","bi","bj","bm","bn","bo","br","bs","bt","bv","bw","by","bz","ca","cc","cd","cf",
"cg","ch","ci","ck","cl","cm","cn","co","cr","cs","cu","cv","cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","eh","er",
"es","et","eu","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge","gf","gg","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu",
"gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in","io","iq","ir","is","it","je","jm","jo","jp","ke","kg","kh","ki",
"km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc","md","mg","mh","mk","ml","mm",
"mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my","mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz","om","pa",
"pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa","re","ro","ru","rw","sa","sb","sc","sd","se","sg","sh","si","sj",
"sk","sl","sm","sn","so","sr","st","sv","sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to","tp","tr","tt","tv","tw","tz",
"ua","ug","uk","um","us","uy","uz","va","vc","ve","vg","vi","vn","vu","wf","ws","ye","yt","yu","za","zm","zw","aero","arpa","asia","biz","cat",
"com","coop","edu","firm","gov","info","int","jobs","mil","mobi","museum","name","nato","net","org","pro","tel","store","travel","web"];

	var reg;
	var exp = new RegExp(/^([a-zA-Z]+:\/\/)([^\/]+)/); //Must start with [abc]://  , then stores all things between '/'
	var domain_part = "";
	var domain = "";
	var base_domain = "";
	var domain_short = "";


	if (reg = exp.exec(referrer_string)) //reg[0] = whole URL; reg[1] = http or https; reg[2] = domain; reg[n] = directories
		{
		domain = reg[2].toLowerCase();
		var domain_parts = domain.split(".").reverse(); //gets all the parts of the domain, and reverses them.
		while (domain_parts.length > 0) //going through each part of the domain, from the end back.
			{
			domain_part = domain_parts.shift();  //x becomes the first element of the array, and that element is removed from the array
			var is_extension = false;
			for (var i = 0; i < extensions.length; i++)   //goes through each possible extension, with extension being the index
				if(extensions[i] == domain_part) //if that part of the domain matches one of the known domain endings
					is_extension = true;
			if (!is_extension)  //break as soon as one of the part of the domain doesn't match a known one (from the end back)
				break;
			} //domain_part is now the core part of the domain (eg: travelground)
		base_domain = domain_part;
		var pos_dd = domain.indexOf(base_domain);
		domain_short = domain.substring(pos_dd);
		}
	
	return_array[0] = domain;
	return_array[1] = base_domain;
	return_array[2] = domain_short;
	return return_array;
	}




//Looks through the total agent string, and sees if there is a match for the browser. If so, then it returns the version.
function get_version(total,browser)
	{
	var version = "";
	var w = "";
	var i = -1;
	//Checking for the browser type:	
	if(total.indexOf(browser)+1)
		{
		//Getting the version number:
		if(total.indexOf(browser+" ") >= 0)
			i = total.indexOf(browser+" ");
		else if(total.indexOf(browser+"/") >= 0)
			i = total.indexOf(browser+"/");
		if(i > -1)
			{
			var v = total.substring(i + 1 + browser.length);
                        if (total.charAt(0) == "v")
                                total = total.substring(1);

			for (j=0;j<v.length;j++)
				{
				w = v.charAt(j);
				if (w == "." || (w >= 0 && w <= 9))
					version += w;
				else
					break;
				}
			return version;
			}	
		}
	return -99;
	}

//Name is the name of cookie (eg:tracker)
//Value is the value of the cookie (don't really need)
//Expires is specified in days. (probably make a few months)
function set_cookie(Name, value, expires)
	{
	var date_object = new Date();
	date_object.setTime(date_object.getTime()+(expires*86400000));
	var expiry = date_object.toGMTString();
    	var cook = Name + "=" + escape(value) + "; expires=" + expiry + "; path=/";
    	document.cookie = cook;
	}

function get_cookie(Name)
	{
	var results = document.cookie.match ('(^|;) ?'+Name+'=([^;]*)(;|$)');
	if(results)
		return(unescape(results[2]));
	else
		return null;
	}

function create_user_string()
	{
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 16;
	var random_string = '';
	for (var i=0; i<string_length; i++) 
		{
		var number = Math.floor(Math.random() * chars.length);
		random_string += chars.substring(number,number+1);
		}
	return random_string;
	}



//alert(time_zone);
//alert(language);
//alert(referrer);
//alert(user_agent);
//alert(browser);
//alert(version);
//alert(platform);
//alert(url);

