// JavaScript Document
$(document).ready(function() {
    anchor.init();
		
});
anchor = {
    init : function() {
        $("a.anchorLink").click(function () {
            elementClick = $(this).attr("href")
            destination = $(elementClick).offset().top;
            $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: destination
            }, 1100, function() {
                $("div#informate").slideDown("slow");
            });
            return false;            
        });
    }
}
function enviar(){
    var errores="";
    if(document.getElementById("nombre")!=null&&document.getElementById("nombre").value==""){
        errores="- No has introducido tu nombre.\n"
    }
    if(document.getElementById("domicilio")!=null&&document.getElementById("domicilio").value==""){
        errores+="- No has introducido el domicilio. \n"
    }
    var telefono=document.getElementById("telefono").value;
    if(document.getElementById("telefono")!=null&&document.getElementById("telefono").value==""){
        errores+="- No has introducido el telefono o no es válido.\n"
    }else if(!parseFloat(document.getElementById("telefono").value)||document.getElementById("telefono").value.length!=9){
        errores+="- El teléfono debe estar formado por 9 números sin separaciones. \n"
    }else if(telefono.charAt(0)!="6"&&telefono.charAt(0)!="8"&&telefono.charAt(0)!="9"){
        errores+="- El número de telefono no es válido. \n"
    }
    if(document.getElementById("provinciaid")!=null&&document.getElementById("provinciaid").value=="-1"){
        errores+="- No has seleccionado tu provincia. \n"
    }
    if(document.getElementById("poblacionid")!=null&&document.getElementById("poblacionid").value=="-1"){
        errores+="- No has seleccionado tu población. \n"
    }
    if( document.getElementById("codigopostal")!=null&& ( document.getElementById("codigopostal").value=="-1" || document.getElementById("codigopostal").value == "" ) ){
        errores+="- No has seleccionado tu código postal. \n"
    }
    if(document.getElementById("estudiosid")!=null&&document.getElementById("estudiosid").value=="-1"){
        errores+="- No has seleccionado los estudios. \n"
    }
    if(document.getElementById("nacionalidadid")!=null&&document.getElementById("nacionalidadid").value=="-1"){
        errores+="- No has seleccionado la nacionalidad. \n"
    }/*else{
        if(document.getElementById("nacionalidadid").value != "90"){
            if(document.getElementById("dni") != null && document.getElementById("dni").value == ""){
                errores+="- No has introducido el DNI/NIE. \n"
            }else{
                var dni = document.getElementById("dni").value;
                if(!checkNIF(dni)){
                    errores+="- DNI/NIF incorrectos. \n"
                }
            }
        }
    }*/
    if(document.getElementById("frmCupon_posibleAutoreferidoS")!=null&&document.getElementById("frmCupon_posibleAutoreferidoS").checked){
       
            if(document.getElementById("autoreferidoNif") != null && document.getElementById("autoreferidoNif").value == ""){
                errores+="- No has introducido el DNI/NIE autorreferido. \n"
            }else{
                var dni = document.getElementById("autoreferidoNif").value;
                if(!checkNIF(dni)){
                    errores+="- DNI/NIF autorreferido incorrectos. \n"
                }
            }
    }
    if(document.getElementById("edad")!=null&&document.getElementById("edad").value=="-1"){
        errores+="- No has seleccionado la edad. \n"
    }
    if(document.getElementById("aceptoCondiciones")!=null&&document.getElementById("aceptoCondiciones").checked==false){
        errores+="- Debes aceptar las condiciones de la politica de privacidad y aviso legal del servicio. \n"
    }
    if(errores.length>0){
        alert("Errores: \n\n"+errores);
        return
    }
    document.getElementById("frmCupon").submit()
}
function listarPoblaciones(action, provinciaId) {
    $('#comboPoblacionesDiv').html('<select class="campo_fijo_cupon"><option value="-1">Cargando ...</option></select>');
    $('#comboCPDiv').html('<select id="codigopostal" class="campo_fijo_cupon" disabled="disabled"><option value="-1"></option></select>');
    $("div#poblacionesDiv").slideDown("slow");
    $.ajax({
        url: action + '?selectedProvincia='+provinciaId,
        cache: true,
        success: function(html){
            $('#comboPoblacionesDiv').html(html);
        }
    });
}

function listarCP(action, poblacionId) {    
    $('#comboCPDiv').html("<select id='codigopostal' class='campo_fijo_cupon' disabled='disabled'><option>Cargando CP...</option></select>");
    $("div#cpDiv").slideDown("slow");
    var provincia = document.getElementById('provinciaid').value;
    $.ajax({
        url: action + '?selectedProvincia='+provincia+'&selectedPoblacion='+poblacionId,
        cache: true,
        success: function(html){
            $('#comboCPDiv').html(html);
        }
    });
    
}    

function validarNacionalidad(){
    var nacionalidadId = document.getElementById("nacionalidadid").value;
    if(nacionalidadId != 90){
        $('#divDni').slideDown('slow');
    }else{
        $('#divDni').slideUp('slow');
    }
}
function checkNIF(nif) {
    if (!nif) return false;
    var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
    if (nif.length != 9) return false;
    else {
        letra = nif.substr(8, 1);
        letra = letra.toUpperCase();
        dni = nif.substr(0, 8);
        dni = dni.toUpperCase();
        dni = dni.replace('X','0');
        dni = dni.replace('Y','1');
        dni = dni.replace('Z','2');
        dni -= parseInt(dni/23) * 23;
        if (letras.charAt(dni) != letra) return false;
        else return true;
    }
}

