/////
//
//	User Interface JavaScript Utilities
//
//	Thu Jan 18 12:13:59 EST 2001 - F Harvell
//
/////
//
//	Copyright &copy; 2001 - 2008 First Time Software
//
//	All rights reserved worldwide. Usage rights have been granted
//	to the website owner according to the terms and conditions of
//	the Sacoiwa License Agreement.
//
/////
//
//  Version 4.0, $Id: ui.js 1724 2008-09-12 19:08:57Z fharvell $
//
/////


// manage a helpinfo popup
function
hiPopup(
  box,
  v
) {
  var x, y, p;

  if ('string' != typeof(box)) { box = box.id; }
  if (x = box.match(/(.*)\[(.*)\]\[(.*)\]/)) { box = x[1] + '[' + x[2] + '][' + x[3] + ']'; }
  if (-1 == box.search(/_helpinfo$/)) { box += '_helpinfo'; }
  if (p = getE(box)) {
    var s = p.style ? p.style : p;
    var i = getE('sf_Shim'); if (i) { i = i.style; }

    switch (v) {
      // set hide timer
      case 0:
	setTimeout("hiPopup('" + box + "', 2)", 250);
	break;

      // show
      case 1:
	s.zIndex = 2;
	s.visibility = 'visible';
	if (document.all && i) {
	  i.top = p.offsetTop + 10;
	  i.left = p.offsetLeft + 3;
	  i.width = p.offsetWidth - 6;
	  i.height = p.offsetHeight - 13;
	  i.zindex = 1;
	  i.display = 'block';
	}
	y = (p.offsetTop + p.offsetHeight) - ((window.pageYOffset ? window.pageYOffset : (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)) + (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight))) + 15;
	if (0 < y) { window.scrollBy(0, y); }
	break;

      // hide
      case 2:
	s.visibility = 'hidden';
	s.zIndex = -1;
	if (document.all && i) {
	  i.zindex = -1;
	  i.display = 'none';
	}
	break;
    }
  }
}

// clear each text input in an input's form
function
clearText(
  obj
) {
  var f = obj.form;
  var l = f.length;

  for (var i = 0; i < l; i++) { if (f[i].type == 'text') { f[i].value = ''; } }
}

// set each text input in an input's form to the next number in a sequence
function
sequenceText(
  obj,
  s
) {
  var f = obj.form;
  var l = f.length;
  s = s ? s : 1;

  for (var i = 0; i < l; i++) { if (f[i].type == 'text') { f[i].value = s++; } }
}

// convert a date to ISO format
function
ISODate(
  obj,
  dt,
  t
) {
  if (dt) { obj.value = dt; obj.focus(); obj.select(); return true; }
  var s = obj.value = tsp(obj.value);

  if (s) {
    if (t) { t = s.split(' '); s = t[0]; }
    if (-1 == s.search(/(\d+)\D(\d+)\D(\d+)/)) { d = new Date(); s += '/' + (d.getYear() < 2000 ? d.getYear() + 1900 : d.getYear()); }
    s = s.replace(/(\d+)\D(\d+)\D(\d+)/, "$1/$2/$3");
    p = s.split('/');
    if (3 == p.length) {
      if (4 == p[0].length || 12 < p[0]) {
	s = s.replace(/(\d+).(\d+).(\d+)/, "$2/$3/$1");
      }
    } else if (-1 == s.search(/\D/)) {
      if (8 > s.length) {
	if (12 < s.substring(0, 2)) {
	  s = s.replace(/(\d\d)(\d\d)(\d\d)/, "$2/$3/$1");
	} else {
	  s = s.replace(/(\d\d)(\d\d)(\d\d)/, "$1/$2/$3");
	}
      } else {
	s = (1231 < s.substring(0, 4)) ? s.replace(/(\d\d\d\d)(\d\d)(\d\d)/, "$2/$3/$1") : s.replace(/(\d\d)(\d\d)(\d\d\d\d)/, "$1/$2/$3");
      }
    }
    s = Date.parse(s) || s;

    if (s) {
      d = new Date(s);
      d.getYear() < 38 && d.setYear(d.getYear() + (-1 == obj.value.search(/19\d\d/) ? 2000 : 0));
      if (d.getTime()) {
	obj.value = (d.getYear() < 2000 ? d.getYear() + 1900 : d.getYear()) + '-' + (d.getMonth() < 9 ? '0' : '') + (d.getMonth() + 1) + '-' + (d.getDate() <= 9 ? '0' : '') + d.getDate() + ((t && t[1]) ? ' ' + t[1] : '');
	return true;
      } else {
	alert("An invalid date has been entered.\nPlease re-enter the date.");
	obj.focus();
	obj.select();
	return false;
      }
    }
  }

  return true;
}

