var section = '';
var content = '';
idx = 3;
cgistr = '/cgi-bin/citb_new/';	
basestr = '/ctb/';	

org_path=window.location.pathname;
tmpstr=org_path.substring(0, cgistr.length);
if (tmpstr == cgistr) {
	section = 'cgi-bin';
	content = org_path.substring(cgistr.length, org_path.length)
} else {
	path_end=org_path.length;
	path_start=org_path.indexOf(basestr);
	org_path=org_path.substring(path_start ,path_end);
	
	path_end=org_path.length;	
	content = org_path.substring(basestr.length + 3,path_end);

	for (var j=1; j<=idx; j++) {
		path_end=org_path.length;
		path_start=org_path.indexOf("/");
		org_path=org_path.substring(path_start + 1 ,path_end);
	}
	
	path_start=org_path.indexOf("/");
	section=org_path.substring(0, path_start);	
}

function changeVer(lang) {

		if(lang=='simp'){
		path = '/ctb/chs' + content;
		ajaxchkfileext(path);
		} else {
		path = '/ctb/chi' + content;
		ajaxchkfileext(path);
		}
}

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_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,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  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); return x;
}

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];}
}

cookiename = 'ctb';  
cookieval= '123';  

function GetCookie(cookieName) {
thisCookie = document.cookie.split("; ")
for (i = 0; i < thisCookie.length; i++) {
	if (cookieName == thisCookie[i].split("=")[0]) {
		return thisCookie[i].split("=")[1];
	}
}
return "";
}

