﻿///<reference path="/Scripts/jquery-1.7.1.js" />
///<reference path="/Scripts/jquery-ui-1.8.16.custom.min.js" />
//todo: replace /Resources/Javascript/main.js with this file

function isNumeric(s) {
    return !isEmpty(s) && !isNaN(s);
}

function formatNum(val) {
    var fval = val + '';
    var rgx = /(\d+)(\d{3})/;
    if (isNumeric(val)) {
        while (rgx.test(fval)) {
            fval = fval.replace(rgx, '$1' + ',' + '$2');
        }
    }
    return fval;
}

var waitForFinalEventTimers = {};
function waitForFinalEvent(name, callback, delay) {
    name = name || 'error';
    if (waitForFinalEventTimers[name])
        clearTimeout(waitForFinalEventTimers[name]);
    waitForFinalEventTimers[name] = setTimeout(callback, delay || 800);
}

function log(val, isVisible) {
    isVisible = isVisible || true;
    var elem = $('#log').length > 0 ? $('#log') : $('<div></div>').attr('id', 'log').insertAfter('#errorList');
    elem.html(new Date() + ': ' + val + '<br/>\n' + elem.html());
    elem.toggle(isVisible);
}

// custom jQuery methods
(function ($) {
    $.fn.extend({            
        
        showError: function (text, onclick, autoHideDelay) {
            return this.showInfo(text, onclick, autoHideDelay, 'error');
        },

        showInfo: function (text, onclick, autoHideDelay, type) {
            type = type || 'info';

            if(type == 'error')
                this.addClass('error');

            this.validationEngine(
                'showPrompt', text, type, 'topLeft');

            if (typeof onclick === 'function')
                $('.' + this.attr('id') + 'formError div').click(onclick);

            if (autoHideDelay){
                autoHideDelay = (autoHideDelay == true ? 2000 : autoHideDelay);
                $('.' + this.attr('id') + 'formError div').delay(autoHideDelay).fadeOut(400);
            }

            return this;
        },

        hideError: function () {
            var isPostError = $('.error_list span[rel*=' + this.attr('id') + ']').length > 0;
            if(!isPostError){
                this.next('img[src*=error]:not([alt*=required])').hide();
                this.removeClass('error');
            }
            return this.validationEngine('hide');
        }

    });
})(jQuery);


/************** Region below reserved for smaller, minified jQuery plugins, to minimize # of file includes **************/

/*
* jQuery Watermark plugin
* Version 1.2.1 (7-DEC-2010)
* @requires jQuery v1.3 or later
*
* Examples at: http://mario.ec/static/jq-watermark/
* Copyright (c) 2010 Mario Estrada
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*/
(function (a) { var b = a.browser.msie && a.browser.version < 8, c = 4; a.watermarker = function () { }, a.extend(a.watermarker, { defaults: { color: "#999", left: 0, top: 0, fallback: !1, animDuration: 300, minOpacity: .6 }, setDefaults: function (b) { a.extend(a.watermarker.defaults, b) }, checkVal: function (b, c, d) { b.length == 0 ? a(c).show() : a(c).hide(); return b.length > 0 }, html5_support: function () { var a = document.createElement("input"); return "placeholder" in a } }), a.fn.watermark = function (d, e) { var e, f; e = a.extend(a.watermarker.defaults, e), f = this.filter("textarea, input:not(:checkbox,:radio,:file,:submit,:reset)"); if (!e.fallback || !a.watermarker.html5_support()) { f.each(function () { var f, g, h, i, j, k, l, m, n = 0, o, p; f = a(this), f.attr("data-jq-watermark") != "processed" && (g = f.attr("placeholder") != undefined && f.attr("placeholder") != "" ? "placeholder" : "title", h = d === undefined || d === "" ? a(this).attr(g) : d, i = a('<span class="watermark_container"></span>'), j = a('<span class="watermark">' + h + "</span>"), g == "placeholder" && f.removeAttr("placeholder"), i.css({ display: "inline-block", position: "relative" }), b && i.css({ zoom: 1, display: "inline" }), f.wrap(i).attr("data-jq-watermark", "processed"), this.nodeName.toLowerCase() == "textarea" ? (e_height = f.css("line-height"), e_height = e_height === "normal" ? parseInt(f.css("font-size")) : e_height, n = f.css("padding-top") != "auto" ? parseInt(f.css("padding-top")) : 0) : (e_height = f.outerHeight(), e_height <= 0 && (e_height = f.css("padding-top") != "auto" ? parseInt(f.css("padding-top")) : 0, e_height += f.css("padding-bottom") != "auto" ? parseInt(f.css("padding-bottom")) : 0, e_height += f.css("height") != "auto" ? parseInt(f.css("height")) : 0)), n += f.css("margin-top") != "auto" ? parseInt(f.css("margin-top")) : 0, k = f.css("margin-left") != "auto" ? parseInt(f.css("margin-left")) : 0, k += f.css("padding-left") != "auto" ? parseInt(f.css("padding-left")) : 0, j.css({ position: "absolute", display: "block", fontFamily: f.css("font-family"), fontSize: f.css("font-size"), color: e.color, left: c + e.left + k, top: e.top + n, height: e_height, lineHeight: e_height + "px", textAlign: "left", pointerEvents: "none" }).data("jq_watermark_element", f), a.watermarker.checkVal(f.val(), j), j.click(function () { a(a(this).data("jq_watermark_element")).trigger("click").trigger("focus") }), f.before(j).bind("focus.jq_watermark", function () { a.watermarker.checkVal(a(this).val(), j) || j.stop().fadeTo(e.animDuration, e.minOpacity) }).bind("blur.jq_watermark change.jq_watermark", function () { a.watermarker.checkVal(a(this).val(), j) || j.stop().fadeTo(e.animDuration, 1) }).bind("keydown.jq_watermark, paste.jq_watermark", function (b) { a(j).hide() }).bind("keyup.jq_watermark", function (b) { a.watermarker.checkVal(a(this).val(), j) })) }); return this } }, a(".jq_watermark").watermark() })(jQuery);
