function runscript() {
    showhideSubTree(0);
}

function selectSubTree(node) {
    url = ajaxUrl + '?process=select_subTree' + '&node=' + node + '&botsid=' + botsid;
    loadXMLDoc(url);
}

function deselectSubTree(node) {
    url = ajaxUrl + '?process=deselect_subTree' + '&node=' + node + '&botsid=' + botsid;
    loadXMLDoc(url);
}

function showhideSubTree(node) {
    url = ajaxUrl + '?process=showhide_regionTree' + '&node=' + node + '&botsid=' + botsid;
    loadXMLDoc(url);
}

function showSubTreeConfirm(response) {
    var rootNode = document.getElementById(response.getAttribute("root"));
    var outHTML = subTreeConstructor(response);
    var nr = rootNode.parentNode.insertRow(rootNode.rowIndex + 1);
    var nc = nr.insertCell(); 
    var nc = nr.insertCell(); 
    nc.colSpan = 2;
    nc.innerHTML = outHTML;
}

function hideSubTreeConfirm(response) {
    var rootNode = document.getElementById(response.getAttribute("root"));
    rootNode.parentNode.deleteRow(rootNode.rowIndex + 1);
}

function updateCheckBox(chbName, chbStatus, arrName) {
    var url = ajaxUrl + '?process=' + arrName + '&v=' + chbName + '&f=' + chbStatus + '&botsid=' + botsid;
    loadXMLDoc(url);
}

function checkBoxConfirm(ajaxResponse) 
{
    ajaxChboxList = ajaxResponse.getElementsByTagName("set");
    for (var i=0; i<ajaxChboxList.length; i++) {
        ajaxChboxName = ajaxChboxList[i].getAttribute("chbName");
        ajaxChboxSet  = ajaxChboxList[i].getAttribute("chbSet");
        document.getElementById('C' + ajaxChboxName).checked = eval(ajaxChboxSet);
    }
}

function checkBoxHTML(checked, id, arrName) {
    var result = '<input type="checkbox" ';
    if (checked) {
        result = result + ' checked="checked"';
    }
    result = result + 'id="C' + id + '" onclick="javascript:updateCheckBox(\'' + id + '\',this.checked, \'' + arrName + '\')">';
    return(result);
}

// ##########  Žâ®¡à ¦¥­¨¥ ¤¥à¥¢ì¥¢ ################

var treeNullSectImage     = '<img border="0" src="img/null.gif" width="13" height="13">';
var treeNullCheckBoxImage = '&nbsp;';
// var treeNullCheckBoxImage = '<img border="0" src="img/null.gif" width="20" height="13">';
var treeSectImage         = '<img border="0" src="img/sect.gif" width="13" height="13">';


function subTreeConstructor(tree) 
{
    var rowsList = tree.childNodes;
    var result = '';
    if (rowsList.length>0) {
        result = '<table border="0" width="100%" cellspacing="0" cellpadding="0">';
        for (var i=0; i<rowsList.length; i++) {
            if (rowsList[i].nodeName == 'folder') {
                result = result + '<tr id="' + rowsList[i].getAttribute('id') + '"><td>' + treeSectImage + '</td>';
            } else {
                result = result + '<tr id="' + rowsList[i].getAttribute('id') + '"><td>' + treeNullSectImage + '</td>';
            }
            if (eval(rowsList[i].getAttribute('haveCheckBox'))) {
                result = result + '<td>' + checkBoxHTML(eval(rowsList[i].getAttribute('checked')), rowsList[i].getAttribute('id'), rowsList[i].getAttribute('arrName')) + '&nbsp;';
            } else {
                result = result + '<td>' + treeNullCheckBoxImage + '&nbsp;';
            }
            if (rowsList[i].nodeName == 'folder') {
                cid = rowsList[i].getAttribute('id');
                result = result + '<a onclick="showhideSubTree(this.parentNode.parentNode.id)" href="#' + cid + '">' + rowsList[i].getAttribute('name') + '</a></td><td style="padding-left:15px; text-align:right;">';
                result = result + '<a onclick="selectSubTree(this.parentNode.parentNode.id)" href="#' + cid + '" class="select">Âûáðàòü âñå</a>';
                result = result + '&nbsp;&nbsp;<a onclick="deselectSubTree(this.parentNode.parentNode.id)" href="#' + cid + '" class="select">Î÷èñòèòü</a>';
                resulit = result + '</td></tr>';
            } else {
                result = result + '<i>' + rowsList[i].getAttribute('name') + '</i></td></tr>';
            }
            if(rowsList[i].hasChildNodes()) {
                result = result + '<tr><td>' + treeNullSectImage + '</td><td colspan="2">' + subTreeConstructor(rowsList[i]) + '</td></tr>';
            }
        }
        result = result + '</table>';
    }
    return(result);
}

