//Basado en:
//HTMLHttpRequest v1.0 beta2 (c) 2001-2005 Angus Turnbull, TwinHelix Designs http://www.twinhelix.com
//Licensed under the CC-GNU LGPL, version 2.1 or later: http://creativecommons.org/licenses/LGPL/2.1/


// Some common event API code I use. Syntax:
//   addEvent(object_reference, 'name_of_event', function_reference, legacy);
// 'name_of_event' is without the 'on' prefix, e.g. 'load', 'click' or 'mouseover'.
// 'legacy' disables addEventListener if true.
// You can add multiple events to one object, and in MSIE all are removed onunload.

if (typeof addEvent != 'function')
{
 var addEvent = function(o, t, f, l)
 {
  var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
  if (o[d] && !l) return o[d](t, f, false);
  if (!o._evts) o._evts = {};
  if (!o._evts[t])
  {
   o._evts[t] = o[n] ? { b: o[n] } : {};
   o[n] = new Function('e',
    'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
     'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
     '} return r');
   if (t != 'unload') addEvent(window, 'unload', function() {
    removeEvent(rO, rT, rF, rL);
   });
  }
  if (!f._i) f._i = addEvent._i++;
  o._evts[t][f._i] = f;
 };
 addEvent._i = 1;
 var removeEvent = function(o, t, f, l)
 {
  var d = 'removeEventListener';
  if (o[d] && !l) return o[d](t, f, false);
  if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
 };
}

function cancelEvent(e, c)
{
 e.returnValue = false;
 if (e.preventDefault) e.preventDefault();
 if (c)
 {
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
 }
};

// SYNTAX INSTRUCTIONS for HTMLHttpRequest:
//
// function callback_function(DOMDocument) { /* Parse DOM document here */ }
//
// var objectName = new HTMLHttpRequest('objectName', callback_function);
//
// Create an instance of an HTMLHttpRequest object, and pass its own name as a string,
// a reference to a callback function, and a pathname to a blank HTML document.
// The callback function will be called on load/submit completion with parameters:
//  1) A reference to the loaded DOM document (which you may then parse).
//  2) The requested URI.
// NOTE: All requested documents must reside on the same domain as this document!
//
// Available methods are:
//
// objectName.load('file.html');
//
// This will issue a GET request to the server to return a named file.
//
// objectName.submit(form_reference, event_object);
//
// This will capture a form's submission and redirect it to a background POST or GET
// request, respecting the form's 'method' attribute and its 'action' URI.
// Pass it a reference to the form's DOM node, and the event object from the submittal.
// Note that the form must be *already* in the process of submission when this is called.
// It is therefore suggested that you call it from within an ONSUBMIT handler on a form.
var threads = [];


