function CambiaEstado(elem){
	elem.disabled = !elem.disabled;
	if (elem.disabled){elem.value = '';}
};

function buscarControl(vform, vcontrolName)
	{
		var objControl = null;
		for(nContElem=0;nContElem<vform.elements.length;nContElem++)
		{
			if (fdatos.elements[nContElem].name==vcontrolName)
			{
				objControl = fdatos.elements[nContElem];
				break;
			};
		};
		return objControl;
};

function SetearValor(elem, valor){
  elem.value = valor;
  return true;
};

function cambiarDecimal(valor,decimal){
  numero = new String(valor.value);
  numero = reempCar(numero, ",", decimal);
  numero = reempCar(numero, ".", decimal);
  valor.value = numero;
  return true;
};

function contarCaracter(valor,caracter){
  var cadena = new String(valor);
  var cont = 0;
  for (ind=0;ind<cadena.length;ind++){
	if (cadena.charAt(ind) == caracter) cont++;
  };
  return cont;
};

function valorInt(objeto){
  var rvalor = 0;
  if ((objeto.value!="") && (validaNumeros(objeto, "", "")==true)){
    rvalor = objeto.value;
  };
  return rvalor;
};

function validaNumeros(valor,texto,decimal){
  numero=new String(valor.value);
  ok=1;
  var validos="0123456789";
  validos = validos + decimal;
  for (ind=0;ind<numero.length;ind++){
	if (validos.indexOf(numero.charAt(ind)) == "-1") ok = 0;
  };
  if ((ok==1) && (contarCaracter(numero,decimal)>1)){
    ok = 0;
  };
  // if (numero<desde || numero>hasta) {ok = 0; texto="Rango inválido.";}
  if (ok==0){
	if (texto==""){
		alert("Ingresar sólo números.");
	}else{
		alert(texto);
	};
	valor.value="";
	valor.focus();
	return false;
  };
  // if (numero.length!=4) {alert("Ingrese 4 dígitos"); return false;}
  return true;
};

function validaRangos(valor,desde,hasta,texto,decimal){
  numero=new String(valor.value);
  ok=1;
  var validos="0123456789";
  validos = validos + decimal;
  for (ind=0;ind<numero.length;ind++){
	if (validos.indexOf(numero.charAt(ind)) == "-1") ok = 0;
  };
  if ((ok==1) && (contarCaracter(numero,decimal)>1)){
    ok = 0;
  };
  // valor numerico
  if (ok==1){
    numero = reempCar(numero, decimal, ".");
    if (numero<desde || numero>hasta) { ok = 0; };
  };
  if (ok==0){
	if (texto==""){
		alert("Ingresar sólo números.");
	}else{
		alert(texto);
	};
	valor.focus();
	return false;
  };
  return true;
};

function validaCombo(combo,texto,base){
  var exito = (combo.selectedIndex>base);
  if ((exito==false) && (texto!="")){
    alert(texto);
  };
  return exito;
};

function validaComboValor(combo,texto,base){
  var exito = (combo.value>base);
  if ((exito==false) && (texto!="")){
    alert(texto);
  };
  return exito;
};

function reempCar(valor, caracter1, caracter2){
  cadena = new String(valor);
  resultado = new String("");
  for (ind=0;ind<cadena.length;ind++){
	if (cadena.charAt(ind)!=caracter1){ resultado = resultado + cadena.charAt(ind); }else{ resultado = resultado + caracter2; };
  };
  return resultado;
};

function alltrim(cadena){
  return reempCar(cadena, " ", "");
};

function vacio(valor, texto){
  var cadena = new String(valor.value);
  cadena = alltrim(cadena);
  var esVacio = (cadena.length<=0);
  if ((esVacio==true) && (texto.length>0)){
    alert(texto);
  }
  return esVacio;
};

function marcarHidden(elem, casilla){
	if (casilla.checked){
		elem.value = 1;
	}else{
		elem.value = 0;
	};
	return true;
};

function ChequearDatos(){
  // Chequea que todos los datos ingresados sean válidos según el tipo.
  if (DatosPAE.selTipo.selectedIndex>0){
	if (validaNumeros(DatosPAE.txtLongitud, "", 1, 9999)==false){
		return false;
	};
  }else{
	DatosPAE.txtLongitud.value=0;
  };
  DatosPAE.Axion.value = 'PROCEDER';
  return true;
};

