// JavaScript Document
var HTTP_WEB_ROOT = "http://www.tienda-disfraces.es/";

function cargando( texto ) {
	var cadena = "<div id=\"carga_index\"><img src=\""+ HTTP_WEB_ROOT +"img/cargando_normal.gif\" /><p>"+texto+"</p></div>";
	return cadena;
}

//Busca disfraces
function busca_disfraces( div )
{
	var idcat_buscador = document.getElementById("idcat_buscador").value;
	var tematica = document.getElementById("tematica").value;

	document.getElementById(div).innerHTML = cargando("Buscando artículos...");
	window.location.href = "listado_miniaturas.php?idcat_buscador="+ idcat_buscador + "&tematica=" + tematica;
}

function muestra_imagen( imagen ) {
	document.getElementById("imagen").src = "dinamic/marca_de_agua.php?src=../img_bd/" + imagen;
}

var con_envia_form = new XHConn();
var fin_envia_form = function (oXML) { document.getElementById('contacto_ficha').innerHTML = oXML.responseText; };
function envia_form( ) {
	var texto_error = "";
	
	texto_error += document.getElementById("nombre").value == "" ? "\n- Nombre" : "";
	texto_error += document.getElementById("email").value == "" && document.getElementById("telefono").value == "" ? "\n- E-mail o Teléfono" : "";
	texto_error += document.getElementById("consulta").value == "" ? "\n- Consulta a realizar" : "";
	texto_error += document.getElementById("confirma").checked == false ? "\n- Aceptar que nuestros comerciales se pongan en contacto con usted" : "";
	
	if( texto_error == "" ) {
		var cadena = prepara_envio('contacto_ficha');
		document.getElementById('contacto_ficha').innerHTML = "<div id=\"cargando_form\"><img src=\""+ HTTP_WEB_ROOT +"img/cargando_normal.gif\" /><p>Enviando formulario</p></div>";
		
		
		con_envia_form.connect("dinamic/mail_contacto.php", "POST", cadena, fin_envia_form);
	}
	else {
		alert( "Los siguientes campos son obligatorios: \n" + texto_error );
	}
}

function carga_contenido_ficha( tipo_contenido , idproducto )
{
	var fin_envia_form = function (oXML)
	{ 
		document.getElementById('div_contenidos_ficha').innerHTML = oXML.responseText;
		
		//Inicializamos el menú sin ninguna pestaña marcada
		if( document.getElementById("link_descripcion") )
		{
			document.getElementById("link_descripcion").className = "descripcion";
		}
		if( document.getElementById("link_envio") )
		{
			document.getElementById("link_envio").className = "envio";
		}
		if( document.getElementById("link_devoluciones") )
		{
			document.getElementById("link_devoluciones").className = "devoluciones";
		}
		if( document.getElementById("link_mas_info") )
		{
			document.getElementById("link_mas_info").className = "mas_info";
		}

		
		//Ponemos la clase actual para que se marque la pestaña en la que estamos
		document.getElementById("link_"+tipo_contenido).className = tipo_contenido+" actual";
	};
	document.getElementById('div_contenidos_ficha').innerHTML = "<div class=\"cargando_contenido_ficha\"><img src=\""+ HTTP_WEB_ROOT +"img/cargando_normal.gif\" /></div>";
	new XHConn().connect("dinamic/contenido_fichas.php", "POST", "ajax=1&tipo_contenido="+tipo_contenido+"&idproducto="+idproducto , fin_envia_form);
}

function valida_talla_seleccionada()
{
	var errores_formulario = "";
	var error_talla = 1;
	
	var form = document.getElementById("form_carrito_multiple");
	for( var i = 0 ; i < form.elements.length; i++ )
	{
		//El match devuelve null si no encuentra el campo
		var respuesta = form.elements[i].name.match(/^talla_([0-9]*)$/);
		if( respuesta != null )
		{
			var idtalla = respuesta[1];
			error_talla = ( document.getElementById("talla_"+idtalla).checked == true )? 0 : error_talla;
		}
	}
	
	errores_formulario += ( error_talla == 1 )? "- Debe seleccionar una talla como mínimo para poder añadir al carrito \r\n" : "";
	
	if( errores_formulario ==  "" )
	{
		form.submit();
	}
	else
	{
		alert(errores_formulario);	
	}
}
