//*********************************************************************************
// Desc: 				Browser Redirect for unsupported browsers
// Created : 			September 30, 2005
// Last Modified: 		September 30, 2005
//*********************************************************************************
if (((navigator.appName == 'Netscape')&&(parseInt(navigator.appVersion.charAt(0)) <= 4))||(navigator.userAgent.indexOf('Netscape6')!=-1)){
			document.location.replace('/ca/browser.html');
}


//*********************************************************************************
// Title: 				getCookieValue
// Desc: 				Get cookie values for a given cookie
// Created : 			July 31, 2002
// Last Modified: 		Sept 23, 2002
// Accepts:				cookieName - Name of the cookie.
// Returns:				The cookie value(s).
//*********************************************************************************
function getCookieValue(cookieName){
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
	
	// check if cookie exists.  If cookie does not exist, do a second check at the beginning of the string
	if (cookieStartsAt == -1){
		cookieStartsAt = cookieValue.indexOf(cookieName + "=");
	}
	
	if (cookieStartsAt == -1){
		// coookie really does not exist
		cookieValue = null;
	}else{
		cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
		var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
		if (cookieEndsAt == -1){
			// cookie is the last one in the string
			cookieEndsAt = cookieValue.length;
		}
		cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
	}
	return cookieValue;
}

//*********************************************************************************
// Title: 				setCookie
// Desc: 				Get cookie values for a given cookie
// Created : 			July 31, 2002
// Last Modified: 		March 1, 2005
// Accepts:				cookieName - Name of the cookie.
// 						cookieValue - Cookie value(s).
// 						cookiePath - Path cookie is valid for.
// 						cookieExpires - Expriy date/time of the cookie.
// Returns:				
//*********************************************************************************
function setCookie(cookieName, cookieValue, cookieExpires, cookiePath, cookieDomain){
	cookieValue = escape(cookieValue);
	
	if (cookieExpires == "" || cookieExpires == null){
		var nowDate = new Date();
		nowDate.setMonth(nowDate.getMonth() + 1);
		cookieExpires = nowDate.toGMTString();
	}
	
	if (cookiePath == "" || cookiePath == null){
		cookiePath = "; path=/";
	}else{
		cookiePath = "; path=" + cookiePath;
	}

	if (cookieDomain == "" || cookieDomain == null || cookieDomain == undefined){
		cookieDomain = "; domain=" + document.domain.replace(/www\./,"");
	}else{
		cookieDomain = "; domain=" + cookieDomain;
	}
	//alert(cookieName + "=" + cookieValue + "; expires=" + cookieExpires + cookiePath + cookieDomain);
	document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires + cookiePath + cookieDomain;
}


//*********************************************************************************
// Desc: 				Cookie tracking code for all pages on cibc.com
// Created : 			April 27, 2004
// Last Modified: 		June 29, 2004
//*********************************************************************************
var trackTest = getCookieValue("trackCookie");
var locTest = getCookieValue("locCookie");
var domain = ".cibc.com";
var trackingVal = "";
var theDate = new Date();
theDate.setYear(theDate.getFullYear() + 1);
dateExpires = theDate.toGMTString();
if(!trackTest){
	trackingVal = Math.random() * 99999999999999999;
	setCookie('trackCookie', trackingVal, dateExpires, '/', domain);
}
if(!locTest){
	trackingVal = "m";
	setCookie('locCookie', trackingVal, dateExpires, '/', domain);
}

//*********************************************************************************
// Title: 				todaysDate
// Desc: 				writes the date to the document object in a standard format
//						in English or French.
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				
// Returns:				N/A
//*********************************************************************************

// Get today's current date.
var now = new Date();
function todaysDate()	{
	//first determine language by checking path
	var x=""+document.location;
	if (x.indexOf('-fr.html') > 0) { lang="fr"; }
	else{ lang="en"; }
	
	// Array list of months.
	if (lang=="fr"){
		var months = new	Array('janvier','f&eacute;vrier','mars','avril','mai','juin','juillet','ao&ucirc;t','septembre','octobre','novembre','d&eacute;cembre');
	}
	else{
		var months = new	Array('January','February','March','April','May','June','July','August','September','October','November','December');
	}
	// Calculate the number of the current day in the week.
	var date = now.getDate();
	// Calculate four digit year.

	// Join it all together
	if (lang=="fr"){
		date = ((now.getDate()<10) ? "0" : "")+ date;
		today = "le " +  date + " " + months[now.getMonth()] + " " + (fourdigits(now.getYear()));
	}else{
		today =  months[now.getMonth()] + " " + date + ", " + (fourdigits(now.getYear()));
	}
	
	// Print out the data.
	document.write("" +today+ "");
}
//Usage: <script>todaysDate()</script>

//This is to cope with two digit years.
function fourdigits(number)	{
	return (number < 1000) ? number + 1900 : number;
}

