// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 28;

var acl_panel_url = "http://apps.hkedcity.net/acladmin/MagisterApplication.php";
//var settings_url = "http://apps.hkedcity.net/acladmin/assets/xml/Settings.xml";
var acl_panel_width = 600;
var acl_panel_height = 450;

function acl_close()	{
	var contentObj = document.getElementById("floatingAclAdminDiv");
	contentObj.innerHTML = "";
	hidefloatingAclAdminDiv();
}

function hidefloatingAclAdminDiv()	{
	if (document.getElementById)	{
		document.getElementById("floatingAclAdminDiv").style.display = "none";
	}
	else if (document.all)	{
		document.all["floatingAclAdminDiv"].style.display = "none";
	}
	else if (document.layers)	{
		document.layers["floatingAclAdminDiv"].style.display = "none";
	}
}

function showfloatingAclAdminDiv() {
	var oDiv;
	floatingLayerSetVariables('floatingAclAdminDiv');floatingLayerCheckLocation();
	if (oDiv = document.getElementById("floatingAclAdminDiv")) {
		if (oDiv.style) {
        		oDiv.style.display = 'block';
        		oDiv.style.visibility = 'visible';
		}
	}
	return;
}

function PopAclAdminPanel(tool_type, tool_id, identifier, show_cols, descr, show_profile_page, role_list)	{
	if (tool_type == "" || tool_id == "")
	{	alert("Invalid parameters!");
		return;
	}
	var contentObj = document.getElementById("floatingAclAdminDiv");
	var param_str = "tool_type=" + AclURLEncode(AclBase64.encode(tool_type));
	param_str += "&tool_id=" + AclURLEncode(AclBase64.encode(tool_id));
	if (identifier == "")
		param_str += "&identifier=";
	else
		param_str += "&identifier=" + AclURLEncode(AclBase64.encode(identifier));
	if (show_cols)
		param_str += "&show_cols=" + AclURLEncode(AclBase64.encode(show_cols));
	else
		param_str += "&show_cols=" + AclURLEncode(AclBase64.encode("111111111111111111111111111111"));
	if (descr)
		param_str += "&description=" + AclURLEncode(AclBase64.encode(descr));
	else
		param_str += "&description=";
	if (show_profile_page)
		param_str += "&show_profile_page=" + AclURLEncode(AclBase64.encode(show_profile_page));
	else
		param_str += "&show_profile_page=";
	if (role_list)
		param_str += "&role_list=" + AclURLEncode(AclBase64.encode(role_list));
	else
		param_str += "&role_list=";
//	param_str += "&settings_url=" + AclURLEncode(AclBase64.encode(settings_url));
	win = window.open(acl_panel_url + "?" + param_str, "ACL_PANEL", "width=" + acl_panel_width + ",height=" + acl_panel_height + ",left=10,top=10,resizable=yes,menubar=no,location=no,status=no,titlebar=no,toolbar=no");
	return win;
}

// ====================================================================
//       AclURLEncode and AclURLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in AclURLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function AclURLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function AclURLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

/**
*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
**/

var AclBase64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = AclBase64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = AclBase64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function floatingLayerSetVariables(layerObj) {
	if (navigator.appName == "Netscape") {
		floating_layer_v = ".top=";
		floating_layer_h = ".left=";
		floating_layer_dS = "document.";
		floating_layer_sD = "";
		floating_layer_y = "window.pageYOffset";
		floating_layer_x = "window.pageXOffset";
		floating_layer_iW = "window.innerWidth";
		floating_layer_iH = "window.innerHeight";
	}
	else {
		floating_layer_h = ".pixelLeft=";
		floating_layer_v = ".pixelTop=";
		floating_layer_dS = "";
		floating_layer_sD = ".style";
		floating_layer_y = "document.body.scrollTop";
		floating_layer_x = "document.body.scrollLeft";
		floating_layer_iW = "document.body.clientWidth";
		floating_layer_iH = "document.body.clientHeight";
	}
	floating_layer_object = "document.getElementById('" + layerObj + "')";
	//xyz = 500;
	floating_layer_innerX = eval(floating_layer_iW) - 215;
	floating_layer_innerY = eval(floating_layer_iH) - 360; // ****
	
	floatingLayerCheckLocationA();
}

floating_layer_movex = 0;
floating_layer_movey = 0;
floating_layer_xdiff = 0;
floating_layer_ydiff = 0;
floating_layer_ystart = 0;
floating_layer_xstart = 0;

function floatingLayerCheckLocation() {
	floating_layer_yy = eval(floating_layer_y);
	floating_layer_xx = eval(floating_layer_x);
	floating_layer_ydiff = floating_layer_ystart - floating_layer_yy;
	floating_layer_xdiff = floating_layer_xstart - floating_layer_xx;
	if ((floating_layer_ydiff < (-1)) || (floating_layer_ydiff > (1))) floating_layer_movey = Math.round(floating_layer_ydiff / 10), floating_layer_ystart -= floating_layer_movey;
	if ((floating_layer_xdiff < (-1)) || (floating_layer_xdiff > (1))) floating_layer_movex = Math.round(floating_layer_xdiff / 10), floating_layer_xstart -= floating_layer_movex;
	floating_layer_innerX = eval(floating_layer_iW) - floating_layer_movex;
	floating_layer_innerY = eval(floating_layer_iH) - floating_layer_movey; // ****
	eval(floating_layer_dS + floating_layer_object + floating_layer_sD + floating_layer_v + (floating_layer_y));
	eval(floating_layer_dS + floating_layer_object + floating_layer_sD + floating_layer_h + (floating_layer_x));
	setTimeout("floatingLayerCheckLocation()", 10);
}

function floatingLayerCheckLocationA() {
	floating_layer_ystart = eval(floating_layer_y);
	floating_layer_xstart=eval(floating_layer_x);
}

function floatingLayerDelayLayer(layerObj) {
	Eval(floating_layer_dS + floating_layer_object + floating_layer_sD + floating_layer_v + 0);
	Eval(floating_layer_dS + floating_layer_object + floating_layer_sD + floating_layer_h + (-200));
	floating_layer_object = "document.getElementById('" + layerObj + "')";
}
