// JavaScript Zimmer
function validarbusqueda(form){
	
	if(document.busquedaf.criterio.value==""){
		alert("Ingrese un criterio de Busqueda")
		return false;
		}
	return true;
	}
	
function validarproducto(){
				var elementos = document.formulario.elements.length;
				for(i=0; i<elementos; i++){
	
						if(document.formulario.txtnombre.value=="" || document.formulario.txtprecio.value=="" || document.formulario.txtvalor.value==""){
							if(document.formulario.elements[i].name!="txtreferencia"){
							document.getElementById('msjerror').innerHTML="<span class='error'>Los campos con * son obligatorios</span>";
							
							return false;
							}
						
						
						
						
					}
				}
			
	return true;
	
	}	


	   function cambiaritems(){
		
		document.itemsform.submit();
		
		}
//Agregamos un nuevo producto a la canasta
		function updateProducto(idr,id,color){
			//alert("va a actualizar "+id+document.getElementsByName("produc"+id)[0].value+" asda");
			 var cantidad=document.getElementsByName("produc"+id)[0].value;
		  $.ajax({
			   type: "POST",
			   url: "asycall.php",
			   data: "opt=7&id="+idr+"&idf="+id+"&col="+color+"&can="+cantidad,
			   success: function(msg){
				//alert("prueba");
				
				 document.getElementById('main').innerHTML=msg;	 
			   }
			 });			
			}
			
	  function addToCart(idp){
		  var cantidad=document.carrito.txtcantidad.value;
		  var color=document.carrito.colores.value;

		  var precio=document.carrito.hiddenprecio.value;
		  //valores flete
		  var cflete=document.carrito.hiddenCantFlete.value;
		  var vflete=document.carrito.hiddenprecioFlete.value
		  var descuento=document.carrito.hiddenDescuento.value
	
	  
	  var tipo=idp;
	   if(Number(cantidad)){
		   $.ajax({
			   type: "POST",
			   url: "asycall.php",
			   data: "opt=2&id="+tipo+"&can="+cantidad+"&col="+color+"&precio="+precio+"&cf="+cflete+"&vf="+vflete+"&des="+descuento,
			   success: function(msg){
				alert(msg);
				// document.getElementById('carritoVenta').innerHTML=msg;
			   	 $.ajax({
				   type: "POST",
				   url: "asycall.php",
				   data: "opt=3",
				   success: function(msg){
				
					// document.getElementById('carritoVenta').innerHTML=msg;
					document.getElementById('totalitems').innerHTML="<a href='carrito.php'>("+msg+" items)</a>";
					 
				   }
				 });			 
			   }
			 });
		   document.carrito.txtcantidad.value=1;

	   	}else{
			document.carrito.txtcantidad.value=1;
			
			}

		}
	  function addToCartFinal(idp){
		  var cantidad=document.carrito.txtcantidad.value;
		  var color=document.carrito.colores.value;

		  var precio=document.carrito.hiddenprecio.value;
		  //valores flete
		  var cflete=document.carrito.hiddenCantFlete.value;
		  var vflete=document.carrito.hiddenprecioFlete.value
		  var descuento=document.carrito.hiddenDescuento.value
	
	  
	  var tipo=idp;
	   if(Number(cantidad)){
		   $.ajax({
			   type: "POST",
			   url: "asycall.php",
			   data: "opt=2&id="+tipo+"&can="+cantidad+"&col="+color+"&precio="+precio+"&cf="+cflete+"&vf="+vflete+"&des="+descuento,
			   success: function(msg){
				alert(msg);
				// document.getElementById('carritoVenta').innerHTML=msg;
			   	 $.ajax({
				   type: "POST",
				   url: "asycall.php",
				   data: "opt=3",
				   success: function(msg){
				
					// document.getElementById('carritoVenta').innerHTML=msg;
					document.getElementById('totalitems').innerHTML="<a href='carrito.php'>("+msg+" Artículos)</a>";
					window.location = 'carrito.php';
					 
				   }
				 });			 
			   }
			 });
		   document.carrito.txtcantidad.value=1;

	   	}else{
			document.carrito.txtcantidad.value=1;
			
			}
		
		}		
	function addsuscriptor(){
		if (echeck(document.suscript.email.value)==true){
						$.ajax({
			   type: "POST",
			   url: "asycall.php",
			   data: "opt=5&correo="+document.suscript.email.value,
			   success: function(msg){
				alert("Suscripcion Exitosa");
				document.suscript.email.value="";
				 //document.getElementById('main').innerHTML=msg;
				 
			   }
			 });
			}else{
				alert("Correo Invalido");
				document.suscript.email.value="";
				}
		
		}	
		
		//Eliminar un producto 
	  function deleteProducto(id){
		if(confirm("Desea Eliminar este producto?")){
		  $.ajax({
			   type: "POST",
			   url: "asycall.php",
			   data: "opt=4&id="+id,
			   success: function(msg){
				//alert("prueba");
				 document.getElementById('main').innerHTML=msg;
				$.ajax({
				   type: "POST",
				   url: "asycall.php",
				   data: "opt=3",
				   success: function(msg){
					// document.getElementById('carritoVenta').innerHTML=msg;
					document.getElementById('totalitems').innerHTML="<a href='carrito.php'>("+msg+" items)</a>";
					 
				   }
				 });	 
			   }
			 });
			}
		  }		
		function enviarCorreo(){
			
			var nombre=document.form2.txtnombre.value;
			var apellido=document.form2.txtapellido.value;
			var correo=document.form2.txtcorreo.value;
			var prod=document.form2.nombre.value;
			var color=document.form2.color.value;
			var descr=document.form2.textfield4.value;
		
			if(nombre=="" || apellido=="" || correo=="" ){
				document.getElementById('msjcontact').innerHTML="<span class='wrong'>Los campos con * son obligatorios</span>";
				return false;
				}
			/* $.ajax({
				   type: "POST",
				   url: "asycall.php",
				  // data: "opt=9&no="+nombre+"&ap="+apellido+"&co="+correo+"&pro="+prod+"&col="+color+"&des="+descr,
				  data: "opt=9&no="+nombre+"&ap="+apellido+"&co="+correo+"&pro="+prod+"&col="+color,
				   success: function(msg){
					
					// document.getElementById('carritoVenta').innerHTML=msg;
	document.getElementById('msjcontact').innerHTML="Tu mensaje fue enviado";
					 
				   }
				 });	*/
				document.getElementById('msjcontact').innerHTML="<span class='exito'>Tu mensaje fue enviado</span>";
				return true;
			}
			
			
			
            function getBrowserHeight() {
                var intH = 0;
                var intW = 0;
               
                if(typeof window.innerWidth  == 'number' ) {
                   intH = window.innerHeight;
                   intW = window.innerWidth;
                } 
                else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                    intH = document.documentElement.clientHeight;
                    intW = document.documentElement.clientWidth;
                }
                else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
                    intH = document.body.clientHeight;
                    intW = document.body.clientWidth;
                }

                return { width: parseInt(intW), height: parseInt(intH) };
            }  

            function setLayerPosition() {
                var shadow = document.getElementById("shadow");
                var question = document.getElementById("question");

                var bws = getBrowserHeight();
                shadow.style.width = bws.width + "px";
                shadow.style.height = bws.height + "px";

                question.style.left = parseInt((bws.width - 350) / 2);
               question.style.top = parseInt((bws.height - 200) / 2);

                shadow = null;
                question = null;
            }			
			
		function grabarInformacion(idref,total,vflete){
			     setLayerPosition();

                var shadow = document.getElementById("shadow");
                var question = document.getElementById("question");
 
                shadow.style.display = "block"; 
                question.style.display = "block";

                shadow = null;
                question = null;  
			
			
			 $.ajax({
				   type: "POST",
				   url: "asycall.php",
				   data: "opt=8&ref="+idref+"&vf="+vflete+"&vt="+total,
				   success: function(msg){
					  
 
					// document.getElementById('carritoVenta').innerHTML=msg;
					//alert(msg)
					if(msg=="true"){
							document.formpagos.submit();
							return true;
						}else{
												    var shadow = document.getElementById("shadow");
						var question = document.getElementById("question");
		 
						shadow.style.display = "none"; 
						question.style.display = "none";
		
						shadow = null;
						question = null;
							document.getElementById('errormsj').innerHTML="<span style='color:#F00'>Error al procesar la informacion. Intente mas Tarde</span>";
						return false;					

							}
					//document.getElementById('carritomod4').innerHTML=msg;
					 
				   }
				 });	
			
			}	
			window.onresize = setLayerPosition;
	  function cambiarImagen(id,cat){
								
		  $.ajax({
			   type: "POST",
			   url: "asycall.php",
			   data: "opt=1&idp="+id+"&c="+document.carrito.colores.value+"&cat="+cat+"&tit="+document.carrito.hiddenTitulo.value,
			   success: function(msg){
				   
				document.getElementById('imagenprod').innerHTML="";
				
				$('#imagenprod').append(msg); 
					 $.ajax({
						   type: "POST",
						   url: "asycall.php",
						   data: "opt=6&idp="+id+"&c="+document.carrito.colores.value+"&cat="+cat+"&tit="+document.carrito.hiddenTitulo.value,
						   success: function(msg){
							
							// document.getElementById('carritoVenta').innerHTML=msg;
							document.getElementById('carritomod4').innerHTML=msg;
							 
						   }
						 });
				/*$.ajax({
				   type: "POST",
				   url: "asycall.php",
				   data: "opt=10&idp="+id+"&c="+document.carrito.colores.value,
				   success: function(msg){*/
					
					//Modificar informacion de caracterisiticas y precio
					//document.getElementById('precioArticulo').innerHTML=msg;
					
					/*document.carrito.hiddenprecio.value=document.getElementById('hiddenprecioaux').value
					
	*/
				 //  }
				 //});
						  
						  
	  
						 // carritomod4
			   }
			 });
		  } 
  function validarContacto(){
			var elementos = document.contactoform.elements.length;
				for(i=0; i<elementos; i++){
					if(document.contactoform.elements[i].type == "text"){
						if(document.contactoform.elements[i].value==""){
							document.getElementById('msjerror').innerHTML="<span class='error'>Los campos con * son obligatorios</span>";
							return false;
						}
					}
				}
				if(document.contactoform.ciudad.value==0){
										document.getElementById('msjerror').innerHTML="<span class='error'>Selecciona la Ciudad</span>";
					return false;
					}
						if(document.contactoform.asunto.value==0){
										document.getElementById('msjerror').innerHTML="<span class='error'>Selecciona la Ciudad</span>";
					return false;
					}
	if (echeck(document.contactoform.email.value)==false){
					document.getElementById('msjerror').innerHTML="<span class='error'>Rectifica el Correo</span>";
					return false;
					}else{	
				
	  		return true;
					}
	  }
	  
	  //Validar password
	 function  validarPasword(){
		
			if(document.formpassword.confpassword.value != document.formpassword.password.value || document.formpassword.confpassword.value==""){
					document.getElementById('msjerror').innerHTML="<span class='error'>Las contrase&ntilde;as no coinciden</span>";
					document.formpassword.confpassword.value="";
					 document.formpassword.password.value="";
					return false;
					}else{		
				
						if(document.formpassword.confpassword.value<=5){
							document.getElementById('msjerror').innerHTML="<span class='error'>La contrase&ntilde;a tiene que ser de mas de 5 caracteres</span>";
							return false;
						}else{	
							return true;
						}
					}
		 
		 }
	  
	  //VALIDAR ACTUALIZACION DE DATOS
	  function validarActualizacion(){
		
		if(document.registro.nombre.value == "" || document.registro.apellido.value=="" || document.registro.ciudad.value=="" || document.registro.direccion.value==""){
					document.getElementById('msjerror').innerHTML="<span class='error'>Los campos con * son obligatorios</span>";
			alert("Validando"+document.registro.nombre.value )
					return false;
					}else{
						
						return true;
						}		  
		
		  
		  }
	  
