var jsvMaxcharLengthArray = Array();

var jsvIsIE501 = navigator.userAgent.indexOf("MSIE 5.01") > 0 ? true : false;
var jsvIsNN6 = navigator.userAgent.indexOf("Netscape6") > 0 ? true : false;
var jsvIsIE=document.all&&navigator.userAgent.indexOf("Opera")==-1;

//Event.observe(window, 'load', pws_jsfInit, false);


function pws_jsfTogglePage(self)
{
	if (jsvIsIE) 
	{
		var container = self.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
		var element1 = container.nextSibling;
		var element2 = element1.nextSibling;
		
		self.parentElement.parentElement.className="selected";
		if (self.parentElement.parentElement.nextSibling != null)
			self.parentElement.parentElement.nextSibling.className="";
		if (self.parentElement.parentElement.previousSibling != null)
			self.parentElement.parentElement.previousSibling.className="";
		
		
		if (element1.style.display == "none")
		{
			element1.style.display = "";
			element2.style.display = "none";
		} else {
			element1.style.display = "none";
			element2.style.display = "";
		}
	} else {
		var container = self.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
		var element1 = container.nextSibling.nextSibling;
		var element2 = element1.nextSibling.nextSibling;
		
		self.parentNode.parentNode.className="selected";
		if (self.parentNode.parentNode.nextSibling.nextSibling != null)
			self.parentNode.parentNode.nextSibling.nextSibling.className="";
		if (self.parentNode.parentNode.previousSibling.previousSibling != null)
			self.parentNode.parentNode.previousSibling.previousSibling.className="";
		
		if (element1.style.display == "none")
		{
			element1.style.display = "";
			element2.style.display = "none";
		} else {
			element1.style.display = "none";
			element2.style.display = "";
		}
	}
}

