<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/>.
 * 
 */

function ipGetSearchParams() {
    var moreCriteria = [];	

	var searchInput = $("#basic_search").find("#search_input").val();	 
    if (searchInput != null && searchInput.length > 0) {	           
        moreCriteria.push("&keyword="+todb(searchInput));	       
    }     

	var $advancedSearchPopup = getAdvancedSearchPopupInSearchContainer();
	if ($advancedSearchPopup.length > 0 && $advancedSearchPopup.css("display") != "none" ) {		

		var zone = $advancedSearchPopup.find("#adv_search_zone").val();	
	    if (zone!=null && zone.length > 0) 
			moreCriteria.push("&zoneId="+zone);	
				        
        if ($advancedSearchPopup.find("#adv_search_domain_li").css("display") != "none") {		
		    var domainId = $advancedSearchPopup.find("#adv_search_domain").val();		
		    if (domainId!=null && domainId.length > 0) 
			    moreCriteria.push("&domainid="+domainId);	
    	}	
    	
		if ($advancedSearchPopup.find("#adv_search_account_li").css("display") != "none" 
    	    && $advancedSearchPopup.find("#adv_search_account").hasClass("textwatermark") == false) {	
		    var account = $advancedSearchPopup.find("#adv_search_account").val();		
		    if (account!=null && account.length > 0) 
			    moreCriteria.push("&account="+account);		
		}	
	} 	
	
	return moreCriteria.join("");          
}

