function ssoIps() {
	return ssoJumpFormAction('IPS');
}


function ssoJump() {
	var selectedValue= document.ssoJumpForm.ssoJumpList[document.ssoJumpForm.ssoJumpList.selectedIndex].value;
	if (typeof selectedValue != 'undefined' && selectedValue != '') {
		var selectedValueArray = selectedValue.split('#');
		var acctType = selectedValueArray[0];
		var acctIndex = parseInt(selectedValueArray[1], 10);
		if (isNaN(acctIndex)) {
			acctIndex= "";
		}				
		
		document.ssoJumpForm.ssoJumpAccount.value= acctIndex;
		document.ssoJumpForm.ssoJumpType.value= acctType;
		
		if (acctType == il || acctType == nb || acctType == mc) {
			//Can use any sso action to do popup check
			checkSSOPopup("/OLBmain/ssoDefault?mode=checkPopup", "ssoJumpForm");
		} else {
			doSsoJump(acctType, acctIndex);
		}
	}
	return false;
}

function ssoJumpFormAction(acctType, acctIndex) {
	document.ssoJumpForm.ssoJumpAccount.value= acctIndex;
	document.ssoJumpForm.ssoJumpType.value= acctType;
	
	if (acctType == il || acctType == nb || acctType == mc) {
		//Can use any sso action to do popup check
		checkSSOPopup("/OLBmain/ssoDefault?mode=checkPopup", "ssoJumpForm");
	} else {
		doSsoJump(acctType, acctIndex);
	}
}

function ssoJumpMC() {
	if (typeof selectedValueMC != 'undefined' && selectedValueMC != '') {
		var selectedValueArray = selectedValueMC.split('#');
		var acctType= selectedValueArray[0];
		var acctIndex= parseInt(selectedValueArray[1], 10);
		if (isNaN(acctIndex)) {
			acctIndex= "";
		}
		ssoJumpFormAction(acctType, acctIndex);
	}
	return false;
}

function ssoUpdateFormAction(acctType, acctIndex) {
	// open within the same target window by default
	document.ssoJumpForm.target= "";
	document.ssoJumpForm.ssoJumpAccount.value= acctIndex;
	document.ssoJumpForm.ssoJumpType.value= acctType;
	set(document.ssoJumpForm, '/OLB/fin/sum/null/ssoAm?mode=form3');
	document.ssoJumpForm.submit();
	return false;
}

function doSsoJump(acctType, acctIndex, showPopupText) {
	// rename the window to make sure that CFM always opens as a popup
	window.name="main";
	// open within the same target window by default
	document.ssoJumpForm.target= "";
	
	if (acctType == il || acctType == nb || acctType == mc) {
		if(showPopupText){
			var popupText = "VEUILLEZ NOTER :";
			popupText+= "\n\nNous vous redirigeons vers un nouveau site. Vous n'aurez pas besoin d'entrer votre nom d'utilisateur et votre mot de passe.";
			popupText+= "\n\nVous pouvez retourner aux services bancaires en ligne en tout temps en choisissant « Services bancaires en ligne » dans le menu déroulant qui apparaît dans le coin supérieur droit de chaque page.";	
			popupText+= "\n\nPour votre protection, la session des services bancaires en ligne sera maintenant interrompue. Lorsque vous accéderez de nouveau aux services bancaires en ligne, la date et l’heure de la « dernière session » auront été mises à jour.";	
			popupText+= "\n\nCe transfert peut prendre quelques instants. Ne cliquez pas sur le bouton « Précédent » de votre navigateur avant d’avoir utilisé l’autre site pour empêcher la fermeture de toutes vos applications (cette caractéristique vise à assurer votre sécurité).";
			// ask user to confirm and if the answer is "OK" - submit
			if (confirm(popupText)) {
				if (acctType == il || acctType == nb) {	
					set(document.ssoJumpForm, '/OLBmain/ssoAm');
				}else if (acctType == mc) {
					set(document.ssoJumpForm, '/OLBmain/ssoMc');			
				}
				document.ssoJumpForm.submit();
			}
		} else {
			if (acctType == il || acctType == nb) {	
				set(document.ssoJumpForm, '/OLBmain/ssoAm');
			}else if (acctType == mc) {
				set(document.ssoJumpForm, '/OLBmain/ssoMc');			
			}
			document.ssoJumpForm.submit();
		}
	}else if (acctType == 'IPS'){
		//If Term Investment default was chosen, go to the Purchase a GIC option in ONline Banking in the same window.  
		//Don't pop it up.
		goFor('/OLB/inv/gic/tif/termInvestJump?mode=confirm');
	 	
	}else if (acctType == 'CFM' || acctType == 'IL' || 
				acctType == 'NB' || acctType == 'MMC' || acctType == 'OMC') {
		// default forward (new window)
		defaultWindowHandler = window.open('',"window_" + acctType,'width=640,height=480,top=50,left=150,toolbar=yes,scrollbars=yes,resizable=yes,location=yes,menubar=yes,directories=no,status=yes,copyhistory=no');
		document.ssoJumpForm.target= "window_" + acctType;
		set(document.ssoJumpForm, '/OLBmain/ssoDefault');
		document.ssoJumpForm.submit();
	} else {
		// CFM will popup in a new window
		keep = null;
		document.ssoJumpForm.target= "cfmWindow";
		set(document.ssoJumpForm, '/OLBmain/ssoCfm');
		top.cfmWindowHandler = window.open('','cfmWindow','width=640,height=480,top=50,left=150,toolbar=yes,scrollbars=yes,resizable=yes,location=yes,menubar=yes,directories=no,status=yes,copyhistory=no');
		document.ssoJumpForm.submit();
		top.cfmWindowHandler.focus();
	}
	return false;
}

