var selectedSection ='AU'; //which page this is
var writeSWNprotocol = (document.location.protocol == 'https:') ? 'https' : 'http';
var iBrowserVersion;
$(document).ready(function() {	
	// START BROWSER TWEAKS
		iBrowserVersion = Number($.browser.version);
		$('#cmrpLeftColTopContent').css('position','relative');
		if($.browser.msie) {  // Apply all Internet Explorer tweaks (all version?)
			$('.cmrpCellImg').css('position','relative');
			$('.cmrpCellImg').css('left','2px');
			if(iBrowserVersion==6) { // Apply all Internet Explorer 6 tweaks
				$('#cmrpLeftColTopContent').css('top','35px');
			} else if (iBrowserVersion > 6) { // Fix all Internet Explorer 7 tweaks
				$('#cmrpLeftColTopContent').css('top','35px');
			}
		} else if($.browser.safari) { // Apply all saferi tweaks
			// Check for Safari on a Mac
			$('#cmrpLeftColTopContent').css('top','35px');
			$('.cmrpCellImg').css('position','relative');
			$('.cmrpCellImg').css('left','2px');
			if(navigator.userAgent.indexOf("Macintosh") > -1) {
				$('.cmrpCellImg').css('position','relative');
				$('.cmrpCellImg').css('left','2px');
				$('#sel_return_to').width('120px');
				$('#cmrpLeftColTopContent').css('top','35px');
			}
		} else if($.browser.mozilla) { // Apply all FireFox tweaks
			// Check for FireFox on a Mac
			$('#cmrpLeftColTopContent').css('top','34px');
			if(navigator.userAgent.indexOf("Macintosh") > -1) {
				$('.cmrpCellImg').css('position','relative');
				$('.cmrpCellImg').css('left','2px');
				$('#sel_return_to').width('120px');
				$('#cmrpLeftColTopContent').css('top','34px');
			}
		} else { // All other browsers (ie. The ones that we do not support :p)
			$('#cmrpLeftColTopContent').css('top','35px');
		}
	
	// END BROWSER TWEAKS

	//$('div.loginborder').border('landingd2d-');
	//$('div.height').css("height", "130px");
	//$.getScript("http://content.united.com/ual/asset/swn-scripts-2007.js",function(){
		//determine login state
		if (isDefined(myCookies["rememberProfile"]) && isDefined(myCookies["isSecure"]) && myCookies["isSecure"]=="true") {
			var profile=myCookies["rememberProfile"];
			profile = unescape(profile);
			var parsedValue=profile.split("||");
			//var mpiNumber=parsedValue[0];
			var mpLevel=parsedValue[3];
			var mpBal=parsedValue[4];
			//var homeArpt=parsedValue[5];
			if (isDefined(myCookies["mpInformation"])){
				
				var profile=myCookies["mpInformation"];
				profile = unescape(profile);
				var parsedValue=profile.split("||");
				var prefix=parsedValue[0];
				var firstName=parsedValue[1];
				var middleinit=parsedValue[2];
				var lastName=parsedValue[3];
				var suffix= parsedValue[4];
				var emlErrFlg = parsedValue[5];
				var addrErrFlg = parsedValue[6];
				if ( (parsedValue.length >= 9 ) && ( isDefined("country") || country=="united" )){
					pbaoLandingPageSub = parseInt(parsedValue[7]);
					epuLandingPageSub =  parseInt(parsedValue[8]);
					rccLandingPageSub = parseInt(parsedValue[9]);
				}
				//alert("prefix: "+prefix+"\ninitial: "+middleinit+"\nsuffix: "+suffix);
			}
			
			// Hide the login boxes
			
			//31 characters print OK		
			if (firstName.length < 31 || (firstName.length > 31 && firstName.indexOf(" ") > -1 && firstName.indexOf(" ")  < 31))  {
				$('#loggedin').html('<span style="color:#000000;"><B>Hello ' +prefix+' '+ firstName+' ' +middleinit+ ' ' + lastName +' '+suffix +'</b></span>');
				
			} else {
				//give up, print HELLO only
				$('#loggedin').html('<div style="color:#000000;"><B>Hello </b></div>');
			}
			
			if (mpLevel != "" && mpBal != "") {
				$('#loggedin').append('<div style="white-space:nowrap;color:#000000;margin-top:5px">' + mpLevel + '</div>');
				$('#loggedin').append('<div style="white-space:nowrap;color:#000000;margin-top:5px">' + 'MP Visa Signature Card'+ '</div>');
				$('#loggedin').append('<div style="white-space:nowrap;color:#000000;margin-top:5px;margin-left:100px;margin-right:10px;text-align:right; font-weight: bold;">' + addCommas(mpBal)+'  Miles ' + '</div>');
			}
			var lang="en"// for now, only errors in english
			if (( typeof(country)== "undefined" || country=="united" )&& (emlErrFlg == "true" || addrErrFlg == "true")){
				$('#loggedin').append('<div style="color:#FFF794;">' + gblErr[lang][0]+' <a href="https://www.ua2go.com/ci/UpdateProfile.jsp">'+gblErr[lang][1]+'</a></div>');;
			}
			
			//This block is added for annual options -- starts here
			var countAO = 3;
			if((pbaoLandingPageSub >0 && pbaoLandingPageSub <4)){
				countAO--;
			}
			if((epuLandingPageSub>0 && epuLandingPageSub<4)){
				countAO--;
			}
			if((rccLandingPageSub >0 && rccLandingPageSub <4)){
				countAO--;
			}
			if((rccLandingPageSub >0 && rccLandingPageSub <4)|| (epuLandingPageSub >0 && epuLandingPageSub <4)|| (pbaoLandingPageSub >0&& pbaoLandingPageSub <4)){
				
				var topStyleLogin = countAO * 10;
				document.getElementById("loggedin").style.paddingTop= topStyleLogin+'px';
				
				$('#loggedin').append('<div style="clear:left;line-height:1px;"> </div><span style="white-space: nowrap;color:#EEEEEE;">Annual options</span>');
				if(epuLandingPageSub>0 && epuLandingPageSub<4){
					$('#loggedin').append('<div class=""><ul><li><a href="https://store.united.com/traveloptions/control/product?product_id=UM_EPA&category_id=UM_LEGRM&navSource=My+Profile+Subscriptions&linkTitle=UM_EPA">Economy Plus</a></li></ul></div>');
				}
				if(pbaoLandingPageSub >0 && pbaoLandingPageSub <4){
					$('#loggedin').append('<div class=""><ul><li><a href="https://store.united.com/traveloptions/control/product?product_id=UM_PBA&category_id=UM_BAGS&navSource=My+Profile+Subscriptions&linkTitle=UM_PBA">Premier Baggage</a></li></ul></div>');
				}
				if(rccLandingPageSub >0 && rccLandingPageSub <4){
					$('#loggedin').append('<div class=""><ul><li><a href="https://store.united.com/traveloptions/control/product?product_id=UM_RCC&category_id=UM_LOUNGES&navSource=My+Profile+Subscriptions&linkTitle=UM_RCC">Red Carpet Club</a></li></ul></div>');
				}
			}
			//This block is added for annual options -- ends here
			
			$('#loggedin').append('<div style="margin-right:10px;text-align:right;"><a href="http://www.united.com/updateprofile">My profile</a> ' +'|'+
			' <a href="https://www.ua2go.com/ci/Login.jsp?return_to=mpsummary_us">My mileage summary</a> '+'|'+
			' <a href="https://www.ua2go.com/ci/Logout.jsp?static=true">Log out</a></div>').show();
			//since we're logged in, hide the MP login and guest login divs, just display the condinue button.
			//$('#mi_notLogged, #cio_mplogin, #cio_gstlogin').hide();
			
			document.getElementById('loggedin').setAttribute('id','loggedin2')
			$('.removeMyId').hide();
			
		} else {  //display the log in module
			//look for remember cookies. If found fill in userid field
			
			if (myCookies["rememberME"] != undefined) {
				//	if (cookieAUTHTYPE!= undefined) {	
				switch (myCookies["rememberME"]) {
				case "E":
					var rememberEmail = myCookies["rememberEMAIL"];
					//fill 3 fields with this value
					if(rememberEmail) $('input.userId').val(rememberEmail);
					break;
					
				case "M":
					var rememberMP = myCookies["getMP"];
					//fill 3 fields with this value
					if (rememberMP) $('input.userId').val(rememberMP);
					break;
					
				}		//end switch
				
				// click remember me box 3 locations
				$.fn.check = function() {
					return this.each(function() {
						this.checked = true;
					});
				};
				//if value, check the checkbox
				$(".remMP").check();
			}//end if rememberME
			$('.removeMyId').show();
		}//end of else
	
	// SMS - ADDED FOR PSU-6096 To Set the Default Selected Option in the top right login box to Reedem Miles
	document.getElementById('sel_return_to').options[document.getElementById('sel_return_to').options.length-1].selected = true;
});

function handletheclick() {
	document.getElementById("topLoginSubmit").style.backgroundPosition = "0% -36px";
	document.getElementById("topLoginSubmit").href="javascript:void(0)";
	if (ValidateUserId(document.topLogin)) {
		document.topLogin.submit()
	} else {
		document.getElementById("topLoginSubmit").href="javascript:handletheclick()";
		document.getElementById("topLoginSubmit").style.backgroundPosition = "0% 0";
	}
}
