﻿


function validar(){
	f = document.form1;
  if(f.usuario.value == '') {
	if(f.clave.value == '')	{
		alert("Ingrese su nombre de usuario y su clave");
		f.usuario.focus();
		return false;
		}
	else
		{
		alert("Ingrese su nombre de usuario");
		f.usuario.focus();
		return false;
		}
	}
  else
	{
	if(f.clave.value == '')
		{
		alert("Ingrese su clave");
		f.clave.focus();
		return false;
		}
	}
}

function verificarfechadma(d,m,a){

	if(d.value == '')
		{
		alert("Ingrese el día");
		d.focus();
		return false;
		}
	if(m.value == '')
		{
		alert("Ingrese el mes");
		m.focus();
		return false;
		}
	if(a.value == '')
		{
		alert("Ingrese el año");
		a.focus();
		return false;
		}

	if ((m.value == 1)||(m.value == 3)||(m.value == 5)||(m.value == 7)||(m.value == 8)||(m.value == 10)||(m.value == 12)){
		if ((d.value < 1)||(d.value > 31)){
			alert("Ingrese una cantidad de días válida(máx 31)");
			d.focus();
			return false;
		}
	 }
	else{
		if ((m.value == 4)||(m.value == 6)||(m.value == 9)||(m.value == 11)){
			if ((d.value < 1)||(d.value > 30)){
				alert("Ingrese una cantidad de días válida(máx 30)");
				d.focus();
				return false;
			}
		}
		if (m.value == 2){
			if ((a.value % 4) == 0) {
				if ((d.value < 1)||(d.value > 29)){
					alert("Ingrese una cantidad de días válida(máx 29)");
					d.focus();
					return false;
				}
			 }
			else {
				if ((d.value < 1)||(d.value > 28)){
					alert("Ingrese una cantidad de días válida(máx 28)");
					d.focus();
					return false;
				}
			}
		}
	}

	if ((m.value < 1)||(m.value > 12)) {
		alert("Ingrese un mes válido");
		m.focus();
		return false;
	}
		
	if ((a.value < 1901)||(a.value > 2150)) {
		alert("Ingrese un año válido(1901-2150)");
		a.focus();
		return false;
	}

return true ;

}

function verificarfechama(m,a){

	if(m.value == '')
		{
		alert("Ingrese el mes");
		m.focus();
		return false;
		}
	if(a.value == '')
		{
		alert("Ingrese el año");
		a.focus();
		return false;
		}

	if ((m.value < 1)||(m.value > 12)) {
		alert("Ingrese un mes válido");
		m.focus();
		return false;
	}

	if ((a.value < 1901)||(a.value > 2150)) {
		alert("Ingrese un año válido(1901-2150)");
		a.focus();
		return false;
	}

return true ;
}

function OnlyNumber(ctl)
{
  
   var str;
   var largo;

   str = ctl.value;
   largo = str.length;   

   if (!(isNumberChar(str.substring(largo - 1,largo))))
   {
      ctl.value = str.substring(0, largo - 1);
   } 
   return true;
}

function isNumberChar (InString)  {
    if(InString.length!=1) 
        return (false);
    RefString="1234567890";
    if (RefString.indexOf (InString, 0)==-1) 
        return (false);
    return (true);
}

function UnformatNumber(ctl)
{
var t;
var caracter;
var i;
var sts;
   
	sts = ctl;

	if (sts.length == 0 )
		return "0";

	t = "";
 
	for (i=0;i<sts.length;i++)
	{
		caracter = sts.substring(i, i + 1);
		
		if (caracter != ".")
			t = t + caracter;
	}

	return t;
}

function FormatNumber(ctl)
{
var salida;
var t;
var caracter;
var separador;
var i;
var largo;
var str;
var nn;
var esNegativo;
   
	sts = ctl.value;

	if (sts.length == 0 )
		return false;

	t = "";
 
	for (i=0;i<sts.length;i++)
	{
		caracter = sts.substring(i, i + 1);
		
		if ((caracter != "."))
			t = t + caracter;
		else
			nn = nn - 1;
	}

	salida = "";
	
	while(i>=0)
	{
		separador = "";
	
		if ( t.substring(0,1) != "-" )
		{
			if (i == (t.length - 4) || i == (t.length - 7) || (i == (t.length - 10)) || (i == (t.length - 13)))
			{
				separador = ".";
				nn++;
			}
		} else {
			if ( i != 0 )
			{
				if (i == (t.length - 4) || i == (t.length - 7) || (i == (t.length - 10)) || (i == (t.length - 13)))
				{
					separador = ".";
					nn++;
				}
			}
		}
	
		salida = t.substring(i, i + 1) + separador + salida;
		i--;
   }
   
	ctl.value = salida;
}