function closePopups() {
	if (top.cfmWindowHandler) {
		top.cfmWindowHandler.close();
	}
	if (top.cfmWindow) {
		top.cfmWindow.close();
	}
	return true;
}

/* Pop up window for all external links */

function Popup(myUrl,popType) {
	var winName='olb_popup';
	var winTop=50;
	var winLeft=150;
	var d = new Date();
	var winTime = d.getUTCHours()+'_'+d.getUTCMinutes()+'_'+d.getUTCSeconds();

	switch (popType) {
		case 1:   // tools & info/help section - max size pop up, full too bar, resizable, scrollbars
		  	popup = window.open(myUrl,winName+winTime,'width=640,height=480,top=' + winTop + ',left=' + winLeft + ',toolbar=yes,scrollbars=yes,resizable=yes,location=yes,menubar=yes,directories=no,status=yes,copyhistory=no');
		break
		case 2:  // olb tool/message  - max pop up size, no tool bar, not resizable, scrolling if necessary
		  popup = window.open(myUrl,winName+winTime,'width=640,height=480,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=no,location=no,menubar=no,directories=no,status=no,copyhistory=no');
		break
		case 3:  // olb tool/message  - smaller pop up size, no tool bar, not resizable, scrolling if necessary
		  popup = window.open(myUrl,winName+winTime,'width=480,height=360,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=no,copyhistory=no');
		break
		case 4:  // calendars  - smallest pop up size, no tool bar, not resizable, scrolling if necessary
		  popup = window.open(myUrl,winName+winTime,'width=360,height=360,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=no,location=no,menubar=no,directories=no,status=no,copyhistory=no');
		break
		case 5:  // popups within popups  - smallest pop up size, no tool bar, not resizable, scrolling if necessary
			winTop=100;
			winLeft=200;
	  	popup = window.open(myUrl,winName+winTime,'width=640,height=480,top=' + winTop + ',left=' + winLeft + ',toolbar=yes,scrollbars=yes,resizable=yes,location=yes,menubar=yes,directories=no,status=no,copyhistory=no');
		break
		case 6:  // printer friendly pages
		  popup = window.open(myUrl,winName+winTime,'width=600,height=480,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=yes,location=no,menubar=no,directories=no,status=no,copyhistory=no');
		break
	}

	popup.focus();

}

function selectAll(formname,checkType) {
	var idx, theCheckbox;
	idx = 0;
	theCheckbox = eval("document." + formname + "." + checkType + idx);
	while (typeof(theCheckbox) == "object")
	{
		theCheckbox.checked = true
		idx++;
		theCheckbox = eval("document." + formname + "." + checkType + idx);
	}

}

