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);
	return window.open(url, title, 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 -->
function loadIframe(id,url,useTimeout){
	if(useTimeout == true){
		setTimeout("loadIframe('"+id+"','"+url+"',false);",200);
	}else{
		try{
			$(id).src=url;
		}catch(e){}
	}
}
function loadContent(id,content,useTimeout){
	if(useTimeout == true){
		content = new String(content);
		content = content.replace(/'/g,"\\'");
		setTimeout("loadContent('"+id+"','"+content+"',false);",200);
	}else{
		try{
			$J('#' + id).append(content);
		}catch(e){}
	}
}

// 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);
}
function AjustHeight(jQueryObj1, jQueryObj2){
	if(jQueryObj1 != null && jQueryObj2 != null){
		if(jQueryObj1.height() < jQueryObj2.height()){
			jQueryObj1.height(jQueryObj2.height());
		} else {
			jQueryObj2.height(jQueryObj1.height());
		}
	}
}

function initExternalLinks() { //find all elements <a class="external"> for start PopWin()
	$J("a.external").bind("click", function (e){
		e.preventDefault();
		doTrack('/redirect/'+ $J(this).attr("href"));
		var newwin = window.open($J(this).attr("href"));
		return !newwin;
	});
}



/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        var lsRegExp = /\+/g;
        // Return the decoded string
        return String(string).replace(lsRegExp, " ");
        //return string;
    }
}

