﻿String.prototype.HtmlEncode = function () 
{
    return $('<div/>').text(this.toString()).html();
}


String.prototype.Trim = function () { 
    return this.replace(/(^\s+)|(\s+$)/, '');
}
String.prototype.IsInt = function () {
    return this.Trim().match(/^\d+$/);
}

/*-----------------*/

var Utils = {};

Utils.Serialize = function (obj, indentValue) {
    var hexDigits = "0123456789ABCDEF";
    function ToHex(d) {
        return hexDigits[d >> 8] + hexDigits[d & 0x0F];
    }
    function Escape(string) {
        return string.replace(/[\x00-\x1F'\\]/g,
        function (x) {
            if (x == "'" || x == "\\") return "\\" + x;
            return "\\x" + ToHex(String.charCodeAt(x, 0));
        })
    }

    var indent;
    if (indentValue == null) {
        indentValue = "";
        indent = ""; // or " "
    }
    else {
        indent = "\n";
    }
    return GetObject(obj, indent).replace(/,$/, "");

    function GetObject(obj, indent) {
        if (typeof obj == 'string') {
            return "'" + Escape(obj) + "',";
        }
        if (obj instanceof Array) {
            result = indent + "[";
            for (var i = 0; i < obj.length; i++) {
                result += indent + indentValue +
            GetObject(obj[i], indent + indentValue);
            }
            result += indent + "],";
            return result;
        }
        var result = "";
        if (typeof obj == 'object') {
            result += indent + "{";
            for (var property in obj) {
                result += indent + indentValue + "'" +
            Escape(property) + "' : " +
            GetObject(obj[property], indent + indentValue);
            }
            result += indent + "},";
        }
        else {
            result += obj + ",";
        }
        return result.replace(/,(\n?\s*)([\]}])/g, "$1$2");
    }
}


Utils.Ajax = {
    SubmitForm: function (form, datatype, successCallback, errorCallback, extraParams) {
        var formData = form.serialize();


        if (extraParams) {
            for (var param in extraParams) {
                formData += '&' + param + '=' + extraParams[param];
            }
        }

        $.ajax({
            type: form.attr('method') || 'POST',
            url: form.attr('action'),
            data: formData,
            success: successCallback,
            error: errorCallback,
            dataType: datatype
        });
    }
}

Utils.UI =
{
    ScrollIntoView: function (element, container) {
        container = container || $(document);
        $(container).scrollTop(element.offset().top)
    },
    AssignRollover: function (jQueryObject, findString, replaceString) {
        findString = findString || 'btn';
        replaceString = replaceString || 'btn_hi';

        jQueryObject.hover(
            function () {
                var btnSource = $(this).attr('src');
                $(this).attr('src', btnSource.replace(findString, replaceString));
            },
            function () {
                var btnSource = $(this).attr('src');
                $(this).attr('src', btnSource.replace(replaceString, findString));
            }
         );
    },
    AssignCollapsibleFormPanels: function () {
      $('.simple-form.collapsible').each(
           function () {
               function toggleFormContent(form) {
                   var content = form.find('.form-content');
                   content.is(':visible') ? content.slideUp() : content.slideDown();
               }
               var form = $(this);
               form.find('legend').click(function () { toggleFormContent(form) });
           }
       );
    }
}


$('.require-int').live('keyup', function () { 
    this.value = this.value.replace(/[^0-9\.+-]/g,'');
});

$('.require-websafe').live('keyup', function () {
    this.value = this.value.replace(/[<>|]/g, '');
});



