// -------------------------------------------------------------------------------- //
//  Funcion       : FUNCIONES PARA AYAX	y otras más 					            //
//  Autor         : Jorge Vera                                                      //
//  Actualizacion : 27/05/2008                                                      //
// -------------------------------------------------------------------------------- //

//*Editor
var _navegador = navigator.userAgent;
var ie = /msi/i.test(_navegador);
var op = /opera/i.test(_navegador);
var mo = /gecko/i.test(_navegador);
var otro = !(ie || mo);
var _insertor, _insertar, _formulario, _texto, _lector = "";
//*Editor

var net = new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;

// Constructor
net.CargadorContenidos = function(url, funcion, metodo, parametros, contentType, funcionError) {
							this.url = url;
							this.request = null;
							this.onload = funcion;
							this.onerror = (funcionError) ? funcionError : this.defaultError;
							this.cargaContenidoXML(url, metodo, parametros, contentType);
						}
						
net.CargadorContenidos.prototype = {
	
	cargaContenidoXML: function(url, metodo, parametros, contentType)  {
		if (window.XMLHttpRequest) { this.request = new XMLHttpRequest(); } // Mozilla, Safari
		else if (window.ActiveXObject) { this.request = new ActiveXObject("Microsoft.XMLHTTP"); } // IE
		
		if(this.request) {
			try {
				var loader = this; // Este Punto No está Claro "loader"
				this.request.onreadystatechange = function() { loader.onReadyState(); }
				this.request.open(metodo, url, true);
				if(contentType) { this.request.setRequestHeader("Content-Type", contentType); }
				this.request.send(parametros);
			} 
			catch (err) { this.onerror(); }
		}
	},

	defaultError: function() {
		var req = this.request;
		var cerror = "\n\nreadyState:" + req.readyState
					+ "\nstatus: " + req.status
					+ "\nheaders: " + req.getAllResponseHeaders();
		alert("Se ha producido un error al obtener los datos" + cerror);
	},
	
	onReadyState: function() {
		var req = this.request;
		if (req.readyState == net.READY_STATE_COMPLETE) {
			var httpStatus = req.status;
			if(httpStatus == 200 || httpStatus == 0) {
				this.onload();
			}
			else { this.onerror(); }
		}
	}

}

//*******************************************************************************************************		
function KeyEnter(form,field){
	//*** Esta función realiza un TAB al presionarse la tecla Enter.
	var next=0, found=false
	var f=form
	
	if(event.keyCode!=13) return;
	
	for(var i=0;i<f.length;i++) {
		if(field.id==f.item(i).id){
			next=i+1;
			found=true
			break;
		}
	}
	
	while(found){
		if( f.item(next).disabled==false && f.item(next).type!='hidden'){
			f.item(next).focus();
			f.item(next).select();
			break;
		}
		else{
			if(next<f.length-1)
				next=next+1;
			else
				break;
		}
	}
}

//*******************************************************************************************************
function BuscarElemento(aArray, cElemento) {
	//*** Esta función devuelve la posición en que se encuentra el elemento, en el arreglo, en caso de no encontrarse se devuelve un -1.  
	for(var i=0;i<aArray.length;i++) {
		if(aArray[i]==cElemento){ return i; }
	}
	return -1;	
}

