$(function() {
    setupTips();
    setupErrors();
    blockButton();
});

function setupTips()
{

    // set up tip for each text input box so that when you click into it, the tip displays
    $("input[type=text],[type=password],[type=file],textarea").focus(function() {
        $(this).siblings(".tips").show();
        $(this).siblings(".error").html("").hide();
    });

    // when you click out of it, the tip disappears
    $("input[type=text],[type=password],[type=file],textarea").blur(function() {
        $(this).siblings(".tips").hide();
    });

}

function setupErrors()
{

    $("#ContactoNombre").blur(function() {
        if (jQuery.trim(this.value) == "")
            notOkay(this, "Por favor ingrese su nombre como respaldo para su aviso.");
        else
            itsOkay(this);
    });

    $("#ContactoRut").blur(function() {
        if (jQuery.trim(this.value) == "")
            notOkay(this, "Por favor ingrese su R.U.T.");
        else
            itsOkay(this);
    });

    $("#ContactoCorreo").blur(function() {
        if (! /^[a-zA-Z0-9_\~\-\.]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+){1,6}$/.test(this.value))
            notOkay(this, "Por favor asegúrese que esta sea una dirección de correo electrónico existente.");
        else
            itsOkay(this);
    });

    $("#ContactoTelefono").blur(function() {
        if (jQuery.trim(this.value) == "") {
            notOkay(this, "Por favor ingrese un numero telefónico de contácto.");
        } else if (! /^[()\d\s]+$/.test(this.value)) {
            notOkay(this, "Solo puedes usar numeros y espacios.");
        } else
            itsOkay(this);
    });

    $("#Titulo").blur(function() {
        if (jQuery.trim(this.value) == "") {
            notOkay(this, "Por favor indiquenos el titulo para el aviso.");
        } else if (! /^[ñ\a-zA-Z0-9\s]+$/.test(this.value)) {
            notOkay(this, "Solo puedes usar letras, numeros y espacios.");
        } else
            itsOkay(this);
    });

    $("#Contenido").blur(function() {
        if (jQuery.trim(this.value) == "")
            notOkay(this, "Por favor escriba un contenido amplio para su aviso.");
        else
            itsOkay(this);
    });

    $("#Precio").blur(function() {
        if (jQuery.trim(this.value)) {
            if (! /^[\d]+$/.test(this.value)) {
                notOkay(this, "Por favor, solo numeros, sin punto ni guión.");
             }
        }
    });

    $("#Clave").blur(function() {
        errorMsg = "";

        if (this.value.length < 6) {
            errorMsg = "Su clave debe ser de al menos 6 caracteres."
        } else if  (! /\d/.test(this.value)) {
            errorMsg = "Su clave debe contener al menos 1 número."
        } else if (/^\d+$/.test(this.value)) {
            errorMsg = "Su clave debe contener al menos 1 letra."
        }

        if (errorMsg) {
            notOkay(this, errorMsg);
        } else
            itsOkay(this);
    });

    $("#ClaveConf").blur(function() {
        if ($("#Clave").val() == "" || $("#Clave").val() != this.value)
            notOkay(this, "Su clave y la confirmación de la misma no coinciden. Por favor escríbalas de nuevo.");
        else
            itsOkay(this);
    });
}

function itsOkay(which)
{
    $(which).siblings(".ok").css("display","inline");
}

function notOkay(which, errorMsg)
{
    $(which).siblings(".ok").hide();
    $(which).siblings(".error").html(errorMsg).show();
}