function selectAll_1(formname,checkType) {
	var idx, theCheckbox;
	idx = 0;
	theCheckbox = eval("document." + formname + "." + checkType);
	while (typeof(theCheckbox) == "object")
	{
		try {
			theCheckbox[idx].checked = true;
		
			idx++;
			theCheckbox = eval("document." + formname + "." + checkType);
		} catch (ex){
			//one element only
			theCheckbox.checked = true;

			return;
		}
	}
}

function openB2CWindow(URL) {
	window.open(URL,"B2C_PARENT","width=640,height=480,top=50,left=150,toolbar=yes,scrollbars=yes,resizable=yes,location=yes,menubar=yes,directories=no,status=yes,copyhistory=no");
}

function clearAll(formname,checkType) {
	var idx, theCheckbox;
	idx = 0;
	theCheckbox = eval("document." + formname + "." + checkType + idx);
	while (typeof(theCheckbox) == "object")
	{
		theCheckbox.checked = false
		idx++;
		theCheckbox = eval("document." + formname + "." + checkType + idx);
	}
}

function clearAll_1(formname,checkType) {
	var idx, theCheckbox;
	idx = 0;
	theCheckbox = eval("document." + formname + "." + checkType);
	while (typeof(theCheckbox) == "object")
	{
		try {
			theCheckbox[idx].checked = false;
			idx++;
			theCheckbox = eval("document." + formname + "." + checkType);
		} catch (ex){
			//one element only
			theCheckbox.checked = false;

			return;
		}
	}
}

function changeAll(formname,checkType,ifChecked) {
	var idx, theCheckbox;

	if (eval(ifChecked)) {
		idx = 0;
		theCheckbox = eval("document." + formname + "." + checkType + idx);
		while (typeof(theCheckbox) == "object")
		{
			theCheckbox.checked = true
			idx++;
			theCheckbox = eval("document." + formname + "." + checkType + idx);
		}		
	} else {
		idx = 0;
		theCheckbox = eval("document." + formname + "." + checkType + idx);
		while (typeof(theCheckbox) == "object")
		{
			theCheckbox.checked = false
			idx++;
			theCheckbox = eval("document." + formname + "." + checkType + idx);
		}
	}
}

//sets the number of 'checked' checkboxes in the 'counter' field of 'formname'
function processCheckboxes(formname,checkType,counter) {
		
	var idx = 0;
	var count = 0;
	var theCheckbox = eval("document." + formname + "." + checkType);

	var counter = eval("document." + formname + "." + counter);
	
	while (typeof(theCheckbox) == "object")	{

		try {			
			if (theCheckbox[idx].checked == true) {
				counter.value = ++count; 
			}
		} catch (ex) {
			//there is only one checkbox on the form
			if (theCheckbox.checked == true) {
				counter.value = ++count; 
			}
				
			return;
		}

		idx++;
		theCheckbox = eval("document." + formname + "." + checkType);
	}

   	return ;	
}

function resetMe() {
	document.form1.reset();
}

function goHere(loc) {
	this.location.href=loc;
}


function filter (imagename, objectsrc) {
	if (document.images) {
	document.images[imagename].src=eval(objectsrc+".src")
	}
}


/* main tab rolloves */

if (document.images) {

	chinese = new Image;
	chinese_on = new Image;
	finsum = new Image;
	finsum_on = new Image;
	billpay = new Image;
	billpay_on = new Image;
	transfers = new Image;
	transfers_on = new Image;
	messcent = new Image;
	messcent_on = new Image;
	persinfo = new Image;
	persinfo_on = new Image;
	accnts = new Image;
	accnts_on = new Image;
	invest = new Image;
	invest_on = new Image;
	mortlons = new Image;
	mortlons_on = new Image;

	chinese.src = "/images/chinese.gif";
	chinese_on.src = "/images/chinese_on.gif";

	finsum.src = "/images/fr/finsum.gif";
	finsum_on.src = "/images/fr/finsum_on.gif";
	
	billpay.src = "/images/fr/billpay.gif";
	billpay_on.src = "/images/fr/billpay_on.gif";
	
	transfers.src = "/images/fr/transfers.gif";
	transfers_on.src = "/images/fr/transfers_on.gif";
	
	messcent.src = "/images/fr/messcent.gif";
	messcent_on.src = "/images/fr/messcent_on.gif";
	
	persinfo.src = "/images/fr/persinfo.gif";
	persinfo_on.src = "/images/fr/persinfo_on.gif";

	accnts.src = "/images/fr/accnts.gif";
	accnts_on.src = "/images/fr/accnts_on.gif";

	invest.src = "/images/fr/invest.gif";
	invest_on.src = "/images/fr/invest_on.gif";

	mortlons.src = "/images/fr/mortlons.gif";
	mortlons_on.src = "/images/fr/mortlons_on.gif";


				
}


