﻿function format(str) {
    for (i = 1; i < arguments.length; i++) {
        str = str.replace("{" + (i - 1) + "}", arguments[i]);
    }
    return str;
}

function getQueryString(name) {
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)", "ig");
    var qs = regex.exec(window.location.href);
    if (qs)
        return qs[1];
    else
        return '';
}


function getQueryString(name, url) {
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)", "ig");
    var qs = regex.exec(url);
    if (qs)
        return qs[1];
    else
        return '';
}

function fulltrim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}


//////////////////////////////////
//function trim(str) 
//{
//    return str.replace(/^\s+|\s+$/g,"");
//}
////////////////////////////////////////////////////////////////////////

function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
//addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
//addLoadEvent(function() {});

function findelement(element) {
    if (typeof element == "string")
        element = document.getElementById(element);

    return element;
}

function GetKeyPressed(e) {
    var keycode;

    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else
        keycode = -1;

    return keycode;
}

function refreshPage() {
    var s = window.location.href;
    window.location.href = "about:blank";
    window.location.href = s;
}

function div_display(objid, left, top) {
    document.getElementById(objid).style.display = "block";
    document.getElementById(objid).style.top = top;
    document.getElementById(objid).style.left = left;

}

function display(objid) {
    document.getElementById(objid).style.display = "block";
}
function undisplay(objid) {
    document.getElementById(objid).style.display = "none";
}
//  dialog
function showDialog(divId, url, modal, title, width, height, callOK, okText) {
    //modal=modal||true;
    var dialog = new jt_DialogBox(modal, { okButton: true, titleOpacity: 100, okButtonText: okText });
    var obj = findelement(divId);

    var width = width || 400;
    var height = height || 300;

    var title = title || "&nbsp;";
    dialog.setTitle(title);

    if (url) {
        dialog.setUrl(url, height, width);
    }
    else {
        dialog.setContent(obj.innerHTML);
        dialog.setHeight(height);
    }

    if (callOK) {
        dialog.setCallOK(callOK);
    }

    var d = getWinDims();
    var x = (d.width - width) / 2;
    var y = (d.height - height) / 2 + getScrollXY().YOffset - 50;

    dialog.setWidth(width);
    dialog.moveTo(x, y);
    dialog.show();
}






function showDialogPos(divId, url, modal, title, width, height, Xpos, Ypos, callOK, okText) {
    //modal=modal||true;
    var dialog = new jt_DialogBox(modal, { okButton: true, titleOpacity: 100, okButtonText: okText });
    var obj = findelement(divId);

    var width = width || 400;
    var height = height || 300;

    var title = title || "&nbsp;";
    dialog.setTitle(title);

    if (url) {
        dialog.setUrl(url, height, width);
    }
    else {
        dialog.setContent(obj.innerHTML);
        dialog.setHeight(height);
    }

    if (callOK) {
        dialog.setCallOK(callOK);
    }

    //var d = getWinDims();
    var x = Xpos;
    var y = Ypos;

    dialog.setWidth(width);
    dialog.moveTo(x, y);
    dialog.show();
}

function getWinDims() {
    var myWidth = 0, myHeight = 0;
    if (typeof window.innerWidth == "number") {
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return { width: myWidth, height: myHeight };
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    }
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    }
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }

    return { XOffset: scrOfX, YOffset: scrOfY };
}

// *********************
// ***** RadWindow *****
// *********************
function GetRadWindow() {
    var win = null;
    if (window.radWindow) {
        win = window.radWindow;
    }
    else if (window.frameElement.radWindow) {
        win = window.frameElement.radWindow;
    }
    return win;
}

function OpenRadWindow(url, title, width, height, isCentered, isModal, name) {
    var win;
    if (name)
        win = window.radopen(url, name);
    else
        win = window.radopen(url);

    win.set_title(title);
    win.setSize(width, height);

    if (isCentered)
        win.center();

    if (isModal)
        win.set_modal(true);
}

function CloseRadWindow() {
    var win = GetRadWindow();
    win.close();
}
/*********************/


