// JavaScript Document
jQuery(document).ready(function(){	
	var alt_ini = 0;
	
	jQuery(".desplaza a").click( function(){
		var title = jQuery(this).attr("title");
		jQuery("#imgp").hide();
		
		jQuery("#imgp").attr("src", title).fadeIn('slow');
		jQuery("img").fullsize({destroy: true});
		jQuery("#imgp").attr("longdesc", title.replace('thumb_','')); 
		jQuery("img").fullsize();
	});	 
	
	jQuery("#ir").click( function(){
		var keyword = jQuery("#buscar").val()					   	
		buscar('index.php',keyword)							
	})	
	var posX = new Array("0px","-540px","-1080px","-1620");
	
	jQuery(".tabs a").eq(0).css("background-position","0px -50px");
	jQuery(".tabs a").each(function(index){
		jQuery(this).click(function(){
			jQuery(".tabs a").css("background-position","0px 0px");
			jQuery(this).css("background-position","0px -50px");
			jQuery(".slide").animate({left : posX[index]}, 350);
		});
	});
		
	jQuery('.bg-menu a')
		.css({backgroundPosition: "-158px 0px"})
		.mouseover(function(){
			jQuery(this).stop().animate({backgroundPosition:"(0px 0px)"}, {duration:500})
		})
		.mouseout(function(){
			if(jQuery(this).attr("id") != "active"){
				jQuery(this).stop().animate({backgroundPosition:"(-158px 0px)"}, {duration:200, complete:function(){
					jQuery(this).css({backgroundPosition: "-158px 0px"})
				}})
			}
		})
		jQuery('.bg-menu a').each(function(x){
			if(jQuery(this).attr("id") == "active"){
				jQuery('.bg-menu a').eq(x).css({backgroundPosition: "0px 0px"});		
			}						  
		});
		
		// MENU PRODUCTOS
		
		jQuery('.menu-navegacion a')
		.css({backgroundPosition: "0px -17px"})
		.mouseover(function(){
			jQuery(this).stop().animate({backgroundPosition:"(0px 0px)"}, {duration:300})
		})
		.mouseout(function(){
			if(jQuery(this).attr("id") != "ac"){
				jQuery(this).stop().animate({backgroundPosition:"(0px -17px)"}, {duration:200, complete:function(){
					jQuery(this).css({backgroundPosition: "0px -17px"})
				}})
			}
		})
		jQuery('.menu-navegacion a').each(function(x){
			if(jQuery(this).attr("id") == "ac"){
				jQuery('.menu-navegacion a').eq(x).css({backgroundPosition: "0px 0px"});		
			}						  
		});
		
	jQuery("#ir").click( function(){
		var keyword = jQuery("#buscar").val()					   	
		buscar('productos.php',keyword)							
	})	
	

});
function search_enter(e) { 
	tecla = (document.all) ? e.keyCode : e.which; 
    if (tecla==13){
		buscar('index.php',document.getElementById("buscar").value);
	}
	return true;
}
function buscar(url,texto){
	location.replace(url+'?q='+ texto);
}

function validate_delete_direccion(id){
	if(confirm("Esta seguro de eliminar la direccion?")){
		location.replace("direcciones.php?Address=delete&ID="+id);
	}	
}
 
function valida_detalle(id, opcion){
	if(jQuery("#stock").val() !="" && jQuery("#stock").val() > 0){
			var f1 = eval("document.detalle");
			var total=0;
			
			f1.action="cesta.php?prod="+id+"&car="+opcion;
			f1.submit();
	
	}else{
		return false;
	}
}

function validar(){
	if(document.frm.nombre.value==""){
		alert("CUIDADO: Por favor introduzca su nombre");
		document.frm.nombre.focus();
		return false;
	}else if(document.frm.apellido.value==""){
		alert("CUIDADO:Por favor introduzca su apellido");
		document.frm.apellido.focus();
		return false;
	}else if(document.frm.telefono.value==""){
		alert("CUIDADO: Por favor escriba al menos un telefono");
		document.frm.telefono.focus();
		return false;
	}else if(document.frm.email.value==""){
		alert("CUIDADO: Por favor escriba su e-mail");
		document.frm.email.focus();
		return false;
	}else if(document.frm.mensaje.value==""){
		alert("CUIDADO: Por favor escriba el mensaje");
		document.frm.mensaje.focus();
		return false;
	}else{
		return true;	
	}
}



