<commit_before> /**
 *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
 * 
 * This software is licensed under the GNU General Public License v3 or later.
 * 
 * It is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

var $selectedDomainTreeNode;
var defaultRootLevel = 0;	   
var childParentMap = {};  //map childDomainId to parentDomainId
var domainIdNameMap = {}; //map domainId to domainName    

function refreshWholeTree(rootDomainId, rootLevel) {
    drawRootNode(rootDomainId);
    drawTree(rootDomainId, (rootLevel+1), $("#domain_children_container_"+rootDomainId));  //draw the whole tree (under root node)			
    $("#domain_"+rootDomainId).show();	//show root node
    clickExpandIcon(rootDomainId);      //expand root node	    
}

//draw root node
function drawRootNode(rootDomainId) {
    var $loading = $("#leftmenu_domain_tree").find("#loading_container").show();
    var $domainTree = $("#leftmenu_domain_tree").find("#tree_container").hide();
   
    $.ajax({
        data: createURL("command=listDomains&id="+rootDomainId), 
        dataType: "json",
        async: false,
        success: function(json) {					        
            var domains = json.listdomainsresponse.domain;	
            $domainTree.empty();			        	    
	        if (domains != null && domains.length > 0) {				   					    
			    var node = drawNode(domains[0], defaultRootLevel, $domainTree); 
			    
			    var treeLevelsbox = node.find(".tree_levelsbox");	//root node shouldn't have margin-left:20px				   
			    if(treeLevelsbox!=null && treeLevelsbox.length >0)
			        treeLevelsbox[0].style.marginLeft="0px";        //set root node's margin-left to 0px.
			}		
			$loading.hide();
            $domainTree.show();			
        }
    }); 		
}

function drawNode(json, level, container) {		  
    if("parentdomainid" in json)
        childParentMap[json.id] = json.parentdomainid;	//map childDomainId to parentDomainId   
    domainIdNameMap[json.id] = json.name;               //map domainId to domainName

    var $treeNode = $("#domain_tree_node_template").clone(true).attr("id", "domain_tree_node_template_clone");	  
    $treeNode.find("#domain_indent").css("marginLeft", (30*(level+1)));           
    $treeNode.attr("id", "domain_"+fromdb(json.id));	         
    $treeNode.data("jsonObj", json).data("domainLevel", level); 	      
    $treeNode.find("#domain_title_container").attr("id", "domain_title_container_"+fromdb(json.id)); 	        
    $treeNode.find("#domain_expand_icon").attr("id", "domain_expand_icon_"+fromdb(json.id)); 
    $treeNode.find("#domain_name").attr("id", "domain_name_"+fromdb(json.id)).text(fromdb(json.name));        	              	
    $treeNode.find("#domain_children_container").attr("id", "domain_children_container_"+fromdb(json.id));          
    container.append($treeNode.show());	 
    return $treeNode;   	       
}          

function drawTree(id, level, container) {		        
    $.ajax({
	    data: createURL("command=listDomainChildren&id="+id),
	    dataType: "json",
	    async: false,
	    success: function(json) {					        
	        var domains = json.listdomainchildrenresponse.domain;				        	    
		    if (domains != null && domains.length > 0) {					    
			    for (var i = 0; i < domains.length; i++) {						    
				    drawNode(domains[i], level, container);	
				    if(domains[i].haschild == true) 
		                drawTree(domains[i].id, (level+1), $("#domain_children_container_"+domains[i].id));				   
			    }
		    }				
	    }
    }); 
}	

function clickExpandIcon(domainId) {
    var $treeNode = $("#domain_"+domainId);
    var expandIcon = $treeNode.find("#domain_expand_icon_"+domainId);
    if (expandIcon.hasClass("expanded_close")) {													
		$treeNode.find("#domain_children_container_"+domainId).show();							
		expandIcon.removeClass("expanded_close").addClass("expanded_open");
	} 
	else if (expandIcon.hasClass("expanded_open")) {																	
	    $treeNode.find("#domain_children_container_"+domainId).hide();						
		expandIcon.removeClass("expanded_open").addClass("expanded_close");
	}			
}					

function domainAccountJSONToTemplate(jsonObj, $template) {   
    $template.data("jsonObj", jsonObj);  
    $template.find("#grid_header_title").text(fromdb(jsonObj.name));
    $template.find("#id").text(jsonObj.id);
    $template.find("#role").text(toRole(jsonObj.accounttype));
    $template.find("#account").text(fromdb(jsonObj.name));
    $template.find("#domain").text(fromdb(jsonObj.domain));
    $template.find("#vm_total").text(jsonObj.vmtotal);
    $template.find("#ip_total").text(jsonObj.iptotal);
    $template.find("#bytes_received").text(convertBytes(jsonObj.receivedbytes));
    $template.find("#bytes_sent").text(convertBytes(jsonObj.sentbytes));
    $template.find("#state").text(jsonObj.state);
}

function afterLoadDomainJSP() {
    if(isAdmin()) {
        initAddDomainDialog();
    }
}

function initAddDomainDialog() {
    initDialog("dialog_add_domain", 450);
    
    var $dialogAddDomain = $("#dialog_add_domain");
               
	$.ajax({
	  data: createURL("command=listDomains"),
		dataType: "json",
		async: false,
		success: function(json) {
		    var $domainDropdown1 = $dialogAddDomain.find("#domain_dropdown").empty();		  
			var domains = json.listdomainsresponse.domain;						
			if (domains != null && domains.length > 0) {
				for (var i = 0; i < domains.length; i++) {
					$domainDropdown1.append("<option value='" + fromdb(domains[i].id) + "'>" + fromdb(domains[i].name) + "</option>"); 					
				}
			} 
		}
	});   
		 
    //add button ***
    $("#midmenu_add_link").find("#label").text("Add Domain"); 
    $("#midmenu_add_link").show();     
    $("#midmenu_add_link").unbind("click").bind("click", function(event) {      
		$dialogAddDomain
		.dialog('option', 'buttons', { 					
			"Create": function() { 	
			    var $thisDialog = $(this);
			    				    			
				// validate values
				var isValid = true;					
				isValid &= validateString("Name", $thisDialog.find("#add_domain_name"), $thisDialog.find("#add_domain_name_errormsg"));					
				if (!isValid) 
				    return;
				 
				$thisDialog.dialog("close");    			
							
				var array1 = [];	
				var name = trim($thisDialog.find("#add_domain_name").val());	
				array1.push("&name="+todb(name));
																		
				var parentDomainId = $thisDialog.find("#domain_dropdown").val();				
				array1.push("&parentdomainid="+parentDomainId);	
						
				$.ajax({
				    data: createURL("command=createDomain"+array1.join("")),
					dataType: "json",<commit_msg>Add Domain Dialog box - default parent domain dropdown field to the domain who is curretnly loaded in right panel.<commit_after> /**
 *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
 * 
 * This software is licensed under the GNU General Public License v3 or later.
 * 
 * It is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

var $selectedDomainTreeNode;
var defaultRootLevel = 0;	   
var childParentMap = {};  //map childDomainId to parentDomainId
var domainIdNameMap = {}; //map domainId to domainName    

function refreshWholeTree(rootDomainId, rootLevel) {
    drawRootNode(rootDomainId);
    drawTree(rootDomainId, (rootLevel+1), $("#domain_children_container_"+rootDomainId));  //draw the whole tree (under root node)			
    $("#domain_"+rootDomainId).show();	//show root node
    clickExpandIcon(rootDomainId);      //expand root node	    
}

//draw root node
function drawRootNode(rootDomainId) {
    var $loading = $("#leftmenu_domain_tree").find("#loading_container").show();
    var $domainTree = $("#leftmenu_domain_tree").find("#tree_container").hide();
   
    $.ajax({
        data: createURL("command=listDomains&id="+rootDomainId), 
        dataType: "json",
        async: false,
        success: function(json) {					        
            var domains = json.listdomainsresponse.domain;	
            $domainTree.empty();			        	    
	        if (domains != null && domains.length > 0) {				   					    
			    var node = drawNode(domains[0], defaultRootLevel, $domainTree); 
			    
			    var treeLevelsbox = node.find(".tree_levelsbox");	//root node shouldn't have margin-left:20px				   
			    if(treeLevelsbox!=null && treeLevelsbox.length >0)
			        treeLevelsbox[0].style.marginLeft="0px";        //set root node's margin-left to 0px.
			}		
			$loading.hide();
            $domainTree.show();			
        }
    }); 		
}

function drawNode(json, level, container) {		  
    if("parentdomainid" in json)
        childParentMap[json.id] = json.parentdomainid;	//map childDomainId to parentDomainId   
    domainIdNameMap[json.id] = json.name;               //map domainId to domainName

    var $treeNode = $("#domain_tree_node_template").clone(true).attr("id", "domain_tree_node_template_clone");	  
    $treeNode.find("#domain_indent").css("marginLeft", (30*(level+1)));           
    $treeNode.attr("id", "domain_"+fromdb(json.id));	         
    $treeNode.data("jsonObj", json).data("domainLevel", level); 	      
    $treeNode.find("#domain_title_container").attr("id", "domain_title_container_"+fromdb(json.id)); 	        
    $treeNode.find("#domain_expand_icon").attr("id", "domain_expand_icon_"+fromdb(json.id)); 
    $treeNode.find("#domain_name").attr("id", "domain_name_"+fromdb(json.id)).text(fromdb(json.name));        	              	
    $treeNode.find("#domain_children_container").attr("id", "domain_children_container_"+fromdb(json.id));          
    container.append($treeNode.show());	 
    return $treeNode;   	       
}          

function drawTree(id, level, container) {		        
    $.ajax({
	    data: createURL("command=listDomainChildren&id="+id),
	    dataType: "json",
	    async: false,
	    success: function(json) {					        
	        var domains = json.listdomainchildrenresponse.domain;				        	    
		    if (domains != null && domains.length > 0) {					    
			    for (var i = 0; i < domains.length; i++) {						    
				    drawNode(domains[i], level, container);	
				    if(domains[i].haschild == true) 
		                drawTree(domains[i].id, (level+1), $("#domain_children_container_"+domains[i].id));				   
			    }
		    }				
	    }
    }); 
}	

function clickExpandIcon(domainId) {
    var $treeNode = $("#domain_"+domainId);
    var expandIcon = $treeNode.find("#domain_expand_icon_"+domainId);
    if (expandIcon.hasClass("expanded_close")) {													
		$treeNode.find("#domain_children_container_"+domainId).show();							
		expandIcon.removeClass("expanded_close").addClass("expanded_open");
	} 
	else if (expandIcon.hasClass("expanded_open")) {																	
	    $treeNode.find("#domain_children_container_"+domainId).hide();						
		expandIcon.removeClass("expanded_open").addClass("expanded_close");
	}			
}					

function domainAccountJSONToTemplate(jsonObj, $template) {   
    $template.data("jsonObj", jsonObj);  
    $template.find("#grid_header_title").text(fromdb(jsonObj.name));
    $template.find("#id").text(jsonObj.id);
    $template.find("#role").text(toRole(jsonObj.accounttype));
    $template.find("#account").text(fromdb(jsonObj.name));
    $template.find("#domain").text(fromdb(jsonObj.domain));
    $template.find("#vm_total").text(jsonObj.vmtotal);
    $template.find("#ip_total").text(jsonObj.iptotal);
    $template.find("#bytes_received").text(convertBytes(jsonObj.receivedbytes));
    $template.find("#bytes_sent").text(convertBytes(jsonObj.sentbytes));
    $template.find("#state").text(jsonObj.state);
}

function afterLoadDomainJSP() {
    if(isAdmin()) {
        initAddDomainDialog();
    }
}

function initAddDomainDialog() {
    initDialog("dialog_add_domain", 450);
    
    var $dialogAddDomain = $("#dialog_add_domain");              
	
    //add button ***
    $("#midmenu_add_link").find("#label").text("Add Domain"); 
    $("#midmenu_add_link").show();     
    $("#midmenu_add_link").unbind("click").bind("click", function(event) {  
    
        $dialogAddDomain.find("#add_domain_name").val("");
        
        $.ajax({
	      data: createURL("command=listDomains"),
		    dataType: "json",
		    async: false,
		    success: function(json) {
		        var $domainDropdown1 = $dialogAddDomain.find("#domain_dropdown").empty();		  
			    var domains = json.listdomainsresponse.domain;						
			    if (domains != null && domains.length > 0) {
				    for (var i = 0; i < domains.length; i++) {
					    $domainDropdown1.append("<option value='" + fromdb(domains[i].id) + "'>" + fromdb(domains[i].name) + "</option>"); 					
				    }
			    } 
			   
			    var $thisTab = $("#right_panel_content").find("#tab_content_details");    
                var domainId = $thisTab.find("#id").text();   //get domainId from here in case domain page is empty (e.g. when a domain was just deleted)
                if(domainId != null && domainId.length > 0)
                    $domainDropdown1.val(domainId);			    		    
		    }
	    });  
        
		$dialogAddDomain
		.dialog('option', 'buttons', { 					
			"Create": function() { 	
			    var $thisDialog = $(this);
			    				    			
				// validate values
				var isValid = true;					
				isValid &= validateString("Name", $thisDialog.find("#add_domain_name"), $thisDialog.find("#add_domain_name_errormsg"));					
				if (!isValid) 
				    return;
				 
				$thisDialog.dialog("close");    			
							
				var array1 = [];	
				var name = trim($thisDialog.find("#add_domain_name").val());	
				array1.push("&name="+todb(name));
																		
				var parentDomainId = $thisDialog.find("#domain_dropdown").val();				
				array1.push("&parentdomainid="+parentDomainId);	
						
				$.ajax({
				    data: createURL("command=createDomain"+array1.join("")),
					dataType: "json",
{'fraction_non_alphanumeric': 0.14929879889955042, 'fraction_numerical': 0.004562839696705362, 'mean_word_length': 2.96488427773344, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4915942', 'n_tokens_mistral': 5117, 'n_tokens_neox': 4434, 'n_words': 856}