var tempHtml;
var tempString;
var tempString02;
var tempString03;
//var greenColor = "#57744B";
var greenColor = "#43902f";
var pulldownHeightArray = new Array(200, 200, 200, 200, 200, 200, 200, 200);
var pulldownW = 146;

var isIE;
var completeTable;
var completeField;
var autorow;
var req;
var menu;

var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

if (section == null)
	{
		var section;
	}

//var totalTopNum = sectionArray.length;
var totalTopNum = 8;

if (subSection == null)	var subSection;
if (level1 == null)	var level1;

//PreLoad Images
for (i=1; i<=totalTopNum; i++) {
	if (subSectionArray[i-1] != null) {
		for (j=1; j<=subSectionArray[i-1].length; j++) {
			MM_preloadImages(path+'images/topnav/'+sectionArray[i-1].id+'_'+subSectionArray[i-1][j-1].id+'.gif');
		}
	}
}

tempHtml = '<center>';
tempHtml += '<div style="position:relative; width:770px;">';

tempHtml += '<form name="autofillform" action="' + path + '../../autocomplete" method="get" onSubmit="return doSearch(this);">';

tempHtml += '<div style="position:absolute; width:770px; height:10px; z-index:2000;">';

tempHtml += '<input type="hidden" name="action" value="lookupbyname">';
tempHtml += '<input type="hidden" name="lang" value="chi">';

tempHtml += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';

tempHtml += '	<tr valign="top">';
tempHtml += '		<td style="padding-bottom:3px;"><a href="'+path+'index.jsp"><img src="'+path+'images/common/logo.gif" alt="Bank Consortium Trust" width="250" height="71" border="0"></a></td>';
tempHtml += '		<td height="100%" align="right" style="padding-bottom:4px;">';

//Top Navigation Start

tempHtml += '<table height="100%" border="0" cellspacing="0" cellpadding="0">';
tempHtml += '	<tr>';
tempHtml += '		<td valign="top" align="right">';
tempHtml += '<table height="18" border="0" cellspacing="0" cellpadding="0">';
tempHtml += '	<tr valign="top">';

//Home Button
tempHtml += '		<td width="1" bgcolor="#CCCCCC"><img src="'+path+'images/common/spacer.gif" width="1" height="1"></td>';
tempHtml += '		<td style="padding-top:6px;"><a href="'+path+topArray[0].link+'" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\''+topArray[0].id+'\',\'\',\''+path+'images/topnav/btn_'+topArray[0].id+'_f2.gif\',1)"><img src="'+path+'images/topnav/btn_'+topArray[0].id+'.gif" border="0" name="'+topArray[0].id+'" hspace="11" alt="'+topArray[0].name+'"></a></td>';

for (i=11; i<=15; i++) {
	tempHtml += '		<td width="1" bgcolor="#CCCCCC"><img src="'+path+'images/common/spacer.gif" width="1" height="1"></td>';
	tempHtml += '		<td style="padding-top:6px;"><a href="'+path+sectionArray[i].link+'" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\''+sectionArray[i].id+'\',\'\',\''+path+'images/topnav/btn_'+sectionArray[i].id+'_f2.gif\',1)"><img src="'+path+'images/topnav/btn_'+sectionArray[i].id+'.gif" border="0" name="'+sectionArray[i].id+'" hspace="11" alt="'+sectionArray[i].name+'"></a></td>';
}

//Laugage Button
tempHtml += '		<td width="1" bgcolor="#CCCCCC"><img src="'+path+'images/common/spacer.gif" width="1" height="1"></td>';
tempHtml += '		<td style="padding-top:6px;"><a href="javascript:changeLanguage(\'eng\');" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\''+topArray[1].id+'\',\'\',\''+path+'images/topnav/btn_'+topArray[1].id+'_f2.gif\',1)"><img src="'+path+'images/topnav/btn_'+topArray[1].id+'.gif" border="0" name="'+topArray[1].id+'" hspace="11" alt="'+topArray[1].name+'"></a></td>';
tempHtml += '	</tr>';
tempHtml += '</table>';
tempHtml += '		</td>';
tempHtml += '	</tr>';
tempHtml += '	<tr valign="bottom">';
tempHtml += '		<td style="padding-left:34px; padding-right:10px;">';
tempHtml += '<table height="17" border="0" cellspacing="0" cellpadding="0">';
tempHtml += '	<tr>';

