
function ValidaVacio(Elemento,campo,requerido)
{
	if ((Elemento.value=="") && (requerido==true))
	{
		alert("Debe ingresar " + campo)
		Elemento.focus();
		if (Elemento.type=="Text")
			Elemento.select();
		return false;
	}
	return true;
}

function ValidaRadioButton(Elemento,campo,requerido)
{
    var cnt = -1;
    for (var i=Elemento.length-1; i > -1; i--) 
    {
        if (Elemento[i].checked) 
        {
        	cnt = i; 
        	i = -1;
        }
    }
    if (cnt==-1)
    {
    	alert("Debe Seleccionar " + campo);
    	return false;
    }
    else
    	return true;

}

function ValidaFile(Elemento,campo,requerido,iType) 
{
	var file=Elemento.value;
	//var iType=1;
	//if (ValidaVacio(Elemento,campo,requerido)==false)
		//return false;
	if (iType==1) 
		extArray = new Array(".gif",".jpg",".png",".bmp");
	if (iType==2)
		extArray = new Array(".pdf");

	allowSubmit = false;
	
	if (Elemento.value!="")
	{
		while (file.indexOf("\\") != -1) file = file.slice(file.indexOf("\\") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) 
		{
			if (extArray[i] == ext) 
			{
				allowSubmit = true;
				break;
			}
		} 
		
		if (!allowSubmit) 
		{
			alert("Debe ingresar un archivo con extensiones " + (extArray.join(" ")) + " en el campo " + campo);
			return false;
		}
	}	
	return true;
} //FIN VALIDA ARCHIVO
function ValidaNumero(Elemento,campo,requerido)
{
	if (ValidaVacio(Elemento,campo,requerido)==false)
		return false;
	if (Elemento.value!="")
	{
		exprecion=/^([0-9])*$/;
		if (exprecion.test(Elemento.value))
			return true
		else
		{
			alert("Debe ingresar solo numeros en " + campo)
			Elemento.focus();
			Elemento.select();
			return false;
		}
	}
	return true;
}

function ValidaLetras(Elemento,campo,requerido)
{
	if (ValidaVacio(Elemento,campo,requerido)==false)
		return false;
	if (Elemento.value!="")
	{
		exprecion=/^([A-Za-zÑñ\s])*$/;
		if (exprecion.test(Elemento.value))
			return true
		else
		{
			alert("Debe ingresar solo letras en " + campo)
			Elemento.focus();
			Elemento.select();
			return false;
		}
	}
	return true;
}

function ValidaAlfaNumerico(Elemento,campo,requerido)
{
	if (ValidaVacio(Elemento,campo,requerido)==false)
		return false;
	if (Elemento.value!="")
	{
		exprecion=/^([A-Za-zÑñ0-9\s])*/;
		if (exprecion.test(Elemento.value))
			return true	
		else
		{
			alert("Debe ingresar solo letras o numeros en: " + campo)
			Elemento.focus();
			Elemento.select();
			return false;
		}
	}
	return true;
}

function ValidaFecha(Elemento,campo,requerido)
{
	if (ValidaVacio(Elemento,campo,requerido)==false)
		return false;
	if (Elemento.value!="")
	{
		exprecion=/^\d{1,2}\-\d{1,2}\-\d{2,4}$/;
		if (exprecion.test(Elemento.value))
			return true
		else
		{
			alert('Debe ingresar una fecha en ' + campo + '\r Ej: xx-xx-xxxx');
			Elemento.focus();
			Elemento.select();
			return false;
		}
	}
	return true;
}

function ValidaHora(Elemento,campo,requerido)
{
	if (ValidaVacio(Elemento,campo,requerido)==false)
		return false;
	if (Elemento.value!="")
	{
		exprecion=/^(0[1-9]|1\d|2[0-3]):([0-5]\d)$/;
		if (exprecion.test(Elemento.value))
			return true
		else
		{
			alert('Debe ingresar una hora en ' + campo + '\r Ej: xx:xx');
			Elemento.focus();
			Elemento.select();
			return false;
		}
	}
	return true;
}
function ValidaTelefono(Elemento,campo,requerido)
{
	if (ValidaVacio(Elemento,campo,requerido)==false)
		return false;
	if (Elemento.value!="")
	{
		exprecion=/^[0-9]{2,3}-[0-9]{6,7}$/;
		if (exprecion.test(Elemento.value))
			return true
		else
		{
			alert('Debe ingresar un numero telefonico en ' + campo + '\r Ej: xx-xxxxxxx');
			Elemento.focus();
			Elemento.select();
			return false;
		}
	}
	return true;
}



