﻿// precache images
// from http://technosophos.com/content/precaching-images-jquery-slightly-better-way
function cacheImages(imageArray) {

    // Add hidden element
    var hidden = $('body').append('<div id="img-cache" style="display:none/>').children('#img-cache');
    // Add images to hidden element.

    $.each(imageArray, function(i, val) {
        $('<img/>').attr('src', val).appendTo(hidden);
    });
}

// modified from http://greatwebguy.com/programming/dom/prevent-double-submit-with-jquery/ 
// comment 12
$.fn.preventDoubleSubmit = function(alertText, buttonClass, buttonClickedImage) {
    $(this).submit(function() {
        if (this.beenSubmitted) {
            if (alertText != '')
                alert(alertText);
            return false;
        }
        else {
            if (Page_IsValid) {
                this.beenSubmitted = true;
                if (buttonClass != '' && buttonClickedImage != '')
                    $(buttonClass).attr("src", buttonClickedImage);
            }
            else {
                this.beenSubmitted = false;
            }
        }
    });
};

// preventDoubleSubmit is only good if its the only form on the page
// so this method attaches to the button click instead
// which won't prevent enter button twice submissions, although if its a textarea, then that can't happen anyway
$.fn.preventButtonDoubleSubmit = function(formName, alertText, buttonClickedImage) {
    $(this).click(function() {
        if (this.beenSubmitted) {
            if (alertText != '')
                alert(alertText);
            return false;
        }
        else {
            if (Page_IsValid) {
                this.beenSubmitted = true;
                if (this != '' && buttonClickedImage != '')
                    $(this).attr("src", buttonClickedImage);
                formName.submit();
            }
            else {
                this.beenSubmitted = false;
            }
        }
    });
};

$.fn.redirectEnterToClick = function(button) {
    $(this).keypress(function(event) {
        if (event.keyCode == 13) { button.click(); }
    });
};

