// Change these variables if the HTML IDs change.
var input_id = 'txtQ';

//don mod
var hidden_answer_id   = 'txtAnswer';
var hidden_glossary_id = 'txtGlossary';


var hidden_input_id = 'txtQuestion';
var input_frm_id = 'frmInput';
var output_id = 'divOutput';
var submit_id = 'btnSubmit';

// Change this URL to point to ServletProcess
var post_url = '/AOLBotWebApp/ServletProcess';

window.onload = init;

function init() {
  Event.observe( input_id, 'focus',
                 function ( event ) {
                   $( input_id ).addClassName( 'focused' );
                 }
               );
  Event.observe( input_id, 'blur',
                 function ( event ) {
                   $( input_id ).removeClassName( 'focused' );
                 }
               );
  Event.observe( input_id, 'keypress',
                 function ( event ) {
                   if ( event.keyCode == Event.KEY_RETURN ) {
                     submit_input();
                     return false;
                   } else {
                     return true;
                   }
                 }
               );
  Event.observe( submit_id, 'click',
                 function() {
                   submit_input();
                 }
               );
  $( input_id ).focus();
}

function submit_input() {
  $( hidden_input_id ).value = $( input_id ).value.strip();

  if ( ( $( hidden_input_id ).present() ) || ( $( hidden_glossary_id ).present() )  ){
    new Ajax.Updater( output_id, post_url, {
      method: 'post',
      parameters: $( input_frm_id ).serialize(),
      insertion: Insertion.Bottom,
      onComplete: function() {
        // clear the user's input
        $( input_id ).clear();

        $( hidden_answer_id ).clear(); //don mod
        $( hidden_glossary_id).clear(); //don mod

        // scroll the output div down to the latest update
        $( output_id ).scrollTop = $( output_id ).scrollHeight ;

        // flash the output div
        new Effect.Highlight( output_id,
                              { startcolor: '#D7D7D7',
                                endcolor: '#ffffff',
                                duration: 0.7 }
                            );
        // focus on input
        $( input_id ).focus();
      }
    });  
  }
}
