var AJAX_SUCCESS = 1;
function isValidEMail(email) {
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if(!emailReg.test(email)) {
        return false;
    }
    return true;
}

function highlightInput(selector) {
    $(selector).focus();
    $(selector).select();
}

function isEmpty(inputSelector) {
    var value = $(inputSelector).val();
    if(jQuery.trim(value) == '') {
        highlightInput(inputSelector);
        return true;
    }
    return false;
}

function contact() {
    $(".tab2 p.msg").hide();
    var email = $("#email").val();
    var name = $("#name").val();
    var comment = $("#comment").val();
    if(isEmpty("#name") || isEmpty("#email") || isEmpty("#comment")) {
        $(".tab2 .error").show();
        return;
    }
    if(!isValidEMail(email)) {
        highlightInput("#email");
        $(".tab2 .error").show();
        return;
    }

    $.post("/private/contact.php",
        { email: email, name: name, comment: comment},
        function(data) {
            if(data == AJAX_SUCCESS) $(".tab2 .success").show();
            else                     $(".tab2 .error").show();
        });
}

function subscribe() {
    $(".tab1 p.msg").hide();
    var email = $("#subscriber").val();
    if(isEmpty("#subscriber") || !isValidEMail(email)) {
        highlightInput("#subscriber");
        $(".tab1 .error").show();
        return;
    }
    $.post("/private/subscribe.php", { email: email }, function(data) {
        if(data == AJAX_SUCCESS) {
            $(".tab1 .success").show();
        }
        else {
            $(".tab1 .error").show();
        }
    });
}

