function qsToArray() 
{
	var ret     = new Object();
	var kvpairs = new Array();
	var qs = location.search.replace("?", "");
	kvpairs = qs.split("&");

	for (var i = 0; i < kvpairs.length; i++) {
		var tmp = new Array();
		tmp = kvpairs[i].split("=");
		ret[tmp[0]] = tmp[1]; 
	}
	return ret;
}

function ckToArray()
{
	var ret = new Object();
	var ck  = new Array();
	ck = document.cookie.split(";");

	for (var i = 0; i < ck.length; i++) {
		var tmp = new Array();
		tmp = ck[i].split('=');
		ret[tmp[0]] =tmp[1]; 
	}
	return ret;
}

function getValueByKey(array, key)
{
	var ret = "";
	for (k in array) {
		if (k.match(key) != null) {
			ret = array[k];
			break;
		}
	}
	return ret;
}

function getSrvc()
{
	var ret = "";
	ret = getValueByKey(ckToArray(), 'tvly_service');
	if (ret == "")
		ret = getValueByKey(qsToArray(), 'Service');
	if (ret == "")
		ret = "TRAVELOCITY";
	return ret;
}

function sendSrvc() 
{
    	var srvc = getSrvc();

    	for ( var i = 0; i < document.links.length; i++ ) {   
		var l = document.links[i];
        	if (l.href.indexOf("REPLACE_SERVICE") < 0)
			continue;

        	document.links[i].href = l.href.replace(/REPLACE_SERVICE/, srvc);
    	}
}



function getCookieVal ( offset ) 
{
	var endstr = document.cookie.indexOf( ";", offset );
	if ( endstr == -1 ) endstr = document.cookie.length;
	return unescape( document.cookie.substring( offset, endstr ) );
}
function get_cookie( c ) 
{
	var arg  = c + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while ( i < clen ) {
		var j = i + alen;
		var c = document.cookie.substring( i, j );
		if ( document.cookie.substring( i, j ) == arg ) {
			return getCookieVal( j );
		}
		i = document.cookie.indexOf( " ", i ) + 1;
		if ( i == 0 ) {
			break;
		}
	}
	return null;
}

function popUp() {
	if ( get_cookie( "ck_popup" ) == null ) {
		var popwin = "user_registration.html";
		var windowprops = "location=no,directories=no,scrollbars=no,menubars=no,statusbar=no,toolbars=no,resizable=no,width=340,height=420,left=360,top=110" ;
		mywindow = open(popwin,'emailreg',windowprops);
		if (mywindow) {
			mywindow.location.href = popwin;
			window.name = "index";
			if (mywindow.opener == null) 
			mywindow.opener = self;
		}
	}
}

function popUnder() {

	var affiliate = get_cookie( "ck_affiliate_key" )

	if ( affiliate != null ) {
		if (affiliate == 9985 || affiliate == 9986 || affiliate == 9982 || affiliate == 9983) return;
	}
	// Current date to use for comparison
	var d = new Date();

	popWidth = 500;
	popHeight = 250;

	if ( document.all ) {
		popLeft = 5000;
		popTop = 5000;
	} else {
		popLeft = (screen.width - popWidth)/2;
		popTop = ((screen.height - popHeight)/2) - 20;
	}

	// Travelocity popunder settings
	var popwin = "/tvly_tt.html?in_promoclick=tvly_pop_launched";
	var windowprops = "location=no,directories=no,scrollbars=no,menubars=no,statusbar=no,toolbars=no,resizable=no,width=" + popWidth  + ",height=" + popHeight  + ",left=" + popLeft  + ",top=" +  popTop;

	// If cookie doesn't exist, launch popunder
	// Then set it to launch again the next day
	if ( get_cookie( "ck_s59popunder" ) == null ) {

		mywindow = open(popwin,'tvly_tt',windowprops);
		setExpiry(d);
		
	} else {

		expiry = get_cookie( "ck_s59popunder" );

		// Test whether the user has disabled the popunder
		if ( expiry != '0' ) {

			// Test whether the current time exceeds the time for relaunch
			if ( d.getTime() > expiry ) {

				// If so, launch popunder and reset expiry time
				mywindow = open(popwin,'tvly_tt',windowprops);
				setExpiry(d);
			}
		}
	}
	self.focus();
}

// Sets the time in which popunder suppression expires.
// Unless user clicks the "Don't show again" link, 
// popunder automatically relaunches after the next day

function setExpiry(d) {

	var expiry = new Date();
	expiry.setDate(d.getDate()+1); // Launch popunder the next day
	expiry.setHours(0);
	expiry.setMinutes(0);
	expiry.setMilliseconds(0);

	// set cookie with expiry time and one year lifespan
	nextYear = new Date();
	nextYear.setFullYear( nextYear.getFullYear() + 1); // cookie expires a year from now
	document.cookie = "ck_s59popunder=" + expiry.getTime() + "; expires=" + nextYear.toGMTString();
	return true;
}

function popUrl (page) {
	name = 'popup';
	params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=660,height=300';
	newwin = window.open(page, name, params); 
}

function windowPop(url) {
	var popHeight = screen.availHeight - 423;
	var popLeft = 100;
	var winParams = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=422,height=' + popHeight + ',left=' + popLeft + ',top=100';
	newwin = window.open(url,'MyWindow',winParams);
	//delay a bit here because IE4 encounters errors
	//when trying to focus a recently opened window
	setTimeout('newwin.focus();',250);
}

