$(function() {
  $('#modal-contattaci').bind('show', function () {
    $.get('/contattaci', function(data){ $('#modal-body').html(data); });
  });

  $('#modal-preventivo').bind('show', function () {
    $.get('/contattaci/preventivo', function(data){ $('#modal-body-preventivo').html(data); });
  })

});

function contattaci() {
  //alert('inviato');
  
  var 
    contacttype = $('#contacttype').val()
  , name        = $('#name').val()
  , surname     = $('#surname').val()
  , email       = $('#email').val()
  , phone       = $('#phone').val()
  , company     = $('#company').val()
  , companytype = $('#companytype').filter(":selected").val()
  , note        = $('#note').val()
  , privacy     = $('#input-privacy:checked').val()
  , error       = false;
  ;
  
  if(privacy != 1 ){
    $('#input-privacy').parent().parent().parent().parent().addClass('error');
    $('#input-privacy').addClass('error');
    $('#input-privacy').focus();
    error = true;
  }

  if(note == 'Richiedi informazioni' || note.length < 5 ){
    $('#note').parent().parent().addClass('error');
    $('#note').addClass('error');
    $('#note').focus();
    //$('#infos_error').empty().html('<span class="errore">Indicaci ciò di cui hai bisogno.</span>');
    error = true;
  }

  reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if(reg.test(email) == false) {
    $('#email').parent().parent().addClass('error');
    $('#email').addClass('error');
    $('#email').focus();
    error = true;
  }

  if(surname.length < 5 ){
    $('#surname').parent().parent().addClass('error');
    $('#surname').addClass('error');
    $('#surname').focus();
    error = true;
  }

  if(name.length < 5 ){
    $('#name').parent().parent().addClass('error');
    $('#name').addClass('error');
    $('#name').focus();
    error = true;
  }

  if (error) return false;

  $.ajax({
    type: "POST",
    url: "/contattaci/invia",
    data: "contacttype="+contacttype+"&"+
          "name="+name+"&"+
          "surname="+surname+"&"+
          "email="+email+"&"+
          "phone="+phone+"&"+
          "company="+company+"&"+
          "companytype="+companytype+"&"+
          "note="+note+"&",
    beforeSend: function(){
      $('#modal-contattaci-send').attr('disabled','true');
      $('#modal-contattaci-result').empty().html('<img src="/assets/img/loading.gif" style="position:relative; top:4px;" />');
    },
    success: function(result){
      $('#modal-contattaci-result').empty().html('La richiesta è stata correttamente inoltrata.');
      $('#modal-contattaci-result').empty().html(result);
      $('#modal-contattaci-reset').remove();
      setTimeout(function() { $('#modal-contattaci').modal('hide'); return false; }, 2000)
      return false;
      /*
      if(result) {
        $('#modal-contattaci-result').empty().html('La richiesta è stata correttamente inoltrata.');
        $('#infoscorsi').css('backgroundColor', 'white').css('padding','5px');
        return false;
      }
      else {
        $('#infos_error').empty().html('<span class="errore">Errore: ' + result + '.</span>');
        $('#infosinvia').removeAttr('disabled');
        return false;
      }
      */
    },
    error: function() {
      $('#modal-contattaci-result').empty().html('Errore di collegamento');
      $('#modal-contattaci-send').removeAttr('disabled');
    }
  });
  return false;
}

