var tree;

function $(id){
    return document.getElementById(id);
}

function createTree(){
    initLoading();
    tree = new dhtmlXTreeObject("treeBox","100%","100%",0);
    tree.setSkin('dhx_skyblue');
    tree.setImagePath("../images/csh_dhx_skyblue/");
    
    var url = "ModuleGridSource.aspx?handle=modtree&uid="+newGuid();
    tree.loadJSON(url,function(){
        finishLoading();
    });
    
    tree.attachEvent("onClick", function(id){
        if(tree.isItemChecked(id)){
            tree.closeItem(id);
            tree.setCheck(id,false);
        }else{
            tree.openItem(id);
            tree.setCheck(id,true);
        }
    });
    
    tree.enableCheckBoxes(true);
//    setTimeout(function(){
//        var id = $("h_moduleid").value;
//        
//        tree.selectItem(id,false);
//        tree.openItem(id);
//    },100);
}

function newGuid(){
    var guid = "";
    for (var i = 1; i <= 32; i++){
      var n = Math.floor(Math.random()*16.0).toString(16);
       guid +=    n;
      if((i==8)||(i==12)||(i==16)||(i==20))
         guid += "-";
     }
    return guid.toUpperCase();
}

function focusOnTree(){;
    if(tree != null){
        if($("h_moduleid").value != ""){
            var id = $("h_moduleid").value;
        }
    }
}

function assignTo(){
    var modArg = new Array();
    var selParentObj = $("sel_module");
    
    
    if(tree != null){
        if(tree.getAllChecked().indexOf(",") >= 0){
            //modArg = tree.getAllChecked().split(",");
            alert("指定错误,只能指定一个模块作为父模块");
            return;
        }else{
            modArg.push(tree.getAllChecked());
        }
        
        if(selParentObj.options.length < 1){
            var varItem = new Option(tree.getItemText(modArg[0]), modArg[0]);      
            selParentObj.options.add(varItem);
            
            window.opener.document.getElementById("txt_parent").value = tree.getItemText(modArg[0]);
            window.opener.document.getElementById("h_parentid").value = modArg[0];
            //var parentId = window.opener.document.getElementById("h_parentid").value;
        }else{
            alert("已指定父模块信息,不能重复添加,请先移除后再添加");
            return;
        }
    }
}

function removeAssign(){
    var selParentObj = $("sel_module");
    
    if(selParentObj.options.length > 0){
        selParentObj.options.remove(0);
        window.opener.document.getElementById("txt_parent").value = "";
        window.opener.document.getElementById("h_parentid").value = "";
    }
}

function mouseMoveImg(obj,type){
    var imgUrl = obj.src;
    var urlArg = imgUrl.split("/");
    
    if(type == 1){
        if(urlArg[urlArg.length-1] == "edit_left.gif"){
            var newUrl = "";
            for(var i=0;i<urlArg.length;i++){
                if(i == urlArg.length - 1){
                    newUrl += "edit_lefthover.gif";
                }else{
                    newUrl += urlArg[i]+"/";
                }
            }
            
            obj.src = newUrl;
        }else{
            var newUrl = "";
            for(var i=0;i<urlArg.length;i++){
                if(i == urlArg.length - 1){
                    newUrl += "edit_left.gif";
                }else{
                    newUrl += urlArg[i]+"/";
                }
            }
            
            obj.src = newUrl;
        }
    }else{
        if(urlArg[urlArg.length-1] == "edit_right.gif"){
            var newUrl = "";
            for(var i=0;i<urlArg.length;i++){
                if(i == urlArg.length - 1){
                    newUrl += "edit_righthover.gif";
                }else{
                    newUrl += urlArg[i]+"/";
                }
            }
            
            obj.src = newUrl;
        }else{
            var newUrl = "";
            for(var i=0;i<urlArg.length;i++){
                if(i == urlArg.length - 1){
                    newUrl += "edit_right.gif";
                }else{
                    newUrl += urlArg[i]+"/";
                }
            }
            
            obj.src = newUrl;
        }
    }
    
}

function searchModule(txtObjName){

    var searchVal = $(txtObjName).value;
    
    var searchModuleObj = null;
    
    var url = "../Modules/ModuleGridSource.aspx?handle=checkmod&searchmod="+escape(searchVal)+"&uid="+newGuid();
    var loader = dhtmlxAjax.getSync(url);
    var postStatus = loader.xmlDoc.responseText;
    
    if(postStatus != ""){
        searchModuleObj = JSON.parse(postStatus,function(key,value){
            return value;
        });
    }
    if(tree != null){
        for(var i=0;i<searchModuleObj.mods.length;i++){
            tree.selectItem(searchModuleObj.mods[i].gid);
        }
    }
}

function finishLoading(){
    $("progressBar").style.display = "none";
}

function initLoading(){
    $("progressBar").style.display = "";
}