var pointsPerDollar = 1;
var bonusPoints = 0;
var pointsPerFlightShort = 15000;
var pointsPerFlightLong = 25000;
var pointsPerEsso = 6500;
var pointsPerHardware = 6500;
var currentPoints = 0;
var currentFlights = 0;
var option = 0
var optionSB = 0
var ddmiles = 0;		//direct deposit miles
var pamiles = 0;		//pre auth miles
var rewardType = 0;
var rewardUnit = 0;
var rewardImage = 0;
var rewardImageWidth = 0;
var rewardImageHeight = 0;
//defining month variables added sep 15 08
var oct = 0;
var nov = 0;
var dec = 0;
var welcomeBonusCheck="no";		//set as null to begin
var selectedOption ="";


var SelectRewardTemp = 0;   // used only when 1 reward

/*For the tool pop-up; if being sent from visa or chequing page */
var srchstring = unescape(location.search.substring(1, location.search.length));
 if (srchstring.length > 0){
   querryArr = srchstring.split (";");
   	selectedOption = (querryArr[0]);	//determines drop down list option
   	directdeposit = (querryArr[1]);
	oct = (querryArr[2]);
	nov = (querryArr[3]);
	dec = (querryArr[4]);
	monthly = (querryArr[5]);
	othermonthly = (querryArr[6]);
	welcomeBonusCheck="yes";
	
	if(	directdeposit == ""){		//if fields are left blank change to zero
		directdeposit=0;
	}
	if(	othermonthly == ""){
		othermonthly=0;
	}
	if(	monthly == ""){
		monthly=0;
	}
	
	
}else{
	option22 = "";
    directdeposit ="";
	oct ="";
	nov ="";
	dec ="";
	monthly = "";
	othermonthly ="";
    welcomeBonusCheck="no";
}/*For the tool pop-up end **************************************/

/********* Business Banking ****************/
function addCommas(nStr)	{
	if((window.location.href).indexOf('-fr.html')!='-1') {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '&nbsp;' + '$2');
	}
	return x1 + x2;
	
	}else {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
	
	}
}//end addCommas

function WelcomeBonus() {

	if((window.location.href).indexOf('-fr.html')!='-1') {
			document.getElementById('WelcomeBonus').innerHTML = ('30&nbsp;000');
			option = '0';
		}else {
			document.getElementById('WelcomeBonus').innerHTML = (addCommas('30000'));
			option = '0';
		}
		
		
	if (document.VisaTool.SelectCard.value == '0') {
		
		
		if((window.location.href).indexOf('-fr.html')!='-1') {
			document.getElementById('WelcomeBonus').innerHTML = ('30&nbsp;000');
			option = '0';
		}else {
			document.getElementById('WelcomeBonus').innerHTML = (addCommas('30000'));
			option = '0';
		}showOtherVisa ();
		showDirectDeposit ();
		showGasGrocery ();
		//hideaeroClassicField();
		//showMonthlyDisplay ();
	}else if (document.VisaTool.SelectCard.value == '1') {
		showGasGrocery ();
		hideDirectDeposit ();
		showOtherVisa ();
		//hideaeroClassicField();
		//hideMonthlyDisplay ();
		if((window.location.href).indexOf('-fr.html')!='-1') {
			document.getElementById('WelcomeBonus').innerHTML = ('30&nbsp;000');
			option = '1';
		}else {
			document.getElementById('WelcomeBonus').innerHTML = (addCommas('30000'));
			option = '1';
		} 
	}else if (document.VisaTool.SelectCard.value == '2') {
		hideGasGrocery ();
		showDirectDeposit ();
		hideOtherVisa ();
		//hideaeroClassicField();
		//showMonthlyDisplay ();
		if((window.location.href).indexOf('-fr.html')!='-1') {//uca
			document.getElementById('WelcomeBonus').innerHTML = ('10&nbsp;000');		
			option = '2';
		}else {
			document.getElementById('WelcomeBonus').innerHTML = (addCommas('10000'));
			option = '2';
		} 
	}else if (document.VisaTool.SelectCard.value == '3') {			//aerogold infinite
		showGasGrocery ();
		hideDirectDeposit ();
		showOtherVisa ();
		//hideaeroClassicField();
		//hideMonthlyDisplay ();
		if((window.location.href).indexOf('-fr.html')!='-1') {
			document.getElementById('WelcomeBonus').innerHTML = ('15&nbsp;000');		//aerogold visa infinte
			option = '3';
		}else {
			document.getElementById('WelcomeBonus').innerHTML = (addCommas('15000'));
			option = '3';
		} 
	}else if (document.VisaTool.SelectCard.value == '4') {
		showGasGrocery ();
		showDirectDeposit ();
		showOtherVisa ();
		//hideaeroClassicField();
		//showMonthlyDisplay ();
		if((window.location.href).indexOf('-fr.html')!='-1') {
			document.getElementById('WelcomeBonus').innerHTML = ('30&nbsp;000');
			option = '4';
		}else {
			document.getElementById('WelcomeBonus').innerHTML = (addCommas('30000'));
			option = '4';
		}
	}else if (document.VisaTool.SelectCard.value == '5') {				//not used
		hideGasGrocery ();
		hideDirectDeposit ();
		hideOtherVisa ();
		//showaeroClassicField();
		if((window.location.href).indexOf('-fr.html')!='-1') {
			document.getElementById('WelcomeBonus').innerHTML = ('2&nbsp;500');
			option = '5';
		}else {
			document.getElementById('WelcomeBonus').innerHTML = (addCommas('2500'));
			option = '5';
		}
	}else if (document.VisaTool.SelectCard.value == '6') {				//not used
		showGasGrocery ();
		hideDirectDeposit ();
		showOtherVisa ();
		if((window.location.href).indexOf('-fr.html')!='-1') {
			document.getElementById('WelcomeBonus').innerHTML = ('15&nbsp;000');
			option = '6';
		}else {
			document.getElementById('WelcomeBonus').innerHTML = (addCommas('15000'));
			option = '6';
		} 
	}
}//end welcome bonus function


