var Try = {
	these: function() {
	var returnValue;
	
	for (var i = 0; i < arguments.length; i++) {
		var lambda = arguments[i];
		try {
			returnValue = lambda();
			break;
		} catch (e) {}
		}
		
		return returnValue;
	}
} // Try

function $(element) {
	if (arguments.length > 1) {
		for (var i = 0, elements = [], length = arguments.length; i < length; i++) elements.push($(arguments[i]));
		return elements;
	}
	if (typeof element == 'string') element = document.getElementById(element);
	return element;
} // $

function AjaxTransport() {
	return Try.these(
		function() {return new ActiveXObject('Msxml2.XMLHTTP');},
		function() {return new ActiveXObject('Microsoft.XMLHTTP');},
		function() {return new XMLHttpRequest();}
	) || false;
} // AjaxTransport

function AjaxUpdate(Url, LoadContainer, ContentContainerOrCallBack, PostContentArray, CallBackParameter) {
	var RequestSuccess=false;
	try {
		var AjaxTransportUpdate=AjaxTransport();
		if ( AjaxTransportUpdate ) {
			var UrlParameters='Ajax=True';
			if ( typeof(PostContentArray)=='object' ) {
				for (var key in PostContentArray) UrlParameters+='&'+key+'='+escape_utf8(PostContentArray[key]);
			}
		    AjaxTransportUpdate.open('POST', Url, true);
		    AjaxTransportUpdate.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

			if ( typeof(LoadContainer)=='object' ) LoadContainer.style.display='block';
			if ( typeof(ContentContainerOrCallBack)=='object' ) ContentContainerOrCallBack.innerHTML='';

		    AjaxTransportUpdate.onreadystatechange = function() {
		        if ( AjaxTransportUpdate.readyState==4 ) {
					var ResponseContent=AjaxTransportUpdate.responseText;

					if ( typeof(LoadContainer)=='object' ) LoadContainer.style.display='none';
					if ( typeof(ContentContainerOrCallBack)=='function' ) ContentContainerOrCallBack(ResponseContent, AjaxTransportUpdate.status, CallBackParameter);
					else if ( typeof(ContentContainerOrCallBack)=='object' ) {
						ContentContainerOrCallBack.innerHTML=ResponseContent;
						EvalContentScripts(ResponseContent);
					}
				}
		    }
		    AjaxTransportUpdate.send(UrlParameters);
			RequestSuccess=true;
		}
	} catch (ex) {
	}
	return RequestSuccess;
} // AjaxUpdate

function EvalContentScripts(ContentHtml) {
	if ( ContentHtml ) {
		var ScriptRegExp=new RegExp('(?:<script[^>]*>)((\n|\r|.)*?)(?:<\/script>)', 'img');
		var ScriptMatches=ContentHtml.match(ScriptRegExp);

		if ( ScriptMatches ) {
			for ( var i=0; i<ScriptMatches.length; i+=1 ) {
				var EvalContent=ScriptMatches[i].replace(/<\/?script[^>]*>/gi, '');
				setTimeout(EvalContent, 0);
			}
		}
	}
} // EvalScripts

document.getElementsByClassName = function(className, parentElement) {
	var children = ($(parentElement) || document.body).getElementsByTagName('*');
	var elements = [], child;
	for (var i = 0, length = children.length; i < length; i++) {
		child = children[i];
		if ( child.className==className ) elements[elements.length]=child;
	}
	return elements;
} // getElementsByClassName


// Method for UTF-8 encoding
function escape_utf8(text) {
	return URLEncode(encode_utf8(text));
} // escape_utf8

function encode_utf8(text) {
	var utftext = "";
	if ( text ) {
		for (var n=0; n < text.length; n++) {
			var c=text.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;
} // encode_utf8

function unescape_utf8(text) {
	return decode_utf8(URLDecode(text));
} // unescape_utf8

// Method for UTF-8 decoding
function decode_utf8(text) {
	var string = "";
	var i=0;
	var c=c1=c2=0;
	if ( text ) {
		while ( i < text.length ) {
			c=text.charCodeAt(i);
			
			if (c < 128) {
				string+=String.fromCharCode(c);
				i+=1;
			} else if((c > 191) && (c < 224)) {
				c2=text.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i+=2;
			} else {
				c2=text.charCodeAt(i+1);
				c3=text.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i+=3;
			}
		}
	}
	return string;
} // decode_utf8

function URLEncode(text) {
	// 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 = "";
	if ( typeof(text)=='string' ) {
		for ( var i=0; i<text.length; i+=1 ) {
			var ch=text.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;
} // URLEncode

function URLDecode(encoded) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var text = "";
   if ( typeof(encoded)=='string' ) {
	   var i = 0;
	   while (i < encoded.length) {
	       var ch = encoded.charAt(i);
		   if (ch == "+") {
		       text += " ";
			   i++;
		   } else if (ch == "%") {
				if (i < (encoded.length-2) 
						&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
						&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
					text += unescape( encoded.substr(i,3) );
					i += 3;
				} else {
					alert( 'Bad escape combination near ...' + encoded.substr(i) );
					text += "%[ERROR]";
					i++;
				}
			} else {
			   text += ch;
			   i++;
			}
		} // while
	}
	return text;
} // URLDecode

function GetPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	var scrollTop=document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop?document.body.scrollTop:window.pageYOffset;
	var scrollLeft=document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft?document.body.scrollLeft:window.pageXOffset;
	
	if ( isNaN(scrollTop) ) scrollTop=0;
	if ( isNaN(scrollLeft) ) scrollLeft=0;

	return {"pageWidth":pageWidth, "pageHeight":pageHeight, "windowWidth":windowWidth, "windowHeight":windowHeight, "scrollLeft":scrollLeft, "scrollTop":scrollTop};
} // GetPageSize

function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} // AttachEvent