function filtro(tipo, edit, id){
	this.target = edit;
	var vpopup = window.open('filtro.jsp?tipo=' + tipo + '&id=' + id + '&valor=' + edit.value,
							'Filtro', 
							'width=400,height=400,status=yes,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'
							);
	vpopup.opener = window;
	vpopup.focus();
};

function consultaLista(tipo, titulo, vobjID){
	this.target = window;
	var vpopup = window.open('consulta.jsp?t=' + tipo + '&m=0&o=' + vobjID,
							'Consulta',
							'width=400,height=400,status=yes,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'
							);
	vpopup.opener = window;
	vpopup.focus();
};

function consultaEdit(tipo, titulo, vobjID, vobjDESC){
	this.target = window;
	var vpopup = window.open('consulta.jsp?t=' + tipo + '&m=1&o=' + vobjID + '&od=' + vobjDESC,
							'Consulta',
							'width=400,height=400,status=yes,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'
							);
	vpopup.opener = window;
	vpopup.focus();
};

function consultaEquipoEdit(tipo, titulo, vobjID, vobjDESC, vobj2ID, vobj2DESC){
	consultaEquipoComponente(tipo, titulo, vobjID, vobjDESC, vobj2ID, vobj2DESC, 1);
};

function consultaEquipoComponente(tipo, titulo, vobjID, vobjDESC, vobj2ID, vobj2DESC, completo){
	consultaEquipoFrecuencia(tipo, titulo, vobjID, vobjDESC, vobj2ID, vobj2DESC, completo, -1);
};

function consultaEquipoFrecuencia(tipo, titulo, vobjID, vobjDESC, vobj2ID, vobj2DESC, completo, frecuencia){
	this.target = window;
	var vpopup = window.open('consultaE.jsp?t=' + tipo + '&m=0&o=' + vobjID + '&od=' + vobjDESC + '&o2=' + vobj2ID + '&od2=' + vobj2DESC + '&completo=' + completo + '&frecuencia=' + frecuencia,
							'Consulta',
							'width=600,height=400,status=yes,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'
							);
	vpopup.opener = window;
	vpopup.focus();
};

function consultaEditMaterial(tipo, titulo, vobjID, vobjDESC, vobjSTK, vobjCTRL, vDeposito){
	this.target = window;
	var vpopup = window.open('consultaMat.jsp?t=' + tipo + '&m=1&o=' + vobjID + '&od=' + vobjDESC + '&os=' + vobjSTK + '&oc=' + vobjCTRL + '&dep=' + vDeposito,
							'Consulta',
							'width=400,height=400,status=yes,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'
							);
	vpopup.opener = window;
	vpopup.focus();
};

function nuevoParametro(tipo, Titulo, objeto){
   var wnuevo = window.open(
     				'parametros.jsp?o=nuevo&dialog=1&tipoc=' + tipo + '&objDestino=' + objeto,
     				'Nuevo',
     				'width=550,height=400,status=no,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'
				);
   wnuevo.opener = window;
   wnuevo.focus();
};

function nuevoProveedor(tipoDialog, Titulo, objID, objDESC){
   var wnuevo = window.open(
     				'proveedor.jsp?o=nuevo&dialog=1&tipoDialog=' + tipoDialog + '&objID=' + objID + '&objDESC=' + objDESC,
     				'Nuevo',
     				'width=550,height=600,status=no,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'
				);
   wnuevo.opener = window;
   wnuevo.focus();
};

function agregarOpcion(combo, id, texto){
    if (document.forms['fdatos']){
	    if (document.forms['fdatos'].elements[combo]){
			objCombo = document.forms['fdatos'].elements[combo];
	    	objCombo.options[objCombo.length] = new Option(texto, id, false, false);
	    	objCombo.value = id;
	    };
	};
	return true;
};

function seleccionarOpcion(objID, objDESC, id, texto){
    if (document.all[objID]){
		objOID = document.all[objID];
    	objOID.value = id;
    };
    if (document.all[objDESC]){
		objODESC = document.all[objDESC];
    	objODESC.value = texto;
    };	    
	return true;
};

function seleccionarOpcionMaterial(objID, objDESC, objSTK, objCTRL, id, texto, stock, controla){
    if (document.all[objID]){
		objOID = document.all[objID];
    	objOID.value = id;
    };
    if (document.all[objDESC]){
		objODESC = document.all[objDESC];
    	objODESC.value = texto;
    };	    
    if (document.all[objSTK]){
		objOSTK = document.all[objSTK];
    	objOSTK.value = stock;
    };	    
    if (document.all[objCTRL]){
		objOCTRL = document.all[objCTRL];
    	objOCTRL.value = controla;
    };    
	return true;
};

