// get cookie

	function openWindow(url,toolbar,location,directories,status,scrollbars,resizable,menubar,width,height,top,left){
		WindowName="POPUP";	
		settings= "toolbar="+ toolbar +",location="+location +",directories="+ directories +","+ "status="+status+",menubar="+menubar+",scrollbars="+scrollbars+","+"resizable="+resizable+",width="+width+",height="+height+",top="+top+",left="+left;
		
		MyNewWindow=window.open(url,WindowName,settings);	
	}


function getCookieVal( offset )
{
  var endstr = document.cookie.indexOf ( ";", offset );
  if ( endstr == -1 )
  {endstr = document.cookie.length;}
return unescape( document.cookie.substring( offset, endstr ) );
}

function getCookie( name )
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen)
  {
    var j = i + alen;

    if (document.cookie.substring( i, j ) == arg)
    {return getCookieVal ( j );}

	i = document.cookie.indexOf( " ", i ) + 1;
    if (i == 0){break;}
  }
return null;
}


// to center the pop up window to be displayed
function centerWindow(url,w,h,scr) {
  if ((document.all) || (document.getElementById))
        var xMax = screen.width, yMax = screen.height;
  else
  if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
  else
            var xMax = 640, yMax=480;
    var xOffset = (xMax - w)/2, yOffset = (yMax - h)/2;
  if (top.objWindow){
    	if (!top.objWindow.closed){
    		top.objWindow.focus();
    		top.objWindow.location=url;
    	}else{
top.objWindow=window.open(url,'MTR','width='+w+',height='+h+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',scrollbars='+scr);
    	}
    }else{
top.objWindow=window.open(url,'MTR','width='+w+',height='+h+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',scrollbars='+scr);
    }
top.objWindow.focus();
  }

// redirect pages
function redir(rval){
	document.location.href = rval;
	return false;
}

// pop up window
function popupShop (shopurl){
    var t_width = 800;
    var t_height = 500;
    var t_top = (screen.height/2)-(t_height/2);
    var t_left = (screen.width/2)-(t_width/2);
    msgWin=window.open (shopurl, "mtrshop", "menubar=no,resizable=yes,scrollbars=yes,width="+t_width+",height="+t_height+",top="+t_top+",left="+t_left);
    msgWin.focus ();
}

// pop up window
function popup(filetopop,scrollval)
  {
if (scrollval == "yes"){
  var myWin = window.open(filetopop, "Win","width=500,height=400,scrollbars=yes,resizable=yes,left=100,top=100");
  }
  if (scrollval == "no"){
  var myWin = window.open(filetopop, "Win","width=500,height=400,scrollbars=no,resizable=yes,left=100,top=100");
  }
  }

// pop up window with different sizes
function popupXY(filetopop,x,y,scrollval)
  {
  var myWin = window.open(filetopop, "Win","width="+x+",height="+y+",scrollbars="+scrollval+",resizable=yes,left=100,top=100");
  }

self.name = "main";


// function popPDF
function popPDF(theURL) { //v2.0
 if(navigator.appName == "Microsoft Internet Explorer") {
  screenY = document.body.offsetHeight;
  screenX = window.screen.availWidth;
 }
 else {
  screenY = window.outerHeight
  screenX = window.outerWidth
 }
 window.open(theURL,'pdf','scrollbars=yes,resizable=yes,width=screenX,height=screenY');
}

// to display the current section rollover image by default
function sectionOnload(i){
  if (typeof(i) != "undefined") {
	  id = i.split("_");
	  id = id[0];
	  img_obj = eval("window.rollover"+id);
	  if (typeof(img_obj) != "undefined") {
	    section = new Image();
	    section.src= eval("window.rollover"+id);
	    if (eval("document.menu"+id)) q16("menu"+id).src = section.src;
	  }
	}
};

// generate breadcrumbs
function tree(i){
	trees = i.split("_");

	result = "";
	if (eval("window.txthome")) {
		result += "<a href='"+eval("window.urlhome")+"' class='txtnav'>" + eval("window.txthome") + "</a> > ";
	}
	
	// added for new lvl for corp site
	if (eval("window.txtcorp")) {
		result += "<a href='"+eval("window.urlcorp")+"' class='txtnav'>" + eval("window.txtcorp") + "</a> > ";
	}
	
	for (x=0;x<trees.length;x++)
	{
		pagestr = "";
		for (j=0;j<=x;j++)
		{
			if (j>0) pagestr += "_";
			pagestr += trees[j];
		}


		if (x>0 && eval("window.txt"+pagestr)) result +=  " > ";
		if (eval("window.url"+pagestr) && x!=trees.length-1) {
		result += "<a href="+eval("window.url"+pagestr)+" class='txtnav'>";
		}

		if (eval("window.txt"+pagestr)) result += eval("window.txt"+pagestr);
		if (eval("window.url"+pagestr) && x!=trees.length-1) result += "</a>";
	}
	document.write("<font class='txtnav'>");
	document.write(result);
	document.write('</font>');
};

