if (vsActive || vsActiveExt)
	{
		// Attach Event to page onload which tags all links (for global usage)
		if (window.addEventListener) //DOM 
			if(((navigator.userAgent.toLowerCase()).indexOf("applewebkit"))!=-1) 
			{ 
				window.addEventListener("beforeunload", vsReport, false);
			} else {
				window.addEventListener("unload", vsReport, false);
			}
		else if (window.attachEvent) //IE 
		window.attachEvent("onbeforeunload", vsReport)
		else if (document.getElementById) // Legacy
		window.onunload=vsReport;
	}

var vsLinkClicked = "";
var v_pc = ""; // page category
var v_ln = ""; // link name
var v_startTime = new Date();
//var writeVSprotocol = "http"; if(document.location.href.indexOf('https://') !=- 1){writeVSprotocol = "https";}
var writeVSprotocol = (location.protocol == "http:") ? "http" : "https";
if (typeof sHeader=='string') {writeVSprotocol=sHeader;}

function vsReport() {
	if (vsActive) { 
		var vsQueryDebug = "";
		var vsQuery = "";
		var v_endTime = new Date();

		// 1  of 4 assemble string of standard values
		if (window.webAnalyticsPageTitle) {
			if (webAnalyticsPageTitle != "") {
				vsQueryDebug+="(page name) &v_pn="+escape(webAnalyticsPageTitle)+"\n";
				vsQuery+="&v_pn="+escape(webAnalyticsPageTitle);
			}
		}
		if (window.vsErrorString) {
			if (vsErrorString != "") {
				vsQueryDebug+="(error string) &v_errorString="+escape(vsErrorString)+"\n";
				vsQuery+="&v_errorString="+escape(vsErrorString);
			}
		}
		if (window.vsLinkClicked) {
			if (vsLinkClicked != "") {
				q=vsLinkClicked.split('?');
				if (q[1]) {
					//Get all Name/Value pairs from the QueryString
					var pairs = q[1].split('&');
					for (i=0;i<pairs.length;i++) {
						//Get the Name from given Name/Value pair
						var keyval = pairs[i].split('=');
						if (keyval[0]=="navSource" && keyval[1].length>=1) { v_pc=keyval[1]; vsQueryDebug+="(page category) &v_pc="+escape(v_pc)+"\n";}
						if (keyval[0]=="linkTitle" && keyval[1].length>=1) { v_ln=keyval[1]; vsQueryDebug+="(link name) &v_ln="+escape(v_ln)+"\n";}

						if (keyval[0]=="navSource" && keyval[1].length>=1) { v_pc=keyval[1]; vsQuery+="&v_pc="+escape(v_pc);}
						if (keyval[0]=="linkTitle" && keyval[1].length>=1) { v_ln=keyval[1]; vsQuery+="&v_ln="+escape(v_ln);}
					}
				}
			}
		}
		if (window.vsIsLoggedIn) {
			if (vsIsLoggedIn == "true") {
				vsQueryDebug+="(logged in state) &v_loggedIn=true\n";
				vsQuery+="&v_loggedIn=true";
			} else if (vsIsLoggedIn == "false") {
				vsQueryDebug+="(logged in state) &v_loggedIn=false\n";
				vsQuery+="&v_loggedIn=false";
			}

			if (vsGetCookie("rememberProfile")) {
					var vs_profile=vsGetCookie("rememberProfile"); vs_profile = vs_profile; var v_parsedValue=vs_profile.split("||");
					var v_mpLevel=v_parsedValue[3];
					vsQueryDebug+="(MP Level) &v_els="+v_mpLevel+"\n";
					vsQuery+="&v_els="+escape(v_mpLevel);
			}
			if (vsGetCookie("UALLocaleCookie")) {
					var vs_locale=vsGetCookie("UALLocaleCookie");
					vsQueryDebug+="(v_locale) &v_locale="+vs_locale+"\n";
					vsQuery+="&v_locale="+escape(vs_locale);
			}
		}
		// 2 of 4 appnd string with custom name/values
		if (window.vsCustomItems) {
			for(i=0;i<vsCustomItems.length;i++) {
				if (vsCustomItems[i][0].length>=1 && vsCustomItems[i].length==2) {
					vsQueryDebug += "(custom value) &"+escape(vsCustomItems[i][0])+"="+escape(vsCustomItems[i][1])+"\n";
					vsQuery += "&"+escape(vsCustomItems[i][0])+"="+escape(vsCustomItems[i][1])+"\n";
				}
			}
		}

		// 3 of 4 add uri and timestamp as last parameter.
		vsQueryDebug += "(Time spent on this page) &v_duration="+(v_endTime-v_startTime)+"\n";
		vsQuery += "&v_duration="+(v_endTime-v_startTime);  // duration

		vsQueryDebug += "(Timestamp) &v_timestamp="+v_startTime.getTime()+"\n";
		vsQuery += "&v_timestamp="+v_startTime.getTime(); // timestamp

		if (screen.width) {
			vsQueryDebug += "(Screen resolution) &v_res="+screen.width+"x"+screen.height+"\n";
			vsQuery += "&v_res="+screen.width+"x"+screen.height; // uri
		}

		vsQueryDebug += "(URI of this page) &v_uri="+window.location.href+"\n";
		vsQuery += "&v_uri="+escape(window.location.href); // uri

		if (document.referrer&&document.referrer!="") {
			if ( (document.referrer.indexOf("//www.united.com")==-1) && (document.referrer.indexOf("//www.ua2go.com")==-1) && (document.referrer.indexOf("//travel.united.com")==-1) ) {
				vsQueryDebug += "(External Referrer to this page) &v_referrer="+document.referrer+"\n";
				vsQuery += "&v_referrer="+escape(document.referrer); // referring URL
			}
		}

		// 4 of 4 request image
		//if (vsQuery != "") alert(vsActive+"\n"+vsActiveExt+"\n"+"Web Analytics src of img to be requested: www.united.com/ual/asset/vs.gif?Log=1\n"+vsQueryDebug)

		// interim disabled if (vsActiveExt) 	{ document.getElementById('vsImageExt').src = writeVSprotocol+'://csvtk.united.com/ual/asset/vs.gif?Log=1'+vsQuery; } // ext req
		vsQuery = writeVSprotocol+'://www.united.com/ual/asset/vs.gif?Log=1'+vsQuery;
		document.getElementById('vsImage').src = vsQuery; 

		// onUnload delay, to ensure images (above) are requested
		var rightNow=new Date;
		var delayTime = 550; 

		//if (window.webAnalyticsPageTitle)
		//	{
		//		if (webAnalyticsPageTitle == "Incorrect password") { delayTime = 550; }
		//	}

		while(new Date-rightNow<delayTime);

		// addl safari delay
		if (navigator.appVersion.indexOf("Safari")!=-1)
			{
				var rightNow=new Date;
				var delayTime = 800;
				while(new Date-rightNow<delayTime);
			}
	} // ual req
} // end vsReport


// during load, embed vs target image
document.write('<div><im'+'g src="'+writeVSprotocol+'://www.united.com/images/d.gif" border="0" width="1" height="1" alt="vs" id="vsImage"></div>');
document.write('<div><im'+'g src="'+writeVSprotocol+'://www.united.com/images/d.gif" border="0" width="1" height="1" alt="vs" id="vsImageExt"></div>');

// legacy insightfirst function (incase)
function if_queryForm()
{
return true; // basically, do nothing.
}

// remove spaces from a string
function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}


function vsGetCookie(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 ""
}