// convert an email address to proper format
function
eAddr(
  obj
) {
  var s = obj.value = tsp(obj.value);

  if (s) {
    if (-1 == s.search(/^[^ \t]+@(([0-9a-z][0-9a-z-]*\.)+[a-z]{2,4})$/i)) {
      alert("An invalid email address has been entered.\nPlease re-enter the address.");
      obj.focus();
      obj.select();
      return false;
    } else {
      obj.value = s;
      return true;
    }
  }

  return true;
}

// convert an internet address to proper format
function
iAddr(
  obj
) {
  var s = obj.value = tsp(obj.value);
  var invalid = false;

  if (s) {
    if ('/' == s.charAt(0)) {
      invalid = (-1 == s.search(/^(\/[^ \t]*)*$/i));
    } else {
      if (-1 == s.search(/^https*:\/\//i)) { s = 'http://' + s; }
      invalid = (-1 == s.search(/^https*:\/\/([0-9a-z][0-9a-z-]*\.)+[a-z]{2,4}(\/[^ \t]*)*$/i));
    }

    if (invalid) {
      alert("An invalid internet address has been entered.\nPlease re-enter the address.");
      obj.focus();
      obj.select();
      return false;
    } else {
      obj.value = s;
      return true;
    }
  }

  return true;
}

// update checklist group inputs
function
clUpdate(
  obj,
  i
) {
  var c = obj.checked;
  var f = obj.form;
  var e;
  var x;

  for (var n in i) {
    e = getE(n);
    if (c) {
      if (!e) {
	e = document.createElement('input');
	if (document.all) {
	  e.type = 'hidden';
	  e.id = n;
	  e.name = n;
	} else {
	  e.setAttribute('type', 'hidden');
	  e.setAttribute('id', n);
	  e.setAttribute('name', n);
	}

	f.appendChild(e);
      }

      e.value = i[n];
    } else {
      e.value = null;
      e.parentNode.removeChild(e);
    }
  }

  return true;
}

// open loading window and call application to load options
function
dateTool(
  obj,
  u,
  i,
  t
) {
  u = u + '?token=' + t + '&form=' + obj.form.name + '&st=' + escape(obj.form['sf_datetool' + i].value);
  toolWin = window.open(u, 'datetool', 'dependent,width=755,height=580,scrollbars');
  toolWin.focus();

  return false;
}

// open loading window and call application to load options
function
fileTool(
  obj,
  u,
  i,
  t
) {
  u = u + '?token=' + t + '&form=' + obj.form.name + '&ft=' + escape(obj.form['sf_filetool' + i].value);
  toolWin = window.open(u, 'filetool', 'dependent,width=755,height=580,scrollbars');
  toolWin.focus();

  return false;
}

// set an input option
function
setOpt(
  f,
  e,
  v
) {
  var d = window.opener.document;
  var s = d.f ? d.f.elements[e].options : d.getElementById(e).options;
  s.length = 0;
  s.length = 1;
  s[0].text = s[0].value = v;
  s.selectedIndex = 0;
}

// open loading window and call application to load options
function
selectTool(
  obj,
  u,
  p,
  t
) {
  u = u + '?token=' + t + '&form=' + obj.form.name + '&st=' + escape(obj.form['sf_st:' + p].value);
  toolWin = window.open(u, 'selecttool', 'dependent,width=755,height=580,scrollbars');
  toolWin.focus();

  return false;
}

