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;
}

function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	  "; path=/" +  
	  ((domain == null) ? "" : ("; domain=" + domain)) +    
	  ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
	var _exp = new Date();  
	_exp.setTime (_exp.getTime() - 2);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + _exp.toGMTString() + "; path=/";
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}


function isSimplifiedChinesePage() {
	// simplied chinese has the URL prefix: sc.info.gov.hk/gb/
	var domainStr = document.domain;
	return (domainStr.indexOf("sc.info.gov.hk") >= 0);

	// fay:
	// var url = window.location.href;
	// return (domainStr.indexOf("sc.info.gov.hk") >= 0 || url.indexOf("/chi/") >= 0);
}

function checkDisclaimerCookie_1() {
	////if (isSimplifiedChinesePage()) {
		var sc_1 = GetCookie('sc_1');
		////var url = window.location.href;
		////SetCookie('sc1_src', url);
		if (sc_1 == null) {
			location = "../global/sc_disclaimer/index.html";
			////window.location.href = "../global/sc_disclaimer/index.html";
		}
	////}else
	////{
	////	DeleteCookie('sc_1');
	////}
}


function disclaimerOK_1() {
	var expDays = 1; // number of days the cookie should last
	var exp = new Date(); 
	var sc1_src = GetCookie('sc1_src');	
	exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

         //alert("referrer = " + document.referrer);
        SetCookie('sc_1', 1, exp);
		window.location.href = sc1_src;
        // window.location.href = window.location.href;
        // window.location.href = document.referrer;
	// window.location.reload(true);
	////history.back();
	////history.go(-1);
	////history.go(1);
}

function disclaimerCookie_2_isSet() {
	if (isSimplifiedChinesePage() && (GetCookie('sc_2')==null)) {
		return false;
	}
	return true;
}

function disclaimerCookie_2_remove() {
	DeleteCookie('sc_2');
}

function disclaimerCookie_2_setSrc (url) {
	SetCookie('sc2_src', url);
}

function disclaimerCookie_2_getSrc (url) {
	return GetCookie('sc2_src');
}

function disclaimerOK_2() {
	var expDays = 1; // number of days the cookie should last
	var exp = new Date(); 
	exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

        SetCookie('sc_2', 1, exp);
	var sc2_src = GetCookie('sc2_src');	
	if (sc2_src != null) {
		DeleteCookie('sc2_src');
		window.location.href = sc2_src;
	} else {
		window.close();
	}
}
