/**
 * Global variables
 */
var helpTab; // Help tabs
var ivpChoices = new Array;
var ivpChoiceValues = new Array;
var selectInfo = new Object();
/**
 * Load the help page and create the tabs.
 */
function loadHelpPage(){
    createHelpTab();
    createVerifyXCATTab();
}
/**
 * Create the Help Tab
 *
 * @return Tab object
 */
function createHelpTab(){
    // Create help tab
    var tab = new Tab();
    setHelpTab(tab);
    tab.init();
    $('#content').append(tab.object());
    var helpForm = $('
');
    helpForm.append(
        '' +
            'Quick Start ' +
            '' +
            '' +
                '1. View defined nodes 2. Manage operating system images 3. Provision nodes 4. Manage and provision storage and networks  ' +
        ' ' +
        '' +
            'Settings ' +
            '' +
            '' +
                'a. Manage and control user access b. Edit the xCAT database tables  ' +
        ' ');
    tab.add('helpTab', 'Help', helpForm, false);
}
/**
 * Create the Verify xCAT Tab
 *
 * @return Tab object
 */
function createVerifyXCATTab() {
    var comments = 'Description of the IVP run';
    var ivpEnabled = 'checked';
    var ivpDisabled = '';
    // Get the tab
    var tab = getHelpTab();
    var fs, legend;
    // Generate new tab ID
    var instance = 0;
    var newTabId = 'verifyXCATTab' + instance;
    while ($('#' + newTabId).length) {
        // If one already exists, generate another one
        instance = instance + 1;
        newTabId = 'verifyXCATTab' + instance;
    }
    // Build the list of IVPs in the table
    readIvpTable();
    // Create info bar and status bar
    var infoBar = createInfoBar( 'Run or schedule Installation Verification Procedures to verify:
' );
    verifyXCATForm.append( infoBar, statBar );
    // Create 'Create a Verify xCAT' fieldset
    fs = $( 'Verify: ' ));
    fs.append( $( 'ID of Run: '
                    + ''
                    + ' '
                  + '
' ));
    fs.append( $('Type of IVP Run: 
'));
    fs.append( $('
' ));
    fs.append( $('
' ));
    fs.append( $('Basic and Full IVP Parameters: 
'));
    fs.append( $('Orchestrator Script Parameters: 
' ));
    fs.append( $('Main IVP Script Parameters: 
' ));
    fs.append( $('Full IVP Parameters: 
'));
    fs.append( $('OpenStack System IP Address: 
' ));
    fs.append( $('OpenStack user: 
' ));
    fs.append( $('Preparation Script Parameters: 
' ));
    fs.append( $('Automated IVP Parameters: 
'));
    fs.append( $('Automated IVP Comments: 
' ));
    fs.append( '');
    fs.append( $('Disable or Enable the IVP Run:
'));
    fs.append( $('
' ));
    fs.append( $('
' ));
    verifyXCATForm.append( fs );
    var verifyBtn = createButton( 'Run an IVP Now' );
    verifyBtn.click(function() {
        var driveFunction = 1;
        var argList = '';
        // Remove any warning messages
        $(this).parents('.ui-tabs-panel').find('.ui-state-error').remove();
        var runType = $(this).parent().find('input[name="runType"]:checked').val();
        if ( runType == 'verifyBasic' ) {
            argList += '||--basicivp';
        } else if ( runType == 'verifyOpenStack' ) {
            argList += '||--fullivp';
            var openstackIP = $(this).parent().find('input[name=openstackIP]').val();
            if ( openstackIP != '' ) {
                argList += '||--openstackip ' + openstackIP;
            } else {
                // Show an information message.
                $('#' + statBarId).find('div').append(
                    'You did not specify the IP address of the OpenStack system.  The IVP ' +
                    'will use the IP address of the system running the xCAT management node as ' +
                    'the OpenStack IP address.' );
            $('#' + statBarId).find('img').show();
            $.ajax( {
                url : 'lib/cmd.php',
                dataType : 'json',
                data : {
                    cmd  : 'webrun',
                    tgt  : '',
                    args : 'verifynode '+ argList,
                    msg  : 'out=' + statBarId + ';cmd=verifynode'
                },
                success : function(data) {
                    updateStatusBar(data);
                    readIvpTable();
                }
            });
            // Show status bar
            statBar.show();
        }
    });
    verifyXCATForm.append( scheduleBtn );
    var removeBtn = createButton( 'Remove an IVP Run' );
    removeBtn.click(function() {
        var driveFunction = 1;
        var argList = '';
        // Remove any warning messages
        $(this).parents('.ui-tabs-panel').find('.ui-state-error').remove();
        var ivpId = $(this).parent().find('select[name=ivpId]').val();
        if ( ivpId != 'NEW' ) {
            argList = '||--remove' + '||--id ' + ivpId + '||end';
        } else {
            // Show warning message
            var warn = createWarnBar('You did not select the ID of an existing run.');
            warn.prependTo($(this).parents('.ui-tabs-panel'));
            driveFunction = 0;
        }
        if ( driveFunction == 1 ) {
            $('#' + statBarId).find('div').append( 'Invoking verifynode to remove the IVP.' + ivpChoices[i] + ' ';
    }
    // Find the division containing the select and replace its contents
    var thisTabId = $(this).parents('.tab').attr('id');
    var thisIvpSelect = $( '#' + thisTabId + ' select[name=ivpId]' );
    thisIvpSelect.children().remove();
    thisIvpSelect.append( selectIdOptions );
}
/**
 * Set node tab
 *
 * @param tab
 *            Tab object
 * @return Nothing
 */
function setHelpTab(tab) {
    helpTab = tab;
}
/**
 * Set IVP variables based on the chosen Id
 *
 * @param data Data from HTTP request
 */
function setVarsForId( selected ) {
    var id = selected.value;
    // Change the form fields based on the selected ID.
    var thisTabId = $(this).parents('.tab').attr('id');
    var thisField = $( '#' + thisTabId + ' input[name="runType"]' );
    if ( selectInfo[id]['typeOfRun'] == 'basicivp' ) {
        thisField.val(['verifyBasic']);
    } else if ( selectInfo[id]['typeOfRun'] == 'fullivp' ) {
        thisField.val(['verifyOpenStack']);
    } else {
        var warn = createWarnBar('IVP with the id of '+id+' has an unrecognized type of run value: '+selectInfo[id]['typeOfRun']);
        warn.prependTo($(this).parents('.ui-tabs-panel'));
    }
    thisField = $( '#' + thisTabId + ' input[name=orchParms]' );
    thisField.val( selectInfo[id]['orchParms'] );
    thisField = $( '#' + thisTabId + ' input[name=prepParms]' );
    thisField.val( selectInfo[id]['prepParms'] );
    var thisfield = $( '#' + thisTabId + ' input[name=mainParms]' );
    thisfield.val( selectInfo[id]['mainParms'] );
    thisField = $( '#' + thisTabId + ' input[name=openstackIP]' );
    thisField.val( selectInfo[id]['ip'] );
    thisField = $( '#' + thisTabId + ' input[name=openstackUser]' );
    thisField.val( selectInfo[id]['openstackUser'] );
    var hours = new Object();
    var fullDay = 1;
    var hour = selectInfo[id]['schedule'].split(' ');
    for ( var j = 0; j < hour.length; j++ ) {
        hours[hour[j]] = 1;
    }
    for (var i = 0; i <= 23; i++) {
        thisField = $( '#' + thisTabId + ' input[name=ivpSchedule][value='+i+']' );
        if ( hours[i] == 1 ) {
            thisField.attr( 'checked', true );
        } else {
            fullDay = 0;
            thisField.attr( 'checked', false );
        }
    }
    if ( fullDay == 1 ) {
        thisField = $( '#' + thisTabId + ' input[name=ivpSchedule][value=Every hour]' );
        thisField.attr( 'checked', true );
        for (var i = 0; i <= 23; i++) {
            thisField = $( '#' + thisTabId + ' input[name=ivpSchedule][value='+i+']' );
            thisField.attr( 'checked', false );
        }
    }
    thisField = $( '#' + thisTabId + ' input[name=comments]' );
    thisField.val( selectInfo[id]['comments'] );
    thisField = $( '#' + thisTabId + ' input[name=disable]' );
    if ( selectInfo[id]['disable'] == 1 || selectInfo[id]['disable'] == 'yes' ) {
        thisField.val(['disabled']);
    } else if ( selectInfo[id]['disable'] == '' || selectInfo[id]['disable'] == 0 ) {
        thisField.val(['enabled']);
    } else {
        var warn = createWarnBar('IVP with the id of '+id+' has an unrecognized disable value: '+selectInfo[id]['disable']);
        warn.prependTo($(this).parents('.ui-tabs-panel'));
    }
}
/**
 * Update status bar of a given tab
 *
 * @param data Data returned from HTTP request
 */
function updateStatusBar(data) {
    // Get ajax response
    var rsp = data.rsp;
    var args = data.msg.split(';');
    var statBarId = '';
    var cmd = '';
    for ( var i=0; i < args.length; i++ ) {
        if ( args[i].match('^cmd=') ) {
            cmd = args[i].replace('cmd=', '');
        } else if ( args[i].match('^out=') ) {
            statBarId = args[i].replace('out=', '');
        }
    }
    if (cmd == 'verifynode') {
        // Hide loader
        $('#' + statBarId).find('img').hide();
        // Write ajax response to status bar
        var prg = writeRsp(rsp, '');
        $('#' + statBarId).find('div').append(prg);
        // Put a check box after the response.
        var icon = $('