$(function(){
    showBadge("as");
    $(document).asMask().enterToTab();
    // Element Clicks
    $(" .off").live('click',function(){
        $("#tabs > div").each(function(){
            $(this).attr("class","off");
        });
        $("#tabsContent > div").each(function(){
            $(this).hide();
        });
        $(this).attr("class","on");
        $("#content"+$(this).attr("id").substr(3)).show();
    });
    $('#step1').validate({
        remoteValidationEmail: 'ASMailChecker.php',
        waitingValidation: true,
        rules:{
            txNome:{
                required:true,
                nome_apelido:true
            },
            txEmail:{
                required:true,
                remote:true,
                email:true
            },
            txTelefone:{
                required:true,
                phone_PT:true
            },
            txLocalidade:{
                required:true
            }
        },
        messages:{
            txNome:{
                required:"O campo Nome Completo é obrigatório",
                nome_apelido:"Por favor, preencha o Nome Completo correctamente"
            },
            txEmail:{
                required:"O campo Email é obrigatório",
                remote:"Por favor, preencha um Email válido",
                email:"Por favor, preencha o Email correctamente"
            },
            txTelefone:{
                required:"O campo Telefone é obrigatório",
                phone_PT:"Por favor, preencha o Telefone correctamente"
            },
            txLocalidade:{
                required:"O campo Localidade é obrigatório"
            }
        },
        onWait:function(){
            $("#btnSubmit").attr("src","images/preloader.gif");
        },
        onValidationStop:function(){
            $("#btnSubmit").attr("src","images/btnSubmit.png");
        },
        invalidHandler: function(){
            $("#btnSubmit").attr("src","images/btnSubmit.png");
        }, 
        onfocusout:false,
        submitHandler: function(form) {
            $("#btnSubmit").attr("src","images/preloader.gif");
            checkForm(form);
        },
        showMessageOn: $('.errorLabel'),
        inputHighlight:true,
        afterValidate:function(){
            if($('.error').length > 0){
                if((parseInt($('.error').length)-parseInt($(".errorLabel .error").length))==0){
                    $(".errorLabel").show();
                }
                $('#formFields .error:input').each(function(){
                    $(this).parent().prev().attr("class","inputLeftError");
                    $(this).parent().attr("class","inputCenterError");
                    $(this).parent().next().attr("class","inputRightError");
                });
            }
            if($('.valid').length > 0){
                if((parseInt($('.error').length)-parseInt($(".errorLabel .error").length))==0){
                    $(".errorLabel").html('');
                }
                $('#formFields .valid:input').each(function(){
                    $(this).parent().prev().attr("class","inputLeftValid");
                    $(this).parent().attr("class","inputCenterValid");
                    $(this).parent().next().attr("class","inputRightValid");
                });
            }
        }
    });
});

function popitup(url) {
    newwindow=window.open(url,'','height=300,width=400,scrollbars=1');
    if (window.focus) {
        newwindow.focus()
    }
    return false;
}
        
function checkForm(form){
    var formID ="#"+form.id;
    var successForm = formID+"_success";
        
    var formURL = form.id+".php";
    var formData = $(formID).serialize();
        
    $.post(formURL, formData, function(data){
        if(data.message=="success"){
            $("#conv").html(data.conv);
            $(formID).hide();
            $(successForm).show();
        }else{
            $(formID).hide();
            $(successForm).html("Ocorreu um erro! Por favor tente mais tarde.").show();
        }
        return false;
    },"json");
}
