/*
         ___                         _____       __
        /  /\        ________       /    /\     / /\
(-----//  /  \//----/_____/__\\--) /____/  \___/_/ //--------------------------)
      /  /   /      \     | //     \    \  /    /\/    this code was created by
     /__/   /        \    |///-----\\____\/____/  \                       sigea
(---\\  \  /   (-----\\___|/        /    /\    \  /       
      \__\/                        /____/ /\____\/            __       sigea.ch
                                   \____\/  \  /             /\_\--------------)
                                         \___\//-------------\/_/  

*/

function addLoadEvent(func){
	if(window.addEventListener){
		window.addEventListener('load',func,false);
	}else if(window.attachEvent){
		window.attachEvent('onload',func);
	}else{
		var oldonload = window.onload;
		if (typeof window.onload != 'function'){
			window.onload = func;
		}else{
			window.onload = function(){
				oldonload();
				func();
			}
		}
	}
}

function insertAfter(newElement,targetElement){
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement){
		parent.appendChild(newElement);
	}else{
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

function addClass(element,value){
	if(!element.className){
		element.className = value;
	}else{
		if(element.className.indexOf(value) == -1){
			var newClassName = element.className;
			newClassName+= " ";
			newClassName+= value;
			element.className = newClassName;
		}
	}
}
function removeClass(element,value){
	if(element.className){
		var pattern= new RegExp(value);
		element.className=element.className.replace(pattern,'');
	}
}

function checkDate(date){
	var pattern= new RegExp("^[0-9]{1,2}\\D[0-9]{1,2}\\D([0-9]{2}){1,2}$");
	if(date.search(pattern)==-1){
		return false;
	}else{
		date = date.replace(/\D/g,'.');
		var tempDate=date.split('.');
		if(parseInt(tempDate[0],10)<1 || parseInt(tempDate[0],10)>31){
			return false;
		}else if(parseInt(tempDate[1],10)<1 || parseInt(tempDate[1],10)>12){
			return false;
		}else if((parseInt(tempDate[2],10)>99 && parseInt(tempDate[2],10)<1900) || parseInt(tempDate[2],10)>2100){
			return false;
		}else{
			return true;
		}
	}
}
function checkTime(time){
	var pattern= new RegExp("^[0-9]{1,2}\\D[0-9]{1,2}$");
	if(time.search(pattern)==-1){
		return false;
	}else{
		time = time.replace(/\D/g,':');
		var tmpTime=time.split(':');
		if(parseInt(tmpTime[0],10)<0 || parseInt(tmpTime[0],10)>24){
			return false;
		}else if(parseInt(tmpTime[1],10)<0 || parseInt(tmpTime[1],10)>59){
			return false;
		}else{
			return true;
		}
	}
}

function trimString(thisString){
	if(thisString!=''){
		while(thisString.substring(0,1)==' '){
			thisString = thisString.substring(1, thisString.length);
		}
		while(thisString.substring(thisString.length-1, thisString.length)==' '){
			thisString = thisString.substring(0,thisString.length-1);
		}
	}
	return thisString;
}
// http://javascript.crockford.com/memory/leak.html
function purge(d){
	var a = d.attributes, i, l, n;
	if(a){
		l = a.length;
		for (i = 0; i < l; i += 1){
			n = a[i].name;
			if (typeof d[n] === 'function'){
				d[n] = null;
			}
		}
	}
	a = d.childNodes;
	if(a){
		l=a.length;
		for (i = 0; i < l; i += 1){
			purge(d.childNodes[i]);
		}
	}
}