


/*
 * Validates that the exclusion check is activated and shows a confirmation
 * message.
 */

function validateAutoExclusionField() {
    if (!document.getElementById('excluirme').checked) {
    	window.alert('???autoexclusion.form.checkbox.alert???');
        return false;
    }

    return window.confirm('???autoexclusion.form.questionfirsthalf??? ' +
        document.getElementById('exclusionPeriod').value +
        ' ???autoexclusion.form.questionsecondhalf???');
}

/*
 * Submit blacklisting data.
 */
 function submitBlacklistingForm(form, checkbox) {
   submit = false;
   if (checkbox.checked) {
      submit = window.confirm('???message.confirm.blacklisting???');
   } else {
      submit = window.confirm('???message.confirm.removefromblacklist???');
   }

   if (submit) {
      form.submit();
   } else {

      // If the user cancels the operation the checkbox should be reverted to its original value
      checkbox.checked = !checkbox.checked;
   }
 }

/*
 * Submit exclusion data.
 */
 function submitExclusionForm(form, checkbox) {
   submit = false;
   if (checkbox.checked) {
      submit = window.confirm('???message.confirm.exclusion???');
   } else {
      submit = window.confirm('???message.confirm.inclusion???');
   }

   if (submit) {
      form.submit();
   } else {

      // If the user cancels the operation the checkbox should be reverted to its original value
      checkbox.checked = !checkbox.checked;
   }
 }

 /*
 * Submit activation data.
 */
 function submitActivationForm(form, checkbox) {
   if (window.confirm('???message.confirm.activation???')) {
      form.submit();
   } else {

      // If the user cancels the operation the checkbox should be reverted to its original value
      checkbox.checked = !checkbox.checked;
   }
 }

 /*
 * Submit Transaction Form: Accept or Reject transaction.
 */
 function submitTransactionOperationForm(form, action, id) {

   form.action = action;
   form.operationId.value = id;
   form.submit();
 }


 /*
 * Submit CrmAccount activation data.
 */
 function submitCrmAccountActivationForm(form, checkbox) {
   if (window.confirm('???message.confirm.crm.account.activation???')) {
      form.submit();
   } else {

      // If the user cancels the operation the checkbox should be reverted to its original value
      checkbox.checked = !checkbox.checked;
   }
 }

 /*
 * Submit CrmAccount activation data.
 */
 function submitCrmAccountDeactivationForm(form, checkbox) {
   if (window.confirm('???message.confirm.crm.account.deactivation???')) {
      form.submit();
   } else {

      // If the user cancels the operation the checkbox should be reverted to its original value
      checkbox.checked = !checkbox.checked;
   }
 }

/*
 * Attach an event to an object
 * @deprecated Now should use init method
 */
function addEvent(obj, type, fn) {
  if (obj.addEventListener) {
    obj.addEventListener( type, fn, false );
  } else if (obj.attachEvent) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
    obj.attachEvent( "on"+type, obj[type+fn] );
  }
}

function attachDisableFunctionToFormsOnSubmit() {
  if (!document.getElementsByTagName) {
    return;
  }

  forms = document.getElementsByTagName('form');
  for (i = 0; i < forms.length; i++) {
    forms[i].onsubmit = function () {disableButtons(forms[i]);}
  }
}

function disableButtons(form) {
  inputs = document.getElementsByTagName('input');
  for (i = 0; i < inputs.length; i++) {
    if (inputs[i].type == 'submit') {
      inputs[i].disabled = true;
    }
  }
  return true;
}

function fixIEButtons() {
  //Detect IE5.5+
  version = 0;
  if (navigator.appVersion.indexOf("MSIE")!=-1){
    temp=navigator.appVersion.split("MSIE")
    version=parseFloat(temp[1])
  }
  if (version < 5.5) {
    //return;
  }
  var buttons = document.getElementsByTagName('button');
  for (var i = 0; i < buttons.length; i++) {
    if(buttons[i].onclick) continue;
    buttons[i].onclick = function () {
      for (var j = 0; j < buttons.length; j++) {
        buttons[j].disabled = true;
      }
      this.disabled = false;
      this.value = this.attributes.getNamedItem("value").nodeValue;
    }
  }
}

function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) {
  clearInterval(_timer);
  _timer = null;
  }

  //attachDisableFunctionToFormsOnSubmit();
  fixIEButtons();
};

/* for Mozilla */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
  if (this.readyState == "complete") {
    init(); // call the onload handler
  }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
  if (/loaded|complete/.test(document.readyState)) {
    init(); // call the onload handler
  }
  }, 10);
}

/* for other browsers */
window.onload = init;
