/*
$Id: ajax.js,v 1.16 2011/12/15 13:55:17 gulpi Exp $

Utility for Ajax Communication
*/

var HttpLoader = function()
{}

HttpLoader.prototype.GetHttpRequest = function()
{
	// Gecko / IE7
	if ( typeof(XMLHttpRequest) != 'undefined' )
		return new XMLHttpRequest() ;

	// IE6
	try { return new ActiveXObject( 'Msxml2.XMLHTTP' ) ; }
	catch(e) {}

	// IE5
	try { return new ActiveXObject( 'Microsoft.XMLHTTP' ) ; }
	catch(e) {}

	return null ;
}

HttpLoader.prototype.LoadUrl = function( urlToCall, asyncFunctionPointer )
{
	var oFCKXml = this ;
	var bAsync = ( typeof(asyncFunctionPointer) == 'function' ) ;
	var oXmlHttp = this.GetHttpRequest() ;
	oXmlHttp.onreadystatechange = function()
	{
		if ( oXmlHttp.readyState == 4 )
		{
			if ( oXmlHttp.responseXML == null || oXmlHttp.responseXML.firstChild == null)
			{
				if ( bAsync && (oXmlHttp.status == 200 || oXmlHttp.status == 304) ) {
					alert( 'The server didn\'t send back a proper XML response.\r\n\r\n' +
							'Requested URL: ' + urlToCall + '\r\n' +
							'readyState: ' + oXmlHttp.readyState  + '\r\n' +
							'oXmlHttp.responseXML: ' + oXmlHttp.responseXML + '\r\n' +
							'Response text:\r\n' + oXmlHttp.responseText
							  ) ;
					return ;
				}
				else {
					alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')'  + ' (' + oXmlHttp.readyState + ')' ) ;					
					return;
				}
			}
			oFCKXml.DOMDocument = oXmlHttp.responseXML ;
			if ( bAsync && (oXmlHttp.status == 200 || oXmlHttp.status == 304) ) {
				asyncFunctionPointer(oFCKXml);
			}
			else if ( !bAsync && (oXmlHttp.status == 200 || oXmlHttp.status == 304) ) {
				
			}
			else
				alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')'  + ' (' + oXmlHttp.readyState + ')' ) ;
		}
	}
	
	oXmlHttp.open( "GET", urlToCall, bAsync ) ;
	oXmlHttp.send( null ) ;
}

HttpLoader.prototype.PostUrl = function( urlToCall, asyncFunctionPointer, data )
{
	var oFCKXml = this ;
	var bAsync = ( typeof(asyncFunctionPointer) == 'function' ) ;
	var oXmlHttp = this.GetHttpRequest() ;
	oXmlHttp.onreadystatechange = function()
	{
		if ( oXmlHttp.readyState == 4 )
		{
			if ( oXmlHttp.responseXML == null || oXmlHttp.responseXML.firstChild == null)
			{
				if ( bAsync && (oXmlHttp.status == 200 || oXmlHttp.status == 304) ) {
					alert( 'The server didn\'t send back a proper XML response.\r\n\r\n' +
							'Requested URL: ' + urlToCall + '\r\n' +
							'readyState: ' + oXmlHttp.readyState  + '\r\n' +
							'oXmlHttp.responseXML: ' + oXmlHttp.responseXML + '\r\n' +
							'Response text:\r\n' + oXmlHttp.responseText
							  ) ;
					return ;
				}
				else {
					tmpStatusText = "";
					if (oXmlHttp.statusText) tmpStatusText = oXmlHttp.statusText;
					tmpStatus = "";
					if (oXmlHttp.status) tmpStatus = oXmlHttp.status;
					tmpReadyState = "";
					if (oXmlHttp.readyState) tmpReadyState = oXmlHttp.readyState;
					alert( 'XML request error: ' + tmpStatusText + ' (' + tmpStatus + ')'  + ' (' + tmpReadyState + ')' ) ;					
					return;
				}
			}
			oFCKXml.DOMDocument = oXmlHttp.responseXML ;
			if ( bAsync && (oXmlHttp.status == 200 || oXmlHttp.status == 304) ) {
							asyncFunctionPointer(oFCKXml);
			}
			else if ( !bAsync && (oXmlHttp.status == 200 || oXmlHttp.status == 304) ) {
				
			}
			else
				alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
		}
	}
	
	oXmlHttp.open('POST', urlToCall, bAsync);
	oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oXmlHttp.setRequestHeader("Content-length", data.length);
	oXmlHttp.setRequestHeader("Connection", "close");
	oXmlHttp.send( data ) ;
}

HttpLoader.prototype.PostUrlHTML = function( urlToCall, asyncFunctionPointer, data )
{
	var oFCKXml = this ;
	var bAsync = ( typeof(asyncFunctionPointer) == 'function' ) ;
	var oXmlHttp = this.GetHttpRequest() ;
	oXmlHttp.onreadystatechange = function()
	{
		if ( oXmlHttp.readyState == 4 )
		{
			if ( bAsync ) {
				asyncFunctionPointer(oXmlHttp);
			}
		}
	}
	
	oXmlHttp.open('POST', urlToCall, bAsync);
	oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oXmlHttp.setRequestHeader("Content-length", data.length);
	oXmlHttp.setRequestHeader("Connection", "close");
	oXmlHttp.send( data ) ;
}

