﻿/* update log */
/* 2007.02.22 : add MM_showHideLayer */
/* 2007.03.06 : change lang function */

/* part : mm.js */
/* usage : copy basic function of MM */

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) { //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_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];}
}

/* MM added 20070510 */
function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

/* Preload GovHK images */
MM_preloadImages(
'images/language_buttons/toplangtc_on.gif',
'images/language_buttons/toplangsc_on.gif',
'images/language_buttons/toplangen_on.gif',
'en/images/header/btn_searchh.gif',
'tc/images/header/btn_searchh.gif',
'sc/images/header/btn_searchh.gif'   );

/* 20070527 make global variable 'lang' and 'lang_prefix' */
var lang = 0; 
var lang_prefix = "";

/* 20070527 move out from function "langCurrentPage" */
/* 20070612 not only "homepage", check for other pages also */
/* sc - 2, tc - 1, en - 0 */
if (location.href.toString().search('/sc.info.gov.hk/') != -1) {
	lang = 2;
	lang_prefix = '/sc';
} else if (location.href.toString().search('/tc/') != -1) {
	lang = 1;
	lang_prefix = '/tc';
} else {
	lang = 0;
	lang_prefix = '/en';
} 

/*  remark old code at 20070612 - for homepage only
if (location.href.toString().search('sc.info.gov.hk/index.html') != -1) {
	lang = 2;
	lang_prefix = '/sc';
} else if (location.href.toString().search('tc/index.html') != -1) {
	lang = 1;
	lang_prefix = '/tc';
} else {
	lang = 0;
	lang_prefix = '/en';
} 
*/

/* 20070527 now dummy return function (for old use) */
function langCurrentPage() {
	return lang;
}

/* part : lang.js */
/* host with port */
var myhost = document.location.host;
/* host only */
var myhostname = document.location.hostname;

var sc_prefix = "sc.info.gov.hk/TuniS/";
var full_path_init = document.location.href;
var index = full_path_init.indexOf('#');
var full_path = index > 0 ? full_path_init.substring(0,index) : full_path_init;

var div_path,cv_sc_base_path,cv_nonsc_base_path;

if (full_path.indexOf(sc_prefix) < 0) {
  /* EN -> SC + TC (NON_SC) */
  /* TC -> SC + EN (NON_SC) */
  div_path = myhost + "/";
  cv_sc_base_path = "http:" + "//" + sc_prefix + myhostname;
  cv_nonsc_base_path = "http:" + "//" + myhost;

} else {
  /* for dynamic translated SC versions */
  var pathA = full_path.substring(full_path.indexOf(sc_prefix) + sc_prefix.length);
  div_path = pathA.substring(0,pathA.indexOf("/")+1);
  cv_sc_base_path = "http:" + "//" + sc_prefix + div_path.substring(0,div_path.length - 1);
  cv_nonsc_base_path = "http:" + "//" + div_path.substring(0,div_path.length - 1);
}


function changeVer(lang) {

	chi_path = "/tc/";
	eng_path = "/en/";

	/* reuse global variable full_path */
	org_path = full_path;
	org_path_start = org_path.indexOf(div_path) + div_path.length - 1;
	org_path = org_path.substring(org_path_start);

	/* 2006.10.03 : add for tc-homepage or en-homepage check */
	/* IF   current page is tc-homepage or en-homepage and want to do sc translate */
    	/* THEN go to disclaimer first                                */
	if ((org_path == '/en/residents/' || org_path == '/tc/residents/') && lang =='s' )
	{
		window.location.href = '/scdisclaimer.htm' ; 	
	}
	else 
	{
		if (org_path.indexOf(chi_path) != -1) {
			org_lang_path = chi_path;
		} else {
			org_lang_path = eng_path;
		}

		if (lang == 'e') {
			des_lang_path = eng_path;
		} else {
			des_lang_path = chi_path;
		}
	
		if (lang == 's') {
			base_path = cv_sc_base_path;
		} else {
			base_path = cv_nonsc_base_path;
		}	

		tail = org_path.replace(org_lang_path, des_lang_path);
	
		// alert(base_path + tail);

		window.location.href = base_path + tail;
	}
}


