//   BBL Web Services Server Side JavaScript

// Alternates tables rows background colours
function alternateRowColors() {
	var className1 = 'BB_table';
	var className2 = 'BB_table_maxWidth';
	var className3 = 'BB_table_centre';
	var classSkip = 'BB_tableGroupHead';
	var rowcolor = '#ECECEC';
	var rows, arow, atable;
	var tables = document.getElementsByTagName("table");

	var rowCount = 0;
	//alert ("table length = "+tables.length);
	for(var i=0;i<tables.length;i++) {
		if(tables.item(i).className == className1 || tables.item(i).className == className2 || tables.item(i).className == className3) {
			atable = tables.item(i);
			rows = atable.getElementsByTagName("TR");
			for(var j=0;j<rows.length;j++) {
				arow = rows.item(j);
				if(arow.className != classSkip && j==0) {rowCount =1;
				} else if (arow.className == classSkip) {rowCount =0;
				} else if (arow.nodeName == "TR"  && j!=0) {
					if (rowCount % 2) {arow.style.backgroundColor = rowcolor;}
					rowCount++;
				}

			}
			rowCount = 0;
		}
	}
}
/////////////////
//Macromedia Dreamweaver functions
/////////////////

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length; for(i=0; i<MM_preloadImages.length; i++)
    if (MM_preloadImages[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=MM_preloadImages[i];}}
}
MM_preloadImages( '/global/images/online_bank_logon_button_down.gif','/global/images/online_bank_logon_button.gif','/global/structure_graphics/topRight/searchLogin_r4_c1.gif', '/global/structure_graphics/topRight/searchLogin_r4_c2.gif' ,'/global/structure_graphics/topRight/searchLogin_r4_c3.gif','/global/structure_graphics/topRight/searchLogin_r7_c1.gif','/global/structure_graphics/topRight/searchLogin_r5_c2.gif'); 

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

///////////////////////////////////
// Bendigo Bank Popup Function
// Handles consistent popups

function popUp(URLStr, poptype, popName)
{ 
if (!popName){popName = (!poptype)?"Bendigo":poptype;}

 switch (poptype){
	  case 'Print': poptype="'toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=620,height=450,left=0, top=0,screenX=0,screenY=0'";
		break;
	  case 'External': poptype="'toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=600,height=450,left=0, top=0,screenX=0,screenY=0'";
		break;
	  case 'ExternalLarge': poptype="'toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=700,height=450,left=0, top=0,screenX=0,screenY=0'";
		break;
	  case 'minimum' : poptype = "'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=600,height=450,left=0, top=0,screenX=0,screenY=0'";
		break;
	  case 'everything' : poptype = "'directories=yes,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=600,height=450,left=0, top=0,screenX=0,screenY=0'";
		break;
	  case 'basic' : poptype = "'toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=600,height=450,left=0, top=0,screenX=0,screenY=0'";
		break;
	  case 'basicLarge' : poptype = "'toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=700,height=470,left=0, top=0,screenX=0,screenY=0'";
		break;
	  case 'webcast' : poptype = "'toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=795,height=588,left=0, top=0,screenX=0,screenY=0'";
		break;
	  case 'max_screen' : poptype = "'toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=790,height=535,left=0, top=0,screenX=0,screenY=0'";
		break;		
	  case 'client' : poptype = " ";
		break;
	  case 'virtualMap': poptype="'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=660,height=512,left=0,top=0,screenX=0,screenY=0'";
		break;		
	  case 'ebanking_screen': poptype="'toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=790,height=470,left=0,top=0,screenX=0,screenY=0'";
		break;		
	  case 'Online_share_registry': poptype="'toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=660,height=450,left=0, top=0,screenX=0,screenY=0'";
		break;		

	  default: poptype = "'toolbar=yes,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=600,height=450,left=0, top=0,screenX=0,screenY=0'";
	  	break;
	  }  
  if(eval("window."+popName))
  {
    if(eval("!"+popName+".closed")) {eval(popName+".focus()");}
  }else{
	  eval(popName +"= open('', popName,poptype)" ) ;
	  eval(popName+".focus()");
  }
  eval(popName +"= open(URLStr, popName, poptype)" ) ;  
}

