/*
 * qna_common.js
 *
 * JavaScript file for common Q&A functionality.
 * 
 */


//var qna_browser        = new Browser();
var qna_submitOK1       = true;
var qna_instructions1   = "";
//var qna_max_size_alert = "";

/**
  * Handle a key press release event in the question box.
  */
function handleKeyPress_QuestionBox1(p_evt)
{
    var ew = new EventWrapper(p_evt, qna_browser);

    if (ew.isKeyPress(ew.key.ENTER))
    {
        var qBox = ew.getElement();
        qBox.blur();
        qBox.value = qBox.value.replace(/\s+$/,''); // Strip whitespace from end
        submitQuestion1();
        return false;
    }
    else
    {
        if (ew.getElement().value.length > 135)
        {
            ew.getElement().value = ew.getElement().value.substring(0,135);
            alert(qna_max_size_alert);
            return false;
        }
    }

    return true;
}


/**
  * Handle a focus event in the question box.
  */
function handleFocus_QuestionBox1(p_evt)
{
    var ew = new EventWrapper(p_evt, qna_browser);

    var question = document.question_form1.question_box.value;
    // Clear the instructions if they are in the text box
    if (qna_instructions1.length > 0
        && question.indexOf(qna_instructions1) > -1)
    {
        document.question_form1.question_box.value = "";
        var qbox_class = document.question_form1.question_box.className;
        var instruction_class_idx = qbox_class.indexOf("qna-input-instructions");
        document.question_form1.question_box.className = qbox_class.substring(0, instruction_class_idx);
    }
    else
    {
        document.question_form1.question_box.select();
    }

    return true;
}


/**
  * Submit the question.
  */
function submitQuestion1()
{
    // Submit the question
    if (!isSubmitOK1())
    {
        return false;
    }

    // Prevent another quick submit and submit this question
    disableSubmit1();
    document.question_form1.submit();

    // Wait 10 seconds before submit available
    setTimeout("enableSubmit1();", 10000);
    startSearchingIndicator();

    return false;
}


/**
  * Check to see if submitting is OK.
  */
function isSubmitOK1()
{
    if (!qna_submitOK1)
        return false;

    var question = document.question_form1.question_box.value;
    // Trim any whitespace from question
    question = question.replace(/^\s+/,'');
    question = question.replace(/\s+$/,'');

    // Don't submit a blank query
    if (question == "")
    {
        document.question_form1.question_box.value = "";
        document.question_form1.question_box.focus();
        return false;
    }

    // Don't submit if the instructions are still in the text box
    if (qna_instructions1.length > 0
        && question.indexOf(qna_instructions1) > -1)
    {
        document.question_form1.question_box.value = "";
        document.question_form1.question_box.focus();
        return false;
    }

    return true;
}


/**
  * Disable the submit feature.
  */
function disableSubmit1()
{
    qna_submitOK1 = false;
    document.question_form1.Ask.disabled = true;
}


/**
  * Enable the submit feature.
  */
function enableSubmit1()
{
    qna_submitOK1 = true;
    document.question_form1.Ask.disabled = false;
}
