String.prototype.trim = 	function(){
	return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = 	function(){
	return this.replace(/^\s+/,"");
};
String.prototype.rtrim = 	function(){
	return this.replace(/\s+$/,"");
};
String.prototype.isnumeric = function(){
	return (this - 0) == this && this.length > 0;
};
String.prototype.rmvwhitespace = function(){
	return this.trim().split(" ").join("");
};
String.prototype.ispostcode = function(){
	var regex = /^[1-9][0-9]{3}[A-Z]{2}$/; 
	return this.rmvwhitespace().toUpperCase().match(regex);
};
String.prototype.isemail = function(){
	var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return this.trim().match(regex);
};
String.prototype.isdouble = function(){
	 var pattern = /^\d+.?\d*$/;
     return this.trim().match(pattern);
}
String.prototype.replaceComma = function(){
	return this.toString().split(",").join(".");
}
String.prototype.toMoney = function(){
	var intVal = (Math.round(parseFloat(this) * 100) / 100).toString();
	if(intVal.indexOf(".") >= 0){
		var parts = intVal.split(".");
		if(parts[1].length == 1) parts[1] += "0";
		intVal = parts.join(",");
	}else{
		intVal += ",--";
	}
	return intVal;
}


String.prototype.isbsn = function(){
	var bsnlength 	= 9;
	var elfproef 	= 0;
	if(this.trim().length == bsnlength && this.isnumeric()){
		for(var i = 0; i < (bsnlength - 1); i++){
			elfproef += (this.charAt(i) * (bsnlength - i));
		}
		elfproef += (this.charAt(bsnlength-1) * -1);
	}
	if(elfproef != 0 && (elfproef % 11) == 0)
		return true;
	return false;
};
String.prototype.maxlength = function(maxlength){
	if(this.trim().length > maxlength)
		return false;
	return true;
};
String.prototype.minlength = function(minlength){
	if(this.trim().length < minlength)
		return false;
	return true;
};

var inArray = function(haystack, needle) {
	for (var i in haystack) { if (haystack[i] === needle) return true; }
	return false;
};

var checkGeboorteDatum = 	function(dag, maand, jaar)
{
	var dd = 	dag.trim();
	var mm = 	maand.trim();
	var jj = 	jaar.trim();
	var today = new Date();
	
	if(dd.length < 1 || dd.length > 2 || dd < 1 || dd > 31)
	{
		return false;
	}
	if(mm.length < 1 || mm.length > 2 || mm < 1 || mm > 12)
	{
		return false;
	}
	if(jj.length != 4 || jj < 1900 || jj > today.getFullYear())
	{
		return false;
	}
	
	return true;
};
var checkString = 			function(string){
	return true;
};
/**
 * This function returns the age as an integer
 * @param dag
 * @param maand
 * @param jaar
 * @return age
 */
var checkAge = 				function(dag, maand, jaar)
{
	var today = 	new Date();
	var birthdate = new Date(jaar, maand * 1 - 1, dag);
	var age = 		Math.floor((today.getTime() - birthdate.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
	return age;
};

var mousePos = function(evt){
	var e = evt || window.event;
	var posx;
	var poxy;
	if(e.pageX || e.pageY){
		posx = parseFloat(e.pageX);
		posy = parseFloat(e.pageY);
	} 
	else if(e.clientX || e.clientY){
		posx = parseFloat(e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);
		posy = parseFloat(e.clientY + document.body.scrollTop + document.documentElement.scrollTop);
	}
	
	return {x : posx, y : posy};
};

//Options are given in format 'key:val;key:val;', this must be converted to an array.
var keyvalStringToArray = function(_string)
{
	var result = [];
	
	_string = _string.trim();
	if(_string.charAt(_string.length -1) == ";")
		_string = _string.substr(0, _string.length -1);
	_string = _string.split(";");
	
	for(var i = 0; i < _string.length; i++)
	{
		if(_string[i] !== "")
		{
			var vars = _string[i].split(":");
			result.push({key:vars[0], val:vars[1]});
		}
	}
	return result;
};


function getRealXPos(elemento) {
	var x=0;
	while(elemento) {
		x += parseFloat(elemento.offsetLeft);
		elemento=elemento.offsetParent;
	}
	return x;
}

//Get Y position
function getRealYPos(elemento) {
	var y=0;
	while(elemento) {
		y += elemento.offsetTop;
		elemento=elemento.offsetParent;
	}
	return y;
}

function getRealMousePos(evt){
	var e = evt || window.event;
	
	if(e == undefined) return null;
	var posx;
	var poxy;
	if(e.pageX || e.pageY){
		posx = parseFloat(e.pageX);
		posy = parseFloat(e.pageY);
	} 
	else if(e.clientX || e.clientY)
	{
		posx = parseFloat(e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);
		posy = parseFloat(e.clientY + document.body.scrollTop + document.documentElement.scrollTop);
	}
	
	return {x : posx, y : posy};
}


function disableSelection(target)
{
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return false;};
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="none";
	else //All other route (ie: Opera)
		target.onmousedown=function(){return false;};
	target.style.cursor = "default";
}

function enableSelection(target)
{
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return true;};
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="all";
	else //All other route (ie: Opera)
		target.onmousedown=function(){return true;};
	target.style.cursor = "default";
}

function HexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function HexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function HexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}
function highlightNode(node, originalBackground){
	if(!originalBackground) originalBackground = "#FFFFFF";
	dojo.animateProperty({
			node: node,
			properties: {
				backgroundColor: {
					begin: originalBackground,
					end: "#BDE5F8"
				}
			},
			onEnd: function(){
				dojo.animateProperty({
				node: node,
				properties: {backgroundColor: originalBackground},
				duration:	500
				}).play();
			},
			duration:	500
		}).play();
}

function in_in_array (needle, index, haystack, _returnfirstrow) {
    if (_returnfirstrow === null)
        _returnfirstrow = false;

    for (var i in haystack) {
        var row = haystack[i];
        if (row[index] == needle) {
            if (_returnfirstrow) {
                return row;
            }
            else {
                return true;
            }
        }
    }
    return false;
}
var like = function (string, compare) {
	if (compare.length <= string.length) {
		for (var i = 0; i < compare.length; i++) {
			if (compare.charAt(i).toLowerCase() != string.charAt(i).toLowerCase()) {
				return false;
			}
		}
		return true;
	}
	return false;
};

function loopcoupletable (data, couple, index1, index2) {
	var array =[];
	for (var i in data) {
		var dataRow = data[i];
		for (var i2 in couple) {
			var coupleRow = couple[i2];
			if (dataRow[index1] == coupleRow[index2])
				array.push(dataRow);
		}
	}
	return array;
}

