function validarRut(rut){

var count=0;
var count2=0;
var factor=2;
var suma=0;
var sum=0;
var digito=0;
count2=rut.length - 1;

while(count < rut.length){
	sum = factor * (parseInt(rut.substr(count2,1)));
	suma = suma + sum;
	sum=0;
	count = count + 1;
	count2 = count2 - 1;
	factor = factor + 1;
	if(factor > 7){
		factor=2;
	}
}

digito= 11 - (suma % 11)

if(digito==11){
	digito=0;
}

if(digito==10){
	digito="k";
}
return digito;
}

function validarEmails(whatYouTyped){
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}

function validarRut2(capa1,capa2){
	if(document.getElementById(capa1).value != "" && document.getElementById(capa2).value != ""){
        if(document.getElementById(capa2).value == "K" || document.getElementById(capa2).value == "k"){
            var dv1 = "k";
        }else{
            var dv1 = document.getElementById(capa2).value;
        }

        var dv2 = validarRut(document.getElementById(capa1).value);
		if(dv1 != dv2){
			return false;
		}else{
        	return true;
	    }
     }
}

function validarEmails2(whatYouTyped){
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		if(txt==document.getElementById("mail").value){
			fieldset.className = "welldone";
		}else{
			fieldset.className = "kindagood";
		}
	} else {
		fieldset.className = "";
	}
}

function validar(e) {
	tecla = (document.all) ? e.keyCode : e.which;
	if (tecla==8 || tecla == 0) return true; //Tecla de retroceso (para poder borrar)
	patron = /\d/; //ver nota
	te = String.fromCharCode(tecla);
	return patron.test(te);
}

function validarDV(e) {
	tecla = (document.all) ? e.keyCode : e.which;
	if (tecla==8 || tecla == 0 || tecla==107 || tecla==75) return true; //Tecla de retroceso (para poder borrar)
	patron = /\d/; //ver nota
	te = String.fromCharCode(tecla);
	return patron.test(te);
}

function validarNumero(whatYouTyped,largo,campo){
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if(txt.charAt(0)==0){
		campo.value="";
	}
	if(txt.length>largo){
		fieldset.className = "welldone";
	} else {
		fieldset.className = "kindagood";
	}
}

function habilitaDivs(bloque){

	if(document.getElementById(bloque).style.display == 'none'){
		document.getElementById(bloque).style.display = '';
	}else{
		document.getElementById(bloque).style.display = 'none';
	}
}

function agregaIdiomas(bloque){

	if(document.getElementById(bloque).style.display == 'none'){
		document.getElementById(bloque).style.display = '';
	}else{
		document.getElementById(bloque).style.display = 'none';
	}
}

function quitarIdiomas(bloque,select1,select2){

	if(bloque != 'idioma2'){
		document.getElementById(bloque).style.display = 'none';
	}
	document.getElementById(select1).value= '';
	document.getElementById(select2).value= '';
}

function limitaTexto(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function checkear_extension(fichero)	{
	// definimos antes los métodos prever() y limpiar()
	(/.(gif|jpg|png)$/i.test(fichero.value)) ? prever() : limpiar();
}

function limpiar()	{
	f = document.getElementById("archivo");
	nuevoFile = document.createElement("input");
	nuevoFile.id = f.id;
	nuevoFile.type = "file";
	nuevoFile.name = "archivo";
	nuevoFile.value = "";
	nuevoFile.onchange = f.onchange;
	nodoPadre = f.parentNode;
	nodoSiguiente = f.nextSibling;
	nodoPadre.removeChild(f);
	(nodoSiguiente == null) ? nodoPadre.appendChild(nuevoFile):
		nodoPadre.insertBefore(nuevoFile, nodoSiguiente);
}

function checkear_extension(fichero)	{

	function prever() {
		var campos = new Array("maxpeso", "maxalto", "maxancho");
		for (i = 0, total = campos.length; i < total; i ++)
			fichero.form[campos[i]].disabled = false;
		actionActual = fichero.form.action;
		targetActual = fichero.form.target;
		fichero.form.action = "previsor.php";
		fichero.form.target = "ver";
		fichero.form.submit();
		for (i = 0, total = campos.length; i < total; i ++)
			fichero.form[campos[i]].disabled = true;
		fichero.form.action = actionActual;
		fichero.form.target = targetActual;
	}

	function no_prever() {
		alert("El fichero seleccionado no es válido...");
		limpiar();
	}

	(/.(gif|jpg|png)$/i.test(fichero.value)) ? prever() : no_prever();
}

function agregaFotos(bloque){

	if(document.getElementById(bloque).style.display == 'none'){
		document.getElementById(bloque).style.display = '';
	}else{
		document.getElementById(bloque).style.display = 'none';
	}
}

function quitarFotos(bloque,select1,select2,select3){

	document.getElementById(bloque).style.display = 'none';
	document.getElementById(select1).value= '';
	document.getElementById(select2).checked = false;
	document.getElementById(select3).value= '';
}
function quitaFoto(select1,select2,select3){

	document.getElementById(select1).value= '';
	document.getElementById(select2).checked = false;
	document.getElementById(select3).value= '';
}

//*****************************************************************************************
// esBisiesto(anio)
//
// Determina si el año pasado com parámetro es o no bisiesto
//*****************************************************************************************
function esBisiesto(anio){
var BISIESTO;
if(parseInt(anio)%4==0){
	if(parseInt(anio)%100==0){
		if(parseInt(anio)%400==0){
			BISIESTO=true;
		}else{
			BISIESTO=false;
		}
	}else{
		BISIESTO=true;
	}
}else
	BISIESTO=false;

return BISIESTO;
}

//***********************************************************************************************
// validarFecha(dia,mes, año)
//
// Valida que el día y el mes introducidos sean correctos. Además valida que el año introducido
// sea o no bisiesto
//
//***********************************************************************************************
function validarFecha(dia,mes,anio){

var elMes = parseInt(mes);

if(elMes>12)
return false;
// MES FEBRERO
if(elMes == 2){
	if(esBisiesto(anio)){
		if(parseInt(dia) > 29){
			return false;
		}else
			return true;
	}else{
		if(parseInt(dia) > 28){
			return false;
		}else
			return true;
	}
}
//RESTO DE MESES

if(elMes== 4 || elMes==6 || elMes==9 || elMes==11){
	if(parseInt(dia) > 30){
		return false;
	}
}
return true;

}

function seleccionaTodosCheckD(check1,check2,check3,check4){
	document.getElementById(check1).checked = true;
	document.getElementById(check2).checked = true;
	document.getElementById(check3).checked = true;
	document.getElementById(check4).checked = true;
}

function seleccionaContextura(capa,valor){
	alert("seleccionado");
	document.getElementById(capa).value = valor;
}

function agregaIdiomaOtro(capa){
	if(document.getElementById(capa).value == 'X'){
		document.getElementById("otro_idioma_d").style.display = '';
		document.getElementById(capa).value = '';
	}
}