function displayEN() {
	document.writeln('<div class="topHolder"><a title="English" href="javascript:changeVer(\'e\');" onmouseout="MM_swapImgRestore()" onblur="MM_swapImgRestore()" onmouseover="MM_swapImage(\'btnEN\',\'\',\'/images/header/btn_enh.gif\',1)" onfocus="MM_swapImage(\'btnEN\',\'\',\'/images/header/btn_enh.gif\',1)"><img src="/images/header/btn_en.gif" alt="English" lang="en" name="btnEN" id="btnEN" width="40" height="19" /></a></div>');
}

function displayTC() {
	document.writeln('<div class="topHolder"><a title="繁體" href="javascript:changeVer(\'t\');" onmouseout="MM_swapImgRestore()" onblur="MM_swapImgRestore()" onmouseover="MM_swapImage(\'btnBIG5\',\'\',\'/images/header/btn_big5h.gif\',1)" onfocus="MM_swapImage(\'btnBIG5\',\'\',\'/images/header/btn_big5h.gif\',1)"><img src="/images/header/btn_big5.gif" alt="繁體" lang="zh" name="btnBIG5" id="btnBIG5" width="40" height="19" /></a></div>');
}

function displaySC() {
	document.writeln('<div class="topHolder"><a title="简体" href="javascript:changeVer(\'s\');" onmouseout="MM_swapImgRestore()" onblur="MM_swapImgRestore()" onmouseover="MM_swapImage(\'btnGB\',\'\',\'/images/header/btn_gbh.gif\',1)" onfocus="MM_swapImage(\'btnGB\',\'\',\'/images/header/btn_gbh.gif\',1)"><img src="/images/header/btn_gb.gif" alt="简体" lang="zh" name="btnGB" id="btnGB" width="40" height="19" /></a></div>');
}

/* 2007.05.17 move down */
function displayTwoLang() {
	// temp check for LL 20070723
	if (lang == 2) {
		displayEN();
		displayTC();
	} else if (lang == 1) {
		displayEN();
		displaySC();
	} else {
		displayTC();
		displaySC();
	}
}


/* for validate Search */
function isQueryValid(thisForm){
  with(thisForm){
    if (!query.value || clear==0){
      return 0;
    }
  }
  return 1;
}

function validateForm(){  

	var alertMsg= [
		['Please enter search keyword.', '請輸入查詢字串。', '请输入查询字串。'],
		['Please select a valid date.', '請選擇有效的日期。', '请选择有效的日期。']		
	];

  var flag = 1;
  var frontForm = document.basic_search;  
  
  /* avoid null query */
  if (!isQueryValid(frontForm)){ alert(alertMsg[0][lang]); flag = 0;}

  return flag;
}

function openwindow(url) {
	window.open(url,"_blank");
	return false;
}

// Modern browsers generates onclick event when pressing "Enter" key
// so this function is not required
// With onkeypress event, this verifies "Enter" key
function verifykey(oElement,oEvent) {
//	if (oEvent.keyCode==13 && oElement.onclick) {
//		oElement.onclick();
//	}
}

// for Search Keyword Box (2007.04.30)
var clear = 0;
function ClearText()
{
	if (clear == 0)
	{
		document.getElementById("topNavSearchInput").value = "";		
	}
	clear = 1;
}

// for survey use
function openwindowlink(url) {
	window.open(url,"_blank","width=800,height=600,copyhistory=no,scrollbars=yes,resizable=yes,screenX=0,screenY=0,left=0,top=0,hotkeys=no,status=yes");
	return false;
}

// for online demo
function openwindowdemo(url) {
	var w = 640 ;
	var h = 480 ;
	window.open(url,"_blank","width="+w+",height="+h+",copyhistory=no,scrollbars=no,resizable=no,screenX=0,screenY=0,left=0,top=0,hotkeys=no,status=yes");
	return false;
}

// for application
function openwindowapplication(url) {
	window.open(url, "_blank", "width="+screen.width+",height="+(screen.height-70) +",top=0,left=0,directories=no,location=no,menubar=no,scrollbars=yes,status=yes,toolbar=no,resizable=yes");
	return false;
}

// for application
function openwindow800x650(url) {
	window.open(url, '_blank', 'height=650px, top=0px, width=800px, toolbar=no, menubar=no,			scrollbars=yes, resizable=yes');
	return false;
}


/* 20070525: Functions for enlarge font size and 'linear' mode for accessibility */
/* remark: replace Font stylesheet can benefit firefox */
/*         if just change body font size, firefox will not have style to select */

/* 20070525: cookie functions */
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