function OnlyDv(ctl)
{
   var str;
   var largo;
   
   str = ctl.value;
   largo = str.length;   
   
   if (!(isDv(str.substring(largo - 1,largo))))
   {
      ctl.value = str.substring(0, largo - 1);
   } 
   return true;
}

function isDv (InString)  {
    if(InString.length!=1) 
        return (false);
    RefString="1234567890Kk";
    if (RefString.indexOf (InString, 0)==-1) 
        return (false);
    return (true);
}

function isDoubleChar (InString)  
{
	if(InString.length!=1) 
        return (false);
    
    RefString="1234567890,";
    
    if (RefString.indexOf (InString, 0)==-1) 
        return (false);
    
    return (true);
}

function FormatDouble(ctl, dec)
{
var salida;
var t;
var caracter;
var separador;
var i;
var largo;
var str;
var nn;
var esNegativo;
var posDec;
var contador;
var SIMBOLO;
var entero;
var sts;
var stsDecimales;
var stsDecimalesTmp = "";

    SIMBOLO = "."
   
	sts = ctl.value;
	entero = sts;

	if (sts.length == 0 )
		return false;
	
	if ((posDec = sts.indexOf (",", posDec)) != -1)
	{
		stsDecimales = sts.substring(posDec +1);

		entero = sts.substring( 0, posDec);

		if(stsDecimales.length>0)
		{
			for (i=0;i<stsDecimales.length;i++)
			{
				caracter = stsDecimales.substring(i, i + 1);

				if (! isNaN(caracter))
					stsDecimalesTmp = stsDecimalesTmp + caracter;
			}

			stsDecimales = stsDecimalesTmp;
		}

		if (stsDecimales.length > dec)
			stsDecimales = stsDecimales.substring(0, stsDecimales.length - 1);
	}

	t = "";
 
	for (i=0;i<entero.length;i++)
	{
		caracter = entero.substring(i, i + 1);
		
		if (caracter != SIMBOLO && (caracter == "," || ! isNaN(caracter)) )
			t = t + caracter;
		else
			nn = nn - 1;
	}

	salida = "";
	
	while(i>=0)
	{
		separador = "";
	
		if ( t.substring(0,1) != "-" )
		{
			if (i == (t.length - 4) || i == (t.length - 7) || (i == (t.length - 10)) || (i == (t.length - 13)))
			{
				separador = SIMBOLO;
				nn++;
			}
		} else {
			if ( i != 0 )
			{
				if (i == (t.length - 4) || i == (t.length - 7) || (i == (t.length - 10)) || (i == (t.length - 13)))
				{
					separador = SIMBOLO;
					nn++;
				}
			}
		}
	
		salida = t.substring(i, i + 1) + separador + salida;
		i--;
   }
   
	if (posDec != -1)
	{
		if (dec > 0)
			ctl.value = salida + "," + stsDecimales;
		else
			ctl.value = salida ;
	}
	else
		ctl.value = salida;
}

function OnlyDouble(ctl)
{
   var str;
   var strDecimales;
   var largo;
   var contador;
   var pos;
   var i;
   
   str = ctl.value;
   largo = str.length;   
   
	contador = 0;
	pos = 0;
	
	for(i=0;i<largo;i++)
	{
		if ((pos = str.indexOf (",", pos)) == -1 )
		{
			break;
		} else {
			contador++;
			pos++;
			if (contador > 1)
			{
				ctl.value = str.substring(0, largo - 1);
				return true;
			}
		}
   }
   
   if (!(isDoubleChar(str.substring(largo - 1,largo))))
   {
      ctl.value = str.substring(0, largo - 1);
   } 
   return true;
}

