﻿
        var idesp;
        var idcur;
        var curdiv;
        //var aurlM = "http://" + window.location.host + "/institutoalcon/Mediateca/Controles/Mediateca.aspx";
        var aurlM = "http://www.institutoalcon.com/Mediateca/Controles/Mediateca.aspx";
        var xinfM;
        var yinfM;
        var modo;  

        var txtVacio;
        var txtMax;
        var txtCaducado;
        var txtSolicitado;

	    function getLng() {
		    var slng;
		    if (document.getElementById("txtLng"))
		    {
		        slng = document.getElementById("txtLng").value;
            }
            else
            {
		        slng = "ESP";
		    }
		    return slng;
	    }

	    if (document.all) {
		    document.onmousemove = handlerMMM;
	    } else {
		    document.captureEvents(Event.MOUSEMOVE);
		    document.onmousemove = handlerMMM;
	    }

	    document.onclick = docclick;

	    function handlerMMM(e){
		    mxM = (e) ? e.pageX : document.body.scrollLeft+event.clientX;
		    myM = (e) ? e.pageY : document.body.scrollTop+event.clientY;
	    }

	    function docclick(c) {
			    var od = document.getElementById("divInfoA");
			    if (od) {
			        if (od.style.visibility == "visible")
			            od.style.visibility = "hidden";
			    }
	    }

	function gotoprofzone() {
		var tdmtab = document.getElementById("tm2");
		if (tdmtab) {
			ottabmain.TabDown(tdmtab,2,2);
		}

		var tdtab = document.getElementById("tp4");
		if (tdtab) {
			ottab2.TabDown(tdtab,4,4);
		}
	}            

        function confirmarPeticion()
        {
            var param;
            var direccionIni = document.getElementById("direccionIni").value
            var direccion = document.getElementById("direccion").value
            //direccion = direccion.replace(/\n/g,"vbCrLf");
            if (direccion.length==0)
            {
                if (getLng()=="POR")
                {
                    txtVacio = "O campo morada não pode estar vazio.";
                }
                else
                {
                    txtVacio = "El campo de dirección no puede estar vacío.";
                }
                alert(txtVacio);
            }
            else
            {
                if (direccionIni==direccion)
                {
                    param = "?TYP=4&DIR="+encodeURIComponent(direccion)+"&MOD=0";
                }
                else
                {
                    param = "?TYP=4&DIR="+encodeURIComponent(direccion)+"&MOD=1";                  
                }
                var iaxMediaUsr  = new iajax(aurlM);
			    iaxMediaUsr.call(Refrescar,param);           
            }
        }

        function PeticionCDD()
        {
            var direccion = document.getElementById("direccion");
            if (direccion)
            {
                //direccion = direccion.value.replace(/\n/g,"vbCrLf");
                direccion = direccion.value
            }
            else
            {
                direccion = "";
            }        
            var txtCDD = document.getElementById("txtCDD").value
            //txtCDD = txtCDD.replace(/\n/g,"vbCrLf");
            var param ="?TYP=5&OBS="+encodeURIComponent(txtCDD)+"&DIR="+encodeURIComponent(direccion);
            var iaxMediaUsr  = new iajax(aurlM);
			iaxMediaUsr.call(Refrescar,param); 
        }

        function verCDD()
        {
            var CDD = document.getElementById('CDD');
            if (CDD.style.display == 'none')
            {
                for (i=0; ele=document.getElementsByTagName('div')[i]; i++)
                {
                    if (ele.name)
                    {
                        if (ele.name.indexOf("div_e") > -1 || ele.name.indexOf("div_c") > -1)
                        {
                            ele.style.display = 'none';
                        }
                     }
                }
                CDD.style.display = 'inline';
            }
            else
            {
                CDD.style.display = 'none';
            }
        }

        function seleccionarArticulo(IdArticulo)
        {
            var direccion = document.getElementById("direccion");
            if (direccion)
            {
                //direccion = direccion.value.replace(/\n/g,"vbCrLf");
                direccion = direccion.value
            }
            else
            {
                direccion = "";
            }
            var qtt = "1";
            var param = "?TYP=2&ART="+IdArticulo+"&QTT="+qtt+"&DIR="+encodeURIComponent(direccion);
            var iaxMediaUsr  = new iajax(aurlM);
			iaxMediaUsr.call(Refrescar,param);
        }
        
        function eliminarArticulo(IdArticulo)
        {
            var direccion = document.getElementById("direccion");
            if (direccion)
            {
                //direccion = direccion.value.replace(/\n/g,"vbCrLf");
                direccion = direccion.value
            }
            else
            {
                direccion = "";
            }        
            var param = "?TYP=3&ART="+IdArticulo+"&DIR="+encodeURIComponent(direccion);
            var iaxMediaUsr  = new iajax(aurlM);
			iaxMediaUsr.call(Refrescar,param);
        }        

		function verarticulosAJX(ide,mode,slng) {
			var param = "?TYP=1&IDE="+ide+"&MODE="+mode+"&SLNG="+slng+"&LNG="+getLng();
			idesp=ide;
			curdiv = "_e" + ide;
			MostrarOcultar1();
            var iaxMedia  = new iajax(aurlM);
			iaxMedia.call(CBVerArticulos,param);
		}
		
		function verarticulosCatAJX(ide) {
		    var param = "?TYP=6&IDE="+ide;
			idesp=ide;
			curdiv = "_c" + ide;
			MostrarOcultar();
            var iaxMedia  = new iajax(aurlM);
			iaxMedia.call(CBVerArticulos,param);	
		}		
		
		function MostrarOcultar1(){
			var obj;
			obj = document.getElementById('div'+curdiv);
			if (obj) {
				if (obj.style.display == "inline") {
					obj.style.display = "none";
				} else {
					obj.style.display = "inline";
					cargando(curdiv);
				}
			}
		}
		
		function cargando(div) {
			var str = "<table cellspacing= cellpadding=0><tr><td><img src=images/cargando.gif></td><td><font class=slink>&nbsp;cargando ...</font></td></tr></table>"
			var obj = obj = document.getElementById('div'+div);
			document.getElementById('div'+div).innerHTML = str;
		}
	
	    function Refrescar(pstr) {
            var obj = obj = document.getElementById('ControlUsuario');
			if (obj) {
			    if (pstr.length==1)
			    {
			        if (pstr == "1")
			        {
			            if (getLng() == "POR")
			            {
			                txtMax = "Você só pode fazer 4 pedidos ao mesmo tempo. Pedimos desculpas por qualquer inconveniente.";
			            }
			            else
			            {
			                txtMax = "Solamente se pueden realizar 4 peticiones al mismo tiempo. Disculpe las molestias.";
			            }
			            alert(txtMax);
			        }
			        else
			        {
			            if (pstr == "3")
			            {
			                if (getLng() == "POR")
			                {
			                    txtCaducado = "A sessão expirou. Por favor, volte a registar";
			                }
			                else
			                {
			                    txtCaducado = "La sesión ha caducado. Por favor vuelva a registrarse";
			                }
			                alert(txtCaducado);
			                gotoprofzone();
			            }
			            else
			            {
			                if (getLng() == "POR")
			                {
			                    txtSolicitado = "O item selecionado já está solicitada";
			                }
			                else
			                {
			                    txtSolicitado = "El artículo seleccionado ya está solicitado.";
			                }
			                alert(txtSolicitado);
			            }
			        }
			    }
			    else
			    {
				    obj.innerHTML = pstr;
				}
			}
        }	
		
		function artFormat(pid) {
			var carrow;
			var ntabs=2;

			carrow ="<table border=0 cellspacing=0 cellpadding=0 style='width:100%'>";
			carrow += "<tr valign='top'><td class='tabprograma2' style='width:50%' >&nbsp;</td><td style='width:1%'><img src='images/barrow.gif'></td><td class='tabprograma2' style='width:49%'>&nbsp;</td></tr>";
			carrow += "</table>";

			var utd;
			var btd;

			for(t=0;t<ntabs;t++) {
				utd= document.getElementById("atbbnt"+t);
				btd= document.getElementById("atbbnt" + t + "b");

				btd.className = "tabprograma2";
				btd.innerHTML = "&nbsp;";
				
				
				utd.className = "tabprogramaunsel";
			}

			utd= document.getElementById("atbbnt"+pid);
			btd= document.getElementById("atbbnt" + pid + "b");

			if (utd && btd) {
				btd.className = "";
				btd.innerHTML = carrow;
				
				utd.className = "tabprograma";
			}

            var obj = document.getElementById("divEspecArt");
            var obj2 = document.getElementById("divCatArt");

            if (pid == 0) {
                obj.style.display = "inline";
                obj2.style.display = "none";
            } else {
                obj2.style.display = "inline";
                obj.style.display = "none";
            }

		}
		
        function CBVerArticulos(pstr) {
            /*if (pstr=="caduca")
            {
                window.location="Mediateca/msgsession.htm";
            }*/
            var obj = obj = document.getElementById('div'+curdiv);
			if (obj) {
				obj.innerHTML = pstr;
			}
			if (modo==0)
			{
			    var param = "?TYP=8"
			    var iaxMedia2  = new iajax(aurlM);
	            iaxMedia2.call(CBVerAyuda,param);
			}					
        }

        function CBVerAyuda(pstr) {
            var obj = obj = document.getElementById('ControlUsuario');
			if (obj && pstr.length>0) {
				obj.innerHTML = pstr;
			}			
        }

		function infoArticulo(idi, usuario) {
	        if (usuario == 2)
            {
                modo = 0;
            }
            else
            {
                modo = 1;
            }	

			xinfM = mxM;
			yinfM = myM;
			var od = document.getElementById("divInfoA");
			if (od && modo == 1) {
				var str = "<table bgcolor=white cellspacing=0 cellpadding=1 style='BORDER-RIGHT: gainsboro 1px solid; BORDER-TOP: gainsboro 1px solid; BORDER-LEFT: gainsboro 1px solid; BORDER-BOTTOM: gainsboro 1px solid'><tr><td><img src=images/cargando.gif></td><td><font class=slink>&nbsp;Cargando ...</font></td></tr></table>"
				od.innerHTML = str;
            }      
			var param = "?TYP=7&ART="+idi+"&MOD="+modo;
			
            var iaxMedia  = new iajax(aurlM);

			    iaxMedia.call(CBVerInfoArticulo,param);

		}

		function CBVerInfoArticulo(pstr) {
			
			if (modo == 0)
			{
			    document.getElementById("ControlUsuario").innerHTML = pstr;
			}
			else
			{
			    var od = document.getElementById("divInfoA");
			    if (od) {
				    od.innerHTML = pstr;
				    od.style.top = yinfM;
				    od.style.left =xinfM;
				    od.style.width =300;
				    od.style.visibility = "visible";				    
                }
            }        
			    
		}        		
		

	function ayudaMediateca() {
		var w,h;
		var t,l;

		h = screen.availHeight;
		w = screen.availWidth;
		t = (h/2) - 350;
		l = (w/2) - 400;

		window.open ("Mediateca/ayudaMediateca" + getLng() + ".htm",120,"top=" + t + ",left=" + l + ",height=700,width=800,status=yes,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");		
	}

	function condiciones(){
		var w,h;
		var t,l;

		h = screen.availHeight;
		w = screen.availWidth;
		t = (h/2) - 300;
		l = (w/2) - 400;
		window.open ("Mediateca/condiciones" + getLng() + ".htm",120,"top=" + t + ",left=" + l + ",height=600,width=800,status=yes,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");
	}
	function guardaDescarga(IdUser)
	{
	    //alert(IdUser)
		var param = "?TYP=9&USR="+IdUser;
        var iaxMedia  = new iajax(aurlM);
		iaxMedia.call(auxiliar,param);	    
	}
	function auxiliar(pstr)
	{
	    //alert("OK")
	}
