/* -------------------------------------------------------------------- */
/* Cross borwser attach event method
/* -------------------------------------------------------------------- */

function attachEventListener(obj, evt, fnc, useCapture){
	if (!useCapture) useCapture = false;
	if (obj.addEventListener){
		obj.addEventListener(evt, fnc, useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt, fnc);
	else{
		myAttachEventListener(obj,evt,fnc);
		obj['on'+evt] = function(){ myFireEvent(obj,evt) };
	}
} 

function myAttachEventListener(obj, evt, fnc){
	if (!obj.myEvents) obj.myEvents = {};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}

function myFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

/* -------------------------------------------------------------------- */
/* Popup window
/* -------------------------------------------------------------------- */

var currentWindow

function popupWindow(win, winName, winWidth, winHeight, scrollbars){

	leftPos = 0; topPos = 0; // default variables for old browsers

	if (screen){ 
	
	    // checks specified parameters are not outside screen area
		if (winWidth>screen.width){
		   winWidth = screen.width;
		}
		
		if (winHeight>screen.height){
		   winHeight = screen.height - 55;
		}
		
		if (winWidth <= 0){
		   winWidth = screen.width;
		}
		
		if (winHeight <= 0){
		   winHeight = screen.height - 55;
		}
		
		// positions window by checking place parameter
		leftPos = screen.width/2 - winWidth/2;
		topPos = screen.height/2 - winHeight/2;
				
	}
	
	currentWindow = window.open(win,winName,'status=yes,toolbar=no,location=no,scrollbars='+ ((scrollbars)? 'yes' : 'no') +',resizable=yes,width='+winWidth+',height='+winHeight+',left='+leftPos+',top='+topPos+'');
	currentWindow.focus();
	
	return currentWindow;
}
    
/* -------------------------------------------------------------------- */
/* Querystring
/* -------------------------------------------------------------------- */
    
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

	// Turn <plus> back to <space>
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
	// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}