/*************** Aeroplan Tool Functions *****************/

function hideGasGrocery () {
		document.getElementById('gasGrocery').style.visibility = 'hidden'; 
		document.getElementById('gasGrocery').style.display = 'none'; 
	}
function showGasGrocery () {
		document.getElementById('gasGrocery').style.visibility = 'visible'; 
		document.getElementById('gasGrocery').style.display = 'block'; 
	}
	
function hideDirectDeposit () {
		document.getElementById('directDeposit').style.visibility = 'hidden'; 
		document.getElementById('directDeposit').style.display = 'none'; 
	}
function showDirectDeposit () {
		document.getElementById('directDeposit').style.visibility = 'visible'; 
		document.getElementById('directDeposit').style.display = 'block'; 
	}
	
function hideOtherVisa () {
		document.getElementById('otherVisa').style.visibility = 'hidden'; 
		document.getElementById('otherVisa').style.display = 'none'; 
	}
function showOtherVisa () {
		document.getElementById('otherVisa').style.visibility = 'visible'; 
		document.getElementById('otherVisa').style.display = 'block'; 
	}
	
/*function hideaeroClassicField () {
		document.getElementById('aeroClassicField').style.visibility = 'hidden'; 
		document.getElementById('aeroClassicField').style.display = 'none'; 
	}
function showaeroClassicField () {
		document.getElementById('aeroClassicField').style.visibility = 'visible'; 
		document.getElementById('aeroClassicField').style.display = 'block'; 
	}

function hideMonthlyDisplay () {
		document.getElementById('monthlyDisplay').style.visibility = 'hidden'; 
		document.getElementById('monthlyDisplay').style.display = 'none'; 
	}
function showMonthlyDisplay () {
		document.getElementById('monthlyDisplay').style.visibility = 'visible'; 
		document.getElementById('monthlyDisplay').style.display = 'block'; 
	}*/
/*************** Aeroplan Tool Functions END*****************/	

function updateBonus(){
	if (document.VisaTool.SelectCard.value == '0'){
		bonusPoints = 30000;
		pointsPerDollar = 1.5;		//monthly purchase amount text box
		pointsPerDollarOther = 1;		//other monthly purchase amount text box
	}else if (document.VisaTool.SelectCard.value == '1'){
		bonusPoints = 30000;
		pointsPerDollar = 1.5;
		pointsPerDollarOther = 1;
	}else if (document.VisaTool.SelectCard.value == '2'){
		bonusPoints = 10000;
		pointsPerDollar = 1.5;
		pointsPerDollarOther = 1;
	}else if (document.VisaTool.SelectCard.value == '3'){
		bonusPoints = 15000;
		pointsPerDollar = 1.5;
		pointsPerDollarOther = 1;
	}
	else if (document.VisaTool.SelectCard.value == '4'){
		bonusPoints = 30000;
		pointsPerDollar = 1.5;
		pointsPerDollarOther = 1;
	}
	else if (document.VisaTool.SelectCard.value == '5'){
		bonusPoints = 2500;
		pointsPerDollar = .5;
		pointsPerDollarOther = .5;
	}
	else if (document.VisaTool.SelectCard.value == '6'){		//not used
		bonusPoints = 15000;
		pointsPerDollar = 1.5;
		pointsPerDollarOther = 1;
	}
}//end update bonus


