// JavaScript Document
/* validaciones.js */
/*************************************************************************
 Descripción: Validación del CUIT
 Parametro: CUIT - numero de CUIT
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function ValidarCuit(CUIT)
{
    var re = /-/g;
    var NCuit = CUIT.replace(re, "");
    var suma;
    var resto;
    var verif;
//    var pos = CUIT.split('');
    var pos = NCuit.split('');
  
    
    if (! /^\d{11}$/.test(NCuit)) return false;

    while (true) {
        suma = (pos[0] * 5 + pos[1] * 4 + pos[2] * 3 +
        pos[3] * 2 + pos[4] * 7 + pos[5] * 6 +
        pos[6] * 5 + pos[7] * 4 + pos[8] * 3 + pos[9] * 2);
        resto = suma % 11;

        if (resto == 0) {
            verif = 0;
            break;
        }
        else if (resto == 1 && (pos[1] == 0 || pos[6] == 7)) {
            pos[1] = 4;
            continue;
        }
        else {
            verif = 11 - resto;
            break;
        }
    }
    return pos[10] == verif;
}


/*************************************************************************
 Descripción: Validación del Email
 Parametro: email
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function ValidarMail(email)
{
    var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i
    return emailReg.test(email);
}

/*************************************************************************
 Descripción: Filtrado de los caracteres de (CUIT, Cta bancaria, etc)
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function FiltrarCaracteresNumRay(e)
{
    var KPValor = 45; 

    if(window.event) {
        //IE
        if (event.keyCode==KPValor || event.keyCode==8 || (event.keyCode>=48 & event.keyCode<=57)){
            return true;
        }
        else{
            window.event.returnValue = 0;
        } 
    }
    else if(e.which) {
        //Netscape
        if (e.which==KPValor || e.which==8 || (e.which>=48 & e.which<=57)){
            return true;
        }
        else{
            return false;
        }  
    }
}

/*************************************************************************
 Descripción: Filtrado de los caracteres de Fechas (formato: dd/mm/aaaa)
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function FiltrarCaracteresFecha(e)
{
    var KPValor = 47; 
   
    if(window.event) {
        //IE
        if (event.keyCode==KPValor || event.keyCode==8 || (event.keyCode>=48 & event.keyCode<=57)){
            return true;
        }
        else{
            window.event.returnValue = 0;
        } 
    }
    else if(e.which) {
        //Netscape
        if (e.which==KPValor || e.which==8 || (e.which>=48 & e.which<=57)){
            return true;
        }
        else{
            return false;
        }  
    }
}

/*************************************************************************
 Descripción: autocompleta al fecha bajo el formato de dd/mm/aaaa. Ademas,
              permite el ingreso de fechas con formato de años en "aa" y 
              lo convierte al formato "aaaa" - rango 00 al 17
 Parametro: Fecha a Validar, Minima perimitida, Maxima permitida
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function AutoCompletarFecha(e)
{    
    if(window.event) 
    {   
        //IE
        var bytLongitud = event.srcElement.value.length;
        var arrVar = event.srcElement.value.split('/');
        if (event.keyCode!=8 & event.keyCode!=46)
        {   
            if (((bytLongitud==2) || (bytLongitud==5)) && (arrVar.length<3))
            {
                event.srcElement.value=event.srcElement.value + "/";
            }
        }
        if (bytLongitud==8) 
        {
            if ((event.srcElement.value.substring(6, 8) < 18) && (isNaN(event.srcElement.value.substring(5, 8))))
            {
                event.srcElement.value=event.srcElement.value.substring(0, 5) + "/20" + event.srcElement.value.substring(6, 8);
            }
        }
    }
    else if(e.which) 
    {
        //Netscape
        var bytLongitud = e.target.value.length;
        var arrVar = e.target.value.split('/');
        if (e.which!=8 & e.which!=46)
        {                    
            if (((bytLongitud==2) || (bytLongitud==5)) && arrVar.length<3)
            {
                e.target.value=e.target.value + "/";
            }
        }
        if (bytLongitud==8) 
        {
            if ((e.target.value.substring(6, 8) < 18) && (isNaN(e.target.value.substring(5, 8))))
            {
                e.target.value=e.target.value.substring(0, 5) + "/20" + e.target.value.substring(6, 8);
            }
        }
    }
}

function AutoCompletarCUIT(e)
{    
    if(window.event) 
    {   
        //IE
        var bytLongitud = event.srcElement.value.length;
        var arrVar = event.srcElement.value;
        if (event.keyCode!=8 & event.keyCode!=46)
        {
            if ((bytLongitud==2) || (bytLongitud==11))
                arrVar += '-';
            event.srcElement.value = arrVar;
        }
    }
    else if(e.which) 
    {
        //Netscape
        var bytLongitud = e.target.value.length;
        var arrVar = e.target.value;
        if (e.which!=8 & e.which!=46)
        {
            if ((bytLongitud==2) || (bytLongitud==11))
                arrVar += '-';
            e.target.value = arrVar;
        }
    }
}

/*************************************************************************
 Descripción: Filtrado de los caracteres Numericos
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function FiltrarCaracteresEnt(e)
{
	
    if(window.event) {
        //IE
        if (event.keyCode==0 || event.keyCode==8 || (event.keyCode>=48 & event.keyCode<=57)){
            return true;
        }
        else{
            window.event.returnValue = 0;
        } 
    }
    else if(e.which) {
        //Netscape
        if (e.which==0 || e.which==8 || (e.which>=48 & e.which<=57)){
            return true;
        }
        else{
            return false;
        }  
    }
}

/*************************************************************************
 Descripción: Dependiendo del browser me devuelve el caracter presionado
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function DarCaracterPresionado(evt)
{
	var keyCode = 0;
  	if ("which" in evt)
  	{
  		// NN4 & FF &amp; Opera
    	keyCode=evt.which;
  	} else if ("keyCode" in evt)
  	{
  		// Safari & IE4+
    	keyCode=evt.keyCode;
  	} else if ("keyCode" in window.event)
  	{
  		// IE4+
    	keyCode=window.event.keyCode;
  	} else if ("which" in window.event)
  	{
    	keyCode=evt.which;
  	}
  	return keyCode;
    /*
	if (document.all)
       return event.keyCode;
    else 
    if (document.getElementById)
    {
    	alert(e.which);
    	return e.which;
    }
    */  
}