function seleccionarOpcion2(objID, objDESC, id, texto){
//    var nForm;
//    for(nForm=0;nForm<document.formsCount;nForm++)
    if (document.forms['fdatos']){
	    if (document.forms['fdatos'].elements[objID]){
			objOID = document.forms['fdatos'].elements[objID];
	    	objOID.value = id;
	    };
	    if (document.forms['fdatos'].elements[objDESC]){
			objODESC = document.forms['fdatos'].elements[objDESC];
	    	objODESC.value = texto;
	    };	    
	};
	return true;
};

function getHoraDouble(vhoras, vminutos, vsegundos){
    var hora = new Number(0);
    hora = (vhoras * 3600) + (vminutos * 60) + vsegundos;
	return hora;
};

function verificarTexto(texto,carValidos,errorVacio)
{
 if(texto == null || texto.length == 0)
 {
    alert(errorVacio);
    return false;
 }
 else
 {
    var cValidos=carValidos;//new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
    
    textoSplit="";

    cInvalidosUsados="";

    hayInvalidos=false;

    for( i=0 ; i < texto.length ; i++)
    {
        if ((cValidos.indexOf( texto.charAt( i ) ) == -1) && (texto.charAt( i )!=' ') )
        {
            hayInvalidos=true;
            if( cInvalidosUsados.indexOf( texto.charAt( i ) ) == -1 )
            {
                cInvalidosUsados+=texto.charAt( i );
                cInvalidosUsados+=" ";
            }
        }
        else
        {
            if( (hayInvalidos==false) && (texto.charAt( i )!=' '))
            {
                textoSplit+=texto.charAt( i );                
            }
        }    
    }
    
    if( cInvalidosUsados.length != 0)
    {
        alert("Los siguientes caracteres ingresados son invalidos: \n\n"+cInvalidosUsados);
        return false;
    }
    else
    {
        if( textoSplit.length == 0)
        {
            alert(errorVacio);
            return false;
        }
        else
        {
            return true;
        }
    }    
 }
};

//FUNCION QUE VALIDA HORA (Rodrigo)
function ValidaHora(str, texto)
{
	hora=str.value
	
	if (hora=='') 
	{
		alert(texto);
		str.value="";
		str.focus();
		return false;
	}
	if (hora.length>5) 
	{
		alert(texto);
		str.value="";
		str.focus();
		return false;
	}
	if (hora.length!=5) 
	{
		alert(texto);
		str.value="";
		str.focus();
		return false;
	}
	a=hora.charAt(0) //0 y 23 y no letra
	b=hora.charAt(1) //no letra
	c=hora.charAt(2) //:
	d=hora.charAt(3) //<=5 y no letra
	e=hora.charAt(4) // no letra
	/*f=hora.charAt(5) //:
	g=hora.charAt(6) // <=5 y no letra
	h=hora.charAt(7) //no letra     NO HACEN FALTA LOS SEGUNDOS   */ 
	
	if ( (a==2 && b>3) || (a>2) || (!IsNum(a)) || (!IsNum(b)) )
	{
		alert(texto);
		str.value="";
		str.focus();
		return false;
	}
	if ( (d>5) || (!IsNum(d)) || (!IsNum(e)) ) 
	{
		alert(texto);
		str.value="";
		str.focus();
		return false;
	}
	/*if ( (g>5) || (!IsNum(g)) || (!IsNum(h)) ) 
	{
		alert(texto);
		str.value="";
		str.focus();
		return false;
	}*/
	//if (c!=':' || f!=':') 
	if (c!=':')
	{
		alert(texto);
		str.value="";
		str.focus();
		return false;
	}
	return true;
}
//FIN FUNCION QUE VALIDA HORA

function IsNum( numstr )  //Rodrigo
{
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "") 
	return false;
	var isValid = true;
	var decCount = 0; // number of decimal points in the string
	// convert to a string for performing string comparisons.
	numstr += ""; 
	// Loop through string and test each character. If any
	// character is not a number, return a false result.
	// Include special cases for negative numbers (first char == '-')
	// and a single decimal point (any one char in string == '.'). 
	for (i = 0; i < numstr.length; i++) {
	// track number of decimal points
	if (numstr.charAt(i) == ".")
	decCount++;
	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
	(numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
	isValid = false;
	break;
	} else if ((numstr.charAt(i) == "-" && i != 0) ||
	(numstr.charAt(i) == "." && numstr.length == 1) ||
	(numstr.charAt(i) == "." && decCount > 1)) {
	isValid = false;
	break;
	} 
	//if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
	} // END for 
	
	return isValid;
} // end IsNum

