/*		FUNCION QUE MUESTRA UNA CAPA OCULTA CUANDO EL CURSOR PASA SOBRE UN ELEMENTO HTML	*/

function mostrar(elemento)
{
	 document.getElementById(elemento).style.display = "block";
	 
	 if(elemento == 'logueo')
		document.getElementById('out').style.display = "none";
}

/*		FUNCION QUE OCULTA UNA CAPA CUANDO EL CURSOR NO ESTA SOBRE UN ELEMENTO HTML		*/


function ocultar(elemento)
{
	document.getElementById(elemento).style.display = "none";
}


/*		FUNCION QUE COMPRUEBA QUE SE HAN INTRODUCIDO CARACTERES EN LA BUSQUEDA DE PRODUCTOS		*/

function comprobar(formulario) 
{
	var cadena = document.getElementById('buscar').value;
		
	if( cadena == null || cadena.length == 0 || /^\s+$/.test(cadena) ) 
	 {
		alert("Introduzca una cadena de texto.");
		return false;
	  }
	  else
	   return true;
} 

/*		FUNCION QUE COMPRUEBA QUE SE HAN INTRODUCIDO CARACTERES EN LA BUSQUEDA DE PEDIDOS		*/

function comprobarpedidos(formulario) 
{
	var cadena = document.getElementById('id_pedido').value;
		
	if( cadena == null || cadena.length == 0 || /^\s+$/.test(cadena) ) 
	 {
		alert("Introduzca una cadena de texto.");
		return false;
	  }
	  else
	   return true;
} 


/*	FUNCION QUE INSERTA EN UN FORMULARIO EL CODIGO PARA CREAR UN ENLACE A UNA WEB		*/

function insertaenlace()
{
	var enlace = prompt('Inserte el Enlace de una Web');
	
	if(enlace != null)
	{	
		var total = document.getElementById('cuerpo');	

		var inicio = total.selectionStart;

		var fin = total.selectionEnd;
		
		var total = total.value;

		var resultado = total.substring(0, inicio);

		resultado += '[link]' + enlace + '[/link]';

		resultado += total.substring(fin, total.length);

		document.getElementById('cuerpo').value = resultado;
		
		document.getElementById('cuerpo').focus();
	}
}


/*	FUNCION QUE INSERTA EN UN FORMULARIO EL CODIGO PARA ENLAZAR UNA IMAGEN DE UNA WEB		*/

function insertaimagen()
{
	var enlace = prompt('Inserte el Enlace de una Imagen'); 
	
	if (enlace != null)
	{
		imagen = new Image();
	
		imagen.src = enlace;
		
		if(imagen.width > 850)
		{
			imagen.width = imagen.width / (imagen.width / 850);
			
			imagen.height = imagen.height / (imagen.width / 850);		
		}
				
		var total = document.getElementById('cuerpo');	

		var inicio = total.selectionStart;

		var fin = total.selectionEnd;
		
		var total = total.value;

		var resultado = total.substring(0, inicio);

		resultado += '[imagen]' + imagen.src + '[/imagen][altura]'+ imagen.height + '[/altura][ancho]' + imagen.width + '[/ancho]';

		resultado += total.substring(fin, total.length);

		document.getElementById('cuerpo').value = resultado;

	}
}


/*	FUNCION QUE INSERTA EN UN FORMULARIO EL CODIGO PARA ENLAZAR UN VIDEO DE YOUTUBE		*/

function insertavideo()
{
	var enlace = prompt('Inserte el Enlace de un Video de YouTube');
	
	if(enlace != null)
	{	
		if(enlace.length > 11)
		{
			var posicion = enlace.indexOf('v=') + 2;
			enlace = enlace.substring(posicion, posicion + 11);
		}

		/* ejemplo: http://www.youtube.com/watch?v=Js2SAAgBJKU&feature=player_embedded*/
		
		var total = document.getElementById('cuerpo');	

		var inicio = total.selectionStart;

		var fin = total.selectionEnd;
		
		var total = total.value;

		var resultado = total.substring(0, inicio);

		resultado += '[video]' + enlace + '[/video]';

		resultado += total.substring(fin, total.length);

		document.getElementById('cuerpo').value = resultado;
	}
}


/*		FUNCION QUE COMPRUEBA QUE SE HAN INTRODUCIDO CARACTERES AL INSERTAR UNA NOTICIA		*/

function compruebanoticia() 
{
	var cadena = document.getElementById('titulo').value;
		
	if( cadena == null || cadena.length == 0 || /^\s+$/.test(cadena) ) 
	 {
		alert("Introduzca un texto en el Titular.");
		return false;
	 }

	   
	var cadena = document.getElementById('cuerpo').value;
		
	if( cadena == null || cadena.length == 0 || /^\s+$/.test(cadena) ) 
	 {
		alert("Introduzca un texto en el Cuerpo de la Noticia.");
		return false;
	  }
	  else
	   return true;
} 