//wrap text in div tag
function wrapText(msg_content, MAX_WIDTH, divid) {
    var final_msg = "";
    var this_line = "";

    for (i = 0; i < msg_content.length; i++) {
        document.getElementById(divid).innerHTML = document.getElementById(divid).innerHTML + msg_content.charAt(i);
        if (parseInt(document.getElementById(divid).offsetWidth) < MAX_WIDTH) {
            this_line = this_line + msg_content.charAt(i);
        } else {
            final_msg = final_msg + this_line + " ";
            this_line = msg_content.charAt(i);
            document.getElementById(divid).innerHTML = msg_content.charAt(i);
        }
    }
    if (this_line != "") final_msg = final_msg + this_line + " ";
    document.getElementById(divid).innerHTML = "";
    return final_msg;
}

/****************/
function Telavat(img) { foto1 = new Image(); foto1.src = (img); Controlla(img); } function Controlla(img) { if ((foto1.width != 0) && (foto1.height != 0)) { viewFoto(img); } else { funzione = "Controlla('" + img + "')"; intervallo = setTimeout(funzione, 20); } } function viewFoto(img) { largh = foto1.width + 20; altez = foto1.height + 20; stringa = "width=" + largh + ",height=" + altez; finestra = window.open(img, "", stringa); }
/****************/

/********* jqdialog *********/

function OpenWindow(url, title, width, height, modal) {
    var d;

    if (height == 0)
        d = $('#dialog').html('<iframe id="ifrm" onload="Set_IFRAME_Height();" frameborder="0"  style="width: 100%;"></iframe>');
    else
        d = $('#dialog').html('<iframe id="ifrm" frameborder="0"   style="width: 100%; height:100%;"></iframe>');

    d.dialog({
        dialogClass: 'rtl',
        position: 'center',
        closeOnEscape: true,
        draggable: true,
        resizable: true,
        width: width,
        modal: modal,
        title: title
    });

    if (height != 0) {
        d.dialog({ height: height + 15, position: 'center' });
    }

    $("#dialog>#ifrm").attr("src", url);

    return d;
}

function CloseWindow() {
    var d = window.parent.$("#dialog");
    d.dialog('close');
}

function CloseWindowAndCallBackParent(args) {
    window.parent.$("#dialog").dialog('close');
    window.parent.CallBackFunction(args);
}

function Set_IFRAME_Height() {
    var iframe = document.getElementById("ifrm");

    if (iframe.contentDocument) {
        iframe.height = iframe.contentDocument.body.offsetHeight;  //+ 35;
    }
    else {
        iframe.height = iframe.contentWindow.document.body.scrollHeight;
    }

    window.parent.$("#dialog").dialog("option", "position", "center");
}


function ReloadPage(url) {
    var href;
    if (url)
        href = url;
    else
        href = location.href;

    location.href = "about:blank"
    location.href = href;
}

function CheckInt(val) {
    if (val.match(/^\d+$/)) {
        return true;
    }
    return false;
}

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }
    return randomstring;
}


function ConfirmDelete(fieldId) {

    if (fieldId != 0) {
        var message = format("آيا واقعا مي خواهيد فيلد كد {0} را حذف كنيد؟", fieldId);
    }
    else {
        var message = "آيا مطمئن هستيد كه حذف شود؟";
    }
    var reply = confirm(message);
    return reply;
}


function ConfirmDelete(name, fieldId) {


    if (fieldId != 0) {
        var message = format("آيا واقعا مي خواهيد {1} '{0}' را حذف كنيد؟", fieldId, name);
    }
    var reply = confirm(message);
    return reply;
}

// admin


function $(element) {
    if (typeof element == "string")
        element = document.getElementById(element);

    return element;
}

function TryParseInt(str, defaultValue) {
    return /^\d+$/.test(str) ? parseInt(str) : defaultValue;
}


function AddIframe(height, url) {
    var d;
    if (height == 0)
        d = '<iframe id="ifrm" src="' + url + '"  frameborder="0"    style="width: 100%;"></iframe>';
    else
        d = '<iframe id="ifrm" src="' + url + '" frameborder="0"  style="width: 100%; height:100%;"></iframe>';
    return d;
}


function checkfirefox() {
    if ($.browser.mozilla) {
        return true;
    }
    else {
        return false;
    }
}

