var XMLHTTPUtil;
(function() {
	if (XMLHTTPUtil == null) {
		XMLHTTPUtil = {};


	function getXMLHTTP(){
		var A=null;
		try{
			A=new ActiveXObject("Msxml2.XMLHTTP")
		}catch(e){
			try{
				A=new ActiveXObject("Microsoft.XMLHTTP")
			} catch(oc){
				A=null;				
			}
		}
		if(!A && typeof XMLHttpRequest != "undefined") {
			A=new XMLHttpRequest()
		}
		return A
	}
	
	
	XMLHTTPUtil.createRPCCallStringFunction = function (uriPrefix, beforeFunc, afterFunc) {		
		var _xmlHttp = null;
		if (uriPrefix == null)
			uriPrefix = '';
		var callServerFunc = function callServer(uri, func, data){
			if(_xmlHttp&&_xmlHttp.readyState!=0){
					if (afterFunc)
						afterFunc();
					_xmlHttp.abort()
				}
				_xmlHttp=getXMLHTTP();
				if(_xmlHttp){
					var postfix = '?';
					if (uri.indexOf('?') != -1)
						postfix = '&';
					
					postfix += 'cachetimestamp=' + new Date().getTime();
					_xmlHttp.open("POST",uriPrefix + uri + postfix, true);
					_xmlHttp.setRequestHeader('Accept-Charset','ISO-8859-1');
					_xmlHttp.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
					_xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					_xmlHttp.onreadystatechange=function() {
						if(_xmlHttp.readyState==4&&_xmlHttp.responseText) {
							try {
								func(_xmlHttp.responseText);
							} catch(ex) {
								throw ('Error executing process function\n\n' + ex + '\n\n' +func);
							}
							if (afterFunc)
								afterFunc();
							
						}
					};
					if (beforeFunc)
						beforeFunc();
					var dataS = null;
					if (data) {
						var amp ='';
						dataS = '';
						dataS=data;
					}
					_xmlHttp.send(dataS);
				}
			}
		
		return callServerFunc;
		
	}
	
	
	
	
	
	XMLHTTPUtil.createRPCCallFunction = function (uriPrefix, beforeFunc, afterFunc) {
		
		var _xmlHttp = null;
		if (uriPrefix == null)
			uriPrefix = '';
		var callServerFunc = 
			function callServer(uri, func, data){
				
				if(_xmlHttp&&_xmlHttp.readyState!=0){
					if (afterFunc)
						afterFunc();
					_xmlHttp.abort()
				}
				_xmlHttp=getXMLHTTP();
				if(_xmlHttp){
					var postfix = '?';
					if (uri.indexOf('?') != -1)
						postfix = '&';
					
					postfix += 'cachetimestamp=' + new Date().getTime();

					_xmlHttp.open("POST",uriPrefix + uri + postfix, true);
								
					_xmlHttp.setRequestHeader('Accept-Charset','ISO-8859-1');
					_xmlHttp.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
					_xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					_xmlHttp.onreadystatechange=function() {

						if(_xmlHttp.readyState==4&&_xmlHttp.responseText) {
							try {
								func(_xmlHttp.responseText);
							} catch(ex) {
								throw ('Error executing process function\n\n' + ex + '\n\n' +func);
							}
							if (afterFunc)
								afterFunc();
							
						}
					}
					;
					
					if (beforeFunc)
						beforeFunc();
					var dataS = null;
					if (data) {
						var amp ='';
						dataS = '';
						for (var x in data) {
							var val = data[x];
							if (val.push && val.join) {
								for (var i=0; i<val.length; i++) {
									dataS+= amp + x + '=' + escape(data[x][i]);
								}
							} else {
								dataS+= amp + x + '=' + escape(data[x]);
							}
							amp = '&';
						}
					}
					
					_xmlHttp.send(dataS);
				}
			}
		
		return callServerFunc;
		
	}


	}
}) ();

/**
 * showSystemError
 * @author fmolina.
 * Description: Muestra mensaje de error general cuando la aplicación lanza una exception.
 */
 function showSystemError() {
 	alert('Se ha producido un error interno.\nIntente de nuevo.');
 }

/**
 * showErrorMessage
 * @param {String} message 
 */
 function showErrorMessage(message) {
 	alert(message);
 }
 
  function makeRequest(url,type,function_handler,params) {
	var http_request = false;
	var doc = null;
	
   	if(window.XMLHttpRequest){ // Mozilla, Safari,...
    	http_request = new XMLHttpRequest();
        if(http_request.overrideMimeType){
        	http_request.overrideMimeType('text/xml');
            // Ver nota sobre esta linea al final
        }
	}else if(window.ActiveXObject){ // IE
        try{
        	http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
        	try{
            	http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){}
        }
  	}
    if(!http_request){
    	alert('Falla :( No es posible crear una instancia XMLHTTP');
        return false;
    }
	//http_request.async = false;
    http_request.onreadystatechange = function (){
    				
	    if(http_request.readyState == 4){
	    	if(http_request.status == 200){
				if(type == 'xml') 
					doc = http_request.responseXML;
				else
					doc = http_request.responseText;
				if(function_handler != null) function_handler(doc,params);
			}else{
            	alert('Hubo problemas con la petición. Error: '+http_request.status);
            }
		}

	}

//	prompt("debug",url);
	var index=url.indexOf('?');
	if(index!=-1){
		http_request.open('POST', url.substring(0,index), true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send(url.substring(index+1));
	}else{
		http_request.open('POST', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send(null);
	}
}