var toggleActual="";	
var puede=true;
var mySlideParaQuien;

//Para el menu VARIOS
var toggleActualV="";	
var mySlideVarios;
function escribeflash(url,anchoswf,altoswf){
document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+anchoswf+'" height="'+altoswf+'"  align="middle">');
document.write('<param name="allowScriptAccess" value="sameDomain" />');
document.write('<param name="movie" value="'+url+'" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed wmode="transparent" src="'+url+'" quality="high" width="'+anchoswf+'" height="'+altoswf+'"  align="middle"  type="application/x-shockwave-flash" pluginspage="https://www.macromedia.com/go/getflashplayer" />');
document.write('</object>');}
function cargaPagina(a,b,c)
{
	new Ajax(a,{method:'post',postBody:c,update:b}).request();
	/*new Request(
	{
		method: 'post',
		url: url,
		onSuccess: function(texto){
		$(div).innerHTML = texto;}
	}).send(parametros);*/
}

function cambiaFoto()
{
	var rango_superior = total;  
	var rango_inferior = 1; 
	var aleatorio=actual;
	do
	{
		aleatorio = Math.floor(Math.random()*(rango_superior-(rango_inferior-1))) + rango_inferior;  
	}
	while(aleatorio == actual)
	
	eval("myFx" + actual + ".start('opacity', [1,0]);");
	eval("myFx" + aleatorio + ".start('opacity', [0,1]);");
	
	actual = aleatorio;
			
	setTimeout("cambiaFoto();", 10000);
}

function realizarPedido()
{
	var error = "";
		
	if($('nombre').value == "" && error == "")
	{
		alert("Debe rellenar el nombre.");
		error = "nombre";
	}
	
	/*if($('apellidos').value == "" && error == "")
	{
		alert("Debe rellenar los apellidos.");
		error = "apellidos";
	}*/
	
	if($('tlf').value == "" && error == "")
	{
		alert("Debe rellenar el teléfono.");
		error = "tlf";
	}
	
	if(error == "")
	{
		if(!comprueba_tfno($('tlf').value)) error = "tlf";
	}
			
	if($('mail').value == "" && error == "")
	{
		alert("Debe rellenar el e-mail.");
		error = "mail";
	}
	
	if(error == "")
	{
		if(!comprueba_mail($('mail').value)) error = "mail";
	}
				
	if($('nombre_des').value == "" && error == "")
	{
		alert("Debe rellenar el nombre del destinatario.");
		error = "nombre_des";
	}
	
	/*if($('tlf_des').value == "" && error == "")
	{
		alert("Debe rellenar el teléfono del destinatario.");
		error = "tlf_des";
	}
	
	if(error == "")
	{
		if(!comprueba_tfno($('tlf_des').value)) error = "tlf_des";
	}*/
	
	if($('direccion').value == "" && error == "")
	{
		alert("Debe rellenar la dirección.");
		error = "direccion";
	}
	
	/*if($('cp').value == "" && error == "")
	{
		alert("Debe rellenar el código postal.");
		error = "cp";
	}
	
	if((isNaN($('cp').value) || $('cp').value < 0 || $('cp').value > 55000) && error == "")
	{
		alert("El código postal no es válido.");
		error = "cp";
	}*/
	if($('provincia').value == "28")
	{
		if($('localidad1').value == "Otra" && $('localidad2').value == "" && error == "")
		{
			alert("Debe rellenar la localidad.");
			error = "localidad2";
		}
		else
			$('localidad').value = $('localidad1').value
	}
	else
	{
		if($('localidad2').value == "" && error == "")
		{
			alert("Debe rellenar la localidad.");
			error = "localidad";
		}
		else
			$('localidad').value = $('localidad2').value
	}
	
	if($('provincia').value == "" && error == "")
	{
		alert("Debe rellenar la provincia.");
		error = "provincia";
	}
	
	if($('mensaje').value == "" && error == "")
	{
		alert("Debe rellenar el mensaje.");
		error = "mensaje";
	}
	
	if(!comprueba_fecha($('dia').value, $('mes').value, $('anio').value) && error == "")
	{
		alert("La fecha de entrega no es correcta.");
		error = "dia";
	}
	
	if($('div_factura').style.display != 'none')
	{
		if($('nombreFactura').value == "" && error == "")
		{
			alert("Debe rellenar el nombre de la factura.");
			error = "nombreFactura";
		}
		if($('apellidosFactura').value == "" && error == "")
		{
			alert("Debe rellenar los apellidos de la factura.");
			error = "apellidosFactura";
		}
		if($('direccionFactura').value == "" && error == "")
		{
			alert("Debe rellenar la dirección completa de la factura.");
			error = "direccionFactura";
		}
		
		if(!validaCIF($('cifFactura').value) && !isValidNif($('cifFactura').value) && error == "")
		{
			alert("Debe rellenar la correctamente el CIF/NIF de la factura.\n\nFormtato NIF: 00000001X\nFormtato CIF: A00000001");
			error = "cifFactura";
		}
	}
	
	if(error == "")
	{
		if(document.form1.rd_tamano)
		{
			if(document.form1.rd_tamano[0].checked)
				$('tamano').value = 1;
			else if(document.form1.rd_tamano[1].checked)
				$('tamano').value = 2;
			else
				$('tamano').value = 3;
		}
		if(document.form1.formaPago[0].checked)
			$('metodoPago').value = 2;
		else
			$('metodoPago').value = 1;
		if(confirm("¿Todos los datos son correctos?"))
			document.form1.submit();
	}
	else
	{
		$(error).focus();		
		//$(error).setStyles({'border':'1px solid #991010', 'height':'14px'});
	}
}

