/**
 * Contact form Page
 *
 * This is the javascript file for contact.php
 * @author David Vélez <david.velez@entersoftweb.com>
 * @version 1.0
 * @package javascript
 */

/**
 * Unsaved changed pending system
 */
var canExit = true;
window.onbeforeunload = function(e) {          
     if(!canExit) {
          return msgExit;
     }
};

/**
 * Form inicialization
 */
$(document).ready(function() {

    $("#loadingBlock").hide();
    $("#contactSendedBlock").hide();

    /**
     * Dialog startup
     */
    $('#dialogMissingField').dialog({
        autoOpen: false,
        resizable: false,
        width: 600,
        modal: true,
        buttons: {
            Ok: function() {
                    $(this).dialog('close');
            }
        }
    });

    $('#zoomImgOfi').colorbox({ opacity: 0.35 });    

    /**
     * Masks
     */
    mask("telefono", "numeric", 0);
    mask("correo", "email", 0);

    /**
     * Check if any have changed in the form
     */
    $($("#f").elements).bind("change", function(){ canExit = false; })

    /**
     * Form validations
     */
    $("#f").bind("submit", function() {

        // Disable submit button
        $("#submitFormButton").attr("disabled", "disabled").fadeTo("fast", 0.25);

		// First check the requerid field is not empty
        var missingFields = new Array(); var error = null;
        if((error = checkField("nombre", "exist")))     missingFields.push(error);        
        if((error = checkField("correo", "email")))     missingFields.push(error);        
        if((error = checkField("comentarios", "exist")))   missingFields.push(error);

        // If any error cancel form submit
        if(missingFields.length){
            // Enable submit button
        	$("#submitFormButton").attr("disabled", "").fadeTo("fast", 1);



                $('#dialogMissingField').dialog("open");

        }else{
            canExit = true;

            $("#formBlock").fadeOut("fast", function(){
                $("#loadingBlock").fadeIn("fast", function(){
                    $.post("/contacto.php", {
                            // POST Parameters
                            action: 2,
                            name: $('#nombre').val(),
                            phone: $('#telefono').val(),
                            email: $('#correo').val(),
                            address: $('#direccion').val(),
                            nextVisit: $('#proxima_visita').val(),
                            comments: $('#comentarios').val()
                        },
                        // Return control
                        function(data){
                            if(!data["error"]){   // All Ok
                                $("#loadingBlock").fadeOut("fast", function(){
                                    $("#contactSendedBlock").fadeIn("fast");
                                });
                            }
                        }
                        ,"json"
                    );
                });
            });
        }
        // Always return false
        return false;
    });
});
