/*
* X-Developed-By:	DarCas(.net) The Architect; www.DarCas.net - darcas@darcas.net
* X-Supported-By:	EstroWeb Srl; www.estroweb.it - estroweb@estroweb.it
*/
String.prototype.trim = function () { return this.replace(/\s+$|^\s+/g, ""); }
String.prototype.rtrim = function () { return this.replace(/\s+$/, ""); }
String.prototype.ltrim = function () { return this.replace(/^\s+/, ""); }
String.prototype.number_format = function ( int_decimals, string_dec_point, string_thousands_sep ) {

	float_number = this;
	string_dec_point = string_dec_point || '.';
	string_thousands_sep = string_thousands_sep || ',';
	float_number = Math.round(float_number * Math.pow(10, int_decimals)) / Math.pow(10, int_decimals);
	e = float_number + ''; f = e.split('.'); if (!f[0]) f[0] = '0'; if (!f[1]) f[1] = '';
	if (f[1].length < int_decimals) { g = f[1]; for (i=f[1].length + 1; i <= int_decimals; i++) g += '0'; f[1] = g; }
	if(string_thousands_sep != '' && f[0].length > 3) {
		h = f[0]; f[0] = '';
		for(j = 3; j < h.length; j+=3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = string_thousands_sep + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	string_dec_point = (int_decimals <= 0) ? '' : string_dec_point;
	return f[0] + string_dec_point + f[1];

}
String.prototype.datestamp_format = function ( format, intext  ) {

	try {

		this.replace(/\s+$|^\s+/g, '');

		year = this.substr(0, 4);
		month = this.substr(4, 2);
		day = this.substr(6, 2);

		if (intext) {

			switch (parseInt(month)) {

				case  1: month = 'Gennaio'; break;
				case  2: month = 'Febbraio'; break;
				case  3: month = 'Marzo'; break;
				case  4: month = 'Aprile'; break;
				case  5: month = 'Maggio'; break;
				case  6: month = 'Giugno'; break;
				case  7: month = 'Luglio'; break;
				case  8: month = 'Agosto'; break;
				case  9: month = 'Settembre'; break;
				case 10: month = 'Ottobre'; break;
				case 11: month = 'Novembre'; break;
				case 12: month = 'Dicembre'; break;

			}

		}

		result = format.replace('%Y', year);
		result = result.replace('%M', month);
		result = result.replace('%D', day);

		return result;

	} catch (e) { return false; }

};

(function () {
				var m = {
												'\b': '\\b',
												'\t': '\\t',
												'\n': '\\n',
												'\f': '\\f',
												'\r': '\\r',
												'"' : '\\"',
												'\\': '\\\\'
								},
								s = {
												array: function (x) {
																var a = ['['], b, f, i, l = x.length, v;
																for (i = 0; i < l; i += 1) {
																				v = x[i];
																				f = s[typeof v];
																				if (f) {
																								v = f(v);
																								if (typeof v == 'string') {
																												if (b) {
																																a[a.length] = ',';
																												}
																												a[a.length] = v;
																												b = true;
																								}
																				}
																}
																a[a.length] = ']';
																return a.join('');
												},
												'boolean': function (x) {
																return String(x);
												},
												'null': function (x) {
																return "null";
												},
												number: function (x) {
																return isFinite(x) ? String(x) : 'null';
												},
												object: function (x) {
																if (x) {
																				if (x instanceof Array) {
																								return s.array(x);
																				}
																				var a = ['{'], b, f, i, v;
																				for (i in x) {
																								v = x[i];
																								f = s[typeof v];
																								if (f) {
																												v = f(v);
																												if (typeof v == 'string') {
																																if (b) {
																																				a[a.length] = ',';
																																}
																																a.push(s.string(i), ':', v);
																																b = true;
																												}
																								}
																				}
																				a[a.length] = '}';
																				return a.join('');
																}
																return 'null';
												},
												string: function (x) {
																if (/["\\\x00-\x1f]/.test(x)) {
																				x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
																								var c = m[b];
																								if (c) {
																												return c;
																								}
																								c = b.charCodeAt();
																								return '\\u00' +
																												Math.floor(c / 16).toString(16) +
																												(c % 16).toString(16);
																				});
																}
																return '"' + x + '"';
												}
								};

				Object.prototype.json_encode = function () {
								return s.object(this);
				};

				Array.prototype.json_encode = function () {
								return s.array(this);
				};
})();

String.prototype.json_decode = function () {
				try {
								return (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)) &&
												eval('(' + this + ')');
				} catch (e) {
								return false;
				}
};
String.prototype.mylog = function () { if (window.console) window.console.log(this.replace(/\s+$|^\s+/g, "")); else alert(this.replace(/\s+$|^\s+/g, "")); }

Object.prototype.choose = function ( value ) {

	if (!value) return;
	for (var i = 0; i != this.getElementsByTagName('option').length; i++) if (this.getElementsByTagName('option').item(i).getAttribute('value') == value) { this.selectedIndex = i; return; }

}

function removeOptionFromSelect ( theSelect, startFrom, untilThe ) {

		var options = theSelect.getElementsByTagName('option');
		var startFrom = startFrom || 0;
		var untilThe = untilThe || options.length;
		var jobFinished = false;

		do {

				if (untilThe) {

						untilThe--;
						try { options.item((0 + startFrom)).parentNode.removeChild(options.item((0 + startFrom))); } catch (e) { /* -- */ }

				} else jobFinished = true;

		} while (!jobFinished);

}

function ucwords ( text ) { return text.toLowerCase().replace(/\w+/g, function (s) { return s.charAt(0).toUpperCase() + s.substr(1); }); }