/*
document.observe('dom:loaded', function() {
    $('pd01').observe('click',function() {
        Modalbox.show($('contactform'), {title: 'Solicite um Orçamento'});
        return false;
    });
});

window.onresize = function() {
    // se quiser armazenar tamanho da tela, o evento é este
    return true;
};
*/

requestQuote = function(pId) {
    var frm=""
        + "<form id='contactform' method='post' onsubmit='submitQuoteRequest();'>"
        + "<input type='hidden' name='product_id' id='product_id' value='"+ pId +"'/>"
        + "<div class='forminstructions'>Entraremos em contato em até 24 horas úteis. Utilize o campo Informações Adicionais caso queira especificar melhor sua necessidade.<br /><small><div id='formalert'>Os campos marcados com (*) são obrigatórios.</div></small></div>"
        + "<table width='100%' cellpadding='0' cellspacing='0'>"
        + "<tr>"
        + "<td class='formlabel'><label for='name'>E-mail<sup>*</sup> : </label></td>"
        + "<td class='formfield'><input type='text' name='email' id='email' value='' size='35' /></td>"
        + "</tr>"
        + "<tr>"
        + "<td class='formlabel' width='20%'><label for='name'>Nome : </label></td>"
        + "<td class='formfield'><input type='text' name='name' id='name' value='' size='35' /></td>"
        + "</tr>"
        + "<tr>"
        + "<td class='formlabel'><label for='name'>Telefone : </label></td>"
        + "<td class='formfield'><input type='text' name='phone_ddd' id='phone_ddd' value='' maxlength='2' size='3' />&nbsp;&nbsp;<input type='text' name='phone' id='phone' value='' size='12' /></td>"
        + "</tr>"
        + "<tr>"
        + "<td class='formlabel_top' colspan='2'><label for='name'>Informações Adicionais : </label></td>"
        + "</tr>"
        + "<tr>"
        + "<td class='formfield_top' colspan='2'><textarea name='quote_details' id='quote_details' rows='20' cols='100' style='width: 98%; height: 140px;'></textarea></td>"
        + "</tr>"
        + "</table>"
        + "<div class='formtail'><input type='button' onclick='Modalbox.hide();' value='Cancelar' class='button' />&nbsp;<input type='button' onclick='submitQuoteRequest()' value='Enviar' class='button' /></div>"
        + "</form>";
    Modalbox.show(frm,{title: 'Solicitar Orçamento'});
};

submitQuoteRequest = function() {

    // Validate the only mandatory field. We are so lucky we have only one
    // mandatory field, so we can implement straightforward checks.... BT you
    // are a mother fucker!
    if($('email').getValue()) {
        var emailRE = /^(\w+)([-+.][\w]+)*@(\w[-\w]*\.){1,5}([A-Za-z]){2,4}$/;
        if(emailRE.test($('email').getValue())){
            new Ajax.Request(asb.api + '/contact.php',{
                method: 'post',
                parameters: $('contactform').serialize(),
                onCreate: function(transport) {
                    // it would be very nice if we had config options to block closing
                    Modalbox.show("<div class='forminstructions'>Por favor, aguarde.</div>",{title:'Enviando solicitação...',overlayClose: false});
                    return false; 
                },
                onSuccess: function(transport) {
                    Modalbox.show("<div class='forminstructions'>Em até 24 horas úteis a ATIT entrará em contato.</div>",{title:'Solicitação registrada!',afterHide: function(){timer.stop();}});
                    var timer = new PeriodicalExecuter(function(){
                        Modalbox.hide();
                        // this is amazing, but works.... does it? really? I don't believe!
                        timer.stop();
                        return false;
                    }, 7);
                },
                onFailure: function(transport) {
                    Modalbox.show("<div class='forminstructions'>Ocorreu um problema no envio de sua solicitação. Pedimos desculpas. Por favor entre em contato por telefone ou e-mail no endereço contato@atit.com.br</p>",{title:'Isto é constrangedor....'});
                    return false;
                } 
            });
        } else {
            $('formalert').setStyle({'color': 'red'});
            $('formalert').update('<b>'+$('email').getValue() + '</b> não parece ser um e-mail válido. Por favor preencha o campo e-mail novamente.');
            $('email').setStyle({'border':'1px solid red'});
            $('email').clear();
            $('email').focus();
        }
    } else {
        $('formalert').setStyle({'color': 'red'});
        $('formalert').update('Campos obrigatórios não preenchidos. Verifique os campos marcados em vermelho no formulário.');
        $('email').setStyle({'border':'1px solid red'});
        $('email').focus();
    }
    return false;
}

