// JavaScript Document
var gPopUpShowed=0;

function jsBrowseCat(id,tipo)
	{
		//var Tienda = document.getElementById('lyrTienda');
		//	if (Tienda.style.display != "none" )
		//		$('#lyrTienda').Puff(500);
				
		//jsNavigate('directory.php?Cat='+id,'')
		jsAjaxIt('directory.php?Cat='+id+'&Tipo='+tipo,'tdMain','');
		}
		
function jsSearch(txt,tipo,file)
	{
		var Valor = txt.value;
		if (!(Valor)) return false;
		Valor = Valor.replace('<?','');
		Valor = Valor.replace('?>','');
		//jsNavigate('directory.php?srch='+Valor,'');
		jsAjaxIt(file + '?srch='+Valor+'&Tipo='+tipo,'tdMain','');
		}

function jsShowProto(id,Tienda)
	{
		//jsAjaxIt('proto'+id + '.php?Tienda='+Tienda, 'lyrTienda','jsTienda2Show()')
		
		jsTienda2Show(id,Tienda);
	}
	
function jsTienda2Show(id,idTienda)
	{	//alert(idTienda);
		var Tienda = document.getElementById('lyrTienda');
		var Lugar = document.getElementById('tdMain');
			Tienda.style.left = (curLeft(Lugar) - 17) + "px";
		    Tienda.style.top = curTop(Lugar) + "px";
			Tienda.style.width="600px";
			Tienda.style.height="400px";
			Tienda.style.zIndex=10000;
			//Tienda.style.display=absolute;
		
		jsAjaxIt('proto'+id + '.php?tienda='+idTienda, 'lyrTienda','$("#lyrTienda").slideDown();');
		}
		
function jsCloseTiendaShowed()
	{
		$('#lyrTienda').slideUp();return false;
	}
	
function jsEditDir(id)
	{
		var Tienda = document.getElementById('lyrEdit');
		var Lugar = document.getElementById('tdMain');
			Tienda.style.left = curLeft(Lugar)+20 + "px";
		    Tienda.style.top = curTop(Lugar) +20 + "px";
			Tienda.style.width="700px";
			Tienda.style.height="450px";
			Tienda.style.zIndex=1000;
			jsAjaxIt('tbEditDir.php?id='+id,'lyrEdit','jsShowEditDir()');
			//Tienda.style.display=absolute;		
	}
	
function jsShowEditDir()
	{
		$('#lyrEdit').Grow(300);return false;
		}
		
function jsCloseEdit()
	{
		$('#lyrEdit').Puff(300);return false;
	}
	
function jsGetFieldsValues(frm)
	{
		var Ret = "";
		var Campo;
		var Valor;
		var Caracter= "";
		for(var e = 0; e < frm.length; e++){
			//if(frm.elements[e].tagName == "SELECT") 
			//{
			//	frm.elements[e].style.visibility="hidden";
			//}
			if (frm.elements[e].jsField)
				{
					Campo=frm.elements[e].jsField;
					if (frm.elements[e].type=="checkbox")
						Valor= (frm.elements[e].checked?1:0);
					else
						Valor =frm.elements[e].value;
					//Dependiendo el tipo de dato
					switch(frm.elements[e].jsTipo)
						{
							case "String":
								Caracter = "'";
								break;
							default:
								Caracter = "";
						}
					
					Ret += Campo + "=" + Caracter + Valor + Caracter + "&";
				
				}
		}
		alert(Ret);
}

function jsShowBigLogo(imgsrc)
	{	
		var Obj = document.getElementById('lyrLogo');
		if (imgsrc)
			{
				var Img = document.getElementById('imgBigLogo');
				/*if (Img.src != imgsrc.src)
					{*/
					Obj.style.left = curLeft(imgsrc.offsetParent) + 'px';
					Obj.style.top = curTop(imgsrc.offsetParent) + 30 + 'px';
					Obj.style.width= '100px';
					Obj.style.height= '100px';
					Obj.style.zIndex=10000;
					Img.src = imgsrc.src;
					Obj.style.display='block';
					//}
			}
		else
			Obj.style.display='none';
	}

