// JavaScript Document
var empezar = false
var anclas = new Array ("m1","m2","m3","m4")
var capas = new Array("carga","carga1","carga2","carga3")
var retardo 
var ocultar
jQuery(document).ready( function(){					   
	empezar = true
	jQuery("#carga").mouseover(function(){
		muestra_retarda('carga');
	});

	jQuery("#carga").mouseout(function(){
		oculta_retarda('carga','m1');
	});

	jQuery("#carga1").mouseover(function(){
		muestra_retarda('carga1');
	});

	jQuery("#carga1").mouseout(function(){
		oculta_retarda('carga1','m2');
	});

	jQuery("#carga2").mouseover(function(){
		muestra_retarda('carga2');
	});

	jQuery("#carga2").mouseout(function(){
		oculta_retarda('carga2','m3');
	});

	jQuery("#carga3").mouseover(function(){
		muestra_retarda('carga3');
	});
	
	jQuery("#carga3").mouseout(function(){
		oculta_retarda('carga3','m4');
	});

	jQuery("#imagess a").click(function(){
		var title = "aplication/utilities/img_td.php?imagen=" + jQuery(this).attr("title") + "&w=258&h=170";
		//alert(title);
		var len = title.length;
		var text = title.substr(0,33);
		//var newtitle = text + title.substr(39,len);
		//alert(newtitle);
		var newtitle = "aplication/utilities/img_td.php?imagen=" + jQuery(this).attr("title")+ "&w=600&h=400";
		jQuery("#imgp").hide();
		jQuery("#imgp").attr("src",title).fadeIn('slow'); 
		jQuery("#imgp").attr("alt",title); 
		jQuery("#grande").attr("href",newtitle); 
	});	

	jQuery("#find").click( function(){
		var keyword = jQuery("#buscar").val()					   	
		buscar('index.php',keyword)							
	})	

	autoScroll('newsslider','newssection',3,true);
	jQuery(".text-car").css("display","none");
	jQuery(".text-car").eq(0).css("display","block");

	jQuery(".tabs a").each(function(index){
		jQuery(this).click(function(){
			pintar();
			jQuery(".tabs a").eq(index).html('<img src="aplication/webroot/imgs/tab-b-'+index+'.jpg"/>');
			jQuery(".text-car").eq(index).css("display","block");
		});	
	});

	function pintar(){
		for(v=0;v<4;v++){
			jQuery(".tabs a").eq(v).html('<img src="aplication/webroot/imgs/tab-a-'+v+'.jpg"/>');
			jQuery(".text-car").eq(v).css("display","none");
		}
	}

	jQuery(".item-forma-envio").each(function(x){
		jQuery(this).mouseover(function(){
			if(jQuery(".item-forma-envio input").eq(x).attr("checked") != true ){
				jQuery(this).attr("class","item-forma-envio white-oscuro");
				jQuery(this).css("cursor","pointer");
			}else{
				jQuery(this).css("cursor","normal");
			}							
		});
		jQuery(this).mouseout(function(){
			if(jQuery(".item-forma-envio input").eq(x).attr("checked") != true ){
				jQuery(this).attr("class","item-forma-envio white");
			}			
		});	
		jQuery(this).click(function(){
			jQuery(".item-forma-envio").attr("class","item-forma-envio white");
			jQuery(".item-forma-envio input").eq(x).attr("checked",true);
			jQuery(".item-forma-envio").eq(x).attr("class","item-forma-envio white-oscuro");
		});	
	});
});

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 muestra(capa){
	jQuery("#"+capa).fadeIn("slow");
}
function oculta(capa){
	jQuery("#"+capa).fadeOut("slow");
}

