function $count(obj){
	var count = 0;
	for(var i in obj){
		count++;
	}
	return count;
}

function redirect(url){
	window.location = base+url;
}

function reload(nocache){
	window.location.reload(nocache ? true : false);
}

function parseSearchString(str){
	return str.replace(/[^\w\d\s@ñáéíóú_\.]/g,'').replace(/^\s+|\s+$/g,'').replace(/\s+/g,'-').replace(/\./g,'_');
}

function moneda(n){
	n = n.toFixed(2)+'';
	var x = n.split('.');
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x[0])){x[0] = x[0].replace(rgx,'$1'+' '+'$2');}
	return msim+' '+x[0]+'.'+x[1];
}

function noSelects(mode){
	if(Browser.Engine.trident4){
		if(mode){
			body.addClass('noSelects');
		}else{
			body.removeClass('noSelects');
		}
	}
}

/* iRules */
var iRules = {
	name: {msg: _lng.iRules.name, regx: /^.{3,128}$/},
	mail: {msg: _lng.iRules.mail, regx: /^([\w\d_\.\-])+\@(([\w\dáéíóúñ\-])+\.)+([\w\d]{2,4})+$/},
	password: {msg: _lng.iRules.password, regx: /^[\w\d]{4,16}$/},
	telephone: {msg: _lng.iRules.telephone, regx: /^[\d()+\- ]{7,24}$/},
	number: {msg: _lng.iRules.number, regx: /^\d+$/},
	content: {msg: _lng.iRules.content, regx: /^(.){3,}$/},
	date: {msg: _lng.iRules.date, regx: /^[\d]{2}\/[\d]{2}\/[\d]{4}$/},
	hour: {msg: _lng.iRules.hour, regx: /^([0-9]|[0-1][0-9]|2[0-3]):[0-5]0$/},
	bool: {msg: _lng.iRules.bool, regx: /^0|1$/},
	color: {msg: _lng.iRules.color, regx: /^(#([\w\d]{3}|[\w\d]{6})|([\d]{1,3},[\d]{1,3},[\d]{1,3}))$/},
	image: {msg: _lng.iRules.image, regx: /\.(jpg|jpeg|gif|png)$/i},
	jpg: {msg: _lng.iRules.jpg, regx: /\.jpg$/i},
	select: {msg: _lng.iRules.bool, regx: /^.+$/},
	price: {msg: _lng.iRules.price, regx: /^[\d]{1,5}(\.[\d]{1,2})?$/},
	dni: {msg: _lng.iRules.dni, regx: /^[\d]{8}$/},
	ruc: {msg: _lng.iRules.ruc, regx: /^[\d]{11}$/}
};

/* loading */
var Loading = {
	init: function(){
		if(!this.initialized){
			this.initialized = true;
			var sizes = $('body').getCoordinates();
			this.overlay = new Element('div',{'id':'loadingOverlay','class':'hidden'}).setOpacity(0.7).inject(document.body);
			this.overlay.setStyles({'width':sizes.width,'height':sizes.height,'left':'50%','top':sizes.top+'px','margin-left':Math.round(-sizes.width/2)+'px'});
			this.modal = new Element('div',{'id':'loadingModal'}).inject(document.body);
		}
	},
	set: function(message,klass,full){
		this.modal.className = klass || 'wait';
		this.modal.innerHTML = full ? (message || _lng.wait) : '<p>'+(message || _lng.wait)+'</p>';
	},
	show: function(){
		if(!this.open){
			noSelects(true);
			this.open = true;
			this.init();
			this.overlay.className = '';
			this.set();
			window.scroll(0,0);
		}
	},
	hide: function(){
		if(this.open){
			this.open = false;
			this.overlay.className = 'hidden';
			this.modal.className = 'hidden';
			noSelects(false);
		}
	},
	delay: function(){
		
	}
};


var enabledBlink = true;
var tipPos = true;
var validateForm = function(e){
	if(!validator.test(true)){
		new Event(e).stop();
		validator.showInvalid(enabledBlink,tipPos,'Error');
		this.action='/';
	}else{
		loading.show();
		this.action=window.location.href;
	}
};

/* request */
var onError = function(){
	alert('Error: '+this.status+'\nPor favor, comunique al administrador.\nGracias');
	Loading.hide();
}

function str_pad( input, pad_length, pad_string, pad_type ) {
    var half = '', pad_to_go;
 
    var str_pad_repeater = function(s, len){
            var collect = '', i;
 
            while(collect.length < len) collect += s;
            collect = collect.substr(0,len);
 
            return collect;
        };
 
    if (!['STR_PAD_LEFT','STR_PAD_RIGHT','STR_PAD_BOTH'].contains(pad_type)){ pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }
 
    return input;
}
