﻿(function($) {

    $.extend({
        postWebMethod: function(url, data, callback) {
            return jQuery.ajax({
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                url: url,
                data: JSON.stringify(data),
                success: function(data, textStatus) {
                    if (textStatus == 'success')
                        callback(eval(data.d), textStatus);
                    else
                        callback(null, textStatus);
                },
                dataType: 'json',
                async: false
            });
        },

        joinAttr: function(selector, attr, delimiter) {
            var result = '';
            var elm = $(selector);

            if (elm.size() > 0) {
                elm.each(function() {
                    if (result) result += delimiter;
                    result += $(this).attr(attr);
                });
            }

            return result;
        }
    });

    $.fn.extend({
        serializeJSON: function() {
            var result = {};
            var list = $(this).serializeArray();
            for (i = 0; i < list.length; i++) {
                eval("$.extend(result, {'" + list[i].name + "': '" + addslashes(list[i].value) + "'});");
            }
            return result;
        }
    });

    /*=============================
    jQuery Validation
    =============================*/
    if ($.validator) {
        // regular expression validator
        $.validator.addMethod(
            'regex',
            function(value, element, regexp) {
                return this.optional(element) || value.match(new RegExp(regexp));
            }, 'Invalid value');

        // compare validator
        $.validator.addMethod(
            'compare',
            function(value, element, params) {
                if (this.optional(element) || !params.element) return true;

                value = $.trim(value);

                params.datatype = params.datatype || 'string';
                params.type = params.type || '>=';

                var compareValue = $.trim($.isFunction(params.element) ? params.element() : $(params.element).val());
                if (!compareValue) return true;

                var currData = null;
                var compareData = null;
                if (params.datatype == 'date') {
                    currData = value ? parseDateDDMMYYYY(value) : null;
                    compareData = compareValue ? parseDateDDMMYYYY(compareValue) : null;
                } else if (params.datatype == 'number') {
                    currData = value ? parseFloat(value) : null;
                    compareData = compareValue ? parseFloat(compareValue) : null;
                } else {
                    currData = value || null;
                    compareData = compareValue || null;
                }

                if (params.type == '>')
                    return currData > compareData;
                else if (params.type == '>=')
                    return currData >= compareData;
                else if (params.type == '<')
                    return currData < compareData;
                else if (params.type == '<=')
                    return currData <= compareData;

                return currData == compareData;
            }, 'Invalid range');
    }


    $('input:submit').live('mouseover mouseout', function(event) {
        if (event.type == 'mouseover') {
            $(this).addClass('ui-state-hover').css('cursor', 'pointer');
        } else {
            $(this).removeClass('ui-state-hover').css('cursor', 'none');
        }
    });

    $(':button').live('mouseover mouseout', function(event) {
        if (event.type == 'mouseover') {
            $(this).addClass('ui-state-hover').css('cursor', 'pointer');
        } else {
            $(this).removeClass('ui-state-hover').css('cursor', 'none');
        }
    });

})(jQuery);

function parseDateDDMMYYYY(value) {
    if (!value) return null;

    var arrDate = value.split("/");

    return new Date(arrDate[2], arrDate[1] - 1, arrDate[0]);
}

/* Show terms & conditions dialog */
function showTC() {
    var containerTC = $('#containerTC');
    if (!$.trim(containerTC.html()))
        containerTC.load('/Terms.aspx');

    containerTC.dialog('open');
}

function addslashes(str) {
    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}
