
var ClearFormGlobalArray = new Array();
var OnLoadGlobalArray = new Array();
var OnSubmitGlobalArray = new Array();
var OnLoadGlobalLast = new Array();
var OnUnLoadGlobalArray = new Array();
var PageFormsGlobalArray = new Array();
var ChildWindowsGlobalArray = new Array();

function onLoadGlobal()
{
  var i = 0;
  for (i = 0; i < OnLoadGlobalArray.length; i++) {
    OnLoadGlobalArray[i]();
  }
  for (i = 0; i < OnLoadGlobalLast.length; i++) {
    OnLoadGlobalLast[i]();
  }
}

function unLoadGlobal()
{
  var i = 0;
  for (i = 0; i < OnUnLoadGlobalArray.length; i++) {
    OnUnLoadGlobalArray[i]();
  }
  for (i = 0; i < ChildWindowsGlobalArray.length; i++) {
    ChildWindowsGlobalArray[i].close();
  }
}

function onSubmitGlobal()
{
  var i = 0;
  for (i = 0; i < OnSubmitGlobalArray.length; i++) {
    OnSubmitGlobalArray[i]();
  }
}


function HideOneZoneKeepSpace(hide,which)
{
  var ted = document.getElementById(which);
  if (ted == null) {
    //alert('HideOneZone:ted == null:'+which);
  } else {
    if (hide) {
      ted.style.visibility = 'hidden';
      ted.style.display = '';
    } else {
      ted.style.visibility = 'visible';
      ted.style.display = '';
    }
  }
}

function HideOneZone(hide,which)
{
  var ted = document.getElementById(which);
  if (ted == null) {
    alert(':ted == null:');
  } else {
    if (hide) {
      ted.style.visibility = 'hidden';
      ted.style.display = 'none';
    } else {
      ted.style.visibility = 'visible';
      ted.style.display = '';
    }
  }
}

function IsRequired(which)
{
  var ted = document.getElementById(which);
  //debugMessage('IsRequired:'+which+":\n");
  if (ted == null) {
    alert('IsRequired:'+which+":\n");
    return 0;
  }
  return (ted.className == "required_decoration");
}

function FixOneRequired(require,which)
{
  var ted = document.getElementById(which);
  //debugMessage('FixOneRequired:'+which+":"+require+"\n");
  if (ted == null) {
    alert(':ted == null:');
  } else {
    if (require) {
      ted.className = "required_decoration";
    } else {
      ted.className = "no_decoration";
    }
  }
}


function ClearForm(formName)
{
  document.forms[formName].reset();
  for (var cnt = 0;cnt < ClearFormGlobalArray.length;cnt++) {
    ClearFormGlobalArray[cnt](); 
  }
}

function AreWeSavable(base_name)
{
  var save_zone = base_name + '_save_button';
  //var save_close_zone = base_name + '_save_close_zone';
  var save_new_zone = base_name + '_save_new_zone';
  if (ElementExists(save_zone) && IsNotDisabled(save_zone)) {
    return 1;
  }
  //if (ElementExists(save_close_zone) && IsVisible(save_close_zone)) {
  //  return 1;
  //}
  if (ElementExists(save_new_zone) && IsNotDisabled(save_new_zone)) {
    return 1;
  }
  return 0;
}