function formatDouble(ctr,numeroEntero, decimales)
{
	var texto = ctr.value;
	var i =0;
	var pase;
	var posicionDecimal = 0;

	pase = false;

	for (i=0;i<texto.length;i++)
	{

	   if (!(isDoubleChar(texto.substring(i,i+1))))
	   {
		  ctr.value = texto.substring(0,i);
		  return true;
	   } 
		
		if (texto.substring(i,i+1) != ",")
		{
			if ((i + 1> numeroEntero) && (pase == false))
			{
				ctr.value = texto.substring(0,i);
				return true;
			}

			if ((i + 1- posicionDecimal> decimales) && (pase == true))
			{
				ctr.value = texto.substring(0,i);
				return true;
			}

		}
		else
		{
			if (decimales > 0)
			{
				if (pase == true)
				{
					ctr.value = texto.substring(0,i);
					return true;
				}

				pase = true;
				posicionDecimal = i + 1;
			}
			else{
				ctr.value = texto.substring(0,i);
				return true ;
			}
		}
	}

	ctr.value = texto.substring(0,i);
}

function FormatNumero(sts)
{
var salida;
var t;
var caracter;
var separador;
var i;
var largo;
var str;
var nn;
var esNegativo;
   
	if (sts.length == 0 )
		return '';

	t = "";
 
	for (i=0;i<sts.length;i++)
	{
		caracter = sts.substring(i, i + 1);
		
		if ((caracter != "."))
			t = t + caracter;
		else
			nn = nn - 1;
	}

	salida = "";
	
	while(i>=0)
	{
		separador = "";
	
		if ( t.substring(0,1) != "-" )
		{
			if (i == (t.length - 4) || i == (t.length - 7) || (i == (t.length - 10)) || (i == (t.length - 13)))
			{
				separador = ".";
				nn++;
			}
		} else {
			if ( i != 0 )
			{
				if (i == (t.length - 4) || i == (t.length - 7) || (i == (t.length - 10)) || (i == (t.length - 13)))
				{
					separador = ".";
					nn++;
				}
			}
		}
	
		salida = t.substring(i, i + 1) + separador + salida;
		i--;
   }
   
	return salida;
}

function FormatDoble(ctr,numeroEntero, decimales)
{
	var texto = ctr.value;
	var i =0;
	var pase;
	var posicionDecimal = 0;

	pase = false;

	for (i=0;i<texto.length;i++)
	{

	   if (!(isDoubleChar(texto.substring(i,i+1))))
	   {
		  ctr.value = (texto.substring(0,i));
		  return true;
	   } 
		
		if (texto.substring(i,i+1) != ",")
		{
				if ((i + 1> numeroEntero) && (pase == false))
				{
					ctr.value = FormatNumero(texto.substring(0,i));
					return true;
				}

				if ((i + 1- posicionDecimal> decimales) && (pase == true))
				{
					ctr.value = FormatNumero(texto.substring(0,i));
					return true;
				}
		}
		else
		{
			if (decimales > 0)
			{
				if (pase == true)
				{
					ctr.value = FormatNumero(texto.substring(0,i));
					return true;
				}

				pase = true;
				posicionDecimal = i + 1;
			}
			else{
				ctr.value = FormatNumero(texto.substring(0,i));
				return true ;
			}
		}
	}

	ctr.value = FormatNumero(texto.substring(0,i));
}


function Redondear(num,dec)
{
	tmp = num * Math.pow(10,dec); 
	tmp = Math.round(tmp); 
	strTmp = new String (tmp); 
	len = strTmp.length; 
	return strTmp.substring(0, len - dec) + "," + strTmp.substring(len - dec, len); 
}


function isValidPatente(patente)
{
	var numerico;
	var separador="-";
	var i = 0 ;
	var patenteAux = patente.value;
	
    if (patenteAux.length < 9 )
	   return false;

	if (isNumber(patenteAux.substring(0,2)))
       return false;
	
	if (!isNumber(patenteAux.substring(3,7)))
       return false;

    if (patenteAux.substring(2,3) != "-")
	   return false;

	if (patenteAux.substring(7,8) != "-")
	    return false;

	return true;
	   
}


function verificarhms(h,m,s)
{ // JPAM 16/03/2004
	if (h.value == '')
		{
		alert("Ingrese la hora");
		h.focus();
		return false;
		}
	if (m.value == '')
		{
		alert("Ingrese los minutos");
		m.focus();
		return false;
		}
	if (s.value == '')
		{
		alert("Ingrese los segundos");
		s.focus();
		return false;
		}
	if (h.value < 0 || h.value >23)
	{
		alert("Ingrese una hora válida (0-23)");
		h.focus();
		return false;
	}
	if (m.value < 0 || m.value >59)
	{
		alert("Ingrese minutos válidos (0-59)");
		m.focus();
		return false;
	}
	if (s.value < 0 || s.value >59)
	{
		alert("Ingrese segundos válidos (0-59)");
		s.focus();
		return false;
	}
	return true;
}