function afterLoadIpJSP() {
    //***** switch between different tabs (begin) ********************************************************************
    var tabArray = [$("#tab_details"), $("#tab_port_range"), $("#tab_port_forwarding"), $("#tab_load_balancer"), $("#tab_vpn")];
    var tabContentArray = [$("#tab_content_details"), $("#tab_content_port_range"), $("#tab_content_port_forwarding"), $("#tab_content_load_balancer"), $("#tab_content_vpn")];
    var afterSwitchFnArray = [ipJsonToDetailsTab, ipJsonToPortRangeTab, ipJsonToPortForwardingTab, ipJsonToLoadBalancerTab, ipJsonToVPNTab];
    switchBetweenDifferentTabs(tabArray, tabContentArray, afterSwitchFnArray);       
    //***** switch between different tabs (end) **********************************************************************
        
    //dialogs
    initDialog("dialog_acquire_public_ip", 325);
	initDialog("dialog_enable_vpn");
	initDialog("dialog_disable_vpn");
	initDialog("dialog_add_vpnuser");
	initDialog("dialog_confirmation_remove_vpnuser");
	initDialog("dialog_enable_static_NAT");
    
    //*** Acquire New IP (begin) ***
	$.ajax({
	    data: createURL("command=listZones&available=true"),
		dataType: "json",
		success: function(json) {
			var zones = json.listzonesresponse.zone;				
			var zoneSelect = $("#dialog_acquire_public_ip #acquire_zone").empty();	
			if (zones != null && zones.length > 0) {	
			    for (var i = 0; i < zones.length; i++) {
				    zoneSelect.append("<option value='" + zones[i].id + "'>" + fromdb(zones[i].name) + "</option>"); 
			    }
		    }
		}
	});
	    
    $("#acquire_new_ip_button").unbind("click").bind("click", function(event) {  
		var submenuContent = $("#submenu_content_network");
		$("#dialog_acquire_public_ip").dialog('option', 'buttons', {				
			"Acquire": function() { 
				var thisDialog = $(this);	
				thisDialog.dialog("close");
						
				var zoneid = thisDialog.find("#acquire_zone").val();				
				
				var $midmenuItem1 = beforeAddingMidMenuItem() ;	
				
				$.ajax({
				    data: createURL("command=associateIpAddress&zoneid="+zoneid),
				    dataType: "json",
				    success: function(json) {						        
				        var jobId = json.associateipaddressresponse.jobid;				        
				        var timerKey = "associateIpJob_"+jobId;
							    
				        $("body").everyTime(2000, timerKey, function() {
						    $.ajax({
							    data: createURL("command=queryAsyncJobResult&jobId="+jobId),
							    dataType: "json",
							    success: function(json) {										       						   
								    var result = json.queryasyncjobresultresponse;
								    if (result.jobstatus == 0) {
									    return; //Job has not completed
								    } else {											    
									    $("body").stopTime(timerKey);
									    if (result.jobstatus == 1) {
										    // Succeeded										    							   
										    ipToMidmenu(result.jobresult.ipaddress, $midmenuItem1);
						                    bindClickToMidMenu($midmenuItem1, ipToRightPanel, ipGetMidmenuId);  
						                    afterAddingMidMenuItem($midmenuItem1, true);	                            
									    } else if (result.jobstatus == 2) {
									        afterAddingMidMenuItem($midmenuItem1, false, fromdb(result.jobresult.errortext));					        							        								   				    
									    }
								    }
							    },
							    error: function(XMLHttpResponse) {
								    $("body").stopTime(timerKey);
									handleError(XMLHttpResponse, function() {
										afterAddingMidMenuItem($midmenuItem1, false, parseXMLHttpResponse(XMLHttpResponse));
									});
							    }
						    });
					    }, 0);						    					
				    },
				    error: function(XMLHttpResponse) {
						handleError(XMLHttpResponse, function() {
							afterAddingMidMenuItem($midmenuItem1, false, parseXMLHttpResponse(XMLHttpResponse));	
						});
				    }
			    });
			},
			"Cancel": function() { 
				$(this).dialog("close"); 
			}
		}).dialog("open");			
		return false;
	});
    //*** Acquire New IP (end) ***
    
    //*** Port Range tab (begin) ***
    var $createPortRangeRow = $("#tab_content_port_range").find("#create_port_range_row");     
  
    $createPortRangeRow.find("#add_link").bind("click", function(event){	        
		var isValid = true;							
		isValid &= validateInteger("Start Port", $createPortRangeRow.find("#start_port"), $createPortRangeRow.find("#start_port_errormsg"), 1, 65535);
		isValid &= validateInteger("End Port", $createPortRangeRow.find("#end_port"), $createPortRangeRow.find("#end_port_errormsg"), 1, 65535);				
		if (!isValid) 
		    return;			
	    
	    var $template = $("#port_range_template").clone();
	    $("#tab_content_port_range #grid_content").append($template.show());		
	    
	    var $spinningWheel = $template.find("#row_container").find("#spinning_wheel");	
	    $spinningWheel.find("#description").text(g_dictionary["label.adding.processing"]);	
        $spinningWheel.show();   
	    	    
	    var $midmenuItem1 = $("#right_panel_content").data("$midmenuItem1");       
        var ipObj = $midmenuItem1.data("jsonObj");          	        
		
		var startPort = $createPortRangeRow.find("#start_port").val();
	    var endPort = $createPortRangeRow.find("#end_port").val();
	    var protocol = $createPortRangeRow.find("#protocol").val();
	   	    
	    var array1 = [];
        array1.push("&ipaddressid="+ipObj.id);           
        array1.push("&startPort="+startPort);
        array1.push("&endPort="+endPort);
        array1.push("&protocol="+protocol);
                
        $.ajax({
            data: createURL("command=createIpForwardingRule"+array1.join("")),
            dataType: "json",           
            success: function(json) {                                    	                        
                var jobId = json.createipforwardingruleresponse.jobid;                  			                        
                var timerKey = "<KEY> + jobId;					                       
                $("body").everyTime(
                    10000,
                    timerKey,
                    function() {
                        $.ajax({
                            data: createURL("command=queryAsyncJobResult&jobId="+jobId),
	                        dataType: "json",									                    					                    
	                        success: function(json) {		                                              							                       
		                        var result = json.queryasyncjobresultresponse;										                   
		                        if (result.jobstatus == 0) {
			                        return; //Job has not completed
		                        } else {											                    
			                        $("body").stopTime(timerKey);				                        
			                        $spinningWheel.hide();  			                                 		                       
			                        if (result.jobstatus == 1) { // Succeeded 				                            
	                                    var item = json.queryasyncjobresultresponse.jobresult.ipforwardingrule;		       	        	
	                                    portRangeJsonToTemplate(item, $template);
	                                    $spinningWheel.hide();   
	                                    refreshCreatePortRangeRow();			 
			                        } else if (result.jobstatus == 2) { // Failed			                            
				                        $template.slideUp("slow", function() {
					                        $(this).remove();
				                        });
				                        //var errorMsg = g_dictionary["label.failed"] + " - " + g_dictionary["label.error.code"] + " " + fromdb(result.jobresult.errorcode);
	                                    var errorMsg = g_dictionary["label.failed"] + " - " + fromdb(result.jobresult.errortext);	
	                                    $("#dialog_error").text(errorMsg).dialog("open");
			                        }											                    
		                        }
	                        },
	                        error: function(XMLHttpResponse) {	                  
		                        $("body").stopTime(timerKey);	
								handleError(XMLHttpResponse, function() {
					                $template.slideUp("slow", function() {
						                $(this).remove();
					                });
					                var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
		                            $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
				                });								
	                        }
                        });
                    },
                    0
                );
            },
            error: function(XMLHttpResponse) {	
                handleError(XMLHttpResponse, function() {
	                $template.slideUp("slow", function() {
		                $(this).remove();
	                });
	                var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
                    $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
                });		
            }
        });    	    
	    
	    return false;
	});
    //*** Port Range tab (end) ***
   
    //*** Port Forwarding tab (begin) ***
    var $createPortForwardingRow = $("#tab_content_port_forwarding").find("#create_port_forwarding_row");     
    
    $createPortForwardingRow.find("#add_link").bind("click", function(event){	        
		var isValid = true;		
		isValid &= validateDropDownBox("Instance", $createPortForwardingRow.find("#vm"), $createPortForwardingRow.find("#vm_errormsg"));				
		isValid &= validateInteger("Public Port", $createPortForwardingRow.find("#public_port"), $createPortForwardingRow.find("#public_port_errormsg"), 1, 65535);
		isValid &= validateInteger("Private Port", $createPortForwardingRow.find("#private_port"), $createPortForwardingRow.find("#private_port_errormsg"), 1, 65535);				
		if (!isValid) 
		    return;			
	    
	    var $template = $("#port_forwarding_template").clone();
	    $("#tab_content_port_forwarding #grid_content").append($template.show());		
	    
	    var $spinningWheel = $template.find("#row_container").find("#spinning_wheel");	
	    $spinningWheel.find("#description").text(g_dictionary["label.adding.processing"]);	
        $spinningWheel.show();   
	    	    
	    var $midmenuItem1 = $("#right_panel_content").data("$midmenuItem1");       
        var ipObj = $midmenuItem1.data("jsonObj");            	        
				
	    var publicPort = $createPortForwardingRow.find("#public_port").val();
	    var privatePort = $createPortForwardingRow.find("#private_port").val();
	    var protocol = $createPortForwardingRow.find("#protocol").val();
	    var virtualMachineId = $createPortForwardingRow.find("#vm").val();		   
	    		    
	    var array1 = [];
        array1.push("&ipaddressid="+ipObj.id);           
        array1.push("&publicport="+publicPort);
        array1.push("&privateport="+privatePort);
        array1.push("&protocol="+protocol);
        array1.push("&virtualmachineid=" + virtualMachineId);
        
        $.ajax({
            data: createURL("command=createPortForwardingRule"+array1.join("")),
            dataType: "json",           
            success: function(json) {	                               	                        
                var jobId = json.createportforwardingruleresponse.jobid;                  			                        
                var timerKey = "asyncJob_" + jobId;					                       
                $("body").everyTime(
                    10000,
                    timerKey,
                    function() {
                        $.ajax({
                            data: createURL("command=queryAsyncJobResult&jobId="+jobId),
	                        dataType: "json",									                    					                    
	                        success: function(json) {		                                              							                       
		                        var result = json.queryasyncjobresultresponse;										                   
		                        if (result.jobstatus == 0) {
			                        return; //Job has not completed
		                        } else {											                    
			                        $("body").stopTime(timerKey);				                        
			                        $spinningWheel.hide();  			                                 		                       
			                        if (result.jobstatus == 1) { // Succeeded 	
	                                    var item = json.queryasyncjobresultresponse.jobresult.portforwardingrule;		       	        	
	                                    portForwardingJsonToTemplate(item,$template);
	                                    $spinningWheel.hide();   
	                                    refreshCreatePortForwardingRow();			 
			                        } else if (result.jobstatus == 2) { // Failed			                            
				                        $template.slideUp("slow", function() {
					                        $(this).remove();
				                        });
				                        //var errorMsg = g_dictionary["label.failed"] + " - " + g_dictionary["label.error.code"] + " " + fromdb(result.jobresult.errorcode);
	                                    var errorMsg = g_dictionary["label.failed"] + " - " + fromdb(result.jobresult.errortext);	
	                                    $("#dialog_error").text(errorMsg).dialog("open");
			                        }											                    
		                        }
	                        },
	                        error: function(XMLHttpResponse) {	                  
		                        $("body").stopTime(timerKey);	
								handleError(XMLHttpResponse, function() {
					                $template.slideUp("slow", function() {
						                $(this).remove();
					                });
					                var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
		                            $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
				                });								
	                        }
                        });
                    },
                    0
                );
            },
            error: function(XMLHttpResponse) {	
                handleError(XMLHttpResponse, function() {
	                $template.slideUp("slow", function() {
		                $(this).remove();
	                });
	                var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
                    $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
                });		
            }
        });    	    
	    
	    return false;
	});
    //*** Port Forwarding tab (end) ***
    
    //*** Load Balancer tab (begin) ***
    var createLoadBalancerRow = $("#tab_content_load_balancer #create_load_balancer_row");
    
    createLoadBalancerRow.find("#add_link").bind("click", function(event){		
	    // validate values		    
		var isValid = true;					
		isValid &= validateString("Name", createLoadBalancerRow.find("#name"), createLoadBalancerRow.find("#name_errormsg"));
		isValid &= validateInteger("Public Port", createLoadBalancerRow.find("#public_port"), createLoadBalancerRow.find("#public_port_errormsg"), 1, 65535);
		isValid &= validateInteger("Private Port", createLoadBalancerRow.find("#private_port"), createLoadBalancerRow.find("#private_port_errormsg"), 1, 65535);				
		if (!isValid) return;
		 
		var $template = $("#load_balancer_template").clone();	
		$("#tab_content_load_balancer #grid_content").append($template.show());		
		
		var $spinningWheel = $template.find("#row_container").find("#spinning_wheel");	
	    $spinningWheel.find("#description").text(g_dictionary["label.adding.processing"]);	
        $spinningWheel.show();            			 
		 			
		var $midmenuItem1 = $("#right_panel_content").data("$midmenuItem1");        
        var ipObj = $midmenuItem1.data("jsonObj");
       	 
	    var name = createLoadBalancerRow.find("#name").val();  
	    var publicPort = createLoadBalancerRow.find("#public_port").val();
	    var privatePort = createLoadBalancerRow.find("#private_port").val();
	    var algorithm = createLoadBalancerRow.find("#algorithm_select").val();  
	    		   
	    var array1 = [];
        array1.push("&publicipid="+ipObj.id);    
        array1.push("&name="+todb(name));              
        array1.push("&publicport="+publicPort);
        array1.push("&privateport="+privatePort);
        array1.push("&algorithm="+algorithm);
       
        $.ajax({
	        data: createURL("command=createLoadBalancerRule"+array1.join("")),
			dataType: "json",
			success: function(json) {	
				var item = json.createloadbalancerruleresponse.loadbalancer;				
	            loadBalancerJsonToTemplate(item, $template);
	            $spinningWheel.hide();   
	            refreshCreateLoadBalancerRow();	            	
			},
		    error: function(XMLHttpResponse) {				    
			    handleError(XMLHttpResponse, function() {
					$template.slideUp("slow", function() {
						$(this).remove();
					});
					var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
		            $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
				});
		    }			
		});  	    
	    return false;
	});
	//*** Load Balancer tab (end) ***
}

