﻿function FP_preloadImgs() {//v1.0
    var d = document, a = arguments; if (!d.FP_imgs) d.FP_imgs = new Array();
    for (var i = 0; i < a.length; i++) { d.FP_imgs[i] = new Image; d.FP_imgs[i].src = a[i]; }
}

function FP_swapImg() {//v1.0
    var doc = document, args = arguments, elm, n; doc.$imgSwaps = new Array(); for (n = 2; n < args.length;
 n += 2) {
        elm = FP_getObjectByID(args[n]); if (elm) {
            doc.$imgSwaps[doc.$imgSwaps.length] = elm;
            elm.$src = elm.src; elm.src = args[n + 1];
        } 
    }
}

function FP_getObjectByID(id, o) {//v1.0
    var c, el, els, f, m, n; if (!o) o = document; if (o.getElementById) el = o.getElementById(id);
    else if (o.layers) c = o.layers; else if (o.all) el = o.all[id]; if (el) return el;
    if (o.id == id || o.name == id) return o; if (o.childNodes) c = o.childNodes; if (c)
        for (n = 0; n < c.length; n++) { el = FP_getObjectByID(id, c[n]); if (el) return el; }
    f = o.forms; if (f) for (n = 0; n < f.length; n++) {
        els = f[n].elements;
        for (m = 0; m < els.length; m++) { el = FP_getObjectByID(id, els[n]); if (el) return el; } 
    }
    return null;
}

