String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g, '');
};

function formatiereURI(str){
	return encodeURIComponent(str);
}

function formatiereInput(nachricht){
	return brEntfernen(decodeURIComponent(nachricht));
}

function formatiereDiv(nachricht){
	return brHinzu(decodeURIComponent(nachricht));
}

function generatePictureRandomExtension(){
	return '?' + parseInt((Math.random()*20000), 10);
}

function formatiereBildLink(bild){
	bild = bild.split('?');
	return bild[0];
}

/**
 * 	Diese Funktion wandelt alle Bildpfade um und setzt die ROOTDIR richtig
 * 
 * @param html		Der HTML-Inhalt
 * @return HTML		Der neue HTML code
 */
function makeWysiwygPictures(html){
	html = html.replace(/src=["|'](\.\.\/)*/, 'src="');
	var pattern = /src=["|']/g;
	html = html.replace(pattern, 'src="' + ROOTDIR);
	
	return html;
}

function formateEuro(euro){
	euro = euro.toString().replace(/\./g, ',');
	if(euro.match(/^\d+,\d$/g) != null)			euro += '0';		//Falls hinten eine 0 fehlt
	else if(euro.match(/^\d+,$/g) != null)		euro += '00';
	else if(euro.match(/^\d+$/g) != null)		euro += ',00';
	
	if(euro.match(/^\d+,\d\d\d+$/g) != null){
		//Es sind noch zu viele Zahlen am Ende
		euro = euro.substring(0, 5);
	}
	
	return euro;
}

function formateDouble(value){
	if(value && (value >= 0 || value <= 0 || value != '')){
		value = value.toString().replace(/\./g, ',');
	}
	
	return value;
}

function checkDouble(value){
	if(value){
		value = value.toString();
		if(value.match(/^\d+[,|\.]*\d*$/) != null)		return true;
	}
	
	return false;
}

function checkMail(mail){
	//Regulärer Ausdruck welcher folgendes E-Mail-Format überprüft:
	//Am Anfang können beliebig viele Zeichen stehen gefolgt von einem @.
	//Nach dem @ muss ein Zeichen welches von a-z pder A-Z geht.
	//Die folgenden Zeichen können von a-z A-Z 0-9 und dem - sein.
	//Dann muss ein . folgen welcher wieder erst ein Zeichen zwischen a-z und A-Z haben muss
	//gefolgt von Zeichen die a-z A-Z 0-9 und dem - sein können.
	//Bsp.: a@b.c, 23@b3kD.dsd ...
	reg = /^.+@[a-zA-Z][a-zA-Z0-9-]*\.[a-zA-Z][a-zA-Z0-9\.-]*$/;
	exp = reg.exec(mail);
	
	return exp != null ? true : false;
}

function formatiereDate(dateType, date){
	var newDate = dateType;
	if(!date || !date.getDate){
		if(typeof(date) == 'string'){
			date = mysqlTimeStampToDate(date);
		}
		else		return false;	
	}
	
	if(date == null)		return '';
	
	newDate = newDate.replace(/d/g, ((date.getDate() <= 9 ? '0' : '') + date.getDate()));
	newDate = newDate.replace(/m/g, (((date.getMonth()+1) <= 9 ? '0' : '') +(date.getMonth()+1)));
	newDate = newDate.replace(/Y/g, date.getFullYear());
	newDate = newDate.replace(/H/g, ((date.getHours() <= 9 ? '0' : '') + date.getHours()));
	newDate = newDate.replace(/i/g, ((date.getMinutes() <= 9 ? '0' : '') + date.getMinutes()));
	newDate = newDate.replace(/s/g, ((date.getSeconds() <= 9 ? '0' : '') + date.getSeconds()));
	
	return newDate;
}

function mysqlTimeStampToDate(timestamp) {
	if(typeof(timestamp) != 'string')	return timestamp;
	if(timestamp.trim() == '')			return null;
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    
    //Der Erste Wert muss über 0 sein, damit ein regulärer Wert heraus kommt
    if(parts[0] > 0){
    	return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
    }
    
    return null;
}

function clone(obj){
	if(obj == null || typeof(obj) != 'object')
        return obj;
		
    var temp = new obj.constructor(); // changed (twice)

    for(var key in obj)
        temp[key] = clone(obj[key]);

    return temp;
}

function getGETParamenter(){
	var gets = document.location.search.substr(1,document.location.search.length).split('&');
	var getAll = {};

	if(gets.length > 0){
		var i;
		for(i in gets){
			var temp = gets[i].split('=');
			
			if(temp.length > 0){
				getAll[temp[0]] = temp[1];
			}
		}
	}
	
	return getAll;
}


/**
 * Entfernt alle HTML-BRs
 * @param str
 * @return
 */
function brEntfernen(str){
	return str.replace(/<br>/g, '\n');
}

function brHinzu(str){
	return str.replace(/\n/g, '<br>');
}

function addslashes (str) {
    // Escapes single quote, double quotes and backslash characters in a string with backslashes  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/addslashes    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // +   improved by: Nate
    // +   improved by: Onno Marsman    // +   input by: Denny Wardhana
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Oskar Larsson Högfeldt (http://oskar-lh.name/)
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: 'kevin\'s birthday'    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
	return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}