function HTMLHttpRequest(callback, oParams, forceIframeUse) { with (this)
{
 window._iObjCount |= 0;
 this.numero = _iObjCount;
 this.Tipo = 0;
 this.myName = 'Objeto_' + window._iObjCount++;
 threads[_iObjCount-1] = this;
 this.callback = callback;
 this.params = oParams;
 // 'xmlhttp': Our preferred request object. Accepted wherever HTML is sold.
 this.xmlhttp = null;
 // 'iframe' is our fallback: a reference to a dynamically created IFRAME buffer.
 this.iframe = null;
 window._ifr_buf_count |= 0;
 this.iframeID = 'iframebuffer' + window._ifr_buf_count++;
 // A loading flag, set to the requested URI when loading.
 this.loadingURI = '';

 // Attempt to init an XMLHttpRequest object where supported.
 if (window.XMLHttpRequest && !window.ActiveXObject) xmlhttp = new XMLHttpRequest();
 //Para IE, si puede usar el activeX, va con ese
 if (window.ActiveXObject)
 {
  try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(err)
  {
    xmlhttp = null; //No tiene acceso a crear activex. Voy a ir con la ultima que me queda... el iframe.
  }
 }

 // Si hace un submit, deberia ir siempre con iframe (IE seguro porque el activex no tiene toda la funcionalidad
 // necesaria). Pero tambien voy con el resto porque a la funcion que levanta los valores para submitear no
 // le tengo toda la fé
 if( (typeof(forceIframeUse)!="undefined") && forceIframeUse==true)
 {
  xmlhttp = null;
 }

 if (!xmlhttp)
 {

  // In MSIE or browsers with no XMLHttpRequest support: fallback to IFRAMEs buffers.
  // I'm using IFRAMEs in MSIE due to XMLHTTP not parsing text/html to a DOM.
  // Also used in IE5/Mac, Opera 7.x, Safari <1.2, some Konqueror versions, etc.
  if (document.createElement && document.documentElement &&
   (window.opera || navigator.userAgent.indexOf('MSIE 5.0') == -1))
  {
   var ifr = document.createElement('iframe');
   ifr.setAttribute('id', iframeID);
   ifr.setAttribute('name', iframeID);
   // Hide with visibility instead of display to fix Safari bug.
   ifr.style.visibility = 'hidden';
   ifr.style.position = 'absolute';
   ifr.style.width = ifr.style.height = ifr.borderWidth = '0px';
   iframe = document.getElementsByTagName('body')[0].appendChild(ifr);

  }
  else if (document.body && document.body.insertAdjacentHTML)
  {
   // IE5.0 doesn't like createElement'd frames (won't script them) and IE4 just plain
   // doesn't support it. Luckily, this will fix them both:
   document.body.insertAdjacentHTML('beforeEnd', '<iframe name="' + iframeID +
    '" id="' + iframeID + '" style="display: none"></iframe>');
  }
  // This helps most IE versions regardless of the creation method:
  if (window.frames && window.frames[iframeID]) iframe = window.frames[iframeID];
  iframe.name = iframeID;
 }
 return this;
}};

HTMLHttpRequest.prototype.xmlhttpSend = function(uri, formStr) { with (this)
{

 // Use XMLHttpRequest to asynchronously open a URI, and optionally POST a provided
 // form string if any (otherwise, performs a GET).
 xmlhttp.open(formStr ? 'POST' : 'GET', uri, true);
 var self = this;
 xmlhttp.onreadystatechange = function() {
 
  if (xmlhttp.readyState==4 && xmlhttp.responseText)
  {
   var sDoc;

   if (self.Tipo==0) //tipo=1. Quiero obtener texto
   {
     sDoc = xmlhttp.responseText
   }
   else if(self.Tipo==1) //tipo=1. Quiero levantar un XML (y tratarlo como tal)
   {
     sDoc = xmlhttp.responseXML;
   }
   else{ //tipo = 2. Si viene un XML mejor... sino voy con el HTML
    if (xmlhttp.responseXML)
      sDoc = xmlhttp.responseXML;
    else
      sDoc = xmlhttp.responseText;
   }


   // If you are getting an error where 'doc' is null in your own code, try changing
   // the MIME type returned by the server: setting it to text/xml usually works well!
   if (callback) callback(sDoc, self.params);
   loadingURI = '';
  }
 };

 if (formStr && xmlhttp.setRequestHeader)
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset utf-8');

 //TODO: SACAR ESTE UNESCAPE CUANDO FIREFOX 3 ARREGLE EL BUG DE ENCODI
 //if(navigator.userAgent.match("Firefox/3")) 
//	 formStr = unescape(formStr);
 
 xmlhttp.send(formStr);
 loadingURI = uri;
 return true;
}};


HTMLHttpRequest.prototype.iframeSend = function(uri, formRef) { with (this)
{
 // Routes a request through our hidden IFRAME. Pass a URI, and optionally a
 // reference to a submitting form. Requires proprietary 'readyState' property.
 if (!document.readyState) return false;

 // Opera fix: force the frame to render before setting it as a target.
 if (document.getElementById) var o = document.getElementById(iframeID).offsetWidth;



 // Either route the form submission to the IFRAME (easy!)...
 if (formRef) {
  formRef.setAttribute('target', iframeID);
  formRef.submit();
 }
 else
 {
  // ...or load the provided URI in the IFRAME, checking for browser bugs:
  // 1) Safari only works well using 'document.location'.
  // 2) Opera needs the 'src' explicitly set!
  // 3) Konqueror 3.1 seems to think ifrDoc's location = window.location, so watch that too.
  var ifrDoc = iframe.contentDocument || iframe.document;

  if (!window.opera && ifrDoc.location &&
   ifrDoc.location.href != location.href)
   {ifrDoc.location.href= uri;
    //ifrDoc.location.replace(uri);
  }
  else iframe.src = uri;
 }

 // Either way, set the loading flag and start the readyState checking loop.
 // Opera likes a longer initial timeout with multiple frames running...
 loadingURI = uri;
 setTimeout('threads[' + this.numero + '].iframeCheck()', (window.opera ? 250 : 100));
 return true;
}};

