function empty(obj){
	return obj===undefined || obj==null || obj===false || obj==='';
}

function alt(a,b,c){
	b = b ? b : '';
	c = c ? c : '';
	if(empty(c)){
		return empty(a) ? b : a;
	}else{
		return empty(a) ? c : b;
	}
}

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

function $keys(obj){
	var keys = [];
	for(var k in obj){
		keys.push(k);
	}
	return keys;
}

function redirect(url){
	url = url.substr(0,4)=='http' ? url : '/'+url.replace(/^\/+/,'');
	window.location = url;
}

function reload(){
	window.location = window.location.href+'';
}

function lngCode(id){
	for(var code in LNGS){
		if(LNGS[code][1]==id){
			return code;
		}
	}
	return false;
}

function moneda(n,s){
	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 (s ? MSIM+' ' : '')+x[0]+'.'+x[1];
}

// base64 encode-decode
var b64 = {
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	encode : function (input){
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = utf8.encode(input);

		while (i<input.length){
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
		}
		return output;
	},

	decode : function (input){
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^\w\d\+\/\=]/gi, "");

		while (i < input.length){
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64){
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64){
				output = output + String.fromCharCode(chr3);
			}

		}

		output = utf8.decode(output);

		return output;
	}
};

// utf8 encode-decode
var utf8 = {
	encode : function (string){
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);

			if (c < 128){
				utftext += String.fromCharCode(c);
			} else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},

	decode : function (utftext){
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ){
			c = utftext.charCodeAt(i);

			if (c < 128){
				string += String.fromCharCode(c);
				i++;
			} else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
};

//ccard number validation
function luhn_check(number){
	var number_length = number.length;
	var parity = number_length % 2;
	var total = 0;
	for (i=0; i < number_length; i++){
		var digit = number.charAt(i);
		if (i % 2 == parity){
			digit = digit * 2;
			if (digit > 9){
				digit = digit - 9;
			}
		}
		total = total + parseInt(digit);
	}
	return total % 10 == 0;
}

// html_entity_decode
function html_entity_decode(str){
	var el = new Element('textarea',{'style':'display:none'}).inject(document.body);
	el.innerHTML = str;
	str = el.value;
	el.dispose();
	return str;
}

// tipos de archivo
function fileExt(str){
	var ext = str.match(/\.(.+)$/);
	return ext ? ext[1].toLowerCase() : false;
}

function fileType(str){
	var fileTypes = {
		acrobat : ['pdf'],
		application : ['exe','bat'],
		compress : ['zip','7z','rar','ace','cab','gz','gzip','tar','tar.z','tar.gz','tz','taz','tgz','jar','bz','bz2'],
		excel : ['ods','xlsx','xls','csv'],
		image : ['jpg','gif','bmp','png','tif','jpeg'],
		powerpoint : ['odp','pptx','ppsx','ppt','pps'],
		text : ['txt','inf','log'],
		word : ['odt','doc','docx']
	};
	var type = 'unknown';
	var ext = fileExt(str);
	if(ext){
		for(var _type in fileTypes){
			if(fileTypes[_type].contains(ext)){
				type = _type;
				break;
			}
		}
	}
	return type;
}

// mascara ie para tapar elementos select,object,etc
var Shim = new Class({
	initialize: function(ref,zIndex){
		this.ref = ref;
		this.zIndex = zIndex;
	},

	build: function(){
		this._resize = this.resize.bind(this);
		this.iframe = new Element('iframe',{src:'javascript:false;document.write("");',frameBorder:'0',scrolling:'no',styles:{display:'none',filter:'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)',position:'absolute',display:'none'}}).inject(document.body);
	},

	resize: function(){
		(function(){
			var styles = {'z-index':this.zIndex};
			if(this.ref){
				$each(this.ref.getCoordinates(),function(v,k){
					if(!['right','bottom'].contains(k)){
						styles[k] = v+'px'
					}
				});
			}else{
				styles = $extend(styles,{height:Math.max(body.getHeight(),window.getHeight())+'px',width:'100%',left:'0',top:'0'});
			}
			this.iframe.setStyles(styles);
		}).delay(5,this);
	},

	show: function(focus){
		if(!this.iframe){
			this.build();
		}
		this.resize();
		this.iframe.setStyle('display','block');
		window.addEvent('resize',this._resize);
		if(focus){
			this.iframe.focus();
		}
	},

	hide: function(){
		this.iframe.setStyle('display','none');
		window.removeEvent('resize',this._resize);
	}
});