function NavigationIsOK(url) 
{
  var changes ;
  var changed = new Array;
  if (PageFormsGlobalArray.length <= 0) {
    return 1;
  }
  for (var cnt = (PageFormsGlobalArray.length - 1)  ; cnt >= 0 ; cnt-- ) {
    changes = 0;
    //debugMessage(PageFormsGlobalArray.length+":"+cnt);
    //debugMessage(PageFormsGlobalArray[cnt]);
    var form = document.forms[PageFormsGlobalArray[cnt]];
    if (form == null) { 
      return 1;
    }
    var elements = document.forms[PageFormsGlobalArray[cnt]].elements;
    //debugMessage(elements.length);
    for (var elCnt = 0; elCnt< elements.length; elCnt++) {
      //debugMessage(elements[elCnt].type);
      if (elements[elCnt].type == 'button' && (elements[elCnt].checked != elements[elCnt].defaultChecked)) {
        changes++;
      } else if (elements[elCnt].type == 'file' && elements[elCnt].value != '') {
        changes++;
      } else if (elements[elCnt].type == 'password' && (elements[elCnt].value != elements[elCnt].defaultValue)) {
        changes++;
      } else if (elements[elCnt].type == 'radio' && (elements[elCnt].checked != elements[elCnt].defaultChecked)) {
        changes++;
      } else if (elements[elCnt].type == 'textarea' && (elements[elCnt].value != elements[elCnt].defaultValue)) {
        changes++;
      } else if (elements[elCnt].type == 'select-one' || elements[elCnt].type == 'select-multiple') {
        var options = elements[elCnt].options;
        for (var optCnt = 0; optCnt < options.length; optCnt++) {
          //debugMessage("options["+optCnt+"]: "+options[optCnt].selected+":"+options[optCnt].defaultSelected);
          if (options[optCnt].selected != options[optCnt].defaultSelected) {
            changes++;
          }
        }
      //} else if (elements[elCnt].type == 'hidden' && (elements[elCnt].value != elements[elCnt].defaultValue)) {
        // This is realy realy bad, IE hits this code all the time because it does not store an defaultValue for
        // hidden fields. Or perhaps IE does and Mozilla does not. Need some more poking here.
        //alert (elements[elCnt].name+':'+elements[elCnt].value+':'+elements[elCnt].defaultValue);
      //  changes++;
      } else if (elements[elCnt].type == 'text' && (elements[elCnt].value != elements[elCnt].defaultValue)) {
        changes++;
      }
      var pat = /^ARRAY_(.*)$/;
      var result;
      if ((result = pat.exec(elements[elCnt].name)) != null) {
        var name = RegExp.$1;
        //alert(elements[elCnt].name+':'+name);
        var other_name = 'STARTING_ARR_'+name;
        if (elements[elCnt].value != elements[other_name].value) {
          changes++
        }
      }
      //debugMessage("Changes: "+changes);
    }
    if (changes) {
      changed.push(PageFormsGlobalArray[cnt]);
    }
  }
  for (var cnt=0 ; cnt < changed.length ; cnt++) {
    var pat = /(.*)_editform/;
    pat.exec(changed[cnt]);
    var base_name = RegExp.$1;
    if (AreWeSavable(base_name)) {
      if (confirm('Data on this screen has been modified but not saved!\nPress OK to save data and proceed.\nPress Cancel to proceed without saving data.')) {
        var subaction_string = base_name + '_subaction';
        //debugMessage(changed[cnt]);
        //debugMessage(":"+subaction_string+":");
        if (changed.length > 1) {
          document.forms[changed[cnt]].target = document.forms[changed[cnt]].name;
          document.forms[changed[cnt]].elements[subaction_string].value = 'close_window';
          document.forms[changed[cnt]].submit();
        } else {
          document.forms[changed[cnt]].elements['relocation_url'].value = url;
          document.forms[changed[cnt]].elements[subaction_string].value = 'relocation';
          document.forms[changed[cnt]].submit();
          return 0;
        }
      }
    } else {
      //if (confirm('Data on form '+changed[cnt]+' has been modified but not saved!\nThe form however has not been completed and cannot be saved!\nPress OK to return to form and finish entry.\nPress Cancel to proceed without saving data.')) {
      if (confirm('Data has been modified but not saved!\nThe form however has not been completed and cannot be saved!\nPress OK to return to form and finish entry.\nPress Cancel to proceed without saving data.')) {
       HideOneZone(1,base_name+'_waitmessage_row');
       hideAddsSavesNew(base_name);
       HideOneZone(0,base_name+'_allbuttons_row');
       HideOneZoneKeepSpace(0,base_name+'_required_instruction_row');
       return 0;
      } else {
       return 1;
      }
    }
  }
  return 1;
}

function SafeNavigationChild(url,winName,features) 
{
  if (NavigationIsOK(url)) {
    return DoPopup(url,winName,features);
  } else {
    return;
  }     
}

function UnSafeNavigationChild(url,winName,features) 
{
  return DoPopup(url,winName,features);
}

function SafeNavigation(url) 
{
  if (NavigationIsOK(url)) {
    window.open(url,'_self');
  } else {
    return;
  }     
}

function IsSelected(anArray,anValue)
{
  var i;
  for (i = 0; i < anArray.length; i++) {
    if (anArray[i].key == anValue) {
      return 1;
    }
  }
  return 0;
}

function IsInArr(arr,val)
{
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == val) {
      return 1;
    } 
  }
  return 0;
}

var auto_logout_minutes = 0;
var auto_logout_suppressed = 0;

function SetUpAutoLogout()
{
  if (auto_logout_suppressed == 1) {
    return;
  }
  if (auto_logout_minutes == 0 || isNaN(auto_logout_minutes)) {
    setTimeout("FireAutoLogout()",60000 * 999);
  } else {
    setTimeout("FireAutoLogout()",60000 * auto_logout_minutes);
  }
}

OnLoadGlobalArray.push(SetUpAutoLogout);

function FireAutoLogout()
{
  return(window.open('/index.html?login_message=expired','_self',''));
}

// global flag
var isIE = false;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url,processReqChange) {
var request_obj;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        request_obj = new XMLHttpRequest();
        request_obj.onreadystatechange = processReqChange;
        request_obj.open("GET", url, true);
        request_obj.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        request_obj = new ActiveXObject("Microsoft.XMLHTTP");
        if (request_obj) {
            request_obj.onreadystatechange = processReqChange;
            request_obj.open("GET", url, true);
            request_obj.send();
        }
    }
    return (request_obj);
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            if (result.firstChild) {
              return result.firstChild.nodeValue;    		
            } else {
              if (result.childNodes[1]) {
                return result.childNodes[1].nodeValue;
              } else {
                return "ERROR";
              }
            }
        }
    } else {
        return "n/a";
    }
}





