function fvSoloNumeros( src )
{
	if (isNS) src.captureEvents(Event.KEYPRESS);
	src.onkeypress = FormValidatorSoloNumeros;
}

function FormValidatorSoloNumeros( e )
{
	var tecla=0;

	if (isIE)
	{
		tecla = window.event.keyCode;
		window.event.keyCode = 0;

		if(tecla == 13)
			document.activeElement.blur();
		else if (tecla < 45 || tecla > 57)
			return  false;
		else
			window.event.keyCode = tecla;
	}
	else if (isNS)
	{
		tecla = e.which;
		e.which = 0;

		if (tecla < 45 || tecla > 57)
			return  false;
		else
			e.which = tecla;
	}


	return (true);
}

function fvSacarNoNumeros( src )
{
	var c, num=false;
	var resp = '', menos='';

	for(i=0; i<src.value.length; i++)
	{
		c = src.value.charAt(i);

		if(c >= '0' && c <= '9')
		{
			num = true;
			resp += c;
		}
		if(c == '-' && !num)
			menos = '-';
		if(c == '.' && src.value.length-i < 4)
			resp += c;
	}

	src.value = menos + resp;
	return true;
}

function fvFormatoNumero( src )
{
	var c, estado=0;
	var resp = '', menos='';
	var i, j;

	for(j=0, i=src.value.length-1; i>=0; i--)
	{
		c = src.value.charAt(i);

		if(c >= '0' && c <= '9')
		{
			menos = '';

			if(j==3)
			{
				resp = c + '.' + resp;
				j = 1;
			}
			else
			{
				resp = c + resp;
				j++;
			}
		}

		if(c == '-')
			menos = '-';
	}

	src.value = menos + resp;

	return true;
}

function fvFormatoMoneda( src )
{
	if(fvFormatoNumero(src))
		src.value = '$ ' + src.value;

	return true;
}

/* fvValidarRUT
 * Toma un un string rut que contiene el RUT sin dígito verificador ni puntuación y un caracter dv
 * que contiene el dígito verificador en mayúscula.  Comprueba si el digito verificador corresponde
 * al RUT.  Los rangos para determinar el tipo de RUT no estan confirmados, son sólo estimaciones.
 * Salidas:
 *  false - La combinación de RUT y dígito verificador es incorrecta.
 *  "extranjero" - El dígito verificador es correcto y el RUT corresponde a un extranjero.
 *  "natural" - El dígito verificador es correcto y el RUT corresponde a un persona natural.
 *  "juridica" - El dígito verificador es correcto y el RUT corresponde a un persona jurídica.
 */
function fvValidarRUT( rut, dv )
{
	var suma=0;
	var mod=0;
	var S=0;
	var Rf=0;
	var digito_valido='';
	// REVISION DE PARAMETROS
	rut=parseInt(rut)+'';
	if(dv=="K"||dv=="k"){
		dv="K";
	}else{
		dv=parseInt(dv)+"";
	}
	// RUT PARA EXTRANJEROS
	if(parseInt(rut) > 99999 && parseInt(rut) < 1000000)
	{
		if(rut.substr(0,1) == dv)
			return "extranjero";
		else
			return false;
	}
	//CALCULO DE DIGITO VALIDO DE ACUERDO A rut
	for(i=rut.length;i>0;i--)
	{
		S+=parseInt(rut.substr(i-1,1))*(mod+2);
		mod=(mod+1)%6;
	}
	Rf=11-(S%11);
	if(Rf==11)
		digito_valido="0";
	if(Rf==10)
		digito_valido="K";
	if(Rf<10)
		digito_valido=Rf+"";

	if(dv==digito_valido)
	{
		if(parseInt(rut) < 50000000)
			return "natural"
		else
			return "juridica"
	}
	return false;
}
function fvValidarRUT2( rut, dv )
{
	if(rut < 100000)
		return false

	// RUT PARA EXTRANJEROS
	if(rut > 99999 && rut < 1000000)
	{
		if(rut.substr(0,1) == dv)
			return 'extranjero';
		else
			return false;
	}

	//RUT NORMAL
	largo = rut.length
	suma = 0
	cont = 2

	for(i=largo-1; i>=0; i--)
	{
		if(cont > 7)
			cont = 2

		suma += (rut.substr(i,1) * cont)
		cont++
	}

	digito = 11 - (suma%11)

	if(digito == 10)
		digito = 'K'
	if(digito == 11)
		digito = '0'

	if (dv == digito) {
		if(rut < 50000000)
			return "natural"
		else
			return "juridica"
	}
	
	return false
}

/* fvValidarCalendario:
 * Toma un objeto DOM input, idealmente que se utilize con el calendario js.
 * La función revisa si hay 8 y sólo 8 dígitos en el input, y procede a interpretarlos
 * con el formato DDMMYYYY.  Luego los reescribe de la forma DD-MM-YYYY, que es compatible
 * con el calendario js.
 * Retorna un objeto Date con la fecha escrita si no hay problemas, o false si sí los hay.
 */
function fvValidarCalendario( src ) {
	var str = src.value;

	fvSacarNoNumeros(src);
	if(src.value.length != 8) {
		src.value = str;
		return false;
	} else if(src.value == '00000000') {
		src.value = '';
		return false;
	}
	year = src.value.substr(4,4);
	month = src.value.substr(2,2);
	day = src.value.substr(0,2);
	month--;
	
	var date = new Date(year,month,day);
	
	day = '' + date.getDate();
	if(day.length == 1) day = '0' + day;
	
	month = '' + (date.getMonth()+1);
	if(month.length == 1) month = '0' + month;
	
	src.value = day + "-" + month + "-" + date.getFullYear();
	
	return date;
}

