/* Release 5.2 */

// loaded on relese night 

//BUILD COOKIE ARRAY
var myCookies = [];
var c = document.cookie;
if(c.length){
	tempcookies = c.split(';');
	var i, l, oneCookie, cookieName;
	i = 0;
	l = tempcookies.length;
	for(;i<l;i++){
		oneCookie = tempcookies[i].split('=');
		cookieName = oneCookie[0].replace(/^\s+|\s+$/, '');
		myCookies[cookieName] = oneCookie[1];
	
	}

}


//USED TO POPULATE MILEAGEPLUS SELECT BOX
var StartinSelectLst = [
	["My MileagePlus","&return_to=mp_review","SI~MP~XX"],
	["My itineraries","&return_to=newitin_us", "IC"],
	["-------------------","",""],
	["Shop for flights","&return_to=booking_us","PB"],
	["Book with miles","&return_to=awardShop_us",""],
	["Red Carpet Club","&return_to=rcc&echoParam=rcchome",""],
	["-------------------","",""],
	["Check-in online","&return_to=easy_checkin",""],
	["Change itineraries","&return_to=rax",""],
	["Upgrades","&return_to=eug_sum",""],
	["Ticket refunds","&return_to=refund",""],
	["-------------------","",""],
	["My profile","&whereto=UpdateProfile.jsp",""],
	["Flight notification","&return_to=centerPost",""],
	["My trip templates","&return_to=triptemplate",""]

];
function writeStartIn(){
	var a1, a2, i1, i2, l1, l2, f, o, e;
	a1 = StartinSelectLst;
	a2 = document.getElementsByName('sel_return_to');
	i1 = 0;
	i2 = 0;
	l1 = a1.length;
	l2 = a2.length;
	f = document.createDocumentFragment();
	o = document.createElement('option');
	
	//Create option list and append to document fragment
	for(;i1<l1;i1++){
		e = o.cloneNode(false);
		e.appendChild(document.createTextNode(a1[i1][0]));
		e.value = a1[i1][1];
		if(a1[i1][2].indexOf(selectedSection) > -1) e.selected = true;
		f.appendChild(e);
	
	}
	
	//Append option list to all select boxes with a name of 'sel_return_to'
	for(;i2<l2;i2++){
		a2[i2].appendChild(f);
	
	}
  
}

function logout() {
	document.location.replace("https://www.ua2go.com/ci/Logout.jsp?static=true");

}


	
//common functions
	var colorArray = new Array;
	colorArray['Gold'] = 'red';
	colorArray['1K'] = '#ffffff';
	colorArray['Premier Executive'] = '#fbd571';
	colorArray['US Airways Chairman'] = 'red'
	colorArray['Premier'] = '#c7c7c2';
	colorArray['Silver'] = 'red';
	colorArray['Premier Emeritus'] = 'red';
	colorArray['Global Services'] = '#ffffff';
	colorArray['VIP'] = 'red';
	colorArray['Premier Associate'] = '#ffffff';
	colorArray['Member'] = '#6dccf1';

	
function  colorHderText(level, objId){

		if (colorArray[level] !== 'undefined') {
			var memLevel = level;
			if(memLevel.indexOf("1K")>=0){
				memLevel="1K";
			}else if(memLevel.indexOf("Premier Executive")>=0){
				memLevel="Premier Executive";
			}else if(memLevel.indexOf("Global Services")>=0){
				memLevel="Global Services";
			}else if(memLevel.indexOf("Premier Associate")>=0){
				memLevel="Premier Associate";
			}else if(memLevel.indexOf("Member")>=0){
				memLevel="Member";
			}else if(memLevel.indexOf("Premier")>=0){
				memLevel="Premier";
			}	
			$("#"+objId).css('color',colorArray[memLevel]);
		}
}	
	

function isDefined(variable){//alert("in isdefined with :"+variable+"\ngoing to return: "+(!(!( variable||false ))));
	return (!(!( variable||false )))
	//return eval('(typeof('+variable+') != "undefined");');
}

String.prototype.commafy = function() {
 return this.replace(/(\D?)(\d{4,})/g, function($0, $1, $2) {
 return /[.\w]/.test($1) ? $0 : $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, '$&,');
 });
};
Number.prototype.commafy = function() {
 return String(this).commafy();
}; 


function TrimString(_data) {
return _data.replace(/^\s+|\s+$/g, '');
}

function chkBrs(sInp) {
	var sUA=navigator.userAgent.toLowerCase();
	var bFound=sUA.indexOf(sInp) +1;
	return bFound;
}

	function elevenLength(MPNO) {
    var id = TrimString(MPNO.value);
    if (MPNO && id!="" && id.length < 11) {
        while (id.length < 11) {
            id="0"+id;
        }
		MPNO.value = id;
    }
}


function isDigit(c) {
    return((c>="0")&&(c<="9"))
}
function isCharInString(sc,s) {
    return(!sc||!s)?false:(s.indexOf(sc)>-1)?true:false;
}