//Search Start
//Added by Peter @ 24/08/2006
tempHtml += '	<td width="114"><input type="text" autocomplete="off" id="complete-field" maxlength="40" name="keyword" onkeyup="doCompletion();" style="width:109px; height:17px;" class="box"></td>';
// fix double submission with IE and search witout keyword
// Added by Samuel Lai @ 07/02/2007
tempHtml += '	<td style="padding-left:3px;"><input type="image" src="'+path+'images/topnav/btn_search.gif" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'btn_search\',\'\',\''+path+'images/topnav/btn_search_f2.gif\',1)" name="btn_search" id="btn_search" value="Submit" alt="搜尋"></td>';
//End of Add 
//Search End

for (i=8; i<=10; i++) {
	tempHtml += '		<td><img src="'+path+'images/common/point_01.gif" width="3" height="3" hspace="8" vspace="3"></td>';
	tempHtml += '		<td><a href="'+path+sectionArray[i].link+'" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'top_'+sectionArray[i].id+'\',\'\',\''+path+'images/topnav/btn_'+sectionArray[i].id+'_f2.gif\',1)"><img src="'+path+'images/topnav/btn_'+sectionArray[i].id+'.gif" border="0" name="top_'+sectionArray[i].id+'" id="top_'+sectionArray[i].id+'" alt="'+sectionArray[i].name+'"></a></td>';
}

tempHtml += '		<td><img src="'+path+'images/common/point_01.gif" width="3" height="3" hspace="8" vspace="3"></td>';

tempHtml += '		<td><img src="'+path+'images/common/text_fontsize.gif"><a href="javascript:changeFontSize(\'contentDiv\',1,0);"><img src="'+path+'images/common/btn_increase.gif" width="13" height="13" hspace="1" border="0" align="absmiddle" alt="字型 +"></a><a href="javascript:changeFontSize(\'contentDiv\',-1,0);"><img src="'+path+'images/common/btn_decrease.gif" width="13" height="13" border="0" align="absmiddle" alt="字型 -"></a></td>';

tempHtml += '	</tr>';

tempHtml += '	<tr><td height="1" colspan="10" id="auto-row"></td></tr>';

tempHtml += '</table>';

tempHtml += '		</td>';
tempHtml += '	</tr>';

tempHtml += '</table>';

//Top Navigation End


tempHtml += '		</td>';

tempHtml += '	</tr>';

tempHtml += '	<tr>';
tempHtml += '	<td colspan="2"><table height="17" border="0" cellspacing="0" cellpadding="0">';

tempHtml += '	<tr>';
tempHtml += '	<td width="370" colspan="9"><img src="'+path+'images/common/spacer.gif" width="370" height="1" border="0"></td>';
tempHtml += '	<td width="400" colspan="9"><img src="'+path+'images/common/spacer.gif" width="400" height="1" border="0"></td>';
tempHtml += '	</tr>';

tempHtml += '	<tr valign="top">';


