﻿﻿// Valida todos los campos del formulario
function validate_form() {
    if (validate_nombre()) {
        if (validate_movil()) {
            if (validate_email()) {
                if (validate_tipoVIA()) {
                    if (validate_via()) {
                        if (validate_numero()) {
                            if (validate_provincia()) {
                                if (validate_poblacion()) {
                                    if (validate_tlf()) {
                                        return true;
                                    }
                                    else {
                                        $("#tlf_cobertura").focus();
                                    }
                                }
                                else {
                                    $("#poblaciones").focus();
                                }
                            }
                            else {
                                $("#provincias").focus();
                            }
                        }
                        else {
                            $("#numero").focus();
                        }
                    }
                    else {
                        $("#via").focus();
                    }
                }
                else {
                    $("#tipovia").focus();
                }
            }
            else {
                $("#email").focus();
            }
        }
        else {
            $("#movil").focus();
        }
    }
    else {
        $("#nombre").focus();
    }
    $('html, body').animate({ scrollTop: 0 }, 'slow');
    return false;
}

function check_empty_form() {
    var input;
    
    input = $("#nombre").val();
    if (input.length != 0 || input != "") return false;
    input = $("#movil").val();
    if (input.length != 0 || input != "") return false;
    input = $("#email").val();
    if (input.length != 0 || input != "") return false;
    input = $("#tlf_cobertura").val();
    if (input.length != 0 || input != "") return false;
    input = $("#via").val();
    if (input.length != 0 || input != "") return false;
    input = $("#numero").val();
    if (input.length != 0 || input != "") return false;
    input = $("#provincias").val();
    if (input != -1) return false;
    input = $("#poblaciones").val();
    if (input != -1) return false;
    input = $("input[name='telefono']:checked").val();
    if (input != 0) return false;

    return true;
}

function validate_nombre() {
    var input = $("#nombre").val();
    $("#nombre").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
    if (input.length == 0 || input == "") {
        inlineMsg('nombre', _validation_required_text_nombre, true);
        $("#nombre").attr('isvalid', 'false');
        return false;
    } else {
        $("#nombre").attr('isvalid', 'true');
        return true;
    }
}

function validate_movil() {
    var movil = $("#movil").val();
    $("#movil").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
    if (movil.length == 0 || movil.length != 9 || movil == "") {
        inlineMsg('movil', _validation_required_text_movil, true);
        $("#movil").attr('isvalid', 'false');
        return false;
    } else {
		
        var num = movil.substring(0, 1);
		
		// Obtenemos los 3 primeros dígitos del número de móvil
		
		var numControl = movil.substring(0,3);
		
		//Añadimos validación por seguridad 24-10-2011.
		
        if (num == "6" || num == "7" || num == "9" || num == "8"){
			
			if(numControl != "803" && numControl != "806" && numControl != "807" &&
			numControl != "900" && numControl != "901" && numControl != "902" && numControl != "905"){
				
            $("#movil").attr('isvalid', 'true');
            return true;
			
			}else{
				
				inlineMsg('movil', _validation_badformat_text_movil, true);
            	$("#movil").attr('isvalid', 'false');
            	return false;
				
			}
			
        } else {
			
            inlineMsg('movil', _validation_badformat_text_movil, true);
            $("#movil").attr('isvalid', 'false');
            return false;
        }
    }
}

function validate_email() {
	
    var email = $("#email").val();
    $("#email").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
	
    if (email.length == 0 || email == "") {
		
        inlineMsg('email', _validation_required_text_email, true);
        $("#email").attr('isvalid', 'false');
        return false;
		
    } else {
		
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		
		//Validación para que el usuario no introduzca el email test-user@test-user.com 25-10-2011
		
		if ( emailPattern.test(email) && email != "test-user@test-user.com") {
			
            $("#email").attr('isvalid', 'true');
            return true;
			
        } else {
			
            inlineMsg('email', _validation_badformat_text_email, true);
            $("#email").attr('isvalid', 'false');
            return false;
			
        }
    }
}

function validate_tlf() {
    var tel = $("input[name='telefono']:checked").val();
    $("#tlf_cobertura").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
    if (tel == 1) {
        var telefono = $("#tlf_cobertura").val();
        if (telefono.length == 0 || telefono == "") {
            inlineMsg('tlf_cobertura', _validation_required_text_telefono, true);
            $("#tlf_cobertura").attr('isvalid', 'false');
            return false;
        } else {
        var num = telefono.substring(0, 1);
        	if (num == "6" || num == "7" || num == "9" && telefono.length == 9) {
        		$("#tlf_cobertura").attr('isvalid', 'true');
                return true;
            } else {
                inlineMsg('tlf_cobertura', _validation_badformat_text_telefono, true);
                $("#tlf_cobertura").attr('isvalid', 'false');
                return false;
            }
        }
    } else {
        return true;
    }
}

