/********************************************************************************/
/*	achivo          :	validaciones.js											*/
/*	diseñado por	:	Frameworks												*/
/*	modulo			:	SEGURIDADES												*/
/*	descripcion	    :	validaciones para campos de texto                       */
/*																				*/
/********************************************************************************/
/*	Este programa es parte del paquete administrativo financiero propiedad		*/
/*	de Frameworks																*/
/*	Su uso no autorizado queda expresamente prohibido asi como					*/
/*	cualquier alteracion o agregado hecho por alguno de sus						*/
/*	usuarios sin el debido consentimiento por escrito de Frameworks Cía. Ltda.	*/
/********************************************************************************/
/*	fecha de escritura:	25/agosto/2006											*/
/*	autor		  :	Diego Romero V.										        */
/*  Restricción   : debe llamarse en el onsubmit de la forma o en el onblur     */
/*                  del campo de texto                                          */
/********************************************************************************/
/*	modificaciones																*/
/*	fecha		autor		razon												*/
/*																				*/
/********************************************************************************/

var _Errmsg = "";
var _vfoco = null;

function mostrarErrores()
{
	if (_Errmsg != "")
	{
		alert(_Errmsg);
		//vbAlert(_Errmsg);
		try
		{
			_vfoco.focus();			
		}
		catch(e)
		{}
		_Errmsg = "";
		_vfoco = null;
		event.returnValue = false;		
		return false;
	}
	else return true;
}

function ValidaNumero(IDObj, mensaje)
{
	try
	{
	    if (document.getElementById(IDObj).value != "")
	    {
	        var texto = document.getElementById(IDObj).value;
   		    if (isNaN(texto)) //no es numero
		    {
			    _Errmsg += "- " + mensaje + "\n";
			    _vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
			    return false;
		    }
	    }
	    return true;
	}
	catch(e)
	{
		alert(IDObj + ": " + e.message);
		status = e.message;
		event.returnValue = false;		
		return false;
	}
}

function validaCampo(IDObj,mensaje)
{
	try
	{
		if (document.getElementById(IDObj).value == "")
		{
			_Errmsg += "- " + mensaje + "\n";
			_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
			return false;
		}
		return true;
	}
	catch(e)
	{
		alert(IDObj + ": " + e.message);
		status = e.message;
		event.returnValue = false;		
		return false;
	}
}

function validaCampoDDL(IDObj,mensaje)
{
	try
	{
		if (document.getElementById(IDObj).value == "")
		{
			_Errmsg += "- " + mensaje + "\n";
			_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
			return false;
		}
		
		valor = document.getElementById(IDObj).value;
		if (valor != "" && (parseInt(valor) < parseInt(0)) )
	    {
		    _Errmsg += "- " + mensaje + "\n";
		    _vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
		    return false;
	    }	
		return true;
	}
	catch(e)
	{
		alert(IDObj + ": " + e.message);
		status = e.message;
		event.returnValue = false;		
		return false;
	}
}

function validaCampoDDLNoSeleccionable(IDObj,texto,mensaje)
{
	try
	{
		if (texto.toUpperCase().indexOf('(NO SELECCIONABLE)', 0) > 0)
		{
			_Errmsg += "- " + mensaje + "\n";
			_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
			return false;
		}

		return true;
	}
	catch(e)
	{
		alert(IDObj + ": " + e.message);
		status = e.message;
		event.returnValue = false;		
		return false;
	}
}

function agregaError(mensaje)
{
	_Errmsg += "- " + mensaje + "\n";		
	return false;
}

function validaInnerText(IDObj,mensaje)
{
	try{
	if (document.getElementById(IDObj).innerText == "")
	{
		_Errmsg += "- " + mensaje + "\n";
		_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
		return false;
	}
	return true;
	}catch(e){
		alert(IDObj + ": " + e.message);
		status = e.message;
		event.returnValue = false;		
		return false;
	}
}

function validaComaPunto(Valor,mensaje)
{
	try
	{
	    Valor = document.getElementById(Valor).value;
	    if (Valor.indexOf(",,")>0 || Valor.indexOf("..")>0)
	    {
		    _Errmsg += "- " + mensaje + "\n";
		    _vfoco = (_vfoco==(void 0))? Valor:_vfoco;
		    return false;
	    }
	    return true;
	}catch(e){
		alert(Valor + ": " + e.message);
		status = e.message;
		event.returnValue = false;		
		return false;
	}
}