if (cookieval == GetCookie(cookiename)) {
document.write ("Communications and Technology Branch Commerce and Economic Development Bureau<br><br><font size=\"-1\"><a href=\"/ctb/eng/text.htm\">Back to menu</a></font>"); 
} else {
document.write("<table width=\"772\" border=\"0\" height=\"87\" cellpadding=\"0\" cellspacing=\"0\" background=\"/ctb/eng/images/bkgdtexture.gif\"><form name=\"form1\" method=\"get\" action=\"http://search.gov.hk/search.html\" target=_blank><input name=\"tpl_id\" type=\"hidden\" value=\"stdsearch\"><input name=\"gp0\" type=\"hidden\" value=\"cedb-ctb_home\"><input name=\"gp1\" type=\"hidden\" value=\"cedb-ctb_home\"><input name=\"web\" type=\"hidden\" value=\"this\"><input name=\"ui_charset\" type=\"hidden\" value=\"iso-8859-1\"><input name=\"ui_lang\" type=\"hidden\" value=\"en\">");
document.write("<tr align=\"left\" valign=\"top\">");
document.write("<td height=\"66\" colspan=\"5\" class=\"sidebar\" background=\"/ctb/eng/images/bkgdtexture.gif\"><a href=\"/ctb/index.htm\"><img src=\"/ctb/eng/images/branding.gif\" height=\"65\" border=0 alt=\"Communications and Technology Branch\nCommerce and Economic Development Bureau\"></a></td>");
document.write("<td height=\"66\" background=\"/ctb/eng/images/bkgdtexture.gif\" colspan=\"4\" align=right><a href=\"http://www.brandhk.gov.hk\" target=\"_blank\"><img name=\"BrandHK\" src=\"/ctb/eng/images/BrandHK.gif\" width=\"190\" height=\"65\" border=\"0\" alt=\"Brand Hong Kong - Asia's World City\"></a></td></tr><tr align=\"left\" valign=\"top\">");
document.write("<td height=\"19\" width=\"236\" background=\"/ctb/eng/images/bkgdtexture.gif\" valign=bottom><a href=\"http://www.gov.hk/en/residents/\" target=\"_blank\" onMouseOut=\"MM_swapImgRestore()\"  onMouseOver=\"MM_swapImage('mainbar1','','/ctb/eng/images/mainbar1_f2.gif',1);\"><img name=\"mainbar1\" src=\"/ctb/eng/images/mainbar1.gif\" width=\"171\" height=\"20\" border=\"0\" alt=\"GovHK\"></a></td>");
//document.write("<td align=\"left\" valign=\"top\" height=\"19\" width=\"67\" background=\"/ctb/eng/images/bkgdtexture.gif\"><a href=\"../text.htm\" onMouseOut=\"MM_swapImgRestore()\"  onMouseOver=\"MM_swapImage('mainbar2','','/ctb/eng/images/mainbar2_f2.gif',1);\"><img name=\"mainbar2\" src=\"/ctb/eng/images/mainbar2.gif\" width=\"67\" height=\"20\" border=\"0\" alt=\"TEXT ONLY\"></a></td>");
document.write("<td align=\"left\" valign=\"bottom\" height=\"19\" width=\"51\" background=\"/ctb/eng/images/bkgdtexture.gif\"><a href=\"javascript:changeVer('chi');\" onMouseOut=\"MM_swapImgRestore()\"  onMouseOver=\"MM_swapImage('mainbar3','','/ctb/eng/images/mainbar3_f2.gif',1);\"><img name=\"mainbar3\" src=\"/ctb/eng/images/mainbar3.gif\" width=\"51\" height=\"20\" border=\"0\" alt=\"TRADITIONAL CHINESE\"></a></td>");
document.write("<td align=\"left\" valign=\"bottom\" height=\"19\" width=\"50\" background=\"/ctb/eng/images/bkgdtexture.gif\"><a href=\"javascript:changeVer('simp');\" onMouseOut=\"MM_swapImgRestore()\"  onMouseOver=\"MM_swapImage('mainbar4','','/ctb/eng/images/mainbar4_f2.gif',1);\"><img name=\"mainbar4\" src=\"/ctb/eng/images/mainbar4.gif\" width=\"50\" height=\"20\" border=\"0\" alt=\"SIMPLIFIED CHINESE\"></a></td>");
document.write("<td height=\"19\" background=\"/ctb/eng/images/bkgdtexture.gif\" valign=bottom><img name=\"mainbar5\" src=\"/ctb/eng/images/mainbar5.gif\" height=\"20\" border=\"0\"><img name=\"mainbar5a\" src=\"/ctb/eng/images/mainbar5a.gif\" width=\"113\" height=\"20\" border=\"0\"></td>");
document.write("<td height=\"19\" width=\"54\" background=\"/ctb/eng/images/bkgdtexture.gif\" valign=bottom><a href=\"/ctb/eng/search/index.htm\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('mainbar6','','/ctb/eng/images/mainbar6_f2.gif',1);\"><img name=\"mainbar6\" src=\"/ctb/eng/images/mainbar6.gif\" width=\"54\" height=\"20\" border=\"0\" alt=\"SEARCH\"></a></td>");
document.write("<td valign=\"bottom\" background=\"/ctb/eng/images/bkgdtexture.gif\" width=\"99\">");
IE = (document.all) ? 1 : 0;
	if (IE) { document.write("<input class=\"/ctb/eng/search/index.htm\" type=\"text\" name=\"qu\" size=\"17\">");
	} else {
	document.write("<input class=\"/ctb/eng/search/index.htm\" type=\"text\" name=\"qu\" size=\"17\">");
	}
document.write("</td>");	
document.write("<td height=\"19\" width=\"22\" background=\"/ctb/eng/images/bkgdtexture.gif\" valign=bottom><input type=\"Image\" src=\"/ctb/eng/images/mainbar8.gif\" value=\"ctb/eng/search/index.htm\" alt=\"search\" border=0></td>");
document.write("<td height=\"19\" width=\"50\" background=\"/ctb/eng/images/bkgdtexture.gif\" valign=bottom><a href=\"/ctb/eng/sitemap/index.htm\" onMouseOut=\"MM_swapImgRestore()\"  onMouseOver=\"MM_swapImage('mainbar9','','/ctb/eng/images/mainbar9_f2.gif',1);\"><img name=\"mainbar9\" src=\"/ctb/eng/images/mainbar9.gif\" width=\"65\" height=\"20\" border=\"0\" alt=\"SITE MAP\"></a></td>");
document.write("<td height=\"19\" width=\"36\" background=\"/ctb/eng/images/bkgdtexture.gif\" valign=bottom><a href=\"/ctb/eng/contact/index.htm\" onMouseOut=\"MM_swapImgRestore()\"  onMouseOver=\"MM_swapImage('mainbar10','','/ctb/eng/images/mainbar10_f2.gif',1);\"><img name=\"mainbar10\" src=\"/ctb/eng/images/mainbar10.gif\" width=\"36\" height=\"20\" border=\"0\" alt=\"CONTACT US\"></a></td>");
document.write("</tr><tr align=\"left\" valign=\"top\" bgcolor=\"#CC3333\"><td height=\"2\" colspan=\"10\"><img src=\"http://www.citb.gov.hk/ctb/eng/images/reddot.gif\" width=\"760\" height=\"2\"></td></tr></form></table>");
}

function ajaxchkfileext(url)
	{ 
	xhr=createXHR();
	//xhr.open("HEAD", url, true);
	xhr.open("GET", url, true);
	xhr.onreadystatechange=function() 
	{
		
		if(xhr.readyState == 4) 
		{
			var value;
			if (xhr.status == 200)
			{ 
				 //value = url + " exists...";
				 window.location = url;
			}
			else 
			{
				
				//tmp = tmp.length;
				//alert(tmp);
				//alert(tmp2+window.location.pathname.substring(0,window.location.pathname.lastIndexOf('/')));
				if(xhr.status==404) 
					//value = url + " doesn't exist!";
					window.location = url.substring(0,url.lastIndexOf('/'));
    	 			else 
					//value = "Error, status is " + xhr.status;
					window.location = url.substring(0,url.lastIndexOf('/'));
			}
					

		}
	}

	xhr.send(null); 
	} 

function createXHR() 
{
    var request = false;
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
		try {
			request = new XMLHttpRequest();
		}
		catch (err1) 
		{
			request = false;
		}
            }
        }
    return request;
}