/* 20070525: FIVE(5) global static variable */
var fontarray = new Array();
fontarray[0] = "Medium";
fontarray[1] = "Large";
fontarray[2] = "Extra";
var modeFormat = 'Format';
var modeLinear = 'Linear';
/* 20070720: the Link "ID" and cookie should keep above words */

/* 2 global variables from 2 cookie variables */
var myfonttitle = readCookie("govhkfonttitle");
if (myfonttitle==null) {myfonttitle=fontarray[0]; }

var myscreenmode = readCookie("govhkscreenmode");
if (myscreenmode==null) {myscreenmode=modeFormat; } 

// for clear old R2 cookie
if (myfonttitle!=fontarray[0] && myfonttitle!=fontarray[1] && myfonttitle!=fontarray[2] ) 
{ myfonttitle = fontarray[0]; }
if (myscreenmode!=modeFormat && myscreenmode!=modeLinear) { myscreenmode = modeFormat; }

/*
alert('myscreenmode: '+myscreenmode);
alert('myfonttitle: '+myfonttitle);
*/

// 20070716: use "linearFontSize" to handle the linear and 3A
function linearFontSize()
{
	
  var toggleMsg= [
    ['Linear Version', '簡化格式', '简化格式'],
    ['Normal Version', '標準格式', '标准格式']	
  ];
  
  // display - swap on or off images
  for (i=0;i<fontarray.length;i++)
  {
	if (myfonttitle==fontarray[i] ) {
	  // seperate to 2 part, avoid change 'gif' to 'htm' when grab the website
	  fonttail = '_on.' + 'gif';
	} else {
	  fonttail = '_off.' + 'gif';
	}

	// fontimg:         "MediumFontImg", "LargeFontImg", "ExtraFontImg"
	// fontpath:        "/images/fontsize/Medium_off.gif"

	fontimg = fontarray[i] + 'FontImg';	
	fontpath = '/images/fontsize/' + fontarray[i] + fonttail;
	MM_swapImage(fontimg,'',fontpath,1);
  }

  // consider "myscreenmode":
  if (myscreenmode==modeFormat) {
	
	// change A-title
	MM_changeProp('screenmodeLink','','title',toggleMsg[0][lang],'A');	
	
	// for change text: 
	MM_changeProp('screenmodeLink','','innerHTML',toggleMsg[0][lang],'A');	
	
  } else {
	// myscreenmode = modeLinear;
	 
	// change A-title	  
	MM_changeProp('screenmodeLink','','title',toggleMsg[1][lang],'A');	
	
	// for change text: 
	MM_changeProp('screenmodeLink','','innerHTML',toggleMsg[1][lang],'A');

  }

  // CAUTION: For Bug in IE7, no matter how, 
  //          assign "a.disabled=true" before change the value 
  // CAUTION: firefox2.04 canNOT use: a.getAttribute("title").indexOf("linear")
  //          use it will break the for-loop
  // but firefox can get: a.getAttribute("title")
  // WorkAround is var1=a.getAttribute("title") and do indexOf or compare


  // For all Link-tag, 20070720: change "link" to "style", 0723 change back
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
  {
	// for all "stylesheet" in Link
	if (a.getAttribute("rel").indexOf("stylesheet")!=-1) 
	{
	  // 20070726: especially for "extra" css
	  // if it is linear mode, set all css disable first
	  // if it is normal mode, set all css enable first 
	  if (myscreenmode==modeLinear) { a.disabled=true; } 
	  if (myscreenmode==modeFormat) { a.disabled=true; a.disabled=false; }	

	  // get the title text first
	  var id1 = new String(a.getAttribute("id"));

      // for FontSize and Format relate stylesheet, ( e.g: 'modeFormatMedium' )
	  if (id1.indexOf("mode")!=-1)  {
		a.disabled=true;     // for bug in IE7, no matter how, set to true first
		
		if (id1.indexOf(myfonttitle)!=-1 && id1.indexOf(myscreenmode)!=-1) {
			// for format version and user selected font size, enable it
	        a.disabled=false; 
		}
	  }
	  
	  // 20070719: disable "ie.css" when it is linear version
	  if (id1.indexOf("iecss")!=-1) {
	    a.disabled=true;     // for bug in IE7, no matter how, set to true first
		// enable ie.css when it is "Format" Version
		if (myscreenmode==modeFormat) {
		  a.disabled=false;
		}
	  }
	  
	}
  
  }

}

