// 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;
}



var tktError_fr = "Veuillez saisir un numéro de confirmation valide (5MHL6M par ex.) ou bien les 13 chiffres de votre billet électronique (1234567891012 par ex.). Vous trouverez ces deux numéros sur votre confirmation d’itinéraire.\n";

function ecodlSubmit2_fr(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_fr;}
		else if (searchValue.length < 5) {msg+=tktError_fr;}
		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_fr;}
//		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+="Saisissez le nom de famille tel qu’il apparaît.\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='Merci de fournir les informations suivantes.\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;
   }
	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='Mileage Plus # 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 Mileage Plus 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 Mileage Plus 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);
//		}
	return true;
}




function ValidateUserId2_fr (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='Numéro Mileage Plus ou adresse email sont des champs obligatoires.\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 Mileage Plus 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 Mileage Plus 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+='Le mot de passe est un champ obligatoire.\n';   	
	}
	if (msg) {
        msg='Merci de fournir les informations suivantes.\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);
//		}
	return true;
}

// FS7 CenterTile link extraction function  -- S. Smull 2-3-09   
function vsChangeFlashHrefToThis(sNewHref) {
	vsLinkClicked = sNewHref;
	window.location.assign(sNewHref); //.replace doesn't load in session history, assign does
}		

// FS7 CenterTile fading function  -- S. Smull 2-3-09
	
function doTheFade(sStartColor, sColor) {
	//append color string with #
	var reg= /^#/;
	if(!reg.test(sColor)) {sColor = "#" + sColor; }
	
	// Fade  "hpDivsToFade" class at 1 sec
	$('.hpDivsToFade').animate({ backgroundColor: sColor }, 1000);
}

function NoFlashCenterTile(counLang){ 
	$.get("/local/dynamic/fs9_"+counLang+".xml",{},function(xml){
	var theXML;
		if($.browser.msie){
		theXML = new ActiveXObject( 'Microsoft.XMLDOM');
		theXML.async = false;
		theXML.loadXML(xml);
	} else {
		theXML = xml;
	};
	
		var selPro=(Math.floor(Math.random()*$('slide',theXML).length));	// Select random slide
		var ourbgcolor=$('slide',theXML).eq(selPro).attr('rgb');			// Select color attribute from selected slide
		myHTMLOutput = '<a href="'+$('slide',theXML).eq(selPro).attr('url')+'"><img border="0" src="'+$('slide',theXML).eq(selPro).attr('src')+'"></a>';
		$("#hpCenterTile").html(myHTMLOutput); //write the Center Tile with the selected static image
		
		//apply the color to the Background of the two tiles
		$('.hpFSAbackground').css('background-color',"#"+ourbgcolor);
	});
}	