var global_rutvalido = false;

function dv(T){var M=0,S=1;for(;T;T=Math.floor(T/10))
		S=(S+T%10*(9-M++%6))%11;return S?S-1:'k';}


function comprobarDV() {

var r = document.getElementById("rut");
var d = document.getElementById("dv");
var o = document.getElementById("oka");

	if((dv(r.value)==d.value) && r.value != "" && d.value != ""){
		o.src="img/ok_verde.png";
		global_rutvalido = true;
	}else{
		o.src="img/cancel_rojo.png";
		global_rutvalido = false;
	}
}


// OCULTO O MUESTRO SECCIONES
function mostrar(id_p){
var elementoamostrar = document.getElementById(id_p);
elementoamostrar.style.display = "block";
}
function ocultar(id_p){
var elementoamostrar = document.getElementById(id_p);
elementoamostrar.style.display = "none";
}


function muestra_otrocomo(x) {
	if(x == "Otro") {
		mostrar("otrocomolabel");
	} else {
		ocultar("otrocomolabel");
	}
}
function muestra_otrotipo(x) {
	if(x == "Otro") {
		mostrar("otrotipolabel");
	} else {
		ocultar("otrotipolabel");
	}	
}

// COMPRUEBO LOS DATOS DEL FORMULARIO
// SON TODOS OBLIGATORIOS
function comprobaciones() {
	
	var a1 = document.getElementById("usuario_si");
	if(a1.checked == true){//Si es usuario registrado
		//compruebo RUT
		var a2 = document.getElementById("rut");
		if(global_rutvalido == false){
		alert("Debe ingresar su rut");
		a2.focus();
		return false;
		}
	} 
	

	//Compruebo Nombre
	var a3 = document.getElementById("nombre");
	if(Empty(a3.value) || !isAlpha(a3.value)){
		alert("Debe ingresar su Nombre");
		a3.focus();
		return false;
	}

	//Compruebo Email
	var a4 = document.getElementById("email");
	if(Empty(a4.value) || !isMail(a4.value)){
		alert("Debe ingresar un email válido");
		a4.focus();
		return false;
	}

	//Compruebo Cómo nos conocio
	var a6 = document.getElementById("como");
	if(!isSelected(a6)){
		alert("Debe seleccionar la forma en que nos conoció");
		a6.focus();
		return false;		
	}
	var a7 = document.getElementById("otrocomo");
	if(a6.value == "Otro" && (Empty(a7.value) || !isAlphaNum2(a7.value))){
		alert("Debe indicar la forma en que nos conoció");
		a7.focus();
		return false;			
	}
	//Compruebo Motivo de consulta
	var a8 = document.getElementById("tipo");
	if(!isSelected(a8)){
		alert("Debe seleccionar Tipo de consulta");
		a8.focus();
		return false;		
	}
	var a9 = document.getElementById("otrotipo");
	if(a8.value == "Otro" && (Empty(a9.value) || !isAlphaNum2(a9.value))){
		alert("Debe indicar el tipo de consulta");
		a9.focus();
		return false;			
	}
	
	//Compruebo Consulta
	var a0 = document.getElementById("consulta");
	if(Empty(a0.value)){
		alert("Debe indicar su consulta");
		a0.focus();
		return false;
	}
	
// Si no da errores antes, entonces da true
return true;
}


// FUNCIONES ATÓMICAS QUE USO ARRIBA PARA COMPROBAR

// Empty: devuelve verdadero si value es vacio
function Empty(value) {
    var pattern=new RegExp("^[ ]*$");
    return value.match(pattern) || value.length==0;
}

// isInteger: devuelve verdero si value es un entero
function isInteger(value) {
    var pattern=new RegExp("^[0-9]+$");
    return value.match(pattern);
}

// isDouble: devuelve verdero si value es un double
function isDouble(value) {
    var pattern=new RegExp("^[0-9]+(\\.[0-9]+){0,1}$");
    return value.match(pattern);
}

// isMail: devuelve verdadero si value es una direccion de correo valida
function isMail(value) {
    var pattern=new RegExp("^([a-zA-Z0-9_.+\\-]+\\.{0,1})+@([a-zA-Z0-9_\\-]+\\.)+[a-zA-Z0-9_\\-]+$");
    return value.match(pattern);
}

// isAlpha: devuelve verdadero si la cadena contiene solo caracteres alfabeticos o espacios
function isAlpha(value) {
	var pattern=new RegExp("^[a-zA-Z\\sñÑáÁéÉíÍóÓúÚüÜ]+$");
    return value.match(pattern);
}

// isAlphaNum: devuelve verdadero si la cadena contiene solo caracteres alfabeticos, numeros y espacio
function isAlphaNum(value) {
	var pattern=new RegExp("^[a-zA-Z0-9\\sñÑáÁéÉíÍóÓúÚüÜ]+$");
    return value.match(pattern);
}

// isAlphaNum2: devuelve verdadero si la cadena contiene solo caracteres alfabeticos, numeros, punto, coma, guiones y espacio
function isAlphaNum2(value) {
	var pattern=new RegExp("^[a-zA-Z0-9\\s.,-ñÑáÁéÉíÍóÓúÚüÜ]+$");
    return value.match(pattern);
}

// LTrim: Quita espacios en blanco a la izquerda de una cadena
function LTrim(value) {
    var pattern=new RegExp("^\\s+", "g")
    return value.replace(pattern, "");
}

// RTrim: Quita espacios en blanco a la derecha de una cadena
function RTrim(value) {
    var pattern=new RegExp("\\s+$", "g")
    return value.replace(pattern, "");
}

// Trim: Quita espacios en blanco a la derecha y a la izquierda de una cadena
function Trim(value) {
    return RTrim(LTrim(value));
}

// isSelected: devuelve verdadero si un select tiene elementos seleccionados (multiselect) o 
// si es la opcion seleccionada no es la primera (select normal)
function isSelected (obj) {
	if (obj.multiple) {// Es un Select Mutliple
		for (i=0;i<obj.options.length;i++) {
			if (obj.options[i].selected) return true;
		}	
		return false;
	} else if (obj.multiple==false) {	
		// Es un Select Simple
		return obj.selectedIndex!=0;
	} else {
		// Es un Option
		if(obj=='[object]') {
			if(obj.checked==true) {
				return true;
			}
		}
		var i;

        for (i=0;i<obj.length;i++) {
            if (obj[i].checked) return true;
        }       
		return false;
	}
}	