function ValidaMail(Elemento,campo,requerido)
{
	var E=Elemento;
	if (ValidaVacio(Elemento,campo,requerido)==false)
		return false;
	if (Elemento.value!="")
	{
		exprecion=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if (exprecion.test(Elemento.value))
			return true
		else
		{
			alert('Debe ingresar un correo electronico en ' + campo + '\r Ej: usuario@dominio');
			E.focus();
			E.select();
			return false;
		}
	}
	return true;
}


function ValidaRut(Elemento,campo,requerido)
{
	var E=Elemento;
	var rut=E.value;
	if (ValidaVacio(Elemento,campo,requerido)==false)
		return false;
	var tmpstr = "";
	for ( i=0; i < rut.length ; i++ )
		if ( rut.charAt(i) != ' ' && rut.charAt(i) != '.' && rut.charAt(i) != '-' )
			tmpstr = tmpstr + rut.charAt(i);
	rut = tmpstr;
	largo = rut.length;
// [VARM+]
	tmpstr = "";
	for ( i=0; rut.charAt(i) == '0' ; i++ );
		for (; i < rut.length ; i++ )
			tmpstr = tmpstr + rut.charAt(i);
	rut = tmpstr;
	largo = rut.length;
// [VARM-]
	if ( largo < 2 )
	{
		alert("Debe ingresar el rut completo.");
		Elemento.focus();
		Elemento.select();
		return false;
	}
	for (i=0; i < largo ; i++ )
	{
		if ( rut.charAt(i) != "0" && rut.charAt(i) != "1" && rut.charAt(i) !="2" && rut.charAt(i) != "3" && rut.charAt(i) != "4" && rut.charAt(i) !="5" && rut.charAt(i) != "6" && rut.charAt(i) != "7" && rut.charAt(i) !="8" && rut.charAt(i) != "9" && rut.charAt(i) !="k" && rut.charAt(i) != "K" )
		{
			alert("El valor ingresado no corresponde a un R.U.T valido.");
			Elemento.focus();
			Elemento.select();
			return false;
		}
	}
	var invertido = "";
	for ( i=(largo-1),j=0; i>=0; i--,j++ )
		invertido = invertido + rut.charAt(i);
	var drut = "";
	drut = drut + invertido.charAt(0);
	drut = drut + '-';
	cnt = 0;
	for ( i=1,j=2; i<largo; i++,j++ )
	{
		if ( cnt == 3 )
		{
			drut = drut + '.';
			j++;
			drut = drut + invertido.charAt(i);
			cnt = 1;
		}
		else
		{
			drut = drut + invertido.charAt(i);
			cnt++;
		}
	}
	invertido = "";
	for ( i=(drut.length-1),j=0; i>=0; i--,j++ )
		invertido = invertido + drut.charAt(i);
	Elemento.value = invertido;
	if ( checkDV(Elemento,rut,campo) )
		return true;
	return false;
}

function checkDV(Elemento, crut,campo )
{
	var E=Elemento;
	largo = crut.length;
	if ( largo < 2 )
	{
		alert("Debe ingresar el rut completo en " + campo);
		Elemento.focus();
		Elemento.select();
		return false;
	}
	if ( largo > 2 )
		rut = crut.substring(0, largo - 1);
	else
		rut = crut.charAt(0);
		
	dv = crut.charAt(largo - 1);
	checkCDV(Elemento, dv, campo );
	if ( rut == null || dv == null )
		return 0;
	var dvr = '0';
	suma = 0;
	mul = 2;
	for (i= rut.length -1 ; i >= 0; i--)
	{
		suma = suma + rut.charAt(i) * mul;
		if (mul == 7)
			mul = 2;
		else
			mul++;
	}
	res = suma % 11;
	if (res==1)
		dvr = 'k';
	else if (res==0)
		dvr = '0';
	else
	{
		dvi = 11-res;
		dvr = dvi + "";
	}
	if ( dvr != dv.toLowerCase() )
	{
		alert("EL rut es incorrecto.");
		Elemento.focus();
		Elemento.value = "";
		return false;
	}
	return true;
}

function checkCDV(Elemento, dvr, campo )
{
	dv = dvr + "";
	if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')
	{
		alert("Debe ingresar un digito verificador valido en "+ campo);
		Elemento.focus();
		Elemento.select();
		return false;
	}
	return true;
}

