$(function() {
  $('.error').hide();
  $('#tdobra').hide();
  $('#uobra').hide();

  $('input.text-input').css({backgroundColor:"#f3f3f3"});
  $('input.text-input').css({borderColor:"#a4a4a4"});

  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#f3f3f3"});
  });

  $('textarea.text-input').css({backgroundColor:"#f3f3f3"});
  $('textarea.text-input').css({borderColor:"#a4a4a4"});

  $('textarea.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $('textarea.text-input').blur(function(){
    $(this).css({backgroundColor:"#f3f3f3"});
  });

  $('select.select-input').css({backgroundColor:"#f3f3f3"});
  $('select.select-input').css({borderColor:"#a4a4a4"});

  $('select.select-input').focus(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $('select.select-input').blur(function(){
    $(this).css({backgroundColor:"#f3f3f3"});
  });


  $("#cmp_mot").change(function () {
         var str = "";
         $("#cmp_mot option:selected").each(function () {
               str += $(this).text();
             });
		if(str == "Cotización de obra"){
			$("#tdobra").fadeIn("slow");
			$("#uobra").fadeIn("slow");
		} else {
  			$('#tdobra').fadeOut("slow");
			$("#uobra").fadeOut("slow");
		}
       })




$(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	var nombre = $("input#cmp_nap").val();
		if (nombre == "") {
      $("label#cmp_nap_error").show();
      $("input#cmp_nap").focus();
      return false;
    }

	var telefono = $("input#cmp_tel").val();
		if (telefono == "") {
      $("label#cmp_tel_error").show();
      $("input#cmp_tel").focus();
      return false;
    }

	var email = $("input#cmp_eml").val();
		if (email == "") {
      $("label#cmp_eml_error").show();
      $("input#cmp_eml").focus();
      return false;
    }

	var motivo = $("select#cmp_mot option:selected").val();
		if (motivo == "") {
      $("label#cmp_mot_error").show();
      $("select#cmp_mot").focus();
      return false;
    }


	var asunto = $("input#cmp_as").val();
		if (asunto == "") {
      $("label#cmp_as_error").show();
      $("input#cmp_as").focus();
      return false;
    }


	var mensaje = $("textarea#cmp_msg").val();
		if (mensaje == "") {
      $("label#cmp_msg_error").show();
      $("textarea#cmp_msg").focus();
      return false;
    }

	alert(mensaje);

	var empresa = $("input#cmp_emp").val();
	var caracteristica = $("input#cmp_ctel").val();
	var pais = $("input#cmp_pa").val();
	var tipoobra = $("select#cmp_to option:selected").val();
	var ubicacion = $("input#cmp_ubo").val();
	

	var dataString = 'nombre='+ nombre +'&empresa=' + empresa +'&telcar='+ caracteristica +'&telefono='+ telefono +'&email='+ email +'&pais='+ pais +'&motivo='+ motivo +'&tipoobra='+ tipoobra +'&ubicacion='+ ubicacion +'&asunto='+ asunto +'&mensaje='+ mensaje;
		
	$.ajax({
      type: "POST",
      url: "includes/process.php",
      data: dataString,
      success: function() {
        $('#cntForm').html("<div id='message'></div>");
        $('#message').html("<h3>Formulario enviado exitosamente</h3>")
        .append("<p>En breve responderemos su inquietud.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='/imagenes/check.png' />");
        });
      }
    });
    return false;
	});
});
runOnLoad(function(){
  $("input#cmp_nap").select().focus();
});