//////////////////////////
// e-Banking Logon Functions
//functions from logon.js
function logonTo(){
	if(document.forms[1].radiobutton[0].checked == true){
		checkSessionCookie();
		setCookies();
	}
	else{
		popUp('https://invest.etrade.com.au/Affiliates/LinkRedirect.aspx?SOURCE=BB1&ACTION=BBLOGON&CALLEDFROM=BB', 'client');
	}
}
var BendigoBank = 0;
function checkSessionCookie()
{
	var urlCookieHelp	= "/nocookie.asp"
	var urlLogon		= "https://www.bendigobank.com.au/banking/BBLIBanking/"
	var name 			= "session";
	var value			= "test_value";

	//set session cookie
	document.cookie	= name + "=" + escape(value);

	//determine which url to open
	url = getCookie(name) ? urlLogon : urlCookieHelp;
	if (BendigoBank && !BendigoBank.closed){BendigoBank.focus();}else{
	BendigoBank = window.open (url,"BendigoBank", "top=0,left=0,toolbar=0,menubar=yes,directories=no,scrollbars=yes,resizable=yes,status=yes,screenX=0,screenY=0,width=790,height=470,border=0,hotkeys=0");
	}
}

// returns true if specified cookie exists

function getCookie(name)
{
	var prefix = name + "=" ;
	var cookieStartIndex = document.cookie.indexOf(prefix);
	//return true if cookie exists
	return (cookieStartIndex != -1) ? true : false;
}

// This calls the script removes leftover cookies before logon

function delete_cookie (name ,path)
{
    // Set expiration date to last year
    var expiration_date = new Date ();
    expiration_date.setYear (expiration_date.getYear () - 1);
    expiration_date = expiration_date.toGMTString ();

    // Expire the cookie
    var cookie_string = name + "= ; expires=" + expiration_date;
    if (path != null) {
        cookie_string += "; path=" + path;
    }
    document.cookie = cookie_string;
    return;
}

function setCookies ()
{
  delete_cookie ("PD-H-SESSION-ID", "/");
  delete_cookie ("PD-ID", "/");  
}
////////////////////////////
// Print Window Functions
//

function closeWindow(){
if (window.close){
	setTimeout("window.close()",0);
}else{
	alert('This Web Browser doest not support this close button. \n Please close the window using your mouse, Ctrl W or (Mac) Command W');
	}
}
function printWindow(){
if (window.print){
	window.print();
}else{
	alert('This Web Browser doest not support this print button. \n Please print using File>Print Menu , (PC) Ctrl P or (Mac) Command P');
	}
}

//////////////////////////////
// Search Form Limitation

function checkSearch(formName){
var result1 = 0;
var result2 = 0;
var searchtext = eval("unescape(document."+formName+".zoom_query.value)");
var pattern = /[\?\d\w]{0,3}\*[\?\d\w]{0,3}/g;
var resultArray = searchtext.match(pattern);
if (resultArray){
	for (var i= 0; i<resultArray.length; i++){
		if(resultArray[i].length < 4){
			var alertText = resultArray[i] + " is not allowed.\n";
			alertText += "You must preceed or follow * with at least 3 characters\n";
			alertText += "Example: wor* is OK, wo*d is not.\n";
			alert(alertText);
			result1 = 1;
		}
	}
}

pattern = /[\d\w]{0,2}\?[\d\w]{0,2}/g;
var resultArray2 = searchtext.match(pattern);
if (resultArray2){
	for (var i= 0; i<resultArray2.length; i++){
		if(resultArray2[i].length < 3){
			var alertText = resultArray2[i] + " is not allowed.\n";
			alertText += "You must preceed or follow ? with at least 2 characters\n";
			alertText += "Example: an? is OK, a? is not.\n";
			alert(alertText);
			result2 = 1;
		}
	}
}
result = (result1 + result2 == 0)?true:false;
return(result);
}