// support for editing faq and download sections
var editSection = -1;

// remove all leading and trailing spaces from aString
function trimString(sString)
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}


function ShowHideElement(elementId, show )
{
    document.getElementById(elementId).style.visibility = show ? "visible" : "hidden";;
}
function StripAngleBrackets(str)
{
    str = str.replace(/>/g,"");
    str = str.replace(/</g,"");
    return str;
}

// moves the currently selected <option> up or down
// inputs:
//      selectId: id of the <select> element
//      up: true to move up, false to move down
function MoveSelectOptionUpDown(selectId,up)
{
    var select = document.getElementById(selectId);
    var index = select.selectedIndex;
    if (index >= 0 )
    {
        var text = select.options[index].text;
        var value = select.options[index].value;

        if ( up && index > 0 )
        {
            select.options[index].text = select.options[index-1].text;
            select.options[index].value = select.options[index-1].value;
            select.options[index-1].text = text;
            select.options[index-1].value = value;
            select.selectedIndex = index-1;
        }
        else if ( !up && index < select.length-1 )
        {
            select.options[index].text = select.options[index+1].text;
            select.options[index].value = select.options[index+1].value;
            select.options[index+1].text = text;
            select.options[index+1].value = value;
            select.selectedIndex = index+1;
        }
    }
}

//  deletes the currently selection <option>
// inputs:
//      selectId: id of the <select> element
function DeleteSectionOption(selectId)
{
    var select = document.getElementById(selectId);
    if (select.selectedIndex >= 0 )
    {
        if ( window.confirm('Delete section ' + select.options[select.selectedIndex].text + '?' ) )
        {
            select.remove(select.selectedIndex);
        }
    }
}
// creates xml for the <select> and posts it back
function SaveSections(selectId,xmlTextBoxId,postBackButtonId)
{
    var select = document.getElementById(selectId);
    var xml = "<Categories>";
    
    for ( var ii = 0; ii < select.length; ii++ )
    {
        xml += "<Category><CategoryName>";
        xml += StripAngleBrackets(select.options[ii].text);
        xml += "</CategoryName><CategoryId>";
        xml += select.options[ii].value;
        xml += "</CategoryId><DisplayOrder>";
        xml += ii+1;
        xml += "</DisplayOrder></Category>";
    }
    xml += "</Categories>";
    document.getElementById(xmlTextBoxId).value = xml;   
    __doPostBack(postBackButtonId,'');         
}

// saves a new section to the <select>
function SaveSection(textBoxId,selectId,divId,add)
{
    var sectionName = document.getElementById(textBoxId).value;
    sectionName = trimString(sectionName);
    sectionName = StripAngleBrackets(sectionName);
    var select = document.getElementById(selectId);

    if ( add )
    {
        // add new section
        if ( sectionName.length > 0 )
        {
            var newItem = document.createElement('option');
            newItem.text = sectionName;
            try
            {
                select.add(newItem,null);  // standards compliant
            }
            catch(ex)
            {
                select.add(newItem); // IE only
            }
        }
    }
    else if ( editSection >= 0  && sectionName.length > 0)
    {
        // save edits to existing section
        select.options[editSection].text = sectionName;
    }
    ShowHideElement(divId,false);
}