function jsShowBanners(idCat,flt)
	{
		if ((!(idCat == "null")))
			{
				jsAjaxIt('rbanners.php?Cat='+idCat, 'lyrBnrRight','');
			}
		else if ((!(flt == "null")))
			{
				jsAjaxIt('rbanners.php?Flt='+flt, 'lyrBnrRight','');
			}
		}
		
function RetImg(obj)
	{
		var Ret;
		Ret = "<img width='150' height='100' src='" + obj.src.replace('small/','') + "' />";
		showToolTip(event,Ret);
		//document.getElementById('imgLogoChico').src=obj.src.replace('small/','');
		//document.getElementById('lyrMini').style.display="block";
		return false;
	}

// para que aparesca y desaparesca en medio la imagen
function RetImgOnCenter(obj)
	{
		var Ret;
		Ret= document.getElementById("imgPrincipal").src = obj.src.replace('small/','');
		return false;
	}

function hideToolTipCenter()
	{
		document.getElementById("imgPrincipal").src = "images/bkgShortCut2_.jpg";
	}
// para que aparesca y desaparesca en medio la imagen

function jsShowPopUp(url,w,h)
	{
		//if (gPopUpShowed==1) return false;
		var L = (screen.width * 0.25) + (w * 0.25);
		var T = (screen.height * 0.25) + (h * 0.25);
		wdw = window.open(url,null,'left='+L+',top=' + T + ',width=' + w + ',height=' + h);

	}

function jsShowPic(Img,Show)
	{
		var Url = '';
		var Lyr = document.getElementById('lyrSwapImg');
		var Big = document.getElementById('imgSwap');
		var Pos = document.getElementById('tdBigFotoPlace');
		
		var IdDirectorio = document.getElementById('hIdDirectorio').value;
		var IdImagen = Img.datafield ;
		
		/*if (Show)
			{
				Lyr.style.left = (curLeft(Pos)+100) + 'px' ;
				Lyr.style.top = (curTop(Pos)-50) + 'px';
				//Big.src = Img.src;
				Lyr.style.display="block";
			}
		else
			{
				Lyr.style.display="none";
			}
			*/
		window.status=curTop(Pos);

		Url = 'frmImagen.php?IdDirectorio=' + IdDirectorio + '&IdImagen=' + IdImagen
		jsAjaxIt(Url,'lyrImgBigger',"");
	}

function RemoveBad(strTemp) 
	{ 
		strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,""); 
		return strTemp; 
	}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Cuenta de correo invalida")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Cuenta de correo inválida")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Cuenta de correo inválida")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Cuenta de correo inválida")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Cuenta de correo inválida")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Cuenta de correo inválida")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Cuenta de correo inválida")
		    return false
		 }

 		 return true					
	}
	function jsSendMailautos(nombre,correo,telefono,comentarios)
	{
		if (!nombre.value)
			{
				alert("El nombre es necesario");
				nombre.focus();
				return false;
				}
		if (!telefono.value)
			{
				alert("El telefono es necesario");
				telefono.focus();
				return false;
				}
		if ( (correo.value))
			{
				if (!echeck(correo.value))
					{
						correo.focus();
						return false;
					}
				}
				if (!comentarios.value)
			{
				alert("El comentario es  necesario");
				comentarios.focus();
				return false;
				}
		var Url = "sndmail_auto.php?";
		Url += "nombre=" + RemoveBad(nombre.value) + "&correo=" + RemoveBad(correo.value) + "&comentarios=" + RemoveBad(comentarios.value) + "&telefono=" + RemoveBad(telefono.value);
		jsAjaxIt(Url,'tdMain',"jsEvalSendEmailAutos()");
		}

function jsSendMail(nombre,correo,telefono,comentarios)
	{
		if (!nombre.value)
			{
				alert("El nombre es necesario");
				nombre.focus();
				return false;
				}
		if (!telefono.value)
			{
				alert("El telefono es necesario");
				telefono.focus();
				return false;
				}
		if ( (correo.value))
			{
				if (!echeck(correo.value))
					{
						correo.focus();
						return false;
					}
				}
				if (!comentarios.value)
			{
				alert("El comentario es  necesario");
				comentarios.focus();
				return false;
				}
		var Url = "sndmail.php?";
		Url += "nombre=" + RemoveBad(nombre.value) + "&correo=" + RemoveBad(correo.value) + "&comentarios=" + RemoveBad(comentarios.value) + "&telefono=" + RemoveBad(telefono.value);
		jsAjaxIt(Url,'tdMain',"jsEvalSendEmail()");
		}

