// Various functions for the BestBETs public site
// These are specific to the admin wizards
// (C) 2004 Seven Communications Ltd

// Expanding category tree
function toggleBranch(id)
    {		
    if (document.getElementById(id+"_children").style.display == "none")
        {
        // Turn div on and show a minus
        document.getElementById(id+"_children").style.display = "";
        document.getElementById(id+"_plus").innerHTML="<img src=\"/images/node_open.gif\" width=\"11\" height=\"15\" alt=\"Click to collapse node\">";
        }
    else
        {
        // Turn div off and show a plus
        document.getElementById(id+"_children").style.display = "none";
        document.getElementById(id+"_plus").innerHTML="<img src=\"/images/node_closed.gif\" width=\"11\" height=\"15\" alt=\"Click to expand node\">";
        }
    }

function setCompleted()
    {
    // Check for status 'green' (5) and enable date completed
    if (document.forms[0].elements["ddlStatus"].value == "5")
        {
        document.forms[0].elements["txtDay2"].disabled = "";
        document.forms[0].elements["ddlMonths2"].disabled = "";
        document.forms[0].elements["ddlYears2"].disabled = "";
        }
    else
        {
        document.forms[0].elements["txtDay2"].disabled = "disabled";
        document.forms[0].elements["ddlMonths2"].disabled = "disabled";
        document.forms[0].elements["ddlYears2"].disabled = "disabled";
        }
		
    // Check for status 'red' (1) and enable available checkbox
    if (document.forms[0].elements["ddlStatus"].value == "1" && document.forms[0].elements["ddlAvailable"])
        {
        document.forms[0].elements["ddlAvailable"].disabled = "";
        }
    else
        {
        document.forms[0].elements["ddlAvailable"].disabled = "disabled";
        }
    }

function addResultRow()
    {

    // Get a handle on the existing table
    var tablebody = document.getElementById("list_results");
	
	var spans = tablebody.getElementsByTagName("div");
	var table_row = spans.length;
	var cell_span;

    // Add header row if there isn't one
    if (!tablebody.getElementsByTagName("div").item(0))
        {
        addResultHeader();
        }

    // Make a new row
    var row = document.createElement("div");
      row.setAttribute("id", "row" + table_row);
	  row.setAttribute("class", "row");

    // Names of the fields
    var text = new Array("ResultOutcome_","ResultResult_");

    // Generate two cells with fields
    for (i=0; i<=1; i++)
        {
        cell_span = document.createElement("span");
        var cell_input=document.createElement("input");
          cell_input.setAttribute("type", "text");
          cell_input.setAttribute("size", "28");
          cell_input.setAttribute("name", text[i] + table_row);
        cell_span.appendChild(cell_input);
        row.appendChild(cell_span);
        }

    // Generate third cell with 'remove' link
    cell_span = document.createElement("span");
	cell_span.setAttribute('class', 'remove');
	
	/*var imgNode = document.createElement('img');
	imgNode.setAttribute('src', '/images/trash.gif');
	imgNode.setAttribute('width', '16');
	imgNode.setAttribute('height', '16');
	imgNode.setAttribute('alt', 'Delete this result');
	imgNode.setAttribute('border', '0');*/
		
	//Create Link
	var lnkNode = document.createElement('a');
	lnkNode.setAttribute('href', '#');
	lnkNode.setAttribute("href", "javascript:removeResultRow("+table_row+");");
	lnkNode.innerText = " Remove";
//	lnkNode.appendChild("Remove");	
	cell_span.appendChild(lnkNode);

    row.appendChild(cell_span);

    // Add the new row to the table
    tablebody.appendChild(row);

    // Increment row count
    table_row ++;
    }

function addResultHeader()
    {
    var tablebody=document.getElementById("list_results");
    var row=document.createElement("div");

    // Text of rows
    var text = new Array("Outcome:","Key result:","\u00a0");
    
    // Create the three cells
    for (i=0; i<=2; i++)
        {
        var cell = document.createElement("span");
        var cell_text = document.createTextNode(text[i]);
        cell.appendChild(cell_text);
        row.appendChild(cell);
        }

    // Add the new row to the table
    tablebody.appendChild(row);
    }

function removeResultRow(row_number)
{
 	//check to see if the removed results hidden box is there
	if (document.getElementById("removedResults")) 
	{
		if(document.getElementById("ResultID_"+row_number))
		{
			//it is so when we delete we add the id to the hidden variable so we know what to delete when we postback
			resultDeleteBox = document.getElementById("removedResults");
			resultDeleteBox.value += document.getElementById("ResultID_"+row_number).value+",";
		}
	}
		
	var tablebody=document.getElementById("list_results");
	
    tablebody.removeChild(document.getElementById("row"+row_number));
	
	
}

// Functions for the BET quick-jump forms on menu.pl and admin_browse.pl
function check_id (form_id, last_bet_id)
    {
    var entered_id = document.forms[form_id].elements["BetID"].value;

    // Check the BET number is vaguely valid (i.e. a number and within the range of BET IDs.)
    if (isNaN(entered_id))
        {
        alert ("Please enter a BET number.");
        return false;
        }
    if (entered_id > last_bet_id)
        {
        alert ("The BET number you've entered is not valid.\nPlease try again, or use the menu.");
        return false;
        }

    // If I'm being called from menu.pl, I need me ~action sorting
    if (form_id == "form_quick_jump")
        {
        // Get handle on my form
        var form_quick_jump=document.getElementById("form_quick_jump");
        var step = form_quick_jump.elements["~step"].value;

        // Create a new input element and set its attributes
        if (step == "progress_check" || step == "delete")
            {
            var input_action=document.createElement("INPUT");
            input_action.setAttribute("type", "hidden");
            input_action.setAttribute("name", "~action");
            if (step == "progress_check") input_action.setAttribute("value", "edit");
            if (step == "delete")         input_action.setAttribute("value", "delete");

            // Add input to form
            form_quick_jump.appendChild(input_action);
            }
        }

    // Return true to allow submission of form
    return true;
    }
	
	function submitForm(formName)
	{
		var theForm = document.getElementById(formName);
		theForm.submit()
	}
	