function updatePoints(){
	//if(document.VisaTool.october.checked){oct = 100;}	remove october assign 0
	//else{oct = 0;}
	//if(document.VisaTool.november.checked){nov = 100;}
	//else{nov = 0;}
	oct = 0;
	nov = 0;
	dec=0;
	
	//if(document.VisaTool.december.checked){dec = 100;}
	//else{dec = 0;}

  var validationStatus = true;			//Validation Begins -blank fields and special characters
    if (option == '0'|| option == '4'){
  
  	if(document.VisaTool.DirectDeposit.value== ''){
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez inscrire le nombre de dépôts directs au compte-chèques MaxiOpérations par mois.');
		}else{
			alert('Please enter the number of Unlimited Chequing Account direct deposits per month.');
			}
		validationStatus = false;
		
	}	
	else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.DirectDeposit.value),'currency')){
		validationStatus = false;
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez inscrire le nombre de dépôts directs au compte-chèques MaxiOpérations par mois.');
		}else{
			alert('Please enter the number of Unlimited Chequing Account direct deposits per month.');
		}
	}
	else if(document.VisaTool.DirectDeposit.value > 12){	//**Determine which month recieves bonus ie. Oct, Niv, Dec
		validationStatus = false;
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer le nombre de mois 0-12.');
		}else{
			alert('Please enter the number of months from 0-12.');
		}
	}//** month recieves bonus end
	else if(document.VisaTool.MonthlyPurchaseAmount.value== ''){
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer la valeur approximative de vos autres achats mensuels.');
		}else{
			alert('Please enter the approximate value of your monthly purchases.');
		}
		
		validationStatus = false;
	}else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.MonthlyPurchaseAmount.value),'currency')){
		
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer la valeur approximative de vos autres achats mensuels.');
		}else{
			alert('Please enter the approximate value of your monthly purchases.');
		}
	validationStatus = false;
	}
	else if(document.VisaTool.OtherMonthlyPurchaseAmount.value== ''){
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer la valeur approximative de vos autres achats mensuels.');
		}else{
			alert('Please enter the approximate value of your other monthly purchases.');
		}
		
		validationStatus = false;
	}else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.OtherMonthlyPurchaseAmount.value),'currency')){
		
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer la valeur approximative de vos autres achats mensuels.');
		}else{
			alert('Please enter the approximate value of your other monthly purchases.');
		}
	validationStatus = false;
	}
  }//end of option 0 ******************************************* validation
	
  if (option == '1' || option=='3' ){
	if(document.VisaTool.MonthlyPurchaseAmount.value== ''){
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer le total de vos achats effectués mensuellement dans des commerces liés aux voyages.');
		}else{
			alert('Please enter the number of travel related monthly purchases.');
		}
		
		validationStatus = false;
	}else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.MonthlyPurchaseAmount.value),'currency')){
		
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer le total de vos achats effectués mensuellement dans des commerces liés aux voyages.');
		}else{
			alert('Please enter the number of travel related monthly purchases.');
		}
	validationStatus = false;
	}
	else if(document.VisaTool.OtherMonthlyPurchaseAmount.value== ''){
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer le total de vos achats portés mensuellement à la carte Aéro Or CIBC VISA pour PME.');
		}else{
			alert('Please enter the number of other CIBC Aerogold VISA Card for Business purchases per month.');
		}
		
		validationStatus = false;
	}else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.OtherMonthlyPurchaseAmount.value),'currency')){
		
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer le total de vos achats portés mensuellement à la carte Aéro Or CIBC VISA pour PME.');
		}else{
			alert('Please enter the number of other CIBC Aerogold VISA Card for Business purchases per month.');
		}
	validationStatus = false;
	}
   } //end of option 1 & 3 **************************************validation
  
   if (option == '2'){
	if(document.VisaTool.DirectDeposit.value== ''){
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez inscrire le nombre de dépôts directs au compte-chèques MaxiOpérations par mois.');
		}else{
			alert('Please enter the number of Unlimited Chequing Account direct deposits per month.');
		}
		
		validationStatus = false;
	}else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.DirectDeposit.value),'currency')){
		
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez inscrire le nombre de dépôts directs au compte-chèques MaxiOpérations par mois.');
		}else{
			alert('Please enter the number of Unlimited Chequing Account direct deposits per month.');
		}
	validationStatus = false;
	}else if(document.VisaTool.DirectDeposit.value > 12){	//**Determine which month recieves bonus ie. Oct, Niv, Dec
		validationStatus = false;
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer le nombre de mois 0-12.');
		}else{
			alert('Please enter the number of months from 0-12.');
		}
	}//** month recieves bonus end
	
 } //end of option 2 **************************************validation
 
 if (option == '5'){
	if(document.VisaTool.aeroClassicField.value== ''){
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer la valeur approximative de vos autres achats mensuels.');
		}else{
			alert('Please enter the approximate value of your monthly purchases.');
		}
		
		validationStatus = false;
	}else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.aeroClassicField.value),'currency')){
		
		if((window.location.href).indexOf('-fr.html')!='-1'){
			alert('Veuillez entrer la valeur approximative de vos autres achats mensuels.');
		}else{
			alert('Please enter the approximate value of your monthly purchases.');
		}
	validationStatus = false;
	}
	
 } //end of option 5 aero classic **************************************validation
  
