/*
  SM 06Oct09: Prevent double submissions
  Based on discussions @ http://jquery-howto.blogspot.com/2009/05/disable-submit-button-on-form-submit.html
  
  SM 27/10/2009 12:05:46 AM: Messes up any form that relies on which submit button you pressed.
*/

$(document).ready(function(){
    
  // var === undefined    
  
  //alert('single-submit plugin loaded');
    
  //$('form').submit(function(){
  $('form').each(function() {
    
    // check to bypass this form via attribute
    if('false' == $(this).attr('single-submit')) {
      //alert('skipping single submit bind to submit');
      return true;
    }
    
    // bind this form's submit
    $(this).submit(function(event){
        
      //alert('Submitting form, press OK');
        
      // try/catch for non-quickform forms
      try {
      	var flag_result = false;
      	var fname = 'flag_validate_' + $(this).attr('id');
      	if(typeof fname == 'string') {
      		flag_result = eval(fname);
        }
      } catch(err) {
        // ReferenceError
        single_submit_exec(this, event);
        return true;
      }
      // QF form, check for validation sucess flag before activating single-submit
      if(flag_result) { 
        single_submit_exec(this, event); 
      }
    });
    
  });

});

// handler for single submit enter key lock
var singleSubmitHandler = function(e) {
  if(e.keyCode == 13) { return false; }
}

function single_submit_exec(aForm, event) {
  
  // Add "throbber" gif to submit buttons, using CSS
  var throbber_html = '<span class="throbber">Please wait...</span>';
  
  // SM 21Jul10: Quick hack to prevent a doubleup of throbbers on screen :)
  if($('input[type=submit]').length > 0) { 
  	$('input[type=submit]').after(throbber_html); // REG3/secure servers
  } else if($('input[name=btnSubmit]').length > 0) {
  	$('input[name=btnSubmit]').after(throbber_html); // REG3/secure servers
  }
  
  // Prevent form double-submission
  // SM 07Apr11: Testing for ArbSearch compatability
  
  //$('select', aForm).attr('disabled', 'disabled'); // bugged
  $('input[type=text]', aForm).attr('readonly', 'readonly');
  $('textarea', aForm).attr('readonly', 'readonly');
  
  $('input[type=submit]', aForm).each(function(){
    //alert('submit click with val:'+$(this).val());
    $(this).attr('disabled', 'disabled');
  });
  $('input[type=button]', aForm).each(function(){
    //alert('button click with val:'+$(this).val());
    $(this).attr('disabled', 'disabled');
  });
  $('input[name=btnSubmit]', aForm).each(function(){
    //alert('btnSubmit click with val:'+$(this).val());
    $(this).attr('disabled', 'disabled');
  });
  
  // Prevent enter key from submitting form again
  $('form').bind("keypress",  singleSubmitHandler);
  
}

/**
 * SM 23Aug10: Allow front-end to reset the single submit actions.
 * http://api.jquery.com/unbind/
 */
function single_submit_reset(aForm) {
  
  $(".throbber").remove();
  $('input[type=submit]', aForm).removeAttr('disabled');
  $('input[type=text]', aForm).removeAttr('readonly');
  $('textarea', aForm).removeAttr('readonly');
  $('input[type=button]', aForm).removeAttr('disabled');
  $('input[name=btnSubmit]', aForm).removeAttr('disabled');
  
  $('form').unbind("keypress", singleSubmitHandler); 
}
