
// Default to not showing the captcha
var showing_nucaptcha   = false;
var message_class       = "wp-caption";
var timeout_seconds     = (60 * 30); // 30 minutes
var start_time          = current_time_in_seconds();

function current_time_in_seconds()
{
    var t = new Date;
    var ms = t.getTime();
    return parseInt(ms / 1000);
}

function SubmitForm()
{
    jQuery('#commentform').submit();
}

function NuCaptchaComment(error_msg, callback)
{
    jQuery('#wp_nucaptcha_message').hide();
    jQuery('#wp_nucaptcha_message').html('<div class="'+message_class+'">'+error_msg+'</div>');
    jQuery('#wp_nucaptcha_message').fadeIn('fast', callback);
}

function EnableSubmitButton()
{
    jQuery('.lmanswer').focus();
    jQuery('#submit').removeAttr('disabled');
}

function FadeInNuCaptcha()
{
    jQuery('#wp_nucaptcha').fadeIn('fast');
    jQuery('#submit').fadeIn('fast', EnableSubmitButton);
}

function IsValueEmpty(stringid)
{
    var str = jQuery(stringid).val();
    str = str.replace(/^\s+|\s+$/g,"");
    return (0 == str.length);
}

/**
 * The NuCaptcha Submit button
 *
 * If we're showing the captcha
 */
function NuCaptchaSubmit()
{
    // Figure out if we've timed out.
    var timeout = false;
    if ((current_time_in_seconds() - start_time) >= timeout_seconds)
    {
        jQuery('#nucptcha_ps').remove();
        timeout = true;
    }

    // If showing the nucaptcha, return true
    if (showing_nucaptcha)
    {
        if (IsValueEmpty('.lmanswer'))
        {
            jQuery('#submit').attr('disabled', 'disabled');
            NuCaptchaComment('Please complete the <a href="http://www.nucaptcha.com/" target="_blank">NuCaptcha</a>.', EnableSubmitButton);
            return (false || timeout);
        }
        else
        {
            NuCaptchaComment('Comment Submitted.  Please wait.');
            jQuery('#nucaptcha_error').fadeOut('fast');
            jQuery('#wp_nucaptcha').fadeOut('fast');
            jQuery('#submit').fadeOut('fast');
            return true;
        }
    }

    // Is the form empty?  If so warn about an empty form
    if (IsValueEmpty('#comment'))
    {
        NuCaptchaComment('Please enter a comment.');
        return false;
    }

    // Show the nucaptcha
    if (timeout) return true;
    showing_nucaptcha = true;
    jQuery('#wp_nucaptcha_message').fadeOut('fast');
    jQuery('#comment').fadeOut('fast');
    jQuery('#submit').fadeOut('fast', FadeInNuCaptcha);
    jQuery('#submit').attr('disabled', 'disabled');
    return false;
}

function InitNuCaptchaWPForm(nucaptcha_error, msg_class, previous_comment)
{
    // Update the message class
    message_class = msg_class;

    // Remove the comment by default
    jQuery('#wp_nucaptcha_message').hide();

    // Store our previous comment into the comment
    if (previous_comment.length > 0)
    {
        jQuery('#comment').val(unescape(previous_comment));
    }

    // Move the submit button down and hook in our submit method
    var sub = jQuery('#submit').parent().html();
    jQuery('#submit').parent().empty();
    jQuery('#wp_nucaptcha_sb').append(sub);
    //jQuery('#submit').click( 'return NuCaptchaSubmit();' );

    // Hook us into the form submit onclick
    jQuery('#commentform').submit(function() { return NuCaptchaSubmit(); });

    // If we're showing a captcha error, hide everything and face in the NuCaptcha
    if (nucaptcha_error)
    {
        showing_nucaptcha = true;
        jQuery('#comment').hide();
        jQuery('#wp_nucaptcha').hide();
        jQuery('#submit').attr('disabled', 'disabled');
        jQuery('#submit').hide();
        FadeInNuCaptcha();
    }
    // Showing comment?  Hide the Captcha
    else
    {
        jQuery('#wp_nucaptcha').hide();
    }
}
