var ns4=document.layers
var ie=document.all
var ns6=document.getElementById&&!document.all
var dom=document.getElementById&&!document.all

var hanwebhost = "sc.octopus.com.hk";
var defaultDomainName = "www.octopus.com.hk";

/**********************Disable the Enter Button for the form*****************************/
function kH(e) {
	var pK = document.all? window.event.keyCode:e.which;
	return pK != 13;
}

document.onkeypress = kH;
if (document.layers) {
	document.captureEvents(Event.KEYPRESS);
}
/**********************************************/
			

function initImages() {
	var targetLang = "en";
	
    if( location.href.indexOf("/tc/")>=0 ){
		targetLang = "tc";
    }
    if( location.href.indexOf("/gb/")>=0 ){
		targetLang = "sc";
    }

	header_img_1_0_off = new Image();header_img_1_0_off.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/'+targetLang+'/g_update_off.gif';header_img_1_0_over = new Image();header_img_1_0_over.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/'+targetLang+'/g_update_on.gif';
	header_img_1_1_off = new Image();header_img_1_1_off.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/'+targetLang+'/g_career_off.gif';header_img_1_1_over = new Image();header_img_1_1_over.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/'+targetLang+'/g_career_on.gif';
	header_img_1_2_off = new Image();header_img_1_2_off.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/'+targetLang+'/g_contact_off.gif';header_img_1_2_over = new Image();header_img_1_2_over.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/'+targetLang+'/g_contact_on.gif';
	header_img_1_3_off = new Image();header_img_1_3_off.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/g_tc_off.gif';header_img_1_3_over = new Image();header_img_1_3_over.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/g_tc_on.gif';
	header_img_1_4_off = new Image();header_img_1_4_off.src = 'http://www.octopus.com.hk/images/global/nav/g_tc_off.gif';header_img_1_4_over = new Image();header_img_1_4_over.src = 'http://www.octopus.com.hk/images/global/nav/g_tc_on.gif';	
	header_img_1_5_off = new Image();header_img_1_5_off.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/g_en_off.gif';header_img_1_5_over = new Image();header_img_1_5_over.src = 'http://sc.octopus.com.hk/gb/www.octopus.com.hk/images/global/nav/g_en_on.gif';
}

/**********************Show and Hide Image*****************************/
// Show Image
function ImgOn(Nam) {
	if (document.images) {
		if (eval(Nam+ "_over.src") != null)
		{
			document.images[Nam].src = eval(Nam+ "_over.src");
			if ((Nam == "header_img_1_4") && (location.href.indexOf("/gb/")>=0))
				document.images[Nam].alt = "繁体";
		}
    }
}

// Hide Image
function ImgOff(Nam) {
	if (document.images) {
		document.images[Nam].src = eval(Nam+ "_off.src");
    }
}
/**********************************************/


/******************** Replace Image **********************************/
function ChangeImg(Nam, Url){
    if (document.images){
        document.images[Nam].src = Url;
    }
}

function PopUpWindow(page,name,w,h,resizable) {
	// need to round the value, Safari doesn't like non-integer value
	popLeft = Math.floor((screen.availWidth - w)/2);
	popTop = Math.floor((screen.availHeight - h)/2);

	if (navigator.appName == 'Netscape') {
		popWindow = window.open(page,name,'width='+w+',height='+h+',resizable="+resizable+",menubar=no,status=no,scrollbars=yes,toolbar=no,directories=no,location=no,screenX='+popLeft+',screenY='+popTop);
		popWindow.focus();
	}	
	
    	else if (navigator.appName == 'Microsoft Internet Explorer') {
		popWindow = window.open(page,name,'width='+w+',height='+h+',resizable="+resizable+",menubar=no,status=no,scrollbars=yes,toolbar=no,directories=no,location=no,top='+popTop+',left='+popLeft);
		popWindow.focus();
	}
 	
	else {
		popWindow = window.open(page,name,'width='+w+',height='+h+',resizable="+resizable+",menubar=no,status=no,scrollbars=yes,toolbar=no,directories=no,location=no,top='+popTop+',left='+popLeft);
		popWindow.focus();
	}

/*	this.name = "main";
	OpenWin = window.open(targetUrl, name, "status=no,toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable="+resizable+",width="+ (width+18) + ",height=" + height + ",left=90,top=90");

	OpenWin.focus();
*/
}