function validar_acceso(url){
	if(document.f1.email.value==""){
		alert("ERROR: ingrese su email ");
		document.f1.email.focus();
		return false;
	}else if(checkemail(document.f1.email.value)== false){
		document.f1.email.value="";
		document.f1.email.focus();
		return false;
	}else if(document.f1.password.value==""){
		alert("ERROR: ingrese password ");
		document.f1.password.focus();
		return false;
	}else{
		document.f1.action = url + "?cuenta=acceso";
		document.f1.submit();
	}	
}

function valida_diselo(){
	if(document.f1.nombre.value==""){
		alert("ERROR: ingrese su nombre ");
		document.f1.nombre.focus()
		return false;
	}else if(document.f1.email.value==""){
		alert("ERROR: ingrese su email ");
		document.f1.email.focus()
		return false;
	}else if(document.f1.nombre_amigo.value==""){
		alert("ERROR: ingrese el nombre de su amigo ");
		document.f1.nombre_amigo.focus()
		return false;
	}else if(document.f1.email_amigo.value==""){
		alert("ERROR: ingrese el email de su amigo ");
		document.f1.email_amigo.focus()
		return false;
	}else if(document.f1.titulo.value==""){
		alert("ERROR: ingrese el titulo de mensaje ");
		document.f1.titulo.focus()
		return false;
	}else if(document.f1.mansaje.value==""){
		alert("ERROR: ingrese el mensaje ");
		document.f1.mansaje.focus()
		return false;
	}else {
		document.f1.action="recomienda.php";
		document.f1.submit();
	}	
}


function validpsw(e) { 
	tecla = (document.all) ? e.keyCode : e.which; 
	//alert(tecla)
    if (tecla==8 || tecla==46 || tecla==13) return true; //Tecla de retroceso (para poder borrar) 
    // dejar la línea de patron que se necesite y borrar el resto 
    //patron =/[A-Za-z]/; // Solo acepta letras 
    //patron = /\d/; // Solo acepta números
    patron = /\w/; // Acepta números y letras 
    //patron = /\D/; // No acepta números 
    // patron = /[\d.-]/; numeros el punto y el signo -
    te = String.fromCharCode(tecla); 
    verifica= patron.test(te);
	if(verifica==false){
		alert("El caracter que esta tratando de ingresar no se permite");		
		return false;
	}else{
		return true
	}	
	// uso  onKeyPress="return validpsw(event)"
}

function validacaracter(e) { 
	tecla = (document.all) ? e.keyCode : e.which; 
	//alert(tecla)
    if (tecla==8 || tecla==46 || tecla==13 || tecla==32) return true; //Tecla de retroceso (para poder borrar) 
    // dejar la línea de patron que se necesite y borrar el resto 
    //patron =/[A-Za-z]/; // Solo acepta letras 
    //patron = /\d/; // Solo acepta números
    patron = /\w/; // Acepta números y letras 
    //patron = /\D/; // No acepta números 
    // patron = /[\d.-]/; numeros el punto y el signo -
    te = String.fromCharCode(tecla); 
    return patron.test(te);	
	// uso  onKeyPress="return validacaracter(event)"
}

function actualiza_producto(){
	document.cesta.action="cesta.php?car=update";
	document.cesta.submit();	
}

