var hkgem_url = getDomain(replaceCharacters(''+window.parent.location,'/sc.hkex.com.hk/gb/','/'));
hkgem_url = replaceCharacters(''+hkgem_url,'/202.84.254.74/gb/','/');
hkgem_url = replaceCharacters(''+hkgem_url,'/sc.hkex.com.hk/TuniS/','/');
hkgem_url = replaceCharacters(''+hkgem_url,'/202.84.254.74/TuniS/','/');
var HTTP_HEADER = getHTTPHeader();

function getHTTPHeader() {
 dt = '' + window.parent.location;
 dt = dt.toLowerCase();
 if (dt.substring(0,5) == 'https') {
	return 'https';
 } else {
	return 'http';
 }
}

function changeframe(ver)
{
	var hkgem_url = "www.hkgem.com"
	var sc = false
	if (ver == "sc") { 
		sc = true;
		ver = "tc";
		openDisclaimer();
	}

	var eps_hist_date = "20021019";
	var eps_path      = "/listedco/listconews/gem/";
	var initstring = ver + "_";
	var hkexnews_index = new Array();
	var hkexnews_path  = new Array();
	var hkexSearch_index = new Array();
	
	hkexSearch_index[0] = "/listedco/listconews/search/search_active_gem.asp"
	hkexSearch_index[1] = "/listedco/listconews/search/search_active_gem_c.asp"	
	
	
	hkexnews_index[0] = "/prices/todaynews/news.htm"
	hkexnews_index[1] = "/prices/todaynews/news_c.htm"
	hkexnews_index[2] = "/prices/predaynews/news.htm"
	hkexnews_index[3] = "/prices/predaynews/news_c.htm"
	hkexnews_index[4] = "/listingrules/gemrule/gemrule.htm"
	hkexnews_index[5] = "/listingrules/gemrule/gemrule_c.htm"
	hkexnews_index[6] = "/issuer/listhk/booklet.htm"
	hkexnews_index[7] = "/issuer/listhk/booklet_c.htm"
		
	hkexnews_path[0] = "/news/hkexnews/"
	hkexnews_path[1] = "/prices/todaynews/"
	hkexnews_path[2] = "/prices/predaynews/"
	hkexnews_path[3] = "/listingrules/gemrule/"
	hkexnews_path[4] = "/rulesreg/listrules/gemrules/" 	//2009 Website Revamp new path
	hkexnews_path[5] = "/newsconsul/hkexnews/" 		//2009 Website Revamp new path
	hkexnews_path[6] = "/market/sec_tradinfo/" 		//2009 Website Revamp new path


	var topfullpath = window.parent.frames[0].location.href;
	var menufullpath = window.parent.frames[1].location.href;
	var confullpath = window.parent.frames[2].location + "";

	//alert(" Top URL = " + topfullpath + "\n Menu URL = " + topfullpath + "\n Content URL = " + confullpath);

	if (confullpath == null 
			|| (confullpath.toLowerCase().indexOf(eps_path) > 0 && confullpath.toLowerCase().indexOf('.pdf') == confullpath.length - 4) 
			|| (confullpath.toLowerCase().indexOf(eps_path) > 0 && confullpath.toLowerCase().indexOf('.doc') == confullpath.length - 4)
	) {
		if (topfullpath.toUpperCase().indexOf("E_") < 0)
			alert ("WARNING: Cannot change to other version in this page.. \n\n\tPress \"BACK\" button and try again!");
		else
			alert ("WARNING: Cannot change to other version in this page.. \n\n\tPress \"BACK\" button and try again!");
		return;
	} else if (confullpath.length > 10 && (confullpath.indexOf("_frame.htm") > 0 || confullpath.indexOf("_frame.asp") > 0)) {
		var subcontpath = top.frames[2].frames[1].location + "";

		if (   subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2002news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2002news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2003news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2003news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2004news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2004news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2005news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2005news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2006news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2006news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2007news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2007news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2008news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2008news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2009news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2009news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2010news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2010news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2011news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2011news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2012news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2012news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2013news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2013news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2014news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2014news_c.htm") == -1
			&& subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2015news.htm") == -1 && subcontpath.toLowerCase().indexOf("/newsconsul/hkexnews/2015news_c.htm") == -1
		) {
			subcontpath = null;
		}

		if (subcontpath == null) {
			if (topfullpath.toUpperCase().indexOf("E_") < 0)
				alert ("WARNING: Cannot change to other version in this page.. \n\n\tPress \"BACK\" button and try again!");
			else
				alert ("WARNING: Cannot change to other version in this page.. \n\n\tPress \"BACK\" button and try again!");
			return;
		}
	} else if (chkPathMatched (hkexSearch_index, confullpath))
	{
		var menuname = getfilename(menufullpath);
		var conname  = confullpath.substring(confullpath.lastIndexOf("/") + 1);
		var menupath = getpath(menufullpath);
		var conpath  = getpath(confullpath);
		var toppath  = getpath(topfullpath);
		var condate  = parseInt(conpath.substr(confullpath.toLowerCase().indexOf(eps_path) + eps_path.length, 8));

		var f0 = toppath + initstring + "top.asp";
		var f1 = menupath + initstring + menuname;
		if (ver.toLowerCase() == 'tc' && conname.toLowerCase().indexOf("_c.") == -1) {
			var f2 = conpath + conname.substring(0, conname.lastIndexOf(".")) + "_c" + conname.substring(conname.lastIndexOf("."));
			f2 = replaceCharacters(f2,'/eng/','/chi/'); 
			}
		else if (ver.toLowerCase() == 'tc')
			var f2 = conpath + conname;
		else {
			var f2 = conpath + conname.substring(0, conname.toLowerCase().lastIndexOf("_c.")) + conname.substring(conname.lastIndexOf("."));
			f2=replaceCharacters(f2,'/chi/','/eng/'); 
			}
		top.location.href = "/gem.asp?f0="+escape(f0)+"&f1="+escape(f1)+"&f2="+escape(f2);
		return;

	}

	else if (confullpath.toLowerCase().indexOf(eps_path) > 0 || chkPathMatched(hkexnews_index, confullpath) 
		|| confullpath.toLowerCase().indexOf("/rule/gemrule/") > 0	|| 	confullpath.toLowerCase().indexOf("/tradinfo/") > 0	
		|| confullpath.toLowerCase().indexOf("/news/hkexnews/") > 0     || 	confullpath.toLowerCase().indexOf("/listing/") > 0	
		|| confullpath.toLowerCase().indexOf("/rulesreg/listrules/gemrules/") > 0	
		|| confullpath.toLowerCase().indexOf("/newsconsul/hkexnews/") > 0	
		|| confullpath.toLowerCase().indexOf("/market/sec_tradinfo/") > 0	) {	
		
		var menuname = getfilename(menufullpath);
		var conname  = confullpath.substring(confullpath.lastIndexOf("/") + 1);
		var menupath = getpath(menufullpath);
		var conpath  = getpath(confullpath);
		var toppath  = getpath(topfullpath);
		var condate  = parseInt(conpath.substr(confullpath.toLowerCase().indexOf(eps_path) + eps_path.length, 8));

		if (!isNaN(condate) && condate <= parseInt(eps_hist_date)) {
			if (topfullpath.toUpperCase().indexOf("E_") < 0)
				alert ("WARNING: Cannot change to other version in this page.. \n\n\tPress \"BACK\" button and try again!");
			else
				alert ("WARNING: Cannot change to other version in this page.. \n\n\tPress \"BACK\" button and try again!");
			return;
		}		

		var f0 = toppath + initstring + "top.asp";
		var f1 = menupath + initstring + menuname;
		var f2 = '';
		if (ver.toLowerCase() == 'tc' && conname.toLowerCase().indexOf("_c.") == -1) {
			var f2 = conpath + conname.substring(0, conname.lastIndexOf(".")) + "_c" + conname.substring(conname.lastIndexOf("."));
			f2=replaceCharacters(f2,'/eng/','/chi/');
			if (sc==true) f2=replaceCharacters(f2,'://','://sc.hkex.com.hk/gb/');
		} else if (ver.toLowerCase() == 'tc') {
			var f2 = conpath + conname;
			if (sc==true) f2=replaceCharacters(f2,'://','://sc.hkex.com.hk/gb/');
		} else {
			var f2 = conpath + conname.substring(0, conname.toLowerCase().lastIndexOf("_c.")) + conname.substring(conname.lastIndexOf("."));
			f2=replaceCharacters(f2,'/chi/','/eng/');
			if (sc==true) f2=replaceCharacters(f2,'://','://sc.hkex.com.hk/gb/');
		}
		f0=replaceCharacters(f0,'https://','http://'); //remove https
		f1=replaceCharacters(f1,'https://','http://'); //remove https
		f2=replaceCharacters(f2,'https://','http://'); //remove https

		var dest 
		dest = ''
		if (sc==true) {
			dest = "http://sc.hkex.com.hk/gb/" +  hkgem_url + "/gem.asp?f0="+escape(f0)+"&f1="+escape(f1)+"&f2="+escape(f2);
		}
		else {
			dest = "http://" + hkgem_url + "/gem.asp?f0="+escape(f0)+"&f1="+escape(f1)+"&f2="+escape(f2);
			dest=replaceCharacters(dest,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
			dest=replaceCharacters(dest,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
			dest=replaceCharacters(dest,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
			dest=replaceCharacters(dest,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi
		}

		if (getHTTPHeader() == "https") {
			dest=replaceCharacters(dest,'http','https'); //replace http to https
		}

		window.parent.location = dest;
		return;
	} else if ((confullpath.toUpperCase().indexOf("TC_") < 0 && confullpath.toUpperCase().indexOf("E_") < 0) 
				|| chkPathMatched(hkexnews_path, confullpath)
	) {
		if (ver.toUpperCase().indexOf("TC") < 0)
			alert ("WARNING: Cannot change to other version in this page.. \n\n\tPress \"BACK\" button and try again!");
		else
			alert ("WARNING: Cannot change to other version in this page.. \n\n\tPress \"BACK\" button and try again!");
		return ;
	}

	var menuname = getfilename(menufullpath);
	var conname = getfilename(confullpath);
	var menupath = getpath(menufullpath);
	var conpath = getpath(confullpath);
	var toppath = getpath(topfullpath);

	var f0 = toppath + initstring + "top.asp";
	var f1 = menupath + initstring + menuname;
	var f2 = conpath + initstring + conname;

	f0=replaceCharacters(f0,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
	f1=replaceCharacters(f1,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
	f2=replaceCharacters(f2,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
	f0=replaceCharacters(f0,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
	f1=replaceCharacters(f1,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
	f2=replaceCharacters(f2,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
	f0=replaceCharacters(f0,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
	f1=replaceCharacters(f1,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
	f2=replaceCharacters(f2,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
	f0=replaceCharacters(f0,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi
	f1=replaceCharacters(f1,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi
	f2=replaceCharacters(f2,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi
	f0=replaceCharacters(f0,'https://','http://'); //remove https
	f1=replaceCharacters(f1,'https://','http://'); //remove https
	f2=replaceCharacters(f2,'https://','http://'); //remove https


	var dest 
	dest = ''
	if (sc==true) {
//		if (f2.indexOf('/sc.hkex.com.hk/') != -1) {
//			f2=replaceCharacters(f2,'http://','http://sc.hkex.com.hk/gb/');
//		}
		dest = "http://sc.hkex.com.hk/gb/" +  hkgem_url + "/gem.asp?f0="+escape(f0)+"&f1="+escape(f1)+"&f2="+escape(f2);
		dest=replaceCharacters(dest,'202.84.254.11','www.hkgem.com'); 	/*** 20050901 ***/
		dest=replaceCharacters(dest,'203.78.5.11','www.hkgem.com'); 	/*** 20050901 ***/
	}
	else {
		dest = "http://" + hkgem_url + "/gem.asp?f0="+escape(f0)+"&f1="+escape(f1)+"&f2="+escape(f2);
		dest=replaceCharacters(dest,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
		dest=replaceCharacters(dest,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
		dest=replaceCharacters(dest,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
		dest=replaceCharacters(dest,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi
		dest=replaceCharacters(dest,'202.84.254.11','www.hkgem.com'); 	/*** 20050901 ***/
		dest=replaceCharacters(dest,'203.78.5.11','www.hkgem.com'); 	/*** 20050901 ***/
	}

	if (getHTTPHeader() == "https") {
		dest=replaceCharacters(dest,'http://','https://'); //replace http to https
	}

	window.parent.location = dest;
}


function replaceCharacters(conversionString,inChar,outChar){
  var convertedString = conversionString.split(inChar);
  convertedString = convertedString.join(outChar);
  return convertedString;
}

function chkPathMatched(pathlist, currpath) {
	var i = 0;
	var bMatched = false;

	for (i = 0; i < pathlist.length; i++) {
		if (currpath.toLowerCase().indexOf(pathlist[i]) > 0)
			return true;
	}
	
	return false;
}


function getfilename(fullpath) {
 var start = fullpath.lastIndexOf("/")+1;
 var end = fullpath.length ;
 var verfilename = fullpath.substring(start,end);
 var actualname = verfilename.substring(verfilename.indexOf("_")+1,verfilename.length);
 return (actualname);
}



function getpath(fullpath) {
 var actualpath = fullpath.substring(0, fullpath.lastIndexOf("/")+1);
 return (actualpath);
}


function route(from, to) { 
  dest = '' + window.parent.location;
  dest = dest.toLowerCase();
  dest=replaceCharacters(dest,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
  dest=replaceCharacters(dest,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
  dest=replaceCharacters(dest,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
  dest=replaceCharacters(dest,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi
  if (from=='eng') {
    if (to=='chi' ){ 
       dest=replaceCharacters(dest,'/e_','/tc_');
    }
    if (to=='sim' ){ 
       dest=replaceCharacters(dest,'://202.84.254.11','://www.hkgem.com');	/*** 20050901 ***/
       dest=replaceCharacters(dest,'://203.78.5.11','://www.hkgem.com');	/*** 20050901 ***/
       dest=replaceCharacters(dest,'/e_','/tc_');
       dest=replaceCharacters(dest,'://','://sc.hkex.com.hk/gb/');
   	   openDisclaimer();
    }
  }

  if (from=='chi') {
    if (to=='eng' ){ 
       dest=replaceCharacters(dest,'/tc_','/e_');
    }
    if (to=='sim' ){ 
       dest=replaceCharacters(dest,'://202.84.254.11','://www.hkgem.com');	/*** 20050901 ***/
       dest=replaceCharacters(dest,'://203.78.5.11','://www.hkgem.com');	/*** 20050901 ***/
       dest=replaceCharacters(dest,'://','://sc.hkex.com.hk/gb/');
	   openDisclaimer();
	}
  }

  if (from=='sim') {
    if (to=='eng' ){ 
       dest=replaceCharacters(dest,'/tc_','/e_');
    }
    if (to=='chi' ){ 
       dest=dest
    }
  }

  window.parent.location=dest;
}



function route_reg(from, to) { 
   var hkgem_url = getDomain(replaceCharacters(''+window.parent.frames[0].location,'/sc.hkex.com.hk/gb/','/'));
   hkgem_url = replaceCharacters(''+hkgem_url,'/202.84.254.74/gb/','/');
   hkgem_url = replaceCharacters(''+hkgem_url,'/sc.hkex.com.hk/TuniS/','/');
   hkgem_url = replaceCharacters(''+hkgem_url,'/202.84.254.74/TuniS/','/');
   var f0 = '' + window.parent.frames[0].location;
   var f1 = '' + window.parent.frames[1].location;
   var f2 = '' + window.parent.frames[2].location;

   f0=replaceCharacters(f0,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
   f1=replaceCharacters(f1,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
   f2=replaceCharacters(f2,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
   f0=replaceCharacters(f0,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
   f1=replaceCharacters(f1,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
   f2=replaceCharacters(f2,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
   f0=replaceCharacters(f0,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
   f1=replaceCharacters(f1,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
   f2=replaceCharacters(f2,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
   f0=replaceCharacters(f0,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi
   f1=replaceCharacters(f1,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi
   f2=replaceCharacters(f2,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi

   dest = "https://sc.hkex.com.hk/gb/" +  hkgem_url + "/gem.asp?f0="+escape(f0)+"&f1="+escape(f1)+"&f2="+escape(f2);
   dest = dest.toLowerCase();
   dest=replaceCharacters(dest,'/sc.hkex.com.hk/gb/','/'); //remove X-server from sim-chi
   dest=replaceCharacters(dest,'/202.84.254.74/gb/','/'); //remove X-server from sim-chi
   dest=replaceCharacters(dest,'/sc.hkex.com.hk/TuniS/','/'); //remove X-server from sim-chi
   dest=replaceCharacters(dest,'/202.84.254.74/TuniS/','/'); //remove X-server from sim-chi


  if (from=='eng') {
    if (to=='chi' ){ 
       dest=replaceCharacters(dest,'/e_','/tc_');
    }
    if (to=='sim' ){ 
       dest=replaceCharacters(dest,'://202.84.254.11','://www.hkgem.com');	/*** 20050901 ***/
       dest=replaceCharacters(dest,'://203.78.5.11','://www.hkgem.com');	/*** 20050901 ***/
       dest=replaceCharacters(dest,'/e_','/tc_');
       dest=replaceCharacters(dest,'://','://sc.hkex.com.hk/gb/');
   	   openDisclaimer();
    }
  }

  if (from=='chi') {
    if (to=='eng' ){ 
       dest=replaceCharacters(dest,'/tc_','/e_');
    }
    if (to=='sim' ){ 
       dest=replaceCharacters(dest,'://202.84.254.11','://www.hkgem.com');	/*** 20050901 ***/
       dest=replaceCharacters(dest,'://203.78.5.11','://www.hkgem.com');	/*** 20050901 ***/
       dest=replaceCharacters(dest,'://','://sc.hkex.com.hk/gb/');
	   openDisclaimer();
	}
  }

  if (from=='sim') {
    if (to=='eng' ){ 
       dest=replaceCharacters(dest,'/tc_','/e_');
    }
    if (to=='chi' ){ 
       dest=dest
    }
  }

  if (getHTTPHeader() == "https") {
	dest=replaceCharacters(dest,'http://','https://'); //replace http to https
  }
  window.parent.location=dest;
}

function openDisclaimer() {
	var url = "/disclaimer/disclaimer.htm";
	var w = 510;
	var h = 480;
	var win;
	win = window.open(url, "", "width=" + w + ", height=" + h + ", left=" + (screen.width-w)/2 + ", top=" + (screen.height-h)/2 + ", scrollbars=yes, status=yes");
	if (win.focus != null)
		win.focus();
}

function getDomain(url) {
	var lead_slashes = url.indexOf("://");
	var domain_start = lead_slashes + 3;
	var without_resource = url.substring(domain_start,url.length);
	var next_slash = without_resource.indexOf("/");
	var domain = without_resource.substring(0, next_slash);
	return domain;
}