function ipGetMidmenuId(jsonObj) {   
    return ipGetMidmenuId2(jsonObj.ipaddress);
}

function ipGetMidmenuId2(ipaddress) {  
    return "midmenuItem_" + ipaddress.replace(/\./g, "_");   //e.g. "192.168.33.108" => "192_168_33_108"
}

function ipToMidmenu(jsonObj, $midmenuItem1) {    
    var id = ipGetMidmenuId(jsonObj);
    $midmenuItem1.attr("id", id);  
    $midmenuItem1.data("jsonObj", jsonObj); 
    
    var $iconContainer = $midmenuItem1.find("#icon_container").show();
    $iconContainer.find("#icon").attr("src", "images/midmenuicon_network_networkgroup.png");
    
	var firstRow = jsonObj.ipaddress.substring(0,25);
	if (jsonObj.issourcenat == true) {
		firstRow+="[source nat]";
	}
    $midmenuItem1.find("#first_row").text(firstRow); 
    $midmenuItem1.find("#second_row").text("owned by: "+fromdb(jsonObj.account).substring(0,25));    
}

function isIpManageable(domainid, account) {             
    if(((g_domainid == domainid && g_account == account) || (g_domainid == domainid && isDomainAdmin()) || (isAdmin())) 
        && account!="system") 
        return true;
    else
        return false;
}    
<commit_msg>bug 8413: IP Address page - fix a bug that domain admin can't see PF/LB tabs of Source NAT IP created by sub-domain user.<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/>.
 * 
 */

