﻿function loadjscssfile(filename, filetype) {
    if (filetype == "js") { //if filename is a external JavaScript file
        var fileref = document.createElement("script");
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", filename);
    }
    else if (filetype == "css") { //if filename is an external CSS file
        var fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", filename);
    }
    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
} 

function toStringFA(o)
{
    if(o.toString() == '[object Window]') return;
    Str = '{';
    for (var key in o) 
    {
      if(!o.hasOwnProperty || o.hasOwnProperty(key)) 
      { 
        if(o[key] != null )
        if(typeof(o[key]) == 'object')
            Str += "'"+key+ "':"+toStringFA(o[key])+",";
        else
        {
            temp = "'";
            if (typeof (o[key]) == 'boolean' || typeof (o[key]) == 'number') temp = "";
            else if (o[key] != null && typeof (o[key]) == 'string') { o[key] = o[key].replace(/'/gi, "\\'"); }
            Str += "'"+key +"':"+temp+o[key]+temp+",";
        }
      }
    }
    if(Str.length > 1) Str = Str.substring(0,Str.length-1) ;
    return Str+="}";
}
var cache = [];
var queue = 0;
function CheckCache(url, datao) { if (typeof (datao) == 'object') { datao = toStringFA(datao); } return cache[datao]; }

function GoToServer(url, datao, onsuccess, lid, usecache, LoadingUrl) {
    if (usecache == null) usecache = false;
    if (LoadingUrl == null) LoadingUrl = 'http://Img.Tebyan.net/MainParts/Persian/Thesarus/Tcore/loading.gif';
    if (typeof (datao) == 'object') { datao = toStringFA(datao); }
    if (usecache && cache[datao] != null) {  return onsuccess(cache[datao], 'fromcache'); }
    try {
        queue++;
        jQuery.ajax({
            type: 'POST', url: url, contentType: 'application/json; charset=utf-8', dataType: "json", data: datao,
            success: function (d, s) {
                queue--;
                cache[datao] = d;
                onsuccess(d, s);
                if (lid != null) {
                    lid.parent().find('.loadingDiv').fadeOut(1, function () { $(this).remove(); $('.parentDiv1389').css({ "position": "static" }) });
                    lid.parent().css({ position: 'static' });
                }
            },
            beforeSend: function () {
                if (lid != null) {

                    if (!lid.parent().hasClass('parentDiv1389'))
                        lid.wrap('<div class="parentDiv1389" style="position:relative;"></div>');
                    else $('.parentDiv1389').css({ 'position': 'relative' });
                    parentDiv = lid.parent('.parentDiv1389');

                    parentDiv.append('<div class="loadingDiv"></div>');
                    LoadingDiv = parentDiv.find('.loadingDiv');
                    LoadingDiv.css({ 'position': 'absolute', 'top': '0px', 'left': '0px', 'width': lid.width(), 'height': '100%', 'background': '#EEE url(' + LoadingUrl + ') no-repeat center', 'z-index': '100', 'opacity': '0.5' });


                }
            },
            error: function (xmlHttpRequest, status, err) {
                queue--;
                if (lid != null) {
                    lid.parent().find('.loadingDiv').fadeOut(1, function () { $(this).remove(); $('.parentDiv1389').css({ 'position': 'static' }) });
                    lid.parent().css({ position: 'static' });
                }
            }
        });
    }
    catch(e){}
}
function CheckLogin() {
    if ($('#lbtnLogout').is('a'))
    GoToServer("/WebServices/BaseWebService.asmx/Login", '', function (data) {
        if (data.d == false) {
            window.location.reload();
        }
    }, null, false);
    setTimeout('CheckLogin()', 60000);
}
setTimeout('CheckLogin()', 60000);




var Areas = '';
var AncorArray = new Array();
var AncorAreas = new Array();
function ManageHash(_Areas) {
    Areas = _Areas;
    AncorAreas = Areas.split(',');
    document.location.hash = '#' + AncorArray.length;
}
function UpdateHash() {
    if (document.location.hash == '') {
        document.location.reload();
        return;
    }
    var u = document.location.hash.substring(1);
    if (AncorArray[u] != null) {

        var NewArray = new Array();
        for (var p = 0; p < AncorAreas.length; ++p) {
            NewArray[p] = $(AncorAreas[p]).html();
        }
        AncorArray[u] = NewArray;
    }
}
$(window).bind('hashchange', function () {
    if (document.location.hash == '') {
        document.location.reload();
        return;
    }
    var u = document.location.hash.substring(1);
    if (AncorArray[u] == null) {
        var NewArray = new Array();
        for (var p = 0; p < AncorAreas.length; ++p) {
            NewArray[p] = $(AncorAreas[p]).html();
        }
        AncorArray[u] = NewArray;
    }
    else {
        for (var p = 0; p < AncorArray[u].length; ++p) {
            $(AncorAreas[p]).html(AncorArray[u][p]);
        }
    }
});