function EsNumero(n)
{ // JPAM 17/03/2004
	var aux;
	if (isNaN(n.value) || n.value.length == 0)
		{
		alert("Ingrese un número");
		n.focus();
		return false;
		}
	if (isNaN(parseInt(n.value)))
		return false;
	else 
	{
		aux = "" + parseInt(n.value) ;
		if (n.value.length != aux.length)
		{
			alert("Ingrese un número sin decimales");
			n.focus();
			return false;
		}
	}
	return true;
}

function FormatoNumero2Digitos(n)
{ // JPAM 17/03/2004
var num ;
num = n.value*1 ;
	if (n.value < 10)
	{
		n.value = '0' + num ;
	}
	return true ;
}

function EsNumeroSinoCambia(n,salida)
{ // JPAM 19/03/2004
	if (isNaN(n.value) || n.value.length == 0)
		{
		alert("Ingrese un número");
		n.value = salida ;
		n.focus();
		return false;
		}
	return true;
}

function EsDouble(n)
{ // JPAM 24/03/2004
	msg = "Ingrese un número, puede ser separado por ',' para decimal ";
	if (isFinite(n.value))
		return true;
	else {
		alert(msg) ;
		n.focus() ;
		return false ;
		}
}

function EsDoubleConEnteroFijo(n, intpermitido)
{ // JPAM 24/03/2004
	var parteEntera, tmp ;

	msg = "Ingrese un número, puede ser separado por ',' para decimal ";
	
	if (isFinite(n.value))
	{
		tmp = "" + n.value ;
		if (tmp.length == 0)
		{
			alert("ingrese un número")
				n.focus() ;
				return false ;
		}
		if (isNaN(parseInt(n.value)))
			return false ;
		else
		{
			parteEntera = "" + parseInt(n.value)
			intpermitido = intpermitido*1
			if (parteEntera.length <= intpermitido)
				return true ;
			else
			{
				alert("El número excede el largo de la parte entera ("+ intpermitido +")") ;
				n.focus() ;
				return false ;
			}
		}
	}
	else
	{
		alert(msg) ;
		n.focus() ;
		return false ;
	}
}

function EsMenorfecha1fecha2(d1,m1,a1,d2,m2,a2) 
{ // JPAM 24/03/2004
	msgA = "El año ingresado es inferior a lo permitido."
	msgM = "El mes ingresado es inferior a lo permitido."
	msgD = "El dia ingresado es inferior o igual al anterior (" + d1.value +")"
	if (a1.value < a2.value) {
		return true ;
	}
	else {
		if (a1.value == a2.value) {
			if (m1.value < m2.value) {
				return true ;
			}
			else {
				if (m1.value == m2.value) {
					if (d1.value < d2.value) {
						return true ;
					}
					else {
						alert(msgD) ;
						d2.focus() ;
						return false ;
					}
				}
				else {
					alert(msgM) ;
					m2.focus() ;
					return false;
				}
			}
		}
		else {
			alert(msgA) ;
			a2.focus() ;
			return false ;
		}
	}
}

