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 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 txt_counter(field, cnt_field, max_limit) {
	if (field.value.length > max_limit)
		field.value = field.value.substring(0, max_limit);
	else
		cnt_field.value = max_limit - field.value.length;
}

