// Javascript functions
function injs() {	
		jQuery('ul.sf-menu').superfish();
		// got this next part from:
		// http://nettuts.com/javascript-ajax/how-to-load-in-and-animate-content-with-jquery/	
		// Check for hash value in URL
 		var hash = window.location.hash.substr(1);
		var fullLoc = hash;
		//alert(hash);
		// check to see if there is a query of it.
		if(hash.indexOf("?") !=-1){
			hash = 	hash.slice(0,hash.indexOf("?"));
		//	alert(hash);
		}
		var href = $('#sf-menu li a').each(function(){
			var href = $(this).attr('href');
			// alert(href + " = " + hash + "?");
			if(hash==href){
				var toLoad = fullLoc;
				$('#main').load(toLoad)
				// change the document title
				var subM = href.slice(0,href.indexOf(".php"));
				document.title = "xCAT: " + subM;
    			} 
		});
		// if no page is specified load the default main page.
		if(hash == false ){
			$('#main').load('main.php');
			document.title = "xCAT Control Center";
		}
    $('#sf-menu li a').click(function(){
			var toLoad = $(this).attr('href');
			$('#main').hide('fast',loadContent);
			$('#load').remove();
			$('#wrapper').append('LOADING...');
			$('#load').fadeIn('normal');
			// update the location
			// window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
			window.location.hash = $(this).attr('href');
			// update the title
			document.title = "xCAT: " + $(this).attr('href').slice(0,$(this).attr('href').indexOf(".php"));
			function loadContent() {
				$('#main').load(toLoad,'',showNewContent())
			}
			function showNewContent() {
    		$('#main').show('normal',hideLoader());
			}
			function hideLoader() {
				$('#load').fadeOut('normal');
			}
			return false;
		});
		// code for processing form
		var options = {
			target: '#main',
			url: 'command.php'
		}
		$('#cmdForm').hover(function(){
			$(this).css("background", "url(img/cmd-active.png) no-repeat")
			},function(){
			$(this).css("background", "url(img/cmd.png) no-repeat")
		});
		$('#cmdForm').ajaxForm(options);
		$('#cmd').focus(function() {
			this.value	= "";
		});
}
function loadConfigTab(tab) {
	// if they don't add a table definition, just go to the
	// main page.
	if(tab === undefined){
		document.title = "xCAT: config";
    		$('#main').load('config.php');
		window.location.hash = "config.php";
	}else{
		// update the title
		document.title = "xCAT: config " + tab;
		// update the URL
		window.location.hash = "config.php?t=" + tab;
	
		// load the page
		$('#main').load('config.php?t=' + tab);
	}
}
function controlCmd(cmd, nr){
	//var nrt = $("#nrcmdnoderange").html();
	// strip off Noderange:
	//var nr = nrt.split(" ");
	//nr = nr[1];
	$("#nrcmdnodegrange").text("Noderange: " + nr);
	$("#nrcmdcmd").text("Action: " + cmd);
	// update window command
	window.location.hash = "control.php?nr="+ nr + "&cmd=" + cmd;
	$('#rangedisplay').empty().html(' ');	
	$('#rangedisplay').load('rangeDisplay.php?t=control&nr='+nr+'&cmd='+cmd);
}
function loadMainPage(page){
	// blank the page out
	$('#main').empty().html('
');	
	$('#rangedisplay').load('rangeDisplay.php?t=control&nr='+nr+'&cmd='+cmd);
}
function loadMainPage(page){
	// blank the page out
	$('#main').empty().html(' ');	
	// change the title to the new one.
	var subM = page.slice(0,page.indexOf(".php"));
	document.title = "xCAT: " + subM;
	// load the page
	$('#main').load(page);
	// change the URL
	window.location.hash = page;
}
// call this to update the table with unique log entries.  
// we should probably be more robust cause we may miss some entries
// that happen at the same time.
function tableUpdater(count,oldEntry){
	// The first time this is called, oldEntry is nothing.
	if(oldEntry == ''){
		// this is the base date.
		oldEntry = "
');	
	// change the title to the new one.
	var subM = page.slice(0,page.indexOf(".php"));
	document.title = "xCAT: " + subM;
	// load the page
	$('#main').load(page);
	// change the URL
	window.location.hash = page;
}
// call this to update the table with unique log entries.  
// we should probably be more robust cause we may miss some entries
// that happen at the same time.
function tableUpdater(count,oldEntry){
	// The first time this is called, oldEntry is nothing.
	if(oldEntry == ''){
		// this is the base date.
		oldEntry = "
 ');	
		$('#part2').load(uri,"",
			function(responseText,textStatus,XMLHttpRequest) {
				if(textStatus == 'error'){
					$('#part2').empty();
					$('#part1').html("Looks like you need to copy the media first.  Please run copycds for " + os  + '-' + arch + '
');	
		$('#part2').load(uri,"",
			function(responseText,textStatus,XMLHttpRequest) {
				if(textStatus == 'error'){
					$('#part2').empty();
					$('#part1').html("Looks like you need to copy the media first.  Please run copycds for " + os  + '-' + arch + ' ');	
				// change the args so that we don't ask for any spaces:
				// we have to do this to encode it to the URL
				// yes, this does suck and no, I don't think
				// this function could be any more confusing.
				args = args.replace(/ /g, '+');
				$('#part2').load('command.php?nr='+nr+'&cmd=nodech&args='+args,'',
				function(responseText,textStatus,XMLHttpRequest) {
					if(textStatus != 'error'){
						$('#part2').html('Success.');
						$('#part3').html('running: nodeset '+nr+' '+meth+'');	
						$('#part4').html('
');	
				// change the args so that we don't ask for any spaces:
				// we have to do this to encode it to the URL
				// yes, this does suck and no, I don't think
				// this function could be any more confusing.
				args = args.replace(/ /g, '+');
				$('#part2').load('command.php?nr='+nr+'&cmd=nodech&args='+args,'',
				function(responseText,textStatus,XMLHttpRequest) {
					if(textStatus != 'error'){
						$('#part2').html('Success.');
						$('#part3').html('running: nodeset '+nr+' '+meth+'');	
						$('#part4').html(' ');	
						$('#part4').fadeIn('normal');	
						$('#part4').load('command.php?nr='+nr+'&cmd=nodeset&args='+meth,
							function(responseText,textStatus,XMLHttpRequest) {
								if(textStatus != 'error'){
									$('#part4').html('Success.'+responseText);
									$('#part5').html('running: rpower '+nr+' boot');
									$('#part5').fadeIn('normal');	
									$('#part6').html('
');	
						$('#part4').fadeIn('normal');	
						$('#part4').load('command.php?nr='+nr+'&cmd=nodeset&args='+meth,
							function(responseText,textStatus,XMLHttpRequest) {
								if(textStatus != 'error'){
									$('#part4').html('Success.'+responseText);
									$('#part5').html('running: rpower '+nr+' boot');
									$('#part5').fadeIn('normal');	
									$('#part6').html(' ');	
									$('#part6').fadeIn('normal');	
									$('#part6').load('command.php?nr='+nr+'&cmd=rpower&args=boot',
										function(responseText,textStatus,XMLHttpRequest) {
											if(textStatus != 'error'){
												$('#part6').html('Nodes have rebooted and should be installing...'+responseText);
											}
										}
									);
								}
							}
						);
					}else{
						$('#part2').html('There was a problem...');
					}
				}
				);
			}
		);
	} // so yeah, all these }'s and )'s really suck.  I hope you never have to
		// debug this.  If you do, please make this code easier to read.
}
//added for display the tree
// TODO: there're still issues here.
function init_ositree(){
    //display all the nodes with OSI type
    nrtree = new tree_component();  //-tree begin
    nrtree.init($("#ositree"),{
        rules: { multiple: "Ctrl" },
        ui: { animation: 250 },
        data : {
            type: "json",
            async: "true",
            url: "monitor/osi_source.php"
        }
    });
}
//function updatermcnr()
//{
//    myselection = nrtree.selected_arr;
//
//    for (node in myselection) {
//        $("#rmc_monshow").html($("#rmc_monshow").html()+node);
//    }
//
//}
//update the osi tree 
function init_rmc_ositree() {
    nrtree = new tree_component();  //-tree begin
    nrtree.init($("#rmc_tree"),{
        rules: { multiple: "Ctrl" },
        ui: { animation: 250 },
        callback: {
            onchange: function(n) {
                $("#monshow_tip_1").hide();
                if(n.id) {
                    //if($(n).parent().parent().attr("id") == ",lpar") {
                    //parse the id, then display the "monshow" data for selected noderange
                    $.get("monitor/rmc_monshow_attr_source.php", {id: n.id}, function(data) {
                        //display the "monshow" result
                        $("#monshow_opt").html(data);
                    });
                    //}
                }
            }
        },
        //http://jstree.com/reference/_examples/3_callbacks.html
        //onchange is used to
        data : {
            type: "json",
            async: "true",
            url: "monitor/rmc_source.php"
        }
    });
}
//node_stat_control() can enable/disable nodestatmon for the selected plugin
function node_stat_control(plugin)
{
    //get the label of the button
    var action = $("#node_stat span").text();
    if(action=='Enable') {
        //enable node_stat_monitor
        $.get("monitor/control_node_stat.php",{name:plugin, action:"enable"},function(data) {
            if(data=='successful') {
                //change the label to "Disable"
                $("#node_stat span").text("Disable");
            }
        });
    }else if(action=='Disable') {
        //disable node_stat_monitor
        $.get("monitor/control_node_stat.php",{name:plugin, action:"disable"},function(data) {
            if(data=='successful') {
                //change the label to "enable"
                $("#node_stat span").text("Enable");
            }
        })
        //then, change the label to "Enable""
    }
}
function goto_next()
//TODO: change the function name! it's too silly now!
{
    var str = location.href;
    //TODO:one bug is here.
    var plugin=str.slice(str.indexOf("name")+5);//get the argument from "?name=xxxxx"
    if(plugin == "rmcmon") {
        loadMainPage("monitor/rmc_event_define.php");
    }else {
        //TODO
        //for the others, there's no web page to define evnets/performance now'
        loadMainPage("monitor/monstart.php?name="+plugin);
    }
}
function mkCondResp()
{
    //get the name of the selected condition
    //then, get the response in "checked" status
    //then, run the command "mkcondresp"
    var cond_val = $('input[@name=conditions][@checked]').val();
    if(cond_val) {
        //get the response in "checked" status
        var resps_obj = $('input[@name=responses][@checked]');
        if(resps_obj) {
            $.each(resps_obj,function(i,n) {
                //i is the index
                //n is the content
                //TODO:add one new php file to handle "mkcondresp" command
                $.get("monitor/makecondresp.php", {cond: cond_val, resp: n.value}, function(data) {
                    $("#devstatus").html(data);
                });
            });
        $("#association").load("monitor/updateCondRespTable.php");
        }
    }
}
function control_RMCAssoc(cond, node, resp, action)
{
    //TODO:for define_rmc_event
    //control the RMC Association: startcondresp & stopcondresp;
    $.get("monitor/updateCondResp.php", 
        {c: cond, n: node, r: resp, a: action},
        function(data) {
            $("#association").html(data);
        }
    );
}
function clearEventDisplay()
{
    $('input[@name=conditions][@checked]').attr('checked', false);
    $('input[@name=responses][@checked]').attr('checked', false);
}
function showRMCAttrib()
{
    var class_val = $('input[@name=classGrp][@checked]').val();
    if(class_val) {
        $.get("monitor/rmc_resource_attr.php", {name: class_val}, function(data) {
            $("#rmcScrAttr").html(data);
        });
    }
}
function showPluginOptions()
{
    //for monlist.php, when the user clicks the radiobox, the available options for the plugin will display
    $("input[@name=plugins]").click(function() {
        //when one radiobox is selected, the #options
');	
									$('#part6').fadeIn('normal');	
									$('#part6').load('command.php?nr='+nr+'&cmd=rpower&args=boot',
										function(responseText,textStatus,XMLHttpRequest) {
											if(textStatus != 'error'){
												$('#part6').html('Nodes have rebooted and should be installing...'+responseText);
											}
										}
									);
								}
							}
						);
					}else{
						$('#part2').html('There was a problem...');
					}
				}
				);
			}
		);
	} // so yeah, all these }'s and )'s really suck.  I hope you never have to
		// debug this.  If you do, please make this code easier to read.
}
//added for display the tree
// TODO: there're still issues here.
function init_ositree(){
    //display all the nodes with OSI type
    nrtree = new tree_component();  //-tree begin
    nrtree.init($("#ositree"),{
        rules: { multiple: "Ctrl" },
        ui: { animation: 250 },
        data : {
            type: "json",
            async: "true",
            url: "monitor/osi_source.php"
        }
    });
}
//function updatermcnr()
//{
//    myselection = nrtree.selected_arr;
//
//    for (node in myselection) {
//        $("#rmc_monshow").html($("#rmc_monshow").html()+node);
//    }
//
//}
//update the osi tree 
function init_rmc_ositree() {
    nrtree = new tree_component();  //-tree begin
    nrtree.init($("#rmc_tree"),{
        rules: { multiple: "Ctrl" },
        ui: { animation: 250 },
        callback: {
            onchange: function(n) {
                $("#monshow_tip_1").hide();
                if(n.id) {
                    //if($(n).parent().parent().attr("id") == ",lpar") {
                    //parse the id, then display the "monshow" data for selected noderange
                    $.get("monitor/rmc_monshow_attr_source.php", {id: n.id}, function(data) {
                        //display the "monshow" result
                        $("#monshow_opt").html(data);
                    });
                    //}
                }
            }
        },
        //http://jstree.com/reference/_examples/3_callbacks.html
        //onchange is used to
        data : {
            type: "json",
            async: "true",
            url: "monitor/rmc_source.php"
        }
    });
}
//node_stat_control() can enable/disable nodestatmon for the selected plugin
function node_stat_control(plugin)
{
    //get the label of the button
    var action = $("#node_stat span").text();
    if(action=='Enable') {
        //enable node_stat_monitor
        $.get("monitor/control_node_stat.php",{name:plugin, action:"enable"},function(data) {
            if(data=='successful') {
                //change the label to "Disable"
                $("#node_stat span").text("Disable");
            }
        });
    }else if(action=='Disable') {
        //disable node_stat_monitor
        $.get("monitor/control_node_stat.php",{name:plugin, action:"disable"},function(data) {
            if(data=='successful') {
                //change the label to "enable"
                $("#node_stat span").text("Enable");
            }
        })
        //then, change the label to "Enable""
    }
}
function goto_next()
//TODO: change the function name! it's too silly now!
{
    var str = location.href;
    //TODO:one bug is here.
    var plugin=str.slice(str.indexOf("name")+5);//get the argument from "?name=xxxxx"
    if(plugin == "rmcmon") {
        loadMainPage("monitor/rmc_event_define.php");
    }else {
        //TODO
        //for the others, there's no web page to define evnets/performance now'
        loadMainPage("monitor/monstart.php?name="+plugin);
    }
}
function mkCondResp()
{
    //get the name of the selected condition
    //then, get the response in "checked" status
    //then, run the command "mkcondresp"
    var cond_val = $('input[@name=conditions][@checked]').val();
    if(cond_val) {
        //get the response in "checked" status
        var resps_obj = $('input[@name=responses][@checked]');
        if(resps_obj) {
            $.each(resps_obj,function(i,n) {
                //i is the index
                //n is the content
                //TODO:add one new php file to handle "mkcondresp" command
                $.get("monitor/makecondresp.php", {cond: cond_val, resp: n.value}, function(data) {
                    $("#devstatus").html(data);
                });
            });
        $("#association").load("monitor/updateCondRespTable.php");
        }
    }
}
function control_RMCAssoc(cond, node, resp, action)
{
    //TODO:for define_rmc_event
    //control the RMC Association: startcondresp & stopcondresp;
    $.get("monitor/updateCondResp.php", 
        {c: cond, n: node, r: resp, a: action},
        function(data) {
            $("#association").html(data);
        }
    );
}
function clearEventDisplay()
{
    $('input[@name=conditions][@checked]').attr('checked', false);
    $('input[@name=responses][@checked]').attr('checked', false);
}
function showRMCAttrib()
{
    var class_val = $('input[@name=classGrp][@checked]').val();
    if(class_val) {
        $.get("monitor/rmc_resource_attr.php", {name: class_val}, function(data) {
            $("#rmcScrAttr").html(data);
        });
    }
}
function showPluginOptions()
{
    //for monlist.php, when the user clicks the radiobox, the available options for the plugin will display
    $("input[@name=plugins]").click(function() {
        //when one radiobox is selected, the #options Please select one or more attributes from the table
"); } } function init_rmc_monshow_back_btn() { $("#back_btn").hide(); } function rmc_monshow_back_to_opts() { //clear the