//		FUNCIONES PARA INSERTAR ETIQUETAS HTML EN EL FORMULARIO DE NOTICIAS		

function insertaetiqueta(etiqueta)			
{

	if(etiqueta == "s") 
	{
		etiqueta = "span class='subraya'";
		etiqueta2 = "span";
	}
	else
		etiqueta2 = etiqueta;
		
	var navegador = navigator.appName;
	
	if(navegador != 'Microsoft Internet Explorer')
	{    
		var total = document.getElementById('cuerpo');	

		var inicio = total.selectionStart;

		var fin = total.selectionEnd;
		
		var total = total.value;

		var resultado = total.substring(0, inicio);

		resultado +=  "<"+ etiqueta + ">" + total.substring(inicio, fin) + "</" + etiqueta2 + ">";

		resultado += total.substring(fin, total.length);

		document.getElementById('cuerpo').value = resultado;

	}
	else
	{
      	var texto = document.selection.createRange();

		var texto = texto.text;
       		
		var total = document.getElementById('cuerpo');

		var total = total.value;
		
		var inicio = total.indexOf(texto);

		var fin = texto.length + total.indexOf(texto);
		
		var resultado = total.substring(0, inicio);

		resultado += "<" + etiqueta  +">" + total.substring(inicio, fin) + "</"+ etiqueta2 + ">";

		resultado += total.substring(fin, total.length);

		document.getElementById('cuerpo').value = resultado;
		
	}
}

function confirmar()
{
	if(confirm("¿Esta seguro de Borrar la Noticia?"))
		return true;
	else
		return false;
}

function contador()
{
	setTimeout("mostrando()",30000);
}


function mostrando(elemento)
{
	 document.getElementById("logueo").style.display = "none";
	 
	 document.getElementById('out').style.display = "block";
}


//	FUNCION QUE COMPRUEBA QUE LAS MEDIDAS DEL MUEBLE SON CORRECTAS

function testeo() 
{
	if(document.getElementById('unidades').value == 0) 
	{
		alert("Numero de unidades erroneo.");
		return false;
	}
	
	
	if (document.getElementById('alto').value * 1 < 5)
	{
		alert("Valores no válidos.");
		return false;
	}
	
	
	if (document.getElementById('ancho').value * 1 < 5)
	{
		alert("Valores no válidos.");
		return false;
	}
	
	
	if (document.getElementById('fondo').value * 1 < 5)
	{
		alert("Valores no válidos.");
		return false;
	}
	
	
	if((document.getElementById('zizq').value != '' )&&(document.getElementById('zizq').value < 0))
	{
		alert("Valores no válidos.");
		return false;
	}
	
	if((document.getElementById('zder').value != '' )&&(document.getElementById('zder').value < 0))
	{
		alert("Valores no válidos.");
		return false;
	}
	
	if((document.getElementById('zsup').value != '' )&&(document.getElementById('zsup').value < 0))
	{
		alert("Valores no válidos.");
		return false;
	}
	
	if((document.getElementById('zinf').value != '' )&&(document.getElementById('zinf').value < 0))
	{
		alert("Valores no válidos.");
		return false;
	}
	
	if( (document.getElementById('baldas').value == 'no') && (document.getElementById('nbaldas').value != 0) )
	{
		alert("Valores no válidos.");
		return false;
	}

	if(document.getElementById('margenval').value < 0)
	{
		alert("Valores no válidos.");
		return false;
	}

	
	 return true;
} 


//	FUNCION PARA ABRIR PAGINAS EN UNA NUEVA PESTAÑA

function abrir(url)
{
	window.open(url);
}


//	FUNCION QUE OCULTA / MUESTRA CAMPOS DEL FORMULARIO PARA CREAR PLANOS

