/*********************************************************************
	STUDIO PALLIETER 2007
	Cross browser library
	Author: Bart Tegenbosch
 ********************************************************************/

function $(id, tagName){
	return document.getElementById(id);
}
function $T(tag){
	return document.getElementsByTagName(tag);
}

function flashInstalled(){
	if(navigator.plugins && navigator.plugins.length){
		if(navigator.plugins["Shockwave Flash"]){
			return true;
		}
	}else{
		for(var i = 2; i < 10; i++){
			try{
				var x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
				if(x){
					return true;
				}
			}catch(e){}
		}
	}
	return false;
}
/*********************************************************************
	Object prototypes
 *********************************************************************/
/*
	object Object.merge()
		
		This method doesn't merge all properties of both parties. It
		merges only the existing values in Object(this) with the values in
		object.
		
	object: the object to merge with
*/
Object.prototype.merge = function(object){
	var obj = new Object();
	for(property in this){
		obj[property] = object[property]?object[property]:this[property];
	}
	return obj;
}
 /*********************************************************************
	Array prototypes
 *********************************************************************/
 Array.prototype.contains = function(obj){
	for(var i = 0; i < this.length; i++){
		var e = this[i];
		if(e == obj && typeof(e) == typeof(obj)){
			return true;
		}
	}
	return false;
 }
 /*********************************************************************
	String prototypes
 *********************************************************************/
String.prototype.contains = function(str, ci /*case-insensitive?*/){
	var results = ci?this.match(str, 'i'):this.match(str);
	if(results){
		if(results.length > 0){
			return true;
		}
	}
	return false;
}
String.prototype.startsWith = function(str){
	var portion = this.substring(0, str.length);
	return (portion == str);
}
String.prototype.endsWith = function(str){
	var portion = this.substring(this.length-str.length);
	return (portion == str);
}
String.prototype.ucfirst = function(){
	return ""+(this.substring(0,1).toUpperCase() + this.substring(1));
}
/*********************************************************************
	Event object
 *********************************************************************/
var Event = {
	/*
		void Event.attach()
		
		object: object to attach the event to
		eventName: name of the event (no 'on' prefix)
		handler: function wich handles the event
	*/
    attach: function(object, eventName, handler){
        if(object.attachEvent){
            object.attachEvent("on"+eventName, handler);
        }else if(object.addEventListener){
            object.addEventListener(eventName, handler, false);
        }
    }
};
/*********************************************************************
	Ajax object
 ********************************************************************/
var Ajax = {
	/*
		object Ajax.options()
		
		o: options object to compare with
	*/
	options: {
		encoding:	'UTF-8',
		parameters: '',
		onSuccess:	function(){},
		onFailure:	function(){}
	},
	/*
		object Ajax.create()
	*/
	create: function(){
		if(window.XMLHttpRequest){
			return new XMLHttpRequest();
		}else{
		
			try {
				return new ActiveXObject("MSXML2.XMLHTTP");
			}catch(e){
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					return false;
				}
			}
		}
		return false;
	},
	
	/*
		void Ajax.request()
		
		url: url to script,
		options: object wich contains options for the request
	*/
	request: function(url, options){
		var request = Ajax.create();
		
		options = this.options.merge(options);
		
		request.open('post', url, true);
		request.send(options.parameters);
		request.onreadystatechange = function(){
			if(request.readyState == 4){
				if(request.status == 200){/*success*/
					var handler = options.onSuccess;
					if(!(typeof(handler) == 'function')) return;
					var js   = request.getResponseHeader('text/json');
					var json = js?eval('('+ js +')'):js;
					handler(request, json);
				}else{
					var handler = options.onFailure;
					if(!(typeof(handler) == 'function')) return;
					handler(request);
				}
			}
		};
	}
};
/*********************************************************************
	Element object
 *********************************************************************/
 var Element = {
	/*
		object Element.create()
		
		tag: tag of the element
		attributes: object wich describes the elements attributes with their values.
					extra attribute id 'body' wich describes the innerHTML
	*/
	create: function(tag, attributes){
		var el = document.createElement(tag);
		if(!attributes) return el;
		for(property in attributes){
			if(property == 'body' && el.innerHTML != null){
				el.innerHTML = attributes['body'];
			}
			try{
				el[property] = attributes[property];
			}catch(e){}
		}
		return el;
	},
	/*
		void Element.remove()
		
		element: element to remove
	*/
	remove: function(element){
		if(element.parentNode){
			element.parentNode.removeChild(element);
		}
	},
	
	position: function(element){
		var curleft = curtop = 0;
		if (element.offsetParent) {
			do{
				curleft += element.offsetLeft;
				curtop += element.offsetTop;
			} while (element = element.offsetParent);
		}
		return {x:curleft, y:curtop};
	}
 };
 
 var Mouse = {
	getElement: function(e){
		if (!e) var e = window.event;
		if(e.srcElement){
			return e.srcElement;
		}else if(e.target){
			return e.target;
		}
		return undefined;
	},
	
	getPosition: function(e) {
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		return {x:posx, y:posy};
	}
 }
 
 var Form = {
	Input: {},
	Textarea: {},
	Checkbox: {
		checkAll: function(fieldname){
			for(var i = 0; i < fieldname.length; i++){
				fieldname[i].checked = true;
			}
		},
		uncheckAll: function(fieldname){
			for(var i = 0; i < fieldname.length; i++){
				fieldname[i].checked = false;
			}
		},
		checkInverse: function(fieldname){
			for(var i = 0; i < fieldname.length; i++){
				fieldname[i].checked = !fieldname[i].checked;
			}
		}
	}
 };
 
 var cWindow = {
	handel: null,

	open: function(url, name, width, height, resizable){
		var attrib = "toolbar=no, location=no, scrollbars=yes, menubar=no";
	
		if(resizable){
			attrib += ", resizable=yes";
		}
	
		var left = (screen.width / 2)-(width / 2);
		var top  = (screen.height / 2)-(height / 2);
		
		attrib += ", left="+left+", top="+top+", width="+width+", height="+height;
		
		if(this.handle){
			this.close;
		}
		
		this.handle = window.open(url, "", attrib);
	},
	close: function(){
		try{
			this.handle.close();
			this.handle = null;
		}catch(e){
			/*TODO*/
		}
	}
};
