//Questions page
$(document).addEvent('domready', function() {
    var questionTogglers = $$('.questionDisplay');
    if (questionTogglers) {
        //Register a listener to open the answer for each question
        questionTogglers.each(function(el) {
            el.addEvent('click', ToggleAnswer.pass(el));
        });

        //Hop to id if one is given in the location fragment
        if (location.hash && location.hash.substring(0, 4) == '#qId') {
            //1 for the hash + 3 for "qId" = 4 characters
            var qId = location.hash.substring(4);
            _setAnswerDisplay($('answer' + qId), 'block');
        }
    }
});


function ToggleAnswer(qDisp, evnt) {
    if (!qDisp) {
        return;
    }
    var qId = qDisp.getProperty('id').substring(15);

    var answerBlock = $('answer' + qId);

    var answerStyle = answerBlock.getStyle('display');

    //First hide all answers (to hide any answer that's currently open
    $$('.answerBlock').each(function(el) {
        _setAnswerDisplay(el, 'none');
    });

    //Then toggle the selected answer on if applicable
    if (answerStyle == 'none') {
        _setAnswerDisplay(answerBlock, 'block');
    }

    return false;
}


function _setAnswerDisplay(answerBlock, display) {
    var qDisp = $('questionDisplay' + answerBlock.getProperty('id').substring(6));
    answerBlock.setStyle('display', display);
    if (display == 'none')
        qDisp.setProperties({ 'src': 'images/buttons/btn_table_open.gif', 'alt': 'View this answer' });
    else
        qDisp.setProperties({ 'src': 'images/buttons/btn_table_close.gif', 'alt': 'Close this answer' });
}



//Ask the Expert page
var CharacterCount = new Class({
    maxCharacters: 3000,
    div: null,
    field: null,
    form: null,
    onOverflow: null,
    onUnderflow: null,
    onSubmitOverflow: null,
    cancelSubmitOnOverflow: false,
    initialize: function(options) {
        this.field = $(options['field']);
        if ($defined(options['charCntDiv'])) {
            this.div = $(options['charCntDiv']);
        }
        else {
            this.div = new Element("div", { id: "characterCount" });
            this.div.inject(this.field, 'after');
        }

        if ($defined(options['maxCharacters'])) {
            this.maxCharacters = options['maxCharacters'];
        }

        if ($defined(options['form'])) {
            this.form = $(options['form']);
        }

        if ($defined(options['onOverflow'])) {
            this.onOverflow = options['onOverflow'];
        }

        if ($defined(options['onUnderflow'])) {
            this.onUnderflow = options['onUnderflow'];
        }

        if ($defined(options['onSubmitOverflow'])) {
            this.onSubmitOverflow = options['onSubmitOverflow'];
        }

        if ($defined(options['cancelSubmitOnOverflow'])) {
            this.cancelSubmitOnOverflow = options['cancelSubmitOnOverflow'];
        }

        this.field.addEvent("keydown", this.update.bindWithEvent(this));
        this.field.addEvent("keyup", this.update.bindWithEvent(this));
        if ($defined(this.form)) {
            this.form.addEvent("submit", function(evnt) {
                if (this.fieldLength() > this.maxCharacters) {
                    if (this.onSubmitOverflow && !this.onSubmitOverflow(event)) {
                        return;
                    }
                    if (this.cancelSubmitOnOverflow) {
                        evnt.stop();
                    }
                }
            } .bindWithEvent(this));
        }
    },
    update: function(event) {
        var numChars = this.fieldLength();
        if (numChars > this.maxCharacters) {
            if (this.onOverflow && !this.onOverflow(event)) {
                return;
            }
            this.div.innerHTML = "Your question is " + (numChars - this.maxCharacters) + " character" + (numChars - this.maxCharacters != 1 ? "s" : "") + " over the limit";
            this.div.setStyle('color', '#FF0000');
        }
        else {
            if (this.onUnderflow && !this.onUnderflow(event)) {
                return;
            }
            var remainingChars = this.maxCharacters - numChars;
            this.div.innerHTML = remainingChars + " character" + (remainingChars == 1 ? "" : "s") + " remaining";
            this.div.setStyle('color', '');
        }
    },
    fieldLength: function() {
        //To resolve line ending compatibility issues
        return this.field.value.replace(/\r\n/g, "\n").length;
    }
});

//Initialize dropdown box and set up character count
$(document).addEvent('domready', function() {
    var degree = $('degreeField');
    if (degree) {
        ToggleOtherDegreeBox(degree.getElement('select'));
    }

    var textarea = $(document.body).getElement('.questionInput');
    if (textarea) {
        (new CharacterCount({ field: textarea,
            charCntDiv: 'characterCount', form: this.getElement('form[id=aspnetForm]'), maxCharacters: 3000
        })).update();
    }
});

function ToggleOtherDegreeBox(ddbox) {
    if (!ddbox) {
        return;
    }

    var value = ddbox.options[ddbox.selectedIndex].value;

    var otherDegree = $("otherDegreeField");


    if (value == "other") {
        otherDegree.setStyles({ display: 'block' });
    }
    else {
        otherDegree.setStyles({ display: 'none' });
    }
}

/*
Function: EnableOnCheck(checkbox, buttontoEnable)
If checkbox is checked, submit button is enabled. 
*/

function EnableButtonOnChecked(check, buttontoEnable) {
    if (check.checked) {
        buttontoEnable.className = "btnSubmit";
        buttontoEnable.disabled = false;
        buttontoEnable.style.cursor = "hand";
    }
    else {
        buttontoEnable.className = "btnSubmitGrayed";
        buttontoEnable.disabled = true;
        buttontoEnable.style.cursor = "default";
    }
}