/* wordpress.js -- JavaScript code for interfacing with WordPress 2.0.  */

/*
 * Copyright (C) 2005-2009 Tudor Hulubei <tudor@hulubei.net>.
 * Distributed under the GNU General Public License.
 */


/* This is global so that we can set the focus.  See
   http://www.mozilla.org/access/keyboard/tabindex.  */
var g_wpUsernameInput;
var g_wpAuthorField;
var g_wpTextareaField;

/* Save the last cancelled form here.  */
var g_wpAuthorValue;
var g_wpEmailValue;
var g_wpUrlValue;
var g_wpCommentValue;

/* Associative arrays storing the comments for each post.  */
var g_wpIds = new Array();
var g_wpAuthors = new Array();
var g_wpTimes = new Array();
var g_wpTexts = new Array();

var g_classString = is_ie ? "className" : "class";


function ClearElement(element)
{
    while (element.hasChildNodes())
        element.removeChild(element.lastChild);
}


function Login()
{
    var accountDiv = document.getElementById("account");

    if (accountDiv.hasChildNodes())
    {
        ClearElement(accountDiv);
        return;
    }

    if (!ClosePostForms(true))
        return;

    var form = document.createElement('form');
    form.setAttribute('action', 'http://hulubei.net/wordpress/wp-login.php');
    form.setAttribute('method', 'post');
    form.setAttribute('name', 'loginform');
    form.setAttribute('id', 'loginform');
    form.appendChild(document.createElement('p'));

    g_wpUsernameInput = document.createElement('input');
    g_wpUsernameInput.setAttribute('type', 'text');
    g_wpUsernameInput.setAttribute('name', 'log');
    g_wpUsernameInput.setAttribute('id', 'log');
    g_wpUsernameInput.setAttribute('value', '');
    g_wpUsernameInput.setAttribute('size', '20');
    g_wpUsernameInput.setAttribute('tabindex', '1');
    g_wpUsernameInput.setAttribute('style', 'display: inline;');
    form.appendChild(document.createTextNode('Username: '));
    form.appendChild(document.createElement('br'));
    form.appendChild(g_wpUsernameInput);
    form.appendChild(document.createElement('br'));

    var password = document.createElement('input');
    password.setAttribute('type', 'password');
    password.setAttribute('name', 'pwd');
    password.setAttribute('id', 'pwd');
    password.setAttribute('value', '');
    password.setAttribute('size', '20');
    password.setAttribute('tabindex', '2');
    password.setAttribute('style', 'display: inline;');
    form.appendChild(document.createTextNode('Password: '));
    form.appendChild(document.createElement('br'));
    form.appendChild(password);
    form.appendChild(document.createElement('br'));

    var rememberme = document.createElement('input');
    rememberme.setAttribute('type', 'checkbox');
    rememberme.setAttribute('id', 'rememberme');
    rememberme.setAttribute('value', 'forever');
    rememberme.setAttribute('tabindex', '3');
    rememberme.setAttribute('style', 'display: inline;');
    form.appendChild(document.createTextNode('Remember Me: '));
    form.appendChild(rememberme);
    form.appendChild(document.createElement('p'));

    var submit = document.createElement('input');
    submit.setAttribute('onclick', 'javascript:document.loginform.submit(); return true;');
    submit.setAttribute('style', 'display: inline;');
    submit.setAttribute('tabindex', '4');
    submit.appendChild(document.createTextNode('Submit'));

    form.appendChild(submit);
    form.appendChild(document.createTextNode(' '));

    var redirectTo = document.createElement('input');
    redirectTo.setAttribute('type', 'hidden');
    redirectTo.setAttribute('name', 'redirect_to');
    redirectTo.setAttribute('value', location.href);
    form.appendChild(redirectTo);

    setTimeout("g_wpUsernameInput.focus();", 0);
    accountDiv.appendChild(form);
}


function SetPostSize(post, size)
{
    g_wpIds[post] = new Array(size);
    g_wpAuthors[post] = new Array(size);
    g_wpTimes[post] = new Array(size);
    g_wpTexts[post] = new Array(size);
}