function planos(lado)
{	
	if(lado == "trasero")
	{
		var trasero = document.getElementById('trasero').value;
		
		if(trasero == 1)
		{
			document.getElementById("zocatra").style.display = "block";
		}
		else
		{
			document.getElementById("zocatra").style.display = "none";
		}
	}
	
	var base = document.getElementById('base').value;
			
	var techo = document.getElementById('techo').value;
	
	if(lado == "superior")
	{	
		if(techo == 1)
		{
			document.getElementById("zocasup").style.display = "block";
			document.getElementById("zocaizq").style.display = "none";
			document.getElementById("zocader").style.display = "none";
		}
		else
		{
			document.getElementById("zocasup").style.display = "none";
			
			if(base == 2)
			{
				document.getElementById("zocader").style.display = "block";
				document.getElementById("zocaizq").style.display = "block";
			}
			else
			{
				document.getElementById("zocaizq").style.display = "none";
				document.getElementById("zocader").style.display = "none";
			}
		}
	}
	
	if(lado == "inferior")
	{	
		if(base == 1)
			{
				document.getElementById("zocainf").style.display = "block";
				document.getElementById("zocaizq").style.display = "none";
				document.getElementById("zocader").style.display = "none";
			}
			else
			{
				document.getElementById("zocainf").style.display = "none";
				
				if(techo == 2)
				{
					document.getElementById("zocader").style.display = "block";
					document.getElementById("zocaizq").style.display = "block";
				}
				else
				{
					document.getElementById("zocaizq").style.display = "none";
					document.getElementById("zocader").style.display = "none";
				}
			}
	}

	 if(elemento == 'logueo')
		document.getElementById('out').style.display = "none";
}


//	FUNCION PARA MOSTRAR / OCULTAR LOS ICONOS DE LAS MELAMINAS EN LA CREACION DE PLANOS

function muestram(total)
{
	for(x=1; x <= total; x++)
	{
		if((x != 7) && (x != 15) && (x != 3))
		{
			document.getElementById("mdiv" + x).style.display = "none";
			document.getElementById("visb" + x).style.display = "none";
		}
		
		document.getElementById("m" + x).style.display = "none";
		document.getElementById("visa" + x).style.display = "none";
		document.getElementById("visc" + x).style.display = "none";
	}
	
	var id = document.getElementById("melamina").value;
	var idiv = document.getElementById("melaminadiv").value;

	document.getElementById("m" + id).style.display = "block";
	document.getElementById("mdiv" + idiv).style.display = "block";
	document.getElementById("visa" + id).style.display = "block";
	document.getElementById("visb" + idiv).style.display = "block";
	document.getElementById("visc" + id).style.display = "block";

}

function muestracanto(id)
{
	var valor = document.getElementById("gr" + id).value;
	
	if(valor < 10)
		document.getElementById("canteados").style.display = "none";	
	else
		document.getElementById("canteados").style.display = "block";	
}


function muestram3(total)
{
	var id = document.getElementById("melamina").value;
	
	var x;
	
	for(x=1; x <= total; x++)
		document.getElementById("m" + x).style.display = "none";	

	document.getElementById("m" + id).style.display = "block";
	
}

function muestram2(total)
{
	var id = document.getElementById("melamina").value;
	
	var x;
	
	for(x=1; x <= total; x++)
	{
		document.getElementById("m" + x).style.display = "none";
		document.getElementById("visa" + x).style.display = "none";
		document.getElementById("c" + x).style.display = "none";
		document.getElementById("visc" + x).style.display = "none";				
	}
	
	document.getElementById("canto").options[document.getElementById("melamina").selectedIndex].selected = true;

	document.getElementById("m" + id).style.display = "block";
	document.getElementById("visa" + id).style.display = "block";
	document.getElementById("c" + id).style.display = "block";
	document.getElementById("visc" + id).style.display = "block";
	
	muestracanto(id);
	
}

function muestrac(total)
{
	for(x=1; x <= total; x++)
	{
		document.getElementById("c" + x).style.display = "none";
		document.getElementById("visc" + x).style.display = "none";
	}
	
	var id = document.getElementById("canto").value;

	document.getElementById("c" + id).style.display = "block";
	document.getElementById("visc" + id).style.display = "block";
}

function mostrardos()
{
	for(x=1; x <= 4; x++)
		document.getElementById("cuerpo" + x).style.display = "none";
	
	var id = document.getElementById("total").value;
	
	for(x=1; x <= id; x++)
		document.getElementById("cuerpo" + x).style.display = "block";
}

function mostrarmueble()
{	
	var id = document.getElementById("tipo").value;
	
	if(id == 'armario')
	{
		document.getElementById("taquilla").style.display = "none";
		document.getElementById("armario").style.display = "block";
	}
	else
	{
		document.getElementById("taquilla").style.display = "block";
		document.getElementById("armario").style.display = "none";
	}
}

//FUNCION QUE REDIRECCIONA A UNA URL

function iraurl(url)
{
	document.location.href="" + url;
}


//FUNCIONES QUE PREGUNTAN CONFIRMACION DE BORRADO DE PEDIDOS Y PLANOS

function confirmaciones(mensaje)
{
	if(confirm("" + mensaje))
		return true;
	else
		return false;
}

function borrapedido()
{
	if(confirm("¿Esta seguro de Borrar la Pieza?"))
		return true;
	else
		return false;
}