//Funcion para validar el formulario de registro
		function validarregistro(){
			
		//	alert("asdsadasd"+document.registro.email.value+" + "+document.registro.confemail.value)
			var elementos = document.registro.elements.length;
				for(i=0; i<elementos; i++){
				//si el elemento definido en la array formulario esta vacio...

					if(document.registro.elements[i].type == "text"){
						
						if(document.registro.elements[i].value==""){
							document.getElementById('msjerror').innerHTML="<span class='error'>Los campos con * son obligatorios</span>";
							return false;
						}
					}
				}
				//Verificamos que los correos conincida
				if(document.registro.email.value != document.registro.confemail.value || document.registro.email.value==""){
					document.getElementById('msjerror').innerHTML="<span class='error'>Los correos no coinciden</span>";
					return false;
					}
					
				if (echeck(document.registro.email.value)==false){
					document.getElementById('msjerror').innerHTML="<span class='error'>Ingrese un correo valido</span>";
					return false;
					}
				

				if(document.registro.confpassword.value != document.registro.password.value || document.registro.confpassword.value==""){			
					document.getElementById('msjerror').innerHTML="<span class='error'>Las contrase&ntilde;as no coinciden</span>";
					document.registro.confpassword.value="";
					 document.registro.password.value="";
					return false;
					}
				
				if(document.registro.password.value.length<5){
						document.getElementById('msjerror').innerHTML="<span class='error'>La contrase&ntilde;a tiene que ser de mas de 5 caracteres</span>";
						return false;
						}	
						
						//alert("esooo");
				return true;
			
			
		}

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){
		   
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false
		 }

 		 return true					
	}