function jsSendNextel(nombre,correo,telefono,comentarios,email)
	{		
		if (!nombre.value)
			{
				alert("El nombre es necesario");
				nombre.focus();
				return false;
				}
		if (!telefono.value)
			{
				alert("El telefono es necesario");
				telefono.focus();
				return false;
				}
		if ( (correo.value))
			{
				if (!echeck(correo.value))
					{
						correo.focus();
						return false;
					}
				}
				if (!comentarios.value)
			{
				alert("El comentario es  necesario");
				comentarios.focus();
				return false;
				}
							
		var Url = "sndnextel.php?";
		Url += "nombre=" + RemoveBad(nombre.value) + "&correo=" + RemoveBad(correo.value) + "&comentarios=" + RemoveBad(comentarios.value) + "&telefono=" + RemoveBad(telefono.value) + "&email=" + RemoveBad(email.value);

		jsAjaxIt(Url,'tdMain',"jsEvalSendEmail()");
		}

	function jsSendDirectorio(nombre,correo,telefono,comentarios,email)
	{		
		if (!nombre.value)
			{
				alert("El nombre es necesario");
				nombre.focus();
				return false;
				}
		if (!telefono.value)
			{
				alert("El telefono es necesario");
				telefono.focus();
				return false;
				}
		if ( (correo.value))
			{
				if (!echeck(correo.value))
					{
						correo.focus();
						return false;
					}
				}
				if (!comentarios.value)
			{
				alert("El comentario es  necesario");
				comentarios.focus();
				return false;
				}
							
		var Url = "sndmaildirec.php?";
		Url += "nombre=" + RemoveBad(nombre.value) + "&correo=" + RemoveBad(correo.value) + "&comentarios=" + RemoveBad(comentarios.value) + "&telefono=" + RemoveBad(telefono.value) + "&email=" + RemoveBad(email.value);

		jsAjaxIt(Url,'tdMain',"jsEvalSendEmail()");
		}
		
	function jsEvalSendEmail()
		{
			alert('Mensaje Enviado');
			jsAjaxIt('center.php','tdMain','');
		}
	
	function jsEvalSendEmailAutos()
		{
			alert('Mensaje Enviado');
			jsAjaxIt('centerautos.php','tdMain','');
		}
		
	function jsContacto()
	{								
		jsAjaxIt('contacto.php','tdMain','');
		}
		
	function jsBuro()
		{			
			jsAjaxIt('buro.php','tdMain','');
			}
		function jsMp()
	{								
		jsAjaxIt('mapa1.php','tdMain','');
		}
	
	function jsLink()
	{								
		jsAjaxIt('link.php','tdMain','');
		}
	function jsCatego()
	{								
		jsAjaxIt('frmCategorias.php','tdMain','');
		}
	function jsCenter()
	{								
		jsAjaxIt('center.php','tdMain','');
		}
	function jsCenterAutos()
	{								
		jsAjaxIt('centerautos.php','tdMain','');
		}
		
		function jsPass(usuario,password)
			{								
				if (!usuario.value)
					{
						alert("El usuario es necesario");
						nombre.focus();
						return false;
						}
				if (!password.value)
					{
						alert("El password es necesario");
						password.focus();
						return false;
						}
				var Url = "Ejecuciones.php?Funcion=ValidaUsuario";
				Url += "&Valor1=" + RemoveBad(usuario.value) + "&Valor2=" + RemoveBad(password.value);
				//alert(Url)
				jsAjaxIt(Url,'lyrBack',"jsGetResultado()");										
			}			
		
		function jsGetResultado()
			{			
				var resultado = document.getElementById("lyrBack").innerHTML;				
				
				if (parseFloat(resultado)>0)
					{
					jsAjaxIt("buro1.php",'tdMain',"");															
					}
				else
					alert("No existe el usuario");
					
				document.getElementById("lyrBack").innerHTML="";				
			}
			
	function jsSetPanel(usuario,password)
		{								
			if (!usuario.value)
				{
					alert("El usuario es necesario");
					nombre.focus();
					return false;
					}
			if (!password.value)
				{
					alert("El password es necesario");
					password.focus();
					return false;
					}
			/*var Url = "Ejecuciones.php?Funcion=ValidaUsuario";
			Url += "&Valor1=" + RemoveBad(usuario.value) + "&Valor2=" + RemoveBad(password.value);

			jsAjaxIt(Url,'lyrBack',"jsResCPanel()");										*/
			
			$.ajax({
					   async:true,
					   type: "POST",
					   dataType: "html",
					   contentType: "application/x-www-form-urlencoded",
					   url:"Ejecuciones.php",
					   data: "Funcion=Usuario&Valor1=" + RemoveBad(usuario.value) + "&Valor2=" + RemoveBad(password.value), 
					   success:jsResCPanel,
					   timeout:4000,
					   error:Problemas
					}); 	
		}			
	
	function jsResCPanel(resultado)
		{
			//var resultado = document.getElementById("lyrBack").innerHTML;
					
			if (parseFloat(resultado)>0)
				document.location.href = "http://www.abastosguadalajara.com/Misitio.php?IdDirectorio=" + resultado;
			else
				alert("No existe el usuario");				
		}			
			
			
	function jsPestanaInicio(tienda)
	{								
		jsAjaxIt('proto1.php?tienda='+tienda,'lyrTienda','');
		}
		
	/*function jsPestanaFotos(tienda)
	{								
		jsAjaxIt('fotos.php?tienda='+tienda,'lyrTienda','');
		}*/
	function jsPestanaNosotros(tienda)
	{								
		jsAjaxIt('nosotros.php?tienda='+tienda,'lyrTienda','');
		}
		
	function jsPestanaNextel(tienda)
	{								
		jsAjaxIt('nextel.php?tienda='+tienda,'lyrTienda','');
		}
		
	function jsPestanaMsg(tienda)
	{								
		jsAjaxIt('mensajes.php?tienda='+tienda,'lyrTienda','');
		}
		
	function jsPestanaCorreo(tienda)
		{
			jsAjaxIt('correo.php?tienda='+tienda,'lyrTienda','');
		}
	function jsPestanaLinks(tienda)
	{								
		jsAjaxIt('p_links.php?tienda='+tienda,'lyrTienda','');
		}
	
	function jsPestanasAlVuelo(tienda,Num)
		{
			jsAjaxIt('frmImgTexto' + Num + '.php?tienda='+tienda,'lyrTienda','');
		}
	
	/* Offset position from top of the screen */
	function curTop(obj){
		toreturn = 0;
		objOri=obj;
		while(obj){
			toreturn += obj.offsetTop;
			obj = obj.offsetParent;
		}
		
		return toreturn;
	}
	function curLeft(obj){
		toreturn = 0;
		objOri=obj;
		while(obj){
			toreturn += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	
		return toreturn;
	}

	function ShowPantallaEdicion(IdDirectorio)
		{
			var url = 'frmEdicionDirectorio.php?hIdDirectorio=' + IdDirectorio;
			var opc = "resizable=0,scrollbars=yes,location=0,directories=0,menubar=0,status=0,toolbar=0,left=" + (screen.width * 0.25) + "px,top=" + (screen.height * 0.08) + "px,width=750px,height=640px";
						
			window.open(url,'',opc);	
		}
	
	function SetDatosDirectorio()
		{
			if(!confirm("Actualizar los datos?")) return
			
			var Nombre=$("#txtNombre").attr("value");			
			var Contacto=$("#txtContacto").attr("value");
			var Categoria=document.getElementById("cboCategoria").value;
			var Direccion=$("#txtDireccion").attr("value");
			var Telefono=document.getElementById("txtTel").value;
			var Fax=document.getElementById("txtFax").value;
			var Nextel=document.getElementById("txtNextel").value;
			var Celular=document.getElementById("txtCelular").value;
			var Email=$("#txtEmail").attr("value");
			var Web=document.getElementById("txtWeb").value;
			var Id=$("#hIdDirectorio").attr("value");
			var NumN=$("#txtNumeroNextel").attr("value");
			var Usuario=document.getElementById("txtUsuario").value;
			var Pass=document.getElementById("txtPass").value;
			var Slogan=document.getElementById("txtSlogan").value;
			var Kw=document.getElementById("txtKw").value;
			
			var Pestana1=document.getElementById("txtPestana1").value;
			var Pestana2=document.getElementById("txtPestana2").value;
			var Pestana3=document.getElementById("txtPestana3").value;
			var Pestana4=document.getElementById("txtPestana4").value;
			
			Direccion = Direccion.replace('#',"Num ");
			
			$.ajax({
					   async:false,
					   type: "POST",
					   dataType: "html",
					   contentType: "application/x-www-form-urlencoded",
					   url:"Ejecuciones.php",
					   data: "Funcion=Guardar&Nombre=" + Nombre + "&Contacto=" + Contacto + "&Categoria=" + Categoria + "&Direccion=" + Direccion + "&Telefono=" + Telefono + "&Fax=" + Fax + "&Nextel=" + Nextel + "&Celular=" + Celular + "&Email=" + Email + "&Web=" + Web + "&IdDirectorio=" + Id + "&NumeroNextel=" + NumN + "&Pestana1=" + Pestana1 + "&Pestana2=" + Pestana2 + "&Pestana3=" + Pestana3 + "&Pestana4=" + Pestana4 + "&Slogan=" + Slogan + "&Usuario=" + Usuario + "&Pass=" + Pass + "&Kw=" + Kw, 
					   success:GuardadoValidate,
					   timeout:4000,
					   error:Problemas
					}); 	
		}
	
	function GuardadoValidate(Resultado)
		{
			if(Resultado=="1")
				{
					alert("Registro Actualizado");
					window.close();
				}
			else
				alert("Error al Actualizar los datos");			
		}
	
	function Problemas(error)
		{
			alert("Error en el Query");	
		}
	
	function ShowSetImagen(Id)
		{
			var opt = 'height=600,width=900,left=0,top=0,resizable=no,scrollbars=yes,toolbar=no,status=no,location=no,directories=no,toolbar=no';
		
			window.open('frmCapturaImagen.php?hIdDirectorio=' + Id,'',opt);		
		}
		
		
		function jsLink_auto()
	{								
		jsAjaxIt('link_auto.php','tdMain','');
		}
		
				
	function jsContacto_auto()
	{								
		jsAjaxIt('contacto_auto.php','tdMain','');
		}
	
	function AtrasaAdelanta(Accion)
		{
			switch(Accion)
				{
					case "Adelante":																		
						break;
					
					case "Atras":						
						break;
						
				}			
		}
	
	function jsGuardarBuro()
		{
			if(!confirm("Desea publicar la nota?")) return
						
			var Nombre = document.getElementById("txtNombre").value;
			var Comentario = document.getElementById("txtComentario").value;
			var Publica = document.getElementById("txtPublica").value;
			
			if (Nombre == "")
				{
					alert("Escriba un nombre de Cliente");
					document.getElementById("txtNombre").focus();
					return false;
					}
			if (Comentario == "")
				{
					alert("El comentario se encuentra vacio");
					document.getElementById("txtComentario").focus();
					return false;
					}
				
			if (Publica== "")
				{
					alert("Escriba quien publica la nota");
					document.getElementById("txtPublica").focus();
					return false;
					}
					
			var Url = "Ejecuciones.php?Funcion=SetComentarioBuro";
			Url += "&Nombre=" + RemoveBad(Nombre) + "&Comentario=" + RemoveBad(Comentario) + "&Publica=" + RemoveBad(Publica);
			//alert(Url)
			jsAjaxIt(Url,'lyrBack',"jsGetResultBuro()");						
		}
	
	function jsGetResultBuro()
		{
			var resultado = document.getElementById("lyrBack").innerHTML;				
				
			if (parseFloat(resultado)>0)
				{
				jsAjaxIt("buro1.php",'tdMain',"");															
				}
			else
				alert("Error al guardar el comentario en buro");
					
			document.getElementById("lyrBack").innerHTML="";				
		}