//*********************************************************************************
// Title: 				formatDate
// Desc: 				writes a UTC date to the document object in a standard format
//						in English or French.
// Created : 			Feb 23, 2004
// Last Modified: 		August 30, 2004
// Accepts:				inDate, a date string
// Returns:				N/A
//*********************************************************************************
function formatDate(inDate)	{
	outDate=new Date(inDate);
	
	//first determine language by checking path
	var x=""+document.location;
	if (x.indexOf('-fr.html') > 0) { lang="fr"; }
	else{ lang="en"; }
	
	// Array list of months.
	if (lang=="fr"){
		var months = new	Array('janvier','f&eacute;vrier','mars','avril','mai','juin','juillet','ao&ucirc;t','septembre','octobre','novembre','d&eacute;cembre');
	}else{
		var months = new	Array('January','February','March','April','May','June','July','August','September','October','November','December');
	}
	// Calculate the number of the current day in the week.
	var date = outDate.getUTCDate();
	// Calculate four digit year.

	// Join it all together
	if (lang=="fr"){
		date = ((outDate.getUTCDate()<10) ? "0" : "")+ date;
		today = date + " " + months[outDate.getUTCMonth()] + " " + (fourdigits(outDate.getUTCFullYear()));
	}else{
		today =  months[outDate.getUTCMonth()] + " " + date + ", " + (fourdigits(outDate.getUTCFullYear()));
	}

	// Print out the data.
	document.write("" +today+ "");
}


//*********************************************************************************
// Title: 				importSearchTerms
// Desc: 				imports search terms from /ca/js/search-terms.js,
//						searchTerms array is used in the search functions
// Created : 			Unknown
// Last Modified: 		July 24, 2006
// Accepts:				
// Returns:				N/A
//*********************************************************************************

function importSearchTerms() {
	if (document.getElementsByTagName) {
		var head = document.getElementsByTagName("head").item(0);
		var scriptElement;
		scriptElement = document.createElement("script");
		scriptElement.type = "text/javascript";
		scriptElement.src = "/ca/js/search-terms.js";
		scriptElement.charset = "ISO-8859-1";
		head.appendChild(scriptElement);
	}
}
// Don't import when viewing pages in TMS or infonow locators
if (!(/view-Pages.asp/.test(location.href)) && !(/infonow.net/.test(location.href))) {
	importSearchTerms();
}

//*********************************************************************************
// Title: 				submitSearch
// Desc: 				confirms that a search term has been entered
// Created : 			Unknown
// Last Modified: 		April 22, 2004
// Accepts:				
// Returns:				Boolean indicating whether search field is blank or not
//						and error message if blank
//*********************************************************************************

function submitSearch(){
	testQt = document.basicSearch.qt.value.toLowerCase();
	// trim leading/trailing whitespace
	testQt = testQt.replace(/^\s*|\s*$/g,"");
	/* Targeted search landing pages:
	loop through all search terms, if we match, goto the corresponding landing page*/
	if (typeof(searchTerms) != "undefined") {
		for (var i = 0; i < searchTerms.length; i++) {
			if (testQt == searchTerms[i].en) {
				document.location='/ca/search/' + searchTerms[i].page + '.html?qt='+ escape(testQt);
				return false;
			}
		}
	}

	// Default search
	if(document.basicSearch.qt.value == "" || document.basicSearch.qt.value == "Search"){
		if (document.basicSearch.qt.value == "Search"){
			document.basicSearch.qt.value = "";
		}
		alert('The search word is missing. Enter the word or phrase to search.');
		return false;
	}else{
		document.basicSearch.qt.value = document.basicSearch.qt.value.replace(/\+ /g, "+");
		document.basicSearch.qt.value = document.basicSearch.qt.value.replace(/- /g, "-");
		document.characterSet="UTF-8";
		document.charset="UTF-8";
		document.defaultCharset="UTF-8";
		return true;
	}
}

//*********************************************************************************
// Title: 				submitSearchFR
// Desc: 				confirms that a search term has been entered
// Created : 			Jan 14, 2004
// Last Modified: 		April 22, 2004
// Accepts:				
// Returns:				Boolean indicating whether search field is blank or not
//						and error message if blank
//*********************************************************************************
function submitSearchFR(){
	testQt = document.basicSearch.qt.value.toLowerCase();
	// trim leading/trailing whitespace
	testQt = testQt.replace(/^\s*|\s*$/g,"");
	/* Targeted search landing pages:
	loop through all search terms, if we match, goto the corresponding landing page*/
	if (typeof(searchTerms) != "undefined") {
		for (var i = 0; i < searchTerms.length; i++) {
			if (testQt == searchTerms[i].fr) {
				document.location='/ca/search/' + searchTerms[i].page + '-fr.html?qt='+ escape(testQt);
				return false;
			}
		}
	}
	
	// Default search
	if(document.basicSearch.qt.value == "" || document.basicSearch.qt.value == "Recherche"){
		if (document.basicSearch.qt.value == "Recherche"){
			document.basicSearch.qt.value = "";
		}
		alert('Le terme d\'interrogation n\'est pas inscrit. Inscrivez un mot ou une phrase à rechercher.');
		return false;
	}else{
		document.basicSearch.qt.value = document.basicSearch.qt.value.replace(/\+ /g, "+");
		document.basicSearch.qt.value = document.basicSearch.qt.value.replace(/- /g, "-");
		document.characterSet="UTF-8";
		document.charset="UTF-8";
		document.defaultCharset="UTF-8";
		return true;
	}
}

