/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// tienda
function ancla_productos(){
	document.location.href = "#productos";
}
function cargar_tienda(){
	javascript:FAjax('tienda/carrito.php','carrito',this.name+'='+this.value, 'post');
	//javascript:FAjax('tienda/productos.php','productos',this.name+'='+this.value, 'post');
	javascript:FAjax('tienda/categorias.php','categorias',this.name+'='+this.value, 'post');
	javascript:FAjax('tienda/registrar_comprar.php','loggin',this.name+'='+this.value, 'post');
}
function ver_carrito(){
	javascript:FAjax('tienda/carrito_ver.php','productos',this.name+'='+this.value, 'post'); 
	setTimeout ("ancla_productos()", 1000);
}
function mostrar_productor_categoria(categoria){
	javascript:FAjax('tienda/productos.php','productos','categoria='+categoria, 'post');
	setTimeout ("ancla_productos()", 1000);
}
function agregar_al_carrito(producto, costo){
	javascript:FAjax('tienda/carrito.php','carrito','producto='+producto+'&costo='+costo, 'post');
}
function quitar_del_carrito(producto){
	javascript:FAjax('tienda/carrito_ver.php','productos','producto_eliminar='+producto, 'post');
	javascript:FAjax('tienda/carrito.php','carrito','producto_eliminar='+producto, 'post');
}
function registrar(){
	javascript:FAjax('tienda/formulario_registro.php','productos',this.name+'='+this.value, 'post');
	setTimeout ("ancla_productos()", 1000);
}
function comprar(){
	javascript:FAjax('tienda/loggin.php','loggin',this.name+'='+this.value, 'post');
}
function regresar_registrar_comprar(){
	javascript:FAjax('tienda/registrar_comprar.php','loggin',this.name+'='+this.value, 'post');	
}

function validar_loggin(){
	if(document.getElementById('usuario').value.length < 2){
		alert("Escriba su nombre de Usuario");
		document.getElementById('usuario').focus();
	}else
	if(document.getElementById('contrasena').value.length < 2){
		alert("Escriba su nombre de Contraseña");
		document.getElementById('contrasena').focus();
	}else{
		var usuario = document.getElementById('usuario').value;
		var contrasena = document.getElementById('contrasena').value;
		javascript:FAjax('tienda/loggin.php','loggin','usuario='+usuario+'&contrasena='+contrasena, 'post');	
	}
}