function validnum(e) { 
	tecla = (document.all) ? e.keyCode : e.which; 
	//alert(tecla)
    if (tecla==8 || tecla==46) return true; //Tecla de retroceso (para poder borrar) 
    // dejar la línea de patron que se necesite y borrar el resto 
    //patron =/[A-Za-z]/; // Solo acepta letras 
    patron = /\d/; // Solo acepta números
    //patron = /\w/; // Acepta números y letras 
    //patron = /\D/; // No acepta números 
    //patron = /[\d.-]/; numeros el punto y el signo -
    te = String.fromCharCode(tecla); 
    return patron.test(te);  
	// uso  onKeyPress="return validnum(event)"
}



	var testresults
	function checkemail(value){
		var str = value
		var filter=/^.+@.+\..{2,3}$/

		if (filter.test(str))
			testresults=true
		else{
			alert("Por favor ingrese un e-mail valido...");
			testresults=false
		}
		return (testresults)
	}	
	
	function clave(value){
		var str = value
		var filter=/^.+@.+\..{2,3}$/
			if (filter.test(str))
		testresults=true
			else{
			alert(" Do not accept specials caracters ")
			testresults=false
		}
		return (testresults)
	}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



function validate_direccion_entrega(){
	var frm = eval('document.frmentrega');
	var rpt = 0;
	var validate = 0;
	var opciones = Array();
	var i = 0;


	if(frm.nombre.value == ""){
		opciones[i] =  "Su Nombre debe tener al menos 2 letras.";
		i++;
	}
	if(frm.apellidos.value == ""){
		opciones[i] =  "Sus apellidos deben tener al menos 2 letras.";
		i++;
	}
	if(frm.direccion.value == ""){
		opciones[i] =  "Su direccion debe tener al menos 10 letras.";
		i++;
	}
	if(frm.cp.value == ""){
		opciones[i] =  "Su codigo postal debe tener al menos 4 letras.";
		i++;
	}

	if(frm.provincia.value == ""){
		opciones[i] =  "Ingrese una provincia";
		i++;
	}
	
	if(frm.poblacion.value == ""){
		opciones[i] =  "Su poblacion debe tener al menos 3 letras";
		i++;
	}
	
	if(frm.nombre.value != "" || frm.apellidos.value != ""  ||  frm.direccion.value != "" ||  frm.cp.value != "" ||  frm.provincia.value != ""  || frm.poblacion.value != ""){
		rpt = 1;
		
	}
	
	
	var cadena ="";
	
	for(y=0;y<opciones.length;y++){
		 cadena += "- "+opciones[y]+"\n";	
	}
	if(rpt == 1){
		if(cadena.length > 0){
			alert(cadena);
			return false;	
		}else{
			frm.action = "pedido.php?step=add&pedido=envio";
			return true;		
		}	
	}else{
			frm.action = "pedido.php?step=addEnvio";
			return true;		
	}
	
}
function validate_direccion_facturacion(){
	var frm = eval('document.frmentrega');
	var rpt = 0;
	var validate = 0;
	var opciones = Array();
	var i = 0;


	if(frm.nombre.value == ""){
		opciones[i] =  "Su Nombre debe tener al menos 2 letras.";
		i++;
	}
	if(frm.apellidos.value == ""){
		opciones[i] =  "Sus apellidos deben tener al menos 2 letras.";
		i++;
	}
	if(frm.direccion.value == ""){
		opciones[i] =  "Su direccion debe tener al menos 10 letras.";
		i++;
	}
	if(frm.cp.value == ""){
		opciones[i] =  "Su codigo postal debe tener al menos 4 letras.";
		i++;
	}
	
	if(frm.provincia.value == ""){
		opciones[i] =  "Ingrese una provincia";
		i++;
	}
	
	if(frm.poblacion.value == ""){
		opciones[i] =  "Su poblacion debe tener al menos 3 letras";
		i++;
	}
	
	if(frm.nombre.value != "" || frm.apellidos.value != ""  ||  frm.direccion.value != "" ||  frm.cp.value != "" ||  frm.provincia.value != ""  || frm.poblacion.value != ""){
		rpt = 1;
		
	}
	
	
	var cadena ="";
	
	for(y=0;y<opciones.length;y++){
		 cadena += "- "+opciones[y]+"\n";	
	}
	if(rpt == 1){
		if(cadena.length > 0){
			alert(cadena);
			return false;	
		}else{
			frm.action = "pedido.php?step=add&pedido=pago";
			return true;		
		}	
	}else{
			frm.action = "pedido.php?step=addPago";
			return true;		
	}
	
}