HttpLoader.prototype.GetUrlHTML = function( urlToCall, asyncFunctionPointer )
{
	var oFCKXml = this ;
	var bAsync = ( typeof(asyncFunctionPointer) == 'function' ) ;
	var oXmlHttp = this.GetHttpRequest() ;
	oXmlHttp.onreadystatechange = function()
	{
		if ( oXmlHttp.readyState == 4 )
		{
			if ( bAsync) {
				asyncFunctionPointer(oXmlHttp);
			}
		}
	}
	
	oXmlHttp.open( "GET", urlToCall, bAsync ) ;
	oXmlHttp.send( null ) ;
}

/**
 * 
 */
HttpLoader.prototype.replaceUniCodeEntities = function( s ) {
	var result = escape(s);
	var pos = result.indexOf("%u");
	while(pos != -1) {
		if (result.length > pos+6) {
			hex = result.substring(pos+2, pos+6);
			bin = parseInt(hex,16);
		
			pre = result.substring(0,pos);
			post = result.substring(pos+6);
			result = pre + "%26#" + bin + ";" + post;
			pos = result.indexOf("%u");
		}
		else {
			pos = -1;
		}
	}
	
	result = result.replace(/\+/g, "%2B");
	return result;
}

HttpLoader.prototype.Form2Text = function( form )
{
	var result = "";

	if (form && form.elements && form.elements.length > 0) {
			var elements = form.elements;
			var count = 0;
			for(var i=0;i<elements.length;i++){
				var e=elements[i];
				if(e.disabled) {
					continue;
				}
				switch(e.type) {
					case "text":
					case "password":
					case "hidden":
					case "textarea":
						if (count>0) result += "&";
						result += e.name + "=" + this.replaceUniCodeEntities(e.value);
						break;
					case "select-one":
						if(e.selectedIndex>=0) {
							if (count>0) result += "&";
							result += e.name + "=" + this.replaceUniCodeEntities(e.options[e.selectedIndex].value);
						}
						break;
					case "select-multiple":
						for(var j=0;j<e.options.length;j++) {
							if(e.options[j].selected) {
								if (count>0) result += "&";
								result += e.name + "=" + this.replaceUniCodeEntities(e.options[j].value);
							}
						}
						break;
					case "checkbox":
					case "radio":
						if(e.checked)	{
							if (count>0) result += "&";
							result += e.name + "=" + this.replaceUniCodeEntities(e.value);
	
						}
						break;				
				}
				
				count++;
			}
	}
	
	return result;
}

var AjaxResult = function(oFCKXml) {
	this.oFCKXml = oFCKXml;
	this.rootElement = null;
	
AjaxResult.prototype.init = function(root_element_name) {
	this.rootElement = this.oFCKXml.DOMDocument.getElementsByTagName(root_element_name);
}

AjaxResult.prototype.getItem = function(root_idx,element_idx) {
	var result = null;
	if (this.rootElement && this.rootElement.length>0 && this.rootElement[root_idx].childNodes && this.rootElement[root_idx].childNodes.length>element_idx) {
		result = this.rootElement[root_idx].childNodes[element_idx];
	}
	
	return result;
}

AjaxResult.prototype.getNumOfItems = function(root_idx) {
	var result = 0;
	if (this.rootElement && this.rootElement.length>0 && this.rootElement[root_idx].childNodes) {
		result = this.rootElement[root_idx].childNodes.length;
	}
	
	if (result)
		result = parseInt(result);
	
	return result;
}
	
AjaxResult.prototype.GetIntAttributeByName = function(in_item,in_key) {
	var result = 0;
	var result_str = "";
	if (in_item && in_item.attributes && in_item.attributes[in_key] && in_item.attributes[in_key].value) {
		result_str = in_item.attributes[in_key].value;
		result = parseInt(result_str);
	}
	else if (in_item) {
		result_str = in_item.getAttribute(in_key);
		result = parseInt(result_str);
	}
	
	return result;
}

AjaxResult.prototype.GetAttributeByName = function(in_item,in_key) {
	var result = "";
	if (in_item && in_item.attributes && in_item.attributes[in_key] && in_item.attributes[in_key].value) {
		result = in_item.attributes[in_key].value;
	}
	else if (in_item) {
		result = in_item.getAttribute(in_key);
	}
	
	return result;
}

AjaxResult.prototype.GetItemText = function(in_item) {
	var result = in_item.textContent;
	if (navigator.appVersion.indexOf("MSIE") != -1) {
	//if (Prototype.Browser.IE) {
		result = in_item.childNodes[0].text;
	}
	
	return result;
}
}
