/*
* Forge Industrial Engineering
* @copyright (c) 2009 Quikrete
* @author Michael Maw
*/

$(function()
{
    $("a[rel^='prettyPhoto']").prettyPhoto({ default_width: 400 });
    $("#form-contact").ajaxForm($form.options);
});


/** 
* AJAX Form
* @namespace
*/
$form =
{
    /** 
    * AJAX Form Submission Options
    * @namespace
    */
    options:
    {
        url: "/core/ajax/send.aspx",
        beforeSubmit: function()
        {
            if ($form.validate("#form-contact"))
            {
                return true;
            }
            else
            {
                return false;
            }
        },
        success: function(result)
        {
            if (result == "true")
            {
                alert("Thank you. Your request has been sent.");
            }
            else
            {
                alert("We're sorry. Your request could not be sent at this time.");
            }
        }
    },

    /** 
    * Validate all fields in a form
    * @param {String} form jQuery selector for the form
    * @returns {Boolean} true or false
    */
    validate: function(form)
    {
        // Validate all required fields
        $(".form-required", form).each(function()
        {
            if ($(this).val() === "")
            {
                $form.error.process(this, form);
            }
            if (this.nodeName.toLowerCase() == "select")
            {
                if ($(this).children(":selected").length < 1)
                {
                    $form.error.process(this, form);
                }
            }
        });

        // Validate all email fields
        $(".form-email", form).each(function()
        {
            if ($(this).val() !== "")
            {
                if (!$(this).val().match(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/))
                {
                    $form.error.process(this, form);
                }
            }
        });

        // Show error message if validation fails
        if ($form.error.total > 0)
        {
            alert($form.error.total + " fields are empty or contain invalid values (" + $form.error.fields + ")", "error");
            $form.error.total = 0;
            $form.error.fields = "";
            return false;
        }
        else
        {
            return true;
        }
    },

    /** 
    * Form validation error handling
    * @namespace
    */
    error:
    {
        /** 
        * Total number of form errors encountered during validation
        * @type String
        */
        total: 0,

        /** 
        * Comma-separated list of form field names containing errors
        * @type String
        */
        fields: "",

        /** 
        * Form field error processing
        * @param {Object} field DOM object referring to the field containing the error
        * @param {String} form jQuery selector for the current form
        */
        process: function(field, form)
        {
            $form.error.total++;
            if ($form.error.fields !== "")
            {
                $form.error.fields += ", ";
            }
            $form.error.fields += $("label[for=" + $(field).attr("name") + "]", form).text();
            $(field).addClass("f-form-error").focus(function() { $(field).removeClass("f-form-error"); });
        }
    }
}