function addEvent(obj, evType, fn, useCapture){
  if (obj == null)
    return false;
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent('on' + evType, fn);
    return r;
  } else {
    alert('Handler could not be attached');
  }
}

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent(evType, fn);
    return r;
  } else {
    alert('Handler could not be removed');
  }
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function cancelEvent(e){
	if(!e) var e = window.event;
	try{
		e.returnValue = false;
		e.cancelBubble = true;
	}catch(err){}
	try{
		//e.cancelBubble is supported by IE - this will kill the bubbling process.
		if(e.returnValue){
			e.returnValue = false;
		}
		if(e.cancelBubble){
			e.cancelBubble = true;
		}
	}catch(err){}
	try{
		//e.stopPropagation works only in Firefox.
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}catch(err){}
}

function getEventTarget(e){
	var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return null;
    return target;
}

function openPopupWindow(url, features, replace, title) {
    if( features == null ) {
        features = "scrollbars=0";
    }
    if( title == null ) {
        title = "seek4_popup";
    }
	return window.open(url, "seek4_popup", features, replace);
}

function getSelectedValue( htmlSelect )
{
    return htmlSelect.options[htmlSelect.selectedIndex].value;
}

/* XML manipulation */
function parseToXML(text)
{
    var xmlDoc;
    if (window.ActiveXObject) // IE
    {
        var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(text);
    }
    else if (document.implementation && document.implementation.createDocument) // Mozilla, Firefox, Opera, etc.
    {
        var parser = new DOMParser();
        xmlDoc = parser.parseFromString(text,"text/xml");
    }
    
    return(xmlDoc);
}

function getCurrentForm(e){
	var obj=getEventTarget(e);
	while(obj.tagName.toLowerCase() != 'form'){
		if(obj.tagName.toLowerCase() == 'body'){
			return null;
		}
		obj = obj.parentNode;
	}
	if(obj.tagName.toLowerCase() != 'form'){
		obj = null;
	}
	return obj;
}
function submitCurrentForm(e){
	cancelEvent(e);
	var obj = getCurrentForm(e);
	obj.submit();
}

function redirect(url){
	window.location = url;
}

// URL encodes a string
function urlEncode( value ) {
	if( value != null && value.length > 0 ) {
		value = encodeURIComponent(value);
	}
	
	return value;
}

/* String functions */
function trim( str ) {
   return str.replace(/^\s*|\s*$/g,"");
}

// Original: Ryan Sokol.  Modified:  Ronnie T. Moore. Editor Web Site: The JavaScript Source 
// This script and many more are available free online at The JavaScript Source http://javascript.internet.com 
// Begin
function checkInt(str){
	if (!str) return 0;
	var ok = "";
	var i = 0
	var l = str.length;
	for (i; i < l; i++) {
		var ch = str.substring(i, i+1);
		if (ch < "0" || "9" < ch){ 
			alert("Only integer input is allowed!\n\n" 
				+ parseInt(ok) + " will be used because '" 
				+ str + "' is invalid.\nYou may correct "
				+ "this entry and try again.");
			return parseInt(ok);
		} else {ok += ch;}
	}
	return parseInt(str);
}

function checkDecimal(str) {
	if (!str) return 0;
	var ok = "";
	var i = 0
	var l = str.length;
	for (i; i < l; i++) {
		var ch = str.substring(i, i+1);
		if ((ch < "0" || "9" < ch) && ch != '.') {
			alert("Only numeric input is allowed!\n\n" 
				+ parseFloat(ok) + " will be used because '" 
				+ str + "' is invalid.\nYou may correct "
				+ "this entry and try again.");
			return parseFloat(ok);
		}else{ok += ch;}
	}
	return str;
}

function makeHours(hour, min, sec){
	return (hour + min/60 + sec/3600);
}

function makeTime(num){
	if(num){
		var hour = parseInt(num);
		num -= parseInt(num); 
		num *= 60;
		var min = parseInt(num);
		num -= parseInt(num); 
		num *= 60;
		var sec = parseInt(num);
		return {Hour:hour, Minute:min, Second:sec};
	}
}
//  End -->

// querystring
function Querystring(qs) {
	this.params = {};
	if (qs == null){
	    qs = location.search.substring(1, location.search.length);
	    this.url = location.pathname;
	}else{
	    var args = qs.split('?');
	    this.url = args[0];
	    qs = args[1] || "";
	}
	if (qs.length == 0) return;
	qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');
    for (var i = 0; i < args.length; i++){
	    var pair = args[i].split('=');
	    var name = decodeURI(pair[0]);
	    var value = (pair.length==2)
		    ? decodeURI(pair[1])
		    : name;
	    this.params[name] = value;
    }
}
Querystring.prototype.get = function(key, default_){
	var value = this.params[key];
	return (value != null) ? value : default_;
}
Querystring.prototype.set = function(key, value){
	this.params[key] = value;
}
Querystring.prototype.toString = function(flag){
    var res="";
    for (var i in this.params){res += i + "=" + this.params[i] + "&";}
	if (!flag) res=this.url+"?"+res;
	return res.substring(0, res.length - 1);
}