//*********************************************************************************
// Title: 				roundIt
// Desc: 				rounds a number to x decimal places.
// Created : 			July 15, 2003
// Last Modified: 		Unknown
// Accepts:				number - the float to be changed to "count" decimal places.
//						count - the minimum number of decimal places
// Returns:				a number to "count" decimal places
//*********************************************************************************
function roundIt(number,count){
	//Convert value to string and split on the
	//decimal point. tempAmount[0] represents
	//dollars and tempAmount[1] represents cents.
	var outpt = "";
	var num = number+"";
	var index = num.indexOf('.');
	if(index==-1){
		return num;
	}else{
		var tempAmount = num.split(".");
		outpt = "1." + tempAmount[1];
		outpt = Math.round(parseFloat(outpt) * Math.pow(10, count));
		outpt = "" + outpt;
		if (parseInt(outpt.charAt(0)) > 1){
			tempAmount[0] = Math.round(parseFloat(tempAmount[0]) + 0.5);
			return parseFloat(tempAmount[0] + "." + outpt.slice(1, outpt.length));
		}else{
			return parseFloat(tempAmount[0] + "." + outpt.slice(1, outpt.length));
		}
	}
}


//*********************************************************************************
// Title: 				toTwoDecimal
// Desc: 				Changes an integer input into a 2 decimal number.
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				number - the integer to be changed to 2 decimal places.
// Returns:				a number to two decimal places
//*********************************************************************************
function toTwoDecimal(number){
	//Convert value to string and split on the
	//decimal point. tempAmount[0] represents
	//dollars and tempAmount[1] represents cents.
	var count = 2;
	var outpt = "";
	var num = number+"";
	var index = num.indexOf('.');
	if(index==-1){
		return num + ".00";
	}else{
		var tempAmount = num.split(".");
		outpt = "1." + tempAmount[1];
		outpt = Math.round(parseFloat(outpt) * Math.pow(10, count));
		outpt = "" + outpt;
		if (parseInt(outpt.charAt(0)) > 1){
			tempAmount[0] = Math.round(parseFloat(tempAmount[0]) + 0.5);
		}
		if (parseFloat(outpt) != 0){
			return tempAmount[0] + "." + outpt.slice(1, outpt.length);
		}else{
			return tempAmount[0] + ".00";
		}
	}
}
//*********************************************************************************
// Title: 				toTwoDecimalFr
// Desc: 				Changes an integer input into a 2 decimal number in french.
// Created : 			Unknown
// Last Modified: 		July 12, 2005
// Accepts:				number - the integer to be changed to 2 decimal places.
// Returns:				a number to two decimal places
//*********************************************************************************
function toTwoDecimalFr(number){
	//Convert value to string and split on the
	//decimal point. tempAmount[0] represents
	//dollars and tempAmount[1] represents cents.

	var count = 2;
	var outpt = "";
	var num = number+"";
	var index = num.indexOf(',');

	if(index==-1){
		return num + ",00";
	}else{
		var tempAmount = num.split(",");
		outpt = "1," + tempAmount[1];
		outpt = Math.round(parseFloat(outpt) * Math.pow(10, count));
		outpt = "" + outpt;
		
		if (parseInt(outpt.charAt(0)) > 1){
			tempAmount[0] = Math.round(parseFloat(tempAmount[0]) + 0.5);
		}
		if (parseFloat(outpt) != 0){
			return tempAmount[0] + "," + outpt.slice(1, outpt.length);
		}else{
			return tempAmount[0] + ",00";
		}
	}
}