//Drop Down List for Site Swicth
//change the site when the user choose the site
function ChangeSite(form, langCode) {
	var target = form.siteDropDownList.value;
	if( location.href.indexOf("/gb/")>=0 )
		langCode = "sc";
	
	if( (target!=null) && ("rewards"==target) ) {
		if (langCode == "sc")
			location.href = "http://sc.octopus.com.hk/gb/sc.octopusrewards.com.hk/gb/www.octopusrewards.com.hk/tc/index.jsp"
		else
			location.href = "http://sc.octopusrewards.com.hk/gb/www.octopusrewards.com.hk/" + langCode + "/index.jsp";
	}
	if( (target!=null) && ("cards"==target) ) {
		if (langCode == "en")
			location.href = "http://sc.octopuscards.com/gb/www.octopuscards.com/enindex.jsp";
		else
			location.href = "http://sc.octopuscards.com/gb/www.octopuscards.com/";
	}
}


/***********************Flash detect*************************************/
var contentVersion = 7;
var FlashCanPlay = false;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if (plugin) {
	var words = navigator.plugins["Shockwave Flash"].description.split(" ");
    for (var i = 0; i < words.length; ++i)
    {
		if (isNaN(parseInt(words[i])))
		continue;
		var PluginVersion = words[i]; 
	}
	if (PluginVersion >= contentVersion)
	{
		FlashCanPlay = true;
	}
	else
	{
		FlashCanPlay = false;
	}
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('FlashCanPlay = ( IsObject(CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" & contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}

// initialize global variables
var detectableWithVB = false;
var pluginFound = false;

if( !FlashCanPlay ) {
	
	document.location.href = "http://sc.octopus.com.hk/gb/www.octopus.com.hk/flash.jsp";
}


function goURL(daURL) {
    // if the browser can do it, use replace to preserve back button
    if(javascriptVersion1_1) {
	window.location.replace(daURL);
    } else {
	window.location = daURL;
    }
    return;
}

function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
    // check for redirection
    if( redirectURL && ((pluginFound && redirectIfFound) || 
	(!pluginFound && !redirectIfFound)) ) {
	// go away
	goURL(redirectURL);
	return pluginFound;
    } else {
	// stay here and return result of plugin detection
	return pluginFound;
    }	
}

function canDetectPlugins() {
    if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
	return true;
    } else {
	return false;
    }
}

function detectFlash(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Shockwave','Flash'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    // check for redirection
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectDirector(redirectURL, redirectIfFound) { 
    pluginFound = detectPlugin('Shockwave','Director'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('SWCtl.SWCtl.1');
    }
    // check for redirection
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectQuickTime(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectQuickTimeActiveXControl();
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectReal(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('RealPlayer');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }	
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectWindowsMedia(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Windows Media');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
        
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
	var pluginsArrayLength = navigator.plugins.length;
	// for each plugin...
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
	    // loop through all desired names and check each against the current plugin name
	    var numFound = 0;	    
	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
		// if desired plugin name is found in either plugin name or description
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
		    // this name was found
		    numFound++;
		}   
	    }
	    // now that we have checked all the required names against this one plugin,
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	    }
	}
    }
    return pluginFound;
} // detectPlugin

// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<' + 'script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}
/************************************************************************/


/*****************email Subscription Box*******************/
//clear the box when use forcus the box
function clearField(obj, valueSet, fieldName)
{
	eval( fieldName + " = '" + obj.id + "'");

	if ( eval("!" + valueSet) )
	{
		obj.value = "";
		eval(valueSet + " = true");
	}
}