// generate sitemap content
function sitemap(i,j){
result = "";
section = 0;
while (eval("window.txt"+section))
{
if(i == "cust"){
	result += "<P class=subtitleBlue><A href='"+eval("window.url"+section)+"'><IMG height=17 src=/images/customer/eng/greybox_bullet.gif width=21 align=absMiddle border=0>"+eval("window.txt"+section)+"</A></P>";
}else{
	result += "<P class=subtitleBlue><A href='"+eval("window.url"+section)+"'><IMG height=17 src=/images/corporate/eng/morebullet.gif width=17 align=absMiddle border=0>"+eval("window.txt"+section)+"</A></P>";
}
	subsection = 0;
	result += '<UL>';

	while (eval("window.txt"+section+"_"+subsection))
	{
		txt = eval("window.txt"+section+"_"+subsection);
		url1 = eval("window.url"+section+"_"+subsection);
		
		url2 = url1 + "";
		
		if(j=="engtxt"){
			url2 = url2.replace("/eng/","/engtxt/"); 
		}
		if(j=="chitxt"){
			url2 = url2.replace("/chi/","/chitxt/"); 
		}

		
		if (txt != "" && txt != "&nbsp;")
		{

			result += '<LI>';

			if (url1) result += "<A class=content href="+url2+">";

			result += txt;

			if (url1) result += "</a>";

			result += '</LI>';
		}

		subsection++;

	}

	result += '</UL>';

	section++;

	result += "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
        result += "<tr>";
        result += "<td><hr size='1' noshade='noshade' /></td>";
        result += "</tr>";
      	result += "</table>";
}



	document.write(result);

};

function sitemap_old(){

result = "";
section = 0;
while (eval("window.txt"+section))
{

result += '<tr><td><font class="multititle">';
if (eval("window.url"+section)){result += '<a href='+eval("window.url"+section)+'>';}
result += eval("window.txt"+section);
if (eval("window.url"+section)){result += '</a>';}
result += '</font></td></tr>';

	subsection = 0;
	result += '<tr><td bgcolor="#FFFFFF">';

	while (eval("window.txt"+section+"_"+subsection))
	{
		txt = eval("window.txt"+section+"_"+subsection);
		url = eval("window.url"+section+"_"+subsection);
		if (txt != "" && txt != "&nbsp;")
		{

			result += '<div class=divpadding>&nbsp;<img src="/images/customer/common/arrow_red.gif" width=4 height=7 border=0>&nbsp;';

			if (url) result += "<a href="+url+">";

			result += txt;

			if (url) result += "</a>";

			result += '</div>';
		}

		subsection++;

	}

	result += '</td></tr>';

section++;
}

	document.write(result);

};

// generate station pull down menu
function genStations(){
	result = "";

	x = 0;
	while (eval("window.myValue"+x)!=null)
	{
		result += "<option value='";
		if (eval("window.myValue"+x))
			result += eval("window.myValue"+x)
		result += "' ";

		if (eval("window.style"+x)) {
			result += "style='";
			result += eval("window.style"+x);
			result += "'";
		}

		result += ">";
		if (eval("window.caption"+x))
			result += eval("window.caption"+x);
		result += "</option>";
		x++;
	}
	document.write(result);
}

// to initialize images for all roll over images
function handleOnload(){
	sectionOnload(pageid);

	if(q61)return;

	q31();
	if(!ie5only){
		i=0;
		while(eval("window.rollover"+i)){
			q20=q20.concat(new Array(new Image()));
			q20[i].src=eval("rollover"+i);
			q21=q21.concat(new Array(q16("menu"+i).src));
			i++;
		}
	}
	q61=true;

};


function q27(nid){
	if(eval("window.rollover"+nid)){
		if(ie5only)eval("q16(qmim"+nid+")."+q59);
		else q16("menu"+nid).src=q21[nid];
	}
};

function q30(menu){
	if((menu.lasthl!=null)&&(menu.q60==null))q4(menu);
};

function q31(){
	
	for(i=0;i<q18;i++){
		if(eval("window.txt"+i+"_0"))q1(i,true);
	}

	for(i=0;i<q19.length;i++) q1(q19[i],false);
};

// assign url links to the layer elements
function q32(index){
	if(!eval("window.url"+index))return;
	if(eval("window.urltarget"+index))where=eval("urltarget"+index);
	else where=urltarget;
	if((where.length<1)||(where.toLowerCase()=="_self")){
	window.location.href=eval("url"+index);
	}else {
		if(where.toLowerCase().indexOf("_parent")>-1)eval("parent.window.location=url"+index);
		else if(where.toLowerCase().indexOf("_new")<0)eval("parent."+where+".location=url"+index);
		else window.open(eval("url"+index));
	}
};

