/* (PUBLIC) FUNCTIONS DEFINED HEREIN:

  function openNotesWindow(CompX, Value, Edit, winx, winy)
  function openChildWindow(Page, winx, winy, params)
  function closeWindowIn(numMS)
  function closeChildWindow()
  function loadDefaults()
  function limitText(fieldName, Length)
  function saveWindowPosition()
  function scrollRestore()
  function getBrowserVersion()

** END OF FUNCTION DEFINITIONS */

/***** GLOBAL FUNCTIONS *****/

var win = null; // used as pointer to child window.
var pageName, lastPageName = "";

function saveWindowPosition(fieldName) {
  var form = document.Form;
  if (form == null || form.YOffset == null) return;
  if (window.pageYOffset != null) { // For Netscape
    form.YOffset.value = window.pageYOffset;
  } else if (document.body.scrollTop != null) { // For IE
    form.YOffset.value = document.body.scrollTop;
  }
  if (form.LastFocus != null) form.LastFocus.value = fieldName;
}

function scrollRestore() {
  var form = document.Form;
  if (form == null) return;
  if (form.YOffset != null) {
    var y = parseInt(form.YOffset.value);
    if (isNaN(y)) y = 0;
    window.scrollTo(0, y);
  }
  if (form.LastFocus != null) {
    var setFocus = eval("form."+form.LastFocus.value);
    if (setFocus != null) setFocus.focus();
  }
}

function openChildWindow(pageName, winx, winy, params) {
  if (winx == null) winx = 525;
  if (winy == null) winy = 280;
  var t = Math.floor(((screen.height - winy) / 2) * 0.8);
  var l = Math.floor((screen.width   - winx) / 2);
  var prefParams = "screenX=" + l + ",screenY=" + t + ",height=" + winy + ",width=" + winx + ",";
  if (params == null || params == "") {
    params = prefParams + "status=1,resizable=1";
  } else {
    params = prefParams + params;
  }
  if (win != null && win.closed == false) win.close();
  win = open(pageName, "ChildWindow", params);
  win.moveTo(l,t);
  win.focus();
}

function openNotesWindow(CompX, Value, Edit, winx, winy) {
  var params = "";
  if (CompX != "ClientDetails") {
    pageName = "../include/NotePadEditor.asp?Field=" + CompX + "&Value=" + Value + "&Edit=" + Edit;
  } else {
    pageName = "../include/ClientDetails.asp?Value=" + Value;
    params = "scrollbars=1,";
    if (winx == null) { winx = 640; winy = 400; }
  }
  if (win != null && win.closed == false) win.close();
  if (winx == null) winx = 525;
  if (winy == null) winy = 280;
  params += "status,screenX=" + l + ",screenY=" + t + ",height=" + winy + ",width=" + winx + ",resizable=1";
  var t = Math.floor(((screen.height - winy) / 2) * 0.8);
  var l = Math.floor((screen.width   - winx) / 2);
  win = open(pageName, "NotesWindow", params);
  win.moveTo(l,t);
  win.focus();
}

/***** END OF GLOBALS *****/

function selectHandleKey(evt) {
  if (event.keyCode == 27) { 
    this.subStr = null;
    return false;
  }
  var origStr = this.subStr;
  if (this.subStr == null) this.subStr = "";
  var theChar = String.fromCharCode(event.keyCode);
  var idx;
  this.subStr = this.subStr + theChar.toUpperCase();
  for (idx = 0; idx < this.length; idx++) {
    var tmp = this[idx].firstChild;
    if (tmp != null && tmp.nodeValue.indexOf(this.subStr) == 0) {
      this[idx].selected = true;
      return false;
    }
  }
  if (idx == this.length) {
    this.subStr = origStr;
  }
  return false;
}
  
function selectClearStr() {
  if (this.subStr == null) return true;
  this.subStr = null;
  if (this.onchange != null) {
    for (var i = 0; i < this.length; i++) {
      if (this[i].selected == true) {
        var tmp = this[i].firstChild;
        if (tmp != null && this.lastVal != tmp.nodeValue || 
            tmp == null && this.lastVal == "") {
          this.onchange();
          break;
        }
      }
    }
  }
  this.lastVal = null;
  return true;
}
  
function selectSaveState() {
  for (var i = 0; i < this.length; i++) {
    if (this[i].selected == true) {
      var tmp = this[i].firstChild;
      if (tmp == null)
        this.lastVal = "";
      else
        this.lastVal = tmp.nodeValue;
      break;
    }
  }
  return true;
}

function selectInit() {
  var selColn = document.getElementsByTagName("select");
  for (var i = 0; i < selColn.length; i++) {
    selColn[i].onkeypress = selectHandleKey;
    selColn[i].onblur     = selectClearStr;
    selColn[i].onfocus    = selectSaveState;
  }
}

function doShow() {
  if (x.style.display == "none") 
    x.style.display = "";
  else
    x.style.display = "none";
}

/******** ANCHOR COLOURS, CHANGE ON FOCUS ***********/

function anchorInit() {
  var anchorColn = document.getElementsByTagName("a");
  for (var i = 0; i < anchorColn.length; i++) {
    var tmp = anchorColn[i];
    if (tmp.firstChild != null) {
      var tag = tmp.firstChild.tagName;
      if (tag == null || tag.toUpperCase() != "IMG") {
        tmp.onfocus = anchorFocus;
        tmp.onblur  = anchorBlur;
      }
    }
  }
}

function anchorFocus() { this.className = "focus"; }
function anchorBlur()  { this.className = "blur";  }

/******** LIMIT INPUT LENGTH IN TEXTAREAS ***********/

// call on events change and keypress, eg onchange="limitText(this,280)"
function limitText(field, len) { 
  var data = field.firstChild;
  if (data == null) return;
  data = data.nodeValue;
  if (data.length >= len) {
    if (event.type == "change" && data.length > len) {
      alert("Text will be truncated to fit in the field.\n" +
            "The field can not contain more than " + len + " characters.");
    }
    data = data.substring(0, len);
    data = data.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"");
    data = data.replace(/\n/g, "\\n").replace(/\r/g, "\\r");
    setTimeout("document.Form."+field.name+".firstChild.nodeValue=\""+data+"\"",5);
  }
}

/****************************************************/

function loadDefaults() {
  if (navigator.appVersion.indexOf("MSIE") < 0) return;
  selectInit();
  anchorInit();
}

var openerCloseWindowInRef = null;

function closeWindowIn(timeMS) {
  if (timeMS == null) timeMS = 5000;
  if (window.opener != null) 
    openerCloseWindowInRef = window.opener 
  else 
    openerCloseWindowInRef = null;
  if (window != null && window.closed == false) {
    setTimeout("window.close()", timeMS);
  }
  setTimeout("setFocusOnOpener()", timeMS);
}

function setFocusOnOpener() {
  if (openerCloseWindowInRef != null && openerCloseWindowInRef.closed == false) {
    openerCloseWindowInRef.focus();
  }
  openerCloseWindowInRef = null;
}

function closeChildWindow() {
  if (window.opener != null && window.opener.closed == false) window.opener.focus();
  window.close();
}

// Returns -1 if not Internet Explorer, else returns the version of IE.
function getBrowserVersion() {
  var tmp = navigator.appVersion;
  var ix = tmp.indexOf("MSIE");
  if (ix == -1) return -1;
  tmp = tmp.substring(ix+4, tmp.length);
  var vsn = new String(parseFloat(tmp) * 100);
  if (vsn.length < 3) return vsn;
  return vsn.substring(0,1) + "." + vsn.substring(1,3);
}