//*****************************************************************************************************************************************************
function RellenaCeldas(cTabla, nColum, nFilaIni, cTexto, nTipo, nDire, nIncremento, lFormato, nDec) {
	//*** Esta función rellena en forma inteligente los datos de la columna y filas indicadas.
	var cDatTex = "";
	var oTabla = document.getElementById(cTabla);
	var nTotRow = oTabla.rows.length;
	
	lFormato = parseInt(lFormato);
	if(nTipo==1) {
		//*** Copia en forma correlativa.
		var aDatos = cTexto.split(" ");
		var aMes = { MesEsp1: ["ENE","FEB","MAR","ABR","MAY","JUN","JUL","AGO","SEP","OCT","NOV","DIC"],
					 MesEsp2: ["ENERO","FEBRERO","MARZO","ABRIL","MAYO","JUNIO","JULIO","AGOSTO","SEPTIEMBRE","OCTUBRE","NOVIEMBRE","DICIEMBRE"],
					 MesIng1: ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"],
					 MesIng2: ["JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"] };
					 
		var nTotRow = oTabla.rows.length;
		for(var i=0;i<aDatos.length;i++) {
			if( !isNaN(parseInt(aDatos[i])) ) {
				//*** Valor Númerico
				var nNum = parseFloat(aDatos[i].replace(",","."));
	
				if(nDire==1) {
					//*** Vertical
					for(var nR=nFilaIni; nR<nTotRow; nR++) {
						var oCeldas = oTabla.rows(nR).cells;
						var oInput = oCeldas(nColum-1).firstChild;
						
						if (!oInput.readOnly) {
							if(lFormato) oInput.value = cDatTex + " " + FormatoNumero(nNum,nDec)
							else  oInput.value = cDatTex + " " + nNum;
						}
						
						//**** Marcar la Fila como Modificada
						if(parseInt(oInput.title)>0) document.getElementById("Row_" + nR).title = "1";

						nNum = nNum + nIncremento;
					}
				}
				
				else {
					//*** Horizontal
					var oCeldas = oTabla.rows(nFilaIni).cells;
					var nTotCol = oCeldas.length;
					for(var nC=nColum; nC<=nTotCol; nC++) {
						var oInput = oCeldas(nC-1).firstChild;
						
						if (!oInput.readOnly) {

							if(lFormato) oInput.value = cDatTex + " " + FormatoNumero(nNum,nDec)
							else  oInput.value = cDatTex + " " + nNum;
						
							//**** Marcar la Fila como Modificada
							if(parseInt(oInput.title)>0) document.getElementById("Row_" + nFilaIni).title = "1";
						}
						
						nNum = nNum + nIncremento;
					}
				}
				
				break;	
			}
			if( isNaN(parseInt(aDatos[i])) ) {
				//*** String
				var aMeses;
				if( aDatos[i].toUpperCase()=="ENE"          ) aMeses = aMes.MesEsp1; 
				else if( aDatos[i].toUpperCase()=="ENERO"   ) aMeses = aMes.MesEsp2; 
				else if( aDatos[i].toUpperCase()=="JAN"     ) aMeses = aMes.MesIng1; 
				else if( aDatos[i].toUpperCase()=="JANUARY" ) aMeses = aMes.MesIng2; 
				else aMeses = 0;
				
				if( aMeses!=0) {
					nNum = 0;
					
					if(nDire==1) {
						for(var nR=nFilaIni; nR<nTotRow; nR++) {
							if( nNum<=aMeses.length ) {
								var oCeldas = oTabla.rows(nR).cells;
								var oInput = oCeldas(nColum-1).firstChild;
								if (!oInput.readOnly) {

									oInput.value = aMeses[nNum];
								}
								nNum++;
							}
						}
					}
					else {
						var oCeldas = oTabla.rows(nFilaIni).cells;
						var nTotCol = oCeldas.length;
						for(var nC=nFilaIni; nC<=nTotCol; nC++) {
							if( nNum<=aMeses.length ) {
								var oInput = oCeldas(nC-1).firstChild;
								if (!oInput.readOnly) {

									oInput.value = aMeses[nNum];
								}
								nNum++;
							}
						}
					}
					break;	
				}
				else {cDatTex = aDatos[i]; }
			}
		}
	}
	else {
		//*** Copia el mismo datos en todas las celdas.
		if(nDire==1) {
			for(var nR=nFilaIni; nR<nTotRow; nR++) {
				var oCeldas = oTabla.rows(nR).cells;
				var oInput = oCeldas(nColum-1).firstChild;
				
				if (!oInput.readOnly) {

					if( isNaN(cTexto) ) {
						oInput.value = cTexto;
					}
					else {
						if(lFormato) oInput.value = FormatoNumero(cTexto,nDec)
						else  oInput.value = cTexto;
					}
					
					//**** Marcar la Fila como Modificada
					if(parseInt(oInput.title)>0) document.getElementById("Row_" + nR).title = "1";
				}
			}
		}
		else {
			var oCeldas = oTabla.rows(nFilaIni).cells;
			var nTotCol = oCeldas.length;
			for(var nC=nColum; nC<=nTotCol; nC++) {
				var oInput = oCeldas(nC-1).firstChild;
				
				if (!oInput.readOnly) {

					if( isNaN(cTexto) ) {
						oInput.value = cTexto;
					}
					else {
						if(lFormato) oInput.value = FormatoNumero(cTexto,nDec)
						else  oInput.value = cTexto;
					}
					
					//**** Marcar la Fila como Modificada
					if(parseInt(oInput.title)>0) document.getElementById("Row_" + nFilaIni).title = "1";
				}
			}
		}
	}
}

