/*
 * Author: Jacek Pakulski, Digitise Media, 2009
 * Date: 22/12/2009
 * Description:
 * Ticket submission form validation script ment to be used by eForm MODx module
 */

function validate(form)
{
    var is_valid = 1;

    // Grab all elements from the form
    $(form).find("input, select, textarea").each(function() {

        var rel = $(this).attr("rel");

        if (rel)
        {
            var eform_arr = rel.split(":");
            var eform_is_required = eform_arr[2];

            if (eform_is_required === "1")
            {
                var eform_name = eform_arr[0];
                var eform_type = eform_arr[1];
                var error_message = (eform_arr.length > 3) ? eform_arr[3] : "";
                var reg_exp = (eform_arr.length > 4) ? eform_arr[4] : "";
                var value = $(this).val();

                switch (eform_type)
                {
                    case "string":
                        is_valid &= isEmpty(this, eform_name, value, error_message) ? false : true;
                        break;
                    case "email":
                        var valid_email_pattern = /^[\d\w.-_]+@[\d\w.-_]+\.[\w]{2,3}$/;
                        error_message = error_message ? error_message : "A valid email address is required!";
                        is_valid &= isInvalid(this, eform_name, value, valid_email_pattern, error_message) ? false : true;
                        break;
                    default:
                        is_valid &= isInvalid(this, eform_name, value, reg_exp, error_message) ? false : true;
                }
            }

            // Clear the error if the user selects the element
            $(this).bind("focus", function() {
                removeErrorMsg(this);
            });
        }
    });

    return (is_valid === 1) ? true : false;
}

// Check if the value is empty (or contains all white space characters)
function isEmpty(element, name, value, error_message)
{
    if (value.match(/^\s*$/))
    {
        addErrorMsg(element, name, error_message);
        return true;
    }
    
    removeErrorMsg(element);
    return false;
}

// Check if the field validates using the supplied regular expression
function isInvalid(element, name, value, reg_exp, error_message)
{
    if (reg_exp)
    {
        if (!value.match(reg_exp))
        {
            addErrorMsg(element, name, error_message);
            return true;
        }
    }

    removeErrorMsg(element);
    return false;
}

// Mark an error
// Add an error flag beside the form element
// Add an Error class to the element
function addErrorMsg(element, name, error_message)
{
    if (!error_message)
    {
        error_message = name + " is required!"
    }

    var error_tag = $("<span>").addClass("InlineError").text(error_message);

    // If the element is not already flagged with an error insert an error marker
    if ($(element).next(".InlineError").length === 0)
    {
        $(element).after(error_tag);
        $(element).addClass("Error");
    }
}

// Remove the error flag and Error class
function removeErrorMsg(element)
{
    $(element).next(".InlineError").remove();
    $(element).removeClass("Error");
}
