

var Event = {
  add: function(obj,type,fn) {
    if (obj.attachEvent) {
      obj['e'+type+fn] = fn;
      obj[type+fn] = function() { obj['e'+type+fn](window.event); }
      obj.attachEvent('on'+type,obj[type+fn]);
    } else
    obj.addEventListener(type,fn,false);
  },
  remove: function(obj,type,fn) {
    if (obj.detachEvent) {
      obj.detachEvent('on'+type,obj[type+fn]);
      obj[type+fn] = null;
    } else
    obj.removeEventListener(type,fn,false);
  }
}

 
function  getElementsByClass (searchClass, domNode, tagName) {
  if (domNode == null) domNode = document;
  if (tagName == null) tagName = '*';
  var el = new Array();
  var tags = domNode.getElementsByTagName(tagName);
  var tcl = " "+searchClass+" ";
  for(i=0,j=0; i<tags.length; i++) {
    var test = " " + tags[i].className + " ";
    if (test.indexOf(tcl) != -1)
      el[j++] = tags[i];
  }
  return el;
}


function  nice_all() {
  elems = document.getElementsByTagName('label');
  for (n = 0; (e = elems[n]); n++)
    Event.add (e, 'mouseover', function(){nice_label(this)});
  elems = document.getElementsByTagName('input');
  for (n = 0; (e = elems[n]); n++)
    if (e.type != 'text' && e.type != 'password' && e.type != 'file')
      Event.add (e, 'mouseover', function(){nice_basic(this)});
  elems = document.getElementsByTagName('select');
  for (n = 0; (e = elems[n]); n++)
    Event.add (e, 'mouseover', function(){nice_basic(this)});
  elems = getElementsByClass ('unselectable');
  for (n = 0; (e = elems[n]); n++)
    unselectable (e);
}     


function  isset (
  variable)
{
  return (typeof (variable) != 'undefined');
}


function clamp (
  value,
  min,
  max)
{
  return (((value) > (max)) ? (max) : (((value) < (min)) ? (min) : (value)));
}


function comma_formatted (
  nStr)
{
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? ',' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + '.' + '$2');
  }
  return x1 + x2;
}


function  is_numeric (value) {
  return (value.toString().match(/^[-]?\d*\.?\d*$/) ? true : false);
}


function strip_slashes (
  str)
{
  return (str.replace (/\\/g, ''));
}


function getcookie (c_name)
{
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(decodeURIComponent(document.cookie.substring(c_start,c_end)));
    } 
  }
  return "";
}


function setcookie (name, value, days) 
{
  if (days) {
    if (days < 0)
      days = 5000;
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/";
}


function  date_zero_pad (dd) { 
  return (dd < 10 ? '0' + dd : dd);
}
 
 
function  set_inner (id, value) {
  document.getElementById(id).innerHTML = value;
}


function email_current_page (
  subject,
  text)
{
  mail_str = "mailto:?subject=" + subject;
  mail_str += "&body=" + text + encodeURI ("\n") + location.href;
  location.href = mail_str;
}


function unselectable (e) 
{
  e.onselectstart = function(){return false};
  e.unselectable = 'on';
  e.style.MozUserSelect = 'none';
}


function nice_basic (e)
{                                      
  if (!e.disabled)
    e.style.cursor = 'pointer';
}


function  nice_label (e) 
{
  le = document.getElementById(e.htmlFor);
  if (!le.disabled && !(le.readOnly && (le.type!='checkbox' && le.type!='radio')))
    e.style.cursor = 'pointer';
  unselectable (e);
}


function  load_js (url)
{
  var e = document.createElement("script");
  e.type = "text/javascript";
  e.src = url;
  document.getElementsByTagName('head')[0].appendChild(e);
}


function  load_css (url)
{
  var e = document.createElement('link');
  e.type = 'text/css';
  e.rel = 'stylesheet';
  e.href = url;
  document.getElementsByTagName('head')[0].appendChild(e);
}


function  get_viewport() {
  var e = window, a = 'inner';
  if (!('innerWidth' in e)) {
    var t = document.documentElement
    e = t && t.clientWidth ? t : document.body 
    a = 'client';
  }
  return {width: e[a+'Width'], height: e[a+'Height']}
}


function  put_viewport_info() { 
  s = get_viewport();
  setcookie ('viewport_x', s.width, 0);
  setcookie ('viewport_y', s.height, 0);
}

                       
function  get_viewport_scroll() {
  var scrOfX = 0, scrOfY = 0;
  if (typeof(window.pageYOffset) == 'number') {
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
  else if (document.body && ( document.body.scrollLeft || document.body.scrollTop)) {
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } 
  else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return {x: scrOfX, y: scrOfY}
}


function  win_img_show (url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'toolbar=no, scrollbars=yes');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}


function  btn_style_out (e, uniqid, height)
{
  if (e.is_disabled)
    return;
  document.getElementById('btn_left_'+uniqid).style.backgroundPosition = '0 0';
  be = document.getElementById('btn_body_'+uniqid);
  be.style.backgroundPosition = '0 0';
  be.style.padding = '0 2 2 0';
  document.getElementById('btn_right_'+uniqid).style.backgroundPosition = '0 0';
}  


function  btn_style_over (e, uniqid, height)
{
  if (e.is_disabled)
    return;
  document.getElementById('btn_left_'+uniqid).style.backgroundPosition = '0 -' + height;
  document.getElementById('btn_body_'+uniqid).style.backgroundPosition = '0 -' + height;
  document.getElementById('btn_right_'+uniqid).style.backgroundPosition = '0 -' + height;
}  


function  btn_style_down (e, uniqid, height)
{
  if (e.is_disabled)
    return;
  document.getElementById('btn_left_'+uniqid).style.backgroundPosition = '0 -' + (height*2);
  be = document.getElementById('btn_body_'+uniqid);
  be.style.backgroundPosition = '0 -' + (height*2);
  be.style.padding = '0px';
  document.getElementById('btn_right_'+uniqid).style.backgroundPosition = '0 -' + (height*2);
}


function  autosubmit (action) {
  document.form_universal.action = action;
  document.form_universal.submit();
}


function  autosubmit_extra (action_extra) {
  autosubmit (document.form_universal.action + action_extra);
}


function  enter_pressed (e) 
{
  var  keycode;

  if (window.event) 
    keycode = window.event.keyCode;
  else if (e)
    keycode = e.which;
  else 
    return (false);
  if (keycode == 13) 
    return (true);
  return (false);
}


function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function
