$(document).ready(function(){    
	
    $("#dataCotacao").mask("99/99/9999");
    $("#newsletter #telefone, #contato #telefone, #cotacoes #telefone").mask("(99) 9999-9999");
    
    //Formatando como moeda
	$('#valorConverter, #valorcarga').priceFormat({
	    prefix: '',
	    centsSeparator: ',',
	    thousandsSeparator: '.'
	});
    $('#pesobruto').priceFormat({
	    prefix: '',
	    centsSeparator: ',',
	    thousandsSeparator: '',
        centsLimit: 1
	});
});


$(document).ready(function() {
    //CADASTRO NEWSLETTER
    $("#newsletter").submit( function() {
    
         if ($("#newsletter #nome").val() == '' || $("#nome").val() == 'Seu Nome') {
            alert("Por favor, informe seu Nome");
            $("#newsletter #nome").val('');
            $("#newsletter #nome").focus();
            return false;
         }
         
		 if ($("#newsletter #email").val() == '' || $("#email").val() == 'Seu E-mail'){
            alert("Por favor, informe o E-mail");
            $("#newsletter #email").val('');
            $("#newsletter #email").focus();
            return false;
         }
         else{
			  var regmail = /^[\w!#$%&amp;'*+\/=?^`{|}~-]+(\.[\w!#$%&amp;'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
			  if(!regmail.test($("#newsletter #email").val())){
				   alert("E-mail inválido");
                   $("#newsletter #email").val('');
                   $("#newsletter #email").focus();
				   return false;
			  }
		 }
         
         $("#newsletter #submit").val('ENVIANDO!...');
         $('#newsletter #nome, #newsletter #email, #newsletter #submit').attr("disabled", true);

        $.post($(this).attr('action'), { nome:  "" , 
                                         email:  encodeURI( $('#newsletter #email').val() ) },
        function(data) {
            if(data == '1'){
                $("#newsletter").hide();
                $("#newsletter_obrigado").fadeIn('slow');
            }
            else{
                $("#newsletter #submit").val('CADASTRAR');
                alert(data);
            }
        });
        $('#newsletter #nome, #newsletter #email, #newsletter #submit').removeAttr("disabled");
        return false;
	});
    
    
    
    //CONTATO
    $("#contato").submit( function() {
        
         if ($("#contato #nome").val() == '') {
            alert("Por favor, informe o Seu Nome");
            $("#contato #nome").focus();
            return false;
         }
         
         if ($("#contato #telefone").val() == '') {
            alert("Por favor, informe o Telefone");
            $("#contato #telefone").focus();
            return false;
         }
         
         if ($("#contato #email").val() == ''){
            alert("Por favor, informe o E-mail");
            $("#contato #email").focus();
            return false;
         }
         else{
              var regmail = /^[\w!#$%&amp;'*+\/=?^`{|}~-]+(\.[\w!#$%&amp;'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
              if (!regmail.test($("#contato #email").val())) {
                   alert("E-mail inválido");
                   $("#contato #email").focus();
                   return false;
              }
         }
         
         if ($("#contato #mensagem").val() == '') {
            alert("Por favor, informe a Mensagem");
            $("#contato #mensagem").focus();
            return false;
         }
         
         $("#contato #submit").val('ENVIANDO!...');
         $('#contato input, #contato textarea').attr("disabled", true);
         
        $.post($(this).attr('action'), {nome: encodeURI(  $('#contato #nome').val()  ), 
                                                    telefone: encodeURI(  $('#contato #telefone').val() ),
                                                    ramal: encodeURI(  $('#contato #ramal').val() ),
                                                    email: encodeURI(  $('#contato #email').val() ),
                                                    mensagem: encodeURI(  $('#contato #mensagem').val() ) },
        function(data) {
            if(data == "1"){
                $('#contato').hide();
                $('#obrigado_contato').fadeIn("slow");
            }
            else
                alert(data);
            
            $("#contato #submit").val('ENVIAR');
            $('#contato input, #contato textarea').removeAttr("disabled");
        });
        
        return false;

    });
    
    
    
    //COTACAOES
    $("#cotacoes").submit( function() {
         idioma = $("#idioma").val();
         if ($("#cotacoes #cliente").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Cliente" : "Please, fill the field Costumer" ));
            $("#cotacoes #cliente").focus();
            return false;
         }
         
         if ($("#cotacoes #modal").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Modal" : "Please, fill the field Shipment(Sea or Air)" ));
            $("#cotacoes #modal").focus();
            return false;
         }
         
         if ($("#cotacoes #valorcarga").val() == '' || $("#cotacoes #valorcarga").val() == '0,00') {
            alert(((idioma!="en") ? "Por favor, informe Valor da Carga" : "Please, fill the field Value of the cargo" ));
            $("#cotacoes #valorcarga").focus();
            return false;
         }
         
         if ($("#cotacoes #incoterm").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe INCOTERM" : "Please, fill the field INCOTERM" ));
            $("#cotacoes #incoterm").focus();
            return false;
         }
         
         if ($("#cotacoes #pesobruto").val() == '' || $("#cotacoes #pesobruto").val() == '0,0') {
            alert(((idioma!="en") ? "Por favor, informe Peso Bruto" : "Please, fill the field Gross Weight" ));
            $("#cotacoes #pesobruto").focus();
            return false;
         }
         
         if ($("#cotacoes #dimensoes").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Dimensão do Volume" : "Please, fill the field Dimensions" ));
            $("#cotacoes #dimensoes").focus();
            return false;
         }
         
         if ($("#cotacoes #quantidade").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Quanidade Volume" : "Please, fill the field Quantities" ));
            $("#cotacoes #quantidade").focus();
            return false;
         }
         
         if ($("#cotacoes #origem").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Porto/Aeroporto de Origem" : "Please, fill the field Port/Airport (Origin) POL" ));
            $("#cotacoes #origem").focus();
            return false;
         }
         
         if ($("#cotacoes #destino").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Porto/Aeroporto de Destino" : "Please, fill the field Port/Airport (Destination) POD" ));
            $("#cotacoes #destino").focus();
            return false;
         }
         
         if ($("#cotacoes #zipcode").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Endereço Completo c/ ZIP CODE" : "Please, fill the field Complete Address with ZIP CODE" ));
            $("#cotacoes #zipcode").focus();
            return false;
         }
         
         if ($("#cotacoes #produtodescricao").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Produto (Descrição)" : "Please, fill the field Product (Cargo Description)" ));
            $("#cotacoes #produtodescricao").focus();
            return false;
         }
         
         if($("input[name=20dry]:checked").length == 0){
            alert(((idioma!="en") ? "Por favor, informe 20 Dry" : "Please, fill the field 20 Dry" ));
            $("#cotacoes #20dry_sim").focus();
            return false;
         }
         
         if($("input[name=40dry]:checked").length == 0){
            alert(((idioma!="en") ? "Por favor, informe 40 Dry" : "Please, fill the field 40 Dry" ));
            $("#cotacoes #40dry_sim").focus();
            return false;
         }
         
         if($("input[name=40hc]:checked").length == 0){
            alert(((idioma!="en") ? "Por favor, informe 40 HC" : "Please, fill the field 40 HC" ));
            $("#cotacoes #40hc_sim").focus();
            return false;
         }
         
         if($("input[name=40Reffer]:checked").length == 0){
            alert(((idioma!="en") ? "Por favor, informe 40 Reffer" : "Please, fill the field 40 Reffer" ));
            $("#cotacoes #40Reffer_sim").focus();
            return false;
         }
         
         if($("input[name=40open]:checked").length == 0){
            alert(((idioma!="en") ? "Por favor, informe 40 open Top" : "Please, fill the field 40 open Top" ));
            $("#cotacoes #40open_sim").focus();
            return false;
         }else{
            if($("#40open_sim").is(":checked") && $('#altura40open').val() == ""){
                alert(((idioma!="en") ? "Por favor, informe Altura" : "Please, fill the field (C X L X A)" ));
                $('#altura40open').focus();
                return false;
            }
         }
         
         if($("input[name=cargasolta]:checked").length == 0){
            alert(((idioma!="en") ? "Por favor, informe Carga Solta - LCL" : "Please, fill the field Consolidated cargo - LCL" ));
            $("#cotacoes #cargasolta_sim").focus();
            return false;
         }
         
         if ($("#cotacoes #ncm").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe NCM" : "Please, fill the field NCM (Harmonized Code)" ));
            $("#cotacoes #ncm").focus();
            return false;
         }
         
         if ($("#cotacoes #frete").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Frete Prepaid / Collect" : "Please, fill the field Freight (Prepaid / Collect)" ));
            $("#cotacoes #frete").focus();
            return false;
         }
         
         if ($("#cotacoes #nome").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Nome" : "Please, fill the field Name" ));
            $("#cotacoes #nome").focus();
            return false;
         }
         
         if ($("#cotacoes #telefone").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Telefone" : "Please, fill the field Phone Number" ));
            $("#cotacoes #telefone").focus();
            return false;
         }
         
         if ($("#cotacoes #email").val() == ''){
            alert(((idioma!="en") ? "Por favor, informe Email" : "Please, fill the field Email" ));
            $("#cotacoes #email").focus();
            return false;
         }
         else{
              var regmail = /^[\w!#$%&amp;'*+\/=?^`{|}~-]+(\.[\w!#$%&amp;'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
              if (!regmail.test($("#cotacoes #email").val())) {
                   alert(((idioma!="en") ? "E-mail inválido" : "Invalid E-mail" ));
                   $("#cotacoes #email").focus();
                   return false;
              }
         }
         
         if ($("#cotacoes #mensagem").val() == '') {
            alert(((idioma!="en") ? "Por favor, informe Observações" : "Please, fill the field Comments" ));
            $("#cotacoes #mensagem").focus();
            return false;
         }
         
         $("#cotacoes #submit").val(((idioma!="en") ? "ENVIANDO..." : "SENDING..." ));
         $('#cotacoes input, #cotacoes textarea').attr("disabled", true);
         
        $.post($(this).attr('action'), {cliente:            encodeURI( $('#cotacoes #cliente').val()  ), 
                                        modal:              encodeURI( $('#cotacoes #modal').val() ),
                                        valorcarga:         encodeURI( $('#cotacoes #valorcarga').val() ),
                                        incoterm:           encodeURI( $('#cotacoes #incoterm').val() ),
                                        pesobruto:          encodeURI( $('#cotacoes #pesobruto').val() ) ,
                                        dimensoes:          encodeURI( $('#cotacoes #dimensoes').val() ) ,
                                        quantidade:         encodeURI( $('#cotacoes #quantidade').val() ) ,
                                        origem:             encodeURI( $('#cotacoes #origem').val() ) ,
                                        destino:            encodeURI( $('#cotacoes #destino').val() ) ,
                                        zipcode:            encodeURI( $('#cotacoes #zipcode').val() ) ,
                                        produtodescricao:   encodeURI( $('#cotacoes #produtodescricao').val() ) ,
                                        
                                        tipo_20dry:         encodeURI( (($('#cotacoes #20dry_sim:checked')) ?   $('#cotacoes #20dry_sim').val() : $('#cotacoes #20dry_nao').val() ) ) ,
                                        tipo_40dry:         encodeURI( (($('#cotacoes #40dry_sim:checked')) ?   $('#cotacoes #40dry_sim').val() : $('#cotacoes #40dry_nao').val() ) ) ,
                                        tipo_40hc:          encodeURI( (($('#cotacoes #40hc_sim:checked')) ?   $('#cotacoes #40hc_sim').val() : $('#cotacoes #40dry_nao').val() ) ) ,
                                        tipo_40Reffer:      encodeURI( (($('#cotacoes #40Reffer_sim:checked')) ?   $('#cotacoes #40Reffer_sim').val() : $('#cotacoes #40dry_nao').val() ) ) ,
                                        
                                        tipo_40open:        encodeURI( (($('#cotacoes #40open_sim:checked')) ?   $('#cotacoes #40open_sim').val() : $('#cotacoes #40dry_nao').val() ) ) ,
                                        altura40open:       encodeURI( $('#cotacoes #altura40open').val() ) ,
                                        
                                        cargasolta:         encodeURI( (($('#cotacoes #cargasolta_sim:checked')) ?   $('#cotacoes #cargasolta_sim').val() : $('#cotacoes #40dry_nao').val() ) ) ,
                                        
                                        ncm:                encodeURI( $('#cotacoes #ncm').val() ) ,
                                        frete:              encodeURI( $('#cotacoes #frete').val() ) ,
                                        nome:               encodeURI( $('#cotacoes #nome').val() ) ,
                                        telefone:           encodeURI( $('#cotacoes #telefone').val() ) ,
                                        ramal:              encodeURI( $('#cotacoes #ramal').val() ) ,
                                        email:              encodeURI( $('#cotacoes #email').val() ) ,
                                        mensagem:           encodeURI( $('#cotacoes #mensagem').val() ),
                                        idioma_form:        encodeURI( ((idioma=="en") ? "Inglês" : "Português" ) )
                                        },
        function(data) {
            if(data == "1"){
                $('#cotacoes').hide();
                $('#obrigado_cotacoes').fadeIn("slow");
            }
            else
                alert(data);
            $("#cotacoes #submit").val(((idioma!="en") ? "ENVIAR" : "SEND" ));
            $('#cotacoes input, #cotacoes textarea').removeAttr("disabled");
        });
        
        return false;
    });
    
});
