/* Release 5.2 */

/*
 * 
 * This file has been deprecated. Please change references to: /ual/asset/ua.united.js
 * If changes are absolutely necessary to this file, please make sure they are also 
 * reflected in the new file as well. - Rob 12/7/10
 * 
 */

// JavaScript Document common library

String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/gi,"") };


function validateMP(MPNO){
	if (!/^\d{11}$/.test(MPNO) ) {return false;}
	var factor = [5,4,3,2,7,6,5,4,3,2]; var check = 0;
	for (var digit = 0; digit < 10; digit++)
		check += MPNO.charAt(digit) * factor[digit];
	check = (((11 - (check % 11)) % 11) % 10);
	return (check == MPNO.charAt(10));
//	var digits = MPNO.split("",10);
//	for (i in digits) check += digits[i]* factor[i]
//	check = (((11 - (check % 11)) % 11) % 10);
//	return (check == MPNO.charAt(10));
	
}

function validateEml(str) {
//	re = new RegExp("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.{1}[a-zA-Z]{2,6}$");
	re = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]{0,4}[a-zA-Z]$/;
	return (re.test(str));
}

function validateCC(cardNo) {
/*
Visa	4-4-4-4, prefix 4 (4111 1111 1111 1111)
American Express	4-6-5, prefix 37-34  (3400 000000 00009)
Carte Blanche	3000 0000 0000 04
Discover	4-4-4-4, prefix 6011 (6011 0000 0000 0004)
Diner's Club	3000 0000 0000 04
enRoute	2014 0000 0000 009
JCB	2131 0000 0000 0008
MasterCard	51-55, 5500 0000 0000 0004
Solo	6334 0000 0000 0004
Switch	4903 0100 0000 0009
UATP 101657900007043
*/

	var nCheck = 0,	nDigit = 0,	bEven = false;
	cardNo = cardNo.replace(/\D/g, ""); 
	//confirm("here is stripped card: "+cardNo+"\ncard length: "+cardNo.length);
	cardRe = /^\d{13,16}$/;
	if(!cardRe.test(cardNo)) { return false;} 
//LUHN10 check
//	for (var n = cardNo.length - 1; n >= 0; n--) {
//		var cDigit = cardNo.charAt(n);
//		var nDigit = parseInt(cDigit, 10);
//		if (bEven) {	if ((nDigit *= 2) > 9) nDigit -= 9;		}
//		nCheck += nDigit;
//		bEven = !bEven;
//	}
//	return (nCheck % 10) == 0;
return true;
}

var tktError = "Please enter your valid confirmation number (e.g. 5M4L6M) or your 13-digit e-ticket number (e.g. 0160987654321). Both numbers can be found on your travel itinerary.\n";

function ecodlSubmit2(f)	{
	var msg="";
	var searchValue = f.searchValueId.value.replace(/[\(\)\.\-\s,]/g, "");
	f.searchValueId.value = searchValue;//put it back
	if ($("#mi_cc").is(":checked")) {
//		$('#mi_searchValueId').attr("name", 'ccNumber');
			searchValue = searchValue.replace(/\D/g, "");
			f.searchValueId.value = searchValue;//put it back
		if(searchValue == ""){msg+="Please enter the credit card number.\n"; } 

		else if(!validateCC(searchValue)) {msg+="Please enter a valid credit card number.\n"; }
	}
	else if ($("#mi_pnr").is(":checked")) {//confirm("checking ci_pnr");
		if (searchValue =="") {msg+=tktError;}
		else if (searchValue.length < 5) {msg+=tktError;}
		else if (searchValue.length <= 6) {
			if (!/^[a-zA-Z0-9]{5,6}$/.test(searchValue)) {
				msg+='Confirmation number should only contain letters and numbers.\n'
			}	else {
				$('#authForm2 #mi_pnr').val('pnr');
//				$('#ci_searchValueId').attr("name", 'pnrNumber');
			}
		}
		else if (!/^\d{13}$/.test(searchValue)){msg+=tktError;}
//		else if (searchValue.substr(0,3) != "016") {msg+='e-ticket must begin with "016."\n';}
		else {//confirm("good e-ticket value");
				$('#authForm2 #mi_pnr').val('tkt');
//				$('ci_searchValueId').attr("name", 'tktNumber');
		}//end of e-ticket validation
		
	}
	
	var lastName = f.lastName.value;
	if(!lastName){msg+="Please enter the last name as it appears on your itinerary.\n"} // check for empty form elements
	else 
	{
		lastName = lastName.trim();//leading trailing
		f.lastName.value = lastName;//put it back
		if(!/^[a-zA-Z]{1,40}$/.test(lastName))msg+="Last name should only contain letters.\n"; // check for empty form elements
	}

	if (msg) {
        msg='Please Supply the Following:\n\n' + msg;
        alert(msg);
        return false;
   }
	return true;
}