/*************************************************************************
 Descripción: Dependiendo del browser me devuelve el caracter presionado
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function DarCaracterPresionado(evt)
{
	var keyCode = 0;
  	if ("which" in evt)
  	{
  		// NN4 & FF &amp; Opera
    	keyCode=evt.which;
  	} else if ("keyCode" in evt)
  	{
  		// Safari & IE4+
    	keyCode=evt.keyCode;
  	} else if ("keyCode" in window.event)
  	{
  		// IE4+
    	keyCode=window.event.keyCode;
  	} else if ("which" in window.event)
  	{
    	keyCode=evt.which;
  	}
  	return keyCode;
    /*
	if (document.all)
       return event.keyCode;
    else 
    if (document.getElementById)
    {
    	alert(e.which);
    	return e.which;
    }
    */  
}

/*************************************************************************
 Descripción: Dependiendo del browser me el texto del control que generó el evento
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function DarTextoActual(e)
{
    if (document.all)
        return event.srcElement.value;
    else 
    if (document.getElementById)
        return e.target.value;
}

/*************************************************************************
 Descripción: Dependiendo del browser fija el valor del del control que generó el evento
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function FijarTextoActual(e, texto)
{
    if (document.all)
        event.srcElement.value = texto;
    else 
    if (document.getElementById)
        e.target.value = texto;
}

/*************************************************************************
 Descripción: Filtrado de los caracteres con decimales
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function FiltrarCaracteresDec(e){
    var KPComa = 46;
    var KPPunto = 44;
    var sPunto =',';
    var sComa ='.';
    
    
    var caracter = DarCaracterPresionado(e);
	
    if (caracter == KPPunto)
        caracter = KPComa;
    
    if (caracter == KPComa)
    {
        var TextoActual = DarTextoActual(e);
        
        for(i=0;i<TextoActual.length;i++)
            if(TextoActual.charAt(i)==sComa) return false;
        
        FijarTextoActual(e, TextoActual + sComa);
        return false;
    }
    else    
    if (!((caracter==0) || (caracter==8) || (caracter>=48 & caracter<=57)))
    {
        return false;
    }
//    
//    
//    
//    
//    if (document.all)
//    {
//        if (event.keyCode==46)
//            event.keyCode = KPValor;
//    
//        if (!((event.keyCode==KPValor) || (event.keyCode==8) || (event.keyCode>=48 & event.keyCode<=57)))
//        {
//            event.returnValue=false;
//            event.cancel = true;
//         }
//         else
//        {       
//            if (event.keyCode == KPValor)
//            {
//                var arrVar = event.srcElement.value;
//                for(i=0;i<arrVar.length;i++)
//                    if(arrVar.charAt(i)==".") return false;
//            }
//        }
//    }
//    else if (document.getElementById)
//    {
//        if (e.which==46)
//            e.which = KPValor;
//            
//        if (!(e.which == KPValor || e.which == 8 || (e.which >= 48 & e.which <= 57)))
//        {
//            return false;
//        }
//        else
//        {       
//        
//            if (e.which == KPValor)
//            {
//                var arrVar = e.target.value;
//                for(i=0;i<arrVar.length;i++)
//                    if(arrVar.charAt(i)==",") return false;
//            }
//        }
//    }
}


/* funcion adicional para controlar el ingreso de numeros decimales */
function ValidarDecimal(e){
	var target=null;
	var intCode=null;
	if(bIsIE5){
		e=event;
		valor = event.srcElement.value;
		intCode=parseInt(e.keyCode);
	}else{
		valor = e.target.value;
		intCode=parseInt(e.charCode);
	}

//	alert(valor.search(/^((\d+(\.\d*)?)|((\d*\.)?\d+))$/))
//	alert(intCode)
	if(!( ( intCode==0 ) ||( intCode==46 ) || ( intCode>47 && intCode<60 ) ) ) return false;
	if(intCode==46 && valor.split(".").length>1) return false;
	return true;
}