//CONJUNTO DE FUNCIONES QUE VALIDAN FECHAS (Rodrigo)
function esDigito(sChr)
{
	var sCod = sChr.charCodeAt(0);
	return ((sCod > 47) && (sCod < 58));
}

function valSep(oTxt)
{
	var bOk = false;
	bOk = bOk || ((oTxt.value.charAt(2) == "-") && (oTxt.value.charAt(5) == "-"));
	bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));
	return bOk;
}
function finMes(oTxt)
{
	var nMes = parseInt(oTxt.value.substr(3, 2), 10);
	var nRes = 0;
	switch (nMes)
	{
		case 1: nRes = 31; break;
		case 2: nRes = 29; break;
		case 3: nRes = 31; break;
		case 4: nRes = 30; break;
		case 5: nRes = 31; break;
		case 6: nRes = 30; break;
		case 7: nRes = 31; break;
		case 8: nRes = 31; break;
		case 9: nRes = 30; break;
		case 10: nRes = 31; break;
		case 11: nRes = 30; break;
		case 12: nRes = 31; break;
	}
	return nRes;
}

function valDia(oTxt)
{
	var bOk = false;
	var nDia = parseInt(oTxt.value.substr(0, 2), 10);
	bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
	return bOk;
}

function valMes(oTxt)
{
	var bOk = false;
	var nMes = parseInt(oTxt.value.substr(3, 2), 10);
	bOk = bOk || ((nMes >= 1) && (nMes <= 12));
	return bOk;
}

function valAno(oTxt)
{
	var bOk = true; //ver
	var nAno = oTxt.value.substr(6);
	bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));
	if (bOk)
	{
		for (var i = 0; i < nAno.length; i++){
			bOk = bOk && esDigito(nAno.charAt(i));
		}
	}
	return bOk;
}

function valFecha(oTxt,texto)
{
	var bOk = true;
	if (oTxt.value != "")
	{
		bOk = bOk && (valAno(oTxt));
		bOk = bOk && (valMes(oTxt));
		bOk = bOk && (valDia(oTxt));
		bOk = bOk && (valSep(oTxt));
		if (!bOk)
		{
			if (texto.length>0){
				alert(texto);
			};
			oTxt.value = "";
			oTxt.focus();
			return false;
		}
	}
	else
	{
		oTxt.focus();
		return false;
	}
	return true;
}

