var ancho=screen.width/2;
var alto=screen.height/2;
var agenda_fecha=1;
var notas_agenda_v=false;
var IE = document.all?true:false;
var LL=false;
var LL2=false;
function getPos(id){
	xx=document.getElementById(id).offsetLeft;
	yy=document.getElementById(id).offsetTop;
	r=new Array(xx,yy);
	return r;
}
function Muestra(id){
	window.open("mensaje.php?id="+id,"",'width=400,height=300,top='+(alto-80)+',left='+(ancho-165)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
}
function cambia(sel){
	document.location="?p=principal&pag="+sel;
}
function verify(){
	v=document.getElementById('busca').value;
	if(v.length<3) {
		alert("La palabra ingresada no es valida");
		return false;
	}
	return true;
}
function Terminos(){
	window.open("legal.html","",'width=300,height=370,toolbar=no,menubar=no,scrollbars=yes,location=no');
}

function MuestraMensaje(id){
	window.open("admin/mensaje.php?id="+id,"",'width=250,height=320,top='+(alto-250)+',left='+(ancho-125)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
}
function EliminaMensaje(id){
	rpta=confirm("Estas seguro que quieres borrar este mensaje?");
	if(rpta==true){
		borra=document.getElementById("borramensaje");
		borra.value=id;
		document.F1.submit();
	}
}
function VerificaNick(){
	v=document.getElementById('nick').value;
	if(v.length<1){
		alert("El alias debe tener aunque sea 1 letra");
	}else{
		ifr=document.getElementById('IFX1');
		ifr.document.form5.nick.value=v;
		ifr.document.form5.submit();
	}
}
function EligeDia(d,c){
	document.location="?p=Agenda&d="+d+"&contacto="+c;
}

function Color(t,c){
	t.style.background=c;
}

function nuevocorreo(p,d){
	cadena="";
	if(document.usuarios!='undefined'){
		for(cb in document.usuarios.elements){
			if(cb.substr(0,2)=="cb" || parseInt(cb)==cb){
				if(document.usuarios.elements[cb].checked==true){
					cadena+=document.usuarios.elements[cb].name.substr(2)+";";document.usuarios.elements[cb].checked=false;
				}
			}
		}
	}
	if(p>0) p=cadena+p;
	else p=cadena.substr(0,cadena.length-1);
	window.open("gerencia/correo.php?to="+p+"&d="+d,"",'width=410,height=540,top='+(alto-270)+',left='+(ancho-205)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
}

function nuevomensaje(p,d){
	cadena="";
	if(document.usuarios!='undefined'){
		for(cb in document.usuarios.elements){
			if(cb.substr(0,2)=="cb" || parseInt(cb)==cb){
				if(document.usuarios.elements[cb].checked==true){
					cadena+=document.usuarios.elements[cb].name.substr(2)+";";document.usuarios.elements[cb].checked=false;
				}
			}
		}
	}
	if(p>0) p=cadena+p;
	else p=cadena.substr(0,cadena.length-1);
	window.open("admin/nmensaje.php?p="+p+"&d="+d,"",'width=250,height=320,top='+(alto-225)+',left='+(ancho-125)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
}

function seleccionaTodos(v){
	cadena="";
	if(document.usuarios!='undefined'){
		for(cb in document.usuarios.elements){
			if(cb.substr(0,2)=="cb" || parseInt(cb)==cb){
				document.usuarios.elements[cb].checked=v.checked;
			}
		}
	}
}

function MuestraNuevo(t,id){
	switch(t){
		case 1:pag='gerencia/nuevo_contacto.php';t1='Edita Contacto';t2='Nuevo Contacto';break;
		case 2:pag='gerencia/nueva_empresa.php';t1='Edita Empresa';t2='Nueva Empresa';break;
		case 3:pag='gerencia/producton.php';t1='Edita Producto';t2='Nuevo Producto';break;
		case 4:pag='bproject/proyecto.php';t1='Edita Proyecto';t2='Nuevo Proyecto';break;
		case 5:pag='bproject/agrega_a_proyecto.php';t1='Edita Personal del proyecto';t2='Agrega personal al Proyecto';break;
		case 6:pag='bproject/nueva_tarea.php';t1='Edita Tarea';t2='Agrega Tarea';break;
	}
	if(id>0){
		tiempo=new Date();
		tiempo=Math.round(tiempo.getTime()/1000);
		pagina=pag+"?id="+id+"&jsts="+tiempo;
		document.getElementById('TituloNuevo').innerHTML=t1;
	}else{
		pagina=pag;
		document.getElementById('TituloNuevo').innerHTML=t2;
	}
	document.getElementById('nuevo_iframe').src=pagina;
	ifr=document.getElementById('nuevo_table');
	ifr.style.visibility='visible';
	ifr.style.display='';
}
function MuestraTarea(pid,id){
	pag='bproject/nueva_tarea.php?pid='+pid;
	if(id>0){
		pag+="&id="+id;
		document.getElementById('TituloNuevo').innerHTML="Editar Tarea";
	}else{
		document.getElementById('TituloNuevo').innerHTML="Agregar Tarea";
	}
	document.getElementById('nuevo_iframe').src=pag;
	ifr=document.getElementById('nuevo_table');
	ifr.style.visibility='visible';
	ifr.style.display='';
}
function OcultaNuevo(){
	ifr=document.getElementById('nuevo_table');
	ifr.style.visibility='hidden';
	ifr.style.display='none';
}

function MuestraDatos(t,id){
	switch(t){
		case 1:pag='contacto.php';t='Datos de Contacto';break;
		case 2:pag='cliente.php';t='Datos de Empresa';break;
	}
	pagina="admin/"+pag+"?id="+id;
	document.getElementById('TituloNuevo').innerHTML=t;
	document.getElementById('nuevo_iframe').src=pagina;
	ifr=document.getElementById('nuevo_table');
	ifr.style.visibility='visible';
	ifr.style.display='';
}
function NuevoProyecto(id,pid){
	if(pid>0){
		window.open("bproject/proyecto.php?pid="+id+"&id="+pid,"1",'width=340,height=370,top='+(alto-325)+',left='+(ancho-175)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
	}else{
		window.open("bproject/proyecto.php?pid="+id,"1",'width=340,height=370,top='+(alto-325)+',left='+(ancho-175)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
	}
}
function NuevoProductoEmpresa(id,pid){
	if(pid>0){
		window.open("gerencia/producto.php?eid="+id+"&id="+pid,"1",'width=340,height=320,top='+(alto-225)+',left='+(ancho-175)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
	}else{
		window.open("producto.php?eid="+id,"1",'width=340,height=320,top='+(alto-225)+',left='+(ancho-175)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
	}
}

function Personal(){
	window.open("admin/personal.php","",'width=340,height=500,top='+(alto-325)+',left='+(ancho-175)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
}
function Modifica(val,id){
	document.forms.form1.accion.value=val;
	document.forms.form1.id.value=id;
	document.forms.form1.submit();
}

function CambiaFecha(id){
	fi=document.getElementById('tareai');
	ff=document.getElementById('tareaf');
	if(id==1){
		fi.style.color="#FF0000";
		ff.style.color="#000000";
	}else{
		ff.style.color="#FF0000";
		fi.style.color="#000000";
	}
	agenda_fecha=id;
}
function CambiaHora(tipo,v,cadena){
	if(agenda_fecha==1) seth="seth=i";
	else seth="seth=f";
	if(tipo=="m") valor="&m="+v;
	else valor="&h="+v;
	document.location="?"+cadena+"&"+seth+valor;
}
function SelHora(m,d){
	document.location='?p=Agenda&id=0&m='+m+'d='+d;
}
function EliminaAgenda(id){
	if(id>0){
		rpta=confirm("Estas seguro que quieres borrar esta tarea?");
		if(rpta==true){
			elimina=document.getElementById('borraagenda');
			elimina.value=id;
			document.agendaform.submit();
		}
	}else alert("Ninguna tarea seleccionada");
}
function NuevaNota(id,cid){
	window.open("admin/notas.php?id="+id+"&cid="+cid,"",'width=220,height=185,top='+(alto-150)+',left='+(ancho-125)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
}
function EliminaNota(id){
	rpta=confirm("Estas seguro que quieres borrar este mensaje?");
	if(rpta==true){
		borra=document.getElementById("borranota");
		borra.value=id;
		alert(document.F1);
		document.F1.submit();
	}
}
function VerificaTiempos(){
	fechai=document.getElementById("fechai").value;
	fechai=fechai.split(" ");
	fechai=fechai[1].split(":");
	minutoi=fechai[0]*6+Math.floor(fechai[1]/10);
	fechaf=document.getElementById("fechaf").value;
	fechaf=fechaf.split(" ");
	fechaf=fechaf[1].split(":");
	minutof=fechaf[0]*6+Math.floor(fechaf[1]/10);
	for(a in tiempos_usados){
		if(minutoi>=tiempos_usados[a][0] && minutoi<tiempos_usados[a][1]){
			alert("El tiempo de inicio ingresado se cruza con otra tarea");
			return false;
		}
		if(minutof>tiempos_usados[a][0] && minutof<=tiempos_usados[a][1]){
			alert("La hora final ingresada se cruza con otra tarea");
			return false;
		}
		if(minutoi<=tiempos_usados[a][0] && minutof>=tiempos_usados[a][1]){
			alert("La tarea ingresada se cruza con otra tarea");
			return false;
		}
	}
	return true;
}
function  VerificaAgenda(){
	if(!VerificaTiempos())return false;
	alerta="";
	re=true;
	fechai=document.getElementById('fechai').value;
	fechaf=document.getElementById('fechaf').value;
	titulo=document.getElementById('titulo').value;
	if(fechai.length<8){alerta+="La fecha del inicio de la tarea no esta establecida\n";re=false;}
	if(fechaf.length<8){alerta+="La fecha del fin de la tarea no esta establecida\n";re=false;}
	if(titulo.length<2){alerta+="El titulo de la tarea no esta establecido";re=false;}
	if(!re) alert(alerta);
	return re;
}
function EliminaEmpresa(id){
	rpta=confirm("Estas seguro que quieres borrar este cliente?");
	if(rpta==true){
		borra=document.getElementById("borracliente");
		borra.value=id;
		document.F1.submit();
	}
}
function EliminaContacto(id){
	rpta=confirm("Estas seguro que quieres borrar este contacto?");
	if(rpta==true){
		borra=document.getElementById("borracontacto");
		borra.value=id;
		document.F1.submit();
	}
}

function EliminaProducto(id){
	rpta=confirm("Estas seguro que quieres borrar este producto?");
	if(rpta==true){
		borra=document.getElementById("borraproducto");
		borra.value=id;
		document.F1.submit();
	}
}

function DesplazaAgenda(){
	tabla=document.getElementById('tabla_notas');
	if(!notas_agenda_v){
		posp=getPos('tabla_principal');
		post=getPos('tabla_agenda');
		pos=getPos('desplaza_agenda');
		if(document.getElementById('celdapa')!='undefined' && document.getElementById('celdapa')!=null) pospa=getPos('celdapa');
		else pospa=new Array(0,0);
		tabla.style.visibility='visible';
		tabla.style.display='';
		//alert(pos[0]+"-"+posp[1]+"-"+post[0]+"-"+pospa[0]);
		tabla.style.left=pos[0]+posp[0]+post[0]+pospa[0]+6;
		tabla.style.top=pos[1]+posp[1]+post[1]+pospa[1]+30-tabla.offsetHeight;
		notas_agenda_v=true;
	}else{
		tabla.style.visibility='hidden';
		tabla.style.display='none';
		notas_agenda_v=false;
	}
}
function CalculaPrecio(){
	conta=document.getElementById('conta').value;
	costo=0;
	for(i=0;i<conta;i++){
		tiempo=document.getElementById('tiempo'+i).value;
		recurso=document.getElementById('recurso'+i).value;
		precio=recurso.split(",");
		precio=precio[1];
		costo+=tiempo*precio;
	}
	tiempo=document.getElementById('tiempo').value;
	recurso=document.getElementById('recurso').value;
	precio=recurso.split(",");
	precio=precio[1];
	costo+=tiempo*precio;
	preciodiv=document.getElementById('preciodiv');
	preciodiv.innerHTML="CF= "+costo;
}
function AbreFoto(id){
	window.open("images/fotos/gr/"+id+".jpg","",'width=260,height=340,top='+(alto-150)+',left='+(ancho-170)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
}
function AbreEditaFoto(id){
	window.open("foto.php?id="+id,"4",'width=280,height=80,top='+(alto-150)+',left='+(ancho-135)+',toolbar=no,menubar=no,scrollbars=yes,location=no');
}
function MuestraDetallesP(id){
	id=document.getElementById(id);
	id.style.display=(id.style.display=='')?"none":'';
	//id.style.visibility=(id.style.visibility=='hidden')?"visible":"hidden";
}
function ActualizaProyecto(t,id){
	document.getElementById('tipo').value=t;
	document.getElementById('id').value=id;
	val=prompt("Porcentaje de avance");
	if(parseInt(val)+""==val){
		if(parseInt(val)>100 || parseInt(val)<0) alert("El valor ingresado no es válido");
		else{
			document.getElementById('valor').value=val;
			document.forms.FP1.submit();
		}
	}
	else alert("El valor ingresado no es un número");
}
function MuestraLinea(e){
	var l=document.getElementById('linea');
	var hora1=document.getElementById('hora_1');
	var hora2=document.getElementById('hora_2');
	p=getPos('celda_principal');py =p[1];px =p[0];
	p=getPos('tabla_principal');py+=p[1];px+=p[0];
	for(var i=1;i<4;i++){p=getPos('h'+i);py+=p[1];px+=p[0];}
	if(IE){
		t = event.clientY + document.body.scrollTop;
		d=10;
	}else{
		if(!LL2){
			document.captureEvents(Event.MOUSEMOVE);
			document.getElementById('mmm').onmousemove=MuestraLinea;
			l.onmousemove=MuestraLinea;
			LL2=true;
		}
		t = e.pageY;
		d=7;
	}
	if(!LL){
		l.style.left=px-d;
		LL=true;
	}
	val=24-(t-py-11)/(350/24);
	if(val>=0 && val<=24){
		h=Math.floor(val);
		m=Math.round((val-h)*60);
		if(h<10) h="0"+h;
		if(m<10) m="0"+m;
		val=h+":"+m;
		l.style.top=t-7;
		hora1.innerHTML=val;
		hora2.innerHTML=val;
		if(l.style.visibility=='hidden')l.style.visibility='visible';
	}else{
		if(l.style.visibility=='visible')l.style.visibility='hidden';
	}
}
