var ns4=document.layers
var ie=document.all
var ns6=document.getElementById&&!document.all
var dom=document.getElementById&&!document.all

var hanwebhost = "sc.octopuscards.com";
var defaultDomainName = "www.octopuscards.com";
var defaultSSLDomainName = "secure.octopuscards.com";

/**********************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);
}
/**********************************************/
			


/**********************Show and Hide Image*****************************/
// Show Image
function ImgOn(Nam) {
	if (document.images) {
		document.images[Nam].src = eval(Nam+ "_over.src");
    }
}

// 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 ChangeImgSC(Nam, UrlTC){
  if(document.images) {
    document.images[Nam].src = (window.location.href.indexOf("/gb/") != -1)? UrlTC.replace("/tc/","/sc/") : UrlTC;
  }
}


function PopUpWindow(targetUrl,name,width,height,resizable) {
	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();
}

// popup window
function popwin(url,nam,w,h,x,y,st,tb,di,mb,sb,rs,lo){
	if (url.length<=0){
		return false;
	}
	w = (w<=0) ? 460 : w;
	h = (h<=0) ? 418 : h;
	if (window.screen) {
		var cw = Math.floor((screen.availWidth-w)/2);
		var ch = Math.floor((screen.availHeight-h)/2);
	}else{
		cw = 0;
		ch = 0;
	}
	x = (x<0) ? cw : x;
	y = (y<0) ? ch : y;
	st = (st==1) ? 'yes' : 'no';
	tb = (tb==1) ? 'yes' : 'no';
	di = (di==1) ? 'yes' : 'no';
	mb = (mb==1) ? 'yes' : 'no';
	sb = (sb==1) ? 'yes' : 'no';
	rs = (rs==1) ? 'yes' : 'no';
	lo = (lo==1) ? 'yes' : 'no';
	var features = 'left='+x+',top='+y+',width='+w+',height='+h+',status='+st+',toolbar='+tb+',directories='+di+',menubar='+mb+',scrollbars='+sb+',resizable='+rs+',location='+lo;
	var winobj = window.open(url,nam,features);
	if (!winobj) return;
	winobj.opener = this;
	winobj.focus();
}

function popOHLwin(url)
{
	popwin(url,'OHL',780,600,-1,-1,1,1,1,1,1,1,1);
	return false;
}

//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("/TuniS/")>=0 ) || (location.href.indexOf("/gb/")>=0))
		langCode = "sc";

	if( (target!=null) && ("consumer"==target) ) {
		if (langCode == "sc")
			location.href = "http://sc.octopuscards.com/gb/www.octopuscards.com/consumer/tc/index.jsp";
		else
			location.href = "http://" + defaultDomainName + "/consumer/" + langCode + "/index.jsp";
	}
	if( (target!=null) && ("corporate"==target) ) {
		if (langCode == "sc")
			location.href = "http://sc.octopuscards.com/gb/www.octopuscards.com/corporate/tc/index.jsp";
		else
			location.href = "http://" + defaultDomainName + "/corporate/" + langCode + "/index.jsp";
	}
	if( (target!=null) && ("rewards"==target) ) {
		if (langCode == "sc")
			location.href = "http://sc.octopusrewards.com.hk/gb/www.octopusrewards.com.hk/tc/index.jsp"
		else
			location.href = "http://www.octopusrewards.com.hk/" + langCode + "/index.jsp";
	}
	if( (target!=null) && ("holdings"==target) ) {
		if (langCode == "sc")
			location.href = "http://sc.octopus.com.hk/gb/www.octopus.com.hk/tc/index.jsp" // "http://sc.octopusholdings.com.hk/TuniS/www.octopusholdings.com.hk/tc/index.jsp"
		else
			location.href = "http://www.octopus.com.hk/" + langCode + "/index.jsp"; //http://www.octopusholdings.com.hk/
	}
}


/***********************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 ) {
	    
	var targetSection = "/consumer/";
    
    if( location.href.indexOf("/corporate/")>=0 ) {
        targetSection = "/corporate/";
    }  
    
    if( location.href.indexOf("/tc/")>=0 ){
    
        //for tc pages
	    document.location.href = "http://"+ defaultDomainName + targetSection + "tc/flash.jsp";
        //return;
    } else if( location.href.indexOf("/gb/")>=0 ) {
    
        //for sc pages
	    document.location.href = "http://"+ hanwebhost +"/gb/" + defaultDomainName + targetSection + "tc/flash.jsp";
    
    }  else {      
	
        //for en pages
	    document.location.href = "http://"+ defaultDomainName + targetSection + "en/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://","");
            ntarget = "http://"+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://","");
            ntarget = "http://"+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://","");
	host = ntarget.substring(0,ntarget.indexOf("/"));
	
	location.href = "http://"+hanwebhost+"/gb/"+host+url;
}

function popUpPress(url) {
	OpenWin =  window.open(url,'popUpPress', 'toolbar=0,menubar=0,personalbar=0,resizable=0,scrollbars=1,height=600,width=620');

	OpenWin.focus();
}

function popUpShop(url) {
	OpenWin =  window.open(url,'popUpShop', 'toolbar=0,menubar=0,personalbar=0,resizable=0,scrollbars=1,height=500,width=620');

	OpenWin.focus();
}

function popUpWhereToUse(url) {
	OpenWin =  window.open(url,'popUpWhereToUse', 'toolbar=0,menubar=0,personalbar=0,resizable=0,scrollbars=1,height=500,width=620');

	OpenWin.focus();
}

function popUpMileStone(url) {
	OpenWin =  window.open(url,'popUpMileStone', 'toolbar=0,menubar=0,personalbar=0,resizable=0,scrollbars=1,height=600,width=660');

	OpenWin.focus();
}

function popAboutUS(url) {
	OpenWin =  window.open(url,'popAboutUS', 'toolbar=0,menubar=0,personalbar=0,resizable=0,scrollbars=1,height=500,width=620');

	OpenWin.focus();
}

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) {
	OpenWin =  window.open(url,'popUpMap', 'toolbar=0,menubar=0,personalbar=0,resizable=0,scrollbars=1,height=600,width=620');

	OpenWin.focus();
}

function SSLRedirect(){
        var current = location.href;
        
        if (current.indexOf("/shop/") >= 0){        
        
                if (current.indexOf(hanwebhost) >= 0){
                        location.href = "https://" + defaultSSLDomainName + "/shop/tc/index.jsp";
                        
                } else if (current.indexOf(defaultDomainName) >= 0){                
                        current = current.replace("http://" + defaultDomainName,"https://" + defaultSSLDomainName);
                        current = current.replace("https://" + defaultDomainName,"https://" + defaultSSLDomainName);
                        location.href = current;
                        
                } 
                
        } else {
                if (current.indexOf("https://" + hanwebhost) >= 0){
                        location.href = current.replace("https://","http://");
                        
                } else if (current.indexOf("https://" + defaultDomainName) >= 0){
                        location.href = current.replace("https://","http://");
                        
                } 
        }

}

SSLRedirect();  //hanlder by redirect Jsp instead

// For product - card type - sold
function toggleDiv(id,state){
  var e = document.getElementById(id);
  if (e){
    var divClass = (state==1) ? "cardTypeOn" : "cardTypeOff";
    e.className = divClass;
  }
}