function isMpNumber(MPNO){
		 if(MPNO != null && MPNO.length == 11 && !(MPNO =="00000000000")) {
				var s = MPNO;
				var  w60count1 =0;
				var  w60count2 = 0;
				w60count1 = s.substring(0,1) * 5 +
							s.substring(1,2) * 4 +
							s.substring(2,3) * 3 +
							s.substring(3,4) * 2 +
							s.substring(4,5) * 7 +
							s.substring(5,6) * 6 +
							s.substring(6,7) * 5 +
							s.substring(7,8) * 4 +
							s.substring(8,9) * 3 +
							s.substring(9,10) * 2;

			 var w60remainder = w60count1 % 11;
			   w60count1 = w60count1/11;
			  if (w60remainder != 0){
			      w60count2 = 11 - w60remainder;
			   } else {
			      w60count2 = 0;
			   }
			  if (w60count2 == 10){
			   		w60count2 = 0;
			   }
			  if (w60count2 != s.substring(10)){
				  	return false;
			   } else { 
				return true; 
			   }
			}
			
	return false;
}	
/*
function chngbox(){
	if(document.getElementById("remMP").checked == false){
	deleteCookie("getMP","/")
	}
	// this is for retriving the MileagePlus number.
	var gotMPno = null;
	var rememberME = getCookie("rememberME");
	if (rememberME!= undefined && rememberME=="E") {
		gotMPno = getCookie("rememberEMAIL");
	}
	if (rememberME!= undefined && rememberME=="M") {
		gotMPno = getCookie("getMP");
	}
	if (gotMPno != null) { 
 	 document.getElementById("userId").value=gotMPno;
	 document.getElementById("remMP").checked=true;	 
	// var imageObject = document.getElementById("rememberMeImage");
	 //imageObject.src="http://www.united.com/ual/asset/MP_activity_checked_icon.gif";
	}
}
*/
/*
function IsNumeric(a,addChars) {
    	var i;
    	if (a.value.length == 0) {
        return true;
    	}
    	for(i=0;i < a.value.length;i++) {
        var c=a.value.charAt(i);
        if (!isDigit(c) && !isCharInString(c,addChars)) {
            return false;
        }
    	}
    	return true;
}
*/
function IsUserIdNumeric(a,addChars) {
    var i;
	var id = TrimString(a.value);
    if (id.length == 0) {
        return true;
    }
    for(i=0;i < id.length;i++) {
        var c = id.charAt(i);
        if (!isDigit(c) && !isCharInString(c,addChars)) {
            return false;
        }
    }
    return true;
}

function remMP(name) 
{   
	var today = new Date();
	var expires = new Date();
	expires.setTime(today.getTime() + 1000*60*60*24*365*2)
	setCookie("getMP", name, expires);
}

function setCookie(name, value, expire) {  
	var curCookie = name + "=" + escape(value) +
   ((expire) ? "; expires=" + expire.toGMTString() : "") + "; path=/";
	document.cookie = curCookie;
}	

function getCookie(Name) {
   var search = Name + "=";
   if (document.cookie.length > 0) { // if there are any cookies      
		offset = document.cookie.indexOf(search)       
		if (offset != -1) { 
			// if cookie exists         
	 		offset += search.length          
	 		// set index of beginning of value        
	 		end = document.cookie.indexOf(";", offset)         
	 		// set index of end of cookie value        
	 		if (end == -1)             
	 			end = document.cookie.length         
	 		return unescape(document.cookie.substring(offset, end))      
	 	}    
	 }
}

function deleteCookie(name,path) {
  	if (getCookie(name)) {
    	document.cookie = name + "=" + 
    	((path) ? "; path=" + path : "") +
    	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
  	}
}


//This function is used by several forms posting mp number in addition to the login forms.	
//Further only login has dual capability i.e login using mp number & email address. There are 
//several other functionality that use only mp number. The function can handle both these variations.
//Also there are 2 different forms on login forms. There is one type that has a start in box, and
//there is another type that does not have a startin box. This function takes care of both these
//scenarios so that valid login data is passed.	
function ValidateUserId (f) {	
	var fromLoginForm = true;
	var formName = f.name;
	if(f.name != "headerLogin") {
		fromLoginForm = false;	
	}
	var MPNO = f.userId;
	var id = TrimString(MPNO.value); //lop off spaces, pad out front to 11 with 000
	var cr='\n';
	var msg='';
	var isMP = false;
	var startInDropDownExists = false;
	var valid = true;
	var pwd = TrimString(f.password.value);
	if (id.length == 0) {
		valid = false;
		if (fromLoginForm) {
			msg='MileagePlus # or email address is a required field. ' + cr; 
		}
		else {
			msg='MileagePlus # is a required field. ' + cr; 
		}
	}
	if (valid) {
		var isNum = IsUserIdNumeric(MPNO, null);
		if (isNum) {
			isMP = true;
			elevenLength(MPNO);
			id = TrimString(MPNO.value);
		}
		if (isMP) {
			valid = isMpNumber(id);
			if (!valid) {
				msg='The MileagePlus number you have entered is invalid. Please check the number and resubmit.' + cr;
			}
		}
		else {
			valid = isEmail(id);
			if (!isMP & !valid) {
				msg='The MileagePlus number or the email address you have entered is invalid. Please check the number or email address and resubmit.' + cr; 
			}
		}
	}
	if (fromLoginForm && pwd.length==0) {
		msg+='Password is a required field.' + cr    	
	}
	if (msg) {
		msg='Please Supply the Following:' + cr + cr + msg;
		alert(msg);
		return false;
	}
	
	if (fromLoginForm && document.headerLogin.sel_return_to != null) {
		startInDropDownExists = true; 
	}
	if (startInDropDownExists) {
		document.headerLogin.action="https://www.ua2go.com/ci/DoLogin.jsp?stamp=NEWCOOKY*itn/ord=NEWREC,itn/air/united"+f.sel_return_to.value; 
		
	}
	else {
		document.headerLogin.action="https://www.ua2go.com/ci/login.do";
	}
	
	//disable the login button
	disableGrnBttn(".primaryGrnBttn");
	
	return true;
}

function disableGrnBttn(containerHandle) {
	$(containerHandle).addClass('disable')
	.find('a').attr('href', 'javascript: void(0)');
}


function isEmail(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1) {
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;					
	}
	