//*********************************************************************************
// Title: 				dollarOutput
// Desc: 				Function dollarOutput(value) returns value
//						coverted to currency format. Commas are
//						inserted as needed. If value has more than
//						two digits after the decimal point, the
//						digits that follow the first two are truncated.
//						If value has no digits after the decimal point
//						or has one digit after the decimal point one or
//						two trailing zeroes are appended respectively as
//						done by the toTwoDecimal(value) function.
//						Precond: value is a number. 
// Created : 			Unknown
// Last Modified: 		June 19, 2003
// Accepts:				value - the number to be changed to dollar format.
// Returns:				a number to two decimal places
//*********************************************************************************
function dollarOutput(value){	
	toTwoDecimal(value);
	//Convert value to string and split on the
	//decimal point. tempAmount[0] represents
	//dollars and tempAmount[1] represents cents.
	var tempAmount = (value + "").split(".");
	
	var offset = 1;
	var amount = "";

	//Go through each character in the dollar part
	//of value.
	for(var i = tempAmount[0].length - 1; i >= 0; i--){	
		
		//If current offset is a multiple of 3 add a comma.
		if((offset++ % 3) == 0){
			amount = "," + tempAmount[0].substr(i,1) + amount
		}
		else{	
			amount = tempAmount[0].substr(i,1) + amount
		}	
  }
	
	//If value had digits after the decimal point
	//append those back.
	if(tempAmount.length == 2){
		amount += "." + tempAmount[1];
	}
		
	//Get rid of the leading comma if it exists.	
	if(amount.charAt(0) == "," || (amount.charAt(0) == "-" && amount.charAt(1) == ",")){
		amount = amount.replace(/,/,"");		
	}
	
	//Convert the final amount to
	//two decimal point format and return.
	return amount;
}

//*********************************************************************************
// Title: 				dollarOutputFr
// Desc: 				Function dollarOutput(value) returns value
//						coverted to currency format. Commas are
//						inserted as needed. If value has more than
//						two digits after the decimal point, the
//						digits that follow the first two are truncated.
//						If value has no digits after the decimal point
//						or has one digit after the decimal point one or
//						two trailing zeroes are appended respectively as
//						done by the toTwoDecimal(value) function.
//						Precond: value is a number. 
// Created : 			June 18, 2003 (modified dollarOutput)
// Last Modified: 		July 12, 2005
// Accepts:				value - the number to be changed to dollar format.
// Returns:				a number to two decimal places
//*********************************************************************************
function dollarOutputFr(value){	
	toTwoDecimal(value);
	//Convert value to string and split on the
	//decimal point. tempAmount[0] represents
	//dollars and tempAmount[1] represents cents.
	var tempAmount = (value + "").split(".");
	
	var offset = 1;
	var amount = "";

	//Go through each character in the dollar part
	//of value.
	for(var i = tempAmount[0].length - 1; i >= 0; i--){	
		
		//If current offset is a multiple of 3 add a comma.
		if((offset++ % 3) == 0){
			amount = " " + tempAmount[0].substr(i,1) + amount
		}
		else{	
			amount = tempAmount[0].substr(i,1) + amount
		}	
  }
	
	//If value had digits after the decimal point
	//append those back.
	if(tempAmount.length == 2){
		amount += "," + tempAmount[1];
	}
		
	//Get rid of the leading comma if it exists.	
	if(amount.charAt(0) == " " || (amount.charAt(0) == "-" && amount.charAt(1) == " ")){
		amount = amount.replace(/,/,"");		
	}
	
	//Convert the final amount to
	//two decimal point format and return.
	return amount;
}


//*********************************************************************************
// Title: 				dollarOutputFrNbsp
// Desc: 				Returns French currency format 
//						Using &nbsp; as the thousand seperator so number will be
//						wrapped in two lines
// Created : 			July 8, 2005 (modified dollarOutput)
// Last Modified: 		July 8, 2005
// Accepts:				value - the number to be changed to dollar format.
// Returns:				a number to two decimal places
//*********************************************************************************
function dollarOutputFrNbsp(value){	
	toTwoDecimal(value);
	//Convert value to string and split on the
	//decimal point. tempAmount[0] represents
	//dollars and tempAmount[1] represents cents.
	var tempAmount = (value + "").split(".");
	
	var offset = 1;
	var amount = "";

	//Go through each character in the dollar part
	//of value.
	for(var i = tempAmount[0].length - 1; i >= 0; i--){	
		
		//If current offset is a multiple of 3 add a comma.
		if((offset++ % 3) == 0){
			amount = "&nbsp;" + tempAmount[0].substr(i,1) + amount
		}
		else{	
			amount = tempAmount[0].substr(i,1) + amount
		}	
  }
	
	//If value had digits after the decimal point
	//append those back.
	if(tempAmount.length == 2){
		amount += "," + tempAmount[1];
	}
		
	//Get rid of the leading comma if it exists.	
	if(amount.charAt(0) == " " || (amount.charAt(0) == "-" && amount.charAt(1) == " ")){
		amount = amount.replace(/,/,"");		
	}
	
	//Convert the final amount to
	//two decimal point format and return.
	return amount;
}
//*********************************************************************************
// Title: 				dollarOutputNoDec
// Desc: 				Function dollarOutput(value) returns value
//						coverted to currency format. Commas are
//						inserted as needed. No Decimal places are returned.
// Created : 			July 17, 2003
// Last Modified: 		July 17, 2003
// Accepts:				value - the number to be changed to dollar format.
// Returns:				a number formatted with commas.
//*********************************************************************************
function dollarOutputNoDec(value){	
	
	//Convert value to string and split on the
	//decimal point. tempAmount[0] represents
	//dollars and tempAmount[1] represents cents.
	value = Math.round(value);
	var tempAmount = value + ""
	
	var offset = 1;
	var amount = "";

	//Go through each character in the dollar part
	//of value.
	for(var i = tempAmount.length - 1; i >= 0; i--){	
		
		//If current offset is a multiple of 3 add a comma.
		if((offset++ % 3) == 0){
			amount = "," + tempAmount.substr(i,1) + amount
		}
		else{	
			amount = tempAmount.substr(i,1) + amount
		}	
  }
		
	//Get rid of the leading comma if it exists.	
	if(amount.charAt(0) == "," || (amount.charAt(0) == "-" && amount.charAt(1) == ",")){
		amount = amount.replace(/,/,"");		
	}

	return amount;
	
}