function ipGetSearchParams() {
    var moreCriteria = [];	

	var searchInput = $("#basic_search").find("#search_input").val();	 
    if (searchInput != null && searchInput.length > 0) {	           
        moreCriteria.push("&keyword="+todb(searchInput));	       
    }     

	var $advancedSearchPopup = getAdvancedSearchPopupInSearchContainer();
	if ($advancedSearchPopup.length > 0 && $advancedSearchPopup.css("display") != "none" ) {		

		var zone = $advancedSearchPopup.find("#adv_search_zone").val();	
	    if (zone!=null && zone.length > 0) 
			moreCriteria.push("&zoneId="+zone);	
				        
        if ($advancedSearchPopup.find("#adv_search_domain_li").css("display") != "none") {		
		    var domainId = $advancedSearchPopup.find("#adv_search_domain").val();		
		    if (domainId!=null && domainId.length > 0) 
			    moreCriteria.push("&domainid="+domainId);	
    	}	
    	
		if ($advancedSearchPopup.find("#adv_search_account_li").css("display") != "none" 
    	    && $advancedSearchPopup.find("#adv_search_account").hasClass("textwatermark") == false) {	
		    var account = $advancedSearchPopup.find("#adv_search_account").val();		
		    if (account!=null && account.length > 0) 
			    moreCriteria.push("&account="+account);		
		}	
	} 	
	
	return moreCriteria.join("");          
}