// get layer coordinates
function q33(coords){
	var q72=new Array(0,0);
	var txy=coords.indexOf(",");
	if(txy!=-1){
		q72[0]=parseInt(coords.substring(0,txy));
		q72[1]=parseInt(coords.substring(txy+1));
	}
	return q72;
};

// assign layer border style based on values from init.js
function getLevelVal(id,endv,rt){
	level=0;k=0;
	while((k=id.indexOf("_",k+1))>-1)level++;
	if(eval("window.border"+endv+level))
		return eval("border"+endv+level);
	else return rt;
};

// show pop up layer
function showMenu(e){handleOnload();if(loaded)return;}
// hide pop up layer
function hideMenu(e){if(loaded)return;};

// submit the current form
function submitForm(thisform) {
thisform.submit();
}

// switch language
function switchlang() {
 var loc = window.location + "";
 if (loc.indexOf("/eng/")>0)
    loc = loc.replace("/eng/","/chi/");
 else if (loc.indexOf("/chi/")>0)
    loc = loc.replace("/chi/","/eng/"); 
    

if ((loc.indexOf("/railway/station_")>0 || loc.indexOf("/railway/route_")>0 )  ){
	if(loc.indexOf("/chi/")>0)
	  loc = "/chi/railway/protection_boundary_map.html"; 
}
	    
	       
 window.location = loc;
}

function switchlangtxt() {
 var loc = window.location + "";
 if (loc.indexOf("/engtxt/")>0)
    loc = loc.replace("/engtxt/","/chitxt/");
 else if (loc.indexOf("/chitxt/")>0)
    loc = loc.replace("/chitxt/","/engtxt/"); 
      
	       
 window.location = loc;
}
// switch TextOnly or Graphical
function switchsite() {
 
//intro_index.html
//intro.html
//airport_express.html
//corp_index.html
/*
- Light Rail & Bus Services: intro_index.html (lr_bus)
- MTR Souvenir Corner: intro.html  (souvenir)
- Online Ticketing: airport_express.html  (oticketing)
- Useful Links:  corp_index.html  (useful_links)
*/
/*
- folder: (intercity), (chi & eng), --- HTML: index.html ; ---> text version: index.html (special case) "sorry, about it"
- folder: (club), (chi & eng), --- HTML:idnex.html --> there is no plain text version... i will inform you once MTR updated me the link 
*/


 var loc = window.location + "";

 if (loc.indexOf("/eng/")>0)
    loc = loc.replace("/eng/","/engtxt/");

 else if (loc.indexOf("/chi/")>0)
    loc = loc.replace("/chi/","/chitxt/"); 

if (loc.indexOf("/airport_express/intro_index.html")>0)
	    loc = loc.replace("/airport_express/intro_index.html","/airport_express/listing.html"); 


if (loc.indexOf("/souvenir/intro_index.html")>0)
	    loc = loc.replace("/souvenir/intro_index.html","/souvenir/listing.html"); 

if (loc.indexOf("/lr_bus/intro_index.html")>0)
	    loc = loc.replace("/lr_bus/intro_index.html","/lr_bus/listing.html"); 

if (loc.indexOf("/souvenir/intro.html")>0)
	    loc = loc.replace("/souvenir/intro.html","/souvenir/listing.html"); 


if (loc.indexOf("/oticketing/airport_express.html")>0)
	    loc = loc.replace("/oticketing/airport_express.html","/oticketing/listing.html"); 

//if (loc.indexOf("/useful_links/corp_index.html")>0)
//	    loc = loc.replace("/useful_links/corp_index.html","/useful_links/listing.html"); 

if (loc.indexOf("/index.html")>0 && loc.indexOf("/intercity")<1 && loc.indexOf("/whatsnew")<1 && loc.indexOf("/ael")<1 && loc.indexOf("/sustainability")<1 && loc.indexOf("/publications")<1 && loc.indexOf("/club")<1 && loc.indexOf("/tenders")<1 )
	    loc = loc.replace("/index.html","/listing.html"); 

if (loc.indexOf("/homepage/cust_index.html")>0)
	    loc = loc.replace("/cust_index.html","/cust_txt_index.html"); 

if (loc.indexOf("/homepage/corp_index.html")>0)
	    loc = loc.replace("/corp_index.html","/corp_txt_index.html"); 


if (loc.indexOf("/train/intro_index.html")>0)
	    loc = loc.replace("/intro_index.html","/listing.html"); 

if (loc.indexOf("facilities/bill.html")>0)
	    loc = loc.replace("/bill.html","/listing.html"); 


if (loc.indexOf("oticketing/intro.html")>0)
	    loc = loc.replace("/intro.html","/listing.html"); 


if (loc.indexOf("/railway/detail_index.html")>0)
	    loc = loc.replace("/detail_index.html","/listing.html"); 

if (loc.indexOf("/projects/index.html")>0)
	    loc = loc.replace("/index.html","/listing.html"); 
	    
//if (loc.indexOf("/club/")>0) {
//	    var clubcheck = new RegExp("(/club/)");
//      result = loc.split(clubcheck);
//      if (result) { loc = result[0] + "/club/listing.html"; }
//}

if (loc.indexOf("/ael/")>0 &&  loc.indexOf("/ael/index.html")<0 ) {
	    var clubcheck = new RegExp("(/ael/)");
      result = loc.split(clubcheck);
      if (result) { loc = result[0] + "/ael/listing.html"; }
}



 window.location = loc;


}

	function openWindow(url,toolbar,location,directories,status,scrollbars,resizable,menubar,width,height,top,left){
		WindowName="POPUP";	
		settings= "toolbar="+ toolbar +",location="+location +",directories="+ directories +","+ "status="+status+",menubar="+menubar+",scrollbars="+scrollbars+","+"resizable="+resizable+",width="+width+",height="+height+",top="+top+",left="+left;
		
		MyNewWindow=window.open(url,WindowName,settings);	
	}


