var Utf8 = {
 
	// public method for url encoding
	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;
	},
 
	// public method for url decoding
	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 AJAX(options)
	{
    options=
		{
        type: options.type||'html',
        url: options.url||'',
        params: options.params||'',
        timeout: options.timeout||5000,
        onTimeout: options.onTimeout||function(){},
        onComplete: options.onComplete||function(){},
        onError: options.onError||function(){},
        onSuccess: options.onSuccess||function(){},
        onCreateXmlHttpError: options.onCreateXmlHttpError||function(){},
        disableAllElements: options.disableAllElements||false,
        progressBar: options.progressBar||false,
        progressBarCustomMessage: options.progressBarCustomMessage||''
		};
		
	if(options.disableAllElements) DisableContent(0,'FFFFFF','XXXDUMMYXXX');

    var xmlHttp=GetXmlHttpRequestObject(options.onCreateXmlHttpError);

	if (options.progressBar)
		{
		CreateProgressBarDiv(options.progressBarCustomMessage);
		ProgressBarValue(40);
		}

    xmlHttp.open('POST',options.url,true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.setRequestHeader("charset", "utf-8");
    
    if(xmlHttp.overrideMimeType)
		{
		switch(options.type)
			{
			case 'html':
			case 'text/html':
				xmlHttp.overrideMimeType('text/html');
				break;
			case 'xml':
			case 'text/xml':
				xmlHttp.overrideMimeType('text/xml');
				break;
			default:
				break;
			}
		xmlHttp.setRequestHeader('Connection','close');
		}
		
    var timeoutLength = options.timeout;
    var requestDone=false;

    setTimeout(function(){requestDone=true;},timeoutLength);

    xmlHttp.onreadystatechange = function()
		{
		if (options.progressBar)
			{
			ProgressBarValue((xmlHttp.readyState+1)*40);
			}
		
		if(xmlHttp.readyState==4)
			{
			if(!requestDone)
				{
				if(httpSuccess(xmlHttp))
					{
					options.onSuccess(httpData(xmlHttp,options.type));
					}
				else
					{
					if (options.progressBar) DestroyProgressBarDiv();
					options.onError('Error '+xmlHttp.status+': '+xmlHttp.statusText);
					}
				}
			else
				{
				options.onTimeout();
				}
        
			xmlHttp = null;
			if (options.progressBar) DestroyProgressBarDiv();
			
			options.onComplete();
			
			if(options.disableAllElements) HideCDivs();
			}
		};

    xmlHttp.send(options.params);

    function httpSuccess(r)
		{
        try
			{
            return !r.status&&location.protocol=='file:'||(r.status>=200&&r.status<300)||r.status==304||navigator.userAgent.indexOf('Safari')>=0&&typeof r.status=='undefined';
			}
		catch(e){}

        return false;
		}

    function httpData(r,type)
		{
        var ct=r.getResponseHeader('content-type');
        var data=!type&&ct&&ct.indexOf('xml')>=0;
        data=type=='xml'||data?r.responseXML:r.responseText;
		
        if (type=='script') eval.call(window,data);

        return data;
		}
	}
	
function GetXmlHttpRequestObject(pOnError)
	{
	var onError=pOnError||function(){};
	var xmlHttp;
	try
		{
        xmlHttp=new XMLHttpRequest();
		}
	catch(e)
		{
    	try
    		{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    		}
    	catch(e)
    		{
			try
				{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch(e)
				{
				onError();
				}
			}
		}
	return xmlHttp;
	}