function hotelPop(url) {
	var popHeight = screen.availHeight - 40;
	var popLeft = screen.availWidth - 670;
	var winParams = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=660,height=' + popHeight + ',left=' + popLeft + ',top=0';
	newwin = window.open(url,'MyWindow',winParams);
	//delay a bit here because IE4 encounters errors
	//when trying to focus a recently opened window
	setTimeout('newwin.focus();',250);
}

function getObjStyle(name) {
  if (document.getElementById) {
    return document.getElementById(name).style;
  } else if (document.all) {
    return document.all[name].style;
  } else if (document.layers) {
    return document.layers[name];
  } else return false;
}

function getObjDoc(name) {
  if (document.getElementById) {
    return document;
  } else if (document.all) {
    return document.all[name].document;
  } else if (document.layers) {
    return document.layers[name].document;
  } else return false;
}

function getSelectedIndex(select, val) {
	for (var i = 0; i < select.length; i++) {
		if (select.options[i].value == val) return i;
	}
	return 0;
}

function getCheckedIndex(checkbox, val) {
	if (!hasValue(checkbox))
		return 0;
        if (hasValue(val)) {
			for (var i = 0; i < checkbox.length; i++) {
				if (checkbox[i].value == val) return i;
			}
        }
        return 0;
}

var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4up = (is_nav && (is_major >= 4));
var is_nav4   = (is_nav && (is_major == 4));
var is_nav6up = (is_nav && (is_major >= 5));
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_mac    = (agt.indexOf("mac")!=-1);

var oldBrowser = 0;
if (!(is_nav6up || is_ie5up)) { oldBrowser = 1; }



//returns HTML for shared header with a different
//image dependent on if the user is logged in
function GetHeaderLogin() {
	var WorldPerks = get_cookie("WorldPerks");
	if (WorldPerks != "") { 
		return ('<td><img src="images/shared/header/topnav_sub_logout2.gif" border="0" width="465" height="29" usemap="#topnav_sub_logout_map" alt=""></td>'); 
	} else {
		return ('<td><img src="images/shared/header/topnav_sub_login2.gif" border="0" width="465" height="29" usemap="#topnav_sub_login_map" alt=""></td>'); 
	}
}

function GetWPWelcome() {
	var WorldPerks = get_cookie("WorldPerks");
	if (WorldPerks != "") {
		var cookie_flds = WorldPerks.split('&');	 	
		return ('<span class="wplogin-name">'+cookie_flds[2]+' '+cookie_flds[1]+'</span><br>\nWorldPerks '+cookie_flds[4]+' member<br>\n<img src="images/shared/bullet_wpmiles.gif" border="0" width="10" height="10" alt="">'+Comma(cookie_flds[6])+' available miles\n  <br><a href="javascript:logout_worldperks_home()">Logout of WorldPerks</a>'); 
	} else {
		return ('<span class="wplogin-name">Welcome to nwa.com.</span><br>Please <a href="https://webx25.nwa.com/cgi-bin/wp_acctsum.pro">login</a> to access your account information.'); 
	}
}

//turn the subnav link row on or off by changing background/text
//color and associated arrow image
function SubNavLink(element, mode) {

	if ((document.getElementById) || (document.all)) {

		//defaults
		var arrowImage = element + "Arrow";	
		var bgColor = '#E5E6EF';
		var txtColor = '#233385';
		var arrowImg = 'arrowRightOn';
	
		//reassign background and text colors dependent on mode
		if (mode == 'over') {
			bgColor = '#CC0000';		
			txtColor = '#FFFFFF';
			arrowImg = 'arrowRightOn';
		} else if (mode == 'out') {
			arrowImg = 'arrowRightOff';			
		} else if (mode == 'over-open') {
			arrowImg = 'arrowDownOn';		
			bgColor = '#CC0000';				
			txtColor = '#FFFFFF';									
		} else if (mode == 'out-open') {
			arrowImg = 'arrowDownOff';							
		}
	
		//modern browsers
		if (document.getElementById) {	
		
			var navTxt = document.getElementById(element + "Txt");
			var arrowCell = document.getElementById(element + "ArrowCell");
	
			//change colors, swap image
			document.getElementById(element).style.backgroundColor = bgColor;
			if (arrowCell) { arrowCell.style.backgroundColor = bgColor; }
			if (document.images[arrowImage]) { SwapImage(arrowImage, arrowImg); }		
			if (navTxt) { navTxt.style.color = txtColor; }				
		
		//IE 4	
		} else if (document.all) {
			
			var navTxt = document.all(element + "Txt");
			var arrowCell = document.all(element + "ArrowCell");		
	
			//change colors, swap image
			document.all(element).style.backgroundColor = bgColor;
			if (arrowCell) { arrowCell.style.backgroundColor = bgColor; }
			if (document.images[arrowImage]) { SwapImage(arrowImage, arrowImg); }		
			if (navTxt) { navTxt.style.color = txtColor; }			
		}
	}
}

//replace a given img src with passed reference
function SwapImage(imgDocID,imgObjName) {
 if (document.images) {
		document.images[imgDocID].src = eval(imgObjName + ".src")
 }
}

/* run-time */

/* preload sub nav arrow images */
if (document.images) {
	 var arrowRightOn = new Image(13,14);
	 arrowRightOn.src = "images/shared/subnav/arrow_right_on.gif";
	 var arrowRightOff = new Image(13,14);
	 arrowRightOff.src = "images/shared/subnav/arrow_right_off.gif";
	 var arrowDownOn = new Image(13,14);
	 arrowDownOn.src = "images/shared/subnav/arrow_down_on.gif";
	 var arrowDownOff = new Image(13,14);
	 arrowDownOff.src = "images/shared/subnav/arrow_down_off.gif";			 
}