HTMLHttpRequest.prototype.iframeCheck = function()
{
 with (this)
 {
  // Called after a timeout, periodically calls itself until the load is complete.
  // Get a reference to the loaded document, using either W3 contentDocument or IE's DOM.
  doc = iframe.contentDocument || iframe.document;

  // Check the IFRAME's .readyState property and callback() if load complete.
  // IE4 only seems to advance to 'interactive' so let it proceed from there.
  var il = iframe.location, dr = doc.readyState;

  //if ((il && il.href ? il.href.match(loadingURI) : 1) && (dr == 'complete' || (!document.getElementById && dr == 'interactive')))
  if ((il && il.href ? il.href.match(loadingURI.replace("\?", "\\?")) : 1) && (dr == 'complete' || (!document.getElementById && dr == 'interactive')))
  {

   if (this.Tipo==0)
   {
    //if (callback) callback( doc.body.innerText, this.params);
    //callback(cbDoc,(cbDoc.innerHTML||(cbDoc.body?cbDoc.body.innerHTML:'')),loadingURI);
    if (callback){
      callback( (doc.innerHTML || (doc.body ? doc.body.innerHTML : '')), this.params);
    }
   }
   else if (this.Tipo==1)
   {
    if (callback){
    	callback((doc.documentElement || doc), this.params);
    }
//      if (callback) callback(cbDoc, (cbDoc.innerHTML || (cbDoc.body ? cbDoc.body.innerHTML : '')), loadingURI);
   }
   else //tipo=2
   {
        this.params.docDOM = (doc.documentElement || doc);
        this.params.docText= (doc.innerHTML || (doc.body ? doc.body.innerHTML : ''));
     if (callback){
    	 callback(this.params.docText, this.params);
     }
   }

//    if (callback) callback((doc.documentElement || doc), this.params);
////      if (callback) callback(cbDoc, (cbDoc.innerHTML || (cbDoc.body ? cbDoc.body.innerHTML : '')), loadingURI);


   //Esto aplica cuando no se usa form
   if (this.Tipo!=2){
    var oIframeBuffer = document.getElementById(this.iframeID);
    oIframeBuffer.parentNode.removeChild( oIframeBuffer );
   }
   loadingURI = '';
  }
  else
  {
     setTimeout('threads[' + this.numero + '].iframeCheck()', 150);
  }
}};

HTMLHttpRequest.prototype.submit = function(formRef, evt) { with (this)
{
 this.Tipo = 2;
 // Pass a reference to a (submitting) form DOM node and an event object.

 evt = evt || window.event;
 if (!formRef || (!xmlhttp && !iframe)) return false;

 // Retrieve form information then decide what to do with it.
 var method = formRef.getAttribute('method');
 var uri = formRef.getAttribute('action');

 if (method && method.toLowerCase() == 'post')
 {

  // Send the URI and either a parsed form or a form reference to the transports.
  // Note we only cancel form for XMLHTTP, as IFRAMEs still need it to submit.
  if (xmlhttp) { cancelEvent(evt); return xmlhttpSend(uri, parseForm(formRef)) }
  else if (iframe) return iframeSend(uri, formRef);
  else return false;
 }
 else
 {
  // For GET requests, append ?querystring or &querystring to the GET uri and
  // forward it to the load() function. Always cancel form submit().
  cancelEvent(evt);
  return load(uri + (uri.indexOf('?') == -1 ? '?' : '&') + parseForm(formRef));
 }
}};