//*****************************************************************************************************************************************************
function CaracteresEspeciales(cDato) {

	var cTexto = cDato;
	cTexto = Reemplazar(cTexto,"!1!"," = ");
	cTexto = Reemplazar(cTexto,"!2!"," + ");
	cTexto = Reemplazar(cTexto,"*"," * ");
	cTexto = Reemplazar(cTexto,"/"," / ");
	cTexto = Reemplazar(cTexto,"-"," - ");
			
	cTexto = Reemplazar(cTexto,"!a!","á");
	cTexto = Reemplazar(cTexto,"!e!","é");
	cTexto = Reemplazar(cTexto,"!i!","í");
	cTexto = Reemplazar(cTexto,"!o!","ó");
	cTexto = Reemplazar(cTexto,"!u!","ú");
	cTexto = Reemplazar(cTexto,"!n!","ñ");
	cTexto = Reemplazar(cTexto,"!N!","Ñ");
	cTexto = Reemplazar(cTexto,"!ro!","°");
	
	cTexto = Reemplazar(cTexto,"!2!" ,"+"  );
	cTexto = Reemplazar(cTexto,"!M!" ,">"  );
	cTexto = Reemplazar(cTexto,"!m!" ,"<"  );
	cTexto = Reemplazar(cTexto,"!M1!",">=" );
	cTexto = Reemplazar(cTexto,"!m1!","<=" );
	cTexto = Reemplazar(cTexto,"!0!" ,"<>" );
	cTexto = Reemplazar(cTexto,"!O!","Ó");
	cTexto = Reemplazar(cTexto,"!A!","Á");
	cTexto = Reemplazar(cTexto,"!E!","É");
	cTexto = Reemplazar(cTexto,"!I!","Í");
	cTexto = Reemplazar(cTexto,"!U!","Ú");
	
	return cTexto;
}

//*****************************************************************************************************************************************
function Cabecareas(oTabla) {
	var nCol   = oTabla.rows[0].cells.length;
	var cCabe  = "";
	
	cCabe = oTabla.rows[0].cells[0].innerText;
	for(var i=1;i<nCol;i++) {
		cCabe += "," + oTabla.rows[0].cells[i].innerText;
	}
	return cCabe;
}



//*****************************************************************************************************************************************
function datos_ie() {
	_texto = document.selection.createRange().text;
	if (_formulario.createTextRange)
		_formulario.posi = document.selection.createRange().duplicate();
	return true;
}

//*****************************************************************************************************************************************

function captura_ie()	{
	return _texto;
}
//*****************************************************************************************************************************************
function captura_mo() {
	with (_formulario) return value.substring(selectionStart, selectionEnd);
}
//*****************************************************************************************************************************************
function captura_otro()	{
	return "";
}
//*****************************************************************************************************************************************
function poner_mo(f, x)	{//alert(x);
	var _ini = f.selectionStart;
	var _fin = f.selectionEnd;
	var inicio = f.value.substr(0, _ini);
	var fin = f.value.substr(_fin, f.value.length);

	f.value = inicio + x + fin;
	if (_ini == _fin)	{
		f.selectionStart = inicio.length + x.length;
		f.selectionEnd = f.selectionStart;
	}
	else	{
		f.selectionStart = inicio.length;
		f.selectionEnd = inicio.length + x.length;
	}
	f.focus();
}
//*****************************************************************************************************************************************
function poner_otro(f, x)	{// opera u otros navegadores desconocidos
	f.value += x;//alert(x);
	f.focus();
}
//*****************************************************************************************************************************************
function poner_ie(f, x)	{
	f.focus();
	if (f.createTextRange)	{// && f.posi)	{
		if (!f.posi)	datos_ie();
		with(f)	{
			var actuar = (posi.text == "");
			posi.text = x;
			if (!actuar)
				posi.moveStart("character", -x.length);
			posi.select();
		}
	}
}
//*****************************************************************************************************************************************
function ini_editor(formu)	{
	_formulario = formu;
	
	if (op || mo)	{//alert("mozilla u opera");
		_insertar = function(f, x) {poner_mo(f, x);};
		_lector = captura_mo;
	}

else	if (otro)	{//alert("otro");
		_insertar = function(f, x) {poner_otro(f, x);};
		_lector = captura_otro;
	}

else	if (ie)	{
		_formulario.onchange = datos_ie;
		_formulario.onclick  = datos_ie;
		_insertar = function(f, x) {poner_ie(f, x);};
		_lector = captura_ie;
	}
	return formu;
}
//*****************************************************************************************************************************************
function MensajeDeEspera() 
 	{ 
 	document.all.pleasewaitScreen.style.pixelTop = (document.body.scrollTop + 50); 
 	document.all.pleasewaitScreen.style.visibility="visible"; 
 	//window.setTimeout('do_totals2()',1); 
 	}	 
 		function do_totals2() 
 	{ 
 	lengthy_calculation(); 
 	document.all.pleasewaitScreen.style.visibility="hidden"; 
 	} 
 		function lengthy_calculation() 
 	{ 
 	var x,y 
 		for(x=0;x<1000000;x++) 
 		{ 
 		y += (x * y) / (y - x);			 
 		} 
 	}