function pws_getElementsByName_iefix(tag, name)
{
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function pws_contractAll()
{
	var togs, contents;
	
	if (jsvIsIE)
	{
		togs = pws_getElementsByName_iefix("div", "reduceToggle");
		contents = pws_getElementsByName_iefix("div", "content");
	}
	else
	{
		togs = document.getElementsByName("reduceToggle");
		contents = document.getElementsByName("content");
	}
	
	for (i=0; i<togs.length; i++)
	{
		togs[i].style.display = "none";
		contents[i].style.display = "none";
	}
}

function pws_jsfToggleMenu(self)
{
	if (jsvIsIE) 
	{
		if (self.parentElement.parentElement.children[0].style.display == "none")
		{
			self.parentElement.parentElement.children[0].style.display = "";
			self.parentElement.parentElement.children[1].style.display = "none";
			self.parentElement.parentElement.parentElement.children[1].children[1].style.display = "none";
		} else {
			self.parentElement.parentElement.children[0].style.display = "none";
			self.parentElement.parentElement.children[1].style.display = "";
			self.parentElement.parentElement.parentElement.children[1].children[1].style.display = "";
		}
	} else {
		if (self.parentNode.parentNode.childNodes[1].style.display == "none")
		{
			self.parentNode.parentNode.childNodes[1].style.display = "";
			self.parentNode.parentNode.childNodes[3].style.display = "none";
			self.parentNode.parentNode.parentNode.childNodes[3].childNodes[3].style.display = "none";
		} else {
			self.parentNode.parentNode.childNodes[1].style.display = "none";
			self.parentNode.parentNode.childNodes[3].style.display = "";
			self.parentNode.parentNode.parentNode.childNodes[3].childNodes[3].style.display = "";
		}
	}
}

function pws_jsfInit()
{
	pws_jsfBalanceTables();
}

function pws_jsfBalanceTables()
{
	tables = document.getElementsByClassName("jsBalanceColumns");
	//exit if there is only one table
	if(tables.length<2)
	{
		return;
	}
	var firstTRFound;
	//count all letters in cells get maxium for each column
	for(a=0;a<tables.length;a++)
	{
		firstTRFound = false;
		//find TBODY
		for(b=0;b<tables[a].childNodes.length;b++)
		{
			if(tables[a].childNodes[b].nodeName=="TBODY")
			{
				//get first TR
				for(c=0;c<tables[a].childNodes[b].childNodes.length;c++)
				{
					if(tables[a].childNodes[b].childNodes[c].nodeName=="TR"&&!firstTRFound)
					{
						firstTRFound = true;
						pws_jsfCollectCellTextLength(tables[a].childNodes[b].childNodes[c]);
					}
				}
			}
		}
	}
	//convert values into percentages
	columnWidthArray = pws_jsfConvertValuesToPercentage(jsvMaxcharLengthArray);
	
	for(a=0;a<tables.length;a++)
	{
		firstTRFound = false;
		//find TBODY
		for(b=0;b<tables[a].childNodes.length;b++)
		{
			if(tables[a].childNodes[b].nodeName=="TBODY")
			{
				//get first TR
				for(c=0;c<tables[a].childNodes[b].childNodes.length;c++)
				{
					if(tables[a].childNodes[b].childNodes[c].nodeName=="TR"&&!firstTRFound)
					{
						//set column widths
						firstTRFound = true;
						pws_setColumnWidths(columnWidthArray,tables[a].childNodes[b].childNodes[c]);
						
					}
				}
			}
		}
	}
}

function pws_jsfConvertValuesToPercentage(theArray)
{
	totalValue = 0;
	for(f=0;f<theArray.length;f++)
	{
		totalValue+=theArray[f];
	}
	percArray = Array();
	for(f=0;f<theArray.length;f++)
	{
		percArray.push(Math.round(theArray[f]/totalValue*100));
	}
	return percArray;
}

function pws_setColumnWidths(widthArray,row)
{
	cell = 0;
	for(g=0;g<row.childNodes.length;g++)
	{
		if(row.childNodes[g].nodeName=="TD"||row.childNodes[g].nodeName=="TH")
		{
			if (row.childNodes[g].colSpan != null && row.childNodes[g].colSpan > 1)
			{
				var tempLength = 0;
				for (i=0; i<row.childNodes[g].colSpan; i++)
				{
					tempLength += widthArray[cell+i];
				}
				row.childNodes[g].style.width=tempLength+"%";
//				row.childNodes[g].style.width=(widthArray[cell]*row.childNodes[g].colSpan)+"%";
				cell+=row.childNodes[g].colSpan;
			}
			else
			{
				row.childNodes[g].style.width=widthArray[cell]+"%";
				cell++;
			}
		}
	}
}

function pws_jsfCollectCellTextLength(row)
{
	if(row==null)
	{
		return;	
	}
	firstPass = jsvMaxcharLengthArray.length==0?true:false;
	
	cell = 0;
	//go through each td/th
	for(d=0;d<row.childNodes.length;d++)
	{
		//find TD or TH
		if((row.childNodes[d].nodeName=="TD"||row.childNodes[d].nodeName=="TH"))
		{
			for(e=0;e<row.childNodes[d].childNodes.length;e++)
			{
				//find text node
				if(row.childNodes[d].childNodes[e].nodeType==3)
				{
					//alert(row.childNodes[d].childNodes[e].nodeValue);
					if(firstPass)
					{
						if (row.childNodes[d].colSpan != null && row.childNodes[d].colSpan > 1)
						{
							for (x=0; x<row.childNodes[d].colSpan; x++)
							{
								jsvMaxcharLengthArray.push((pws_getStringDisplayLength(row.childNodes[d].childNodes[e].nodeValue)/row.childNodes[d].colSpan));
							}
						}
						else
						{
							jsvMaxcharLengthArray.push(pws_getStringDisplayLength(row.childNodes[d].childNodes[e].nodeValue));
						}
					}
					else
					{
						
						if (row.childNodes[d].colSpan != null)
						{
							for (x=0; x<row.childNodes[d].colSpan; x++)
							{
								jsvMaxcharLengthArray[cell+x] = jsvMaxcharLengthArray[cell+x]<(pws_getStringDisplayLength(row.childNodes[d].childNodes[e].nodeValue) / row.childNodes[d].colSpan)?(pws_getStringDisplayLength(row.childNodes[d].childNodes[e].nodeValue) / row.childNodes[d].colSpan):jsvMaxcharLengthArray[cell+x];
							}
							cell+=row.childNodes[d].colSpan;
						} 
						else
						{
							jsvMaxcharLengthArray[cell] = jsvMaxcharLengthArray[cell]<pws_getStringDisplayLength(row.childNodes[d].childNodes[e].nodeValue)?pws_getStringDisplayLength(row.childNodes[d].childNodes[e].nodeValue):jsvMaxcharLengthArray[cell];
							cell++;
						}
					}
				}
			}
		}
	}
	//find the next row in the table and recurse
	pws_jsfCollectCellTextLength(pws_getNextSibling(row,"TR"));
}


function pws_getStringDisplayLength(theString)
{
	theLength = 0;
	for(i=0;i<theString.length;i++)		
	{
		theLength+=(pws_getCharDisplayLength(theString.charAt(i)));
	}
	return theLength;
}

function pws_getCharDisplayLength(theChar)
{
	switch(theChar)
	{
		case 'i':
		case 'j':
		case 'i':
		case 'j':
		case '.':
		case ',':
			return 1;
		case 'f':
		case 'r':
		case 't':
			return 3;
		case 'k':
		case 's':
		case 'J':
			return 4;
		case 'i':
		case 'j':
		case 'l':
		case 'I':
		case '.':
		case ',':
		case 'f':
		case 'r':
		case 't':
		case 'k':
		case 's':
		case 'J':
		case 'a':
		case 'b':
		case 'c':
		case 'd':
		case 'e':
		case 'g':
		case 'h':
		case 'n':
		case 'o':
		case 'p':
		case 'q':
		case 'u':
		case 'v':
		case 'x':
		case 'Y':
		case 'z':
		case 'E':
		case 'F':
		case 'L':
		case 'P':
		case 'T':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case '(':
		case ')':
		case '/':
			return 5;
		case 'B':
		case 'C':
		case 'D':
		case 'H':
		case 'K':
		case 'N':
		case 'R':
		case 'S':
		case 'U':
		case 'V':
		case 'X':
		case 'Z':
		case '0':
		case '%':
			return 6;
		case 'm':
		case 'A':
		case 'G':
		case 'M':
		case 'O':
		case 'Q':
		case 'Y':
			return 8;
		case 'w':
		case 'W':
			return 11;
		default:
			return 0;
	}
}

function pws_getNextSibling(object,siblingNodeName)
{
	if(object.nextSibling!=null)
	{
		if(object.nextSibling.nodeName==siblingNodeName)
		{
			return object.nextSibling;
		}
		else
		{
			return pws_getNextSibling(object.nextSibling,siblingNodeName);
		}
	}
}

function pws_hbap_popupWin(helpUrl ,widthSize, heightSize) {
var search_string=location.search;
var rs_url="";
var wedTrends_unit = "";
var reg = /WT.mc_id/;
	if(search_string == null || search_string.length == 0){
		rs_url = helpUrl;
	}
	else{
		var mat_index = search_string.search(reg);
		if (mat_index >= 0) {
			var temp = search_string.substring(mat_index);	
			var index_and = temp.search(/&/);
			if(index_and > 0){
				wedTrends_unit = temp.substring(0, index_and);			
			} else {
				wedTrends_unit = temp;			
			}	
			if (helpUrl.indexOf("?") > 0) {	
				rs_url=helpUrl + "&" + wedTrends_unit ;
			} else {
				rs_url=helpUrl + "?" + wedTrends_unit ;
			}
		
		}else {
			rs_url = helpUrl;
		}	
	}
   if(widthSize != null && heightSize != null){   
   	var popWin = window.open(rs_url,"onlinehsbc","width="+widthSize+",height="+heightSize);   	
   } else {   
   	var popWin = window.open(rs_url);
   }	 
	popWin.focus();		
}

function pws_show_TandC(loc,windoc) {

if(loc==null || windoc==null) {
	return ;
}

var urlpath=loc.hash;

if ( (urlpath == null) || (urlpath == "")) {
	return;
}
<!--urlpath must have at least one word after "#"-->
if ( urlpath.length < 2) {
	return;	
}
var id = urlpath.substring(1);
var show_postfix="_s";
var hide_postfix="_h";
var content_postfix="_c";



if ((windoc.getElementById(id+show_postfix)!= null) && (windoc.getElementById(id+hide_postfix)!= null)
		 && (windoc.getElementById(id+content_postfix)!= null) ){

	if ((windoc.getElementById(id+show_postfix).style!= null) && (windoc.getElementById(id+hide_postfix).style!= null)
			&& (windoc.getElementById(id+content_postfix).style!= null) ) {
			
			if ( (windoc.getElementById(id+show_postfix).style.display!= null) 
				&& (windoc.getElementById(id+hide_postfix).style.display!= null) 
				&& (windoc.getElementById(id+content_postfix).style.display!= null) ) {
				
				windoc.getElementById(id+show_postfix).style.display = "none";
				windoc.getElementById(id+hide_postfix).style.display = "";
				windoc.getElementById(id+content_postfix).style.display = "";
				
			}
	}
}



}

function pws_digitalOrder_en(formname, cookievalue)
{
	pws_popupNewBrowser('', 'yes', 'no', 'yes', 'no', 'no', 'yes', screen.width, screen.height*0.88);

	pws_setCookie('FromWebSite', cookievalue, '/', '.hsbc.com.hk', 'true', 0);

	document.forms[formname].action = 'https://www.ebanking.hsbc.com.hk/1/2/logon?LANGTAG=en&COUNTRYTAG=US';
	document.forms[formname].submit();	
}

function pws_digitalOrder_zh(formname, cookievalue)
{
	pws_popupNewBrowser('', 'yes', 'no', 'yes', 'no', 'no', 'yes', screen.width, screen.height*0.88);

	pws_setCookie('FromWebSite', cookievalue, '/', '.hsbc.com.hk', 'true', 0);

	document.forms[formname].action = 'https://www.ebanking.hsbc.com.hk/1/2/logon?LANGTAG=zh&COUNTRYTAG=HK';
	document.forms[formname].submit();	
}


function pws_popupNewBrowser(url, status, location, scroll, mbar, toolbar, resize, width, height, winname)
{
	!winname? winname='nb':winname=winname;
	nb=window.open(url, winname, 'status=' + status + ',location=' + location + ',scrollbars=' + scroll + ',menubar=' + mbar + ',toolbar=' + toolbar + ',resizable=' + resize + ',height=' + height + ',width=' + width + ',left=0,top=0');
	nb.focus();
}

function pws_setCookie(name, value, path, domain, secure, expires)
{
    document.cookie = name + '=' + escape(value) +
    ((path) ? '; path=' + path : '') +
    ((domain) ? '; domain=' + domain : '') +
    ((secure) ? ';secure' : '')+
    ((expires) ? ';expires=' + expires.toGMTString() : '') ;
}

function pws_logon_en() {
	pws_popupNewBrowser('https://www.ebanking.hsbc.com.hk/1/2/logon?LANGTAG=en&COUNTRYTAG=US', 'yes', 'no', 'yes', 'no', 'no', 'yes', screen.width, screen.height*0.88);
}

function pws_logon_zh() {
	pws_popupNewBrowser('https://www.ebanking.hsbc.com.hk/1/2/logon?LANGTAG=zh&COUNTRYTAG=HK', 'yes', 'no', 'yes', 'no', 'no', 'yes', screen.width, screen.height*0.88);
}

function pws_popupPdf(pdfUrl ,windowName, winProperties) {

var targetUrl=it_fullContextPath.substring(0,it_fullContextPath.length-1) + pdfUrl;
var hsbcheader='\n'
+ '    <html>\n'
+ '	    <script language="JavaScript" src="' + wt_header_url +'"><\/script>\n'
+ '	    <SCRIPT LANGUAGE="JavaScript">\n'
+ '		HSBC.SITE.rgn="HBAP";\n'
+ '		HSBC.SITE.subrgn="AMH";\n'
+ '		HSBC.SITE.cnty="HKSAR";\n'
+ '	        HSBC.SITE.ent="HSBC";\n'
+ '             HSBC.LOG.dcsuri="' + pdfUrl + '";\n'
+ '		HSBC.SITE.brand="HSBC";\n'
+ '  	  	HSBC.DCS.ID="dcsur62uf00000s5nnzq4hvrl_6i5x";\n'
+ '    <\/SCRIPT>\n'
+ '    <script language="JavaScript" src="' + wt_footer_url + '"><\/SCRIPT>'
+ '     <body onload="window.location=\'' + targetUrl + '\';"><\/body>'
+ '    </html>\n'
+ '    \n';

   var popWin;
   if(winProperties != null && winProperties.length>0){   
   	popWin = window.open('','_blank',winProperties);   	
   } else {   
   	popWin = window.open('');   	
   }
  	popWin.document.write(hsbcheader);
   	popWin.document.close(); 	
	popWin.history.go(0); 
	popWin.focus();		 
	
}

function pws_transitPage(transitType, formname, fieldname, targetUrl,  width, height, winname){
	switch(transitType){
		case "TransitPulldown":
			if(formname == null || formname == "") {
				alert("Invalid parameters for TransitPage('transitType',???,'fieldname', 'targetUrl', 'width', 'height')");
			} else if(fieldname == null || fieldname == ""){
				alert("Invalid parameters for TransitPage('transitType','formname',???, 'targetUrl', 'width', 'height')");
			} else{
				var idx = document.forms[formname].elements[fieldname].selectedIndex;
				var v = document.forms[formname].elements[fieldname][idx].value;
				if(v != ""){					
					if (v.indexOf("?targetPage=") > 0){
						!winname? winname="nb":winname=winname;
						nb=window.open(v, winname, "status=no,location=no,scrollbars=no,menubar=no,toolbar=no,resizable=no,height=" + height + ",width=" + width + ",left=20,top=20");
						nb.focus();
					}else{
						alert("Please specify the target page using '?targetPage='.");
					}
					
				}
		
			}
			break;
		case "TransitClickOn":
		
			if(targetUrl == null || targetUrl == ""){
				alert("Invalid parameters for TransitPage('transitType','formname','fieldname', ???, 'width', 'height')");
			}else{
					if (targetUrl.indexOf("?targetPage=") > 0){
						!winname? winname="nb":winname=winname;
						nb=window.open(targetUrl, winname, "status=no,location=no,scrollbars=no,menubar=no,toolbar=no,resizable=no,height=" + height + ",width=" + width + ",left=20,top=20");
						nb.focus();
					}else{
						alert("Please specify the target page using '?targetPage='.");
					}				
			}
			break;
				
		default:
			break;	
		
	}
}

function pws_transitPage_Close()
{
	self.close();
}

function pws_targetPage(){
var search_string=location.search;
var target_page = "";
var indicator = "targetPage=";
var target_ind = search_string.indexOf(indicator);
if( target_ind > 0){
	temp = search_string.substring(indicator.length + 1);	
	var index_and = temp.search(/&/);
	if(index_and > 0){
		target_page = temp.substring(0, index_and) + "?" + temp.substring((index_and+1), temp.length);
	} else {
		target_page = temp;
	}
	
}else{
	target_page = "http://www.hsbc.com.hk";
}
var openwin = window.open(target_page);
openwin.focus();
}
