(function(){
	var util = window.util = {
		init: function() {
			this.asegurar();
			this.setup_valid_number();
			setTimeout("util.auto_hide_elements()", 4500);
		},
		setup_valid_number: function() {
			$('input.valid-number').bind('keypress', function(e) {
				return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) ? false : true;
			});
		},
		go_to: function(url) {
			window.location = url;
		},
		get_relpath: function() {
			return _relpath;
		},
		clear_number: function(num) {
			if (undefined == num) return;
			return parseFloat((num.toString().replace(/[^0-9\.\-]*/gi, '') || 0));
		},
		number_format: function(number, decimals, dec_point, thousands_sep) {
		    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
		    var d = dec_point == undefined ? "." : dec_point;
		    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
		    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
		    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
		},
		round: function(val, precision) {
		    return parseFloat(parseFloat(val).toFixed(precision));
		},
		round_and_format: function(number, precision) {
			var p = precision == undefined ? 2 : precision;
			return util.number_format(util.round(number, p));
		},
		currency_format: function(num) {
			num = util.clear_number(num);
			return '$' + util.round_and_format(num);
		},
		ucwords: function(str) {
		    return (str + '').replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
		},
		range: function(value, ranges) {
			for (var i in ranges) {
				var r = ranges[i];
				if (value >= r.min && (value <= r.max || r.max == -1)) return r;
			}
			return null;
		},
		asegurar: function() {
			$('input.warning').click(function() {
				var text_confirm = '¿Seguro?';
				if (this.value != text_confirm) {
					$(this).attr('rel', 'unlocked');
					this.value = text_confirm;
					return false;
				}
			});

			$('a.warning').click(function() {
				var text_confirm = '¿Seguro?';
				if (this.firstChild.nodeValue != text_confirm) {
					this.firstChild.nodeValue = text_confirm;
					return false;
				}
			});
		},
		auto_hide_elements: function() {
			$('div.auto_hide').hide();
		},
		is_in: function(haystack, needle) {
			for (key in haystack) if (needle == haystack[key]) return key;
		},
		get_option_selected: function(select) {
			var option = $(select).find('option:selected')[0];
			if (option) return {'value': option.value, 'text': option.text};
		},
		get_options_selected: function(select) {
			var options = [];
			$(select).find('option:selected').each(function(i, option) {
				options.push({'value': option.value, 'text': option.text});
			});
			return options;
		},
		select_options: function(select, values, unselect_all_before) {
			$(select).find('option').each(function(i, option) {
				if (unselect_all_before) option.selected = false;
				if (util.is_in(values, option.value)) option.selected = true;
			});
		},
		unselect_options: function(select, values) {
			$(select).find('option').each(function(i, option) {
				if (util.is_in(values, option.value)) option.selected = false;
			});
		},
		place_holder: function(element, text) {
			var e = $(element);

			e.focus(function() {
				if (e.attr('value') == text) {
					e.attr('value', '');
					e.removeClass('inactive_field');
				}
			});

			e.blur(function() {
				if (e.attr('value') == text || e.attr('value') == '') {
					e.attr('value', text);
					e.addClass('inactive_field');
				}
			});

			e.blur();
		},
		populate_select: function(select, items) {
			select = $(select);
			this.reset_select(select);
			$.each(items, function(i,item) {
				var option = document.createElement('option');
				option.setAttribute('value', item.value);
				if (item.text) option.appendChild(document.createTextNode(item.text));
				select.append(option);
			});
		},
		remove_all_childs: function(element) {
			while ($(element).firstChild) {
				$(element).removeChild($(element).firstChild);
			}
		},
		reset_select: function(select) {
			$(select).empty();
		},
		popup: function(url, options) {
			var pos_left = (screen.width - options.width) / 2;
			var pos_top = (screen.height - options.height) / 2;
			var win_properties = 'width='+options.width+',height='+options.height+',left='+pos_left+',top='+pos_top+'toolbar=0,location=0,directories=0,menubar=0,resizable=0,scrollbars=auto';
			Win = window.open(url, (options.name || ''), win_properties);
			Win.window.focus();
		}
	};

	$(function(){
		util.init();
	});
})();
