/* CORE *********************************************
 * 
 * Number function **********************************
 * <number>.bytes
 * <number>.kilobytes
 * <number>.megabytes
 * <number>.gigabytes
 * <number>.terabytes
 * <number>.petabytes
 * <number>.exabytes
 * <number>.roundDecimal    (decimal)
 * <number>.toPrecision
 * <number>.toPercent
 * <number>.toHumanSize
 * 
 * String functions *********************************
 * <string>.toDate
 * <string>.isEmail
 * <string>.ltrim
 * <string>.rtrim
 * <string>.trim
 * <string>.right           (nbr_char)
 * <string>.left            (nbr_char)
 * <string>.lpad            (character,nbr_char)
 * <string>.rpad            (character, nbr_char)
 * <string>.insert          (start, value)
 * <string>.get_filename_before_extension
 * <string>.get_filename
 * <string>.get_file_extension
 * 
 * Date functions ***********************************
 * <string>.get_month_s
 * <string>.get_month_name  (lang)
 * <string>.get_week_name   (lang) 
 * <string>.format_date     (lang)
 * <string>.number_days_month
 * <string>.isLeapYear
 * 
 * Prototype functions ******************************
 * <element>.crop_text          (width)
 * <element>.is_numeric
 * <element>.force_show
 * <form-element>.show_error    (text)
 * <form-element>.hide_error
 * <form-element>.update_select (value)
 *****************************************************/

Object.extend(Number.prototype, {
	bytes: function(){return this;},
	kilobytes: function() { return this *  1024; },
	megabytes: function() { return this * (1024).kilobytes(); },
	gigabytes: function() { return this * (1024).megabytes(); },
	terabytes: function() { return this * (1024).gigabytes(); },
	petabytes: function() { return this * (1024).terabytes(); },
	exabytes:  function() { return this * (1024).petabytes(); },
	
	roundDecimal: function(decimal){
		var final_number = 1;
		var i;

    decimal = parseInt(decimal);
	
    for (i=0;i<decimal;i++){
			final_number = final_number * 10;
		} 
		return Math.round(this * final_number) / final_number;
	},
	
	toPrecision: function(){
		var precision = arguments[0] || 2;
	  var s         = Math.round(this * Math.pow(10, precision)).toString();
	  var pos       = s.length - precision;
	  var last      = s.substr(pos, precision);
	  return s.substr(0, pos) + (last.match("^0{" + precision + "}$") ? '' : '.' + last);	
	},
	
	toPercent: function(){
		return (this * 100).toPrecision() + '%';
	},
	
	toHumanSize: function(){
		if(this < (1).kilobytes()) return this + " Bytes";
	  if(this < (1).megabytes()) return (this / (1).kilobytes()).toPrecision()  + ' KB';
	  if(this < (1).gigabytes()) return (this / (1).megabytes()).toPrecision()  + ' MB';
	  if(this < (1).terabytes()) return (this / (1).gigabytes()).toPrecision() + ' GB';
	  if(this < (1).petabytes()) return (this / (1).terabytes()).toPrecision() + ' TB';
	  if(this < (1).exabytes())  return (this / (1).petabytes()).toPrecision() + ' PB';
	                             return (this / (1).exabytes()).toPrecision()  + ' EB';
	}
	
});

Object.extend(String.prototype, {
	toDate: function(){
		var array_date;
		
		if(this.length != 10){
			return new Date();
		}else{
			array_date = this.split("-")
			return new Date(array_date[0], array_date[1]-1, array_date[2]);	
		}
	},
	
	isEmail: function(){
		var reg_exp = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
 	
		return reg_exp.test(email);
	},
	
	ltrim: function() {return(this.replace(/^[\s\n]*/,""));},
	
	rtrim: function() {return(this.replace(/[\s\n]*$/, ""));},

	trim: function() {return(this.rtrim().ltrim());},
	
	right: function(nbr_char){
		if(nbr_char < this.length){
			return this.substr(this.length - nbr_char, nbr_char);
		}else{
			return this;
		}
	},
	
	left: function(nbr_char){
		if(nbr_char < this.length){
			return this.substr(0, nbr_char);
		}else{
			return this;
		}
	},

	lpad: function(character,nbr_char){
		var result = this; 
		var i;
		
		for(i=this.length;i<nbr_char;i++) {
			result = String(character) + result;
		}
		
		return result;	
	},

	rpad: function(character, nbr_char){
		var result = this; 
		var i;
		
		for(i=this.length;i<nbr_char;i++) {
			result = result + String(character);
		}
		
		return result;	
	},
	
	insert: function(start, value){
		var result = this; 
		
		if(start <= result.length){
			result = result.substring(0, start) + value + result.substring(start, result.length);
		}
		
		return result;
	},
	
	get_filename_before_extension: function (){
		var result = this.lastIndexOf('\\', this.length - 1);
	
		result = this.substr(result + 1);
		result = result.substr(0, result.length - (this.get_file_extension().length+1)).toLowerCase();
	
		return result;
	},
	
	get_filename: function() {return this.substr(this.lastIndexOf('\\', this.length)+1).toLowerCase();},
	
	get_file_extension: function() {return this.substr(this.lastIndexOf('.', this.length)+1).toLowerCase();}
	
});