function validaLongitud(IDObj,mensaje,min)
{
	if (document.getElementById(IDObj).value != "" && document.getElementById(IDObj).value.length < min)
	{
		_Errmsg += "- " + mensaje + "\n";
		_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
		return false;
	}	
	return true;
}

function validaLongitud(IDObj,mensaje,min,max)
{
	if (document.getElementById(IDObj).value != "" && (document.getElementById(IDObj).value.length < min || document.getElementById(IDObj).value.length > max))
	{
		_Errmsg += "- " + mensaje + "\n";
		_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
		return false;
	}	
	return true;
}


function validaRango(IDObj, mensaje, min, max)
{
	valor = document.getElementById(IDObj).value.replace(",",".")	
	if (valor != "" && (parseFloat(valor) < parseFloat(min) || parseFloat(valor) > 10.0 ))
	{
		_Errmsg += "- " + mensaje + "\n";
		_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj):_vfoco;
		return false;
	}	
	return true;
}

function validaIgualdad(IDObj1,IDObj2,mensaje)
{
	if (document.getElementById(IDObj1).value != "" && document.getElementById(IDObj2).value != "" && (document.getElementById(IDObj1).value != document.getElementById(IDObj2).value))
	{
		_Errmsg += "- " + mensaje + "\n";
		_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj2):_vfoco;
		return false;
	}	
	return true;
}

function validaNoIgualdad(IDObj1,IDObj2,mensaje)
{
	if (document.getElementById(IDObj1).value != "" && document.getElementById(IDObj2).value != "" && (document.getElementById(IDObj1).value == document.getElementById(IDObj2).value))
	{
		_Errmsg += "- " + mensaje + "\n";
		_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj2):_vfoco;
		return false;
	}	
	return true;
}

function comparaFechas(IDObj1,IDObj2,mensaje,formato,comparacion)
{
	var validarFecha = true;
	var fecha1 = document.getElementById(IDObj1).value;
	var fecha2 = document.getElementById(IDObj2).value;
	var f1,f2;
	
	if(fecha1 == '' || fecha2 == '') return true;
	if(formato.length != 10) validarFecha = false;
	if(comparacion != '>' && comparacion != '<' && comparacion != '>=' && comparacion != '<=' && comparacion != '==')
	{
		alert('Comparación no válida use: > < >= <= ==');
	}

	formato = formato.toUpperCase();
	posDia = formato.indexOf("DD");
	posMes = formato.indexOf("MM");
	posAnio = formato.indexOf("YYYY");
	
	if (posDia < 0 || posMes < 0 || posAnio < 0) 
		validarFecha = false;
	
	if (validarFecha)
	{
		vDia = fecha1.substr(posDia,2);
		vMes = fecha1.substr(posMes,2);
		vAnio = fecha1.substr(posAnio,4);
		f1 = new Date(vAnio,vMes-1,vDia);
		if(f1.getFullYear()!=parseInt(vAnio-0) || (parseInt(f1.getMonth())+1)!= parseInt(vMes-0) || f1.getDate()!=parseInt(vDia-0))
		{
			alert('Fecha inválida');
		}
		
		vDia = fecha2.substr(posDia,2);
		vMes = fecha2.substr(posMes,2);
		vAnio = fecha2.substr(posAnio,4);
		f2 = new Date(vAnio,vMes-1,vDia);
		if(f2.getFullYear()!=parseInt(vAnio-0) || (parseInt(f2.getMonth())+1)!= parseInt(vMes-0) || f2.getDate()!=parseInt(vDia-0))
		{
			alert('Fecha inválida');
		}
		
		if(!eval('f1'+ comparacion + 'f2'))
		{
			_Errmsg += "- " + mensaje + "\n";
			_vfoco = (_vfoco==(void 0))? document.getElementById(IDObj1):_vfoco;
			return false;
		}	
		return true;	
	}else
	{
		alert('Formato no validable');
	}
}