//*********************************************************************************
// Title: 				newWindow
// Desc: 				Opens a new window of standard size.
// Created : 			Unknown
// Last Modified: 		Jan 29, 2004
// Accepts:				help_url - the URL of the page to open
// Returns:				void
//*********************************************************************************
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;
}
//Usage:
//function newWindowMainHelp() { newWindow("help_getting_started.html"); }
//function newWindowEmail() { newWindow("help_getting_started.html#email"); }
//<a href="javascript:newWindowMainHelp()">

//*********************************************************************************
// Title: 				newLargeWindow
// Desc: 				Opens a new window of large size.
// Created : 			Aug 19, 2003
// Last Modified: 		Jan 29, 2004
// Accepts:				pop_url - the URL of the page to open
// Returns:				void
//*********************************************************************************
function newLargeWindow(pop_url) {
	var custWindow;
	custWindow = window.open(pop_url,"custWindow","width=705,height=550,scrollbars=yes,menubars=yes,resizable=yes,top="+(screen.availHeight-550)/2+",left="+(screen.availWidth-705)/2);
	custWindow.focus();
	//return false;
}
//Usage:
//function newWindowMainHelp() { newLargeWindow("help_getting_started.html"); }
//function newWindowEmail() { newLargeWindow("help_getting_started.html#email"); }
//<a href="javascript:newWindowMainHelp()">

//*********************************************************************************
// Title: 				newCustomWindow
// Desc: 				Opens a new window of large size.
// Created : 			Nov 25, 2003
// Last Modified: 		Jan 29, 2004
// Accepts:				pop_url - the URL of the page to open
// Returns:				void
//*********************************************************************************
function newCustomWindow(pop_url,win_width,win_height,win_name) {
	var custWindow;
	if(pop_url.indexOf('/marketing/locators/TeamLocatorSearch') != -1){
		pop_url = '/ca/apply/afp-error.html';
	}
	if(!win_name){
		win_name="custWindow";
	}
	custWindow = window.open(pop_url,win_name,"width="+win_width+",height="+win_height+",scrollbars=yes,menubars=yes,resizable=yes,top="+(screen.availHeight-win_height)/2+",left="+(screen.availWidth-win_width)/2);
	custWindow.focus();	
	//return false;
}
//Usage:
//function newWindowMainHelp() { newCustomWindow("help_getting_started.html","500","500"); }
//function newWindowEmail() { newCustomWindow("help_getting_started.html#email","500","500"); }
//<a href="javascript:newWindowMainHelp()">

//*********************************************************************************
// Title: 				newCustToolbarWindow
// Desc: 				Opens a new window of standard size.
// Created : 			July 14, 2004
// Last Modified: 		July 14, 2004
// Accepts:				pop_url - the URL of the page to open
//						win_width,win_height - height and width
// Returns:				void
//*********************************************************************************
function newCustToolbarWindow(pop_url,win_width,win_height) {
	var toolbr;
	toolbr=window.open(pop_url,"apply","width="+win_width+",height="+win_height+",directories=no,hotkeys=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=yes,top="+(screen.availHeight-win_height)/2+",left="+(screen.availWidth-win_width)/2);
	toolbr.focus();
	//return false;
}
//Usage:
//function newWindowMainHelp() { newCustNoScrollbarWindow("help_getting_started.html","500","500"); }
//function newWindowEmail() { newCustNoScrollbarWindow("help_getting_started.html#email","500","500"); }
//<a href="javascript:newWindowMainHelp()">

