// JavaScript Document
function validarFrm() {
	if(!document.frm['nombre'].value) { alert('El campo nombre es obligatorio...'); document.frm['nombre'].focus(); }
	else if(!document.frm['telefono'].value) { alert('El campo telefono es obligatorio...'); document.frm['telefono'].focus(); }
	else if(!document.frm['email'].value) { alert('El campo e-mail es obligatorio...');	document.frm['email'].focus(); }
	else if(!document.frm['consulta'].value) { alert('El campo consulta es obligatorio...'); document.frm['consulta'].focus(); }
	else { document.frm['enviar_frm'].value = 1; document.frm.submit(); }
	return false;
}

function validarFrmReg() {
	if(!document.frm['nombre'].value) { alert('El campo nombre es obligatorio...'); document.frm['nombre'].focus(); }
	else if(!document.frm['apellidos'].value) { alert('El campo apellidos es obligatorio...'); document.frm['apellidos'].focus(); }
	else if(!document.frm['telefono'].value) { alert('El campo telefono es obligatorio...'); document.frm['telefono'].focus(); }
	else if(!document.frm['email'].value) { alert('El campo e-mail es obligatorio...');	document.frm['email'].focus(); }
	else if(!document.frm['usuario'].value) { alert('El campo usuario es obligatorio...'); document.frm['usuario'].focus(); }
	else if(!document.frm['contrasena'].value) { alert('El campo contraseña es obligatorio...'); document.frm['contrasena'].focus(); }
	else if(!document.frm['repetir'].value) { alert('Debe repetir la contraseña...'); document.frm['repetir'].focus(); }
	else if(document.frm['contrasena'].value != document.frm['repetir'].value) { alert('Las contraseñas no coinciden...'); document.frm['contrasena'].value = ''; document.frm['repetir'].value = ''; document.frm['contrasena'].focus(); }
	else { document.frm['enviar_frm'].value = 1; document.frm.submit(); }
	return false;
}

function borrarLogo() {
	var msg = '¿Desea borrar su logotipo?\nRecuerde que el borrado no es reversible.';
	if(confirm(msg)) {
		document.frm['borrar_logo'].value = 1; 
		document.frm.submit();
	}
	return false;
}

function iniciarSesion() {
	if(!document.frm_login['usuario'].value) { alert('Introduzca un usuario.'); document.frm_login['usuario'].focus(); }
	else if(!document.frm_login['password'].value) { alert('Introduzca una contraseña.'); document.frm_login['password'].focus(); }
	else { document.frm_login.submit(); }			
}

function cerrarSesion() {
	document.frm_logout['var_cerrar'].value = '1';
	document.frm_logout.submit();
}

function validar(e, permiso, destino) { 
	var tecla, patron, te;
	tecla = (document.all) ? e.keyCode : e.which; 
	if (tecla == 46 || tecla == 8 || tecla == 0 ) { return true; }
	else if( tecla == 13 ){ if(destino != ''){ eval(destino); } }
	patron = permiso; 
	te = String.fromCharCode(tecla); 
	return patron.test(te); 
}

function actualizarCesta(url, ind){
	document.frm['act_uni'].value = document.frm['unidades_'+ ind].value;
	document.frm.action = url;
	//alert("document.frm['act_uni'].value = "+ document.frm['act_uni'].value)
	//alert("document.frm.action = "+ document.frm.action);
	document.frm.submit();
}

function borrarArticulo(num, url){
	var msg = '¿Desea realmente el artículo nº '+ num +' de su cesta?\nRecuerde que el borrado no es reversible.';
	if(confirm(msg)) {
		document.frm.action = url; 
		document.frm.submit();
	}
}

function borrarCesta(){
	var msg = '¿Desea realmente borrar todo el contenido de su cesta?\nRecuerde que el borrado no es reversible.';
	if(confirm(msg)) {
		document.frm.action = '/su-cesta/eliminar_cesta_compra/'; 
		document.frm.submit();
	}
}

function enviarCesta(){
	var accion = document.frm['accion'].value.toUpperCase();
	var msg = 'Se va a proceder a '+ accion +' su cesta.\n¿Está seguro de continuar?';
	if(confirm(msg)) {
		document.frm.action = '/su-cesta/';
		document.frm.submit();
	}
	document.frm['accion'].value = '';
	return false;
}

function comprobarDatos(){
	if(!document.frm['direccion'].value && !document.frm['codi_post'].value && !document.frm['localidad'].value && !document.frm['provincia'].value) { 
		var msg = 'Se va a proceder a realizar su pedido.\nAl no especificar datos de envío deberá recogerlo en nuestros almacenes.\n¿Está seguro de continuar?';
		if(confirm(msg)) {
			document.frm['enviar_frm'].value = 1;
			document.frm.action = '/su-cesta/';
			document.frm.submit();
		} 
	}
	else { 
		if(!document.frm['direccion'].value) { alert('El campo dirección es obligatorio...'); document.frm['direccion'].focus(); }
		else if(!document.frm['codi_post'].value) { alert('El campo código postal es obligatorio...'); document.frm['codi_post'].focus(); }		
		else if(!document.frm['localidad'].value) { alert('El campo localidad es obligatorio...'); document.frm['localidad'].focus(); }
		else if(!document.frm['provincia'].value) { alert('El campo provincia es obligatorio...'); document.frm['provincia'].focus(); }
		else{
			var msg = 'Se va a proceder a realizar su pedido.\n¿Está seguro de continuar?';
			if(confirm(msg)) {
				document.frm['enviar_frm'].value = 1;
				document.frm.action = '/su-cesta/';
				document.frm.submit();
			}	
		}
	}
	return false;
}

function confirmarPresupuesto(){
	var msg = 'Se va a proceder a solicitar este presupuesto.\n¿Está seguro de continuar?';
	if(confirm(msg)) {
		document.frm['enviar_frm'].value = 1;
		document.frm.action = '/su-cesta/presupuestar/';
		document.frm.submit();		
	}
	return false;
}

function plegarCategorias(ind){
	for(var i = 0; i < document.frm['total_familias'].value; i++){ if(i != ind) { document.getElementById('dl_fam_'+ i).className = 'dl-dis-none'; } }
	document.getElementById('dl_fam_'+ ind).className = document.getElementById('dl_fam_'+ ind).className == 'dl-dis-none' ? 'dl-dis-bock' : 'dl-dis-none';
}

function calcularPrecioProducto(){ document.frm['precio_tot_pro'].value = roundNumber(document.frm['unidades'].value * document.frm['precio'].value, 2) + ' €'; }
function roundNumber(num, dec) { return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); }

function agregarProducto(){
	if(!document.frm['unidades'].value || document.frm['unidades'].value == 0) {
		var msg = 'No ha insertado unidades. ¿Desea borrar el producto de su cesta?';
		if(confirm(msg)) {
			document.frm.action = '/su-cesta/borrar/';
			document.frm.submit();
		}
	}
	else { document.frm.submit(); }
}


function plegarPedidos(ind, total){
	for(var i = 0; i < total; i++){ if(i != ind) { document.getElementById('div_ped_'+ i).className = 'div-sus-ped-ocu'; } }
	document.getElementById('div_ped_'+ ind).className = document.getElementById('div_ped_'+ ind).className == 'div-sus-ped-ocu' ? 'div-sus-ped-vis' : 'div-sus-ped-ocu';
}
