var sVS40Vars = "vs_R40_Booking_AA_Multiplier,vs_R40_Booking_AA_Origin,vs_R40_Booking_AA_Lowest_Offer,vs_R40_Booking_AA_Accepted_Offer,vs_R40_Booking_AA_AcceptedPrice,vs_R40_Booking_AA_NumPassengers_Accepted_Offer,v_aaAccepted,vs_R40_MyItins_AA_Multiplier,vs_R40_MyItins_AA_Origin,vs_R40_MyItins_AA_Lowest_Offer,vs_R40_MyItins_AA_Accepted_Offer,vs_R40_MyItins_AA_AcceptedPrice,vs_R40_MyItins_AA_NumPassengers_Accepted_Offer,vs_R40_Booking_PL_Accepted_Stations,vs_R40_MyItins_PL_Accepted_Stations,vs_R40_Booking_EPU_PricePerPassenger,vs_R40_Booking_EPU_NumPassengers_Paying_For_EPU,vs_R40_MyItins_EPU_PricePerPassenger,vs_R40_MyItins_EPU_NumPassengers_Paying_For_EPU,vs_R40_Booking_Conf_AA_Price,vs_R40_Booking_Conf_PL_Price,vs_R40_Booking_Conf_EPU_Price,vs_R40_Booking_Conf_TI_Price,vs_R40_Booking_Conf_Currency,vs_R40_MyItins_Conf_AA_Price,vs_R40_MyItins_Conf_PL_Price,vs_R40_MyItins_Conf_EPU_Price,vs_R40_MyItins_Conf_TI_Price,vs_R40_MyItins_Conf_Currency";

if (vsActive || vsActiveExt)
	{
		// Attach Event to page onload which tags all links (for global usage)
		if (window.addEventListener) //DOM 
		window.addEventListener("unload", vsReport, false)
		else if (window.attachEvent) //IE 
		window.attachEvent("onunload", 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";}
if (typeof sHeader=='string') {writeVSprotocol=sHeader;}

if(!window.vsCustomItems) { var vsCustomItems = new Array(); }

function vsReport()
{

if(window.s40_vs_fix_switch) {
	if(s40_vs_fix_switch == "MyItinsAA") {
		vsCustomItems[vsCustomItems.length] = new Array("vs_R40_MyItins_AA_Multiplier", vs_R40_Booking_AA_Multiplier.substring(0,1));
		vsCustomItems[vsCustomItems.length] = new Array("vs_R40_MyItins_AA_Origin", vs_R40_Booking_AA_Origin);
		vsCustomItems[vsCustomItems.length] = new Array("vs_R40_MyItins_AA_Lowest_Offer", vs_R40_Booking_AA_Lowest_Offer);
	//v_aaAccepted 
		$('.isComon').each(function(i) {
			if($(this).attr("checked")==true) {
				vsCustomItems[vsCustomItems.length] = new Array("vs_R40_MyItins_AA_Accepted_Offer", $(this).attr("id"));
				vsCustomItems[vsCustomItems.length] = new Array("vs_R40_MyItins_AA_AcceptedPrice", $(this).attr("amount"));
			}
		});

		vsCustomItems[vsCustomItems.length] = new Array("vs_R40_MyItins_AA_NumPassengers_Accepted_Offer", passengerOfferCount[0]);
		vsCustomItems[vsCustomItems.length] = new Array("v_aaAccepted", passengerOfferCount[0]);
	}
}

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.length>0)
	{
	  var bFound = false;
		for(i=0;i<vsCustomItems.length;i++)
		{
			if (vsCustomItems[i][0].length>=1 && vsCustomItems[i].length==2)
			{
				if(window.s40_vs_fix_switch) {
					if(sVS40Vars.indexOf(vsCustomItems[i][0]) > -1) {
						if(vsQuery.indexOf(vsCustomItems[i][0]) > -1) {
							bFound = true;
						} else {
							bFound = false;
						}
					}
				}
				
				vsQueryDebug += "(custom value) &"+escape(vsCustomItems[i][0])+"="+escape(vsCustomItems[i][1])+"\n";
				if(!bFound) {
					vsQuery += "&"+escape(vsCustomItems[i][0])+"="+escape(vsCustomItems[i][1])+"\n";
					bFound = false;
				}
			}
		}
	}

// 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
if (vsActive) 		{ document.getElementById('vsImage').src = writeVSprotocol+'://www.united.com/ual/asset/vs.gif?Log=1'+vsQuery; } // ual req

// 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);

// safari delay
if (navigator.appVersion.indexOf("Safari")!=-1)
	{
		var rightNow=new Date;
		var delayTime = 800;
		while(new Date-rightNow<delayTime);
	}
} // 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 ""
}

function w(msg, bAlert) {
  if(window.console) {
	  console.error(msg);
	} else if(bAlert) {
		alert(msg);
	}
}