function AddPostComment(post, index, id, author, time, text)
{
    g_wpIds[post][index] = id;
    g_wpAuthors[post][index] = author;
    g_wpTimes[post][index] = time;
    g_wpTexts[post][index] = text;
}


function CommentLabelText(nComments, arrow)
{
    if (nComments == 0)
        return document.createTextNode("No Comments");
    else if (nComments == 1)
        return document.createTextNode("1 Comment " + arrow);

    return document.createTextNode(nComments + " Comments " + arrow);
}


function TogglePostComments(post, userId, nComments, laquo, raquo)
{
    var replyDiv = document.getElementById("reply-" + post);
    var commentsDiv = document.getElementById("comments-" + post);
    var commentsLabelDiv = document.getElementById("comments-label-" + post);

    ClearElement(commentsLabelDiv);

    if (commentsDiv.hasChildNodes())
    {
        ClearElement(commentsDiv);
        commentsLabelDiv.appendChild(CommentLabelText(nComments, raquo));
        if (replyDiv && replyDiv.hasChildNodes())
            SetFocus(userId);
        return;
    }

    commentsLabelDiv.appendChild(CommentLabelText(nComments, laquo));

    var n = g_wpIds[post].length;
    if (n > 0)
    {
        var ol = document.createElement('ol');
        ol.setAttribute('style', 'margin: 2em 0 0 0;');

        for (var i = 0; i < n; i++)
        {
            var li = document.createElement('li');
            li.setAttribute("id", "comment-" + g_wpIds[post][i]);
            li.appendChild(document.createTextNode(
                "::: by " +
                g_wpAuthors[post][i] +
                " on " +
                g_wpTimes[post][i]));
            li.appendChild(document.createElement('br'));
            /* I wish we could avoid using innerHTML here...  */
            var div = document.createElement('div');
            /* Setting a class here doesn't work properly in Firefox.
               Somehow it is overridden by WPBlogEntryText.  */
            div.setAttribute('style', 'font-size: 95%; margin: 0.2em 0 0 0;');
            div.innerHTML = g_wpTexts[post][i];
            li.appendChild(div);
            li.appendChild(document.createElement('p'));
            ol.appendChild(li);
        }
        commentsDiv.appendChild(ol);
    }

    if (replyDiv && replyDiv.hasChildNodes())
        SetFocus(userId);
}


function IsEmail(email)
{
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return filter.test(email);
}


function CheckComment(userId)
{
    if (userId == 0)
    {
        if (document.commentform.author.value == '' ||
            document.commentform.email.value == '')
        {
            alert('The name and email fields are required!');
            return false;
        }

        if (!IsEmail(document.commentform.email.value))
        {
            alert('Invalid email address: \"' +
                  document.commentform.email.value + '\"!');
            return false;
        }

        if (document.commentform.comment.value == '')
        {
            alert('Your comment is empty!');
            return false;
        }
    }

          return true;
}


function ClosePostForms(mustWarn)
{
    var formPresent =
        document.commentform;
    var authorPresent =
        document.commentform &&
        document.commentform.author;
    var emailPresent =
        document.commentform &&
        document.commentform.email;
    var urlPresent =
        document.commentform &&
        document.commentform.url;
    var commentPresent =
        document.commentform &&
        document.commentform.comment;

    g_wpAuthorValue = authorPresent ?
        document.commentform.author.value : '';
    g_wpEmailValue = emailPresent ?
        document.commentform.email.value : '';
    g_wpUrlValue = urlPresent ?
        document.commentform.url.value : '';
    g_wpCommentValue = commentPresent ?
        document.commentform.comment.value : '';

    if (commentPresent &&
        (document.commentform.comment.value != '') &&
        (mustWarn == true) &&
        confirm('Are you sure?  ' +
                'The information entered may be lost!') == false)
        return false;

    var replyDivs = document.getElementsByTagName('div');

    for (var i = 0; i < replyDivs.length; i++)
    {
        var ithReplyDiv = replyDivs[i];
        if (ithReplyDiv && ithReplyDiv.getAttribute(g_classString) == 'PostReply')
            ClearElement(ithReplyDiv);
    }

    return true;
}