function borraplano()
{
	if(confirm("¿Esta seguro de Borrar el Plano?"))
		return true;
	else
		return false;
}


function compruebapedi ()
{
	if(isNaN(document.getElementById("cantidad").value))
	{
		alert("Introduzca una Cantidad válida.");
		return false;
	}
	else
		if (document.getElementById("cantidad").value < 1)
		{
			alert("Introduzca una Cantidad válida.");
			return false;
		}
		
	if(isNaN(document.getElementById("alto").value))
	{
		alert("Introduzca una Altura válida.\nUse el punto en vez de la coma para poner decimales.");		
		return false;
	}
	else
		if((document.getElementById("alto").value < 1) || (document.getElementById("alto").value  > 245))
		{
			alert("Introduzca una Altura válida.");
			return false;
		}
		
	if(isNaN(document.getElementById("ancho").value))
	{
		alert("Introduzca una Anchura válida.\nUse el punto en vez de la coma para poner decimales.");
		return false;
	}
	else
		if((document.getElementById("ancho").value < 1) || (document.getElementById("ancho").value > 245))
		{
			alert("Introduzca una Anchura válida.");
			return false;
		}
	
	
	return true;
}


function envio()
{
	var tipo = document.getElementById("tipo").value;
	
	if(tipo == 'taquilla')
	{
		var ancho = document.getElementById("taqancho").value;
		
		var cuerpos = document.getElementById("total2").value;
		
		var puertas = document.getElementById("puertas").value;
		
		var color = document.getElementById("color").value;
		
		window.open('mueble.php?ancho=' + ancho + '&total2=' + cuerpos + '&puertas=' + puertas + '&tipo=' + tipo + '&color=' + color);
	}
	else
	{
		var ancho1 = document.getElementById("ancho1").value;
		
		var ancho2 = document.getElementById("ancho2").value;
		
		var ancho3 = document.getElementById("ancho3").value;
		
		var ancho4 = document.getElementById("ancho4").value;
		
		var alto = document.getElementById("alto").value * 1;
		
		var fondo = document.getElementById("fondo").value * 1;
		
		var balda1 = document.getElementById("balda1").value * 1;
		
		var balda2 = document.getElementById("balda2").value * 1;
		
		var balda3 = document.getElementById("balda3").value * 1;
		
		var balda4 = document.getElementById("balda4").value * 1;
		
		var cuerpos = document.getElementById("total").value * 1;
		
		var melamina = document.getElementById("melamina").value;
		
		interruptor = 1;
		
		switch(cuerpos)
		{
			case 1: 
					if( (alto > 30) && (alto <= 245) && (fondo <= 65) 
					&& (balda1 <= alto - 30) && (balda1 >= 30) && (balda1.length != 0) )
						interruptor = 0;
				
					break;
			
			case 2:
					if( (alto > 30) && (alto <= 245) && (fondo <= 65) 
					&& (balda1 <= alto - 30) && (balda1 >= 30) && (balda1.length != 0) 
					&& (balda2 <= alto - 30) && (balda2 >= 30) && (balda2.length != 0) )
						interruptor = 0;
			
					break;
					
			case 3: 
					if( (alto > 30) && (alto <= 245) && (fondo <= 65) 
					&& (balda1 <= alto - 30) && (balda1 >= 30) && (balda1.length != 0) 
					&& (balda2 <= alto - 30) && (balda2 >= 30) && (balda2.length != 0) 
					&& (balda3 <= alto - 30) && (balda3 >= 30) && (balda3.length != 0) )
						interruptor = 0;
			
					break;
					
			case 4: 
					if( (alto > 30) && (alto <= 245) && (fondo <= 65) 
					&& (balda1 <= alto - 30) && (balda1 >= 30) && (balda1.length != 0) 
					&& (balda2 <= alto - 30) && (balda2 >= 30) && (balda2.length != 0) 
					&& (balda3 <= alto - 30) && (balda3 >= 30) && (balda3.length != 0) 
					&& (balda4 <= alto - 30) && (balda4 >= 30) && (balda4.length != 0) )
						interruptor = 0;
			
					break;
		}
		
		if( interruptor == 1)
		{	
			alert("Medidas no válidas.");
		}
		else
		{
			window.open('mueble.php?ancho1=' + ancho1 + '&total=' + cuerpos + '&ancho2=' + ancho2 + 
			'&ancho3=' + ancho3 + '&ancho4=' + ancho4 + '&fondo=' + fondo + '&alto=' + alto + '&balda1=' + balda1 + '&balda2=' + balda2 + 
			'&balda3=' + balda3 + '&balda4=' + balda4 + '&tipo=' + tipo + '&melamina=' + melamina );
		}
			
	}
}