// 20070716: changeFontTitle just handle global variable change, then pass to "linearFontSize"
function changeFontTitle(ft) {

	// change global variable "myfonttitle"
	myfonttitle = ft;

	// just call linearFontSize and let it handle
	linearFontSize();
}

// 20070716: toggleLinearStyleSheet just handle global variable change, then pass to "linearFontSize"
function toggleLinearStyleSheet(linearflag) {

  // change 'toggle' to modeFormat or modeLinear
  if (linearflag=='toggle') 
  { 
	// change global variable 'myscreenmode'
    if (myscreenmode==modeFormat) {
	  myscreenmode = modeLinear;
	} else {
	  myscreenmode = modeFormat;
	}
  } else {
	  // what it pass from parameter
	  myscreenmode = linearflag;
  } 
  
  // just call linearFontSize and let it handle
  linearFontSize();
}


// 20070716: change to use "linearFontSize"
// (try avoid "flash" before "onload"), do Font Enlarge and select screenmode before page onload, 
//changeFontTitle(myfonttitle);
//toggleLinearStyleSheet(myscreenmode);
linearFontSize();

/* part  : onload.js 
   usage : put all function that need "body onload" in javascript.
      1) : FontEnlarge (20070525)
      2) : Linear or Normal Mode (20070525)
      3) : a ref="external" 
      4) : leftmenu handling (obsolete)

  CAUTION: one html page canNOT have 2 onload functions (will count last one only).
  WorkAround: use try-catch block below
	  
*/

startList = function() {
  
  /* --- prevent loading GovHK in frames */
  breakOutOfFrame();

  /* --- For FontSize - start --- */
  // 20070716: change to use "linearFontSize"  
  //changeFontTitle(myfonttitle);
  //toggleLinearStyleSheet(myscreenmode);
  linearFontSize();
  //fontinit=1;	// 20070713: set user can action on FontSize
  /* --- For FontSize - end --- */

  /* --- For Expand (online service and sitemap) : start --- */
  try { toggleAllExpand('collapse'); } catch (e) {}
  /* --- For Expand : end --- */

  /* --- For New Window - start --- */
  if (document.getElementsByTagName) {
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") )
      {
        anchor.onkeypress=function() {
          verifykey(this,event);
        }
	// check for different "rel"
	if ( anchor.getAttribute("rel") == "external" )	{
	  anchor.onclick=function() {
	    return openwindow(this.href);
	  }
	}

	if ( anchor.getAttribute("rel") == "application" ) {
	  anchor.onclick=function() {
	    return openwindowapplication(this.href);
	  }
	}

	if ( anchor.getAttribute("rel") == "window800x650" ) {
	  anchor.onclick=function() {
	    return openwindow800x650(this.href);
	  }
	}

	if ( anchor.getAttribute("rel") == "onlinedemo" ) {
	  anchor.onclick=function() {
	    return openwindowdemo(this.href);
	  }
	}

      }
    } 
  }
  /* --- For New Window - end --- */

  /* --- For Leftmenu - start (20070510: obsolete in R2)--- */
  /*  
  if (document.getElementById) {
    if (document.getElementById("nav")) {
      navRoot = document.getElementById("nav");
      for (i=0; i<navRoot.childNodes.length; i++) {
        node = navRoot.childNodes[i];
        if (node.nodeName=="LI" || node.nodeName=="li") {
          node.onmouseover=function() {
            this.className="over";
          }
          node.onmouseout=function() {
            this.className="";
          }
          node.onfocus=function() {
            this.className="over";
          }
          node.onblur=function() {
            this.className="";
          }
          ++ belows are for IE only ++
          node.onactivate=function() {
            this.className="over";
          }
          node.ondeactivate=function() {
            this.className="";
          }
        }
      }
    }
  }
  */
  /* --- For Leftmenu - end --- */


}

window.onload=startList;

/* 20070525: function when page unload */
window.onunload = function(e) {
  /* assign fontsize and screenmode to cookie, cookie set to 3 months */
  createCookie("govhkfonttitle", myfonttitle, 90);
  createCookie("govhkscreenmode", myscreenmode, 90);
}

/* 20070605: prevent loading of GovHK content in frames */
function breakOutOfFrame()
{
  if (self != top)
  {
    if (document.images)
      top.location.replace(window.location.href);
    else
      top.location.href = window.location.href;
  }
}