//*********************************************************************************
// Title: 				newCustNoScrollbarWindow
// Desc: 				Opens a new window of standard size.
// Created : 			Sept 25, 2006
// Last Modified: 		Sept 25, 2006
// Accepts:				pop_url - the URL of the page to open
//						win_width,win_height - height and width
// Returns:				void
//*********************************************************************************
function newCustNoScrollbarWindow(pop_url,win_width,win_height) {
	var toolbr;
	toolbr=window.open(pop_url,"apply","width="+win_width+",height="+win_height+",directories=no,hotkeys=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no,top="+(screen.availHeight-win_height)/2+",left="+(screen.availWidth-win_width)/2);
	toolbr.focus();
	//return false;
}
//*********************************************************************************
// Title: 				newCustStatusWindow
// Desc: 				Opens a new window of standard size, with a Status bar.
// Created : 			June 24, 2005
// Last Modified: 		June 24, 2005
// Accepts:				pop_url - the URL of the page to open
//						win_width,win_height - height and width
// Returns:				void
//*********************************************************************************
function newCustStatusWindow(pop_url,win_width,win_height) {
	var statusbar;
	statusbar=window.open(pop_url,"newWin","width="+win_width+",height="+win_height+",directories=no,hotkeys=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,top="+(screen.availHeight-win_height)/2+",left="+(screen.availWidth-win_width)/2);
	statusbar.focus();
	//return false;
}

//Usage:
//function newWindowMainHelp() { newCustomWindow("help_getting_started.html","500","500"); }
//function newWindowEmail() { newCustomWindow("help_getting_started.html#email","500","500"); }
//<a href="javascript:newWindowMainHelp()">

//*********************************************************************************
// Title: 				newApplyWindow
// Desc: 				Opens a new window of standard size.
// Created : 			Jan 29, 2004
// Last Modified: 		Jan 29, 2004
// Accepts:				apply_url - the URL of the page to open
// Returns:				void
//*********************************************************************************
function newApplyWindow(apply_url) {
	var apply;
	apply=window.open(apply_url,"apply","width=640,height=470,directories=no,hotkeys=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes,top="+(screen.availHeight-470)/2+",left="+(screen.availWidth-640)/2);
	apply.focus();
	//return false;
}
//Usage:
//function newWindowMainHelp() { newWindow("help_getting_started.html"); }
//function newWindowEmail() { newWindow("help_getting_started.html#email"); }
//<a href="javascript:newWindowMainHelp()">

//*********************************************************************************
// Title: 				newApplyWindowLarge
// Desc: 				Opens a new window of standard size.
// Created : 			Jan 29, 2004
// Last Modified: 		Jan 29, 2004
// Accepts:				apply_url - the URL of the page to open
// Returns:				void
//*********************************************************************************
function newApplyWindowLarge(apply_url) {
	var applyLarge;
	applyLarge=window.open(apply_url,"applyLarge","width=797,height=550,directories=no,hotkeys=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes,top="+(screen.availHeight-550)/2+",left="+(screen.availWidth-797)/2);
	applyLarge.focus();
	//return false;
}
//*********************************************************************************
// Title: 				openWindowOpener
// Desc: 			opens the url in the opener window and focuses it. If the opener 
//						window doesn't exist, it opens and focuses a new window.
// Created : 			May 28, 2004
// Accepts:				url - the URL of the page to be redirected / opened
// Returns:				N/A
//*********************************************************************************
function openWindowOpener(url){
	if(top.opener && !top.opener.closed){
		top.opener.location=url;
		top.opener.focus();
	}else{
		replaceOpenerWindow = window.open(url,'newOpener');
		replaceOpenerWindow.focus();
	}
}


//Usage:
//function newWindowMainHelp() { newWindow("help_getting_started.html"); }
//function newWindowEmail() { newWindow("help_getting_started.html#email"); }
//<a href="javascript:newWindowMainHelp()">

//*********************************************************************************
// Title: 				surveyOpener
// Desc: 				opens the CIBC Survey on a randomized instance
// Created : 			February 24, 2006
// Last Modified: 		March 1, 2005
// Accepts:				surveyUrl - the URL of the page to be redirected / opened
//						nth - the value that sets the occurence rate of the survey appearing
// Returns:				N/A
//*********************************************************************************