function PreloadDefault() {
    FP_preloadImgs('App_Themes/Blue/Image/Banner/PicBanner/B/1.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/B/2.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/B/3.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/B/4.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/B/5.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/B/6.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/B/7.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/B/8.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/O/1.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/O/2.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/O/3.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/O/4.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/O/5.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/O/6.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/O/7.gif',
    'App_Themes/Blue/Image/Banner/PicBanner/O/8.gif')
}
////////////////////////////////////////////////////////////////////////////////
function ItemDeleteConfirm(type) {
    type = type == null ? "مورد" : type;
    return retVal = self.confirm("آیا مطمئن هستید که می خواهید این " + type + " را حذف کنید؟");
}
function ItemSendConfirm(type) {
    type = type == null ? "مورد" : type;
    return retVal = self.confirm(" آيا از" + type + " مطمئن هستيد؟");
}
function CheckSelection(sender, args) {
    var ddl = document.getElementById(sender.controltovalidate);

    if (ddl.value == '0') {

        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
}
function ValidatingShamsiDate(sender, args) {
    var sDate = args.Value;
    var dateIsValid = false;
    var splitdate = sDate.split("/");
    if (splitdate[0] >= 1300 && splitdate[0] <= 1400) {
        switch (splitdate[1]) {
            case "01":
            case "02":
            case "03":
            case "04":
            case "05":
            case "06":
                if (splitdate[2] >= 1 && splitdate[2] <= 31)
                    dateIsValid = true;
                break;
            case "07":
            case "08":
            case "09":
            case "10":
            case "11":
                if (splitdate[2] >= 1 && splitdate[2] <= 30)
                    dateIsValid = true;
                break;
            case "12":
                var rem = splitdate[0] % 33;
                if (rem == 1 || rem == 5 || rem == 9 || rem == 13 || rem == 17 || rem == 22 || rem == 26 || rem == 30) {
                    if (splitdate[2] >= 1 && splitdate[2] <= 30)
                        dateIsValid = true;
                }
                else {
                    if (splitdate[2] >= 1 && splitdate[2] <= 29)
                        dateIsValid = true;
                }


                break;
        }
    }
    args.IsValid = dateIsValid;
}

function openWinPicGalery(elementHiden, exten ) {
    var ret_val;
    var x = (screen.availHeight / 2) - 300;
    var y = 100; //(screen.availWidth / 2) - 300;
    
    if (exten == '1') // 1== pic
    //show the modal window with the correct file type...
        ret_val = window.showModalDialog("PickGallery.aspx?MNV=1", "", "resizable:yes;help:no;edge:sunken;dialogWidth:900px;dialogHeight:650px,screenX=" + x + ",screenY=" + y)
    else if (exten == '2') // 2== File
        ret_val = window.showModalDialog("PickFileGallery.aspx", "", "resizable:yes;help:no;scroll:no;edge:sunken;dialogWidth:900px;dialogHeight:650px,screenX=" + x + ",screenY=" + y)
    if (ret_val == null) {
        elementHiden.value = '';
    }
    else {
        //put the relative path of the chosen file in the text box that was passed into the function....
        elementHiden.value = ret_val;
    }
}

function postbackOnCheck(e) {
    var isNav = (window.navigator.appName.toLowerCase().indexOf("netscape") >= 0);
    var o;
    if (isNav && e != null)
        o = e.target;
    else
        o = window.event.srcElement;
    if (o.tagName == 'INPUT' && o.type == 'checkbox'
            && o.name != null && o.name.indexOf('CheckBox') > -1)
        __doPostBack("", "");
}

// Check Tree View Nodes
function client_OnTreeNodeChecked() {
    //    var obj1;// = window.event.srcElement; ;
    //  if (window.event) obj1 = window.event;
    // var obj = obj1.srcElement ? obj1.srcElement : obj1.target;
    event
    var obj = window.event.target;
    var treeNodeFound = false;
    var checkedState;
    if (obj.tagName == 'INPUT' && obj.type == 'checkbox') {
        var treeNode = obj;
        checkedState = treeNode.checked;
        do {
            obj = obj.parentElement;
        } while (obj.tagName != 'TABLE')
        var parentTreeLevel = obj.rows[0].cells.length;
        var parentTreeNode = obj.rows[0].cells[0];
        var tables = obj.parentElement.getElementsByTagName('TABLE');
        var numTables = tables.length
        if (numTables >= 1) {
            for (i = 0; i < numTables; i++) {
                if (tables[i] == obj) {
                    treeNodeFound = true;
                    i++;
                    if (i == numTables) {
                        return;
                    }
                }
                if (treeNodeFound == true) {
                    var childTreeLevel = tables[i].rows[0].cells.length;
                    if (childTreeLevel > parentTreeLevel) {
                        var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                        var inputs = cell.getElementsByTagName('INPUT');
                        inputs[0].checked = checkedState;
                    }
                    else {
                        return;
                    }
                }
            }
        }
    }
}

///////////////////////////////////////////////
function OnCheckBoxCheckChanged(evt) {
    var src = window.event != window.undefined ? window.event.srcElement : evt.target;
    var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
    if (isChkBoxClick) {
        var parentTable = GetParentByTagName("table", src);
        var nxtSibling = parentTable.nextSibling;
        if (nxtSibling && nxtSibling.nodeType == 1)//check if nxt sibling is not null & is an element node
        {
            if (nxtSibling.tagName.toLowerCase() == "div") //if node has children
            {
                //check or uncheck children at all levels
                CheckUncheckChildren(parentTable.nextSibling, src.checked);
            }
        }
        //check or uncheck parents at all levels
        CheckUncheckParents(src, src.checked);
    }
}
function CheckUncheckChildren(childContainer, check) {
    var childChkBoxes = childContainer.getElementsByTagName("input");
    var childChkBoxCount = childChkBoxes.length;
    for (var i = 0; i < childChkBoxCount; i++) {
        childChkBoxes[i].checked = check;
    }
}
function CheckUncheckParents(srcChild, check) {
    var parentDiv = GetParentByTagName("div", srcChild);
    var parentNodeTable = parentDiv.previousSibling;

    if (parentNodeTable) {
        var checkUncheckSwitch;

        if (check) //checkbox checked
        {
            var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
            if (isAllSiblingsChecked)
                checkUncheckSwitch = true;
            else
                return; //do not need to check parent if any(one or more) child not checked
        }
        else //checkbox unchecked
        {
            checkUncheckSwitch = false;
        }

        var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
        if (inpElemsInParentTable.length > 0) {
            var parentNodeChkBox = inpElemsInParentTable[0];
            parentNodeChkBox.checked = checkUncheckSwitch;
            //do the same recursively
            CheckUncheckParents(parentNodeChkBox, checkUncheckSwitch);
        }
    }
}
function AreAllSiblingsChecked(chkBox) {
    var parentDiv = GetParentByTagName("div", chkBox);
    var childCount = parentDiv.childNodes.length;
    for (var i = 0; i < childCount; i++) {
        if (parentDiv.childNodes[i].nodeType == 1) //check if the child node is an element node
        {
            if (parentDiv.childNodes[i].tagName.toLowerCase() == "table") {
                var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
                //if any of sibling nodes are not checked, return false
                if (!prevChkBox.checked) {
                    return false;
                }
            }
        }
    }
    return true;
}
//utility function to get the container of an element by tagname
function GetParentByTagName(parentTagName, childElementObj) {
    var parent = childElementObj.parentNode;
    while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
        parent = parent.parentNode;
    }
    return parent;
}

function OpenNewsPrintWindow(pageName) {
    var ret_val;
    var x = (screen.availWidth / 2) - 200;
    var y = 200; //(screen.availWidth / 2) - 300;

    window.open(pageName, "", "toolbar=no,directories=no,location=no,status=yes,menubar=no, resizable=no, scrollbars=yes,width=700,screenX=" + x + ",screenY=" + y)
}