﻿(function () {
    var Mobawaba = window.Mobawaba = function () {
        return new Mobawaba.prototype.init();
    };

    if (!window.Mobawaba) {
        window.Mobawaba = Mobawaba;
    }

    Mobawaba.proto = Mobawaba.prototype = {
        init: function () {
            return new Mobawaba();
        }
    }
    Mobawaba.AppPath = null;
    Mobawaba.getQSValue = function (q) {
        if (!qsObj) { loadCurrentQS(); }
        return qsObj[q];
    }
    Mobawaba.SetTextAutoComplete = function (text) {
        text = text.replace("+", "\\+");
        text = text.replace("*", "\\*");
        return text;
    }
    Mobawaba.setCookie = function (name, value, expires, path, domain, secure) {
        var today = new Date();
        today.setTime(today.getTime());

        if (expires) {
            expires = expires * 1000 * 60 * 60 * 24;
        }
        var expires_date = new Date(today.getTime() + (expires));

        document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires_date.toGMTString() : "") +
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		((secure) ? ";secure" : "");
    }
    Mobawaba.getCookie = function (name) {
        var dc = document.cookie;
        if (dc.indexOf(name) != -1 && dc.substr(dc.indexOf(name) + name.length, 1) == '=') {
            if (dc.indexOf(";", dc.indexOf(name)) != -1) {
                var EndOfCookie = dc.indexOf(";", dc.indexOf(name));
                var CookieName = dc.indexOf(name) + name.length + 1;
                var CookieValue = EndOfCookie - CookieName;
                return dc.substr(dc.indexOf(name) + name.length + 1, CookieValue)
            }
            else {
                return dc.substr(dc.indexOf(name) + name.length + 1, dc.length)
            }
        }
    }
    Mobawaba.startLoader = function (divName) {
        var loader = $("#loader" + divName)[0];
        var div = $("#" + divName);
        if (!loader) {
            loader = document.createElement("div");
            loader.id = "loader" + divName;
            loader.className = "loader";
            loader.innerHTML = "<img src='" + Mobawaba.AppPath + "images/tloading.gif' class='imgloader' />";
            div.before(loader);
        }
        loader.style.width = div[0].offsetWidth + "px";
        loader.style.height = parseInt(div[0].offsetHeight) + "px";
        $(loader).show();
    }
    Mobawaba.inputOut = function (fld, value) {
        if (fld.value == '') {
            fld.value = value;
        }
    }
    Mobawaba.inputIn = function (fld, value) {
        if (fld.value == value) {
            fld.value = '';
        };
    }
    Mobawaba.imposeMaxLength = function (Object, MaxLen) {
        if (Object.value.length > MaxLen) {
            Object.value = Object.value.substring(0, MaxLen);
            return false;
        }
        else
            return true;
    }
    Mobawaba.showPrompt = function (title, divname, width, height, opacityValue, data) {
        if (data != null) {
            $("#" + divname).html(data);
        }
        $("#" + divname).parent().dialog({
            height: height,
            width: width,
            modal: true,
            draggable: false,
            resizable: false,
            title: title,
            overlay: { opacity: opacityValue, background: "#978a9d" }
        });
    }
    var qsObj;
    function loadCurrentQS() {
        qsObj = new Object()
        var url = location.href;
        if (url.indexOf("?") > -1) {
            var strParams = url.split("?")[1].split("&");
            for (var i = 0; i < strParams.length; i++) {
                var qsParam = strParams[i].split("=");
                qsObj[qsParam[0]] = qsParam[1];
            }
        }
    }
    Mobawaba.validation = {
        isEmpty: function (txt) {
            if (txt.value.replace(/ /g, '') == '') {
                txt.style.borderColor = 'red';
                return true;
            }
            else {
                txt.style.borderColor = 'green';
                return false;
            }
        },
        istxtSearchEmpty: function (txt, defaultvalue) {
            // alert(defaultvalue);
            var txtSearchval = txt.value.replace(defaultvalue, '').replace(/ /g, '');
            if (txtSearchval == '') {
                txt.style.borderColor = 'red';
                return true;
            }
            else {
                txt.style.borderColor = 'green';
                return false;
            }
        },
        IsCharDigitsOnly: function (txt) {
            var filter = /[^أ-يa-zA-Z0-9 آء]+/g;
            if (txt.value.search(filter) == -1) {
                txt.style.borderColor = 'green';
                return true;
            }
            else {
                txt.style.borderColor = 'red';
                return false;
            }
        },
        IsCharDigitsOnlyWithDefaultValue: function (txt, defaultValue) {
            var filter = /[^أ-يa-zA-Z0-9 آء]+/g;
            var txtVal = txt.value.replace(defaultValue, '').replace(/ /g, '');
            if (txt.value.search(filter) == -1) {
                if (txtVal != "") {
                    txt.style.borderColor = 'green';
                }
                return true;
            }
            else {
                if (txtVal != "") {
                    txt.style.borderColor = 'red';
                }
                return false;
            }
        },
        isCharLessThan: function (txt, num) {
            if (txt.value.replace(/ /g, '').length > 0 && txt.value.replace(/ /g, '').length < num) {
                txt.style.borderColor = 'red';
                return true;
            }
            else {
                txt.style.borderColor = 'green';
                return false;
            }
        },

        isChecked: function (chk) {
            if (!chk.checked) {
                chk.style.borderColor = 'red';
                return true;
            }
            else {
                chk.style.borderColor = 'green';
                return false;
            }
        },
        isTooLongWord: function (txt) {
            var words = txt.value.split(" ");
            var word = 0;
            var islong = false;
            while (word < words.length) {
                if (words[word].length > 20) {
                    islong = true;
                }
                word += 1;
            }
            if (islong) {
                txt.style.borderColor = 'red';
                return true;
            }
            else {
                txt.style.borderColor = 'green';
                return false;
            }
        },
        isTooLongWordWithDefaultValue: function (txt, defaultValue) {
            var words = txt.value.split(" ");
            var txtVal = txt.value.replace(defaultValue, '').replace(/ /g, '');
            var word = 0;
            var islong = false;
            while (word < words.length) {
                if (words[word].length > 20) {
                    islong = true;
                }
                word += 1;
            }
            if (islong) {
                if (txtVal != "") {
                    txt.style.borderColor = 'red';
                }
                return true;
            }
            else {
                if (txtVal != "") {
                    txt.style.borderColor = 'green';
                }
                return false;
            }
        },
        isSelected: function (sel) {
            if (sel.selectedIndex > 0) {
                sel.style.borderColor = 'green';
                return true;
            }
            else {
                sel.style.borderColor = 'red';
                return false;
            }
        },
        isItemChecked: function (radioButtons) {
            var isvalid = false;
            for (var x = 0; x < radioButtons.length; x++) {
                if (radioButtons[x].checked) {
                    isvalid = true
                }
            }
            if (isvalid) {
                document.getElementById(radioButtons[0].id).style.borderColor = 'green';
                return true;
            }
            else {
                document.getElementById(radioButtons[0].id).style.borderColor = 'red';
                return false;
            }
        },
        isNumeric: function (txt) {
            var num = txt.value;
            num = num * 1;
            if (num == num) {
                txt.style.borderColor = 'green';
                return true;
            }
            else {
                txt.style.borderColor = 'red';
                return false;
            };
        },
        isNumericValue: function (num) {
            num = num * 1;
            if (num == num) {
                return true;
            }
            else {
                return false;
            };
        },
        isZeroValue: function (txt) {
            var num = txt.value;
            if (num + 1 == 1) {
                txt.style.borderColor = 'red';
                return true;
            }
            else {
                txt.style.borderColor = 'green';
                return false;
            };
        }
		,
        IsCharAndDigitsOnly: function (txt) {
            var filter = /[!@#$%^&*_~=+]+/g;
            //var filter = /[^ء-ىa-zA-Z 0-9]+/g;
            //var filter = /[^أ-يa-zA-Z0-9 آء]+/g;
            if (txt.value.search(filter) == -1) {
                txt.style.borderColor = 'green';
                return true;
            }
            else {
                txt.style.borderColor = 'red';
                return false;
            }
        },
        isValidURL: function (url) {
            //var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
            var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
            if (RegExp.test(url)) {
                return true;
            } else {
                return false;
            }
        }
		,
        validateEmails: function (field) {
            var delimiter = ',';
            var filter = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+>?$/;
            var error = true;

            // Create an array by splitting the field along the delimiter
            var aEmails = field.value.split(';');

            // For each of the emails
            for (index = 0; index < aEmails.length; index++) {
                // Trim spaces from the ends
                aEmails[index] = (aEmails[index].replace(/^\s+/, '')).replace(/\s+$/, '');
                // Check whether an email is present
                if (aEmails[index] != '' && aEmails[index].search(filter) == -1)
                    error = false;
            }
            if (error == true) {
                field.style.borderColor = 'green';
                return true;
            }
            else {
                field.style.borderColor = 'red';
                return false;
            }
        }
    }



    String.prototype.format = function () {
        var str = this;
        for (var i = 0; i < arguments.length; i++) {
            var re = new RegExp('\\{' + (i) + '\\}', 'gm');
            str = str.replace(re, arguments[i]);
        }
        return str;
    }



})();