function isPresent(the_value,list)
{
	    var token_array = list.split('&');
        var i;

        if(document.EmtSendForm.list)
        {
        	if(document.EmtSendForm.list.value=="###")
        	{
                for(i=0;i<token_array.length;i++)
                {
       					if(token_array[i].toUpperCase() == the_value.toUpperCase())
                        {
                                return true;
                        }
                }
                return false;
                }
        	else
        	{
        		return false;
        	}
        }
        else
        {
                return false;
        }
}


//*******************************************************************************
// * Function Name: openStaticPopUp
// * 
// * Parameters:
// *   URL		URL to display in the popup
// *   windowName	windowName as per the javascript window.open definition
// *			(the window name can be anything for the purposes of this
// *			 function)
// *
// * Description:
// *   Opens a pop up browser window with the URL that is passed in. This function
// *   is sensitive to the size of the screen and resizes and repositions the 
// *   popup accordingly
// ******************************************************************************/
function openStaticPopUp(URL,windowName)
{
// If the screen resolution in 800x600 then 
	if(screen.availWidth==800)
	{
		top.viewWin = window.open(URL, windowName, "width=680,height=490,top=20,left=60,scrollbars=1,toolbar=0,resizable=1,menubar=0,location=0,status=0,fullscreen=0,directories=0,channelmode=0");
	}
	else
	{
		top.viewWin = window.open(URL, windowName, "width=680,height=490,top=70,left=200,scrollbars=1,toolbar=0,resizable=1,menubar=0,location=0,status=0,fullscreen=0,directories=0,channelmode=0");
	}
}

function openValidatedStaticPopUp(actualURL, validationURL, windowName) {
	openStaticPopUp(validationURL + "?url=" + actualURL, windowName)
}


/* functions to use XMLHttprequest to check SSO popup indicator */
var popCheckRequest = null;
function checkSSOPopup(url,formName){ 
  
  if ( window.XMLHttpRequest )
  	popCheckRequest = new XMLHttpRequest();
  else if ( window.ActiveXObject ) {
    try {
        popCheckRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(err) {
        popCheckRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
	 
  if (popCheckRequest){
    try{ 
      popCheckRequest.onreadystatechange = processResponse;     
      url = url + getFormAsString(formName);
      popCheckRequest.open("GET",url,true);
      popCheckRequest.send(null);
    }catch (err){
      processError();
    }
  }
}

function processResponse(){
  if (popCheckRequest.readyState == 4){ // Complete
    if (popCheckRequest.status == 200){ // OK response
    	if (popCheckRequest.responseText == 'true' || popCheckRequest.responseText == 'false'){
    	var indicator = eval(popCheckRequest.responseText);
		doSsoJump(document.ssoJumpForm.ssoJumpType.value, document.ssoJumpForm.ssoJumpAccount.value, indicator);
		}else{
    	window.location.replace("/SignOut");
   		}
    }else{
      processError();
    }
  }

 }

function processError(){
   var msgTxt = "Error when checking popup indicator!"
     +"\n readyState:" + popCheckRequest.readyState
     +"\n status: " + popCheckRequest.status
     +"\n headers: "+ popCheckRequest.getAllResponseHeaders();
   
   alert(msgTxt);
}

function getFormAsString(formName){
 	returnString ="";	
 	formElements=document.forms[formName].elements; 	
 	for ( var i=formElements.length-1; i>=0; --i ){
 		if(formElements[i].name != "mode") {
	 		///escape (encode) each value
	 		returnString=returnString+"&"+escape(formElements[i].name) + "=" + escape(formElements[i].value);
	 	}
 	} 		
 	return returnString; 
 }