function afterLoadIpJSP() {
    //***** switch between different tabs (begin) ********************************************************************
    var tabArray = [$("#tab_details"), $("#tab_port_range"), $("#tab_port_forwarding"), $("#tab_load_balancer"), $("#tab_vpn")];
    var tabContentArray = [$("#tab_content_details"), $("#tab_content_port_range"), $("#tab_content_port_forwarding"), $("#tab_content_load_balancer"), $("#tab_content_vpn")];
    var afterSwitchFnArray = [ipJsonToDetailsTab, ipJsonToPortRangeTab, ipJsonToPortForwardingTab, ipJsonToLoadBalancerTab, ipJsonToVPNTab];
    switchBetweenDifferentTabs(tabArray, tabContentArray, afterSwitchFnArray);       
    //***** switch between different tabs (end) **********************************************************************
        
    //dialogs
    initDialog("dialog_acquire_public_ip", 325);
	initDialog("dialog_enable_vpn");
	initDialog("dialog_disable_vpn");
	initDialog("dialog_add_vpnuser");
	initDialog("dialog_confirmation_remove_vpnuser");
	initDialog("dialog_enable_static_NAT");
    
    //*** Acquire New IP (begin) ***
	$.ajax({
	    data: createURL("command=listZones&available=true"),
		dataType: "json",
		success: function(json) {
			var zones = json.listzonesresponse.zone;				
			var zoneSelect = $("#dialog_acquire_public_ip #acquire_zone").empty();	
			if (zones != null && zones.length > 0) {	
			    for (var i = 0; i < zones.length; i++) {
				    zoneSelect.append("<option value='" + zones[i].id + "'>" + fromdb(zones[i].name) + "</option>"); 
			    }
		    }
		}
	});
	    
    $("#acquire_new_ip_button").unbind("click").bind("click", function(event) {  
		var submenuContent = $("#submenu_content_network");
		$("#dialog_acquire_public_ip").dialog('option', 'buttons', {				
			"Acquire": function() { 
				var thisDialog = $(this);	
				thisDialog.dialog("close");
						
				var zoneid = thisDialog.find("#acquire_zone").val();				
				
				var $midmenuItem1 = beforeAddingMidMenuItem() ;	
				
				$.ajax({
				    data: createURL("command=associateIpAddress&zoneid="+zoneid),
				    dataType: "json",
				    success: function(json) {						        
				        var jobId = json.associateipaddressresponse.jobid;				        
				        var timerKey = "associateIpJob_"+jobId;
							    
				        $("body").everyTime(2000, timerKey, function() {
						    $.ajax({
							    data: createURL("command=queryAsyncJobResult&jobId="+jobId),
							    dataType: "json",
							    success: function(json) {										       						   
								    var result = json.queryasyncjobresultresponse;
								    if (result.jobstatus == 0) {
									    return; //Job has not completed
								    } else {											    
									    $("body").stopTime(timerKey);
									    if (result.jobstatus == 1) {
										    // Succeeded										    							   
										    ipToMidmenu(result.jobresult.ipaddress, $midmenuItem1);
						                    bindClickToMidMenu($midmenuItem1, ipToRightPanel, ipGetMidmenuId);  
						                    afterAddingMidMenuItem($midmenuItem1, true);	                            
									    } else if (result.jobstatus == 2) {
									        afterAddingMidMenuItem($midmenuItem1, false, fromdb(result.jobresult.errortext));					        							        								   				    
									    }
								    }
							    },
							    error: function(XMLHttpResponse) {
								    $("body").stopTime(timerKey);
									handleError(XMLHttpResponse, function() {
										afterAddingMidMenuItem($midmenuItem1, false, parseXMLHttpResponse(XMLHttpResponse));
									});
							    }
						    });
					    }, 0);						    					
				    },
				    error: function(XMLHttpResponse) {
						handleError(XMLHttpResponse, function() {
							afterAddingMidMenuItem($midmenuItem1, false, parseXMLHttpResponse(XMLHttpResponse));	
						});
				    }
			    });
			},
			"Cancel": function() { 
				$(this).dialog("close"); 
			}
		}).dialog("open");			
		return false;
	});
    //*** Acquire New IP (end) ***
    
    //*** Port Range tab (begin) ***
    var $createPortRangeRow = $("#tab_content_port_range").find("#create_port_range_row");     
  
    $createPortRangeRow.find("#add_link").bind("click", function(event){	        
		var isValid = true;							
		isValid &= validateInteger("Start Port", $createPortRangeRow.find("#start_port"), $createPortRangeRow.find("#start_port_errormsg"), 1, 65535);
		isValid &= validateInteger("End Port", $createPortRangeRow.find("#end_port"), $createPortRangeRow.find("#end_port_errormsg"), 1, 65535);				
		if (!isValid) 
		    return;			
	    
	    var $template = $("#port_range_template").clone();
	    $("#tab_content_port_range #grid_content").append($template.show());		
	    
	    var $spinningWheel = $template.find("#row_container").find("#spinning_wheel");	
	    $spinningWheel.find("#description").text(g_dictionary["label.adding.processing"]);	
        $spinningWheel.show();   
	    	    
	    var $midmenuItem1 = $("#right_panel_content").data("$midmenuItem1");       
        var ipObj = $midmenuItem1.data("jsonObj");          	        
		
		var startPort = $createPortRangeRow.find("#start_port").val();
	    var endPort = $createPortRangeRow.find("#end_port").val();
	    var protocol = $createPortRangeRow.find("#protocol").val();
	   	    
	    var array1 = [];
        array1.push("&ipaddressid="+ipObj.id);           
        array1.push("&startPort="+startPort);
        array1.push("&endPort="+endPort);
        array1.push("&protocol="+protocol);
                
        $.ajax({
            data: createURL("command=createIpForwardingRule"+array1.join("")),
            dataType: "json",           
            success: function(json) {                                    	                        
                var jobId = json.createipforwardingruleresponse.jobid;                  			                        
                var timerKey = "<KEY> + jobId;					                       
                $("body").everyTime(
                    10000,
                    timerKey,
                    function() {
                        $.ajax({
                            data: createURL("command=queryAsyncJobResult&jobId="+jobId),
	                        dataType: "json",									                    					                    
	                        success: function(json) {		                                              							                       
		                        var result = json.queryasyncjobresultresponse;										                   
		                        if (result.jobstatus == 0) {
			                        return; //Job has not completed
		                        } else {											                    
			                        $("body").stopTime(timerKey);				                        
			                        $spinningWheel.hide();  			                                 		                       
			                        if (result.jobstatus == 1) { // Succeeded 				                            
	                                    var item = json.queryasyncjobresultresponse.jobresult.ipforwardingrule;		       	        	
	                                    portRangeJsonToTemplate(item, $template);
	                                    $spinningWheel.hide();   
	                                    refreshCreatePortRangeRow();			 
			                        } else if (result.jobstatus == 2) { // Failed			                            
				                        $template.slideUp("slow", function() {
					                        $(this).remove();
				                        });
				                        //var errorMsg = g_dictionary["label.failed"] + " - " + g_dictionary["label.error.code"] + " " + fromdb(result.jobresult.errorcode);
	                                    var errorMsg = g_dictionary["label.failed"] + " - " + fromdb(result.jobresult.errortext);	
	                                    $("#dialog_error").text(errorMsg).dialog("open");
			                        }											                    
		                        }
	                        },
	                        error: function(XMLHttpResponse) {	                  
		                        $("body").stopTime(timerKey);	
								handleError(XMLHttpResponse, function() {
					                $template.slideUp("slow", function() {
						                $(this).remove();
					                });
					                var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
		                            $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
				                });								
	                        }
                        });
                    },
                    0
                );
            },
            error: function(XMLHttpResponse) {	
                handleError(XMLHttpResponse, function() {
	                $template.slideUp("slow", function() {
		                $(this).remove();
	                });
	                var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
                    $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
                });		
            }
        });    	    
	    
	    return false;
	});
    //*** Port Range tab (end) ***
   
    //*** Port Forwarding tab (begin) ***
    var $createPortForwardingRow = $("#tab_content_port_forwarding").find("#create_port_forwarding_row");     
    
    $createPortForwardingRow.find("#add_link").bind("click", function(event){	        
		var isValid = true;		
		isValid &= validateDropDownBox("Instance", $createPortForwardingRow.find("#vm"), $createPortForwardingRow.find("#vm_errormsg"));				
		isValid &= validateInteger("Public Port", $createPortForwardingRow.find("#public_port"), $createPortForwardingRow.find("#public_port_errormsg"), 1, 65535);
		isValid &= validateInteger("Private Port", $createPortForwardingRow.find("#private_port"), $createPortForwardingRow.find("#private_port_errormsg"), 1, 65535);				
		if (!isValid) 
		    return;			
	    
	    var $template = $("#port_forwarding_template").clone();
	    $("#tab_content_port_forwarding #grid_content").append($template.show());		
	    
	    var $spinningWheel = $template.find("#row_container").find("#spinning_wheel");	
	    $spinningWheel.find("#description").text(g_dictionary["label.adding.processing"]);	
        $spinningWheel.show();   
	    	    
	    var $midmenuItem1 = $("#right_panel_content").data("$midmenuItem1");       
        var ipObj = $midmenuItem1.data("jsonObj");            	        
				
	    var publicPort = $createPortForwardingRow.find("#public_port").val();
	    var privatePort = $createPortForwardingRow.find("#private_port").val();
	    var protocol = $createPortForwardingRow.find("#protocol").val();
	    var virtualMachineId = $createPortForwardingRow.find("#vm").val();		   
	    		    
	    var array1 = [];
        array1.push("&ipaddressid="+ipObj.id);           
        array1.push("&publicport="+publicPort);
        array1.push("&privateport="+privatePort);
        array1.push("&protocol="+protocol);
        array1.push("&virtualmachineid=" + virtualMachineId);
        
        $.ajax({
            data: createURL("command=createPortForwardingRule"+array1.join("")),
            dataType: "json",           
            success: function(json) {	                               	                        
                var jobId = json.createportforwardingruleresponse.jobid;                  			                        
                var timerKey = "<KEY> + jobId;					                       
                $("body").everyTime(
                    10000,
                    timerKey,
                    function() {
                        $.ajax({
                            data: createURL("command=queryAsyncJobResult&jobId="+jobId),
	                        dataType: "json",									                    					                    
	                        success: function(json) {		                                              							                       
		                        var result = json.queryasyncjobresultresponse;										                   
		                        if (result.jobstatus == 0) {
			                        return; //Job has not completed
		                        } else {											                    
			                        $("body").stopTime(timerKey);				                        
			                        $spinningWheel.hide();  			                                 		                       
			                        if (result.jobstatus == 1) { // Succeeded 	
	                                    var item = json.queryasyncjobresultresponse.jobresult.portforwardingrule;		       	        	
	                                    portForwardingJsonToTemplate(item,$template);
	                                    $spinningWheel.hide();   
	                                    refreshCreatePortForwardingRow();			 
			                        } else if (result.jobstatus == 2) { // Failed			                            
				                        $template.slideUp("slow", function() {
					                        $(this).remove();
				                        });
				                        //var errorMsg = g_dictionary["label.failed"] + " - " + g_dictionary["label.error.code"] + " " + fromdb(result.jobresult.errorcode);
	                                    var errorMsg = g_dictionary["label.failed"] + " - " + fromdb(result.jobresult.errortext);	
	                                    $("#dialog_error").text(errorMsg).dialog("open");
			                        }											                    
		                        }
	                        },
	                        error: function(XMLHttpResponse) {	                  
		                        $("body").stopTime(timerKey);	
								handleError(XMLHttpResponse, function() {
					                $template.slideUp("slow", function() {
						                $(this).remove();
					                });
					                var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
		                            $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
				                });								
	                        }
                        });
                    },
                    0
                );
            },
            error: function(XMLHttpResponse) {	
                handleError(XMLHttpResponse, function() {
	                $template.slideUp("slow", function() {
		                $(this).remove();
	                });
	                var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
                    $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
                });		
            }
        });    	    
	    
	    return false;
	});
    //*** Port Forwarding tab (end) ***
    
    //*** Load Balancer tab (begin) ***
    var createLoadBalancerRow = $("#tab_content_load_balancer #create_load_balancer_row");
    
    createLoadBalancerRow.find("#add_link").bind("click", function(event){		
	    // validate values		    
		var isValid = true;					
		isValid &= validateString("Name", createLoadBalancerRow.find("#name"), createLoadBalancerRow.find("#name_errormsg"));
		isValid &= validateInteger("Public Port", createLoadBalancerRow.find("#public_port"), createLoadBalancerRow.find("#public_port_errormsg"), 1, 65535);
		isValid &= validateInteger("Private Port", createLoadBalancerRow.find("#private_port"), createLoadBalancerRow.find("#private_port_errormsg"), 1, 65535);				
		if (!isValid) return;
		 
		var $template = $("#load_balancer_template").clone();	
		$("#tab_content_load_balancer #grid_content").append($template.show());		
		
		var $spinningWheel = $template.find("#row_container").find("#spinning_wheel");	
	    $spinningWheel.find("#description").text(g_dictionary["label.adding.processing"]);	
        $spinningWheel.show();            			 
		 			
		var $midmenuItem1 = $("#right_panel_content").data("$midmenuItem1");        
        var ipObj = $midmenuItem1.data("jsonObj");
       	 
	    var name = createLoadBalancerRow.find("#name").val();  
	    var publicPort = createLoadBalancerRow.find("#public_port").val();
	    var privatePort = createLoadBalancerRow.find("#private_port").val();
	    var algorithm = createLoadBalancerRow.find("#algorithm_select").val();  
	    		   
	    var array1 = [];
        array1.push("&publicipid="+ipObj.id);    
        array1.push("&name="+todb(name));              
        array1.push("&publicport="+publicPort);
        array1.push("&privateport="+privatePort);
        array1.push("&algorithm="+algorithm);
       
        $.ajax({
	        data: createURL("command=createLoadBalancerRule"+array1.join("")),
			dataType: "json",
			success: function(json) {	
				var item = json.createloadbalancerruleresponse.loadbalancer;				
	            loadBalancerJsonToTemplate(item, $template);
	            $spinningWheel.hide();   
	            refreshCreateLoadBalancerRow();	            	
			},
		    error: function(XMLHttpResponse) {				    
			    handleError(XMLHttpResponse, function() {
					$template.slideUp("slow", function() {
						$(this).remove();
					});
					var errorMsg = parseXMLHttpResponse(XMLHttpResponse);				
		            $("#dialog_error").text(fromdb(errorMsg)).dialog("open");
				});
		    }			
		});  	    
	    return false;
	});
	//*** Load Balancer tab (end) ***
}