for (i=1; i<=totalTopNum; i++) {
	
	if (i >= 5) {
		tempString = greenColor;
	} else {
		tempString = '';
	}
	if (i == 1 | i == 5) {
		tempString02 = 10;
	} else {
		tempString02 = 20;
	}
	
	tempHtml += '<td bgcolor="'+tempString+'"><img src="'+path+'images/common/spacer.gif" width="'+tempString02+'" height="1" border="0"></td>';
	tempHtml += '<td bgcolor="'+tempString+'"><div style="position:absolute; width:100%;">';
	
// Nav Bar
	if (isInternetExplorer)
		{
			tempHtml += '<div style="position:absolute; top:25px; width:100%; height:3px; background-color:'+colorArray[i-1][0]+'; visibility:hidden;" align="center" id="btnBar_'+i+'" onMouseOver="btnOver('+i+', 1);" onMouseOut="btnOver('+i+', 0);"><img src="'+path+'images/common/spacer.gif" width="1" height="3" border="0"></div>';
		}
	else
		{
			tempHtml += '<div style="position:absolute; top:25px; width:50px; height:3px; background-color:'+colorArray[i-1][0]+'; visibility:hidden;" align="center" id="btnBar_'+i+'" onMouseOver="btnOver('+i+', 1);" onMouseOut="btnOver('+i+', 0);"><img src="'+path+'images/common/spacer.gif" width="1" height="3" border="0"></div>';
		}
	
	
// Pulldown Start Here

	if (subSectionArray[i-1] != null) {
		
		if (isInternetExplorer)
			{
				tempHtml += '<div id="mask_'+i+'" style="position:absolute; left:0px; top:28px; width:'+pulldownW+'; height:300; overflow:hidden;">';
				tempHtml += '<div id="pulldown_'+i+'" style="position:absolute; left:0px; top:-'+(pulldownHeightArray[i-1])+'px; width:'+pulldownW+'px; z-index:1000; visibility: visible;" onMouseOver="btnOver('+i+', 1);" onMouseOut="btnOver('+i+', 0);">';
			}
		else
			{
				tempHtml += '<div id="pulldown_'+i+'" style="position:absolute; display:none; left:0px; top:-'+(pulldownHeightArray[i-1])+'px; width:'+pulldownW+'px; z-index:1000; visibility: visible;" onMouseOver="btnOver('+i+', 1);" onMouseOut="btnOver('+i+', 0);">';
			}
		
		//tempHtml += '<div id="rightshadow" style="position: absolute; top: 0px; left: '+Number(pulldownW-6)+'px; height: 100%; width: 6px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'images/common/shadow_right.png\', sizingMethod=\'scale\')"></div>';

		//tempHtml += '<div id="bottomshadow" style="position: absolute; top: 172px; left: 0px; height: 6px; width: 140px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'images/common/shadow_bottom.png\', sizingMethod=\'image\')"></div>';
		
		
		//tempHtml += '<div style="width:'+Number(pulldownW-6)+'px; padding:6px; border:1px solid '+colorArray[i-1][0]+'; background-color:'+colorArray[i-1][1]+';">';
		
		tempHtml += '<table width="'+pulldownW+'" border="0" cellspacing="0" cellpadding="0">';
		tempHtml += '	<tr>';
		tempHtml += '		<td width="100%" style="padding:6px; border:1px solid '+colorArray[i-1][0]+'; background-color:'+colorArray[i-1][1]+';">';
		
		tempHtml += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		
		//alert(subSectionArray[i-1].length);
		for (j=1; j<=subSectionArray[i-1].length; j++) {
			
			tempHtml += '<tr>';
			
			tempHtml += '<td style="padding-left:3px;"><a href="'+path+subSectionArray[i-1][j-1].link+'" target="'+subSectionArray[i-1][j-1].target+'" onMouseOver="subBtnOver('+i+', '+j+', 1);" onMouseOut="subBtnOver('+i+', '+j+', 0);" style="width:100%"><img src="'+path+'images/topnav/'+sectionArray[i-1].id+'_'+subSectionArray[i-1][j-1].id+'.gif" vspace="5" border="0" name="'+sectionArray[i-1].id+'_'+subSectionArray[i-1][j-1].id+'" id="'+sectionArray[i-1].id+'_'+subSectionArray[i-1][j-1].id+'" alt="'+subSectionArray[i-1][j-1].name+'"></a></td>';
			
			tempHtml += '</tr>';
			
			if (j < subSectionArray[i-1].length) {
				tempHtml += '<tr>';
				tempHtml += '	<td bgcolor="#DFDBD4"><img src="'+path+'images/common/spacer.gif" width="1" height="1"></td>';
				tempHtml += '</tr>';
			}
		}
		
		tempHtml += '</table>';
		tempHtml += '</td>';
		
		tempHtml += '<td width="6" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+path+'images/common/shadow_right.png\', sizingMethod=\'scale\')"><img src="'+path+'images/common/spacer.gif" width="6" height="1"></td>';
		
		tempHtml += '</tr>';
		
		tempHtml += '<tr>';
		tempHtml += '	<td colspan="2" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+path+'images/common/shadow_bottom.png\', sizingMethod=\'image\')"><img src="'+path+'images/common/spacer.gif" width="1" height="6"></td>';
		tempHtml += '</tr>';
		
		tempHtml += '</table>';
		
		tempHtml += '</div>';
		if (isInternetExplorer)
			{
				tempHtml += '</div>';
			}
	}
	
// Pulldown End Here	
	tempHtml += '</div>';
	
	
// Main Navigation Start Here

	tempHtml += '<a href="'+path+sectionArray[i-1].link+'" target="'+sectionArray[i-1].target+'" onMouseOver="btnOver('+i+', 1);" onMouseOut="btnOver('+i+', 0);"><img src="'+path+'images/topnav/btn_'+sectionArray[i-1].id+'.gif" alt="'+sectionArray[i-1].name+'" border="0" name="mainBtn_'+i+'"></a>';
	
// Main Navigation End Here

	tempHtml += '</td>';
	
	if (i == 4 | i == 8) {
		tempString02 = 10;
	} else {
		tempString02 = 20;
	}
	if (i == 4 | i == 8)	tempHtml += '<td bgcolor="'+tempString+'"><img src="'+path+'images/common/spacer.gif" width="'+tempString02+'" height="1" border="0"></td>';
}


tempHtml += '	</tr>';
tempHtml += '</table>';
tempHtml += '</td>';
tempHtml += '</tr>';
tempHtml += '</table>';