HTMLHttpRequest.prototype.parseForm = function(form) { with (this)
{
 // Parses a form DOM reference to an escaped string suitable for GET/POSTing.

 var str = '', gE = 'getElementsByTagName', inputs = [
  (form[gE] ? form[gE]('input') : form.all ? form.all.tags('input') : []),
  (form[gE] ? form[gE]('select') : form.all ? form.all.tags('select') : []),
  (form[gE] ? form[gE]('textarea') : form.all ? form.all.tags('textarea') : [])
 ];

 // Loop through each list of tags, constructing our string.
 for (var i = 0; i < inputs.length; i++)
  for (j = 0; j < inputs[i].length; j++)

   if (inputs[i][j] && !inputs[i][j].disabled)
   {
    if ((inputs[i][j].type  == 'radio' || inputs[i][j].type == 'checkbox') &&  !(inputs[i][j].checked)) continue;
    var plus = '++'.substring(0,1); // CodeTrim fix.
//    if (inputs[i][j].value=='' && (inputs[i][j]
    str += escape(inputs[i][j].getAttribute('name')).replace(plus, '%2B') +
     '=' + escape(inputs[i][j].value).replace(plus, '%2B') + '&';
   }

 // Strip trailing ampersand, because we can :)
 return str.substring(0, str.length - 1);
}};

// *** PUBLIC METHODS ***

HTMLHttpRequest.prototype.load = function(uri) { with (this)
{
 // Call with a URI to load a plain text document.
 this.Tipo= 0;
 if (!uri || (!xmlhttp && !iframe)) return false;

 // Route the GET through an available transport layer.
 if (xmlhttp) return xmlhttpSend(uri, '');
 else if (iframe) return iframeSend(uri, null);
 else return false;
}};


HTMLHttpRequest.prototype.loadXML = function(uri) { with (this)
{
 // Call with a URI to load a xml document.
 this.Tipo= 1;
 if (!uri || (!xmlhttp && !iframe)) return false;
 // Route the GET through an available transport layer.
 if (xmlhttp) return xmlhttpSend(uri, '');
 else if (iframe) return iframeSend(uri, null);
 else return false;
}};


function loadContent(sUrl,idTarget){
	target = document.getElementById(idTarget);
	target.style.visibility='visible';
	target.innerHTML = 'Cargando...';
    loadInto(sUrl,idTarget);
}
function copyContentAndExecute(sResult, oParam)
{
  copyContent(sResult,oParam);
  var docText;
  if (oParam.docText)
    docText= oParam.docText;
  else
    docText = sResult  
  oParam.callback(docText, oParam);
}

function loadIntoAndExecuteCallback(sUrl, otarget, oCallback, oParams){


  var oParam = { destId:otarget};
  oParam.callback = oCallback;
  oParam.oParams= oParams;
  var oXML=new HTMLHttpRequest(copyContentAndExecute,oParam);
  oXML.load(sUrl, oParam);
}

//TODO: refactor y extraer funcionalidad comun
function submitAndExecute(oform,event, oCallBack, oParams){
  //Agrego a la coleccion los que va a necesitar si quiere acceder al contenido.
  // TIENE QUE VENIR oPARAMS Y SER UN OBJETO
  oParams.frm = oform;
  oParams.callback=oCallBack;

  if(document.readyState){
      var oXML=new HTMLHttpRequest(oCallBack,oParams,true);
     oXML.submit(oform, event);
  }
  else{
   //FF no implementa readyState (IE y Opera si) asique hay que manejar el submit de otra
   // forma, sino no se puede saber cuando se termino de cargar el frame...ergo, no uso iframes
   var oXML=new HTMLHttpRequest(oCallBack,oParams);
   oXML.submit(oform, event);
 }
}

function submitIntoAndExecute(oform,otarget,event, oCallBack, oParams){
  //Agrego a la coleccion los que va a necesitar si quiere acceder al contenido.
  // TIENE QUE VENIR oPARAMS Y SER UN OBJETO
  oParams.frm = oform;
  oParams.destId=otarget;
  oParams.callback=oCallBack;

  if(document.readyState){
     var oXML=new HTMLHttpRequest(copyContentAndExecute,oParams,true);
     oXML.submit(oform, event);
  }
  else{
   //FF no implementa readyState (IE y Opera si) asique hay que manejar el submit de otra
   // forma, sino no se puede saber cuando se termino de cargar el frame...ergo, no uso iframes
   var oXML=new HTMLHttpRequest(copyContentAndExecute,oParams);
   oXML.submit(oform, event);
 }
}

