﻿/*
 *  Filename:   Forms.js
 *  Purpose:    Adds extra functionality to forms
 *  Created:    2010-07-12
 *  Authors:    John Sedlak
 *  Usage:
 *              - Once the document is ready, call AttachToForm(...) with the parent element of the form.
 *              - Parent should be above the .FormPanel elements
 *  Notes:
 *              - Parent doesn't work currently
 *              - Not tested on multiple forms per page
 */

var extraFieldsValidationCallback = null;

/* ValidateField - Validates a field's input */
function ValidateField(field){
    //alert(field.val());
    if (field.val() == null || field.val().length == 0) return false;

    if(field.hasClass("ZipCode") == true && field.val().length < 5){
        return false;
    }

    return true;
}

function OnRequiredChanged() {
    missingFields = "";
    var check = true;

    $(":input.Required").each(function () {
        if (ValidateField($(this)) == false) {
            $(this).removeClass("FilledIn");

            var name = $(this).attr('title');
            if (name.length == 0) {
                name = $(this).attr('name');
                name = name.substr(name.indexOf("$") + 1);
            }

            missingFields += "<li>" + name + "</li>";

            check = false;
        } else {
            $(this).addClass("FilledIn");
        }
    });

    if (check) {
        $(".SendButton").removeClass("disabled");
        //$(".SendButton").attr("disabled", "");
    }
    else {
        $(".SendButton").addClass("disabled");
        //$(".SendButton").attr("disabled", "disabled");
    }

    return { 
        "check": check,
        "missingFields": missingFields
    };
}

function AttachToForm(parent) {
    AttachToFormEx(parent, null);
}

function AttachToFormEx(parent, extraFieldsValidation) {
    $(".ErrorMessagePanel").hide();

    $(".ErrorMessagePanel .Close").click(function (event) {
        $(".ErrorMessagePanel").fadeOut(500);
    });

    extraFieldsValidationCallback = extraFieldsValidation;

    /* Attach to all the required input fields */
    $(":input.Required").each(function () {
        $(this).bind('keyup', OnRequiredChanged);
    });

    /* Attach to all the required drop downs */
    $("select.Required").each(function () {
        $(this).change(OnRequiredChanged);
    });

    /* For each panel in the form, make sure we can show and hide it */
    $(".FormPanel").each(function () {
        // Each panel is controlled by an H3 tag
        $("h3", this).click(function () {
            var span = $("span", $(this));

            if (span.html() == "Show") { span.html("Hide"); }
            else { span.html("Show"); }

            $(".InnerFormPanel", $(this).parent()).animate(
                { height: 'toggle' },
                function () { }
            );

            return false;
        });
    });

    /* Attach to the button element */
    $(".SendButton").click(function (event) {


        var result = OnRequiredChanged();

        if (extraFieldsValidationCallback != null) {
            var r2 = extraFieldsValidationCallback();

            if (r2.check == false) {
                result.check = false;
                result.missingFields += r2.missingFields;
            }
        }

        if (result.check == false) {
            event.preventDefault();

            $(".ErrorMessagePanel").fadeIn(500);
            $(".ErrorMessagePanel .Message").html('<p>You are missing the following fields.</p><ul>' + result.missingFields + '</ul>');
            $(".ErrorMessagePanel").animate({ backgroundColor: '#fff799' }, 500);

            return false;
        }
    });

    OnRequiredChanged();

    AddValidation();
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function AddValidation() {
    $(".textbox").keyup(function () {
        var i = $(this).attr("name").indexOf("$");

        var id = $(this).attr("id");
        if (i >= 0) { id = $(this).attr("id").substr(i + 1); }

        var count = 0;
        if ($(this).val() != null) count = $(this).val().length;

        var max = $(this).attr("jsMaxLength");
        if (max == null || max == null) { max = $(this).attr("maxLength"); }

        if (max != null && max != NaN && max > 0 && max < count) {
            $(this).val($(this).val().substr(0, max));
            count = max;
        }

        var e = $("#" + id + "CharsLeft");
        if (e != null) { e.html((max - count).toString()); }
    });
}

function sendExternalLinksToBlank() {
    $("a.External").attr("target", "_blank");
    $(".ExternalLink a").attr("target", "_blank");
    $("a.ExternalLink").attr("target", "_blank");
}