tempHtml += '<div style="position: absolute; top:0px;left:370px;z-index:1001; border: solid 0px #CCCCCC;" id="menu-popup">';
tempHtml += '	<table id="completeTable" border="0" cellpadding="0" cellspacing="0"></table>';
tempHtml += '</div>';
tempHtml += '</form>';
tempHtml += '</div>';

document.write(tempHtml);
if (isInternetExplorer) {
	document.getElementById('mask_'+totalTopNum).style.pixelLeft = -(pulldownW - document.getElementById('mainBtn_'+totalTopNum).width - 10);
}

function subBtnOver(i, j, over) {
	if (over == 1) {
		document.getElementById(sectionArray[i-1].id+'_'+subSectionArray[i-1][j-1].id).src = path+'images/topnav/'+sectionArray[i-1].id+'_'+subSectionArray[i-1][j-1].id+'_f2.gif';
	} else {
		document.getElementById(sectionArray[i-1].id+'_'+subSectionArray[i-1][j-1].id).src = path+'images/topnav/'+sectionArray[i-1].id+'_'+subSectionArray[i-1][j-1].id+'.gif';
	}
}

function btnOver(id, over, pulldownGo) {
	if (over == 1) {
		document.getElementById('btnBar_'+id).style.visibility = 'visible';
		if (subSectionArray[id-1] != null)	pulldownStart(id, 1);
	} else {
		document.getElementById('btnBar_'+id).style.visibility = 'hidden';
		if (subSectionArray[id-1] != null)	pulldownStart(id, 0);
	}
}

var goSpeed = 1;
var speed;
var goStep;
var timeOut;
var timeOut02;
var tempTop;
var objTop;
var pulldownNo;

function pulldownStart(target, motion) {
	pulldownNo = target;
	if (isInternetExplorer)
		{
			if (motion == 1) {
				tempTop = 0;
				speed = 7;
				pulldownReset(0);
				pulldownMotion();
				if (section == 0)	if (target == 7 | target == 8)	document.getElementById('planMenu').style.visibility = 'hidden';
			} else {
				tempTop = -pulldownHeightArray[target-1];
				speed = 3;
				timeOut02 = setTimeout('pulldownMotion();', 100);
				if (section == 0)	if (target == 7 | target == 8)	document.getElementById('planMenu').style.visibility = 'visible';
			}
		}
	else
		{
			for (i=1; i<=totalTopNum; i++)
				{
					if (subSectionArray[i-1] != null)
						{
							if ((i == pulldownNo)
							&& (motion == 1))
								{
									document.getElementById('pulldown_'+pulldownNo).style.top = 28;
									document.getElementById('pulldown_'+pulldownNo).style.display = 'block';
								}
							else
								{
									document.getElementById('pulldown_'+i).style.top = -pulldownHeightArray[i-1];
									document.getElementById('pulldown_'+i).style.display = 'none';
								}
						}
				}
		}
}

function pulldownMotion()
{
	objTop = Number(document.getElementById('pulldown_'+pulldownNo).style.pixelTop);
	goStep = (tempTop-objTop)/speed;
	
	if (objTop-tempTop>1 | objTop-tempTop<-1)
	{
		objTop += goStep;
		timeOut = setTimeout("pulldownMotion()",goSpeed);
	}
	else
	{
		objTop = tempTop; 
		clearTimeout(timeOut);
		clearTimeout(timeOut02);
	}
	document.getElementById('pulldown_'+pulldownNo).style.pixelTop = objTop;
}

function pulldownReset(all)
{
	for (i=1; i<=totalTopNum; i++)
	{
		if (subSectionArray[i-1] != null)
		{
			if (all == 1)
			{
				document.getElementById('pulldown_'+i).style.pixelTop = -pulldownHeightArray[i-1];
				document.getElementById('btnBar_'+i).style.visibility = 'hidden';
			}
			else
			{
				if (i != pulldownNo)
				{
					document.getElementById('pulldown_'+i).style.pixelTop = -pulldownHeightArray[i-1];
					document.getElementById('btnBar_'+i).style.visibility = 'hidden';
				}
			}
		}
	}
}

function getElementY(element)
{
	var targetTop = 0;
	if (element.offsetParent)
	{
		while (element.offsetParent)
		{
			targetTop += element.offsetTop;
            element = element.offsetParent;
		}
	}
	else if (element.y)
	{
		targetTop += element.y;
    }
	return targetTop;
}

