var usuario='';
var cate=0;
var marc=0;
var kis=1;
var lista=1;
var l=5;
var sumar=true;
var inicial=false;
function da(ok,ko)
{
	var input=document.getElementById(ok);	
	if(ko==true)
		{
			return input.value;	
		}
		else
		{
			input.value=ko;	
		}
	
}
function es(ok)
{
var detalles=document.getElementById('mensaje');		
detalles.innerHTML=ok+':<br> cate:' + cate +'  <br>marc: ' +marc + ' <br> kis: ' +kis+'<br> lista: ' +lista +'<br> l: ' + l + '<br>  sumar: ' +sumar + '<br>  inicial: ' + inicial;
//alert();
	
}

function posa(ok)
{
var l=1;
for(l=1;l<=5;l++)
{
 var lis=document.getElementById(l);	
 lis.setAttribute('class','pestania')
	
}
 var lis=document.getElementById(ok);	
 lis.setAttribute('class','pest')


}
function defe()
{
 lista=1;
 l=5;
 kis=1;

}
function fede1()
{
var d=1;	
 while(d<6)
  {
   var lis=document.getElementById(d);	
    lis.innerHTML=d;
		  lis.setAttribute('alt',d);	
   d++;
  }
  posa(1);
}  
  
 function fede()
{
 
ocultando(false,'1');
ocultando(false,'2');
ocultando(false,'3');
ocultando(false,'4');
ocultando(false,'5');
ocultando(false,'mass');
ocultando(true,'atras');

}


function cargarlista()
{
var aleatorio=Math.random();
	conexion1=crearXMLHttpRequest();
	conexion1.onreadystatechange = listando;
	conexion1.open('GET',"listaraaa.php?cate="+cate+"&marc="+marc+"&ok="+lista+'&aleatorio='+aleatorio+"&stock="+stock, true);
    conexion1.send(null);
}
function listando()
{  var detalles=document.getElementById('mensaje');		
	
   	if(conexion1.readyState == 4)
	  {
	 
	 
	  var f=0;
	 if(sumar==true)
   		{
		lista++; 
		f=conexion1.responseText;
				  ocultando(false,'atras');

	   }
 	else
		{
			lista--; l=l-10; f=45;

			if(l<=0)
			{
			  ocultando(true,'atras');
	    	}
				ocultando(false,'1');
				ocultando(false,'2');
				ocultando(false,'3');
				ocultando(false,'4');
				ocultando(false,'5');
				ocultando(false,'mass');
	     	}
	   var inc=1;
	   var d=0;

	  while(d<f)
		{
		   d=d+9;
		  var lis=document.getElementById(inc);	
		  l++;
		  inc++;
		  lis.innerHTML=l;
		  lis.setAttribute('alt',l);	
		}
	  while(inc<=5)
		 {	
		  ocultando(true,inc);
		  inc++;
  		  l++;

		  ocultando(true,'mass');
		  
		 }
	 seleccionados('1');
		detalles.innerHTML='';
    } 
	else
	{
		detalles.innerHTML='Cargando lista...'
		
	}
	
	
	
		
			
}