// switch Graphical or TextOnly
function switchsite2() {

/*

- folder: lr_bus; text: listing.html --> HTML: intro_idnex.html (chi & eng)
- folder: souvenir; text: listing.html --> HTML: intro.html
- folder: oticketing; text: listing.html --> airport_express.html (* pls edit the page from HTML on folder: oticketing (chi &eng); HTML: airport_express.html --> text: listing.html)
- folder: useful_links; text: corp_index.html -->HTML: corp_index.html
*/



 var loc = window.location + "";
 if (loc.indexOf("/engtxt/")>0)
    loc = loc.replace("/engtxt/","/eng/");
 else if (loc.indexOf("/chitxt/")>0)
    loc = loc.replace("/chitxt/","/chi/"); 

if (loc.indexOf("/homepage/cust_txt_index.html")>0)
	    loc = loc.replace("/cust_txt_index.html","/cust_index.html"); 

if (loc.indexOf("/homepage/corp_txt_index.html")>0)
	    loc = loc.replace("/corp_txt_index.html","/corp_index.html"); 


if (loc.indexOf("/lr_bus/listing.html")>0)
	    loc = loc.replace("/lr_bus/listing.html","/lr_bus/intro_index.html"); 

if (loc.indexOf("/souvenir/listing.html")>0)
	    loc = loc.replace("/souvenir/listing.html","/souvenir/intro.html"); 

if (loc.indexOf("/airport_express/listing.html")>0)
	    loc = loc.replace("/listing.html","/intro_index.html"); 



if (loc.indexOf("/systemmap_text_index.html")>0)
	    loc = loc.replace("/systemmap_text_index.html","/system_map.html"); 

if (loc.indexOf("/routemap_text_index.html")>0)
	    loc = loc.replace("/routemap_text_index.html","/routemap_index.html"); 



//if (loc.indexOf("/oticketing/listing.html")>0)
//	    loc = loc.replace("/oticketing/listing.html","/oticketing/airport_express.html"); 

//if (loc.indexOf("/useful_links/listing.html")>0)
//	    loc = loc.replace("/useful_links/listing.html","/useful_links/corp_index.html"); 


if (loc.indexOf("/railway/listing.html")>0)
	    loc = loc.replace("/listing.html","/detail_index.html"); 

if (loc.indexOf("/projects/listing.html")>0)
	    loc = loc.replace("/listing.html","/index.html"); 


if (loc.indexOf("/train/listing.html")>0)
	    loc = loc.replace("/listing.html","/intro_index.html"); 


if (loc.indexOf("/facilities/listing.html")>0)
	    loc = loc.replace("/listing.html","/index.html"); 

if (loc.indexOf("/oticketing/listing.html")>0)
	    loc = loc.replace("/listing.html","/intro.html"); 

if (loc.indexOf("/listing.html")>0 && loc.indexOf("/overview")<0 )
	    loc = loc.replace("/listing.html","/index.html"); 


if (loc.indexOf("/overview/listing.html")>0)
	loc = loc.replace("/overview/listing.html","/overview/profile_index.html"); 

 window.location = loc;
}


// dreamweaver popup
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


// dreamweaver
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];}
}

// pull down menu (Railway Protection Boundary)
function getURL(path) {
	window.location = path;
}