function realizar_compra(total){
	var cobrar = total;
	javascript:FAjax('tienda/realizar_compra.php','productos', 'total='+cobrar, 'post');
	setTimeout ("PcancelarPtodo()", 6000);
}
function PcancelarPtodo(){
	document.formulario.submit();
	cargar_tienda();
}
function recuperar_contrasena_formulario(){
	javascript:FAjax('tienda/formulario_recuperar_contrasena.php','recuperar_contrasena', this.name+'='+this.value, 'post');
}
function recuperar_contrasena(){
	javascript:FAjax('tienda/recuperar_contrasena.php','recuperar_contrasena', 'correo='+document.getElementById('recuperar_contrasena_email').value, 'post');
}
function disponibilidad_username(){
	javascript:FAjax('tienda/disponibilidad_username.php','username', 'usuario='+document.getElementById('usuario').value, 'post');
}
function validar_registro(){
	
	var re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
	if(document.getElementById('nombre').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo NOMBRES es obligatorio';
		document.getElementById('nombre').focus();
	}else
	if(document.getElementById('apellidos').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo APELLIDOS es obligatorio';
		document.getElementById('apellidos').focus();
	}else
	if(document.getElementById('mail').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo E-MAIL es obligatorio';
		document.getElementById('mail').focus();
	}else
	if(!re.exec(document.getElementById('mail').value)){
		document.getElementById("errores").innerHTML = 'Por favor verifique su E-MAIL';
		document.getElementById('mail').focus();
	}else
	if(document.getElementById('pais').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo PAIS es obligatorio';
		document.getElementById('pais').focus();
	}else
	if(document.getElementById('estado').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo ESTADO es obligatorio';
		document.getElementById('estado').focus();
	}else
	if(document.getElementById('municipio').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo MUNICIPIO es obligatorio';
		document.getElementById('municipio').focus();
	}else
	if(document.getElementById('colonia').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo COLONIA es obligatorio';
		document.getElementById('colonia').focus();
	}else
	if(document.getElementById('calle').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo CALLE es obligatorio';
		document.getElementById('calle').focus();
	}else
	if(document.getElementById('numero').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo NUMERO DE CASA es obligatorio';
		document.getElementById('numero').focus();
	}else
	if(document.getElementById('postal').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo CODIGO POSTAL es obligatorio';
		document.getElementById('postal').focus();
	}else
	if(document.getElementById('telefono').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo TELEFONO es obligatorio';
		document.getElementById('telefono').focus();
	}else
	if(document.getElementById('lada').value.length < 2){
		document.getElementById("errores").innerHTML = 'El campo LADA es obligatorio';
		document.getElementById('lada').focus();
	}else
	if(document.getElementById('usuario').value.length < 6){
		document.getElementById("errores").innerHTML = 'El campo USUARIO debe ser mayor a 6 caracteres';
		document.getElementById('usuario').focus();
	}else
	if(document.getElementById('usuario').value.length > 15){
		document.getElementById("errores").innerHTML = 'El campo USUARIO debe ser menor a 15 caracteres';
		document.getElementById('usuario').focus();
	}else
	if(document.getElementById('contrasena').value.length < 6){
		document.getElementById("errores").innerHTML = 'El campo CONTRASEÑA debe ser mayor a 6 caracteres';
		document.getElementById('contrasena').focus();
	}else
	if(document.getElementById('contrasena').value.length > 15){
		document.getElementById("errores").innerHTML = 'El campo CONTRASEÑA debe ser menor a 15 caracteres';
		document.getElementById('contrasena').focus();
	}else
	if(document.getElementById('contrasena').value !== document.getElementById('contrasena2').value){
		document.getElementById("errores").innerHTML = 'El campo CONTRASEÑA y REPETIR CONTRASEÑA no coinciden';
		document.getElementById('contrasena').focus();
	}else
	if(document.getElementById('usuario_disponible').value == 'no'){
		document.getElementById("errores").innerHTML = 'El USUARIO que escribio no esta disponible porfavor intente con otro';
		document.getElementById('usuario_disponible').focus();
	}else
	if(document.getElementById('nombre_facturacion').value.length < 3 ||document.getElementById('rfc_facturacion').value.length < 3 ||document.getElementById('direccion_facturacion').value.length < 3){

		var confirmar = confirm('¿Seguro que no desea registrar sus datos de facturación?');
		if(confirmar==true){
			
			javascript:FAjax('tienda/formulario_guardar.php','productos','nombre='+document.getElementById('nombre').value
																	   +'&apellidos='+document.getElementById('apellidos').value
																	   +'&mail='+document.getElementById('mail').value
																	   +'&pais='+document.getElementById('pais').value
																	   +'&estado='+document.getElementById('estado').value
																	   +'&municipio='+document.getElementById('municipio').value
																	   +'&colonia='+document.getElementById('colonia').value
																	   +'&calle='+document.getElementById('calle').value
																	   +'&numero='+document.getElementById('numero').value
																	   +'&postal='+document.getElementById('postal').value
																	   +'&telefono='+document.getElementById('telefono').value
																	   +'&lada='+document.getElementById('lada').value
																	   +'&usuario='+document.getElementById('usuario').value
																	   +'&mod_reg='+document.getElementById('mod_reg').value
																	   +'&nombre_facturacion='+document.getElementById('nombre_facturacion').value
																	   +'&rfc_facturacion='+document.getElementById('rfc_facturacion').value
																	   +'&direccion_facturacion='+document.getElementById('direccion_facturacion').value
																	   +'&contrasena='+document.getElementById('contrasena').value
																	   , 'post');
			setTimeout ("regresar_registrar_comprar()", 3000);
		}
	}else{
		
			javascript:FAjax('tienda/formulario_guardar.php','productos','nombre='+document.getElementById('nombre').value
																	   +'&apellidos='+document.getElementById('apellidos').value
																	   +'&mail='+document.getElementById('mail').value
																	   +'&pais='+document.getElementById('pais').value
																	   +'&estado='+document.getElementById('estado').value
																	   +'&municipio='+document.getElementById('municipio').value
																	   +'&colonia='+document.getElementById('colonia').value
																	   +'&calle='+document.getElementById('calle').value
																	   +'&numero='+document.getElementById('numero').value
																	   +'&postal='+document.getElementById('postal').value
																	   +'&telefono='+document.getElementById('telefono').value
																	   +'&lada='+document.getElementById('lada').value
																	   +'&usuario='+document.getElementById('usuario').value
																	   +'&mod_reg='+document.getElementById('mod_reg').value
																	   +'&nombre_facturacion='+document.getElementById('nombre_facturacion').value
																	   +'&rfc_facturacion='+document.getElementById('rfc_facturacion').value
																	   +'&direccion_facturacion='+document.getElementById('direccion_facturacion').value
																	   +'&contrasena='+document.getElementById('contrasena').value
																	   , 'post');
			setTimeout ("regresar_registrar_comprar()", 3000);
		
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// admin
function pedido(variable1, comprador){
	var id_venta = variable1;
	javascript:FAjax('reporte_clientes_compra.php','mostrar_venta_echa'+comprador, 'venta='+id_venta+'&id_div='+comprador, 'post');	
}
function validar_producto(){
	if(document.getElementById('categoria').value == 0){
		document.getElementById("errores").innerHTML = 'Debe seleccionar un categoria para el nuevo producto';
		document.getElementById('categoria').focus();
	}else
	if(document.getElementById('nombre').value.length < 3){
		document.getElementById("errores").innerHTML = 'Debe escribir un nombre para su producto';
		document.getElementById('nombre').focus();
	}else
	if(document.getElementById('costo').value.length < 2){
		document.getElementById("errores").innerHTML = 'Debe definir el precio de su producto';
		document.getElementById('costo').focus();
	}else
	if(isNaN(document.getElementById('costo').value)){
		document.getElementById("errores").innerHTML = 'Debe escribir un precio correctamente <br><br> <strong>Ejemplo:</strong> 12.50 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1500.80 ';
		document.getElementById('costo').focus();
	}else
	if(document.getElementById('imagen').value == ''){
		document.getElementById("errores").innerHTML = 'Debe seleccionar una imagen para mostrar su producto a los clientes';
		document.getElementById('imagen').focus();
	}else{
		document.getElementById("errores").innerHTML = '';
		document.getElementById("loader").style.display = "block";
		document.form1.submit();
	}
}

function validar_producto_actualizado(){
	if(document.getElementById('nombre').value.length < 3){
		document.getElementById("errores").innerHTML = 'Debe escribir un nombre para su producto';
		document.getElementById('nombre').focus();
	}else
	if(document.getElementById('costo').value.length < 2){
		document.getElementById("errores").innerHTML = 'Debe definir el precio de su producto';
		document.getElementById('costo').focus();
	}else
	if(isNaN(document.getElementById('costo').value)){
		document.getElementById("errores").innerHTML = 'Debe escribir un precio correctamente <br><br> <strong>Ejemplo:</strong> 12.50 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1500.80 ';
		document.getElementById('costo').focus();
	}else{
		document.getElementById("errores").innerHTML = '';
		document.getElementById("loader").style.display = "block";
		document.form1.submit();
	}
}

function validar_categoria(){
	if(document.getElementById('nombre').value.length < 3){
		document.getElementById("errores").innerHTML = 'Debe escribir un nombre para la categoria';
		document.getElementById('nombre').focus();
	}else
	if(document.getElementById('imagen').value == ''){
		document.getElementById("errores").innerHTML = 'Debe seleccionar una imagen para mostrar la categoria a los clientes';
		document.getElementById('imagen').focus();
	}else{
		document.getElementById("errores").innerHTML = '';
		document.getElementById("loader").style.display = "block";
		document.form1.submit();
	}
}

function validar_categoria_modificada(){
	if(document.getElementById('nombre').value.length < 3){
		document.getElementById("errores").innerHTML = 'Debe escribir un nombre para la categoria';
		document.getElementById('nombre').focus();
	}else{
		document.getElementById("errores").innerHTML = '';
		document.getElementById("loader").style.display = "block";
		document.form1.submit();
	}
}

function borrar_categoria(categoria){
			var confirmar = confirm('¿Seguro que desea eliminar esta categoria?');
			if(confirmar==true){
				location.href = "categorias_borrar_seleccion.php?id="+categoria;
			}
}
