//*********************************************************************************
// Title: 				patternValidate
// Desc: 				Validates data using a variety of regular expressions
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				fieldVal as a string
// 						patternName as a string
// Returns:				true or false
//*********************************************************************************	
var validationPatterns = new Array();
validationPatterns['currency'] = /^((([0-9]{1,3}([, ]?[ ]?[0-9]{3})*)|([0-9]*))([.][0-9]{1,2})?)$/;
validationPatterns['currency2'] = /^((([0-9]{1,3}([, ]?[ ]?[0-9]{3}){0,2})|([0-9]{0,9}))([.][0-9]{1,2})?)$/;
validationPatterns['currency-fr'] = /^((([0-9]{1,3}([ ]?[ ]?[0-9]{3})*)|([0-9]*))([,.][0-9]{1,2})?)$/;//not used currently
validationPatterns['currency2-fr'] = /^((([0-9]{1,3}([ ]?[ ]?[0-9]{3}){0,2})|([0-9]{0,9}))([,.][0-9]{1,2})?)$/;//not used currently
validationPatterns['dateDay'] = /^(([3][01])|([12][0-9])|([0]?[1-9]))$/;
validationPatterns['dateYear'] = /^(((19)|(20))[0-9]{2})$/;
validationPatterns['decimalNumber'] = /^(([0-9]+)|([0-9]*[.][0-9]+))$/;
validationPatterns['email'] = /^[a-zA-Z0-9!#\$%\&'\*\+\-\/=\?\^_`\{\|\}~]+([.][a-zA-Z0-9!#\$%\&'\*\+\-\/=\?\^_`\{\|\}~]+)*[@]([a-zA-Z0-9]+([\.\-][a-zA-Z0-9]+)*(\.[a-zA-Z]{2,})|(((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))))$/;
validationPatterns['cibcEmail'] = /^[a-zA-Z0-9!#\$%\&'\*\+\-\/=\?\^_`\{\|\}~]+([.][a-zA-Z0-9!#\$%\&'\*\+\-\/=\?\^_`\{\|\}~]+)*[@][Cc][Ii][Bb][Cc][\.][Cc](([aA])|([oO][mM]))$/;
validationPatterns['expDateMonth'] = /^[01][0-9]$/;
validationPatterns['percentage'] = /^[01]?[0-9]{1,2}$/;
validationPatterns['phoneNumber'] = /^[0-9]{3}[ \-\.]?[0-9]{4}$/;
validationPatterns['phoneNumberFull'] = /^((([\(][0-9]{3}[\)][ ]?)|([0-9]{3}[ \-\.]?))[0-9]{3}[ \-\.]?[0-9]{4})$/;
validationPatterns['foreignPhoneNumber'] = /^[0-9\-\.]+$/;
validationPatterns['postalCode'] = /^[A-Za-z][0-9][A-Za-z][ ]?[0-9][A-Za-z][0-9]$/;
validationPatterns['spacedWholeNumber'] = /^[0-9 ]+$/;
validationPatterns['SINumber'] = /^[0-9]{3}[ ]?[0-9]{3}[ ]?[0-9]{3}$/;
validationPatterns['wholeNumber'] = /^[0-9]+$/;
validationPatterns['agentID'] = /^[a-zA-Z]{2}[0-9]{4}/;

function patternValidate(fieldVal, patternName){
	if(fieldVal.length == 0 || validationPatterns[patternName].test(fieldVal)){
		return true;
	}else{
		return false;
	}
}


//*********************************************************************************
// Title: 				remove Currency Formating
// Desc: 				removes currency formating characters and returns an float
// Created : 			Feb 16, 2005
// Last Modified: 		Feb 16, 2005
// Accepts:				String
// Returns:				Converted String
//*********************************************************************************
function removeCurrencyFormatting(currVal){
	if(checkCurrency(currVal) == 'English'){
		currVal = replaceAll(replaceAll(replaceAll(currVal, ' ', ''), ',', ''), '$', '');
	}else if(checkCurrency(currVal) == 'French'){
		currVal = replaceAll(replaceAll(replaceAll(currVal, ' ', ''), ',', '.'), '$', '');
	}else{
		return currVal;
	}
	if(currVal.length > 0){
		if (currVal.indexOf('.') != -1){
			var valArr = currVal.split('.');
			if (valArr[1].length > 2){
				valArr[1] = valArr[1].substr(0, 2);
			}else{
				while(valArr[1].length < 2){
					valArr[1] += '0';
				}
			}
			currVal = valArr[0] + '.' + valArr[1];
		}else{
			currVal = currVal + '.00'
		}
	}
	return currVal;
}

//*********************************************************************************
// Title: 				CheckCurrency
// Desc: 				Returns the format of currency
// Created : 			Feb 16, 2005
// Last Modified: 		Feb 16, 2005
// Accepts:				String
// Returns:				Converted String
//*********************************************************************************
function checkCurrency(val){
	if(patternValidate(val, 'currency2')){
		return 'English';
	}else if(patternValidate(val, 'currency2-fr') && (document.location.pathname.indexOf('-fr.html') != -1)){
		return 'French';
	}else{
		return false;
	}
}


 //*********************************************************************************
// Title: 				replaceAll
// Desc: 				Performs string replacement	
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				str as a string
//						key as a string
//						rplcmnt as a string
// Returns:				String
//*********************************************************************************
function replaceAll(str, key, rplcmnt){
	while(str.indexOf(key) != -1){
		str = str.replace(key, rplcmnt);
	}
	return str;
}


//*********************************************************************************
//*********************************************************************************
//*********************************************************************************
//*********************************************************************************
	
function getCreditCardUrl(treatmentId){
    var visaAppPopup;
    if (treatmentId != "Treatment ID is empty" ){
        var pop_url;
        pop_url= "https://www.cibccreditcards.com/prospectapp/modules/selectProduct.do?default=Y&";   
        visaAppPopup = window.open(pop_url+"treatmentId="+treatmentId,'visaAppPopup',"width=770,height=550,scrollbars=yes,menubars=yes,resizable=yes,top="+(screen.availHeight-550)/2+",left="+(screen.availWidth-770)/2);
    }else{ //no treatment ID embedded in page
        visaAppPopup = window.open("https://www.cibccreditcards.com/visa");
    }
    visaAppPopup.focus();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
}


//Usage:
//<a href="javascript:getCreditCardUrl('2005034');">Apply now</a>


//*********************************************************************************
//*********************************************************************************
//*********************************************************************************
//*********************************************************************************
function newCustomWindow(pop_url){
var custWindow;
custWindow=window.open(pop_url,'custWindow',"width=797,height=550,scrollbars=yes,menubars=yes,resizable=yes,top="+(screen.availHeight-550)/2+",left="+(screen.availWidth-797)/2);
custWindow.focus();	
//return false;
}


//Usage:


function newWindow(help_url) {
	var popupWin;
	popupWin = window.open(help_url,"popupWin","width=620,height=480,scrollbars=yes,menubars=yes,resizable=yes,top="+(screen.availHeight-480)/2+",left="+(screen.availWidth-620)/2);
	popupWin.focus();
	//return false;
}




//*********************************************************************************
//*********************************************************************************
//*********************************************************************************
//*********************************************************************************

var pointsPerDollar = 1.5;
var pointsPerDollarOther = 1;

var bonusPoints = 15000; // Card specific  
var pointsPerFlight = 15000; // Constant on each card
var currentPoints = 0;
var currentFlights = 0;

function addCommas(nStr){
	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;
}

function updatePoints(){
	if(document.VisaTool.MonthlyPurchaseAmount.value== ''){
			alert('Please enter the approximate value of your monthly purchases.');
	}else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.MonthlyPurchaseAmount.value),'currency')){
			alert('You have entered an invalid currency amount. Please re-enter the amount.');
	}
	
	else if(document.VisaTool.OtherMonthlyPurchaseAmount.value== ''){
			alert('Please enter the approximate value of your other monthly purchases.');
	}else if(!patternValidate(removeCurrencyFormatting(document.VisaTool.OtherMonthlyPurchaseAmount.value),'currency')){
			alert('You have entered an invalid currency amount. Please re-enter the amount.');
	}else{
		currentPointsFirst = Math.round((removeCurrencyFormatting(document.VisaTool.MonthlyPurchaseAmount.value) * pointsPerDollar  * 12));
		currentPointsOther = Math.round((removeCurrencyFormatting(document.VisaTool.OtherMonthlyPurchaseAmount.value) * pointsPerDollarOther * 12));
				
		currentPoints= currentPointsFirst + currentPointsOther + bonusPoints;  
		
		currentFlights = currentPoints / pointsPerFlight;
		document.getElementById('AnnualPoints').innerHTML = (addCommas(currentPoints));
		
		var annualPointsHTML = '';
		
		annualPointsHTML = '<img src="../img/tools/icon-plane-full.gif" width="32" height="12" alt="" border="0" class="iconPlane">';
			
		document.getElementById('sfAnnualPoints').innerHTML = Math.round(currentFlights);
		document.getElementById('flights').innerHTML = annualPointsHTML;
		}
}

function clearFlights(){
	document.getElementById('flights').innerHTML = '<img src="../img/tools/icon-plane-empty.gif" width="32" height="12" alt="" border="0" class="iconPlane">';
	document.getElementById('AnnualPoints').innerHTML = "0";
}



/*-**-*/