/****************************************************************

  Traction Software, Inc. Confidential and Proprietary Information

  Copyright (c) 1996-2010 Traction Software, Inc.
  All rights reserved.

****************************************************************/

// PLEASE DO NOT DELETE THIS LINE -- make copyright depends on it.
//
// Summary:
//
//  This file implements a simple mechanism to ensure that:
// 
//  1) the page is not submitted before it is finished loading (and
//  all javascript setup code has executed).  the page author must
//  call the waitforload_ready() to indicate that the page is loaded.
//
//  2) the page is not submitted more than once.  this can be a
//  problem for articles and attachments.  usually clicking a submit
//  will halt the current submit and start a new one which can result
//  in even longer waiting on slow connections or duplicate articles.
//
//  It is derived from the /html/waitforload.js code and should be
//  used instead of that in all new forms.
//
// Dependencies: 
//
//  1) i18n javascript file from config/i18n/resources
//  src="#{i18n_javascript_src}"
//
// Usage:
//
//  1) Include this file in a script block in the HEAD section after
//  the i18n javascript file from config/i18n/resources.
//
//  2) On the page, create a form named "fm" and a blank hidden form
//  field named "fb_submit".
//
//  3) After the page has loaded, call waitforload_ready() to enable
//  form submits.  This can be done at the end of the page, in the
//  BODY onLoad= event handler, or some other appropriate place.
//
//  4) Use the go() function with a trigger to submit the form.  On
//  the server side, context.getFormParser().findTrigger() can be
//  called to retreive the trigger.  An form parameter of the go()
//  function does not need to be called if the form is named "fm" and
//  accessible using document.fm.
//
// Currently used by:
//
//  com/traction/view/modern/html/newentryform/modernNewEntry.htm
//  com/traction/view/modern/html/newentryform/modernAttachment.htm
//  com/traction/view/modern/html/insertLabel.htm
//  com/traction/view/modern/html/editRelationships.htm
//
// [ajm 14.Oct.2003]
//

var isIE = (navigator.appName.indexOf("Microsoft") != -1);

var waitforload_readyToSubmit = false;
var waitforload_preSubmit = false;
var waitforload_startedSubmit = false;

var waitforload_bePatientCount = 0;
var waitforload_pleaseWaitCount = 0;

// this function will get called just before document.fm.submit().
// this can be redefined to actually do something.
function waitforload_onsubmit() {
  return true;
}


// this can be placed in the BODY onLoad="" or at the end of the page.
// it indicates that the page has completed loading and submits should
// be allowed to proceed.
function waitforload_ready() {
  waitforload_readyToSubmit = true;
}

// returns true if the page is submitting 
function waitforload_isbusy() {
  return waitforload_startedSubmit;
}

// like waitforload_isbusy, but warns the user if busy
function waitforload_isready() {
    // see if we're ready (if waitforload_ready() called)
  if (waitforload_readyToSubmit) {

    // if there's not already a submit in progress
    if (waitforload_startedSubmit) {

      // alert the user that the page is in progress
      if (waitforload_bePatientCount >= 2) {
	var FORCE = prompt(i18n("waitforload2_submit_force_message", "Submit in progress, please be patient. If you are sure the submit has failed and you believe that you are receiving this message in error, type FORCE below:"), "");

	// this allows the user to override in the case that they are
	// certain the page is not submitting.
	if (FORCE && FORCE == "FORCE") {
	  waitforload_bePatientCount = 0;
	  waitforload_startedSubmit = false;
	  return true;
	}
      } else {
	alert(i18n("waitforload2_submit_message", "Submit in progress, please be patient."));
	waitforload_bePatientCount ++;
      }

      return false;
    } else {
      return true;
    }
  } 
  else {
    // alert the user that the page hasn't finished loading
    if (waitforload_pleaseWaitCount >= 2) {
      alert(i18n("waitforload2_please_wait_reload_message", "Please wait for the form to finish loading.\n\nIf the form does not appear to be loading,\nyou may wish to try pressing the Reload button."));
    } else {
      alert(i18n("waitforload2_please_wait_message", "Please wait for the form to finish loading."));
      waitforload_pleaseWaitCount++;
    }

    return false;
  }

}

// the trigger should *not* end in a period.
function go(trigger) {
  go(trigger, document.fm);
}

function go(trigger, form) {
  go(trigger, form, false);
}

