﻿// JScript 文件

function OpenWindow(url, name, width, height) {
    var iTop = (window.screen.height - height) / 2;
    var iLeft = (window.screen.width - width) / 2;
    window.open(url, name, "Scrollbars=yes,Toolbar=no,Location=no,Direction=no,Resizeable=yes,Width=" + width + " ,Height=" + height + ",top=" + iTop + ",left=" + iLeft);
}

function OpenModalDialog(url, args, w, h) {
    var return_Value = window.showModalDialog(url, args, "dialogWidth:" + w + ";dialogHeight:" + h + ";help:no;scroll:yes;center:yes;toolbar=no,location=no,resizable:no;status:no;");
    return return_Value;
}

function win_back_return(strText) {
    window.returnValue = strText;
    window.close();
}

function OpenUpFileWin_SingleImageFile(UpFileUrl, ParentID, hidImageUrlID, UpFileType, UpFilePath, UpFileMaxLength) {
    var url = "";
    url = UpFileUrl + "?ParentID=" + escape(ParentID) + "&FileType=SingleImageFile&UpFileType=" + escape(UpFileType) + "&UpFilePath=" + escape(UpFilePath) + "&UpFileMaxLength=" + escape(UpFileMaxLength);
    var return_value = OpenModalDialog(url, window, '600px', '250px');
    if (return_value != null) {
        document.getElementById(hidImageUrlID).value = return_value;
    }
    return true;
}

function OpenUpFileWin_AttFile(UpFileUrl, ParentID, hidCurrentAllFileLengthID, UpFileType, UpFilePath, CurrentAllFileLength, AllFileLength, UpFileMaxLength) {
    var url = "";
    url = UpFileUrl + "?ParentID=" + escape(ParentID) + "&FileType=AttFile&UpFileType=" + escape(UpFileType) + "&UpFilePath=" + escape(UpFilePath) + "&CurrentAllFileLength=" + CurrentAllFileLength + "&AllFileLength=" + escape(AllFileLength) + "&UpFileMaxLength=" + escape(UpFileMaxLength);
    var return_value = OpenModalDialog(url, window, '600px', '450px');
    if (return_value != null) {
        document.getElementById(hidCurrentAllFileLengthID).value = return_value;
    }
    return true;
}

function OpenUpFileWin_AttFile_NetWork(ParentID, AttSign, hidCurrentAllFileLengthID, UpFileType, UpFilePath, CurrentAllFileLength, AllFileLength, UpFileMaxLength) {
    var url = "";
    url = "/Modules/UpFile/UpFile_NetWork.aspx?ParentID=" + escape(ParentID) + "&AttSign=" + escape(AttSign) + "&UpFileType=" + escape(UpFileType) + "&UpFilePath=" + escape(UpFilePath) + "&CurrentAllFileLength=" + CurrentAllFileLength + "&AllFileLength=" + escape(AllFileLength) + "&UpFileMaxLength=" + escape(UpFileMaxLength);
    var return_value = OpenModalDialog(url, window, '600px', '450px');
    if (return_value != null) {
        document.getElementById(hidCurrentAllFileLengthID).value = return_value;
    }
    return true;
}

function OpenUpFileWin_UpImageData(UpFileUrl, ParentID, UpFileType, UpFileMaxLength) {
    var url = "";
    url = UpFileUrl + "?ParentID=" + escape(ParentID) + "&UpFileType=" + escape(UpFileType) + "&UpFileMaxLength=" + escape(UpFileMaxLength);
    var return_value = OpenModalDialog(url, window, '600px', '450px');
    return true;
}

function OpenUpFileWin_DocumentDatum(UpFileUrl, DocumentID, ProjectID, DatumID, UpFileType, UpFileMaxLength) {
    var url = "";
    url = UpFileUrl + "?DocumentID=" + escape(DocumentID) + "&ProjectID=" + escape(ProjectID) + "&DatumID=" + escape(DatumID) + "&UpFileType=" + escape(UpFileType) + "&UpFileMaxLength=" + escape(UpFileMaxLength);
    var return_value = OpenModalDialog(url, window, '600px', '450px');
    return true;
}

function OpenUpFileWin_UpPicToCMS(UpFileUrl) {
    alert(UpFileUrl);
    return;
    var return_value = OpenModalDialog(UpFileUrl, window, '600px', '450px');
    if (return_value != null) {
        document.getElementById(PicUrlID).value = return_value;
    }
    return true;
}

function SelectAllCheckboxes(spanChk) {

    // Added as ASPX uses SPAN for checkbox
    var oItem = spanChk.children;
    var theBox = (spanChk.type == "checkbox") ?
        spanChk : spanChk.children.item[0];
    xState = theBox.checked;
    elm = theBox.form.elements;

    for (i = 0; i < elm.length; i++)
        if (elm[i].type == "checkbox" &&
              elm[i].id != theBox.id) {
        //elm[i].click();
        if (elm[i].checked != xState)
            elm[i].click();
        //elm[i].checked=xState;
    }
}

function GetPortalContentByAjax(URL, ListControlID) {
    return new Ajax.Updater({ success: ListControlID }, URL, { asynchronous: true, method: 'post', evalScripts: true });
}

//操作文本编辑器
function SetFckEdit(FckName, SetValue) {
    var oEditor = FCKeditorAPI.GetInstance(FckName);
    oEditor.InsertHtml(SetValue);
}


//处理验证码
function getValidImg(PageUrl, ObjName) {
    var PageUrl = PageUrl + '?validid=' + selectFrom(1, 1000);
    document.getElementById(ObjName).src = PageUrl;
    document.getElementById(ObjName).style.display = 'block';
    return false;
}

function selectFrom(iFirstValue, iLastValue) {
    var iChoces = Math.abs(iLastValue - iFirstValue) + 1;
    return Math.floor(Math.random() * iChoces + iFirstValue);
}


/**************************************树控件，多选************************************/
function OnTreeClick(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 child is 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;
}
/**************************************树控件，多选************************************/