// enviar formulario
function submitForm(theForm) {
	//alert(theForm);
	myForm = document.getElementById(theForm);
	myForm.submit();
}


//HIDE DATOS DE ENVIO
function displayLayer(displayingLayer,checkBoxItem) {
	
	if(theForm != null) {
	
		theLayer = document.getElementById(displayingLayer);
	
	
		if(checkBoxItem.checked == true) {
			theLayer.style.display = "none";
		}
		else {
			theLayer.style.display = "block";
		}

	
		/*Explorer FIX*/
		if (navigator.appName.indexOf("Explorer") > 0) {//es EXPLORER
		
			var relocItems = new Array("footer","footChurch");
		
			for (var i=0; i<relocItems.length; i++) {
			
				var relocItem = document.getElementById(relocItems[i]);
				relocItem.style.bottom = 1;
				relocItem.style.bottom = 0;
			}
		}
		
	}
	
}  


//ADD PRODDUCT

//Init Vars
var productos = new Array();

//Get Everything
function setUpForm() {
	
	theForm = document.getElementById("ventaDirecta");
	
	if(theForm != null) {
		
		theForm.onreset = function(){
			pvp_total = 0;
			document.getElementById("totalPrice").innerHTML = pvp_total.toString().replace(".", ",")+"€";
		}
		
		formInputs = theForm.getElementsByTagName("INPUT");
		//alert(formInputs.length);
	
		//Obtengo los id de los INPUT con productos 
		for (var i=0; i<formInputs.length; i++) {
		
			//Productos
			if(formInputs[i].id.substr(0,9) == "producto_" && formInputs[i].className == "checkBox") {
				var product = formInputs[i].id.slice(9);
				productos.push(product);
				//alert("producto = " + product);
			}
		}

	//alert(productos);
	recalcular_total();
	}
	

}

setUpPage = function() {
	//alert ("settingUp!!");
	setUpForm();
	displayLayer("envio",document.getElementById("datosEnvio"));
} 

window.onload = setUpPage;


function seleccionar_producto(producto, seleccionado) {
	if (seleccionado == true)
		eval("theForm." + producto + "_cantidad.value = '1'");
	else
		eval("theForm." + producto + "_cantidad.value = ''");

	recalcular_total();
}

function recalcular_total() {
	
	pvp_total = 0;

	for (var i=0; i<= productos.length - 1; i++) {
		nombre = eval("theForm.producto_" + productos[i] + "_nombre.value");
		precio = eval("theForm.producto_" + productos[i] + "_precio.value");
		cantidad = eval("theForm.producto_" + productos[i] + "_cantidad.value");
		
		if (!isNaN(cantidad) && cantidad > 0) {
			eval("theForm.producto_" + productos[i] + ".checked = true");
			
			subtotal = cantidad * precio;
			//alert("subtotal = " + subtotal);
			eval("theForm.producto_" + productos[i] + "_precioTotal.value =" + subtotal);			
			
			pvp_total = pvp_total - (-subtotal);
			
			
		}
		else
			eval("theForm.producto_" + productos[i] + ".checked = false");
	}

	pvp_total = Math.round(pvp_total * 100) / 100;

	theForm.total.value = pvp_total.toFixed(2).replace(".", ",");
	document.getElementById("totalPrice").innerHTML = pvp_total.toString().replace(".", ",")+"€";
}