// capa para opacar otras capas
var Overlay = new Class({
	initialize: function(ref,zIndex,styles){
		this.ref = ref;
		this.styles = $merge({position:'absolute','background':'#fff','opacity':0.5},styles||{});
		this.zIndex = zIndex || 100;
		this._resize = this.resize.bind(this);
		if(Browser.Engine.trident){
			this.shim = new Shim(ref,zIndex-1);
		}
	},

	build: function(){
		this.initialized = true;
		this.el = new Element('div',{styles:this.styles}).inject(document.body);
		if(Browser.Engine.trident){
			this.shim.build();
		}
	},

	resize: function(){
		var _resize = function(){
			var styles = {'z-index':this.zIndex};
			if(this.ref){
				$each(this.ref.getCoordinates(),function(v,k){
					if(!['right','bottom'].contains(k)){
						styles[k] = v+'px'
					}
				});
			}else{
				styles = $extend(styles,{height:Math.max(body.getHeight(),window.getHeight())+'px',width:'100%',left:'0',top:'0'});
			}
			this.el.setStyles(styles);
		}.bind(this);
		if(Browser.Engine.trident){
			_resize.delay(5,this);
		}else{
			_resize();
		}
	},

	show: function(){
		if(!this.initialized){
			this.build();
		}
		this.resize();
		this.el.setStyle('display','block');
		window.addEvent('resize',this._resize);
		if(Browser.Engine.trident){
			this.shim.show();
		}
	},

	hide: function(){
		if(this.initialized){
			this.el.setStyle('display','none');
			window.removeEvent('resize',this._resize);
			if(Browser.Engine.trident){
				this.shim.hide();
			}
		}
	}
});


// Loading
var Loading = {
	build: function(){
		this._build = true;
		this.overlay = new Element('div',{'id':'loading_overlay'}).setOpacity(0.5).inject(document.body);
		this.modal = new Element('div',{'id':'loading_modal'}).inject(document.body);
		this.doc = body.getParent();
		if(Browser.Engine.trident){
			this._resize = this.resize.bind(this);
			this.shim = new Shim(false,999);
			$$(this.overlay,this.modal).setStyle('position','absolute');
		}else{
			this.input = new Element('input').inject(this.overlay);
		}
	},

	resize: function(){
		this.overlay.setStyle('height',Math.max(body.getHeight(),window.getHeight())+'px');
	},

	set: function(msg,klass){
		this.modal.innerHTML = '<table class="'+(klass || 'wait')+'"><tr><td>'+(msg || _jlng.wait)+'</td></tr></table>';
	},

	show: function(msg,klass){
		if(!this._build){
			this.build();
		}
		if(Browser.Engine.trident){
			window.addEvent('resize',this._resize);
			this.resize();
			this.shim.show(true);
		}else{
			this.input.focus();
		}
		this.set(msg,klass);
		this.doc.setStyle('cursor','wait');
		$$(this.overlay,this.modal).setStyle('display','block');
	},

	hide: function(){
		if(this._build){
			if(Browser.Engine.trident){
				window.removeEvent('resize',this._resize);
				this.shim.hide();
			}
			this.doc.setStyle('cursor','');
			$$(this.overlay,this.modal).setStyle('display','none');
		}
	}
};

// iframe upload (ajax-upload)
var UIF = {
	frame: function(n,c){
		var d = document.createElement('DIV');
		d.className = 'hidden';
		d.innerHTML = '<iframe src="about:blank" id="'+n+'" name="'+n+'" onload="UIF.onLoad(\''+n+'\')"></iframe>';
		document.body.appendChild(d);
		document.getElementById(n).onComplete = c;
		return n;
	},

	send: function(f,n,c){
		f.setAttribute('target',UIF.frame('uif_'+n,c));
	},

	onLoad: function(n){
		var i = document.getElementById(n);
		if(i.contentDocument){
			var d = i.contentDocument;
		}else if(i.contentWindow){
			var d = i.contentWindow.document;
		}else{
			var d = window.frames[n].document;
		}
		if(d.location.href != 'about:blank'){
			setTimeout(function(){
				var f = i.onComplete, r = d.body.innerHTML, p = i.parentNode;
				p.removeChild(i); p.parentNode.removeChild(p);
				f(r);
			},20);
		}
	}
};

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

//ImagesPreview
var ImagesPreview = false;
if($try(function(){return Tips;})){
	ImagesPreview = new Class({
		Extends: Tips,
		attach: function(elements){
			var read = function(option, element){
				if (option == null) return '';
				return $type(option) == 'function' ? option(element) : element.get(option);
			};
			//
			this.options.text = function(el){
				var src = this.options.replace ? el.src.replace(this.options.replace[0],this.options.replace[1]) : el.src;
				return '<img src="'+src+'" alt="loading..." />';
			}.bind(this);
			//
			$$(elements).each(function(element){
				if(element.get('tag')=='img' && !element.src.test(/none\.jpg$/)){
					var title = read(this.options.title, element);
					element.erase('title').store('tip:native', title).retrieve('tip:title', title);
					element.retrieve('tip:text', read(this.options.text, element));
					
					var events = ['enter', 'leave'];
					if (!this.options.fixed) events.push('move');
					
					events.each(function(value){
						element.addEvent('mouse' + value, element.retrieve('tip:' + value, this['element' + value.capitalize()].bindWithEvent(this, element)));
					}, this);
				}
			}, this);
			//
			return this;
		}
	});
}