function ecodlSubmit3(f)	{
	var msg="";
	var searchValue = $('#ci_searchValueId').val().replace(/[\(\)\.\-\s,]/g, "");
	$('#ci_searchValueId').attr("value", searchValue);//put it back
	if ($("#ci_cc").is(":checked")) {
			searchValue = searchValue.replace(/\D/g, "");
			$('#ci_searchValueId').val(searchValue);//put it back
			$('#ci_searchValueId').attr("name", 'ccNumber');
			if(searchValue == ""){msg+="Please enter the credit card number.\n"; } 
			else if(!validateCC(searchValue)) {msg+="Please enter a valid credit card number.\n"; }
	}
	else if ($("#ci_pnr").is(":checked")) {
		if (searchValue=="") {msg+=tktError;}
		else if (searchValue.length < 5) {msg+=tktError}
		else if (searchValue.length <= 6) {
			reCipnr = /^[a-zA-Z0-9]{5,6}$/;
			if (!reCipnr.test(searchValue)) {
				msg+='Confirmation number should only contain letters and numbers.\n'
			}	else {
				$('#ci_pnr').val('pnr');
				$('#ci_searchValueId').attr("name", 'pnrNumber');
			}
		}
		else if (!/^.{13}$/.test(searchValue)){msg+=tktError;}
		else if (!/^\d{13}$/.test(searchValue)) {msg+='The e-ticket should only contain numbers.\n'}
//		else if (searchValue.substr(0,3) != "016") {msg+='e-ticket must begin with "016."\n';}
		else {
				$('#ci_pnr').val('tkt');
				$('#ci_searchValueId').attr("name", 'tktNumber');
		}//end of e-ticket validation
	}
	var lastName = f.lastName.value;
	if(!lastName){msg+="Please enter the last name as it appears on your itinerary.\n"} // check for empty form elements
	else 
	{
		lastName = lastName.trim();//leading trailing
		f.lastName.value = lastName;//put it back
		if(!/^[a-zA-Z]{1,40}$/.test(lastName))msg+="Last name should only contain letters.\n"; // check for empty form elements
	}

	if (msg) {
        msg='Please Supply the Following:\n\n' + msg;
        alert(msg);
        return false;
   }
   if (typeof removeFlash != "undefined") {removeFlash(); }; // Validateuserid from ajax.js SHD 2-15-2010
	return true;
}

function ValidateUserId2 (f) {	

	var searchValue = f.userId.value.replace(/^\s*|\s*$/g,''); 
//	var pwd = f.password.value.replace(/^\s*|\s*$/g,'');
	var pwd = f.password.value;
	var msg='';
	if (searchValue.length == 0) {msg='MileagePlus # or email address is a required field.\n';  }
 
	else if (/^\d{1,11}$/.test(searchValue)) {
		while (searchValue.length<11) {searchValue="0"+searchValue} ;//pad left with 0 until length 11
		f.userId.value = searchValue;

		if (validateMP(searchValue)) {
			f.authType.value="mp"; 
//			if (f.remMP.checked == true) {$.cookie("getMP", searchValue, {expires:730, path: '/'}); $.cookie("rememberME", "M", {expires:730, path: '/'}); }
		}	else {
				msg='The MileagePlus number you have entered is invalid. Please check the number and resubmit.\n';
		}//end testing for mpno
	//try to validate for email now
	} else if (validateEml(searchValue)) {
		f.authType.value="ea";
//		if (f.remMP.checked == true) {$.cookie("rememberEMAIL", searchValue, {expires:730, path: '/'}); 	$.cookie("rememberME", "E", {expires:730, path: '/'});	}
	} else {
	 	msg='The MileagePlus number or the email address you have entered is invalid. Please check the number or email address and resubmit.\n'; 
	}
	
	if (pwd.length==0) {
		msg+='Password is a required field.\n';   	
	}
	if (msg) {
        msg='Please Supply the Following:\n\n' + msg;
        alert(msg);
        return false;
   }
		//remove cookies if unchecked
//		if (f.remMP.checked != true) {
//		$.cookie('getMP', null);
//		$.cookie('rememberEMAIL', null);
//		$.cookie('rememberME', null);
//		}
	if (typeof removeFlash != "undefined") {removeFlash(); }; //validateuserid2 from ajax, if necessary SHD 2-15-2010
	return true;
}

var popupWin; 
function popItUp(url){
    popupWin=window.open(url,'remote','width=500,height=400,scrollbars=yes');
		//alert("here is popup typeof: "+typeof popupWin);
    if (window.focus) {popupWin.focus()}
    //return false;
}


/*Code added by TCS Offshore Starts */
function ValidateUserId3 (mpNum,pwd) {	

	var searchValue = mpNum.replace(/^\s*|\s*$/g,'');

	
	var msg='';
	if (searchValue.length == 0) {msg='MileagePlus # or email address is a required field.\n';  }
 
	else if (/^\d{1,11}$/.test(searchValue)) {
		while (searchValue.length<11) {searchValue="0"+searchValue} ;//pad left with 0 until length 11
		mpNum = searchValue;

		if (validateMP(searchValue)) {
			//f.authType.value="mp"; 

		}	else {
				msg='The MileagePlus number you have entered is invalid. Please check the number and resubmit.\n';
		}//end testing for mpno
	//try to validate for email now
	} else if (validateEml(searchValue)) {
		//f.authType.value="ea";

	} else {
	 	msg='The MileagePlus number or the email address you have entered is invalid. Please check the number or email address and resubmit.\n'; 
	}
	
	if (pwd.length==0) {
		msg+='Password is a required field.\n';   	
	}
	if (msg) {
        msg='Please Supply the Following:\n\n' + msg;
        alert(msg);
        return false;
   }

	return true;
}


/*Code added by TCS Offshore Ends */
