//METODO QUE CREA EL OBJETO HTTPREQUEST
function loadXMLHttpRequest(url,functionResponseHTTPRequest,method){
	var httpRequest;
	if (window.ActiveXObject) // for IE
	{
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) // for other browsers
	{
		httpRequest = new XMLHttpRequest();
	}
	if (method == undefined){
		method = 'GET';
	}
	httpRequest.open(method, url, true);
	httpRequest.onreadystatechange = function() { processRequest(httpRequest,functionResponseHTTPRequest); } ;
	httpRequest.send(null);
}
//METODO QUE CREA EL OBJETO HTTPREQUEST
function sendXMLHttpRequest(url,functionResponseHTTPRequest, SendObject,method){
	var httpRequest;
	if (window.ActiveXObject) // for IE
	{
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) // for other browsers
	{
		httpRequest = new XMLHttpRequest();
	}
	httpRequest.open(method, url, true);
	httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	httpRequest.send(SendObject);
}
//METODO QUE ENVIA DATOS DE UN FORMULARIO POR HTTPREQUEST
function sendFormHttpRequest(url, functionResponseHTTPRequest, formName,method){
	var formParams = setQueryString(formName);
	sendXMLHttpRequest(url, functionResponseHTTPRequest, formParams,method);
	
}
//METODO QUE GERENERA UNA CADENA DE PARAMETROS DE UN FORMULARIO
function setQueryString( formName){
	var queryString="";
	var frm = document.getElementById( formName );
	var numberElements = frm.elements.length;
	for(var i = 0; i < numberElements; i++) {
		if(i < numberElements-1) {
		queryString += frm.elements[i].name+"="+
		encodeURIComponent(frm.elements[i].value)+"&";
		} else {
		queryString += frm.elements[i].name+"="+
		encodeURIComponent(frm.elements[i].value);
		}
	}
	return queryString;
}
//METODO DE RESPUESTA AL CAMBIO DE ESTADO DEL HTTPREQUEST Y EJECUTA LA FUNCION DEFINIDA POR EL USUARIO.
function processRequest(httpRequest,functionResponseHTTPRequest)
{
    if (httpRequest.readyState == 4)
    {
        try {
			if(httpRequest.status == 200)
			{
				functionResponseHTTPRequest(httpRequest);
			}
			else
			{
				var estado = httpRequest.status;
				var mensaje = httpRequest.statusText
				
				if ( estado == 0){
					mensaje = 'Esta Intentando ejecutar httpRequest localmente y no desde un servidor.'
				}
				var message = '<label>Estadode Servcicio :</label> '+estado+'<br><label>Descripcion :</label><br> '+mensaje;
				throw message;
			}
		}catch(err){
				createMessageError(err);	
		}
    }        
} 
// CREA UN ESTILO PREDETERMINADO PARA EL TOOLTIP
function createStyleMessageError() {
	//
	var estilo = document.createElement("span");
	estilo.id = 'styleWindow';
	estilo.style.display = 'none';
	var stylus = '&nbsp;ShowStyle'
	stylus += '	<style>.ErrorBarhttpRequest{'
	stylus +='	z-index:10000;'
	stylus +='	font-family: Arial, Helvetica, sans-serif;'
	stylus +='	font-weight: normal;'
	stylus +='	color: #FF0000;'
	stylus +='	background-color: #F0F0F0;'
	stylus +='	font-size: 12px;'
	stylus +='	margin: 5px;'
	stylus +='	padding: 5px;'
	stylus +='	}'
	stylus +='	.ErrorBarhttpRequest label{'
	stylus +='		font-weight: bold;	'
	stylus +='		}'
	stylus +='	</style>';
	estilo.innerHTML = stylus;
	document.body.appendChild(estilo);
}

function createMessageError(err){
	var message = '';
	if (typeof(err) == 'object'){
		for(var prop in err){
			message += '<label>'+prop +':</label> '+err[prop]+'<br>';
		}
	}else{
		message = err;	
	}
	
	createStyleMessageError();
	var messageError = document.createElement('div');
	messageError.className = 'ErrorBarhttpRequest';
	messageError.name = 'ErrorLogs';
	messageError.id = 'ErrorLogs';
	messageError.innerHTML = message;
	document.body.appendChild(messageError);
	
}
//IMPLEMENTACION DE XPATH PARA MOZILA version 3.0.
if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "Para XML Elements";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "Para Elementos XML Solamente";}
	}

}