//alert(location.protocol)	//	http:
//alert(location.host)		//	hocalhost
//al1ert(location.href)		//	http://localhost/index.asp
//alert(location.pathname)	//	/index.asp
//alert(top.window.name)

var gbCheckOK = true
//***********************************
var gsPopupPage = "index.asp"
//var gsPopupPage = "default.asp"

//***********************************
//var gsHost = "localhost"
//var gsHost = "test.soccer10hk.com"
var gsHost = "www.soccer10hk.com"

//var gsBrowserMsg = "This web site only support Microsoft IE browser 5.0 or later."
//var gsCookieMsg = "Please enable your cookies setting."
//var gsXMLMsg = "Please install the MSXML Parser, and then close and open the browser again."

//disable select text (mouse)
function disableselect(e){return false}
function enableselect(){return true}
document.onselectstart = new Function ("return false"); // if IE4+
if(window.sidebar){document.onmousedown=disableselect; document.onclick=enableselect;} // if NS6


//disable show context menu (both mouse and keyboard)
function document.oncontextmenu() {
	return false
}

//disable key when key press (Only work for IE browser.  If version < 5.0, some key may not be mapped)
var fKeyMap = [
	{ keyCode:112,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F1"  },
	{ keyCode:113,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F2"  },
	{ keyCode:114,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F3"  },
	{ keyCode:115,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F4"  },
	{ keyCode:116,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F5"  },
	{ keyCode:117,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F6"  },
	{ keyCode:118,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F7"  },
	{ keyCode:119,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F8"  },
	{ keyCode:120,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F9"  },
	{ keyCode:121,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F10"  },
	{ keyCode:122,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F11"  },
	{ keyCode:123,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"F12"  },

	{ keyCode:112,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F1"  },
	{ keyCode:113,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F2"  },
	{ keyCode:114,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F3"  },
	{ keyCode:115,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F4"  },
	{ keyCode:116,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F5"  },
	{ keyCode:117,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F6"  },
	{ keyCode:118,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F7"  },
	{ keyCode:119,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F8"  },
	{ keyCode:120,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F9"  },
	{ keyCode:121,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F10"  },
	{ keyCode:122,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F11"  },
	{ keyCode:123,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+F12"  },

	{ keyCode:112,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F1"  },
	{ keyCode:113,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F2"  },
	{ keyCode:114,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F3"  },
	{ keyCode:115,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F4"  },
	{ keyCode:116,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F5"  },
	{ keyCode:117,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F6"  },
	{ keyCode:118,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F7"  },
	{ keyCode:119,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F8"  },
	{ keyCode:120,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F9"  },
	{ keyCode:121,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F10"  },
	{ keyCode:122,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F11"  },
	{ keyCode:123,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+F12"  },

	{ keyCode:112,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F1"  },
	{ keyCode:113,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F2"  },
	{ keyCode:114,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F3"  },
	{ keyCode:115,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F4"  },
	{ keyCode:116,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F5"  },
	{ keyCode:117,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F6"  },
	{ keyCode:118,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F7"  },
	{ keyCode:119,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F8"  },
	{ keyCode:120,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F9"  },
	{ keyCode:121,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F10"  },
	{ keyCode:122,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F11"  },
	{ keyCode:123,  isMapped:true, shift:true, ctrl:false, alt:false,  vKey:"Shift+F12"  },

	{ keyCode:37,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+left"  }, //back
	{ keyCode:38,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+up"  },
	{ keyCode:39,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+right"  }, //forward
	{ keyCode:40,  isMapped:true, shift:false, ctrl:false, alt:true,  vKey:"Alt+down"  },
	{ keyCode:65,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+a"  }, //select all
	{ keyCode:67,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+c"  }, //copy
	{ keyCode:78,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+n"  }, //new window
	{ keyCode:79,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+o"  }, //open
	{ keyCode:80,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+p"  }, //print
	{ keyCode:83,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+s"  }, //save
	{ keyCode:86,  isMapped:true, shift:false, ctrl:true, alt:false,  vKey:"Ctrl+v"  }, //paste
	{ keyCode:91,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"Start Menu"  },
	{ keyCode:93,  isMapped:true, shift:false, ctrl:false, alt:false,  vKey:"Context Menu"  },

	{ keyCode:0,   isMapped:false } // End of table
];
		
function findKeyMap (keycode, shift, ctrl, alt) {
	for (var i = 0; i < fKeyMap.length; i++)
		if (fKeyMap[i].keyCode  == keycode
		&& fKeyMap[i].shift == shift
		&& fKeyMap[i].ctrl  == ctrl
		&& fKeyMap[i].alt   == alt)
			if (fKeyMap[i].isMapped)
				return ("mapped as " + fKeyMap[i].vKey);
			else
				return ("unmapped as " + fKeyMap[i].vKey);

	return "not mapped to anything in the gwebkbd.js table";
}

function doKeyDown (e) {
	var myKeyCode      = e.keyCode;
	var mySrcElement   = e.srcElement;
	var isShiftPressed = e.shiftKey;
	var isCtrlPressed  = e.ctrlKey;
	var isAltPressed   = e.altKey;


	// Enter(13), Shift(16), Ctrl(17), Alt(18), CapsLock(20) keys?
	if (myKeyCode >= 13 && myKeyCode <= 20)
	return true;

/*
	window.status="Key code=" + myKeyCode 
	+ "; Shift=" + isShiftPressed
	+ "; Ctrl=" + isCtrlPressed
	+ "; Alt=" + isAltPressed
	+ "\nThis key is " + findKeyMap (myKeyCode, isShiftPressed, isCtrlPressed, isAltPressed)   ;
*/
	window.status=''

	window.event.returnValue=false;
}

function document.onkeydown() {
	doKeyDown(event);
}

//Check Host
function CheckHost() {
	if (location.host != gsHost) {
		gbCheckOK = false;
	}
}

//check browser
function CheckBrowser() {
	var browserIsOK = 0;

	browserIsOK = navigator.appVersion.indexOf("MSIE ");

	if (browserIsOK < 0) {
		gbCheckOK = false;
		alert(gsBrowserMsg)
	} else if (parseFloat(navigator.appVersion.slice(browserIsOK+5)) < 5.0) {
		gbCheckOK = false;
		alert(gsBrowserMsg)
	}
}

//check cookie enable
function CheckCookie() {
	this.getCookieVal=Browser_getCookieVal;
	this.fixCookieDate=Browser_fixCookieDate;
	this.getCookie=Browser_getCookie;
	this.setCookie=Browser_setCookie;
	this.deleteCookie=Browser_deleteCookie;

	if(navigator.cookieEnabled) {
		this.cookieEnabled=navigator.cookieEnabled;
	} else {
		var expdate=new Date();
		expdate.setTime(expdate.getTime()+(365*24*60*60*1000));
		this.fixCookieDate(expdate);
		this.setCookie("test_js","safe to delete",expdate,"/");
		(this.getCookie("test_js"))?this.cookieEnabled=true:this.cookieEnabled=false;
		this.deleteCookie("test_js","/");
	}

	if (this.cookieEnabled == false) {
		gbCheckOK = false;
		alert(gsCookieMsg)
	}
}
function Browser_getCookieVal(offset){
	var endstr=document.cookie.indexOf(";",offset);
	if(endstr==-1)endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset,endstr));
}
function Browser_fixCookieDate(date){
	var base=new Date(0);
	var skew=base.getTime();
	if(skew>0)date.setTime(date.getTime()-skew);
}
function Browser_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 this.getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if(i==0)break;
	}
	return null;
}
function Browser_setCookie(name,value,expires,path,domain,secure){
	document.cookie=name+"="+escape(value)+((expires)?"; expires="+expires.toGMTString():"")+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+((secure)?"; secure":"");
}
function Browser_deleteCookie(name,path,domain){
	if(this.getCookie(name))document.cookie=name+"="+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function CheckXML() {
	sOSLanguage = navigator.browserLanguage

	try {
		var xmlDom = new ActiveXObject("MSXML2.DOMDocument.4.0")
	}
	catch(e) {
		try {
			var xmlDom = new ActiveXObject("MSXML2.DOMDocument.3.0")
		}
		catch(e) {
//			if (sOSLanguage == 'zh-cn') {

//			} else if ((sOSLanguage == 'zh-hk') || (sOSLanguage == 'zh-tw')) {

//			} else {
				gbCheckOK = false;
				alert(gsXMLMsg);

				//'en-us'
				document.write("<object id='MSXML3SP2' classid='clsid:f5078f32-c551-11d3-89b9-0000f81fe221' codebase='../object/msxml3.cab#version=8,20,8730,4' type='application/x-oleobject' STYLE='display: none'></object>")
//			}
		}
	}
}

function CheckNotFramed() {
	try {
		if (top.location.href != self.location.href) {
			gbCheckOK = false;
		}
	} catch (e) {
		gbCheckOK = false;
	}
}

function CheckHasFrameset() {
	//this page must framed
	if (window.parent != window) {
		try {
			if (top.location.href == parent.location.href) {
				if (self.location.host != gsHost) {
					gbCheckOK = false;
				} // else
					//OK -- has specific frameset
			} else
				gbCheckOK = false;
		} catch (e) {
			gbCheckOK = false;
		}
	} else
		gbCheckOK = false;
}

function CheckWindowName() {
	if (replaceSubstring(top.window.name, '_', '.') != gsHost) {
		gbCheckOK = false
	} else if (replaceSubstring(top.window.name, '_', '.') != location.host) {
		gbCheckOK = false
	}
}

function popup(sPopupPage) {
	var oPopupWin = window.open(sPopupPage, replaceSubstring(location.host, '.', '_'), "scrollbars=1, resizable=1, menubar=0, toolbar=0, width=" + (screen.width-14) + ", height=" + (screen.height-60) + ", top=1, left=1")
}

function CloseBrowser() {
	parent.opener = top;
	parent.close();
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

//main
function CheckSecurity(bIsDefaultPage, bIsCheckHasFrameset, bCloseBrowserIfError) {
	CheckBrowser();
	CheckHost();

	if (bIsCheckHasFrameset) {
		CheckHasFrameset();
	} else if (bIsCheckHasFrameset == false) {
		CheckNotFramed();
	}

	CheckCookie();
	CheckXML();

	if (bIsDefaultPage) {
		//***********************************
		//if (gbCheckOK) {popup(gsPopupPage)}
	} else {
		CheckWindowName();
	}

	if ((gbCheckOK == false) && (bCloseBrowserIfError)) {
		CloseBrowser();
	}
	return gbCheckOK;
}
