/*******************************************************************************
 *
 * Basic functions
 *
*******************************************************************************/

/**
 * Trim leading whitespaces
 * @param str Input string
 * @return string with leading whitespaces removed
 */
function leftTrim( str )
{
    var whitespace = new String( " \t\n\r" );
    var s = new String( str );

    if ( whitespace.indexOf( s.charAt( 0 ) ) != -1 )
    {
        // We have a string with leading blank(s)...
        var j = 0, i = s.length;

        // Iterate from the far left of string until we
        // don't have any more whitespace...
        while ( j < i && whitespace.indexOf( s.charAt( j ) ) != -1 )
            j++;

        // Get the substring from the first non-whitespace
        // character to the end of the string...
        s = s.substring( j, i );
    }
    return s;
}

/**
 * Trim trailing whitespaces
 * @param str Input string
 * @return string with trailing whitespaces removed
 */
function rightTrim(str, targetChar)
{
    var whitespace = new String( " \t\n\r" );
    var s = new String( str );

    if ( whitespace.indexOf( s.charAt( s.length-1 ) ) != -1 )
    {
        // We have a string with trailing blank(s)...
        var i = s.length - 1;       // Get length of string

        // Iterate from the far right of string until we
        // don't have any more whitespace...
        while ( i >= 0 && whitespace.indexOf( s.charAt( i ) ) != -1 )
            i--;

        // Get the substring from the front of the string to
        // where the last non-whitespace character is...
        s = s.substring( 0, i+1 );
    }

    return s;
}

/**
 * Trim whitespaces on the both sides
 * @param str Input string
 * @return string with both leading and trailing whitespaces removed
 */
function trim( str )
{
	return rightTrim( leftTrim( str ) );
}

/*******************************************************************************
 *
 * BrusBlogUpdater
 *
*******************************************************************************/

/**
 * BrusBlogUpdater, which updates the blog list
 * @param formObj Reference to the form object
 * @constructor
 */
function BrusBlogUpdater( formObj )
{
    // OPML Update form
    var form = formObj;
    var update = form.elements[ "update" ];
    var blogBox = form.elements[ "blogBox" ];
    var text = form.elements[ "text" ];
    var xmlUrl = form.elements[ "xmlUrl" ];
    var username = form.elements[ "username" ];
    var password = form.elements[ "password" ];
    var save = form.elements[ "save" ];
    var updateButtonText = update.innerHTML;
    var updateButtonCancelText = "Cancel";

    // Initialize the form
    updateBlogBox();
    toggleBlogBox();

    /**
     * Update the blog box when the fields are changed
     * @private
     */
    function updateBlogBox()
    {
        // If completed filling blog information
        if ( trim( xmlUrl.value ) != "" && trim( text.value ) != ""
            && trim( username.value ) != "" && trim( password.value ) != "" )
        {
            text.value = trim( text.value );
            xmlUrl.value = trim( xmlUrl.value );
            username.value = trim( username.value );
            password.value = trim( password.value );
            save.disabled = false;
        }
        else
        {
            save.disabled = true;
        }
    }

    /**
     * Toggle the blog box
     * @private
     */
    function toggleBlogBox()
    {
        if ( blogBox.style.display != "none" )
        {
            blogBox.style.display = "none";
            update.innerHTML = updateButtonText;
            update.blur();
        }
        else
        {
            blogBox.style.display = "block";
            update.innerHTML = updateButtonCancelText;
            text.focus();
        }
    }

    // Binding functions with events
    if ( document.addEventListener )   // W3C
    {
        blogBox.addEventListener( "change", updateBlogBox, false );
        blogBox.addEventListener( "keyup", updateBlogBox, false );
        update.addEventListener( "click", toggleBlogBox, false );
    }
    else if ( document.attachEvent )   // IE 5+
    {
        blogBox.attachEvent( "onchange", updateBlogBox  );
        blogBox.attachEvent( "onkeyup", updateBlogBox );
        update.attachEvent( "onclick", toggleBlogBox );
    }
    else                              // Any older browsers
    {
        blogBox.onchange = updateBlogBox;
        blogBox.onkeyup = updateBlogBox;
        update.onclick = toggleBlogBox;
    }
}

/*******************************************************************************
 *
 * "main" function
 *
*******************************************************************************/

/**
 * Instantiate classes needed.
 */
function startBrus()
{
    // Grab all the forms
    var forms = document.forms;

    // The forms we interested in
    var updateBlogForms = new Array( 0 );
    for ( var i = 0, length = forms.length; i < length; i++ )
    {
        if ( forms[i].id == "updateBlog" || forms[i].name == "updateBlog" )
        {
            updateBlogForms[ updateBlogForms.length ] = forms[i];
        }
    }

    // Instantiate the corresponsing objects for the forms
    var blogUpdaters = new Array( updateBlogForms.length );
    for ( var i = 0, length = blogUpdaters.length; i < length; i++ )
    {
        blogUpdaters[ blogUpdaters.length ]
            = new BrusBlogUpdater( updateBlogForms[i] );
    }
}

// Add onLoadEvents
if ( window.addEventListener )   // W3C
{
    window.addEventListener( "load", startBrus, false );
}
else if ( window.attachEvent )   // IE 5+
{
    window.attachEvent( "onload", startBrus );
}
else                              // Any older browsers
{
    window.onload = startBrus;
}