var conexion1;
function seleccionados(ok) 
{
		var vale=true;
 var ref=document.getElementById(ok);

  var valor=ref.value;
	var iden=ref.getAttribute('id');
	

	if(iden=='seleccategoria' || iden=='selecmarca' )
	{inicial=true;
		defe();
		fede();

	if(iden=='seleccategoria')
		{ 
	if(valor!='null'){cate=valor;}else{ cate=0;   fede1();  }
		}
	else
		{
	    if(valor!='null'){marc=valor;}else{marc=0;   fede1();  }	
	  }
	
	}
	else
	{

		if(!(ok=='buscar' || ok=='nobuscar')){posa(ok);} 

		inicial=false;
		kis=ref.getAttribute('alt');
	}
	

if(marc==0&&cate==0)
  {
	  inicial=false;

  }
 if((ok=='buscar' || ok=='nobuscar')&& da('textito',true)!='') 
 	{    ocultando(false,'nobuscar');
		 ocultando(false,'prodbusc');

		da('seleccategoria','null')
		da('selecmarca','null')
		if(ok=='nobuscar')
		  {	
		  	da('textito','');

			ocultando(true,'prodbusc');
		    ocultando(true,'nobuscar');
		  }
 		  
		
		  
		  		da('prodbusc','Productos que contiene la palabra '+da('textito',true));
		  
	 inicial=true; defe();fede();cate=0; marc=0;	
 	}

   
	if(vale==true)
	{
    conexion1=crearXMLHttpRequest();			
    conexion1.onreadystatechange = procesardatos;

conexion1.open('GET',"pararegistrar.php?cate="+cate+"&marc="+marc+"&ok="+kis+"&texto="+da('textito',true)+"&stock="+stock+"&inicial="+inicial, true);
    conexion1.send(null);
	}
  
}
function procesardatos()
{
  var detalles=document.getElementById('mensaje');		
  if(conexion1.readyState == 4)
  {	
	var xmlhay = conexion1.responseXML;
		var incc=xmlhay.getElementsByTagName('inc');
		
if(incc[0].firstChild.nodeValue!='false')
	{
		
	var f=0;	
		f=incc[0].firstChild.nodeValue;
		
		 var inc=1;
	   var d=0;
	  while(d<=f)
		{
		   d=d+9;
        try
		{

           var lis=document.getElementById(inc);	
		   lis.innerHTML=inc;
		   lis.setAttribute('alt',inc);	
		 }
		  catch(e) { } 
		  inc++;

	
 		}inicial=true;
		defe();
		fede();


	  while(inc-1<5)
		 {
			 
	 
		 ocultando(true,'mass');
		  ocultando(true,inc);
		  inc++;
		
	
		 }
		 if(f<=45)
		 {
		 	ocultando(true,'mass');

		 }
		 else
		 {
		 	ocultando(false,'mass');
 
		 }	
	defe();

seleccionados('1');
		
 		}  
	
	
	
	
	
	
	
	var x=0;
	var y=0;     
	//para eliminar las etiqueas <tr>
    var dereIzq=document.getElementById('dereIzq');
	var divd=dereIzq.getElementsByTagName('div');
	var cantidad=divd.length;
for(f=1;f<cantidad;f++)
		{
		var referencia=divd[1].parentNode;
	 	referencia.removeChild(divd[1]);
	
		}
	//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_

	var mar=xmlhay.getElementsByTagName('mar');
	
	
	
	for(y=0;y<mar[0].firstChild.nodeValue;y++)
	{
		
	 var divi=xmlhay.getElementsByTagName('detalle'+y);
	 var prod='';
	 var evento=false;
	   if(divi[1].firstChild.nodeValue.length >=51)
	       {
			  prod=divi[1].firstChild.nodeValue.substring(0,51) +'...';
			    evento=true;
		   }  
			 else
		   {
			evento=false;
		    prod=divi[1].firstChild.nodeValue;
		   } 
	 
	    var div=document.createElement('div');	
			div.setAttribute('id',divi[0].firstChild.nodeValue);
			div.setAttribute('class','producto');
			  

				  var imagen= document.createElement('img');			   
					imagen.setAttribute('src','fotoprod/'+divi[9].firstChild.nodeValue);
				   var input1=document.createElement('input');
		   			input1.setAttribute('type','button');
		   			input1.setAttribute('value','comprar');					
		   			input1.setAttribute('class','boton');
				   var input2=document.createElement('input');				   
		   			input2.setAttribute('type','button');				   
		   			input2.setAttribute('value','detalles');
		   			input2.setAttribute('class','boton');
				
					input1.setAttribute('onclick','mandar('+ divi[0].firstChild.nodeValue+ ')');	
		   			input2.setAttribute('onclick','detalle('+ divi[0].firstChild.nodeValue+ ')');
										
				   var texto=document.createTextNode('S/.'+divi[5].firstChild.nodeValue);
				   var precio=document.createElement('precio')	
				  
				  var texto2=document.createTextNode(prod);
				   var detalle=document.createElement('span')		
				    detalle.appendChild(texto2);
				   	detalle.setAttribute('class','nmostrar');				
					detalle.setAttribute('id',divi[0].firstChild.nodeValue+'desc');				
					if(evento==true)
					  {
						detalle.setAttribute('onmouseout',"ver('"+divi[0].firstChild.nodeValue+"desc','"+divi[0].firstChild.nodeValue+"',true)");				   
						detalle.setAttribute('onmouseover',"ver('"+divi[0].firstChild.nodeValue+"desc','"+divi[0].firstChild.nodeValue+"',false)");				   
        			  }
					
				
		   			precio.setAttribute('class','precio');				   
				    precio.appendChild(texto);
					 div.appendChild(detalle);
					 div.appendChild(imagen);
				    div.appendChild(input1);
				    div.appendChild(input2); 
				   div.appendChild(precio); 				   
			   
	   
	   
	   dereIzq.appendChild(div);
detalles.innerHTML = '';
	
	
	

    } 
inicial=false;	


  }
  else 
  {
   detalles.innerHTML = 'Cargando producto...';
	  
 	}



}
function texto(id,url,tapar)
{
	ocultando(tapar,'nosotros')
var aleatorio=Math.random();
	
    conexion1=crearXMLHttpRequest();
    conexion1.onreadystatechange = llenardiv;
	conexion1.open('GET',"texto.php?id="+id+'&aleatorio='+aleatorio+'&url='+url, true);
    conexion1.send(null);
	
  
}
function llenardiv()
{
  var detalles = document.getElementById("nosotros");
  if(conexion1.readyState == 4)
  {
    detalles.innerHTML = conexion1.responseText;
  } 
  else 
  {
    detalles.innerHTML = 'Cargando...';
	
  }	
	
}