// validation rules
var iRules = {"email":{"msg":"Email incorrecto.","regx":"\/^([\\w\\d_\\.\\-])+\\@(([\\w\\d_\\-])+\\.)+([\\w\\d]{2,4})+$\/"},"password":{"msg":"Debe contener s\u00f3lo n\u00fameros y letras.","regx":"\/^[\\w\\d]{4,16}$\/"},"any":{"msg":"Debe ingresar un valor.","regx":"\/\\S{1,128}\/"},"text":{"msg":"No menos de 3 letras.","regx":"\/\\S{3,128}\/"},"phone":{"msg":"Ejemplos: 425-2354 , 051(511) +01 254 25402","regx":"\/^[\\d\\(\\)+\\- ]{7,24}$\/"},"date":{"msg":"dd\/mm\/aaaa (ejemplo: 16\/02\/1981)","regx":"\/^[\\d]{2}\\\/[\\d]{2}\\\/[\\d]{4}$\/"},"hour":{"msg":"S\u00f3lo intervalos de 10 minutos.<br \/><b>Ejemplos<\/b>: 16:20, 00:40, 8:50, 13:00","regx":"\/^([0-9]|[0-1][0-9]|2[0-3]):[0-5]0$\/"},"document":{"msg":"Debe elegir archivos de documento.","regx":"\/\\.(doc|docx|pdf)$\/i"},"image":{"msg":"Debe elegir archivos de tipo imagen.","regx":"\/\\.(jpg|jpeg|gif|png)$\/i"},"jpg":{"msg":"Debe elegir una imagen tipo JPG.","regx":"\/\\.jpg$\/i"},"number":{"msg":"Debe contener s\u00f3lo numeros.","regx":"\/^\\d+$\/"},"ids":{"msg":"Ids no v\u00e1lidos.","regx":"\/^([\\d]+[,])*[\\d]+$\/"},"decimal":{"msg":"Ejemplos: 10, 10.5, 10.05 ","regx":"\/^[\\d]{1,}(\\.[\\d]{1,2})?$\/"},"price":{"msg":"Valores de precio (10, 10.00, 10.25)","regx":"\/^[\\d]{1,5}(\\.[\\d]{1,2})?$\/"},"code":{"msg":"C\u00f3digo incorrecto","regx":"\/^[\\w\\d\\s\\-]+$\/"},"zipcode":{"msg":"C\u00f3digo postal incorrecto","regx":"\/^[\\d]{5}(-[\\d]{4})?$\/"},"ccard":{"msg":"N\u00famero de tarjeta incorrecto","regx":"\/^[\\d]{14,19}$\/"},"cvv":{"msg":"C\u00f3digo de tarjeta incorrecto","regx":"\/^[\\d]{3,4}$\/"},"url":{"msg":"URL incorrecta:<br \/>Ejemplos:<br \/>http:\/\/google.com<br \/>http:\/\/es.ayuda.com\/url","regx":"\/^http\\:\\\/\\\/([\\w\\d\\-]+\\.){1,3}[\\w]{2,}[\\S]*$\/"},"dbbool":{"msg":"Debe elegir una de las opciones.","regx":"\/^0|1$\/"},"select":{"msg":"Debe seleccionar una opci\u00f3n.","regx":"\/^.+$\/"},"color":{"msg":"Debe elegir un color.","regx":"\/^(#[\\w\\d]{3}|[\\w\\d]{6})|([\\d]{1,3},[\\d]{1,3},[\\d]{1,3})$\/"},"bliptv":{"msg":"No es una url v\u00e1lida","regx":"\/^http\\:\\\/\\\/(www\\.)?blip\\.tv\\\/file\\\/([\\d]+)([^\\s]+)?$\/i"}};
for(var i in iRules){
	var rgxmtch = iRules[i].regx.match(/^\/(.+)\/([^\/]+)?$/);
	if(rgxmtch[2]){
		iRules[i].regx = new RegExp(rgxmtch[1],rgxmtch[2]);
	}else{
		iRules[i].regx = new RegExp(rgxmtch[1]);
	}
}
delete(rgxmtch);

// lng text
var _jlng = {"required":"Campo requerido.","wait":"espere por favor..."};
/*Date: Wed, 10 Mar 2010 13:10:07 GMT */