﻿String.prototype.toInt = function () { var val = parseInt(this.toString(), 10); return isNaN(val) ? 0 : val; };
String.prototype.toIntFilter = function () {
    var str = this.toString();
    var val = 0;
    for (var i = 0; i < str.length; i++) {
        var chr = str.charAt(i);
        if (chr >= '0' && chr <= '9') {
            val *= 10;
            val += chr - '0';
        }
    }
    return val;
};
String.prototype.contains = function (str, start) { if (!start) { start = 0; } return this.toString().indexOf(str, start) > -1; };
String.prototype.trim = function (chrs) {
    if (!chrs) { chrs = ' \t'; }
    var i, str = this;
    for (i = 0; i < str.length; i++) { if (!chrs.contains(str.charAt(i))) { break; } }
    if (i > 0) { str = str.substr(i); }
    for (i = str.length - 1; i >= 0; i--) { if (!chrs.contains(str.charAt(i))) { break; } }
    if (i < str.length - 1) { str = str.substr(0, i + 1); }
    return str;
};

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays) {
    var val = c_name + "=" + escape(value) + "; path=/";
    if (expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        val += ";expires=" + exdate.toGMTString();
    }
    document.cookie = val;
}
(function ($) {
    $.WSget = function (url, data, callback, dtype, jcache, ind) {
        return $.WSajax(url, "GET", data, callback, dtype, jcache, ind);
    };

    $.WSpost = function (url, data, callback, dtype, jcache, ind) {
        if($(data).is("form")){
            var d = {};
            $($(data).serializeArray()).each(function(i, o){
                d[o.name] = o.value;
            });
            data = d;
        }
        return $.WSajax(url, "POST", data, callback, dtype, jcache, ind);
    };

    $.WSajax = function (url, type, data, callback, dtype, jcache, ind) {
        if ($.isFunction(data)) {
            callback = data;
            data = null;
        }
        var jxset = $.extend({
            type: type,
            contentType: "application/json; charset=utf-8",
            url: url,
            dataType: dtype || "json",
            jcache: jcache, //false default
            indholder: ind
        }, callback ? ($.isFunction(callback) ? { success: callback} : callback) : {});

        if (type === "GET") {
            jxset.beforeSend = function (xhr, s) { xhr.setRequestHeader("Content-Type", s.contentType); return true; };
            jxset.data = data;
        } else {
            jxset.data = JSON.stringify(data);
        }

        return $.ajax(jxset);
    };
})(jQuery);
var watermark = (function ($) {
    var elArray = null;
    $.fn.watermark = function (opt) {
        if (typeof opt === 'string') { opt = { text: opt }; }
        opt = $.extend({}, $$.globcfg, opt);

        if (elArray === null) {
            elArray = [];
            $("form").submit(function () { $$.clear(); });
        }
        elArray.push({ obj: this, opt: opt });

        $(this).each(function (i, el) {
            el = $(el);

            if (!el.val() || el.val() === opt.text) {
                el.addClass(opt.css).val(opt.text);
            }
            el.focus(function () {
                if ($(this).removeClass(opt.css).val() === opt.text) {
                    $(this).val("");
                }
            }).blur(function () {
                if (!$(this).val()) {
                    $(this).addClass(opt.css).val(opt.text);
                }
            });
        });
        return this;
    };
    var $$ = $.fn.watermark;
    $$.clear = function () {
        if (elArray !== null) {
            $.each(elArray, function (i, o) {
                $(o.obj).each(function (i, el) {
                    el = $(el);
                    if (el.hasClass(o.opt.css) || el.val() === o.opt.text) {
                        el.val("");
                    }
                });
            });
        }
    };

    $$.globcfg = { css: "wtrMark", text: "Enter here..." };

    return $$;
})(jQuery);
(function ($) {
    $.fn.maxlength = function (opt) {
        ///<summary>Shows how many characters left</summary>
        ///<param name="opt" optional="false" type="Object">Options {max:200, onlim: function(){}, cntholder: $("#elem") }</param>
        ///<returns type="Element" />
        if (typeof opt === 'number') { opt = { max: opt }; }
        var def = $.extend({ max: 200, onlim: function () { }, cntholder: null }, opt || {});

        return $(this).each(function (i, o) {
            o = $(o);
            var cnt = $(def.cntholder || "#" + o.attr("id") + "_lim").text(opt.max);
            o.keypress(function (e) {
                var symb = e.charCode > 0 || e.keyCode === 13;
                var left = def.max - o.val().length;
                return !(left <= 0 && symb);
            });
            o.keyup(function (e) {
                var left = def.max - o.val().length;
                if (cnt.length && cnt[0] !== document) { cnt.text(left).css("color", left < 5 ? "red" : ""); }
                if (left < 0) { o.val(o.val().substr(0, def.max)); }
            }).keyup();
            //.iff(":not(.tipinput)", o.keyup);
        });
    };
})(jQuery);
