window._dx_url = '?&filterset=';
window._dx_images_gif_path = 'dxprod/v1.0.045/images/gif';

function mouseoverHighlight(tableRow) {
  tableRow.style.backgroundColor = '#aed368';
}

function mouseoutHighlight(tableRow) {
  tableRow.style.backgroundColor = '';
}

function mousedownHide(ev) {
  if (!ev) ev = window.event;
  ev.returnValue = false;
  return false;
}

function setPref($name, $val) {
  var aj = new Ajax.Request(
      window._dx_url, {
        method: 'get',
        parameters: {action: 'prefs', pref: $name, prefval: $val}
      });
}

function hideHelp(ev, $name)
{
  if (!ev) ev = window.event;

  if (window._help_action_disabled == true) {
    ev.returnValue = false;
    return false;
  }

  var h = $('outer_help');
  var hb = $('helpbutton');

  window._help_action_disabled = true;

  new Effect.SlideUp(h);
  new Effect.Appear(hb, {afterFinish: helpEnableAction});

  setPref($name, 'hide');

  ev.returnValue = false;
  return false;
}

function showHelp(ev, $name)
{
  if (!ev) ev = window.event;

  if (window._help_action_disabled == true) {
    ev.returnValue = false;
    return false;
  }

  var h = $('outer_help');
  var hb = $('helpbutton');

  window._help_action_disabled = true;

  new Effect.Fade(hb);
  new Effect.SlideDown(h, {afterFinish: helpEnableAction});

  setPref($name, 'show');

  ev.returnValue = false;
  return false;
}

function helpEnableAction() {
  window._help_action_disabled = false;
}

function showAddNew(ev) {
  if (!ev) ev = window.event;

  if (window._add_action_disabled == true) {
    ev.returnValue = false;
    return false;
  }
  
  window._add_action_disabled = true;
  new Effect.SlideDown($('newdrop'), {afterFinish: showAddNew_Finish});
  
  ev.returnValue = false;
  return false;
}

function showAddNew_Finish() {
  window.location = window.location+'#add_new_marker';
}

function addEnableAction() {
  window._add_action_disabled = false;
}

function showPrintOps(ev) {
	if (!ev) ev = window.event;
  $('printops').style.display = ($('printops').style.display == ''?'none':'');
  return ev.returnValue = false;
}

function updateElementAjax(element, params, onComplete) {
    window._ajax = {};

  _DoOverlayPleaseWait();
  
  params = params + '&nocache=' + Math.random()*999999;

  _ajax.element = $(element);
  _ajax.onComplete = onComplete;
  _ajax.updater = new Ajax.Updater(
    $(element),
    window._dx_url,
    { method: "post",
      parameters: params,
      onComplete: defaultOnComplete,
      onFailure: defaultOnFailure,
      evalScripts: true});
}

function defaultOnComplete(request) {
  var div = $(window._ajax.element);

  if (_ajax.onComplete)
    _ajax.onComplete(request);

  _DoOverlayHide();
}

function defaultOnFailure() {
  alert('Digital xPress was unable to contact the server. Please try again.');
}

function inspect(o) {
  for (prop in o)
    alert(prop+': '+o[prop]);
}

/*
  Timeout textbox functions
*/

function makeTextTimeout(id, ontimeout, timeout)
{
  //Take an <INPUT TYPE=TEXT...> and make it automatically call a callback when a delay in type
  //entry is encountered

  var el;

  timeout = timeout?timeout:750;
  el = $(id);
  el._timeoutMs = timeout;
  el._timerId = null;
  el.onkeyup = _tt_keyup;
  el.onblur = _tt_blur;

  //note el.ontimeout is the event called in response to a delay in text entry
  el.ontimeout = ontimeout;
}

function _tt_keyup()
{
  //When a key is released on a timout textbox, start a countdown timer
  var cmd;

  if (this._timerId != null)
    clearTimeout(this._timerId);

  cmd = "_tt_evTimeout(\""+this.id+"\")";
  this._timerId = setTimeout(cmd, this._timeoutMs);
}

function _tt_blur()
{
  //When the focus changes from a timeout textbox, clear the timer (if one is set) and run immediately
  if (this._timerId != null)
  {
    clearTimeout(this._timerId);
    _tt_evTimeout(this.id);
  }
}

function _tt_evTimeout(id)
{
  //A timeout has occurred, cleanup and then call the ontimeout event passed in for this element
  var el = $(id);
  el._timerId = null;
  el.ontimeout();
}

function _setCookie(name, value, expireDays) {
  var n = new Date();
  var e = new Date(n.getTime() + 1000*60*60*24*expireDays);
  document.cookie = name+"="+escape(value)+";expires="+e.toGMTString();
}

function _getCookie(name) {
  var c = document.cookie;
  var n = name+'=';

  var ix = c.indexOf(n);
  if (ix == -1)
    return "";

  var c = c.substr(ix+n.length);
  ix = c.indexOf(';');
  if (ix == -1)
    return unescape(c);
  else
    return unescape(c.substr(0, ix));
}

function objectToString(o) {
  var s = '';
  for (p in o) {
    if (p == '')
      continue;
    s = s + p + '=' + o[p] + ';';
  }
  return s;
}

function stringToObject(s) {
  var o = {};
  var pcs = s.split(';');
  for (var ix = 0; ix < pcs.length; ix++) {
    if (pcs[ix] == '')
      continue;

    var vals = pcs[ix].split('=');
    o[vals[0]] = vals[1];
  }
  return o;
}

function getQuerystringValue(prop) {
  var s = window.location.search.substring(1);
  var ix = s.indexOf(prop+'=');
  if (ix == -1)
    return "";
  s = s.substr(ix+prop.length+1);
  ix = s.indexOf('&');
  if (ix == -1)
    return unescape(s);
  else
    return unescape(s.substr(0, ix));
}

/* Filter helpers */

function filterSetElements(names, pageName) {
  for (var ix = 0; ix < names.length; ix++) {
    if ($(names[ix]) == null)
      alert('Internal error - bad element ['+names[ix]+'] in setFilterElements');
  }
  window._filterElements = names;
  window._filterPageName = pageName;
  window._filterSet = getQuerystringValue('filterset');
  if (window._filterSet == '')
    alert('Internal error - no filterset specified');
}

function filterSaveCriteria() {
  var o = {};
  for (var ix = 0; ix < window._filterElements.length; ix++) {
    var e = $(window._filterElements[ix]);
    if (e.value != '')
      o[e.id] = e.value;
  }
  _setCookie(window._filterPageName+'_'+window._filterSet, objectToString(o), 0.5);
}

function filterLoadCriteria() {
  var o = stringToObject(_getCookie(window._filterPageName+'_'+window._filterSet));
  
  for (var ix = 0; ix < window._filterElements.length; ix++) {
    var e = $(window._filterElements[ix]);
    if (o[e.id]) {
      e.value = o[e.id];
      if (e.onchange)
        e.onchange();
    }
  }
}