// JavaScript Document
var array_precios = new Array(90,45,127,150,190,57,57,30,75,85,150,95,290,55,55,95,95,95,125,75,75,75,185);

function cambia_imagen( imagen, nombre_div )
{
	document.getElementById(nombre_div).innerHTML = "<img src=\"_dinamic/redimensiona_imagenes.php?src=../_img/"+imagen+"&w=468&h=308\" />";
	
}// JavaScript Document

function cambia_enlace( imagen, nombre_div )
{
	document.getElementById(nombre_div).innerHTML = "<a href='javascript: muestra_vista_previa(\"foto\",\"../_img/max/"+imagen+"\");' rel='nofollow' class='ampliar'>Ampliar imagen</a>";
	
}// JavaScript Document

/*-----------------------------AVISO LEGAL--------------------------------*/

function muestra_textarea ()
{
	if (document.getElementById('acepto').checked != false){
	document.getElementById('div_textarea').innerHTML = '<p>Consulta</p><textarea name="consulta" id="consulta" cols="4" rows="6"></textarea> <a class="enviar" href="javascript:enviar_contacto(); ">Enviar</a>';
	}else
	{
	document.getElementById('div_textarea').innerHTML = "";	
	}
}

function aviso_legal()
{
	ddrivetip('<div id="aviso_legal"></div>');
	var con_aviso_legal= new XHConn();
	var fin_aviso_legal = function (oXML) { document.getElementById('aviso_legal').innerHTML = oXML.responseText; };	
	con_aviso_legal.connect('aviso_legal.php', 'GET', '', fin_aviso_legal);
}

function muestra_tooltip(ref)
{
	ddrivetip('<div id="aviso_legal"></div>');
	var con_muestra_tooltip= new XHConn();
	var fin_muestra_tooltip = function (oXML) { document.getElementById('aviso_legal').innerHTML = oXML.responseText; };	
	con_muestra_tooltip.connect('muestra_imagen.php', 'GET', 'ref='+ref, fin_muestra_tooltip);
}


/*----------------Carrito------------------*/
var tiempo_retardo;

/*----------------------------EDICIÓN MANUAL DE UNIDADES---------------------------*/
function edicion_manual_unidades( evento , idproducto, precio )
{
	
	var charCode = ( evento.which ) ? evento.which : event.keyCode
	if ( charCode > 47 && charCode < 58  )
	{
		clearTimeout(tiempo_retardo);
		tiempo_retardo = setTimeout('ejecuta_cambio_unidades('+idproducto+','+precio+');',750);

		return true;
	}
	else
	{
		return false; 
	}
}

/*-------------------------EJECUTA CAMBIO UNIDADES AFECTA AL INPUT ------------------------------*/
function ejecuta_cambio_unidades( idproducto, precio )
{
	var con_edicion_unidades_carrito = new XHConn();
	var fin_edicion_unidades_carrito = function (oXML) { document.getElementById('importe_producto_'+idproducto).innerHTML = oXML.responseText; recarga_totales();};
	con_edicion_unidades_carrito.connect("_dinamic/actualiza_unidades_carrito.php", "GET", "idproducto="+idproducto+"&unidades="+document.getElementById("unidades_"+idproducto).value+"&precio="+precio , fin_edicion_unidades_carrito);
	recarga_totales();
}


/*-------------------------RECARGA TOTALES------------------------------*/
function recarga_totales( )
{
	var subtotal = 0;
	for (var i = 0; i <= 22; i++)
	{
			subtotal += (document.getElementById("unidades_"+i).value*array_precios[i]); 
	}
	
	//gastos_envio = (subtotal != 0) ? 9.85 : 0;
	
	//total = subtotal + gastos_envio;
	
	total = subtotal;
	
	var con_totaliza = new XHConn();
	var fin_totaliza = function (oXML) { document.getElementById('totales').innerHTML = oXML.responseText; };
	con_totaliza.connect("_dinamic/totaliza.php", "GET", "total="+total , fin_totaliza);
	
	

}

//Lee el formulario que queremos enviar y prepara una cadena para enviar por ajax
function prepara_envio( formulario ) {
	var cadena = "";
	var num_elementos = document.getElementById(formulario).length;
	for( var i=0; i<num_elementos; i++ ) {
		
		if( document.getElementById(formulario).elements[i].type != "checkbox" && document.getElementById(formulario).elements[i].type != "radio" ) {
			cadena += i>0 ? "&" : "";
			cadena += document.getElementById(formulario).elements[i].name + "=" + document.getElementById(formulario).elements[i].value.replace("&", "%26");
		}
		else {
			if ( document.getElementById(formulario).elements[i].checked == true ) {
				cadena += i>0 ? "&" : "";
				cadena += document.getElementById(formulario).elements[i].name + "=" + document.getElementById(formulario).elements[i].value.replace("&", "%26");
			}
		}
	}
	return cadena;
}


function enviar_contacto(){
	var nombre = document.getElementById('nombre').value;
	var direccion = document.getElementById('direccion').value;
	var cp = document.getElementById('cp').value;		
	var poblacion = document.getElementById('poblacion').value;			
	var provincia = document.getElementById('provincia').value;	
	var telefono = document.getElementById('telefono').value;	
	var email = document.getElementById('email').value;	
	var consulta = document.getElementById('consulta').value;	
	
	if  ( nombre != "" && direccion != "" && cp != "" || poblacion != "" && provincia != "" && telefono != "" && email != "" && consulta != ""){
	var con_contacto = new XHConn();
	var fin_contacto = function (oXML) { document.getElementById('pedido').innerHTML = oXML.responseText; pageTracker._trackPageview('/virtual_contacto.php');};
	con_contacto.connect("_dinamic/form_contacto.php", "POST", "nombre="+nombre+"&direccion="+direccion+"&cp="+cp+"&provincia="+provincia+"&poblacion="+poblacion+"&telefono="+telefono+"&email="+email+"&consulta="+consulta, fin_contacto);
	} else {
		alert("Debe rellenar todos los campos del formulario para enviar su consulta.");	
	}
}

function enviar_pedido(){
	var nombre = document.getElementById('nombre').value;
	var direccion = document.getElementById('direccion').value;
	var cp = document.getElementById('cp').value;		
	var poblacion = document.getElementById('poblacion').value;			
	var provincia = document.getElementById('provincia').value;	
	var telefono = document.getElementById('telefono').value;	
	var email = document.getElementById('email').value;	
	
	if  ( nombre != "" && direccion != "" && cp != "" && poblacion != "" && provincia != "" && telefono != "" && email != "" ){
		
		var subtotal = 0;
		for (var i = 0; i <= 21; i++)
		{
				subtotal += (document.getElementById("unidades_"+i).value*array_precios[i]); 
		}
		
		//gastos_envio = (subtotal != 0) ? 9.85 : 0;
		
		//total = subtotal + gastos_envio;
		
		total = subtotal;
		
		if ( subtotal != 0)
		{
			var con_pedido = new XHConn();
			var fin_pedido = function (oXML) { document.getElementById('pedido').innerHTML = oXML.responseText; pageTracker._trackPageview('/virtual_pedido.php');};
			
			var cadena = prepara_envio("pedido");
			var con_contacto = new XHConn();
	
			con_pedido.connect("_dinamic/form_pedido.php", "POST", cadena, fin_pedido);
			
		}
		else
		{
			alert("Debes seleccionar algún producto para generar un pedido.");	
		}
		
	
	} else {
		alert("Debe rellenar todos los campos de contacto para procesar su pedido.");	
	}
}