function submitContent(formId, targetId) {
	var target = document.getElementById(targetId);
	//target.style.visibility='visible';
	target.innerHTML = 'Cargando...';
	
	var form = document.getElementById(formId);
    submitInto(form, targetId, new function(){});
}

//TODO
function submitInto(oform){

}
function submitInto(oform,targetId,event) {

  var oParam = { frm:oform , destId:targetId};
 if(document.readyState){
   var oXML=new HTMLHttpRequest(copyContent,oParam,true);
   oXML.submit(oform, event);
 }
 else{
   //FF no implementa readyState (IE y Opera si) asique hay que manejar el submit de otra
   // forma, sino no se puede saber cuando se termino de cargar el frame...ergo, no uso iframes
   var oXML=new HTMLHttpRequest(copyContent,oParam);
   oXML.submit(oform, event);
 }
}

function loadInto(sUrl,otarget) {

  var oParam = { destId:otarget};
  var oXML=new HTMLHttpRequest(copyContent,oParam);
  oXML.load(sUrl, oParam);
}

function loadIntoWithScriptExecution(sUrl,otarget) {

  var oParam = { destId:otarget};
  var oXML=new HTMLHttpRequest(copyContent,oParam,true);
  oXML.load(sUrl, oParam);
}

function copyContent(sResult, oParam)
{

  docDOM = oParam.docDOM;
  if (oParam.docText)
    docText= oParam.docText;
  else
    docText = sResult

  destId= oParam.destId;

 // This copies the <body> content of the loaded DOM document into an element in the
 // current page with a specified ID.

 // Retrieve references to the loaded BODY. You might want to modify this so that you
 // load content from <div id="content"> within the loaded document perhaps?
 var src = docDOM ?
  (docDOM.getElementsByTagName ?
   docDOM.getElementsByTagName('body')[0] :
    (docDOM.body ? docDOM.body : null) ) :
     null;
 var dest = document.getElementById ? document.getElementById(destId) :
  (document.all ? document.all[destId] : null);

 if (!destId || (!src && !docText)) return;

 // innerHTML is still a little more reliable than importNode across browsers.

 if (src && src.innerHTML) dest.innerHTML = src.innerHTML;
 else if (src && document.importNode)
 {
  while (dest.firstChild) dest.removeChild(dest.firstChild);
  for (var i = 0; i < src.childNodes.length; i++)
   dest.appendChild(document.importNode(src.childNodes.item(i), true));
 }
	else if (docText)
	{

     //Hack. Deberia funcionar el regexp de abajo...
	 //TODO: chequear porque ese match no funciona con firefox.
	 var iFrom = docText.indexOf("<body>");
	 var iTo = docText.indexOf("</body>");
	 if(iFrom>0 && iTo>0){
	 	docText = docText.substring(iFrom+6, iTo);
	 }
//	 if (docText.match(/(<body>)(.*)(<\/body>)/i)) docText = RegExp.$2;
		// You might want to do some post-processing here if you are rendering
		// plain text in an XHTML document, for instance to keep line breaks:
		dest.innerHTML = docText;
	}
	refreshStyleSheet();
};

function cargarComboXML(sCombo, sItem, sNombre, sValor, iValorSeleccionado, oXml, bAccionGenerica, sTextoGenerico, sValorGenerico)
{
 if(sCombo!='' && document.getElementById(sCombo))
 {
  var oCombo = document.getElementById(sCombo);
  oCombo.length = 0;
  oCombo.selectedIndex = 0;
  var oNodos = oXml.getElementsByTagName(sItem);
  var x=0;

  for(x = 0; x<oNodos.length; x++ )
  {
   var oNodo = oNodos[x];
   //cargo el combo.
   var oItem = document.createElement("option");
   oItem.text = oNodo.getAttribute(sNombre);
   oItem.value = parseInt(oNodo.getAttribute(sValor));

   //oCombo.options[x] = oItem;
   oCombo.options.add(oItem);
   //selecciono opcion.
   if ((oItem.value == iValorSeleccionado) || (oItem.value==0 && x==0))
   {
    oCombo.selectedIndex = x;
   }
  }
  if (bAccionGenerica)
  {
   var oItem = document.createElement("option");
   oItem.text = sTextoGenerico;
   oItem.value = sValorGenerico;
   oCombo.options.add(oItem,0);
   if(!iValorSeleccionado)
   {
    oCombo.selectedIndex= 0;
   }
  }
 }
}