function ipGetMidmenuId(jsonObj) {   
    return ipGetMidmenuId2(jsonObj.ipaddress);
}

function ipGetMidmenuId2(ipaddress) {  
    return "midmenuItem_" + ipaddress.replace(/\./g, "_");   //e.g. "192.168.33.108" => "192_168_33_108"
}

function ipToMidmenu(jsonObj, $midmenuItem1) {    
    var id = ipGetMidmenuId(jsonObj);
    $midmenuItem1.attr("id", id);  
    $midmenuItem1.data("jsonObj", jsonObj); 
    
    var $iconContainer = $midmenuItem1.find("#icon_container").show();
    $iconContainer.find("#icon").attr("src", "images/midmenuicon_network_networkgroup.png");
    
	var firstRow = jsonObj.ipaddress.substring(0,25);
	if (jsonObj.issourcenat == true) {
		firstRow+="[source nat]";
	}
    $midmenuItem1.find("#first_row").text(firstRow); 
    $midmenuItem1.find("#second_row").text("owned by: "+fromdb(jsonObj.account).substring(0,25));    
}

function isIpManageable(domainid, account) {             
    if(((g_domainid == domainid && g_account == account) || (isDomainAdmin()) || (isAdmin())) 
        && account!="system") 
        return true;
    else
        return false;
}    

{'fraction_non_alphanumeric': 0.13707984460584416, 'fraction_numerical': 0.007335376299978284, 'mean_word_length': 1.7471828185072253, 'pattern_counts': {'":': 4, '<': 14, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6419746', 'n_tokens_mistral': 13096, 'n_tokens_neox': 10673, 'n_words': 1684}