// get params from the URL, by Andrew
// careful with non alpha_num chars in param name

function toggledisplay (id)
{
  var element = document.getElementById (id);
  if (element) {
    if (element.style.display == 'none') element.style.display = 'block';
    else element.style.display = 'none';
  }
  else alert ('This id not found: ' + id);
}

function URLparams_ctor () {
  var params = window.location.search.substring (1).split("&");
  for (i = 0; i < params.length; i ++) {
    name = params [i].substring (0, params [i].indexOf ('='));
    value = params [i].substring (params [i].indexOf ('=') + 1);
    if (name) this [name] = value;
  }
  return this;
}

var URLparams = new URLparams_ctor ();

function URLparams_assemblequery () 
{
  query = '?';
  for (property in URLparams) {
    if (query.length > 1) query += '&';
    query += escape (property) + '=' + escape (URLparams [property]);
  }
  return query;
}

function record_page_state () 
{
  // record page state in cookies
  tables = document.getElementsByTagName ('table');
  for (var i = 0; i < tables.length; i ++) {
    if (tables[i].style.display) document.cookie = escape(tables[i].id) + '=' + escape(tables[i].style.display) + ';';
  }

  divs = document.getElementsByTagName ('div');
  for (var i = 0; i < divs.length; i ++) {
    if (divs[i].style.display) document.cookie = escape(divs[i].id) + '=' + escape(divs[i].style.display) + ';';
  }

}

function clear_page_state () 
{
  // record page state in cookies
  tables = document.getElementsByTagName ('table');
  for (var i = 0; i < tables.length; i ++) {
    if (tables[i].style.display) document.cookie = escape(tables[i].id) + '=none;';
  }

  divs = document.getElementsByTagName ('div');
  for (var i = 0; i < divs.length; i ++) {
    if (divs[i].style.display) document.cookie = escape(divs[i].id) + '=none;';
  }

}


function jump () 
{
  window.record_page_state ();
  window.location.search = window.URLparams_assemblequery ();
}

function leave ()
{
  window.clear_page_state ();
  window.URLparams.service = 'exit';
  window.location.search = window.URLparams_assemblequery ();
}



function adjust_message_display ()
{
  var f = document.comment_characteristics;
  
  if (f.viewown.dirty || f.daysback.dirty) {
    if (f.viewown.dirty) {
      if (f.viewown.checked) URLparams ['viewown'] = '1';
      else URLparams ['viewown'] = '0';
    }
    if (f.daysback.dirty) {
      URLparams ['daysback'] = f.daysback.value;
    }
    record_page_state ();
    window.location.search = URLparams_assemblequery ();
  }
  
}

function hidden_ctrl (form, name, value)
{
    document.write ("<input type=hidden name='" + name + "' value='" + value + "'>");
    eval ("document." + form + "['" + name + "'].dirty=true");
}


function trim_submission (form) {
  for (i = 0; i < form.elements.length; i ++) {
    ctrl = form.elements [i];
    if (ctrl.type == 'hidden' || ctrl.type == 'file') continue;
    if (!ctrl.dirty) { ctrl.name = ''; }
    //else if (ctrl.name) alert ('sending: ' + ctrl.name);
  }
}

function form_submit (form)
{
  if (arguments.length > 1) {
    form.target='blank';
    action = arguments [1];
  }
  else action = '';
  
  form.action = action + URLparams_assemblequery ();
  //alert (form.action);
  
  if (form.method == 'post') trim_submission (form);  
  record_page_state ();
  form.submit ();
}

function form_apply (form)
{
  if (arguments.length > 1) {
    loc = arguments [1];
  }
  else loc = '';
  
  loc += URLparams_assemblequery ();
  //alert (loc);
  
  for (i = 0; i < form.elements.length; i ++) {
    ctrl = form.elements [i];
    loc += '&' + escape (ctrl.name) + '=' + escape (ctrl.value);
  }
  record_page_state ();
  //alert (loc);
  
  window.open (loc);

}


