function aleatorio(inferior,superior){
	numPosibilidades = superior - inferior
	aleat = Math.random() * numPosibilidades
	aleat = Math.round(aleat)
	return parseInt(inferior) + aleat
}

function validar_codigo_captcha_crear_floquo(){
	var xobj = new LoadVars();
	var vars = new LoadVars();

	vars.clave_captcha_dos = document.getElementById('codigo_captcha_crear_floq').value;
	vars.id_captcha_dos = document.getElementById('valor_id_captcha_crear_floq').value;

	vars.onData=function(response){
		if(response == ""){
			document.getElementById('valor_id_captcha_crear_floq').value = aleatorio(1,1000000);
			document.getElementById('imagen_captcha_crear_floq').src = "/files/comentarios/captcha_src.php?id=" + document.getElementById('valor_id_captcha_crear_floq').value;
			document.getElementById('codigo_captcha_crear_floq').value = "";

			document.getElementById('div_codigo_captcha_crear_error').style.display = "block";
		}
		else{
			document.getElementById('div_boton_agregar_floquo').innerHTML = response;
			//document.getElementById('div_captcha_validar').style.display = "none";
			$("#div_codigo_captcha_crear_error").slideUp( $("#div_codigo_correcto").slideDown("slow") , "slow");			
			document.getElementById('boton_validar_codigo').disabled = "true";
			comprobar_existencia_user();
		}
	}

	xobj.sendAndLoad("/files/formulario/validarcaptcha.php",vars,"POST");
}

function cargarProvincias(){

	var xobj = new LoadVars();
	var vars = new LoadVars();

	vars.id_pais = document.getElementById('InPais').value;
	vars.onData=function(response){
		document.getElementById('div_provincia').innerHTML = response;
		customize_form_elements2('#div_provincia .estile');
	}

	xobj.sendAndLoad("/files/regiones/buscar_provincias_crear_floq.php",vars,"POST");
}

function comprobar_existencia_user(){
	var xobj = new LoadVars();
	var vars = new LoadVars();

	vars.nombre_usuario = document.getElementById('InNombreUsuario').value;
	vars.onData=function(response){
		document.getElementById('div_error_existencia').innerHTML = response;
	}
	xobj.sendAndLoad("/files/formulario/comprobar_existencia.php",vars,"POST");
}

function validar_nombre_usuario(){
	comprobar_existencia_user();
	if(parseInt(document.getElementById('todo_ok').value) == 1){
		return true;
	}
	else{
		return false;
	}
}

function seguridad_password(e){
	var password = document.getElementById('InPassword').value;
	kc=e.keyCode?e.keyCode:e.which;	
	letra = String.fromCharCode(kc);
	password = password + letra;
	if (password.match(/[a-z]{1,100}/) && password.match(/[A-Z]{1,100}/) && password.match(/[0-9]{1,100}/)) {
		document.getElementById("div_seguridad_password").innerHTML = "<div style='float:left'>Seguridad Alta</div><div style='float:right; margin-top: 3.5px;'><img src='/img/seguridad-alta.jpg'></div>";
	} else if((password.match(/[a-z]{1,100}/) && password.match(/[A-Z]{1,100}/)) || (password.match(/[a-z]{1,100}/) && password.match(/[0-9]{1,100}/)) || (password.match(/[A-Z]{1,100}/) && password.match(/[0-9]{1,100}/))){
		document.getElementById("div_seguridad_password").innerHTML = "<div style='float:left'>Seguridad Media</div><div style='float:right; margin-top: 3.5px;'><img src='/img/seguridad-media.jpg'></div>";
	}
	else if (password.match(/[a-z]{1,100}/) || password.match(/[A-Z]{1,100}/) || password.match(/[0-9]{1,100}/)){
		document.getElementById("div_seguridad_password").innerHTML = "<div style='float:left'>Seguridad Baja</div><div style='float:right; margin-top: 3.5px;'><img src='/img/seguridad-baja.jpg'></div>";
	}
	else if(password == ""){
		document.getElementById("div_seguridad_password").innerHTML = "";
	}
}

function mostrar_mensaje_validar(){
	$("#mensaje_validar").fadeIn("slow");
	setTimeout('$("#mensaje_validar").fadeOut("slow");', 10000);
}

/*
 * Verifica en tiempo real las opciones que deberian permitir y mostrar los select para ingresar una fecha valida
 */
function chequear_fecha(){

    //obtengo todos los dias
    var dias = document.getElementById("InDiaNac");

    //el dia seleccionado
    var dia = document.getElementById("InDiaNac").value;

    //obtengo todos los meses
    var meses = document.getElementById("InMesNac");

    //el mes seleccionado
    var mes = document.getElementById("InMesNac").value;

    //obtengo todos los anios
    var anios = document.getElementById("InAnioNac");

    //el anio seleccionado
    var anio = document.getElementById("InAnioNac").value;


        //si es un mes con 31 dias
        if(mes == '1' || mes == '3' || mes == '5' || mes == '7' || mes == '8' || mes == '10' || mes == '12'){

            var sel = dias.selectedIndex; //guardo el indice del dia que estaba seleccionado
            borrar_dias(dias);
            cargar_dias_hasta(dias, 31);

            dias.options[sel].selected = true;  // seteo el dia que estaba seleccionado

        }
        else {//es un mes con 30 dias o menos
            if(mes == '2'){  // es el mes de Febrero
                if(bisiesto(anio)){
                    var sel = dias.selectedIndex;
                    borrar_dias(dias);
                    cargar_dias_hasta(dias, 29);
                    if(sel > 28){
                        dias.options[28].selected=true;
                    }
                    else{
                        dias.options[sel].selected = true;
                    }
                }
                else{
                    var sel = dias.selectedIndex;
                    borrar_dias(dias);
                    cargar_dias_hasta(dias, 28);
                    if(sel > 27){
                        dias.options[27].selected = true;
                    }
                    else{
                        dias.options[sel].selected = true;
                    }
                }
                
            }
            else{ // es un mes de 30 dias
                var sel = dias.selectedIndex;
                borrar_dias(dias);
                cargar_dias_hasta(dias, 30);
                if(sel > 29){
                    dias.options[29].selected = true;
                }
                else{
                    dias.options[sel].selected = true;
                }
            }
        }

        //customize_form_elements2("#div_fecha .estile.select_dia");
        $("#div_fecha .estile.select_dia").siblings('span','div').text($("option:selected","#div_fecha .estile.select_dia").text());
}

//Retorna true si el anno es bisiesto
function bisiesto(anno){
    return ((anno % 4 == 0 && anno % 100 != 0) || anno % 400 == 0);
}

//Borro todos los dias dentro del select
function borrar_dias(dias){
     dias.options.length = 0;
}

//Cargo el select con la cantidad de dias que recibo como parametro
function cargar_dias_hasta(dias,cant){
    for(var i=1;i<=cant; i++){
            var op = new Option(i,i);
            dias.options[(i - 1)] = op;
    }

}