function muestra_coloca(capa, cat){
 if (empezar){
	for (i=0;i<capas.length;i++){
		if (capas[i] != capa) jQuery("#"+capas[i]).fadeOut("slow");
		jQuery("#"+anclas[i]+" a").attr("id","");
	}
	clearTimeout(retardo)
	jQuery("#"+capa).fadeIn("slow")
	jQuery("#"+cat+" a").attr("id","hov");
	
	
 }
}
function oculta_retarda(capa, cat){
 if (empezar){
	ocultar =capa
	clearTimeout(retardo)
	retardo = setTimeout("xHide('" + ocultar + "','"+cat+"')",1000)
	
 }
}
function xHide(val, cat){
	jQuery("#"+val).fadeOut("slow");
	jQuery("#"+cat+" a").attr("id","");
}
function muestra_retarda(ind){
 if (empezar){
	clearTimeout(retardo)
 }
}

function valida_detalle(id, opcion){
	var f1 = eval("document.detalle");
	var total=0;
	
	f1.action="cesta.php?prod="+id+"&car="+opcion;
	f1.submit();
}

function validar(){
	if(document.frm.nombre.value==""){
		alert("CUIDADO: Por favor introduzca su nombre");
		document.frm.nombre.focus();
		return false;
	}else if(document.frm.empresa.value==""){
		alert("CUIDADO:Por favor introduzca su Empresa");
		document.frm.empresa.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.message.value==""){
		alert("CUIDADO: Por favor escriba el mensaje");
		document.frm.message.focus();
		return false;
	}else{
		document.frm.submit();
		//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)"
}

function buscar(url,texto){
	document.f2.action=url+'?q='+ texto;
	document.f2.submit(); 
}

	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 validar_mis_datos(url){
		document.cuenta.action = "cuenta.php?cuenta="+ url;
		document.cuenta.submit();
}

function validar_mi_contrasenia(url){
		document.cuenta.action = "cuenta.php?cuenta="+ url;
		document.cuenta.submit();
}

//ingreso de clientes
function validar_clientes_olda(opcion){
	//alert(opcion);
	if(document.frm_reg.nombre.value==""){
		alert(" ERROR: Por favor ingrese el nombre ");
		document.frm_reg.nombre.focus();
		return false
	}else if(document.frm_reg.apellidos.value==""){
		alert(" ERROR: Por favor ingrese el apellido ");
		document.frm_reg.apellidos.focus();
		return false	
	}else if(document.frm_reg.dni.value==""){
		alert(" ERROR: Por favor ingrese el dni ");
		document.frm_reg.dni.focus();
		return false	
	}else if(document.frm_reg.email.value==""){
		alert(" ERROR: Por favor ingrese el email ");
		document.frm_reg.email.focus();
		return false	
	}else if(checkemail(document.frm_reg.email.value)== false){
		document.frm_reg.email.value="";
		document.frm_reg.email.focus();
		return false;
	}else if(document.frm_reg.password.value==""){
		alert(" ERROR: Por favor ingrese el Password ");
		document.frm_reg.password.focus();
		return false	
	}else if(document.frm_reg.password_confirma.value==""){
		alert(" ERROR: Por favor ingrese la Conf. de password ");
		document.frm_reg.password_confirma.focus();
		return false	
	}else if(document.frm_reg.password.value!=document.frm_reg.password_confirma.value){
		alert(" ERROR: Por favor ingrese la Conf. de password correcta");
		document.frm_reg.password_confirma.focus();
		return false	
	}else if(document.frm_reg.empresa.value==""){
		alert(" ERROR: Por favor ingrese la Empresa ");
		document.frm_reg.empresa.focus();
		return false	
	}else if(document.frm_reg.direccion.value==""){
		alert(" ERROR: Por favor ingrese la Direccion ");
		document.frm_reg.direccion.focus();
		return false	
	}/*else if(document.frm_reg.suburbio.value==""){
		alert(" ERROR: Por favor suburbio ");
		document.frm_reg.suburbio.focus();
		return false	
	}*/else if(document.frm_reg.cp.value==""){
		alert(" ERROR: Por favor ingrese el codigo postal ");
		document.frm_reg.cp.focus();
		return false	
	}else if(document.frm_reg.poblacion.value==""){
		alert(" ERROR: Por favor ingrese la poblacion ");
		document.frm_reg.poblacion.focus();
		return false	
	}else if(document.frm_reg.provincia.value==""){
		alert(" ERROR: Por favor ingrese la provincia ");
		document.frm_reg.provincia.focus();
		return false	
	}else if(document.frm_reg.pais.value==""){
		alert(" ERROR: Por favor seleccione el pais ");
		document.frm_reg.pais.focus();
		return false	
	}else if(document.frm_reg.telefono.value==""){
		alert(" ERROR: Por favor ingrese el telefono ");
		document.frm_reg.telefono.focus();
		return false	
	}else{
		document.frm_reg.action='cuenta.php?cuenta='+opcion;
		document.frm_reg.submit();
	}
	
}

function validar_acceso_left_1(){
	if(document.frm_reg_1.email.value==""){
		alert("ERROR: ingrese su email ");
		document.frm_reg_1.email.focus();
		return false;
	}else if(checkemail(document.frm_reg_1.email.value)== false){
		document.frm_reg_1.email.value="";
		document.frm_reg_1.email.focus();
		return false;
	}else if(document.frm_reg_1.password.value==""){
		alert("ERROR: ingrese password ");
		document.frm_reg_1.password.focus();
		return false;
	}else{
		//document.frm_reg.action = url + "?cuenta=acceso";
		//document.frm_reg.submit();
	}	
}


function busca_old(){
	url='index.php';
	texto=document.getElementById("q").value;
	if(texto=="" || texto==" Search"){
		alert("Ingrese un criterio para la busqueda");	
		document.getElementById("q").focus();
		return false;
	}else{
		location.href=url+'?q='+ texto;
		//document.frm_search.submit();
	}
}

function img_inicio(){
	document.getElementById("ini").style.backgroundImage='url(aplication/webroot/imgs/img_inicio_blank.jpg)';	
}
function img_inicio_out(){
	document.getElementById("ini").style.backgroundImage='';	
}

function img_cuenta(){
	document.getElementById("cue").style.backgroundImage='url(aplication/webroot/imgs/img_cuenta_blank.jpg)';	
}
function img_cuenta_out(){
	document.getElementById("cue").style.backgroundImage='';	
}

/**/
function img_pedido(){
	document.getElementById("ped").style.backgroundImage='url(aplication/webroot/imgs/img_pedido_blank.jpg)';	
}
function img_pedido_out(){
	document.getElementById("ped").style.backgroundImage='';	
}
/**/

function img_servicio(){
	document.getElementById("sera").style.backgroundImage='url(aplication/webroot/imgs/img_servicios_blank.jpg)';	
}
function img_servicio_out(){
	document.getElementById("sera").style.backgroundImage='';	
}

/**/

function img_contactenos(){
	document.getElementById("cont").style.backgroundImage='url(aplication/webroot/imgs/img_contactenos_blank.jpg)';	
}
function img_contactenos_out(){
	document.getElementById("cont").style.backgroundImage='';	
}
function btn_menos(){
	valor = document.getElementById("cantidad").value;
	if(valor>1){
		valor = valor - 1;
		document.getElementById("cantidad").value = parseInt(valor)
	}
}
function btn_mas(){
	valor = document.getElementById("cantidad").value;
	valor = parseInt(valor) + 1;
	document.getElementById("cantidad").value = parseInt(valor)
}

function ver_sgte(x,y){
	archivo="page_novedades.php?x="+ x + "&y="+y;
	$("#prod_nov").hide();
	$("#prod_nov").load(archivo).fadeIn('slow');
}

function validar_envio_logo(valor){
	//alert(valor)
	document.getElementById("tipo_envio").value=valor;
	if(valor==2){
		document.getElementById("paquete").style.display='';
		document.getElementById("nombre_envio").value="Ud. a elegido PAQUETE AZUL";
	}else{
		document.getElementById("paquete").style.display='';
		document.getElementById("nombre_envio").value="Ud. a elegido ENVIO EXPRESS";
	}
}

function validar_pago_logo(valor){
	//alert(valor)
	document.getElementById("tipo_pago").value=valor;
	if(valor==1){
		document.getElementById("paquete").style.display='';
		document.getElementById("nombre_pago").value="Ud. a elegido PAYPAL";
	}else if(valor==2){
		document.getElementById("paquete").style.display='';
		document.getElementById("nombre_pago").value="Ud. a elegido Transferencia Bancaria";
	}else if(valor==3){
		document.getElementById("paquete").style.display='';
		document.getElementById("nombre_pago").value="Ud. a elegido CONTRA REEMBOLSO";
	}
}

function validar_sendcontrasenia(opcion){
	if(document.cuenta.email.value==""){
		alert(" ERROR: Por favor ingrese su email ");
		document.cuenta.email.focus();
		return false
	}else if(checkemail(document.cuenta.email.value)== false){
		document.cuenta.email.value="";
		document.cuenta.email.focus();
		return false;
	}else{
		document.cuenta.action='cuenta.php?cuenta='+opcion;
		document.cuenta.submit();
	}
}

function validar_send_mailings(opcion){
	if(document.frmemail.email.value==""){
		alert(" ERROR: Por favor ingrese su email ");
		document.frmemail.email.focus();
		return false
	}else if(checkemail(document.frmemail.email.value)== false){
		document.frmemail.email.value="";
		document.frmemail.email.focus();
		return false;
	}else{
		document.frmemail.action='cuenta.php?cuenta='+opcion;
		document.frmemail.submit();
	}
}


function regresar_rp(){
	location.href='cuenta.php?cuenta=recuperar_contrasenia';	
}

function calcular(valor){
	subtotal=0;
	var cod=document.cesta.elements["id[]"];
	tot=document.getElementById("totpro").value;
	for(var g=0;g<tot;g++){
		cantidad = document.getElementById('canti'+g).value;
		if(cantidad>0){
			flag = document.getElementById('canti'+g).value*document.getElementById('txtprecio'+g).value;
			document.getElementById('txttot'+g).value = flag.toFixed(2);
			subtotal = subtotal + parseInt(document.getElementById('txttot'+g).value);
			//actualiza_cesta("update" , cod , cantidad);
			document.cesta.action = 'cesta.php?car=update';
			document.cesta.submit();
		}else if(cantidad==0 || cantidad==""){
			alert("Debe ingresar una cantidad valida");	
			subtotal = subtotal + parseInt(document.getElementById('txttot'+g).value);
			document.getElementById('canti'+g).value = document.getElementById('cantih'+g).value;
			document.getElementById("subtot").value=subtotal.toFixed(2);
			return false;
		}
	}
	document.getElementById("subtot").value=subtotal.toFixed(2);
}


function actualiza_cesta(opcion , id , cantidad){
	//alert("das");
	archivo="update_cesta.php?car="+opcion+'&id='+id+'&cantidad='+cantidad;
	$("#capa_cesta").load(archivo);
}

function ver_productos(){
	archivo="ver_producto.php";
	$("#carga").load(archivo);
}

function ver_submenu(){
	//$('#arte').hide('slow');
	$('#carga').show('slow');
}

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(document.getElementById("pais").value == 0){
		opciones[i] =  "Debe seleccionar un pais de la lista desplegable";
		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 != "" || document.getElementById("pais").value != 0  || 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("Ocurrio los siguientes errores:\n\n"+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(document.getElementById("pais").value == 0){
		opciones[i] =  "Debe seleccionar un pais de la lista desplegable";
		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 != "" || document.getElementById("pais").value != 0  || 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("Ocurrio los siguientes errores:\n\n"+cadena);
			return false;	
		}else{
			frm.action = "pedido.php?step=add&pedido=pago";
			return true;		
		}	
	}else{
			frm.action = "pedido.php?step=addPago";
			return true;		
	}
	
}