Object.extend(Date.prototype, {
	get_month_s: function() {return String(this.getMonth()+1).lpad("0", 2);},

	get_month_name: function(lang) {
		var months;
		
		switch(lang){
			case "fr" : 
				months = new Array("Janvier", "F�vrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "D�cembre");
				break;
			case "en" : 
				months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
				break;
		}
		
		return months[this.getMonth()];	
	},
	
	get_week_name: function(lang) {
		var days;
		
		switch(lang){
			case "fr" : 
				days = new Array("Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");
				break;
			case "en" : 
				days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");				
				break;
		}
		
		return days[this.getDay()];	
	},
	
	format_date: function(lang){
		switch(lang){
			case "en" :
				return this.get_month_s() + "-" + this.getDate() + "-" + this.getFullYear();
				break;
			case "fr" :
				return this.getDate() + "-" + this.get_month_s() + "-" + this.getFullYear();
				break;
		}
	},
	
	number_days_month: function(){
		var days;
		
		switch (this.getMonth()+1){
			case 1 :
			case 3 :
			case 5 :
			case 7 :
			case 8 :
			case 10 :
			case 12 : 
				days = 31
				break;
			case 2 :
				if (this.isLeapYear()){
					days = 29;
				}else{
					days = 28;
				}
				break;
			case 4 :
			case 6 :
			case 9 :
			case 11 :
				days = 30;
				break;
		}	

		return days;
	},
	
	isLeapYear: function(){
		if(this.getFullYear() % 4 == 0){
			if(this.getFullYear() % 100 == 0){
				if(this.getFullYear() % 400 == 0){
					return true;
				}
			}else{
				return true;
			}
		}
		
		return false;
	}

});

Element.Methods.crop_text = function(element, width){
	var container;
	var ratio = 0;
	var nbr_char = 0;
	var content = "";
	var changed = false;
	var finished = false;
	
	element = $(element);
	
	width -= 20;
	
	element.innerHTML = '<span id="SpanDim' + element.id + '">' + element.innerHTML + '</span>'	
	container = $("SpanDim" + element.id);
				
	if(container.getWidth() > width){
		ratio = width / container.getWidth();
		
		nbr_char = Math.round(container.innerHTML.length * ratio);
					
		content = container.innerHTML;
		
		do{
			container.innerHTML = content.left(nbr_char);
							
			if(container.getWidth() > width){
				nbr_char--;
				if(changed){
					container.innerHTML = content.left(nbr_char);
					finished = true;
				}
			}else if(container.getWidth() < width){
				nbr_char++;
				changed = true;
			}else{
				finished = true;
			}
		}while(!finished)
			
		container.innerHTML += "..."
	}
}

Element.Methods.is_numeric = function(element){
	var olRegExp = /^[0-9]+$/;
		
	element = $(element);
	return olRegExp.test(element.innerHTML);
};

Element.Methods.force_show = function(element){
  element.setStyle({display: 'block'});
}

Form.Element.Methods.show_error = function(element, text){
	var wrapper = new Element('div', { 'class': 'fieldWithErrors' });
	var error_text = new Element('div', {'class': 'formError'});
	
	error_text.innerHTML = text;
	$(element).parentNode.appendChild(error_text);
	$(element).wrap(wrapper);
}

Form.Element.Methods.hide_error = function(element){
	var parent;
	var error_element;
	
	if($(element).parentNode.className == 'fieldWithErrors'){
		parent = $(element).parentNode.parentNode
		$(parent).insert(element,'top');
		
		error_element = $(parent).select('.fieldWithErrors');
		error_element.each(function(item) {item.remove();});
		
		error_element = $(parent).select('.formError');
		error_element.each(function(item) {item.remove();});
	}
}

Form.Element.Methods.update_select = function(element, value) {
	var i;
	var obj_option;
	var found = false;
	
	element = $(element);
  
	for(i=0; i<element.options.length; i++){
		obj_option = element.options[i];
			
		if (obj_option.value == value) {
			element.options[i].selectedIndex = i;
			obj_option.selected = true;
			found = true;
		}else{
			obj_option.selected = false;
		}
	}
	
	return element;
};

Element.addMethods();