function SetFocus(userId)
{
          if (userId == 0)
              setTimeout("g_wpAuthorField.focus();", 0);
          else
        setTimeout("g_wpTextareaField.focus();", 0);
}


function PostLabelText(arrow)
{
    return document.createTextNode("Post a Comment " + arrow);
}


function TogglePostForm(post, userId, nameEmailRequired, laquo, raquo)
{
    var replyDiv = document.getElementById("reply-" + post);
    var hasChildren = replyDiv.hasChildNodes();

    if (!ClosePostForms(hasChildren))
        return;

    var replyLabelDiv = document.getElementById("reply-label-" + post);

    if (hasChildren == true)
    {
        ClearElement(replyDiv);
        ClearElement(replyLabelDiv);
        replyLabelDiv.appendChild(PostLabelText(raquo));
        return;
    }

    ClearElement(replyLabelDiv);
    replyLabelDiv.appendChild(PostLabelText(laquo));

    var form = document.createElement('form');
    form.setAttribute('action', 'http://hulubei.net/wordpress/wp-comments-post.php');
    form.setAttribute('method', 'post');
    form.setAttribute('name', 'commentform');
    form.setAttribute('id', 'commentform');

    form.appendChild(document.createElement('p'));

    var ycDiv = document.createElement('div');
    ycDiv.setAttribute(g_classString, 'WPCommentHeader');
    ycDiv.appendChild(document.createElement('br'));
    ycDiv.appendChild(document.createTextNode('Your Comment:'));
    form.appendChild(ycDiv);

    form.appendChild(document.createElement('p'));

    /*
     * Remember that for cookies to work, the "Blog address
     * (URI)" field in the "General Options" configuration panel
     * has to be set to the site name, i.e. http://hulubei.net/.
     */

    if (userId == 0)
    {
        /* If not logged in, user must enter name & email.  */

	/* The name paragraph.  */
	var nameParagraph = document.createElement('p');
	nameParagraph.setAttribute('style', 'clear: both;');

        g_wpAuthorField = document.createElement('input');
        g_wpAuthorField.setAttribute('type', 'text');
        g_wpAuthorField.setAttribute('name', 'author');
        g_wpAuthorField.setAttribute('id', 'author');
        if (g_wpAuthorValue)
            g_wpAuthorField.setAttribute('value', g_wpAuthorValue);
        g_wpAuthorField.setAttribute('size', '22');
        g_wpAuthorField.setAttribute('tabindex', '1');
        g_wpAuthorField.setAttribute(g_classString, 'WPComment');
        nameParagraph.appendChild(g_wpAuthorField);

        var nameLabelText = ' Name ';
        if (nameEmailRequired == true)
            nameLabelText += '(required) ';
        var nameLabel = document.createElement('label');
        nameLabel.setAttribute('for', 'author');
	nameLabel.setAttribute('class', 'WPComment');
	nameLabel.appendChild(document.createTextNode(nameLabelText));
        nameParagraph.appendChild(nameLabel);

        form.appendChild(nameParagraph);

	/* The email paragraph.  */
	var emailParagraph = document.createElement('p');
	emailParagraph.setAttribute('style', 'clear: both;');

        var email = document.createElement('input');
        email.setAttribute('type', 'text');
        email.setAttribute('name', 'email');
        email.setAttribute('id', 'email');
        email.setAttribute(g_classString, 'WPComment');
        if (g_wpEmailValue)
            email.setAttribute('value', g_wpEmailValue);
        email.setAttribute('size', '22');
        email.setAttribute('tabindex', '2');
        emailParagraph.appendChild(email);

        var emailLabelText = ' Email (will not be published) '
        if (nameEmailRequired == true)
            emailLabelText += '(required) ';
        var emailLabel = document.createElement('label');
        emailLabel.setAttribute('for', 'email');
	emailLabel.setAttribute('class', 'WPComment');
        emailLabel.appendChild(document.createTextNode(emailLabelText));
        emailParagraph.appendChild(emailLabel);

        form.appendChild(emailParagraph);

	/* The URL paragraph.  */
	var urlParagraph = document.createElement('p');
	urlParagraph.setAttribute('style', 'clear: both;');

        var url = document.createElement('input');
        url.setAttribute('type', 'text');
        url.setAttribute('name', 'url');
        url.setAttribute('id', 'url');
        url.setAttribute(g_classString, 'WPComment');
        if (g_wpUrlValue)
            url.setAttribute('value', g_wpUrlValue);
        url.setAttribute('size', '22');
        url.setAttribute('tabindex', '3');
        urlParagraph.appendChild(url);

	var urlLabelText = " Website";
        var urlLabel = document.createElement('label');
        urlLabel.setAttribute('for', 'url');
	urlLabel.setAttribute('class', 'WPComment');
        urlLabel.appendChild(document.createTextNode(urlLabelText));
        urlParagraph.appendChild(urlLabel);

        form.appendChild(urlParagraph);

	var br = document.createElement('br');
	br.setAttribute('style', 'clear: both;');
        form.appendChild(br);
    }

    g_wpTextareaField = document.createElement('textarea');
    g_wpTextareaField.setAttribute('name', 'comment');
    g_wpTextareaField.setAttribute('id', 'comment');
    /*g_wpTextareaField.setAttribute('cols', '80%');*/
    g_wpTextareaField.setAttribute('rows', '10');
    g_wpTextareaField.setAttribute(g_classString, 'WPComment');
    g_wpTextareaField.setAttribute('tabindex', '4');
    if (g_wpCommentValue)
    {
        ClearElement(g_wpTextareaField);
        g_wpTextareaField.appendChild(document.createTextNode(g_wpCommentValue));
    }
    form.appendChild(g_wpTextareaField);
    form.appendChild(document.createElement('p'));

    var br = document.createElement('br');
    br.setAttribute('style', 'clear: both;');
    form.appendChild(br);

    var commentPostId = document.createElement('input');
    commentPostId.setAttribute('type', 'hidden');
    commentPostId.setAttribute('name', 'comment_post_ID');
    commentPostId.setAttribute('value', post);
    form.appendChild(commentPostId);

    var submit = document.createElement('input');
    submit.setAttribute('onclick', 'javascript:return CheckComment(' + userId + ');');
    submit.setAttribute('name', 'submit');
    submit.setAttribute('type', 'submit');
    submit.setAttribute('id', 'submit');
    submit.setAttribute('value', 'Submit Comment');
    submit.setAttribute(g_classString, 'WPComment');
    submit.setAttribute('tabindex', '5');
    form.appendChild(submit);
    form.appendChild(document.createTextNode(' '));

    var cancel = document.createElement('input');
    cancel.setAttribute('onclick', 'javascript:CancelPostForm(' + post +',\"' + raquo + '\"); return true;');
    cancel.setAttribute('name', 'cancel');
    cancel.setAttribute('type', 'button');
    cancel.setAttribute('id', 'cancel');
    cancel.setAttribute('value', 'Cancel');
    cancel.setAttribute(g_classString, 'WPComment');
    cancel.setAttribute('tabindex', '6');
    form.appendChild(cancel);
    form.appendChild(document.createTextNode(' '));

    var reset = document.createElement('input');
    reset.setAttribute('onclick', 'javascript:SetFocus(' + userId + '); return true;');
    reset.setAttribute('name', 'reset');
    reset.setAttribute('type', 'reset');
    reset.setAttribute('id', 'reset');
    reset.setAttribute('value', 'Reset');
    reset.setAttribute(g_classString, 'WPComment');
    reset.setAttribute('tabindex', '7');
    form.appendChild(reset);

    var redirectTo = document.createElement('input');
    redirectTo.setAttribute('type', 'hidden');
    redirectTo.setAttribute('name', 'redirect_to');
    redirectTo.setAttribute('value', location.href);
    form.appendChild(redirectTo);

    SetFocus(userId);
    replyDiv.appendChild(form);

    FixGoogleToolbarAutoFill();
}


function CancelPostForm(post, raquo)
{
    if (!ClosePostForms(true))
        return;

    var replyDiv = document.getElementById("reply-" + post);
    var replyLabelDiv = document.getElementById("reply-label-" + post);
    ClearElement(replyDiv);
    ClearElement(replyLabelDiv);
    replyLabelDiv.appendChild(PostLabelText(raquo));
}

