function swap_and_submit(f,a,t) {
  var sa = f.action;
  var st = f.target;
  f.action = a;
  f.target = t;
  f.submit();
  f.action = sa;
  f.target = st;
}

function form_data_to_array(f) {
  var data = new Array();
  var e;
  for (var i = 0; i < f.elements.length; i++) {
    e = f.elements[i];
    if (e.type == 'select-multiple') {
      var j = 0;
      for (var k = 0; k < e.options.length; k++) {
        if (e.options[k].selected) {
          data[e.id+'['+j+']'] = e.options[k].value;
          j++;
        }
      }
    }
    else if (e.type == 'checkbox') {
      if (e.checked) {
        data[e.name] = e.value;
      }
    }
    else {
      data[e.name] = e.value;
    }
  }
 
  return data;
}

function element_data_to_array(o,include_empty_inputs) {
  var data = new Array();
  var objects;

  if (o.tagName.toLowerCase() == 'form') {
    objects = o.elements;
  }
  else {
    objects = o.childNodes;
  }

  var e;
  for (var i = 0; i < objects.length; i++) {
    e = objects[i];
    /*
    if (e.tagName && (e.tagName.toLowerCase() == 'div' || e.tagName.toLowerCase() == 'form')) {
      var tmp = element_data_to_array(e);
      for (var k in tmp) {
        if (typeof tmp[k] != 'function' && typeof tmp[k] != 'undefined') {
          data[k] = tmp[k];
        }
      }
    }
    */
    if (e.type == 'select-multiple') {
      var j = 0;
      for (var k = 0; k < e.options.length; k++) {
        if (e.options[k].selected) {
          data[e.id+'['+j+']'] = e.options[k].value;
          j++;
        }
      }
      if (!j) {
        // nothing was selected, so we need to make
        // sure this overwrites it
        data[e.id+'[0]'] = -1;
      }
    }
    else if (e.type == 'checkbox') {
      if (e.checked) {
        data[e.name] = e.value;
      }
      else {
        data[e.name] = 'UNCHECKED';
      }
    }
    else if (e.type == 'radio') {
      if (e.checked) {
        data[e.name] = e.value;
      }
    }
    else if (e.type != null && (e.value || include_empty_inputs)) {
      data[e.name] = e.value;
    }
  }

  var counts = new Array();
  for (var k in data) {
    if (!counts[typeof k]) {
      counts[typeof k] = 0;
    }

    counts[typeof k]++;
  }

  for (var k in counts) {
    //alert(k + ' => ' + counts[k]);
  }

  return data;
}
/*
function form_data_to_array(o) {
  // backwards compatibility
  element_data_to_array(o);
}
*/
function strip_px(x) {
  var y = x.match(/^(\d+)(px)?$/);
  if (y) {
    return Number(y[1]);
  }
  else {
    return 0;
  }
}

function table_clear(e) {
  while (e.childNodes.length > 0) {
    e.removeChild(e.firstChild);
  }
}

function table_add_row(e) {
  return e.insertRow(e.rows.length);
}

function table_add_cell(r, h) {
  var c = r.insertCell(r.cells.length);
  c.innerHTML = h;
  return c;
}

function multiple_selected(e) {
  if (!e || e.type != 'select-multiple') { return false; }
  var f = false;
  for (var i = 0; i < e.options.length; i++) {
    if (e.options[i].selected) {
      if (f) { return true; }
      f = true;
    }
  }
  return false;
}

function submitForm() {
    document.main_form.submit();
}

function array_to_http(pa) {
  var p = '';
  for (var k in pa) {
    if (typeof pa[k] != 'function') {
      if (typeof(pa[k]) == 'object') {
        pa[k] = array_to_http(pa[k]);
      }
      
      p += (p?'&':'') + k + '=' + escape(pa[k]);
    }
  }
  return p;
}

function http_to_array(h) {
  var arr = new Hash();
  var exploded = h.split('&');
  var x;
  var y;
  for (var i = 0; i < exploded.length; i++) {
    x = exploded[i].split('=');
    if (x.length == 2) {
      x[0] = unescape(x[0]);
      x[1] = unescape(x[1]);
      
      if (y = x[0].match(/(\w+)\[(\d+)\]/)) {
        if (typeof arr[y[1]] != 'object') {
          arr.set(y[1], new Hash());
        }
        arr.get(y[1]).set(y[2], x[1]);
      }
      else {
        arr.set(x[0], x[1]);
      }
    }
  }

  return arr;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function get_selected_radio(n) {
  var e = document.getElementsByName(n);
  for (var i = 0; i < e.length; i++) {
    if (e[i].checked) {
      return e[i].value;
    }
  }
  return -1;
}

function js_countTextAreaChars(text) {
  var n = 0;
  for (var i = 0; i < text.length; i++) {
    if (text.charAt(i) != "\r") {
      n++;
    }
  }
  return n;
}

function js_CursorPos(start, end) {
  this.start = start;
  this.end = end;
}

function js_getCursorPosition(textArea) {
  var start = 0;
  var end = 0;
  textArea.focus();
  var sel1 = document.selection.createRange();
  var sel2 = sel1.duplicate();
  sel2.moveToElementText(textArea);
  var selText = sel1.text;
  sel1.text = String.fromCharCode(28);
  var index = sel2.text.indexOf(String.fromCharCode(28));
  start = js_countTextAreaChars((index == -1) ? sel2.text : sel2.text.substring(0, index));
  end = js_countTextAreaChars(selText) + start;
  sel1.moveStart("character", -1);
  sel1.text = selText;

  return new js_CursorPos(start, end);
}

function js_setCursorPosition(textArea, cursorPos) {
  var sel = textArea.createTextRange();
  sel.collapse(true);
  sel.moveStart("character", cursorPos.start);
  sel.moveEnd("character", cursorPos.end - cursorPos.start);
  sel.select();

  textArea.focus();
}

function insert_into_textarea(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    myField.focus();
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
      + myValue
      + myField.value.substring(endPos, myField.value.length);
    myField.focus();
    myField.selectionStart = startPos + myValue.length;
    myField.selectionEnd = startPos + myValue.length;
  } else {
    myField.value += myValue;
    myField.focus();
  }
}

var ajax_opt = {
  method: 'post',
  onFailure: function(e) {},
  onException: function(t) {}
};

function pop(x,y,w,h,s,m,r){var z=window.open(x,y,"height="+h+"px,width="+w+"px,scrollbars="+(s?"yes":"no")+(m?",menubar":"")+(r?",resizable=yes":""));z.focus();return z;}
function newRecord(){var x=window.open("/records/new/","sage_edit_record","height=525px,width=700px,top=50px,left=50px,status=yes");x.focus();}
function popinfo(v,b){pop('/common/info/'+v+'.php','sage_info_pop',500,400,b);}
function pophelp(v,b){pop('/common/help/'+v+'.html','sage_help_pop',600,500,b);}
function emessenger() {
  var z=window.open('http://emessenger.sage-systems.com/','sage_emessenger',"height=600px,width=800px,scrollbars=yes,resizable=yes");
  z.focus();
}