function surveyOpener(surveyUrl,nth){
	var rnd = Math.floor(Math.random() * nth) + 1;
	cookieName = "CIBCsurvey";
	
	//alert(getCookieValue(cookieName));
	cookieTest = getCookieValue(cookieName);
	
	if (rnd ==   nth && (cookieTest < 1 || cookieTest == null)) {
		document.write('	<div id="surveyDiv">');
		document.write('		<div id="surveyTop"><a href="#" onclick="document.getElementById(\'surveyDiv\').style.display = \'none\';"><img src="/ca/img/sign-off/button-close.gif" alt="Close" width="57" height="16" border="0"></a></div>');
		document.write('		<div id="surveyBottom">');
		document.write('			<h2>Your participation is needed.</h2>');
		document.write('			<p>At CIBC we value your input. We would like you to participate in a short survey to learn how we can service you better. Your answers will help us to continue to develop our site in a way that meets your needs. All individual responses collected are completely confidential and protected under <a href="http://www.cibc.com/ca/legal/privacy-policy.html">CIBC\'s privacy policy</a>.</p>');
		document.write('			<p>This survey should take less than 10 minutes to complete.</p>');
		document.write('			<p><a href="' + surveyUrl + '" onclick="surveyTaken(\'' + surveyUrl + '\', \'' + cookieName + '\');return false;"><img src="/ca/img/button/take-the-survey.gif" alt="Take the survey" width="145" height="23" border="0"></a>&nbsp;&nbsp;<a href="#" onclick="surveyNotInterested(\'' + cookieName + '\');return false;"><img src="/ca/img/button/im-not-interested.gif" alt="I\'m not interested" width="165" height="23" border="0"></a></p>');
		document.write('			<p><form name="surveyForm" action="none"><input type="checkbox" name="surveyNone" value="1"><label for="surveyNone">Don\'t ask me to participate in this survey again</label></form></p>');
		document.write('		</div>');
		document.write('	</div>');
	}
}
//***********Required by surveyOpener*******************
// Created: 			March 1, 2005
// Last Modified: 		March 1, 2005
function surveyNotInterested(cookieName){
	if(document.surveyForm.surveyNone.checked){
		var nowDate = new Date();
		declinedDate = nowDate;
		declinedDate.setMonth(nowDate.getMonth() + 12);
		declinedDate.toGMTString();
		//alert(cookieName);
		setCookie(cookieName, 2, declinedDate);
		//alert(getCookieValue(cookieName));
	}
	document.getElementById('surveyDiv').style.display='none';
}
//***********Required by surveyOpener*******************
// Created: 			March 1, 2005
// Last Modified: 		March 31, 2005
function surveyTaken(surveyUrl, cookieName){
	window.open(surveyUrl ,'CIBCSurvey','height=580,width=750,top=20,left=20,scrollbars=yes,resizable=yes');
	document.getElementById('surveyDiv').style.display='none';
	return false;
}
//***********Required by surveyOpener*******************
// Created: 			March 31, 2005
// Last Modified: 		March 31, 2005
function surveyTakenCookie(cookieName){
	var nowDate = new Date();
	takenDate = nowDate;
	takenDate.setMonth(nowDate.getMonth() + 6);
	takenDate.toGMTString();
	setCookie(cookieName, 1, takenDate);
}

//*********************************************************************************
// Title: 				checkText
// Desc: 				Validates that the value of a field is text.
// Created : 			July 31, 2002
// Last Modified: 		Sept 23, 2002
// Accepts:				field - A reference to the field to be checked.
// Returns:				True if valid. False if not valid.
//*********************************************************************************
function checkText(field){
	var isValid = true;
	//field = eval(field);
	/*setup regular expression to use to validate text.  A backslash is used
	in the regular expression to escape a special regular expression
	character.  Since backslash is a special character within javascript, it
	must also be escaped, hence all the double slashes.  The expression
	includes alphanumerics, all standard puntuation, and extended latin
	characters.*/
	strExp = "^[A-Za-z0-9Ç-ùáíóúñÑÖÜ,/;:'-=_!@#%&\\\"\\f\\n\\r\\+\\*\\?\\.\\[\\]\\^\\$\\(\\)\\{\\}\\|\\\\\\&\\ ]+$";
	myReg = new RegExp(strExp);
	isValid = myReg.test(field.value);
	if(field.value.length > 0 && isValid){
		return true;
	}
	return false
}

//*********************************************************************************
// Title: 				checkRadio
// Desc: 				Validates that the value of a field is text.
// Created : 			July 31, 2002
// Last Modified: 		Sept 23, 2002
// Accepts:				field - A reference to the field to be checked.
// Returns:				True if valid. False if not valid.
//*********************************************************************************
function checkRadio(field){
	field = eval(field);
	for(var i=0; i<field.length; i++){
		if(field[i].checked){
			return true;
		}
	}
	return false;
}

//*********************************************************************************
// Title: 				isEmpty
// Desc: 				
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				inputStr
// Returns:				True if valid. False if not valid.
//*********************************************************************************
function isEmpty( inputStr )
{
   if ((inputStr!=null)&&(inputStr!="")) { var c='?'; for (var i=0; i < inputStr.length; i++) { c=inputStr.charAt(i); if (c!=" ") return false; } }
   return true;
}

//*********************************************************************************
// Title: 				isNumber
// Desc: 				
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				inputStr
// Returns:				True if valid. False if not valid.
//*********************************************************************************
function isNumber( inputStr )
{
   var c="?";
   for (var i=0; i < inputStr.length; i++) { c=inputStr.charAt(i); if (c!=" ") { if ((c<"0") || (c>"9")) return false; } }
   return true;
}

//*********************************************************************************
// Title: 				submitFeedbackForm
// Desc: 				
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				s
// Returns:				True
//*********************************************************************************
function submitFeedbackForm()
{
   document.feedback.submit(); return true;
}