function comprueba_tfno(telefono)
{
	if (isNaN(telefono))
	{	
		alert("El teléfono introducido no es válido");
		return false;
	}
	else
	{
		if ( !( (telefono >= 900000000) && (telefono < 1000000000) || (telefono >= 600000000) && (telefono < 700000000) ) )  
		{	
			alert("El teléfono introducido no es válido");
			return false;
		}
	}	
	
	return true;
}

function comprueba_mail(email)
{
	var b=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;	
	if ( b.test(email) == false )
	{
		alert("El formato del e-mail no es válido");
		return false;
	}
	return true;
}

function comprueba_fecha(dia,mes,anio)
{
	var elMes = parseInt(mes);		
	
	//alert(hoy + ": " + hoy.getTime() + " | " + fecha + ": " + fecha.getTime());

	if(elMes == 2)
	{
		if(esBisiesto(anio))
		{
			if(parseInt(dia) > 29)
				return false;
			else
				return true;
		}
		else
		{
			if(parseInt(dia) > 28)
				return false;
			else
				return true;
		}
	}

	if(elMes== 4 || elMes==6 || elMes==9 || elMes==11)
	{
		if(parseInt(dia) > 30)
			return false;
	
	}
	
	var hoy = new Date();
	var fecha = new Date(anio, mes - 1, dia, $('hora').value, $('minuto').value, hoy.getSeconds());
	
	if(hoy.getTime() > fecha.getTime())
		return false;
	
	return true;
}

function esBisiesto(anio)
{
	if(parseInt(anio)%4==0)
	{
		if(parseInt(anio)%100==0)
		{
			if(parseInt(anio)%400==0)		
				return true;
			else
				return false;
		}
		else
			return true;
	}
	else
		return false;
} 
function redondea(num)
{
	num = Math.round(num*100)/100;
	return num;
}

function cargaMapa()
{
	if (GBrowserIsCompatible()) {
		var map = new GMap2($("mapa"));
		var geocoder = new GClientGeocoder();
		var address = "Verdaguer y García, 1 (Madrid)";
		
		var LatLng = new GLatLng(geocoder.getLatLng(address, function(point) {
				map.setCenter(point, 15);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml(address);
				var myHtml = "<font style='font-family:verdana;font-size: 16px; color: #991010;'><b>Abaca Floristas</b></font>";
				map.openInfoWindowHtml(point, myHtml);			  
			}
		  )
		);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(LatLng);
	}
}
function llamaFuncion(tipo, seccion)
{
	setTimeout("recargaProductos('" + tipo + "', " + seccion + ")", 30000);
}

function llamaFuncion2(tipo, seccion)
{
	setTimeout("recargaProductos2('" + tipo + "', " + seccion + ")", 30000);
}
	
function recargaProductos(tipo, seccion)
{
	$('carrusel-' + seccion).effect('opacity').custom(1,0);
	setTimeout("cargaPagina('ver-carrusel2.php', 'carrusel-" + seccion + "', 'tipo=" + tipo + "&seccion=" + seccion + "');", 1000);	
	setTimeout("$('carrusel-" + seccion + "').effect('opacity').custom(0,1);", 1100)
}

function recargaProductos2(tipo, seccion)
{
	$('carrusel-' + seccion).effect('opacity').custom(1,0);
	setTimeout("cargaPagina('ver-carrusel.php', 'carrusel-" + seccion + "', 'tipo=" + tipo + "&seccion=" + seccion + "');", 1000);	
	setTimeout("$('carrusel-" + seccion + "').effect('opacity').custom(0,1);", 1100)
}


function actualizaPrecios()
{
	if(document.form1.rd_tamano)
	{
		if(document.form1.rd_tamano[0].checked)
			$('precio').value = document.form1.rd_tamano[0].value;
		else if(document.form1.rd_tamano[1].checked)
			$('precio').value = document.form1.rd_tamano[1].value;
		else
			$('precio').value = document.form1.rd_tamano[2].value;
			
		$('precio_producto').innerHTML = formatCurrency($('precio').value) + " &euro;";
	}
 	if($('provincia').value != 28)
		num = 30
	else if($('localidad1').value.toUpperCase() != "MADRID")
		num = 20;
	else
		num = 10

	$('gastos_envio').innerHTML = num + ",00";
	precio = parseFloat($('precio').value) + parseFloat(num);
	precio = precio.toFixed(2);
	$('total_compra').innerHTML = precio.replace(".", ",");
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+'.'+
	num.substring(num.length-(4*i+3));
	return (num + ',' + cents);
}