function validate_pago(){
	var forma = document.frmpago.elements['forma_pago[]'];
	var confirma = 0;
	for(i=0;i<forma.length;i++){
		if(forma[i].checked == true){
			confirma = 1;
		}	
	}
	if(confirma == 1){
		return true;	
	}
	alert("Por favor seleccione un metodo de pago.");
	return false;
}

function select_category(val){
	if(val != 0){
		location.replace("index.php?cat="+val);
	}
}

function view_select(){
	if(jQuery("#nombre").val() != "" && jQuery("#apellidos").val() != ""){
		if(jQuery("#pais").val() == "0"){
			alert("Error: Por favor seleccione un pais");	
		}	
	}
}

function active(check){
	if(check.checked==true){
		jQuery("#company").css("display","block");
		jQuery("#cif").css("display","block");
		jQuery("#nif").css("display","none");
		jQuery(".claves").css("display","none");
		document.f1.nif.value = "";
	}else{
		jQuery("#company").css("display","none");
		jQuery("#cif").css("display","none");
		jQuery("#nif").css("display","block");
		jQuery(".claves").css("display","block");
		document.f1.empresa.value = "";
		document.f1.cif.value = "";
		document.f1.password.value = "";
		document.f1.password_confirma.value = "";

	}	
}

function validate_registro(){
	var f1 = eval("document.f1");
	var filtro = /^([a-zA-Z0-9]{6,20})$/;
	
	if(f1.nombre.value == ""){
		alert("Por favor, Ingrese su nombre");
		f1.nombre.focus();
		return false;	
	}
	else if(f1.apellidos.value == ""){
		alert("Por favor, Ingrese sus apellidos");
		f1.apellidos.focus();
		return false;	
	}
	else if(f1.email.value == ""){
		alert("Por favor, Ingrese su email");
		f1.email.focus();
		return false;	
	}
	else if(checkemail(f1.email.value) == false){
		f1.email.focus();
		return false;	
	}
	else if(f1.sexo[0].checked == false && f1.sexo[1].checked == false){
		alert("Por favor, seleccione una opcion.");
		f1.sexo[0].focus();
		return false;	
	}
	else if(f1.profesional.checked==true && f1.empresa.value == ""){
		alert("Por favor, ingrese el nombre de su empresa.");
		f1.empresa.focus();
		return false;
	}
	else if(f1.profesional.checked==true && f1.cif.value == ""){
		alert("Por favor, ingrese su cif.");
		f1.cif.focus();
		return false;
	}
	else if(f1.profesional.checked==false && f1.nif.value == ""){
		alert("Por favor, ingrese su nif.");
		f1.nif.focus();
		return false;
	}
	else if(f1.direccion.value == ""){
		alert("Por favor, ingrese su direccion.");
		f1.direccion.focus();
		return false;	
	}
	else if(f1.cp.value == ""){
		alert("Por favor, ingrese su codigo postal.");
		f1.cp.focus();
		return false;	
	}
	else if(f1.poblacion.value == ""){
		alert("Por favor, campo obligatorio.");
		f1.poblacion.focus();
		return false;	
	}
	else if(f1.telefono.value == ""){
		alert("Por favor, ingrese su telefono.");
		f1.telefono.focus();
		return false;	
	}
	else if(filtro.test(f1.password.value)==false && f1.profesional.checked==false){
		alert("- La contraseña debe contener 6 caracteres como mínimo y 20 caracteres como máximo. \n- Se  distingue entre mayusculas y minusculas.\n- No se acepta caracteres especiales.");
		f1.password.focus();
		return false	
	}
	else if(f1.password_confirma.value=='' && f1.profesional.checked==false){
		alert("Por favor, ingrese la confirmacion de la contraseña.");
		f1.password_confirma.focus();
		return false	
	}else if(f1.password.value != f1.password_confirma.value && f1.profesional.checked==false){
		alert("La confirmacion de la contraseña no es valida.");
		f1.password_confirma.focus();
		return false	
	}
}