﻿(function ($) {

    $.postOrIFrameInjection = function (selector, url, data, callback) {

        if ($.browser.msie || ($.browser.mozilla && $.browser.version != null && parseInt($.browser.version.slice(0, 1)) < 5)) {

            var parameters = "";

            if (data != null)
                for (var key in data)
                    parameters += (parameters != "" ? "&" : "") + key + "=" + (data[key] != undefined ? escape(data[key]) : "");

            $(selector).attr("src", url + "?" + parameters);

            setTimeout(function () {
                if (callback != null)
                    callback({ ServiceError: false });
                },
                5000);
        }
        else {
            $.post(url, data, callback);
        }
    };
    
    $.fn.enterPress = function (callback) {

        this.keypress(function (e) {

            code = (e.keyCode ? e.keyCode : e.which);

            if (code == 13) {
                callback(this);
                return false;
            }
        });
    };
})(jQuery);

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
//http: //localhost:58130/?wtDebugMode=true
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GENERATE RANDOM TAIL                                                                                         //
// A standardized method for generating a random querystring to add to the end of post & json requests to       //
// prevent caching.                                                                                             //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getRandomTail() {
    return "&RND=" + String(Math.floor(Math.random() * 9)) + String(Math.floor(Math.random() * 9)) +
                     String(Math.floor(Math.random() * 9)) + String(Math.floor(Math.random() * 9)) +
                     String(Math.floor(Math.random() * 9)) + String(Math.floor(Math.random() * 9));
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INIT DEBUG                                                                                                   //
// Checks for the debug frame, if not then requests it.                                                         //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function initDebug() {
    var debugManagerReady = false;

    try {
        debugManagerReady = parent.DebugManager.ready;
    } catch (err) {

    }

    if (debugManagerReady == false) {
        try {
            DebugManager.startDebugging();
        } catch (err) {

        }
    } else if (debugManagerReady == true) {
        try {
            parent.DebugManager.initWindow();
            parent.DebugManager.attachDebugToReporting(reporting.bellman);
        } catch (err) {

        }
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// REPORT SITE EVENT                                                                                            //
// A standardized method for site event reporting.                                                              //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function reportSiteEvent(reportingObject, theError, errorLevel) {
    // if site set to debug mode, notify user via debug console
    try {
        if (parent.DebugManager != null && typeof parent.DebugManager != "undefined") {

            switch (errorLevel) {
                case 2:
                    errorLevelCategory = "Warnings";
                    break;

                case 3:
                    errorLevelCategory = "Trace Out";
                    break;

                case 4:
                    errorLevelCategory = "Reporting";
                    break;

                case 5:
                    errorLevelCategory = "Dart";
                    break;

                case 6:
                    errorLevelCategory = "Omniture";
                    break;

                case 7:
                    errorLevelCategory = "Efficient Frontier";
                    break;

                default:
                    var errorLevelCategory = "Fatal Errors";
                    break;
            }

            parent.DebugManager.report(reportingObject, theError, errorLevelCategory);
        }
    } catch (err) {

    }
}

jQuery.fn.setupForm = function (addWatermarks) {
    // get parameters.
    var args = arguments[0] || {}; // ({PARAMETERNAME: []}) == args.PARAMETERNAME

    // get base element.
    var form = $(this[0]);

    // run through all form elements, any known ids get setup, ignore the rest.
    $("#" + form.attr('id') + " input").each(function () {
        if ($(this).attr('id').toLowerCase().indexOf("phone") > -1) {
            if ($(this).mask != null && typeof $(this).mask != null)
                $(this).mask("(999) 999-9999");

            if (addWatermarks != false)
                $(this).watermark("Phone");
        }

        if ($(this).attr('id').toLowerCase().indexOf("zipcode") > -1) {
            if ($(this).mask != null && typeof $(this).mask != null)
                $(this).mask("99999");

            if (addWatermarks != false)
                $(this).watermark("Zip Code");
        }

        if ($(this).attr('id').toLowerCase().indexOf("firstname") > -1)
            if (addWatermarks != false)
                $(this).watermark("First Name");

        if ($(this).attr('id').toLowerCase().indexOf("city") > -1)
            if (addWatermarks != false)
                $(this).watermark("City");

        if ($(this).attr('id').toLowerCase().indexOf("lastname") > -1)
            if (addWatermarks != false)
                $(this).watermark("Last Name");

        if ($(this).attr('id').toLowerCase().indexOf("email") > -1) {
            if (addWatermarks != false)
                $(this).watermark("Email Address");
        } else if ($(this).attr('id').toLowerCase().indexOf("address") > -1) {
            if (addWatermarks != false)
                $(this).watermark("Street Address");
        }

        if ($(this).attr('id').toLowerCase().indexOf("comments") > -1)
            if (addWatermarks != false)
                $(this).watermark("Comments");
    });
};

jQuery.fn.validateForm = function () {
    // get parameters.
    var args = arguments[0] || {}; // ({PARAMETERNAME: []}) == args.PARAMETERNAME
    var required = args.RequiredFields;
    var skipRegex = args.SkipRegex;
    var failCondition = "";

    // get base element.
    var form = $(this[0]);

    // run through all required fields and make sure they are filled out.

    // if its not null
    if (required != null) {

        // and it has entries
        if (required != "") {

            // convert them to array (from comma deliminated string)            
            required = required.toLowerCase().split(",");

            // if there are entries to run through
            if (required.length > 0) {
                // for each entry
                for (var i = 0; i < required.length; i++) {

                    // check each input field (in this 'form' or parent)
                    $("#" + form.attr('id')).find("select,input,textarea").each(function () {

                        // if this field id has the phrase in it anywhere.
                        if ($(this).attr('id').toLowerCase().indexOf(required[i]) > -1) {

                            // then check that they didnt leave the feild empty.
                            if ($(this).val() == "") {
                                failCondition = required[i];
                                return required[i];

                                // also if its an email feild, do standard email validation on it as well.
                            } else if (required[i].toLowerCase().indexOf("email") > -1) {
                                if (skipRegex != true) {
                                    var emailRegExTest = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

                                    if (!emailRegExTest.test($(this).val())) {
                                        failCondition = required[i];
                                        return required[i];
                                    }
                                } else {
                                    if ($(this).val().indexOf("@") == -1) {
                                        failCondition = required[i];
                                        return required[i];
                                    }

                                    if ($(this).val().indexOf(".") == -1) {
                                        failCondition = required[i];
                                        return required[i];
                                    }
                                }
                            } else if (required[i].toLowerCase().indexOf("zipcode") > -1) {
                                if (skipRegex != true) {
                                    var zipRegExTest = /^\d{5}$|^\d{5}-\d{4}$/;

                                    if (!zipRegExTest.test($(this).val())) {
                                        failCondition = required[i];
                                        return required[i];
                                    }
                                } else {
                                    if ($(this).val().length != 5) {
                                        failCondition = required[i];
                                        return required[i];
                                    }
                                }
                            }
                        }
                    });
                }
            }
        }
    }

    if (failCondition != "")
        return failCondition;
    else
        return true;
};