function ValidaNumeroOnPaste(campo)
{
	var texto= new String(window.clipboardData.getData( "Text" ));	
	if(texto!='') 
	{
		if (isNaN(texto)) //no es numero
		{
			window.clipboardData.setData( "Text", "" );	
			campo.value = "";
			campo.focus();	
			return false;
		}
		else //si es numero
		{
		}
	}
	return true;
}

/*
function validaEmail(IDObj, mensaje)
{
alert(1);
    var filter= /(^[(a-z|0-9)]([(a-z|0_9)_\.]*)@([[a-z|0-9]_\.]*)([.][(a-z|0-9)]{3})$)|(^[(a-z|0-9)]([(a-z|0-9)_\.]*)@([(a-z|0-9)_\.]*)(\.[(a-z|0-9)]{3})(\.[(a-z|0-9)]{2})*$)/i;
    if (document.getElementById(IDObj).value == "") 
        return true;
alert(filter);
    if (filter.test(s))
        return true;
    else
    {
    alert(333)
		_Errmsg += "- " + mensaje + "\n";
        campo.focus();
        return false;
    }
}*/



function Confirma_Grabar(valor, cedu)
{ 
   var text=document.getElementById(valor).value;
   if (/^[a-z0-9*\s.]+$/i.test(text))
      {
       if (text.length < 5)
           {
               //txtApeNom.focus();
               alert("Ingrese m\u00e1s de 4 Letras, por favor");
               return (false);
           }
           else
               {
                var txtini = text.substring(0, 4);
                if (/^[a-z*\s.]+$/i.test(txtini))
                   {
                    if (/^[*\s.]+$/i.test(txtini))
                       {
                        //valor.focus();
                        alert("Ingrese Letras como principio de la consulta, por favor");
                        return (false) 
                        }
                        else

//cédula
     {
     var text=document.getElementById(cedu).value;
     if (text.length < 10)
       {
           //cedu.focus();
           alert("Ingrese 10 d\u00edgitos del Nro. de c\u00e9dula, por favor");
           return (false);
       }
       else
           { 
             if (/^[0-9]+$/i.test(text))
                 {
                   if (text > 0)
                       {
                          return (true);
                       }
                       else
                           {
                            alert("Ingrese una c\u00e9dula valida, por favor  " + text);
                            return (false);
                           }
                 }
               else 
                   {
      	            //cedu.focus();
                    alert("Ingrese n\u00fameros, por favor  " + text);
                    return (false);
                   }
           }       
           return (true)
                            }
                   }
               else
                   { 
                    //valor.focus();
                    alert("Ingrese Letras como principio de la consulta, por favor");
                    return (false) 
                   }
               }
      }
      else 
      {
      	//valor.focus();
        alert("Ingrese Letras y n\u00fameros, por favor");
        return (false);
       }
}


function Confirma_Titulo(valor)
{ 
     var text=valor.value;
     if (text.length < 11)
       {
           valor.focus();
           alert("Ingrese 11 d\u00edgitos del Nro. del t\u00edtulo, por favor");
           return (false);
       }
       else
           { 
             if (/^[0-9]+$/i.test(text))
                 {
                   
                    if (text > 0)
                       {
                          return (true);
                       }
                       else
                       {
                        alert("Ingrese el Nro. del t\u00edtulo, por favor  " + text);
                        return (false);
                       }
                 }
               else 
                   {
      	            valor.focus();
                    alert("Ingrese n\u00fameros, por favor  " + text);
                    return (false);
                   }
           }
}

function Confirma_Numero(valor)
{ 
      var text=valor.value;
      if ((text.length = 0) || (text.length > 7))       {
           valor.focus();
           alert("Ingrese de 1 a 7 d\u00edgitos, por favor");
           return (false);
       }

     else      
          {
              if (/^[0-9]+$/i.test(text))
                 {
                  
                   if (text > 0)
                       {
                          return (true);
                       }
                       else
                       {
                        alert("Ingrese de 1 a 7 d\u00edgitos > 0, por favor " + text);
                        return (false);
                       }
                 }
               else 
                   {
      	            valor.focus();
                    alert("Ingrese n\u00fameros, por favor " + text);
                    return (false);
                   }
            }
}