/*************************************************************************
 Descripción: Filtrado de los caracteres del Organismo
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function FiltrarCaracteresOrganismo(e)
{
    if(window.event) {
        //IE
        if (event.keyCode==8 || (event.keyCode>=48 & event.keyCode<=57) || (event.keyCode>=65 & event.keyCode<=90) || (event.keyCode>=97 & event.keyCode<=122)){
            return true;
        }
        else{
            window.event.returnValue = 0;
        } 
    }
    else if(e.which) {
        //Netscape
        if (e.which==8 || (e.which>=48 & e.which<=57) || (e.which>=65 & e.which<=90) || (e.which>=97 & e.which<=122)){
            return true;
        }
        else{
            return false;
        }  
    }    
}

/*************************************************************************
 Descripción: Valida que un campo string tiene una fecha valida 'd/m/a'
 Parametro: Fecha a Validar, Minima perimitida, Maxima permitida
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function ValidarFecha(strFecha, strMininmo, strMaximo)
{
    strFecha = strFecha.toString();
	var arrFecha = strFecha.split('/');
	if (arrFecha.length!=3) return 'La fecha ingresada no tiene el formato correcto, debe ser dd/mm/aaaa';
		
	if (arrFecha[2]<100 ) arrFecha[2] = parseInt(arrFecha[2]) + 2000;
	arrFecha[1]--;
	var datAux = new Date(arrFecha[2], arrFecha[1], arrFecha[0]);

	if (datAux.getFullYear()!=arrFecha[2]) return 'La fecha ingresada no es válida';
	if (datAux.getMonth()!=arrFecha[1]) return 'La fecha ingresada no es válida';		
	if (datAux.getDate()!=arrFecha[0]) return 'La fecha ingresada no es válida';

	if (strMininmo)
	{	
		var arrFechaMin = strMininmo.split('/');
		if (arrFechaMin[2]<100 ) arrFechaMin[2] = parseInt(arrFechaMin[2]) + 2000;
		arrFechaMin[1]--;
		var datAuxMin = new Date(arrFechaMin[2], arrFechaMin[1], arrFechaMin[0]);
		
		if (datAux<datAuxMin) return 'La fecha ingresada no puede ser menor a ' + strMininmo;
	}

	if (strMaximo)
	{	
		var arrFechaMax = strMaximo.split('/');
		if (arrFechaMax[2]<100 ) arrFechaMax[2] = parseInt(arrFechaMax[2]) + 2000;
		arrFechaMax[1]--;
		var datAuxMax = new Date(arrFechaMax[2], arrFechaMax[1], arrFechaMax[0]);
		
		if (datAux>datAuxMax) return 'La fecha ingresada no puede ser mayor a ' + strMaximo;
	}		

	return '';
}

/*************************************************************************
 Descripción: Valida que un campo string tiene una fecha valida 'd/m/a'
 Parametro: Fecha a Validar, Minima perimitida, Maxima permitida
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function ValidarPeriodo(strFchDesde, strFchHasta)
{
    var datFchDesde = new Date(strFchDesde.substring(6, 11), strFchDesde.substring(3, 5)-1, strFchDesde.substring(0, 2));
    var datFchHasta = new Date(strFchHasta.substring(6, 11), strFchHasta.substring(3, 5)-1, strFchHasta.substring(0, 2));

    if (datFchDesde > datFchHasta) {
        return 'La fecha inicial del Periodo no puede ser mayor a la fecha final del mismo.';
    }
    else{
        return '';
    }
}

/*************************************************************************
 Descripción: Filtrado de los caracteres para Telefono
 Parametro: e - parametro usado por el mozilla
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function FiltrarCaracteresTel(e)
{
    if(window.event) {
        //IE
        if (event.keyCode==8 || event.keyCode==32 || event.keyCode==40 || event.keyCode==41 || event.keyCode==43 || event.keyCode==45 || (event.keyCode>=48 & event.keyCode<=57)){
            return true;
        }
        else{
            window.event.returnValue = 0;
        }
    }
    else if(e.which) {
        //Netscape
        if (e.which==8 || e.which==32 || e.which==40 || e.which==41 || e.which==43 || e.which==45 || (e.which>=48 & e.which<=57)){
            return true;
        }
        else{
            return false;
        }
    }
}

/*************************************************************************
 Descripción: Elimina espacios en blanco al comienzo y al final de una
 cadena si posee espacios internos duplicados los agrupa en uno solo
 Tipo de Función: Publica
 Referencia: 0
*************************************************************************/
function Trim(JSvalue)
{
    var JStemp = JSvalue;
    var JSobj = /^(\s*)([\W\w]*)(\b\s*$)/;
    //Elimina los espacios de delante y detrás
    if (JSobj.test(JStemp))
    {
        JStemp = JStemp.replace(JSobj, '$2');
    }
    //Elimina los espacios duplicados
    var JSobj = / +/g;
    JStemp = JStemp.replace(JSobj, " ");
    if (JStemp == " ")
    {
        JStemp = "";
    }
    return JStemp;
}