/**
 * caw-media.de - Content Management System
 *
 * This is the T3LIB_DIV CSS DOCUMENT
 * of the caw-media.de - content managament system frontend
 *
 * @file    t3lib_div.js
 * @author  dpi one <www.dpi-one.de>
 */

var t3lib_div = {
	decimalSign: '.',
	parseDouble: function(value) {
		var theVal = '' + value;
		var dec = 0;
		if (!value) return 0;
		theVal = t3lib_div.noSeparator(theVal);
		for (var i = theVal.length; i > 0; i--) {
			if (theVal.substr(i - 1, 1) == '.' || theVal.substr(i - 1, 1) == ',') {
				dec = theVal.substr(i);
				theVal = theVal.substr(0, i - 1);
				break;
			}
		}
		dec = t3lib_div.getNumChars(dec) + '00';
		theVal = t3lib_div.getNumChars(theVal) + t3lib_div.decimalSign + dec.substr(0, 2);
		return theVal;
	},
	noSeparator: function(value) {
		var theVal = '' + value;
		var dec = 0;
		if (theVal.lastIndexOf('.') < theVal.lastIndexOf(',')) {
			dec = theVal.substring(theVal.lastIndexOf(',') + 1, theVal.length);
			theVal = theVal.substr(0, theVal.lastIndexOf(','));
		} else if (theVal.lastIndexOf('.') > theVal.lastIndexOf(',')) {
			dec = theVal.substring(theVal.lastIndexOf('.') + 1, theVal.length);
			theVal = theVal.substr(0, theVal.lastIndexOf('.'));
		}
		return theVal.replace(/,|\./g, '') + t3lib_div.decimalSign + dec;
	},
	parseInt: function(value) {
		var theVal = '' + value;
		if (!value)	return 0;
		for (var i = 0; i < theVal.length; i++) {
			if (theVal.substr(i, 1) != '0') {
				return parseInt(theVal.substr(i, theVal.length)) || 0;
			}
		}
		return 0;
	},
	noSpace: function(value) {
		var theVal = '' + value;
		var newString = "";
		for (var i = 0; i < theVal.length; i++) {
			var theChar = theVal.substr(i, 1);
			if (theChar != ' ') {
				newString += theChar;
			}
		}
		return newString;
	},
	getNumChars: function(value) {
		var theVal = '' + value;
		if (!value)	return 0;
		var outVal = "";
		for (var i = 0; i < theVal.length; i++) {
			if (theVal.substr(i, 1) == parseInt(theVal.substr(i, 1))) {
				outVal += theVal.substr(i, 1);
			}
		}
		return outVal;
	},
	inList: function(list, item) {
		list = ',' + list + ',';
		return (list.indexOf(',' + item + ',') != -1) ? true : false;
	},
	addToList: function(list, item) {
		if (typeof(list) == 'object' && typeof(list.value) != 'undefined') {
			list.value += list.value ? ',' + item : item;
		} else {
			list += list ? ',' + item : item;
			return list;
		}
	},
	removeFromList: function(list, item) {
		if (typeof(list) == 'object' && typeof(list.value) != 'undefined') {
			list.value = list.value.replace(item, '');
			list.value = list.value.replace(/^,/g, '');
			list.value = list.value.replace(/,$/g, '');
			list.value = list.value.replace(/,,+/g, ',');
		} else {
			list = list.replace(item, '');
			list = list.replace(/^,/g, '');
			list = list.replace(/,$/g, '');
			list = list.replace(/,,+/g, ',');
			return list;
		}
	},
	explode: function(delim, string, limit) {
		if (!limit) {
			return string.split(delim);
		} else {
			// support for limit argument
			var splitted = string.split(delim);
			var partA = splitted.splice(0, limit - 1);
			var partB = splitted.join(delim);
			partA.push(partB);
			return partA;
		}
	},
	trimExplode: function(delim, string, onlyNonEmptyValues) {
		var array = string.split(delim);
		var value, i;
			// for two perfomance reasons the loop is duplicated
			//  a) avoid check for onlyNonEmptyValues in foreach loop
			//  b) avoid unnecessary code when onlyNonEmptyValues is not set
		if (typeof(onlyNonEmptyValues) != 'undefined') {
			if (onlyNonEmptyValues) {
				var new_array = new Array();
				for(i = 0; i < array.length; i++) {
					value = t3lib_div.trim(array[i]);
					if (value != '') {
						new_array.push(value);
					}
				}
					// direct return for perfomance reasons
				return new_array;
			}
		}
		for(i = 0; i < array.length; i++) {
			array[i] = t3lib_div.trim(array[i]);
		}
		return array;
	},
	trim: function(value) {
		// Replace leading whitespaces
		value = value.replace(/^\s+/, '');
		// Replace ending whitespaces
		value = value.replace(/\s+$/, '');
		return value;
	}
};
