/*
 * question.js
 *
 * Opens a new window with CAPTCHA explanation
 */
function explain(field) {
    switch(field) {
        case 'math':
            window.open('http://wcm.napco.com/sitesadmin/lib/forms/explain_math_question.htm','new_window','width=300,height=140,scrollbars=no,toolbar=no,location=no,status=no,menubar=no,left=300,top=200,screenX=300,screenY=200');
            break;

        case 'url':
            window.open('http://wcm.napco.com/sitesadmin/lib/forms/explain_url.htm','new_window','width=300,height=140,scrollbars=no,toolbar=no,location=no,status=no,menubar=no,left=300,top=200,screenX=300,screenY=200');
            break;

        default:
            window.open('http://wcm.napco.com/sitesadmin/lib/forms/explain_math_question.htm','new_window','width=300,height=140,scrollbars=no,toolbar=no,location=no,status=no,menubar=no,left=300,top=200,screenX=300,screenY=200');
    }
}

/*
 * question.js
 *
 * Writes the question label and all form fields for math CAPTCHA
 */
function question(active_question, j_element) {

    /*
     * Setup variables
     *
     * Generate the numbers for use in our math problems,
     * and some extra garbage vars for bots.
     */
    var randomize_question  = 0,
        answer              = 0,
        first               = Math.floor(Math.random()*20),
        second              = Math.floor(Math.random()*20),
        first_small         = Math.floor(Math.random()*6),
        second_small        = Math.floor(Math.random()*4),
        ext                 = Math.floor(Math.random()*20),
        ra                  = Math.floor(Math.random()*30),
        neo                 = Math.floor(Math.random()*20),
        temp_first, third, fourth, fifth,
        questionArr = [],
        qwe, asd, zxc, the, turing;

    /*
     * set the active question
     * 0	-	addition
     * 1	-	subtraction
     * 2	-	multiplication
     * 3	-	random
     */

    if(typeof active_question == 'undefined') {	active_question = 0; }

    /*
     * turn on to randomize the math function question
     */
    if(active_question > 2)
    {
        randomize_question = 1;
    }
    else
    {
        randomize_question = 0;
    }

    /*
     * Switch first and second if first is less than second
     * So we don't have to deal with negative numbers
     */
    if(first < second) {
        temp_first  = first;
        first       = second;
        second      = temp_first;
    }

    /*
     * calculate answers
     * third    = sum answer
     * fourth   = difference answer
     * fifth    = product answer
     */
    third   = first + second;
    fourth  = first - second;
    fifth   = first_small * second_small;

    /*
     * Generate questions
     * Build an array of possible questions
     */

    /*
     * build sum question
     */
    questionArr[0] = 'What does ' + first + ' + ' + second + ' equal?';

    /*
     * build difference question
     */
    questionArr[1] = 'What does ' + first + ' - ' + second + ' equal?';

    /*
     * build products question
     */
    questionArr[2] = 'What does ' + first_small + ' x ' + second_small + ' equal?';

    /*
     * if no question type was specified then randomize our question
     */
    if(randomize_question) { active_question = Math.floor(Math.random()*3); }

    /*
     * Generate form fields
     */

    /*
     * create some extra form fields and put some numbers in them
     */
    qwe = '<input class=\"hidden_field\" size=\"3\" type=\"text\" name=\"qwe\" value=\"' + ext + '\">';
    asd = '<input class=\"hidden_field\" size=\"3\" type=\"text\" name=\"asd\" value=\"' + ra + '\">';
    zxc = '<input class=\"hidden_field\" size=\"3\" type=\"text\" name=\"zxc\" value=\"' + neo + '\">';

    /*
     * create THE answer to the math problem above
     */
    switch(active_question) {
        case 0:
            answer = third;
            break;
        case 1:
            answer = fourth;
            break;
        case 2:
            answer = fifth;
            break;
    }

    the    = '<input class=\"hidden_field the\" size=\"4\" type=\"text\" id=\"the\" name=\"the\" value=\"' + answer + '\" />';
    turing = '<input class=\"required digits text math form-page\" type=\"text\" name=\"turing\" id=\"turing\" size=\"4\" maxlength=\"6\" value=\"\" /> <em class=\"explain\"><a href=\"javascript:explain(\'math\');\">Why are we asking this?</a></em>';

    /*
     * write question and our form fields
     */
    j_element.append('<label class=\"form-page\" for=\"turing\">' + questionArr[active_question] + ' *</label>');
    j_element.append(qwe + the + asd + turing + zxc);

}