//if the validations are successfull do the following**********************************
	if(validationStatus){
		updateBonus();		//grab the points per flight var
				if (option == '0' || option == '4'){					
					{			
						ddmiles = document.VisaTool.DirectDeposit.value * 100;
					}
					
					currentPoints = Math.round(bonusPoints + ddmiles + pamiles + (removeCurrencyFormatting(document.VisaTool.MonthlyPurchaseAmount.value) * pointsPerDollar * 12) + (removeCurrencyFormatting(document.VisaTool.OtherMonthlyPurchaseAmount.value) * pointsPerDollarOther * 12)+ oct + nov + dec);
				}//end option 0,5
				else if(option == '1' ||option == '3'){
					currentPoints = Math.round(bonusPoints + (removeCurrencyFormatting(document.VisaTool.MonthlyPurchaseAmount.value) * pointsPerDollar * 12) + (removeCurrencyFormatting(document.VisaTool.OtherMonthlyPurchaseAmount.value) * pointsPerDollarOther * 12));	
				}
				else if(option == '2'){
					{ddmiles = document.VisaTool.DirectDeposit.value * 100;
						
					}
					currentPoints = Math.round(bonusPoints + ddmiles + pamiles + oct + nov + dec);
				 }
		
		if (document.getElementById('SelectReward').value == 0 ){  // SelectRewardTemp==0hardcode the variable for one offer only
			rewardType = pointsPerFlightShort;		
			rewardUnit = "Number of Flights: ";
			if((window.location.href).indexOf('-fr.html')!='-1'){
				rewardUnit = "Nombre de vols : ";
			}
			rewardImage = "/ca/img/focus/summer-focus/tool-plane-full.gif";
			rewardImageWidth = 34;
			rewardImageHeight = 28;
		}
		
		else if (document.getElementById('SelectReward').value == 1){//these were used for previously rewards
			rewardType = pointsPerFlightLong;
			rewardUnit = "Number of Flights: ";
			if((window.location.href).indexOf('-fr.html')!='-1'){
				rewardUnit = "Nombre de vols : ";
			}
			rewardImage = "/ca/img/focus/summer-focus/tool-plane-full.gif";
			rewardImageWidth = 34;
			rewardImageHeight = 28;
					
		}	
		/*
		else if (document.getElementById('SelectReward').value == 2){		
			rewardType = pointsPerEsso;
			rewardUnit = "$50 Esso gift cards";
			if((window.location.href).indexOf('-fr.html')!='-1'){
				rewardUnit = "Cartes-cadeaux Esso de 50 $";
			}
			rewardImage = "/ca/img/esso-logo.gif";
			rewardImageWidth = 44;
			rewardImageHeight = 32;
					
		}
		
		else if (document.getElementById('SelectReward').value == 3){
			rewardType = pointsPerHardware;
			rewardUnit = "$50 Home Hardware gift cards";
			if((window.location.href).indexOf('-fr.html')!='-1'){
				rewardUnit = "Cartes-cadeaux Home Hardware de 50 $";
			}
			rewardImage = "/ca/img/home-hardware-logo.gif";
			rewardImageWidth = 87;
			rewardImageHeight = 32;
			}*/
			
			
			

			
			
			
				
		currentFlights = Math.floor(currentPoints / rewardType);
		document.getElementById('AnnualPoints').innerHTML = (addCommas(currentPoints));
		var annualPointsHTML = '';
	
	
		if(currentFlights < 1){
			rewardImage = "/ca/img/focus/summer-focus/tool-plane-empty.gif";
			}
		
			/*if ( Math.round(currentFlights) == 1){  //SelectRewardTemp==0 &&to determine singular or plural
					rewardUnit = "short-haul flight**";	
					if((window.location.href).indexOf('-fr.html')!='-1'){
						rewardUnit = "vol court courrier**";
					}	
			}*/
				
				/*if (document.getElementById('SelectReward').value == 0 && Math.round(currentFlights) == 1){
					rewardUnit = "short-haul flight";	
					if((window.location.href).indexOf('-fr.html')!='-1'){
						rewardUnit = "vol court courrier";
					}	
				}*///commented out because we are going back to the earlier method of using plane images.
		/*if (document.getElementById('SelectReward').value == 0){
			if(currentFlights < 1){
			if(currentFlights > 0.5){
				annualPointsHTML += '<img src="/ca/img/focus/summer-focus/tool-plane-half.gif" width="34" height="28" alt="" border="0">';
			}else{
				annualPointsHTML += '<img src="/ca/img/focus/summer-focus/tool-plane-empty.gif" width="34" height="28" alt="" border="0">';
			}
		}else{
			var i=1;
			for(i; i<=currentFlights; i++){
				annualPointsHTML += '<img src="/ca/img/focus/summer-focus/tool-plane-full.gif" width="34" height="28" alt="" border="0">';
				if(i==22){
					i = currentFlights;
				}
			}
			if((Math.abs(currentFlights - (i -1)) >= 0.5) && i<=20){
				annualPointsHTML += '<img src="/ca/img/focus/summer-focus/tool-plane-half.gif" width="34" height="28" alt="" border="0">';
			}
			if(i>7){
				annualPointsHTML = '<table width="150" cellpadding="0" cellspacing="0" border="0">';
				annualPointsHTML += '	<colgroup span="2">';
				annualPointsHTML += '		<col width="42">';
				annualPointsHTML += '		<col width="108">';
				annualPointsHTML += '	</colgroup>';
				annualPointsHTML += '	<tr>';
				annualPointsHTML += '		<td><img src="/ca/img/focus/summer-focus/tool-plane-full.gif" width="34" height="28" alt="" border="0"></td>';
				annualPointsHTML += '		<td valign="middle"><span class="sfAnnualPoints"> ' + Math.round(currentFlights) + ' </span> ' + rewardUnit + ' </td>';
				annualPointsHTML += '	</tr>';
				annualPointsHTML += '</table>';
			}
		}		
		} */
				
				/*if (document.getElementById('SelectReward').value == 1 && Math.round(currentFlights) == 1){
					rewardUnit = "$50 Esso gift card";	
					if((window.location.href).indexOf('-fr.html')!='-1'){
						rewardUnit = "Carte-cadeaux Esso de 50 $";
					}	
				}
				if (document.getElementById('SelectReward').value == 2 && Math.round(currentFlights) == 1){
					rewardUnit = "$50 Home Hardware gift card";	
					if((window.location.href).indexOf('-fr.html')!='-1'){
						rewardUnit = "Carte-cadeaux Home Hardware de 50 $";
					}	
				}*/

				annualPointsHTML += '<img src=" ' + rewardImage + '" width="' + rewardImageWidth + '" height="' + rewardImageHeight + '" alt="" border="0" align="left">';
				annualPointsHTML += '<span class="sfAnnualPoints"><span class="emphasizedText">&nbsp;' + rewardUnit + '&nbsp;' + Math.round(currentFlights) + '</span></span> ';
				
		//}
		document.getElementById('Flights').innerHTML = annualPointsHTML;
	}
	
}
function clearFlights(){
	document.getElementById('Flights').innerHTML = '<img src="/ca/img/focus/summer-focus/tool-plane-empty.gif" width="34" height="28" alt="" border="0">';
	document.getElementById('AnnualPoints').innerHTML = "0";
}




