var destino="";
var infoimg = (
	{
		infoLoad:false,
		imageLoad:false,
		info:{
			width:0,
			height:0,
			pathdown:''
		},
		clear:function(){
			this.infoLoad=false;
			this.imageLoad=false;
		},
		show: function(){
			descargar = '<a href="'+this.info.pathdown+'"><img src="/images/tools/descargar.gif" alt="Descargar"/></a>';
			$("popimg_tool").update("<span>Tama&ntilde;o: "+this.info.width+"x"+this.info.height+"</span> "+descargar);
		}
	});
images = ['tools/descargar.gif','loading.gif'];
images.each(function(i){a=new Image;a.src='/images/'+i});

function lTrim(cadena){
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}
	return cadena;
}

function chr(c){
	return String.fromCharCode(c);
}

function valrut(nRut, dv){
	dve = "";
	while((8-nRut.length)>0){nRut="0"+nRut}

	digo  = 0;
	digo += nRut.charAt(0)*3;
	digo += nRut.charAt(1)*2;
	digo += nRut.charAt(2)*7;
	digo += nRut.charAt(3)*6;
	digo += nRut.charAt(4)*5;
	digo += nRut.charAt(5)*4;
	digo += nRut.charAt(6)*3;
	digo += nRut.charAt(7)*2;

	resto=11 - mod(digo,11);

	if(resto==10){
		dve="K";
	}else{
		dve=resto.toString();
	}

	if(resto==11){
		dve="0";
	}


	if(dve==dv){
		return true;
	}else{
		return false;
	}
}

function mod(a , b){
	return Math.round(((a/b)-Math.floor(a/b))*b)
}

function valNum(e, float, valor) {
	tecla = (document.all)?e.keyCode:e.which;
	if (tecla==13||tecla==8||tecla==0) return true;
	if(float){
		patron = /^(?:\+|-)?\d+(\.)?\d*$/;
		return patron.test(valor+String.fromCharCode(tecla));
	}
	patron = /^(\d)*$/;
	return patron.test(String.fromCharCode(tecla));
}

function valDV(e) {
	tecla = (document.all)?e.keyCode:e.which;
	if (tecla==8||tecla==0) return true;
	patron = /[\dKk]/;
	return patron.test(String.fromCharCode(tecla));
}

function redirect(donde){
	window.location=donde;
}

function emailCheck (emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	//yo
	if (emailStr==''){
		//1return "No ha escrito la direccion de E-Mail";
		return false;
	}
	//fin yo
	if (matchArray==null){
		//return "Dirección Email está incorrecta Por favor verifique @ y .";
		return false;
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
		//return "Username no valido.";
		return false;
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//return "Dirección IP no Valida!";
				return false;
			}
		}
		return true;
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//return "El dominio indicado no existe.";
		return false;
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
		//return "La dirección debe finalizar con un dominio valido.";
		return false;
	}
	if (len<2) {
		//return "Dirección incorrecta";
		return false;
	}
	return true;
}

function msg(strMsg,obj){
	alert(strMsg);
	if(obj)$(obj).focus();
	return false;
}

function openimg(imagen){
	infoimg.clear();
	remove = "remover(['popimg_bg', 'popimg', 'popimg_tool'])";
	style = "display:none; position: absolute";
	lImage = "/images/loading.gif";
	div = '<div id="popimg_bg" onclick="'+remove+'" style="'+style+'; background-color:#000"></div>';
	img = '<img id="popimg" src="'+lImage+'" style="'+style+'" alt="Cargando..." onclick="'+remove+'"/>';
	tool = '<div id="popimg_tool" onclick="'+remove+'" style="'+style+'; background-color:#fff;" onmouseover="this.setOpacity(1)" onmouseout="this.setOpacity(0.3)"></div>';
	new Insertion.Bottom('divimg', div);
	new Insertion.Bottom('divimg', img);
	new Insertion.Bottom('divimg', tool);
	new Insertion.Bottom('divimg', '<img id="tmpImage" src="" style="display:none"/>');
	$("popimg_bg").setOpacity(0.6);
	$("popimg_tool").setOpacity(0.3)
	resize();
	tmpImage = $('tmpImage');//new Image();
	tmpImage.alt = imagen;
	tmpImage.onload = function(){
		$("popimg").hide();
		$("popimg").onload = function(){
			resize(); 
			this.appear({duration:0.5});//show();
			//new Effect.Morph("popimg",{style:'width:'+tmpImage.getWidth()+'px; height:'+tmpImage.getHeight()+'px; top:30px;', duration:1});
		};
		$("popimg").src = this.src;
		infoimg.imageLoad=true;
		if(infoimg.infoLoad){
			infoimg.show();
		}
	}
	new Ajax.Request("/lib/infoimg.php",
	{
		parameters: "file="+imagen,
		method:"post",
		onSuccess: function(transport){
			json = eval(transport.responseText);
			infoimg.info.width = json.width;
			infoimg.info.height = json.height;
			infoimg.info.pathdown = json.pathdown;
			infoimg.infoLoad=true;
			if(infoimg.imageLoad){
				infoimg.show();
			}
		},
		onFailure: function(resp){
			alert(resp.responseText);
		}
	});
	//$("popimg").setStyle({width:'16px', height:'16px', top:'30px'})
	tmpImage.src = imagen;
	$("popimg_bg").show();
	$("popimg").show();
	$("popimg_tool").show();
	Event.observe(window, 'scroll', on_scroll);
	Event.observe(window, 'resize', resize);
}
function resize(event){
	$("popimg_bg").style.height = document.viewport.getHeight()+"px";
	on_scroll(event);
}
function on_scroll(event){
	
	bg = $("popimg_bg");
	wscroll = document.viewport.getScrollOffsets();
	bg.style.left = wscroll.left+"px";
	bg.style.top = wscroll.top+"px";
	img = $("popimg");
	img.style.left = $("popimg_tool").style.left = ( ( wscroll.left+( bg.getWidth()-img.getWidth() ) )/2)+"px";
	img.style.top = $("popimg_tool").style.top = (wscroll.top+30)+"px";
	bg.setStyle({top: (wscroll.top)+'px'});
}
function remover(elementos){
	elementos.each(function(e){$(e).remove()});
	Event.stopObserving(window, 'scroll');
	Event.stopObserving(window, 'resize');
}