function ClearDropDown(sCombo, bAccionGenerica, sTextoGenerico, sValorGenerico)
{
 if(sCombo!='' && document.getElementById(sCombo))
 {
  var oCombo = document.getElementById(sCombo);
  oCombo.length = 0;
  oCombo.selectedIndex = 0;
  if (bAccionGenerica)
  {
   var oItem = document.createElement("option");
   oItem.text = sTextoGenerico;
   oItem.value = sValorGenerico;
   oCombo.options.add(oItem,0);
  }
 }
}

function isArray(obj){return(typeof(obj.length)=="undefined")?false:true;}

function SeleccionarItem(sElemento, valor)
{
  var oElementos = document.getElementsByName(sElemento);
  for(var i=0;i<oElementos.length;i++)
  {
   var oElemento = oElementos[i];
   if(isArray(oElemento)&&(typeof(oElemento.type)=="undefined"))
   {
     for(var j=0;j<oElemento.length;j++)
     {
      setValorInput(oElemento[i],valor);
     }
   }
   else
   {
     setValorInput(oElemento,valor);
   }
  }
}

function setValorInput(obj,valor) {
	switch(obj.type){
		case 'radio':
		case 'checkbox':
		  if(obj.value==valor)
		  {
		    obj.checked=true;
		    return true;
		  }
		  else
		  {
		    obj.checked=false;
		    return false;
		  }
		case 'text':
		case 'hidden':
		case 'textarea':
		case 'password':
		  obj.value=valor;
		  return true;
		case 'select-one':
		case 'select-multiple':
			var o=obj.options;
			for(var i=0;i<o.length;i++)
		  {
				if(o[i].value==valor)
		    {o[i].selected=true;}
				else
		    {o[i].selected=false;}
		  }
			return true;
		}
	return false;
	}

String.prototype.trim = function () {
 return this.replace(/^\s+|\s+$/g, '');
}

function refreshStyleSheet(){
  // IE6 HACK. Algunas versiones renderizan mal esto... hago esto para todos los anteriores al 7  
  if(navigator && navigator.userAgent)
  {
    var atg = navigator.userAgent.toLowerCase();
    if(atg.indexOf("msie 4")!=-1 || atg.indexOf("msie 5")!=-1 || atg.indexOf("msie 6")!=-1)
   //es IE
	  if(document.styleSheets && document.styleSheets.length){
		for(var i=0;i<document.styleSheets.length;i++)
		{
		  document.styleSheets[i].disabled=true;document.styleSheets[i].disabled=false;
		}
	  }
  }
}

function createFrame(iframeID, src){
	if (document.createElement && document.documentElement &&
		(window.opera || navigator.userAgent.indexOf('MSIE 5.0') == -1))
	{	
		var ifr = document.createElement('iframe');
		ifr.setAttribute('id', iframeID);
		ifr.setAttribute('name', iframeID);
		ifr.style.visibility = 'hidden';
		ifr.style.position = 'absolute';
		ifr.style.width = ifr.style.height = ifr.borderWidth = '0px';
		ifr.src = src;
		document.getElementsByTagName('body')[0].appendChild(ifr);
	}
	else if (document.body && document.body.insertAdjacentHTML)
	{
		// IE5.0 doesn't like createElement'd frames (won't script them) and IE4 just plain
		// doesn't support it. Luckily, this will fix them both:
		document.body.insertAdjacentHTML('beforeEnd', '<iframe name="' + iframeID +
		'" id="' + iframeID + '" src="' + src + '" style="display: none"></iframe>');
	}
}
