/**
 * Gets a parameter with the given name from the current url.
 */
function util_getURLParam(name) {
	var href=document.location.href;
	var pos=href.indexOf('?');
	if (pos==-1)
		return null;
	href=href.substring(pos+1);
	var params=href.split("&");
	for (var i=0;i<params.length;i++) {
		var curr=params[i];
		if (curr.indexOf(name+"=")==0) {
			return curr.substring((name+"=").length);
		}
	}
	return null;
}

function util_windowWidth() {
	if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") 
	  return window.innerWidth;
	 if (navigator.appName.indexOf("Microsoft")!=-1) 
	  return document.body.offsetWidth;
	}
	return 630;
}

function util_log(msg) {
	if (typeof console !='undefined' && typeof console.log == 'function') {
		console.log(msg);
	}
}

function util_windowHeight() {
	if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") 
	  return window.innerHeight;
	 if (navigator.appName.indexOf("Microsoft")!=-1) 
	  return document.body.offsetHeight;
	}
	return 460;
}

//from http://www.bradino.com/javascript/string-replace/
function util_replaceAll(instr,tofind,toreplace) {
	return instr.replace(new RegExp(tofind,'g'),toreplace);
}

/**
 * returns true if given array contains given value 
 */
function util_contains(ary,value) {
	for (var i=0;i<ary.length;i++) {
		if (ary[i]==value) 
			return true;
	}
	return false;
}

/**
 * Selects an option in a sel by the given value
 */
function util_selectByValue(sel,value) {
	for (var i=0;i<sel.options.length;i++) {
		if (sel.options[i].value==value) {
			sel.selectedIndex=i;
			sel.options[i].selected=true;
		}
	}
}

function util_getById(name) {
	var elem=document.getElementById(name);
	if (!elem)
		alert('no elem with id: '+name);
	return elem;
}

function util_absorbEvent(e) {
  // http://snipplr.com/view/2810/stop-event-propagation-stop-an-event-from-bubbling-up/
  var event = e || window.event;
  if (event.stopPropagation) {
  	event.stopPropagation();
  } else {
  	event.cancelBubble = true;
  } 
}

/**
* derived from
* http://www.webtoolkit.info/
*
**/
function util_trim(str) {
    return str.replace(new RegExp("^[\\s]+", "g"), "");
}

/**
 * Cross browser function to attach a named event 
 * to given elem which is handled by given func
 *  
 * @param obj elem to attach event to 
 * @param eventName name of event to capture
 * @param func function to call on event
 * @return
 */
function util_attachEvent(obj,eventName,func) {
	// http://www.quirksmode.org/js/events_advanced.html
	if (obj.addEventListener) {
		obj.addEventListener (eventName,func,false);
	} else if (obj.attachEvent) {
		obj.attachEvent('on'+eventName,func);
	} else {
		eval('obj.'+eventName)= func;
		eval('obj.'+eventName)= func;
	}
}

function util_coords(x,y) {
	this.x=x;
	this.y=y;
}

function util_getMouseCoords(event) {
	var tempX = 0;
	var tempY = 0;
	var IE=document.all?true:false;
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = event.pageX;
		tempY = event.pageY;
	}
	return new util_coords(tempX,tempY);
}

function util_findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return new util_coords(curleft,curtop);
}

function addEvent(src,eventName,func) {
  if (src.addEventListener) // w3c        
    src.addEventListener(eventName,func,false);
  else // ie
    src.attachEvent('on'+eventName,func);
}

function cancelBubble(event) {
  event=event||window.event;
  if (event.cancelBubble) // ie
    event.cancelBubble=true;
  else // ff
    event.stopPropagation();
  return false;
}

function addOnLoad(func) {
	addEvent(window,'load',func);
}

function copyToClipboard(txt) {
  window.clipboardData.setData('text',txt);
}

function addClass(elem,className) {
  if (elem.className.indexOf(className)==-1) {
    if (elem.className.length==0)
      elem.className=className;
    else
      elem.className+=' '+className;
  }
//  debug('now '+elem.id+'.className: "'+elem.className+'"');
}

function removeClass(elem,className) {
  if (elem.className==className) {
    elem.className='';
  } else {
    var pos=elem.className.indexOf(className);
    if (pos>-1) {
      if (pos==0) 
        elem.className=elem.className.substring(className.length+1);
      else
        elem.className=elem.className.substring(0,pos)+elem.className.substring(pos+className.length);
    }
  }
//  debug('now '+elem.id+'.className: "'+elem.className+'"');
}

/**
  Inserts the given text as the innerHTML of the element with the given id
  Included scripts are evaluated after the simple innerHTML assignment
*/
function insertHTML(id,text) {9
  var modCnt=document.getElementById(id);
  modCnt.innerHTML=text;
  // invoke scripts, which does not get done in innerHTML assignment.
  var scripts=modCnt.getElementsByTagName('script');
  for (var i=0;i<scripts.length;i++) {
    var script=scripts[i].innerHTML;
    if (script&&script!='')
      eval(script);
  }
}

/**
  Inserts text from request as innerHTML of elem with given id.  Performs
  exception assertion and script evaluation.
*/
function insertResponse(id,req) {
  assertException(req);
  insertHTML(id,req.responseText);
}