function $(o) {
    return document.getElementById(o)
}

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return results[1];
}

function trim(str) {
    var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    for (var i = 0; i < str.length; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    for (i = str.length - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function rand() {
  return (Math.round(Math.random())-0.5);
}

function check_key(e, keycode) {
  if (e && e.which) {
    e = e
    characterCode = e.which
  } else {
	e = event
	characterCode = e.keyCode
  }
  return characterCode == keycode
}

function obj_ajax() {
   var xmlhttp = false;
   try {
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
       try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (E) {
           xmlhttp = false;
       }
   }
   if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
       xmlhttp = new XMLHttpRequest();
   return xmlhttp;
}

function fetch_contents(url, callback) {
    var requestObj = obj_ajax();
    if (callback) {
        requestObj.open("GET", url, true);
        requestObj.onreadystatechange = function() { if (requestObj.readyState==4) callback(requestObj.responseText); }
    } else
        requestObj.open("GET", url, false);
    requestObj.send(null);
    return callback ? true : requestObj.responseText;
}

function toggle(obj, property) {
	switch (property) {
		case 'display' : obj.style.display = obj.style.display == 'block' || obj.style.display == '' ? 'none' : 'block';
		case 'visibility' : obj.style.display = obj.style.display == 'visible' || obj.style.display == '' ? 'hidden' : 'visible';
	}
}

function only_number(e) {
	var code = (e.keyCode ? e.keyCode : e.which);
	var functional = false;
	// allow keypad numbers, 0 to 9
	if((code >= 48 && code <= 57) || (code >= 96 && code <= 105)) functional = true;
	// check Backspace, Tab, Enter, and left/right arrows
	if (code ==  8) functional = true;
	if (code ==  9) functional = true;
	if (code == 13) functional = true;
	if (code == 37) functional = true;
	if (code == 39) functional = true;
	return functional;
}

function obj_pos(obj) {
    // Find the destination's position
    var destx = obj.offsetLeft; 
    var desty = obj.offsetTop;
    var thisNode = obj;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
    return [destx, desty];
}

function window_size() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
function mouse_position(e) {
  var x, y;
  if (IE) { // grab the x-y pos.s if browser is IE
    x = event.clientX + document.body.scrollLeft;
    y = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    x = e.pageX;
    y = e.pageY;
  }  
  if (x < 0) x = 0;
  if (y < 0) y = 0;  
  return [x, y];
}

function get_elements_by_class(name) {
  var all = document.all ? document.all :
  document.getElementsByTagName('*');
  var elements = new Array();
  for (var e = 0; e < all.length; e++)
    if (all[e].className == name)
      elements[elements.length] = all[e];
  return elements;
}
