//- Version 2009-07-14

//- Array
Array.prototype.remove =function (from, to) {
	var rest =this.slice ((to || from) + 1 || this.length) ;
	this.length =from < 0 ? this.length + from : from ;
	return (this.push.apply (this, rest)) ;
}

Array.prototype.chunk =function (chunkSize) {
	var base =[] ;
	for ( var i =0 ; i < this.length ; i +=chunkSize )
		base.push (this.slice (i, i + chunkSize)) ;
	return base ;
}

//- Number
var LocaleNumberCyr ={
	decimal: '.',
	group: ',',
	
} ;
Number.prototype.format =function (options) {
	// Thanks dojo and YUI for some inspiration
	var value =this ;
	if ( !options ) options ={} ;
	var getOption =function (key) {
		if ( options [key] != null ) return options [key] ;
		return LocaleNumberCyr [key] ;
	} ;

	var negative = value < 0,
		decimal = getOption('decimal'),
		precision = getOption('precision'),
		group = getOption('group'),
		decimals = getOption('decimals');

	if (negative){
		var negativeLocale = LocaleNumberCyr ['negative'] || {};
		if (negativeLocale.prefix == null && negativeLocale.suffix == null) negativeLocale.prefix = '-';
		Object.each(negativeLocale, function(value, key){
			options[key] = (key == 'prefix' || key == 'suffix') ? (getOption(key) + value) : value;
		});

		value = -value;
	}

	var prefix = getOption('prefix'),
		suffix = getOption('suffix');

	if (decimals > 0 && decimals <= 20) value = value.toFixed(decimals);
	if (precision >= 1 && precision <= 21) value = value.toPrecision(precision);

	value += '';

	if (getOption('scientific') === false && value.indexOf('e') > -1){
		var match = value.split('e'),
			index,
			zeros = +match[1];
		value = match[0].replace('.', '');

		if (zeros < 0){
			zeros = -zeros - 1;
			index = match[0].indexOf('.');
			if (index > -1) zeros -= index - 1;
			while (zeros--) value = '0' + value;
			value = '0.' + value;
		} else {
			index = match[0].lastIndexOf('.');
			if (index > -1) zeros -= match[0].length - index - 1;
			while (zeros--) value += '0';
		}
	}

	if (decimal != '.') value = value.replace('.', decimal);

	if (group){
		index = value.lastIndexOf(decimal);
		index = (index > -1) ? index : value.length;
		var newOutput = value.substring(index),
			i = index;

		while (i--){
			if ((index - i - 1) % 3 == 0 && i != (index - 1)) newOutput = group + newOutput;
			newOutput = value.charAt(i) + newOutput;
		}

		value = newOutput;
	}

	if (prefix) value = prefix + value;
	if (suffix) value += suffix;

	return value;
}

Number.prototype.formatCurrency =function () {
	var locale ={ prefix: '$ ' } ;
	if ( locale.scientific == null ) locale.scientific =false ;
	if ( locale.decimals == null ) locale.decimals =2 ;
	return this.format (locale) ;
}

Number.prototype.formatPercentage =function () {
	var locale ={} ;
	if ( locale.suffix == null ) locale.suffix ='%' ;
	if ( locale.decimals == null ) locale.decimals =2 ;
	return this.format (locale) ;
}

//- String
String.prototype.urldecode =function () {
	return (unescape (String (this).replace (/\+/g, " "))) ; 
}

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.addslashes =function () {
	var st =this.replace (/\'/g, '\\\'') ;
	st =st.replace (/\"/g, '\\"') ;
	st =st.replace (/\\/g, '\\\\') ;
	st =st.replace (/\0/g, '\\0') ;
	return (st) ;
}

String.prototype.stripslashes =function () {
	var st =this.replace (/\\'/g, '\'') ;
	st =st.replace (/\\"/g, '"') ;
	st =st.replace (/\\\\/g, '\\') ;
	st =st.replace (/\\0/g, '\0') ;
	return (st) ;
}

String.prototype.isNumeric =function () {
	return (this.match(/^(-?\d*\.?\d*)$/) != null) ;		
}

String.prototype.isValidEmail =function () {
	var emailPattern =/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z]{2,4}$/ ; 
	return (emailPattern.test (this)) ; 
}

function sprintf () { 
	if ( !arguments || arguments.length < 1 || !RegExp )
		return ; 
	var str =arguments [0] ; 
	var re =/([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/ ; 
	var a =b =[], numSubstitutions =0, numMatches =0 ; 
	while ( a =re.exec (str) ) { 
		var leftpart =a [1], pPad =a [2], pJustify =a [3], pMinLength =a [4] ; 
		var pPrecision =a [5], pType =a [6], rightPart =a [7] ;
		numMatches++ ; 
		if ( pType == '%' ) { 
			subst ='%' ; 
		} else { 
			numSubstitutions++ ; 
			if ( numSubstitutions >= arguments.length ) { 
				alert (  'Error! Not enough function arguments ('
					   + (arguments.length - 1) + ', excluding the string)\n'
					   + 'for the number of substitution parameters in string ('
					   + numSubstitutions + ' so far).'
				) ; 
			} 
			var param =arguments [numSubstitutions] ; 
			var pad ='' ; 
			if ( pPad && pPad.substr (0, 1) == "'" ) { 
				pad =leftpart.substr (1, 1) ; 
			} else if ( pPad ) { 
				pad =pPad ; 
			} 
			var justifyRight =true ; 
			if ( pJustify && pJustify === "-" )
				justifyRight =false ; 
			var minLength =-1 ; 
			if ( pMinLength )
				minLength =parseInt (pMinLength) ; 
			var precision =-1 ; 
			if ( pPrecision && pType == 'f' ) { 
				precision =parseInt (pPrecision.substring (1)) ; 
			} 
			var subst =param ; 
			switch ( pType ) { 
				case 'b': subst =parseInt (param).toString (2) ; break ; 
				case 'c': subst =String.fromCharCode (parseInt (param)) ; break ; 
				case 'd': subst =parseInt (param) ? parseInt (param) : 0 ; break ; 
				case 'u': subst =Math.abs (param) ; break; 
				case 'f': subst =(precision > -1) ?
							Math.round (parseFloat (param) * Math.pow (10, precision)) / Math.pow (10, precision)
						  : parseFloat (param) ;
					break ; 
				case 'o': subst =parseInt (param).toString (8) ; break ; 
				case 's': subst =param ; break ;  
				case 'x': subst =('' + parseInt (param).toString (16)).toLowerCase () ; break ; 
				case 'X': subst =('' + parseInt (param).toString (16)).toUpperCase () ; break ; 
			} 
			var padLeft =minLength - subst.toString ().length ; 
			if ( padLeft > 0 ) { 
				var arrTmp =new Array (padLeft + 1) ; 
				var padding =arrTmp.join (pad ? pad : " ") ; 
			} else { 
				var padding ="" ;
			}
		} 
		str =leftpart + padding + subst + rightPart ; 
	} 
	return (str) ;
}