function validate_tipoVIA() {
    var tipoVIA = $("#tipovia").val();
    $("#tipovia").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
    if (tipoVIA == -1) {
        return false;
    } else {
        return true;
    }
}

function validate_via() {
    var via = $("#via").val();
    $("#via").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
    if (via.length == 0 || via == "") {
        inlineMsg('via', _validation_required_text_via, true);
        $("#via").attr('isvalid', 'false');
        return false;
    } else {
        $("#via").attr('isvalid', 'true');
        return true;
    }
}

function validate_numero() {
    var numero = $("#numero").val();
    var numPattern = /[0-9]/;
    $("#numero").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
    if (numPattern.test(numero)) {
        $("#numero").attr('isvalid', 'true');
        return true;
    } else {
        inlineMsg('numero', _validation_required_text_numero, true);
        $("#numero").attr('isvalid', 'false');
        return false;
    }
}

function validate_provincia() {
    var actProvincia = $("#provincias").val();
    $("#provincias").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
    if (actProvincia == -1) {
        inlineMsg('provincias', _validation_required_text_provincia, true);
        return false;
    } else {
        return true;
    }
}

function validate_poblacion() {
    var actPoblacion = $("#poblaciones").val();
    $("#poblaciones").css({
        "color": _validation_ok_color,
        "background-color": _validation_ok_background_color
    });
    if (actPoblacion == -1) {
        inlineMsg('poblaciones', _validation_required_text_poblacion, true);
        return false;
    } else {
        return true;
    }
}

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target, string, autohide) {
    var msg;
    var msgcontent;
    if (!document.getElementById('msg')) {
        msg = document.createElement('div');
        msg.id = 'msg';
        msgcontent = document.createElement('div');
        msgcontent.id = 'msgcontent';
        document.body.appendChild(msg);
        msg.appendChild(msgcontent);
        msg.style.filter = 'alpha(opacity=0)';
        msg.style.opacity = 0;
        msg.alpha = 0;
    } else {
        msg = document.getElementById('msg');
        msgcontent = document.getElementById('msgcontent');
    }
    msgcontent.innerHTML = string;
    msg.style.display = 'block';
    var msgheight = msg.offsetHeight;
    var targetdiv = document.getElementById(target);
    //targetdiv.focus();
    var targetheight = targetdiv.offsetHeight;
    var targetwidth = targetdiv.offsetWidth;
    var topposition = topPosition(targetdiv) - targetheight - (msgheight / 2) - MSGOFFSET;  //- ((msgheight - targetheight) / 2);
    var leftposition = leftPosition(targetdiv);  //+ targetwidth + MSGOFFSET;
    msg.style.top = topposition + 'px';
    msg.style.left = leftposition + 'px';
    clearInterval(msg.timer);
    msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
    if (!autohide) {
        autohide = MSGHIDE;
    }
    window.setTimeout("hideMsg()", (autohide * 3000));
}

// hide the form alert //
function hideMsg(msg) {
    var msg = document.getElementById('msg');
    if (null != msg) {
        if (!msg.timer) {
            msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
        }
    }
}

// face the message box //
function fadeMsg(flag) {
    if (flag == null) {
        flag = 1;
    }
    var msg = document.getElementById('msg');
    var value;
    if (flag == 1) {
        value = msg.alpha + MSGSPEED;
    } else {
        value = msg.alpha - MSGSPEED;
    }
    msg.alpha = value;
    msg.style.opacity = (value / 100);
    msg.style.filter = 'alpha(opacity=' + value + ')';
    if (value >= 99) {
        clearInterval(msg.timer);
        msg.timer = null;
    } else if (value <= 1) {
        msg.style.display = "none";
        clearInterval(msg.timer);
    }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
    var left = 0;
    if (target.offsetParent) {
        while (1) {
            left += target.offsetLeft;
            if (!target.offsetParent) {
                break;
            }
            target = target.offsetParent;
        }
    } else if (target.x) {
        left += target.x;
    }
    return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
    var top = 0;
    if (target.offsetParent) {
        while (1) {
            top += target.offsetTop;
            if (!target.offsetParent) {
                break;
            }
            target = target.offsetParent;
        }
    } else if (target.y) {
        top += target.y;
    }
    return top;
}

// preload the arrow //
if (document.images) {
    arrow = new Image(7, 80);
    arrow.src = "/landing/img/msg_arrow.gif";
}