// the trigger should *not* end in a period.
function go(trigger, form, removeOnBeforeUnload) {
  go(trigger, form, removeOnBeforeUnload, false);
}

function go(trigger, form, removeOnBeforeUnload, useCaution) {

  // by default, use document.fm
  if (!form) form = document.fm;

  // see if we're ready (if waitforload_ready() called)
  if (waitforload_readyToSubmit) {

    // if there's not already a submit in progress
    if (!waitforload_startedSubmit) {

      if (useCaution && typeof(Traction) != "undefined") {
	doSubmit_cautious(trigger, form, removeOnBeforeUnload, useCaution);
      } else {
	doSubmit(trigger, form, removeOnBeforeUnload, useCaution);
      }

    } 
    else {

      // alert the user that the page is in progress
      if (waitforload_bePatientCount >= 2) {
	var FORCE = prompt(i18n("waitforload2_submit_force_message", "Submit in progress, please be patient. If you are sure the submit has failed and you believe that you are receiving this message in error, type FORCE below:"), "");
	// this allows the user to override in the case that they are
	// certain the page is not submitting.
	if (FORCE && FORCE == "FORCE") {
	  waitforload_bePatientCount = 0;
	  waitforload_startedSubmit = false;
	  go(trigger, form, removeOnBeforeUnload, useCaution);
	}
      } else {
	alert(i18n("waitforload2_submit_message", "Submit in progress, please be patient."));
	waitforload_bePatientCount ++;
      }

    }

  } 
  else {

    // alert the user that the page hasn't finished loading
    if (waitforload_pleaseWaitCount >= 2) {
      alert(i18n("waitforload2_please_wait_reload_message", "Please wait for the form to finish loading.\n\nIf the form does not appear to be loading,\nyou may wish to try pressing the Reload button."));
    } else {
      alert(i18n("waitforload2_please_wait_message", "Please wait for the form to finish loading."));
      waitforload_pleaseWaitCount ++;
    }

  }

}


function doSubmit(trigger, form, removeOnBeforeUnload, useCaution) {

  waitforload_preSubmit = true;

  // set the waitforload_trigger
  form.fb_submit.value = trigger;

  // call the callback, which may be overridden
  if (!waitforload_onsubmit()) {
    return;
  }

  // set the flag so that we know for next time
  waitforload_startedSubmit = true;

  try {

    // If a rich text interface is present, update it before
    // submitting.  Support for Server14650.  This should not be done
    // here, but should be done instead by the caller's
    // waitforload_onsubmit() function if it needs to be.  [shep 24.Mar.2005]

//     if (typeof(tinyMCE) != "undefined" && tinyMCE != null) {
//       tinyMCE.triggerSave(false, true);
//     }

//     // submit the form, watching for failure
//     if (!form.submit()) {
//       // on IE, form.submit() has no return value, so this block of
//       // code is *always* executed.
//       if (!isIE) {
// 	// if the submit failed, allow it to happen again
// 	waitforload_startedSubmit = false;
//       }
//     }

    if (removeOnBeforeUnload) {
      window.onbeforeunload = null;
    }

    // Can't watch for failure because form.submit() doesn't return
    // anything a lot of times.  If users want to force another
    // attempt to submit in the event of a failure, they can keep
    // clicking until they're offered the option to force the
    // submission.  This fix introduced when addressing non-inline
    // comment submission.  See Server15360.  [shep 24.Mar.2005]

    form.submit();

  } catch (xcp) {
    // something went wrong
    waitforload_preSubmit = false;
    waitforload_startedSubmit = false;
  }

}


var waitforload_pinger = null;


function doSubmit_cautious(trigger, form, removeOnBeforeUnload, useCaution) {

  waitforload_preSubmit = true;

  var args = {
    trigger: trigger,
    form: form,
    removeOnBeforeUnload: removeOnBeforeUnload,
    useCaution: useCaution
  };

  waitforload_pinger = new Util.PingServer(waitforload_getThrobber(),
					   waitforload_getPingStatusArea(),
					   1000, 1, false,
					   null, waitforload_onpingfinish, args);

  waitforload_pinger.start();

}


function waitforload_onpingfinish(isOnline, args) {
  if (isOnline) {
    doSubmit(args.trigger, args.form, args.removeOnBeforeUnload, args.useCaution);
  } else {
    waitforload_preSubmit = false;
  }
}

function waitforload_getThrobber() {
  return null;
}

function waitforload_getPingStatusArea() {
  return null;
}