function EstaSeleccionado(n)
{// JPAM 25/03/2004
	if (n.value <= 0 )
	{
		alert("Seleccione un elemento de la lista.")
		n.focus();
		return false;
	}
	return true ;
}


    function verificarFechaNacimientoEvaluacion(d, m, a, anoActual ){
        if ((m.value == 1)||(m.value == 3)||(m.value == 5)||(m.value == 7)||(m.value == 8)||(m.value == 10)||(m.value == 12)){
            if ((d.value < 1)||(d.value > 31)){
                alert("Ingrese una cantidad de días válida(máx 31)");
                d.value = -1;
                d.focus();
                return false;
            }
         }
        else{
            if ((m.value == 4)||(m.value == 6)||(m.value == 9)||(m.value == 11)){
                if ((d.value < 1)||(d.value > 30)){
                    alert("Ingrese una cantidad de días válida(máx 30)");
                    d.value = -1;                    
                    d.focus();
                    return false;
                }
            }
            if (m.value == 2){
                if ((a.value % 4) == 0) {
                    if ((d.value < 1)||(d.value > 29)){
                        alert("Ingrese una cantidad de días válida(máx 29)");
                        d.value = -1;                        
                        d.focus();
                        return false;
                    }
                 }
                else {
                    if ((d.value < 1)||(d.value > 28)){
                        alert("Ingrese una cantidad de días válida(máx 28)");
                        d.value = -1;
                        d.focus();
                        return false;
                    }
                }
            }
        }
        if ((m.value < 1)||(m.value > 12)) {
            alert("Ingrese un mes válido");
            m.value = -1;
            m.focus();
            return false;
        }	
        if ((a.value > anoActual)) {
            alert("Ingrese un año válido no superior al "+anoActual);
            a.focus();
            a.value = -1;            
            return false;
        }
        return true ;
        }

        //actualiza el valor seleccionado en un elemento select dado
		function ActualizaSeleccionSelect(select, codSeleccionado){
			select.value=codSeleccionado;
			return;
		}


		 function verificarFechaContraFechaActual(d, m, a , diaActual, mesActual, anoActual ){
        if ((m.value == 1)||(m.value == 3)||(m.value == 5)||(m.value == 7)||(m.value == 8)||(m.value == 10)||(m.value == 12)){
            if ((d.value < 1)||(d.value > 31)){
                alert("Ingrese una cantidad de días válida(máx 31)");
                d.value = -1;
                d.focus();
                return false;
            }
         }
        else{
            if ((m.value == 4)||(m.value == 6)||(m.value == 9)||(m.value == 11)){
                if ((d.value < 1)||(d.value > 30)){
                    alert("Ingrese una cantidad de días válida(máx 30)");
                    d.value = -1;                    
                    d.focus();
                    return false;
                }
            }
            if (m.value == 2){
                if ((a.value % 4) == 0) {
                    if ((d.value < 1)||(d.value > 29)){
                        alert("Ingrese una cantidad de días válida(máx 29)");
                        d.value = -1;                        
                        d.focus();
                        return false;
                    }
                 }
                else {
                    if ((d.value < 1)||(d.value > 28)){
                        alert("Ingrese una cantidad de días válida(máx 28)");
                        d.value = -1;
                        d.focus();
                        return false;
                    }
                }
            }
        }
        if ((m.value < 1)||(m.value > 12)) {
            alert("Ingrese un mes válido");
            m.value = -1;
            m.focus();
            return false;
        }	
        if (a.value == -1) {
            alert("Ingrese un año válido");
            a.value = -1;
            a.focus();
            return false;
        }	
            
		var dateContable = new Date(anoActual.value , mesActual.value, diaActual.value);
		var dateNacimiento = new Date( a.value , m.value, d.value );



		if ( dateNacimiento.getTime() > dateContable.getTime()  ){
			alert( "La fecha de nacimiento no puede ser superior a la fecha actual");
			a.focus();
			return false;
		}

        return true ;
        }

		function funValidaNumeroDocumento(tipoDoc, numDoc, tipo, minLen, maxLen)
		{
			if(numDoc.length <= 0) return false;

// se valida el largo del documento segun parametros tipoDocumento / largo minimo 
			if(tipoDoc == tipo)
			{

				if(numDoc.length < minLen) return false;
				if(numDoc.length > maxLen) return false;

			}


			return true;
		}
        
		 function isValidAlpha(input){
            var inputString = input.value;
            var final="";
            for ( var i=0; i<inputString.length; i++){
                if(isAlpha(inputString.charAt(i))) {
                    final=final+inputString.charAt(i);
                }
            }
                input.value=final;
                return true;
        }



        function isAlpha(str){
            var validChars = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZáéíóú ";
            if (str.length!=1) {
                return false;
            }
            if ( validChars.indexOf(str)!=-1) {
                return true;
            } else {
                return false;
            }
        }        

		function trim(s) {
            while (s.substring(0,1) == ' ') {
                s = s.substring(1,s.length);
            }
            while (s.substring(s.length-1,s.length) == ' ') {
                s = s.substring(0,s.length-1);
            }
            return s;
        }    

		function validaFormatoAlfabetico(input, mensaje) {

            if (trim(input.value).length > 0) {
                for(i=0; i < input.value.length; i++ ) {
                    if(! isAlpha(input.value.substring(i,i+1))) {
                        alert(mensaje);
                        input.value = trim(input.value);
                        input.focus();
                        return false;
                    }
                }     
                return true;
            } else {
                return true;
            }
        
        }

	function convierteAMayusculas(input)
	{
	  var text = input.value;
	  text = text.toUpperCase();

	  input.value = text;
	
	}

