/*
 * qna_common.js
 *
 * JavaScript file for common Q&A functionality.
 * 
 */


var qna_browser        = new Browser();
var qna_submitOK       = true;
var qna_instructions   = "";
var qna_max_size_alert = "";

/**
  * Handle a key press release event in the question box.
  */
function handleKeyPress_QuestionBox(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
        submitQuestion();
        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_QuestionBox(p_evt)
{
    var ew = new EventWrapper(p_evt, qna_browser);

    var question = document.question_form.question_box.value;
    // Clear the instructions if they are in the text box
    if (qna_instructions.length > 0
        && question.indexOf(qna_instructions) > -1)
    {
        document.question_form.question_box.value = "";
        var qbox_class = document.question_form.question_box.className;
        var instruction_class_idx = qbox_class.indexOf("qna-input-instructions");
        document.question_form.question_box.className = qbox_class.substring(0, instruction_class_idx);
    }
    else
    {
        document.question_form.question_box.select();
    }

    return true;
}


/**
  * Submit the question.
  */
function submitQuestion()
{
    // Submit the question
    if (!isSubmitOK())
    {
        return false;
    }

    // Prevent another quick submit and submit this question
    disableSubmit();
    document.question_form.submit();

    // Wait 10 seconds before submit available
    setTimeout("enableSubmit();", 10000);
    startSearchingIndicator();

    return false;
}


/**
  * Check to see if submitting is OK.
  */
function isSubmitOK()
{
    if (!qna_submitOK)
        return false;

    var question = document.question_form.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_form.question_box.value = "";
        document.question_form.question_box.focus();
        return false;
    }

    // Don't submit if the instructions are still in the text box
    if (qna_instructions.length > 0
        && question.indexOf(qna_instructions) > -1)
    {
        document.question_form.question_box.value = "";
        document.question_form.question_box.focus();
        return false;
    }

    return true;
}


/**
  * Disable the submit feature.
  */
function disableSubmit()
{
    qna_submitOK = false;
    document.question_form.Ask.disabled = true;
}


/**
  * Enable the submit feature.
  */
function enableSubmit()
{
    qna_submitOK = true;
    document.question_form.Ask.disabled = false;
}


/**
  * Start progress bar
  *
  * This isn't a real progress indicator since we don't really know how
  * long it will take for the search to finish, but it will give
  * the user some feedback that the system is processing their search.
  */
var progressIndex = 0;
var maxIndex      = 0;
function startSearchingIndicator()
{
    // Start up the search indicator if it is defined
    var progBox = document.getElementById("prog_box");

    if (progBox)
    {
        // Show progress area
        progBox.style.visibility = "inherit";

        // Initialize progress area
        var progIdx = 1;
        var progElement = document.getElementById("prog" + progIdx);
        while (progElement != null)
        {
            progElement.className = "qna-progress-empty";
            progIdx++;
            progElement = document.getElementById("prog" + progIdx);
        }
        maxIndex = progIdx - 1; // Keep track of the size of the progress bar

        // Start the animation
        progressIndex = 0;
        showProgress();
    }
}

function clearSearchingIndicator()
{
    // Initialize progress area
    var progIdx = 1;
    var progElement = document.getElementById("prog" + progIdx);
    while (progElement != null)
    {
        progElement.className = "qna-progress-empty";
        progIdx++;
        progElement = document.getElementById("prog" + progIdx);
    }
}

function showProgress()
{
    // Increment to our next progress field
    if (progressIndex < maxIndex)
        progressIndex++;
    else
    {
        clearSearchingIndicator();
        progressIndex = 1;
    }

    // Display progress
    progElement = document.getElementById("prog" + progressIndex);
    progElement.className = "qna-progress-full"

    setTimeout("showProgress();", 1000);
}


/**
  * Return to the previous page
  */
function goBack()
{
    history.back();
    return true;
}


/**
  * Show a message in the browser's status bar
  */
function showStatusMessage(p_message)
{
	/* new */
    	window.status = p_message.replace(/nokia\/answerdisplay\.jsp/g, "nokiaselfservice/answer.jsp");
	
    	return true;
}

/**
 * Redirect url to Genius
 */
function redirectUrlToGenius(p_message)
{
	var p_message_array = p_message.split("=");
	var guid = p_message_array[p_message_array.length-1];	
	document.location.href = "http://knowledge.search.nokiausa.com/nokiaselfservice/answer.jsp?guid=" + guid;
	// document.location.href = "answer.jsp?guid=" + guid;
}

/**
  * Handle onload event.
  */
function handleOnLoad()
{
    // Fill in any message variables
    if (document.qna_messages != null)
    {
        if (document.qna_messages.question_box_instructions != null
            && document.qna_messages.question_box_instructions.value.length > 0)
            qna_instructions = document.qna_messages.question_box_instructions.value;
        if (document.qna_messages.question_length_warning != null)
            qna_max_size_alert = document.qna_messages.question_length_warning.value;
    }


    // Put focus on the question box
    if (document.question_form != null
        && document.question_form.question_box != null)
    {
        if (qna_instructions.length > 0 && document.question_form.question_box.value == "")
        {
            document.question_form.question_box.value = qna_instructions;
            document.question_form.question_box.className += " qna-input-instructions";
        }
        /*
        else if (document.user_feedback_form != null)
            document.question_form.question_box.focus();
        */
    }

    return true;
}


/*
 * Set up a window load handler if none exists.
 */
if (window.onload == null)
    window.onload = handleOnLoad;