function init()
{
    completeField = document.getElementById("complete-field");
    menu = document.getElementById("auto-row");
	autorow = document.getElementById("menu-popup");
	autorow.style.top = getElementY(menu) + "px";
	completeTable = document.getElementById("completeTable");
	completeTable.setAttribute("bordercolor", "white");
}


/*
 * Doris Poon replace the following code
 */
function initRequest(url) {
	request = null;
	try{
		request = new XMLHttpRequest();		// for non-Microsoft browser like safari and firefox
	}catch (trymicrosoft){
		try{
			request = new ActiveXObject("Msxml2.XMLHTTP");		// For IE (most version of IE)
		}catch (othermicrosoft){	
			request = new ActiveXObject("Microsoft.XMLHTTP");	// For IE							
		}
	}
	return request;

/* Doris Poon comment the following code and add the above code
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        isIE = true;
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
*/
}

function doSearch(form) {
	var retValue = true;
	var searchKeyword = form.keyword.value;

    if (searchKeyword == "") {
		retValue = false;
		form.keyword.focus();
	} else {
		//searchKeyword = searchKeyword.replace(/( and | \+ | \&\& )/gi, " AND ").replace(/( not |( -))/gi, " NOT ").replace(/( \|\| | or )/gi, " ");
// XSS prevention
		//searchKeyword = searchKeyword.replaceAll("<", "").replaceAll(">", "");
		//searchKeyword = searchKeyword.replaceAll("eval\\((.*)\\)", "");
		//searchKeyword = searchKeyword.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
		//searchKeyword = searchKeyword.replaceAll("script", "");

		form.keyword.value = searchKeyword;

		if (form.keyword.value.length <= 0) {
			retValue = false;
		}
	}

	return retValue;
}

function doCompletion()
{
	init();
	if (completeField.value == "")
	{
        clearTable();
    }
    else
    {
    	var url = encodeURI(path + "../../autocomplete?lang=chi&action=complete&keyword=" + completeField.value);
        var req = initRequest(url);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    parMsg(req.responseXML);
                } else {
                    clearTable();
                }
            }
        };
        req.open("GET", url, true);
        req.send(null);
    }
}

function parMsg(responseXML)
{
	clearTable();
	
	var searchdata = responseXML.getElementsByTagName("searchdata")[0];
	if (searchdata.childNodes.length > 0)
	{
        completeTable.setAttribute("bordercolor", "black");
        completeTable.setAttribute("border", "0");
        completeTable.setAttribute("table-layout", "fixed");   
		
    	for (loop = 0; loop < searchdata.childNodes.length; loop++)
    	{
    		var result = searchdata.childNodes[loop];
       		var keyword = result.getElementsByTagName("keyword")[0];
       		var searchid = result.getElementsByTagName("searchid")[0];
       		appendResult(keyword.childNodes[0].nodeValue, searchid.childNodes[0].nodeValue);
    	}

	    if (isInternetExplorer) {
		    autorow.style.setAttribute("border", "1px solid #CCCCCC;");
		}else{
			autorow.setAttribute("style", "display: block; white-space: nowrap;position: absolute;top:" + getElementY(menu) + "px; left:370px;z-index:1001; border:1px solid #CCCCCC;");
		}    	    	
    }    
}

function clearTable()
{
    if (completeTable)
    {
	    if (isInternetExplorer) {
			autorow.style.setAttribute("border", "0px solid  #CCCCCC;");
		} else {
			autorow.setAttribute("style", "position: absolute; top:" + getElementY(menu) + "px;left:370px;z-index:1001; border:0px solid #CCCCCC;");
		}	    
    	completeTable.setAttribute("bordercolor", "white");
      	completeTable.setAttribute("border", "0");
      	completeTable.style.visible = false;
      	for (loop = completeTable.childNodes.length -1; loop >= 0 ; loop--)
	    	completeTable.removeChild(completeTable.childNodes[loop]);
    }
}

function appendResult(keyword,searchid)
{
    var keywordCell;
    var row;
    var nameCell;
    if (isInternetExplorer)
    {
        row = completeTable.insertRow(completeTable.rows.length);
        nameCell = row.insertCell(0);
    }
    else
    {
        row = document.createElement("tr");
        nameCell = document.createElement("td");
        row.appendChild(nameCell);
        completeTable.appendChild(row);
    }
    
	nameCell.className = "contentkey";
    nameCell.setAttribute("class", "contentkey");
        
    var linkElement = document.createElement("a");
    linkElement.className = "contentkey";
    linkElement.setAttribute("href", encodeURI(path + "../../autocomplete?lang=chi&action=lookup&keyword=" + keyword));
    linkElement.appendChild(document.createTextNode(keyword));
    nameCell.appendChild(linkElement);
}