function autentificar()
{	

var txtContrasena=document.getElementById('txtContrasena');
	var txtUsuario=document.getElementById('txtUsuario');
		if (autentificado!=true)
			{			

				if(txtContrasena.value!='' &&  txtUsuario.value!='')
				{
					conexion1=crearXMLHttpRequest();
					conexion1.onreadystatechange = validar;
					conexion1.open('GET',"uno.php?contra="+txtContrasena.value+"&usuario="+txtUsuario.value, true);
 
					conexion1.send(null);
				}
				else
					{
						alert('Estan vacios los campos de texto');
						txtUsuario.focus();	
					}
			}
			else
			{
				conexion1=crearXMLHttpRequest();
				conexion1.onreadystatechange = validar;
				conexion1.open('GET',"uno.php?autentificado="+true, true);
				conexion1.send(null);
			}
}
var descativado=true;
function validar()
{
  //var subiendo=document.getElementById('f5');		
  
  if(conexion1.readyState == 4)
  {	
  	var xmlhay = conexion1.responseXML;
	
	var existeaca=xmlhay.getElementsByTagName('hay');

	var mensaje=document.getElementById('existe');
		mensaje.innerHTML="";
	if(existeaca[0].firstChild.nodeValue!='false')
	{
	var texto=document.createTextNode('Bienvenido ' + existeaca[0].firstChild.nodeValue);
		   usuario=existeaca[3].firstChild.nodeValue;
		   mensaje.appendChild(texto);
		if(descativado==true)
		{
		var hidPago=document.getElementById('hidPago');	
			hidPago.value=existeaca[1].firstChild.nodeValue;
			ocultando(false,'btnTUPedido');
			descativado=false;
			ocultando(false,'btnCerrar');
		}
	}
	else
	{
		var texto=document.createTextNode('No existe el usuario o la contraseņa esta mal');
		mensaje.appendChild(texto);	
	}
  }
}

//************************************************************************************************
function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on'+nomevento,funcion);
    return true;
  }
  else  
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}

function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}

