/*********************************************************************
	STUDIO PALLIETER 2007
	Cross browser library
	Author: Bart Tegenbosch
 ********************************************************************/
function $(id, tagName){
	return document.getElementById(id);
}
function $T(tag){
	var elements = document.getElementsByTagName(tag);
	return (elements.length > 1)?elements:elements[0];
}

function flashInstalled(){
	if(navigator.plugins && navigator.plugins.length){
		if(navigator.plugins["Shockwave Flash"]){
			return true;
		}
	}else{
		//IE
		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(){
		return window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("MSXML2.XMLHTTP");
	},
	
	/*
		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.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + options.encoding);
		request.setRequestHeader('Accept', 'text/javascript, text/json, text/html, application/xml, text/xml, */*');
		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{ //Failure!
					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);
		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);
		}
	}
 };
 
 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;
			}
		}
	}
 };
