/**
 * Trim the leading and trailing white spaces.
 */
function trim( str )
{
  return str.replace(/^\s+|\s+$/g, '');
}

/**
 * Uses one checkbox "selectAll" as a switch to toggle a number of
 * other checkboxes.
 */
function toggleSelectAll( selectAllId, checkboxName )
{
    selectAll = document.getElementById( selectAllId );
    checkboxes = document.getElementsByName( checkboxName );
    for( var i=0 ; i < checkboxes.length ; ++i)
        checkboxes[i].checked = selectAll.checked;
}

/**
 * Submits a form to the given URL.
 */
function submitForm( formId, url )
{
    theForm = document.getElementById( formId );
    theForm.action = url;
    theForm.submit();
}

/**
 * Goto a URL.
 */
function gotoUrl( url )
{
  window.location.href = url;
}

/**
 * Confirm before goto a URL.
 */
function confirmGoto( message, url )
{
  if( confirm(message) ) window.location.href = url;
}

/**
 * Conform before submit a form.
 */
 function confirmSubmit( message, form )
 {
   if( confirm(message) ) form.submit();
 }
 
/**
 * Checks whether at least one of the elements is selected.
 */
function checkSelected( elementName )
{
    var elements = document.getElementsByName( elementName );
    for( var i=0 ; i < elements.length ; ++i )
        if( elements[i].checked ) return true;
    return false;
}

/**
 * This function implements a simple in-place edit. textElementId
 * is the id of the element enclosing the text to be edit, e.g.
 *   <div id="textElementId">some text</div>
 * and inputElement is the text box (usually an "input" or a "textarea")
 * that is to be used to edit the text.
 */
function editInPlace( textElementId, inputElement )
{
    textElement = document.getElementById( textElementId );
    if( ! textElement || document.getElementById(inputElement.id) ) return;

    txt = textElement.childNodes.length > 0 ?
          textElement.childNodes[0].nodeValue : "";
 
    if( inputElement.nodeName == "TEXTAREA" )
      inputElement.appendChild( document.createTextNode(txt) );
    else
      inputElement.setAttribute( "value", txt );
 
    textElement.parentNode.replaceChild( inputElement, textElement );
}

/** Functions used in /instructor/viewSections.html */

function showAddSectionForm( quarter )
{
    var ajaxRequest = new AjaxRequest("showAddSectionForm.html");
    ajaxRequest.setQueryString("quarter=" + quarter);
    ajaxRequest.sendRequest();
}

function addSection( form )
{
    course = "none";
    courseSelection = form.courseSelection;
    for( var i=0 ; i < courseSelection.length ; ++i )
    {
        if( courseSelection.options[i].selected )
        {
            course = courseSelection.options[i].text;
            break;
        }
    }

    if( course != "none" )
    {
        message = "Are you sure you want to add " + course + "?";
        confirmSubmit( message, form );
    }
}

/** Functions used in /instructor/editSection.html */

function deleteSection( sectionId )
{
    message = "Are you sure you want to delete this section?";
    confirmGoto( message, "deleteSection.html?sectionId=" + sectionId );
}

function showAddInstructorForm( sectionId )
{
    var ajaxRequest = new AjaxRequest("showAddInstructorForm.html");
    ajaxRequest.setQueryString("sectionId=" + sectionId);
    ajaxRequest.sendRequest();
}

function addInstructor( form )
{
    instructor = "none";
    instructorSelection = form.instructorSelection;
    for( var i=0 ; i < instructorSelection.length ; ++i )
    {
        if( instructorSelection.options[i].selected )
        {
            instructor = instructorSelection.options[i].text;
            break;
        }
    }

    if( instructor != "none" )
    {
        message = "Are you sure you want to add " + instructor
            + " as an instructor for this class?";
        confirmSubmit( message, form );
    }
}

/** TinyMCE Initialization */

function initRichTextEditor()
{
    tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins: "emotions",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_buttons1 : "formatselect,fontselect,fontsizeselect,|,bold,italic,underline,strikethrough,forecolor,backcolor,|,code",
        theme_advanced_buttons2 : "justifyleft,justifycenter,justifyright,justifyfull,|,outdent,indent,blockquote,|,bullist,numlist,|,sub,sup,charmap,emotions,image,link,|,undo,redo,removeformat",
        theme_advanced_buttons3 : ""
    });
}