var newsLetterSet = false;
var newsLetterSetField='';
/**********************************************/

function switchLang(current, target){    
    var ntarget;
    
    if (location.href.indexOf("ht"+"tp://"+hanwebhost+"/gb/")>=0){ // break down string to avoid hanweb translation
        ntarget = location.href.replace(hanwebhost+"/gb/","");
        
        if (target == "/en/"){
            ntarget = ntarget.replace("/tc/","/en/");
        }
    } else {
        if (target == "/sc/"){
            ntarget = location.href.replace(current,"/tc/");
            ntarget = ntarget.replace("http://sc.octopus.com.hk/gb/","");
            ntarget = "http://sc.octopus.com.hk/gb/"+hanwebhost+"/gb/"+ntarget;
        } else {
            ntarget = location.href.replace(current,target);
        }
    }
    location.href = ntarget;
}


function switchLang_gateway(current, target){   // switch language for gateway page 
    var ntarget;
    
    if (location.href.indexOf("ht"+"tp://"+hanwebhost+"/gb/")>=0){ // break down string to avoid hanweb translation
        ntarget = location.href.replace(hanwebhost+"/gb/","");
        
        if (target == "en"){
            ntarget = ntarget.replace("/index.jsp","");
            if( ntarget.lastIndexOf("/")<(ntarget.length-1) ) {
                ntarget += "/";
            }
            ntarget += "enindex.jsp";
        }
    } else {
        if (target == "sc"){
            ntarget = location.href.replace("/enindex.jsp","");
            ntarget = ntarget.replace("http://sc.octopus.com.hk/gb/","");
            ntarget = "http://sc.octopus.com.hk/gb/"+hanwebhost+"/gb/"+ntarget;
        } else {
	        ntarget = location.href;
            ntarget = ntarget.replace("/index.jsp","");
	        ntarget = ntarget.replace("/enindex.jsp","");

            if( ntarget.lastIndexOf("/")<(ntarget.length-1) ) {
                ntarget += "/";
            }
            if (target == "tc"){
                ntarget += "index.jsp";	
            } else {
                ntarget += "enindex.jsp";
            }
        }
    }

    location.href = ntarget;
}


function gotoSCURL(url) {
	var ntarget = location.href;
	var host;
	ntarget = ntarget.replace("http://sc.octopus.com.hk/gb/","");
	host = ntarget.substring(0,ntarget.indexOf("/"));
	
	location.href = "http://sc.octopus.com.hk/gb/"+hanwebhost+"/gb/"+host+url;
}

function popUpPress(url) {
	PopUpWindow(url,'popUpPress',620,600,'no');
/*
	OpenWin =  window.open(url,'popUpPress', 'toolbar=0,menubar=0,personalbar=0,resizable=0,scrollbars=1,height=600,width=620');

	OpenWin.focus();
*/
}

function popUpKeyExec (url) {
	PopUpWindow(url, 'pop', 760, 520, 'no');
}

function popUpCompStruct (url) {
	PopUpWindow(url, 'pop', 620, 380, 'no');
}


function popAgreement(url) {
	OpenWin =  window.open(url,'popAgreement', 'toolbar=0,menubar=0,personalbar=0,resizable=0,scrollbars=1,height=500,width=620');

	OpenWin.focus();
}

function popUpMap(url) {
	PopUpWindow(url, 'pop', 620, 620, 'no');
}

function SSLRedirect(){
        var current = location.href;

        if (current.indexOf("/shop/cart/") >= 0){
                if (current.indexOf("http://sc.octopus.com.hk/gb/") >= 0){
                        location.href = current.replace("http://sc.octopus.com.hk/gb/","https://");
                }
        } else {
                if (current.indexOf("https://") >= 0){
                        location.href = current.replace("https://","http://");
                }
        }

}

// SSLRedirect();  hanlder by redirect Jsp instead


