﻿var FormIds = [];
var AjaxError = function(x, e) {
    if (x.status == 0) {
        alert('You are offline!!\n Please Check Your Network.');
    } else if (x.status == 404) {
        alert('Requested URL not found.');
    } else if (x.status == 500) {
        alert('Internel Server Error.');
    } else if (e == 'parsererror') {
        alert('Error.\nParsing JSON Request failed.');
    } else if (e == 'timeout') {
        alert('Request Time out.');
    } else {
        alert('Unknow Error.\n' + x.responseText);
    }
};
var getElementPosition = function(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return { left: curleft, top: curtop };
}
var UrlInfo = function(url) {
    var index = url.indexOf('?', 0);
    var result = [];
    if (index >= 0) {
        index++;
        var tmp = url.substring(index, url.length);
        tmp = tmp.split(/&/g);
        for (var i = 0; i < tmp.length; i++) {
            tmp[i] = tmp[i].split(/=/);
            result[tmp[i][0]] = tmp[i][1];
        };
    };
    return result;
};
var LookupAC = function(id, control, field, sort, onData, onAjax, searchField) {
    $(document).ready(function() {
        if ($("#" + id + "_AutoComplete").length == 0) {
            $('body').append('<div class="LookupAutoComplete" id="' + id + '_AutoComplete"></div>');
            $("#" + id + "_AutoComplete").bind("mouseover", function() {
                LookupAC.CancelHideWindow(id);
            });
            $("#" + id + "_AutoComplete").bind("mouseout", function() {
                LookupAC.HideWindow(id);
            });
        };

        var $el = $("#" + id);
        $el.data("timer", -1);
        $el.data("control", control);
        $el.data("field", field);
        $el.data("sort", sort);
        $el.data("fnc", { onData: onData, onAjax: onAjax });
        $el.data("search", searchField);
        $el.bind("keydown", function(e) {
            var key = (e.keyCode) ? e.keyCode : e.which;
            if (key == 38 || key == 40 || key == 13) {
                var show = $("#" + id + "_AutoComplete");
                if (show.data("show")) {
                    var CIndex = $el.data("activeIndex");
                    show.find("tr.selected").removeClass();
                    if (key == 38 || key == 40) {
                        var CLength = show.find("table tbody tr").length;
                        var downMode = (key == 40);
                        if (CLength > 0) {
                            if (downMode) {
                                if (CIndex < (CLength - 1)) {
                                    CIndex++;
                                } else {
                                    CIndex = 0;
                                };
                            } else {
                                if (CIndex > 0) {
                                    CIndex--;
                                } else {
                                    CIndex = (CLength - 1);
                                };
                            };
                            show.find("table tbody tr:eq(" + CIndex + ")").addClass("selected");
                            $el.data("activeIndex", CIndex);
                        };
                    } else if (key == 13) {
                        e.returnValue = false;
                        e.cancel = true;
                        if (e.preventDefault)
                            e.preventDefault();
                        if (e.stopPropagation)
                            e.stopPropagation();
                        if (CIndex >= 0) {
                            LookupAC.HideWindow(id);
                            var nid = id.split(/_/);
                            var fnc = $el.data("fnc");
                            var dataAttr = show.find("table tbody tr:eq(" + CIndex + ")").attr("data");
                            dataAttr = eval(dataAttr);
                            if (dataAttr.ids.length >= 2) {
                                $("#" + nid[0]).val(dataAttr.ids[0].value);
                                $("#" + id).val(dataAttr.ids[1].value);
                            }
                            var tmpData = [];
                            for (var i = 0; i < dataAttr.ids.length; i++) {
                                tmpData[dataAttr.ids[i].key] = dataAttr.ids[i].value;
                            };
                            if ($el.data("search")) {
                                $("#" + nid[0] + "_lookup").val(tmpData[$el.data("search")]);
                            }
                            if (fnc.onData) {
                                fnc.onData(field, tmpData);
                            }
                        }
                    };
                };
            } else {
                var timerData = $(this).data("timer");
                window.clearTimeout(timerData);
                $(this).data("timer", window.setTimeout("LookupAC.Show('" + id + "')", 1000));
            };
        }).bind("blur", function() {
            var timerData = $(this).data("timer");
            window.clearTimeout(timerData);
            LookupAC.HideWindow(id);
        }).bind("focus", function() {
            LookupAC.CancelHideWindow(id);
        });
    });
}
LookupAC.HideWindow = function(id) {
    $("#" + id + "_AutoComplete").data("timer", window.setTimeout('LookupAC.Hide("' + id + '")', 1000));
}
LookupAC.Hide = function(id) {
    $("#" + id + "_AutoComplete").hide("normal", function() {
        $(this).data("show", false);
    });
}
LookupAC.CancelHideWindow = function(id) {
    window.clearTimeout($("#" + id + "_AutoComplete").data("timer"));
}
LookupAC.Show = function(id) {
    var $obj = $("#" + id);
    $obj.data("activeIndex", -1);
    var offset = getElementPosition(document.getElementById(id));
    var width = $obj.width();
    var height = $obj.height();
    //Load and Clear Data
    ////////////////////
    if ($obj.val()) {
        var f = $obj.data("field") + "$" + $obj.data("sort") + "$8$" + $obj.val() + "$0$";
        if (!$obj.attr("disabled")) {
            var data = {
                Filter: f,
                Sort: '',
                PageIndex: 0,
                PageSize: 5
            };
            var fnc = $obj.data("fnc");
            if (fnc.onAjax) {
                fnc.onAjax(data);
            }
            $.ajax({
                url: "/" + $obj.data("control") + '/AjaxLookup/',
                dataType: 'json',
                type: 'POST',
                data: data,
                cache: false,
                success: function(result) {
                    var BoxWidth = $("#" + id + "_AutoComplete").width();
                    $("#" + id + "_AutoComplete").css({
                        'top': (offset.top + height + 5) + 'px',
                        'left': (offset.left - BoxWidth + width + 5) + 'px'
                    }).show('normal', function() {
                        $(this).data("show", true);
                    }).html("");
                    var nid = id.split(/_/);
                    if (result.TotalRecords > 0) {
                        var html = '<table class="dsPager-grid"><thead><tr>';
                        result.Columns = eval("(" + result.Columns + ")");
                        for (var col in result.Columns) {
                            html += '<th>' + result.Columns[col].name + '</th>';
                        };
                        html += '</tr></thead><tbody>' + result.Html + '</tbody></table>';
                        $("#" + id + "_AutoComplete").html(html);
                        $("#" + id + "_AutoComplete tbody tr").bind("click", function() {
                            var dataAttr = $(this).attr("data");
                            dataAttr = eval(dataAttr);
                            if (dataAttr.ids.length >= 2) {
                                $("#" + nid[0]).val(dataAttr.ids[0].value);
                                $("#" + id).val(dataAttr.ids[1].value);
                            }
                            var tmpData = [];
                            for (var i = 0; i < dataAttr.ids.length; i++) {
                                tmpData[dataAttr.ids[i].key] = dataAttr.ids[i].value;
                            };
                            if ($obj.data("search")) {
                                $("#" + nid[0] + "_lookup").val(tmpData[$obj.data("search")]);
                            }
                            if (fnc.onData) {
                                fnc.onData(field, tmpData);
                            }
                        });
                    } else {
                        $("#" + id + "_AutoComplete").html('<p>نتیجه ای یافت نشد</p>');
                        $("#" + nid[0]).val("");
                        $("#" + id).val("");
                        if ($obj.data("search")) {
                            $("#" + nid[0] + "_lookup").val("");
                        }
                    };
                },
                error: AjaxError
            });
        };
    };
}
var openLookup = function(src, width, height, onOpen) {
    var field = UrlInfo(src);
    field = field["fid"];
    if (!$("#" + field).attr("disabled")) {
        var htmlcode = $("#lookupwin").length;
        if (htmlcode == 0) {
            $(document.body).append('<div style="text-align:center" id="lookupwin"><iframe id="lookupframe" width="' + width + 'px" height="' + height + 'px" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto"></iframe></div>');
            $("#lookupwin").dialog({
                autoOpen: false,
                modal: false,
                width: width + 15,
                height: height + 45,
                close: function() {
                    $("#lookupframe").attr("src", "about:blank");
                },
                resizable: true,
                resizeStop: function() {
                    var w = $("#lookupwin").dialog('option', 'width');
                    var h = $("#lookupwin").dialog('option', 'height');
                    $('#lookupframe').attr('width', (w - 15) + 'px').attr('height', (h - 45) + 'px');
                }
            });
        };
        if (onOpen) {
            var data = onOpen();
            var str = [];
            for (var key in data) {
                str[str.length] = key + '=' + data[key];
            };
            src += '&' + str.join('&');
        };
        $("#lookupframe").attr("src", src);
        $("#lookupwin").dialog('open');
    };
};
var openFieldLookup = function(control, field, sort, el, onData, onAjax) {
    if (el.value) {
        var f = field + "$" + sort + "$0$" + el.value + "$0$";
        var id = el.id.split(/_/);
        id = id[0];
        if (!$("#" + id).attr("disabled")) {
            var data = {
                Filter: f,
                Sort: '',
                PageIndex: 0,
                PageSize: 5
            };
            $("#" + id + "_img").show();
            if (onAjax) {
                onAjax(data);
            };
            $.ajax({
                url: "/" + control + '/AjaxLookup/',
                dataType: 'json',
                type: 'POST',
                data: data,
                cache: false,
                success: function(result) {
                    if (result.TotalRecords == 0) {
                        el.value = '';
                        $("#" + id).val("");
                        $("#" + id + "_text").val("");
                    } else if (result.TotalRecords == 1) {
                        var dataAttr = $(result.Html).attr("data");
                        dataAttr = eval(dataAttr);
                        if (dataAttr.ids.length >= 2) {
                            $("#" + id).val(dataAttr.ids[0].value);
                            $("#" + id + "_text").val(dataAttr.ids[1].value);
                            if (onData) {
                                var tmpData = [];
                                for (var i = 0; i < dataAttr.ids.length; i++) {
                                    tmpData[dataAttr.ids[i].key] = dataAttr.ids[i].value;
                                };
                                onData(field, tmpData);
                            }
                        } else {
                            $("#" + id).val("");
                            $("#" + id + "_text").val("");
                        };
                    } else {
                        alert('تعداد موارد پیدا شده ' + result.TotalRecors);
                    };
                },
                error: AjaxError,
                complete: function() {
                    $("#" + id + "_img").hide();
                }
            });
        };
    };
};
var closeLookup = function(f, fn, ids) {
    $("#lookupwin").dialog('close');
    if (fn) {
        try {
            fn2 = eval(fn);
            if (fn2) {
                fn2(f, ids);
            };
        } catch (ex) { };
    };
};
var openTreeLookup = function(src, width, height, onOpen) {
    var field = UrlInfo(src);
    field = field["fid"];
    if (!$("#" + field).attr("disabled")) {
        var htmlcode = $("#treelookupwin").length;
        if (htmlcode == 0) {
            $(document.body).append('<div style="text-align:center" id="treelookupwin"><iframe id="treelookupframe" width="' + width + 'px" height="' + height + 'px" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto"></iframe></div>');
            $("#treelookupwin").dialog({
                autoOpen: false,
                modal: false,
                width: width + 15,
                height: height + 45,
                close: function() {
                    $("#treelookupframe").attr("src", "about:blank");
                },
                resizable: true,
                resizeStop: function() {
                    var w = $("#treelookupwin").dialog('option', 'width');
                    var h = $("#treelookupwin").dialog('option', 'height');
                    $('#treelookupframe').attr('width', (w - 15) + 'px').attr('height', (h - 45) + 'px');
                }
            });
        };
        if (onOpen) {
            var data = onOpen();
            var str = [];
            for (var key in data) {
                str[str.length] = key + '=' + data[key];
            };
            src += '&' + str.join('&');
        };
        $("#treelookupframe").attr("src", src);
        $("#treelookupwin").dialog('open');
    };
};
var closeTreeLookup = function(f, fn, ids) {
    $("#treelookupwin").dialog('close');
    if (fn) {
        try {
            fn2 = eval(fn);
            if (fn2) {
                fn2(f, ids);
            };
        } catch (ex) { };
    };
};
var closeMultiSelect = function(f, fn, ids) {
    $("#lookupwin").dialog('close');
    if (fn) {
        try {
            fn2 = eval(fn);
            if (fn2) {
                var index = 0;
                var keylist = [];
                for (var key in ids) {
                    keylist[index] = key;
                    index++;
                    if (index == 2) {
                        break;
                    };
                };
                for (index = 0; index < ids[keylist[0]].length; index++) {
                    fn2.add(ids[keylist[1]][index], ids[keylist[0]][index]);
                };
            };
        } catch (ex) { };
    };
};
var onClear = null;
var clearLookup = function(name) {

    if (!$("#" + name).attr("disabled")) {
        $("#" + name + "_text").val("");
        $("#" + name + "_lookup").val("");
        $("input[name='" + name + "']").val("");
        if (onClear) {
            onClear(name);
        };
    };
};
var openZoneLookup = function(src, width, height) {
    var htmlcode = $("#zonelookupwin").length;
    if (htmlcode == 0) {
        $(document.body).append('<div id="zonelookupwin"><iframe id="zonelookupframe" width="' + width + 'px" height="' + height + 'px" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto"></iframe></div>');
        $("#zonelookupwin").dialog({
            autoOpen: false,
            modal: true,
            title: 'انتخاب منطقه جغرافیایی',
            width: width + 20,
            height: height + 40,
            close: function() {
                $("#zonelookupframe").attr("src", "about:blank");
            },
            resizable: false
        });
    };
    $("#zonelookupframe").attr("src", src);
    $("#zonelookupwin").dialog('open');
};
var closeZoneLookup = function() {
    $("#zonelookupwin").dialog('close');
};
(function() {
    var pageUrl = "";
    var pageName = "";
    var pageSite = "";
    var pageQuerystring = function() {
        var url = window.location.href;
        pageSite = window.location.host;
        pageUrl = window.location.pathname;
        var index = url.indexOf('?', 0);
        var result = [];
        if (index >= 0) {
            index++;
            var tmp = url.substring(index, url.length);
            tmp = tmp.split(/&/g);
            for (var i = 0; i < tmp.length; i++) {
                tmp[i] = tmp[i].split(/=/);
                result[tmp[i][0]] = tmp[i][1];
            };
        };
        pageName = pageUrl.split(/\//g);
        if (pageName.length == 1)
            pageName = pageName[0];
        else
            pageName = pageName[(pageName.length - 1)];
        return result;
    };
    window.page = {
        query: pageQuerystring(),
        name: pageName,
        site: pageSite,
        path: pageUrl
    };
})();
var ChangeFilePicker = function(id, val) {
    switch (val) {
        case "File":
            $("#" + id + "_chk_File").show();
            $("#" + id + "_chk_Text").hide();
            $("#" + id).val("");
            $("#" + id + "_Link").attr("href", "");
            break;
        case "Text":
            $("#" + id + "_chk_File").html($("#" + id + "_chk_File").html()).hide();
            $("#" + id + "_File").val("");
            $("#" + id + "_chk_Text").show();
            break;
    };
};
var openFilePicker = function(src, width, height) {
    var htmlcode = $("#fphtmlwin").length;
    if (htmlcode == 0) {
        $(document.body).append('<div style="text-align:center" id="fphtmlwin"><iframe id="fphtmlframe" width="' + width + 'px" height="' + height + 'px" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto"></iframe></div>');
        $("#fphtmlwin").dialog({
            autoOpen: false,
            modal: true,
            title: 'انتخاب فایل / عکس',
            width: width + 5,
            height: height + 35,
            close: function() {
                $("#fphtmlframe").attr("src", "about:blank");
            },
            resizable: false
        });
    };
    $("#fphtmlframe").attr("src", src);
    $("#fphtmlwin").dialog('open');
};
var closeFilePicker = function(url, fid) {
    $("#" + fid).val(url);
    $("#" + fid + "_Link").attr("href", "/" + url);
    $("#fphtmlwin").dialog('close');
};
function SaveStyle(Styleid) {
    $.cookie('styleName', Styleid, { expires: 7, path: '/' });
}
function getCookie(name) {
    return $.cookie('styleName');
}

