ajax_script='/ajax.php';
function initAjaxPage(){
	AjaxPage.clearParams();
	AjaxPage.loadParams();
}

var throbberstate=0;
function showthrobber(){
	throbberstate+=1;
	$("throbber").style.display="block";
}
function hidethrobber(){
	throbberstate-=1;
	if (throbberstate<=0){
		thorbberstate=0;
		$("throbber").style.display="none";
	}
}
function AjaxLinkRequest(reqparams,link){
	link.blur();
	links=document.getElementsByClassName('activeLink');
	for (i=0; i<links.length; i++){
		links[i].className=links[i].className.replace(/ ?activeLink ?/,' ');
	}
	link.className+=' activeLink';
	AjaxRequest(reqparams);
	return false;
}
function AjaxRequest(reqparams,onsuccess){
	showthrobber();
	new Ajax.Request(ajax_script+"?"+Math.random(), {
			method: 'post',
			parameters:reqparams,
			noerror: true,
			initialize: function() { },
			onSuccess: function(transport) {
				ajaxEvalResponse(transport.responseText);
				if (onsuccess){
					onsuccess(transport.responseText);
				}
			},
			onComplete:function () {
				hidethrobber();
			}
		});
}

$E=function (el){ // hack for IE
	oldId=el.id;
	el.id='e_'+Math.random();
	res=$(el.id);
	el.id=oldId;
	return res;
}
function form_submit(f,event){
	submitbtns= new Array();
	inputs=f.getElementsByTagName('input');
	for (x in inputs){
		btn=inputs[x];
		if (btn){
			if (btn.type=='submit'){
				submitbtns.push(btn);
				btn.disabled=true;
			}
		}
	}
	showthrobber();
	$E(f).request({
		onSuccess: function(transport) {
			ajaxEvalResponse(transport.responseText);
		},
		onComplete: function(){
			for (x in submitbtns){
				btn=submitbtns[x];
				btn.disabled=false;
			}
			hidethrobber();
		}
	});
	return false;
}

function switchDisplay(objId,normalDisplay){
	if (!normalDisplay){
		normalDisplay='block'
	}
	obj=$(objId);
	if (obj){
		if (obj.style.display=='none'){
			obj.style.display=normalDisplay;
			return true;
		} else {
			obj.style.display='none';
			return false;
		}
	}else {
		alert(objId+' not found');
	}
}

function text2json(text){
	try{
		json=text.evalJSON();
	} catch(err) {
		alert(err);
	}
	return json
}
function ajaxEvalResponse(resp){
	if (resp.length>0){
			json=text2json(resp);
			if (json.HTML){
				HTML=json.HTML;
					for (x in HTML){
						var el=$(x);
						if (el){
							if (el.onAjaxInnerHTMLChange){
								el.onAjaxInnerHTMLChange(el.innerHTML,HTML[x]);
							}
							el.innerHTML=HTML[x];
						}
					}
			}
			if (json.APPENDHTML){
				APPENDHTML=json.APPENDHTML;
					for (x in APPENDHTML){
						var el=$(x);
						if (el){
							el.innerHTML+=APPENDHTML[x];
						}
					}
			}
			if (json.CSS){ // NOT USED YET
				CSS=json.CSS;
				for (elid in CSS){
					el=$(elid);
					if (el){
						for (cssVar in CSS[elid]){
							try{
								el.style[cssVar]=eval(CSS[elid][cssVar]);
							} catch(err) {
								el.style[cssVar]=CSS[elid][cssVar];
							}
						}
					}
				}
			}
			if (json.HTMLPROPS){ // NOT USED YET
				HTMLPROPS=json.HTMLPROPS;
				for (elid in HTMLPROPS){
					var el=$(elid);
					if (el){
						for (prop in HTMLPROPS[elid]){
								el[prop]=HTMLPROPS[elid][prop];
						}
					}
				}
			}
			if (json.evalJS){
				eval(json.evalJS);
			}
	}
}

function getKeyCode(e){
		var characterCode;
		if(e && e.which) {
				e = e
				characterCode = e.which
		} else {
				if (window.event){
						e = window.event;
				}
				characterCode = e.keyCode
		}
		return characterCode;
}

function getDocumentHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
	return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
var AjaxPage = new Object();
		AjaxPage.params= {};
		AjaxPage.getParams=function (){
			return AjaxPage.params;
		}
		AjaxPage.clearParams=function(){
			AjaxPage.params= {};
		}
		AjaxPage.setParam=function (param,val){
				this.params[param]=val;
				document.location.hash=this.getUrl();
				if (AjaxPage.onSetParam){
					AjaxPage.onSetParam();
				}
		}
		AjaxPage.getParam=function (param){
				if (this.params[param]){
						return this.params[param];
				} else {
						return null;
				}
		}
		AjaxPage.deleteParam=function (param){
				if (this.params[param]){
						delete this.params[param];
						document.location.hash=this.getUrl();
				}
		}

		AjaxPage.getUrl=function(){
				var params=new Array();
				for (x in this.params){
						param=this._encode(x);
						if (param.length>0){
							params.push(param+'='+this._encode(this.params[x]));
						}
				}
				return params.join('&');
		}

		AjaxPage.loadParams=function(){
				params=document.location.hash.substr(1).split('&');
// 				st='';
				for (i=0;i<params.length;i++){
						param=params[i].split('=');
						if (param.length==1){
								AjaxPage.params[this._decode(param[0])]='1';
						}
						if (param.length==2){
								AjaxPage.params[this._decode(param[0])]=this._decode(param[1]);
// 								st+=param[0]+' = '+param[1] + ' | ';
						}
				}
		}
		AjaxPage._encode=function(st){
				return encodeURIComponent(st);
		}
		AjaxPage._decode=function(st){
				return decodeURIComponent(st);
		}


/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
var Base64 = {

	// 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 = Base64._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 = Base64._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 htmlspecialchars (string, quote_style, charset, double_encode) {
    // Convert special characters to HTML entities
    //
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/htmlspecialchars    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // +      input by: Mailfaker (http://www.weedem.fr/)
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      input by: felix    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: charset argument not supported
    // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
    // *     returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
    // *     example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);    // *     returns 2: 'ab"c&#039;d'
    // *     example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
    // *     returns 3: 'my &quot;&entity;&quot; is still here'
    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined' || quote_style === null) {        quote_style = 2;
    }
    string = string.toString();
    if (double_encode !== false) { // Put this first to avoid double-encoding
        string = string.replace(/&/g, '&amp;');    }
    string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');

    var OPTS = {
        'ENT_NOQUOTES': 0,        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/'/g, '&#039;');
    }    if (!noquotes) {
        string = string.replace(/"/g, '&quot;');
    }

    return string;
}


function htmlspecialchars_decode (string, quote_style) {
    // Convert special HTML entities back to characters
    //
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/htmlspecialchars_decode    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Mateusz "loonquawl" Zalega
    // +      input by: ReverseSyntax
    // +      input by: Slawomir Kaniecki    // +      input by: Scott Cariss
    // +      input by: Francois
    // +   bugfixed by: Onno Marsman
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)    // +      input by: Ratheous
    // +      input by: Mailfaker (http://www.weedem.fr/)
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: htmlspecialchars_decode("<p>this -&gt; &quot;</p>", 'ENT_NOQUOTES');    // *     returns 1: '<p>this -> &quot;</p>'
    // *     example 2: htmlspecialchars_decode("&amp;quot;");
    // *     returns 2: '&quot;'
    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined') {        quote_style = 2;
    }
    string = string.toString().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    var OPTS = {
        'ENT_NOQUOTES': 0,        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/&#0*39;/g, "'"); // PHP doesn't currently escape if more than one 0, but it should
        // string = string.replace(/&apos;|&#x0*27;/g, "'"); // This would also be useful here, but not a part of PHP
    }
    if (!noquotes) {
        string = string.replace(/&quot;/g, '"');
    }
    // Put this in last place to avoid escape being double-decoded    string = string.replace(/&amp;/g, '&');

    return string;
}

function trim(string) {
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