function compararFecha(valor,texto) //Rodrigo
{
	var fecha=valor.value;
	var fechaActual = obtiene_fecha();
	var temp = Comparar_Fecha(fechaActual, fecha);
	
	if (temp==false)
	{
		alert(texto);
		valor.value="";
		valor.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function obtiene_fecha() //Rodrigo
   {
   var fecha_actual = new Date()

   dia = fecha_actual.getDate()
   mes = fecha_actual.getMonth() + 1
   anio = fecha_actual.getYear()

   if (anio < 100)
      anio = '19' + anio
   else if ( ( anio > 100 ) && ( anio < 999 ) ) {
      var cadena_anio = new String(anio)
      anio = '20' + cadena_anio.substring(1,3)
   }

   if (mes < 10)
      mes = '0' + mes

   if (dia < 10)
      dia = '0' + dia

   return (dia + "/" + mes + "/" + anio)
   }
   
function Comparar_Fecha(String1,String2) //Rodrigo
{
	if (String1.substring(1,2)=="/" || String1.substring(1,2)=="-") 
	{
		String1="0"+String1
	}
	if (String1.substring(4,5)=="/" || String1.substring(4,5)=="-")
	{
		String1=String1.substring(0,3)+"0"+String1.substring(3,9)
	}
	
	if (String2.substring(1,2)=="/" || String2.substring(1,2)=="-") 
	{
		String2="0"+String2
	}
	if (String2.substring(4,5)=="/" || String2.substring(4,5)=="-")
	{
		String2=String2.substring(0,3)+"0"+String2.substring(3,9)
	}
	
	dia1=String1.substring(0,2);	//fecha actual
	mes1=String1.substring(3,5);	//fecha actual
	anyo1=String1.substring(6,10);	//fecha actual
	dia2=String2.substring(0,2);
	mes2=String2.substring(3,5);
	anyo2=String2.substring(6,10);
	
	
	if (dia1 == "08") // parseInt("08") == 10 base octogonal
		dia1 = "8";
	if (dia1 == '09') // parseInt("09") == 11 base octogonal
		dia1 = "9";
	if (mes1 == "08") // parseInt("08") == 10 base octogonal
		mes1 = "8";
	if (mes1 == "09") // parseInt("09") == 11 base octogonal
		mes1 = "9";
	if (dia2 == "08") // parseInt("08") == 10 base octogonal
		dia2 = "8";
	if (dia2 == '09') // parseInt("09") == 11 base octogonal
		dia2 = "9";
	if (mes2 == "08") // parseInt("08") == 10 base octogonal
		mes2 = "8";
	if (mes2 == "09") // parseInt("09") == 11 base octogonal
		mes2 = "9";
	
	dia1=parseInt(dia1);
	dia2=parseInt(dia2);
	mes1=parseInt(mes1);
	mes2=parseInt(mes2);
	anyo1=parseInt(anyo1);
	anyo2=parseInt(anyo2);
	
	if (anyo2<anyo1)
	{
		return false;
	}
	
	if ((anyo1==anyo2) && (mes1>mes2))
	{
		return false;
	}
	if ((anyo1==anyo2) && (mes1==mes2) && (dia1>dia2))
	{
		return false;
	} 
	
	return true;
}

function validarSoloNumeros (input)
{
	var texto=input.value;
	
	for (var i=0; i<texto.length; i++)
	{
		var caracter=texto.charAt(i);		
		if ((caracter < "0" || caracter > "9") && caracter!='.')
		{
			return false;
		}
	}
	return true;
}

function mensajeBorrar(obj,url){
	if(window.confirm("¿Esta seguro que desea borrar este Item "+ obj +"?") == true){
	  document.location.href = url;
	  return true;
	}else{
	  return false;
    }
    
    /*this.target = window;
	var vpopup = window.open('Mensaje.jsp',
							'Mensaje',
							'width=400,height=200,status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes'
							);
	vpopup.opener = window;
	vpopup.focus();*/
}
/*---Funciones pertenecientes a los tabas de los Filtros---*/
	function redirigir(url){
		document.location.href = url;
	}
	
	function MM_preloadImages() { 
	  	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

	function MM_findObj(n, d) { 
		var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  	if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

	function MM_nbGroup(event, grpName) { 
  		var i,img,nbArr,args=MM_nbGroup.arguments;
	  	if (event == "init" && args.length > 2) {
	    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
	      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
	      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
	      nbArr[nbArr.length] = img;
	      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
	        if (!img.MM_up) img.MM_up = img.src;
	        img.src = img.MM_dn = args[i+1];
	        nbArr[nbArr.length] = img;
	    } }
	  } else if (event == "over") {
	    document.MM_nbOver = nbArr = new Array();
	    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
	      if (!img.MM_up) img.MM_up = img.src;
	      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
	      nbArr[nbArr.length] = img;
	    }
	  } else if (event == "out" ) {
	    for (i=0; i < document.MM_nbOver.length; i++) {
	      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
	  } else if (event == "down") {
	    nbArr = document[grpName];
	    if (nbArr)
	      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
	    document[grpName] = nbArr = new Array();
	    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
	      if (!img.MM_up) img.MM_up = img.src;
	      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
	      nbArr[nbArr.length] = img;
	  } }
	}
function mostrarPopup(pagina,nombre,argumentos, miAncho, miAlto, centrar) 
	{ 
	if(window.screen)
		if(centrar)
			if(centrar=="true")
			{
				var distanciaIzquierda = (screen.width-miAncho)/2;
				var distanciaArriba = (screen.height-miAlto)/2;				
				argumentos+=(argumentos!='')?',':'';
				argumentos+=',left='+distanciaIzquierda+',top='+distanciaArriba;
			}
			
			ret=window.open(pagina,nombre,argumentos+((argumentos!='')?',':'')+'width='+miAncho+',height='+miAlto);
			window.ret.focus();
			return ret;
	}
//FIN FUNCIONES DE FECHA

function isEmailAddress(theElement)
{
	alert(theElement);
	var s = theElement.value;
	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))
		return true;
	else
		alert("Ingrese una dirección de correo válida");
	theElement.focus();
	return false;
}
