
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

String.prototype.isEmail = function(){
	var value = this.trim();
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
	return !r1.test(value) && r2.test(value);	
}

String.prototype.toCurrency = function(){
	var value = this.trim().replace(/[a-zA-Z]/g,'');
	value = value.replace(/,/g,'.');
	value = value.trim();
	return value.length ? Number(value).toFixed(2) : '';
}

String.prototype.toInteger = function(){
	var value = this.trim().replace(/[a-zA-Z]/g,'');
	value = value.replace(/,/g,'.');
	value = value.trim();	
	return value.length ? parseInt(value) : '';
}


String.prototype.toUnsignedInteger = function(){
	var value = this.trim().replace(/[a-zA-Z-]/g,'');
	value = value.replace(/,/g,'.');
	value = value.trim();	
	return value.length ? parseInt(value) : '';
}

String.prototype.toTime = function(){
	var value = this.trim().replace(/[\.,]/g,':');
	if(value && value.length){
		value = value.split(':');
		if(value[0] && value[0].substr(0,1) == "0") value[0] = value[0].substr(1,1);
		if(value[1] && value[1].substr(0,1) == "0") value[1] = value[1].substr(1,1);
		var H = (value[0]) ? Math.min(23,parseInt(value[0].replace(/[^0-9]/g,''))) : 0;
		var i = (value[1]) ? Math.min(59,parseInt(value[1].replace(/[^0-9]/g,''))) : 0;
		
		if(i<10) i = '0' + i;				
		return H + ':' + i;
	}
}

String.prototype.toDuration = function(){
	var value = this.trim().replace(/[\.,]/g,':');
	if(value && value.length){
		value = value.split(':');
		if(value[0] && value[0].substr(0,1) == "0") value[0] = value[0].substr(1,1);
		if(value[1] && value[1].substr(0,1) == "0") value[1] = value[1].substr(1,1);
		
		var H = value[0] ? parseInt(value[0].replace(/[^0-9]/g,'')) : 0;
		var i = value[1] ? Math.min(59,parseInt(value[1].replace(/[^0-9]/g,''))) : 0;
		
		if(i<10) i = '0' + i;				
		return H + ':' + i;
	}
}

String.prototype.toInitials = function(){
	var value = this.trim()
	if(value.length){
		var value = value.toUpperCase();
		value = value.trim().replace(/ /g,'.').replace(/[^a-zA-Z\.]/g,'').replace(/\.\./g,'.');
		if(value.indexOf('.') == -1){
			value = value.split('');
		}
		else{
			value = value.split('.');
			for(var i=0;i<value.length;i++){
				if(value[i].length > 1) value[i] = value[i].substr(0,1) + value[i].substr(1).toLowerCase();
			}
		}
		value = (value.join('.') + '.').replace('..','.');
	}
	return value;
}

String.prototype.toName = function(){
	var value = this.trim()
	if(value.length){
		if(value.substr(0,2) == "ij"){
			value = value.substr(0,2).toUpperCase() + value.substr(2);
		}
		else{
			value = value.substr(0,1).toUpperCase() + value.substr(1);
		}
	}
	return value;
}

String.prototype.toURL = function(){
	var value = this.trim().replace('http://','').trim();
	return value.length ? 'http://' + input.value : '';
}