//*********************************************************************************
// Title: 				isWhitespace
// Desc: 				
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				s
// Returns:				True if valid. False if not valid.
//*********************************************************************************
var whitespace = " \t\n\r";
function isWhitespace (s)
{   
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


//*********************************************************************************
// Title: 				isEmail
// Desc: 				
// Created : 			Unknown
// Last Modified: 		Unknown
// Accepts:				s
// Returns:				True if valid. False if not valid.
//*********************************************************************************
function isEmail (s)
{   
    if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    //If 1st character is not an alpha numeric, it is then not a valid email address.
    ch = s.charAt(0);
    if (!((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" ) ||
          (ch >= "0" && ch <= "9") || (ch == "_") || (ch == "-"))) 
    {

       return false;
    }

    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    {
       // ensure character is alpha, numeric, underscore, or a dot
       ch = s.substring(i, i+1);
       if (!((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" ) ||
             (ch >= "0" && ch <= "9") || (ch == "_") || (ch == ".") || (ch == "-"))) 
       {

          return false;
       }
       if ((s.charAt(i) == ".") && (s.charAt(i+1) == ".")) {
          return false;
       }
       if ((s.charAt(i) == ".") && (s.charAt(i+1) == "@")) {
          return false;
       }
       i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) {
       return false;
    } else if ((s.charAt(i) == "@") && (s.charAt(i+1) == ".")) {
       return false;
    } else {
       i += 1;
    }
    
    // look for at least one dot
    while ((i < sLength) && (s.charAt(i) != "."))
    { 
       // ensure character is alpha, numeric, or underscore
       ch = s.substring(i, i+1);
       if (!((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" ) ||
             (ch >= "0" && ch <= "9") || (ch == "_") || (ch == "-"))) 
       {
          return false;
       }

       i++;
    }

    // there must be at least one character after the dot
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;

    //ensure there are no consecutive dots.
    if ((s.charAt(i) == ".") && (s.charAt(i+1) == ".")) {
       return false;
    }

    // ensure the rest of the characters after the first "." are alpha, 
    // numeric, underscore, or a dot.
    while ((i < sLength)) {
       ch = s.substring(i, i+1);
       if (!((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" ) ||
             (ch >= "0" && ch <= "9") || (ch == "_") || (ch == "."))) 
       {
          return false;
       }
       
       if ((s.charAt(i) == ".") && (s.charAt(i+1) == ".")) {
          return false;
       }
       i++;
    }

    // ensure the last character is not a dot
    if (s.charAt(sLength-1) == ".") {
       return false;
    }
    
    return true;
}


//*********************************************************************************
// Title: 				trailingCurrencyEnglish
// Desc: 				To handle maggic numbers like 4.000000002
// Created : 			July 12, 2005
// Last Modified: 		July 12, 2005

//*********************************************************************************
  
	  function trailingCurrencyEnglish(curValue)
  { 
   	    curValue = Math.round(parseFloat(curValue) *100) / 100.00;

		return curValue;
  }


//*********************************************************************************
// Title: 				trailingCurrencyFrench
// Desc: 				To handle maggic numbers like 4.000000002
// Created : 			July 12, 2005
// Last Modified: 		July 12, 2005

//*********************************************************************************
  
	  function trailingCurrencyFrench(curValue)
  {  // var curValue = curValue.toString(); 
   	    curValue = Math.round(parseFloat(curValue) *100) / 100.00;
		return curValue
  }

//*********************************************************************************
// Title: 				selectorFeature
// Desc: 				To handle passing values to the Credit Card Selector
// Created : 			March 10, 2006
// Last Modified: 		March 10, 2006

//*********************************************************************************
 	function selectorFeatureVISAEN() {
		if (document.getElementById('feature1').checked == true) {
			newCustomWindow('/ca/visa/article-tools/cc-selector-1.html?feature=1','740','540');
		}
		else {
			newCustomWindow('/ca/visa/article-tools/cc-selector-1.html?feature=2','740','540');
		}
	}

	function selectorFeatureVISAFR() {
		if (document.getElementById('feature1').checked == true) {
			newCustomWindow('/ca/visa/article-tools/cc-selector-1-fr.html?feature=1','740','540');
		}
		else {
			newCustomWindow('/ca/visa/article-tools/cc-selector-1-fr.html?feature=2','740','540');
		}
	}	

//*********************************************************************************
// Title: 				writeActiveXObject
// Desc: 				To fix IE 6 ActiveX update
// Created : 			May 31, 2006
// Last Modified: 		May 31, 2006

//*********************************************************************************
	function writeActiveXObject(text){
		document.write(text);
	}

//*********************************************************************************
// Title: 				Useful function to retrieve all elements of specified className
// Desc: 				Returns all elements of specified className
// Created : 			Feb 08, 2008
// Last Modified: 		Feb 08, 2008

//*********************************************************************************
	function getElementsByClassName(searchClass, node, tag) {
		var classElements = new Array();
		if (node == null) { node = document; }
		if (tag == null) { tag = '*'; }

		// safari mac 2.0 doesn't support this
		var els = node.getElementsByTagName(tag);

		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
		for (var i = 0, j = 0; i < elsLen; i++) {
			if (pattern.test(els[i].className)) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}