var estaInicializado = false;
function abreSubMenu(a,e)
{
	if(!estaInicializado)
		inicializa("a");
	else
	{
		var o={};
		var b=new Fx.Elements($(a),{wait:false,duration:300,transition:Fx.Transitions.Back.easeOut});
		//$("sub"+a).setStyle("display","inline");
		if(toggleActual!="" && toggleActual!=a)
		{
			ocultaSubMenu();
			toggleActual=a
		}
		else if(toggleActual==a)
			toggleActual="";
		else
			toggleActual=a;
		eval("mySlide"+a+".toggle();")
		
		if($("regaloBoton").src == "http://www.abacafloristas.com/imagenes/arriba.gif")
			 $("regaloBoton").src = "http://www.abacafloristas.com/imagenes/abajo.gif";
		else
			 $("regaloBoton").src = "http://www.abacafloristas.com/imagenes/arriba.gif";
	}
}

function ocultaSubMenu()
{
	var o={};
	var a=new Fx.Elements($(toggleActual),{wait:false,duration:300,transition:Fx.Transitions.Back.easeOut});	
	eval("mySlide"+toggleActual+".toggle();")
}

function inicializa(a){	
	mySlideParaQuien=new Fx.Slide('subParaQuien');
	mySlideParaQuien.toggle();
	$('subParaQuien').style.display = '';
	estaInicializado = true;
	$("regaloBoton").src = "http://www.abacafloristas.com/imagenes/abajo.gif";
}

//MENU VARIOS
var estaInicializadoV = false;
function abreSubMenuV(b)
{
	if(!estaInicializadoV)
	{
		inicializaV("b");
	}
	else
	{
		var o={};
		var a=new Fx.Elements($(a),{wait:false,duration:300,transition:Fx.Transitions.Back.easeOut});
		//$("sub"+a).setStyle("display","inline");
		if(toggleActualV!="" && toggleActualV!=a)
		{
			ocultaSubMenuV();
			toggleActualV=b
		}
		else if(toggleActualV==b)
			toggleActualV="";
		else
			toggleActualV=b;
		
		if($("variosBoton").src == "http://www.abacafloristas.com/imagenes/arriba.gif")
			 $("variosBoton").src = "http://www.abacafloristas.com/imagenes/abajo.gif";
		else
			 $("variosBoton").src = "http://www.abacafloristas.com/imagenes/arriba.gif";
		
		eval("mySlide"+b+".toggle();")		
	}
}

function ocultaSubMenuV()
{
	var o={};
	var b=new Fx.Elements($(toggleActualV),{wait:false,duration:300,transition:Fx.Transitions.Back.easeOut});	
	eval("mySlide"+toggleActualV+".toggle();")
}

function inicializaV(b){	
	mySlideVarios=new Fx.Slide('subVarios');
	mySlideVarios.toggle();
	$('subVarios').style.display = '';
	estaInicializadoV = true;
	$("variosBoton").src = "http://www.abacafloristas.com/imagenes/abajo.gif";
}

//MENU DESPLEGABLE


function verSolicitarFactura()
{
	if($('div_factura').style.display == 'none')
		$('div_factura').style.display = 'inline';
	else
		$('div_factura').style.display = 'none'
}

function validaCIF(CIF)
{
	var pares = 0;
	var impares = 0;
	var suma, ultima, numero;
	var letra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I");
	var cad;
	
	CIF = CIF.toUpperCase();
	
	var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g);
	if (!regular.exec(CIF)) return false;
		 
	ultima = CIF.substr(8,1);
	
	for (var cont = 1 ; cont < 7 ; cont ++){
		cad = (2 * parseInt(CIF.substr(cont++,1))).toString() + "0";
		impares += parseInt(cad.substr(0,1)) + parseInt(cad.substr(1,1));
		pares += parseInt(CIF.substr(cont,1));
	}
	cad = (2 * parseInt(CIF.substr(cont,1))).toString() + "0";
	impares += parseInt(cad.substr(0,1)) + parseInt(cad.substr(1,1));
	
	suma = (pares + impares).toString();
	numero = parseInt(suma.substr(suma.length - 1, 1));
	numero = (10 - numero).toString();
	if(numero == 10) numero = 0;
	
	if ((ultima == numero) || (ultima == letra[numero]))
		return true;
	return false; 
}
function isValidNif(abc)
{
	dni=abc.substring(0,abc.length-1);
	let=abc.charAt(abc.length-1);
	if (!isNaN(let)) {
		return false;
	}else{
		cadena = "TRWAGMYFPDXBNJZSQVHLCKET";
		posicion = dni % 23;
		letra = cadena.substring(posicion,posicion+1);
		if (letra!=let.toUpperCase()){
			return false;
		}
	}
	return true;
}




















