// JavaScript Document+

var PRODUCTOS;
var CESTA;
var CANTIDAD;

function init(){
	MM_preloadImages('../img/m_1b.gif','../img/m_7b.gif','../img/m_3b.gif');
	PRODUCTOS = document.formu.P_productos;
	CESTA = document.formu.P_cesta;
	CANTIDAD = document.formu.P_cantidad;
	document.formu.IMPORTE.value=0;
	document.formu.PAIS.selectedIndex=0;
	document.formu.FORMA_PAGO.selectedIndex=0;
}

function compra(){
	if(PRODUCTOS.selectedIndex==-1)return (false);
	
	idProducto=PRODUCTOS.selectedIndex+'';
	precio=PRODUCTOS[PRODUCTOS.selectedIndex].value;
	unidades=CANTIDAD[CANTIDAD.selectedIndex].value;

	
	descripcion='DVD Sem. Santa - '+unidades+' ud.';
	
	if(idProducto.length<2)idProducto='0'+idProducto;
	valor=idProducto+'x'+unidades+'-'+precio;

	addOption(CESTA,descripcion,valor);

	calculaPrecioTotal();
}

function elimina(){
	if(CESTA.selectedIndex>0){
		deleteOption(CESTA,CESTA.selectedIndex);
		calculaPrecioTotal();
	}else{
		alert('Por favor, seleccione el producto que desea eliminar.');
	}
}

function calculaPrecioTotal(){

	subtotal=0;
	for(i=1; i<CESTA.length; i++){
		miCodigo=CESTA[i].value;
		misUnidades=miCodigo.substr(3,1);
		miPrecio=parseInt(miCodigo.substr(5,miCodigo.length));
		subtotal+=miPrecio*misUnidades;
	}
	subtotal=subtotal/100;
	subtotal=subtotal+'';
	if(subtotal.length-subtotal.indexOf('.')==2){	subtotal=subtotal+'0'	}
	document.formu.IMPORTE.value=subtotal;
}


function noVISA(obj){
	alert('El pago con tarjeta VISA no está actualmente operativo.\nPor favor, seleccione como método de pago \"TRANSFERENCIA\".');
	obj.checked=false;
	return false;
}


//--------------------------------------------------------------------------------
function especificarPais(obj){
	if(obj.selectedIndex==1){
		MM_showHideLayers('pais_2','','show');
	}else{
		MM_showHideLayers('pais_2','','hide');
	}
}


function chngFP(miValor){
	FORMA_PAGO=document.formu.FORMA_PAGO;
	
	if(miValor==FORMA_PAGO.value){return false}
	
	capaPago=MM_findObj('datosPago');
	capaVisa=MM_findObj('pagoVisa');
	capaTransferencia=MM_findObj('pagoTransferencia');

	if(miValor=="Visa"){
		capaPago.innerHTML=capaVisa.innerHTML;
		chngCaducidad();
	}
	if(miValor=="Transferencia"){
		capaPago.innerHTML=capaTransferencia.innerHTML;
	}
	
	FORMA_PAGO.value=miValor;

}


function chngCaducidad(){
	with(document.formu){
		TARJETA_CAD.value=mianyo.options[mianyo.selectedIndex].value+''+mimes.options[mimes.selectedIndex].value;
	}
}


function procesarDatos(){
	//el formu ya esta validado

	//   INTRODUZCO DATOS EN EL FORMULARIO DE PEDIDO
	document.Pedido.NOMBRE.value=document.formu.NOMBRE.value;
	document.Pedido.APELLIDOS.value=document.formu.APELLIDOS.value;
	document.Pedido.DIRECCION.value=document.formu.DIRECCION.value;
	document.Pedido.POBLACION.value=document.formu.POBLACION.value;
	document.Pedido.PROVINCIA.value=document.formu.PROVINCIA.value;
	document.Pedido.CP.value=document.formu.CP.value;
	document.Pedido.TELEFONO.value=document.formu.TELEFONO.value;
	document.Pedido.PAIS.value=document.formu.PAIS.value;
	if(document.formu.PAIS.selectedIndex==0){
		document.Pedido.PAIS.value=document.formu.PAIS.value;
	}else{
		document.Pedido.PAIS.value=document.formu.OTRO_PAIS.value;
	}
	document.Pedido.EMAIL.value=document.formu.EMAIL.value;
	document.Pedido.FORMA_PAGO.value=document.formu.FORMA_PAGO.value;
	document.Pedido.IMPORTE_TOTAL.value=document.formu.IMPORTE.value;

	//  CALCULO EL NUMERO DE PRODUCTOS
	deuvedes=new Array();
	for(i=0; i<11; i++){
		deuvedes[i]=0;
	}
	for(i=1; i<CESTA.length; i++)
	{
		miCodigo=CESTA[i].value;
		miProducto=parseInt(miCodigo.substr(0,2),10);
		misUnidades=parseInt(miCodigo.substr(3,1));
		deuvedes[miProducto]+=misUnidades;
	}
	with(document.Pedido){
		 DVD_0.value=deuvedes[0];
		 DVD_1.value=deuvedes[1];
		 DVD_2.value=deuvedes[2];
		 DVD_3.value=deuvedes[3];
		 DVD_4.value=deuvedes[4];
		 DVD_5.value=deuvedes[5];
		 DVD_6.value=deuvedes[6];
		 DVD_7.value=deuvedes[7];
		 DVD_8.value=deuvedes[8];
		 DVD_9.value=deuvedes[9];
	 	 DVD_10.value=deuvedes[10];
	}

	document.Pedido.submit();
}

function valida(){
	with (document.formu){

			if(NOMBRE.value==""){
				alert("Por favor, rellene el campo \"NOMBRE\"");
				marcar(NOMBRE);
				return false;
			}
			if(APELLIDOS.value==""){
				alert("Por favor, rellene el campo \"APELLIDOS\"");
				marcar(APELLIDOS);
				return false;
			}
			if(DIRECCION.value==""){
				alert("Por favor, rellene el campo \"DIRECCION\"");
				marcar(DIRECCION);
				return false;
			}
			if(POBLACION.value==""){
				alert("Por favor, rellene el campo \"POBLACION\"");
				marcar(POBLACION);
				return false;
			}
			if(PROVINCIA.value==""){
				alert("Por favor, rellene el campo \"PROVINCIA\"");
				marcar(PROVINCIA);
				return false;
			}
			if(CP.value==""){
				alert("Por favor, rellene el campo \"CÓDIGO POSTAL\"");
				marcar(CP);
				return false;
			}
			if(TELEFONO.value==""){
				alert("Por favor, rellene el campo \"TELEFONO\"");
				marcar(TELEFONO);
				return false;
			}
			if(PAIS.selectedIndex==1 && OTRO_PAIS.value==""){
				alert("Por favor, rellene el campo \"PAIS\"");
				marcar(OTRO_PAIS);
				return false;
			}
			e=EMAIL.value;
			if(e==""){
				alert("Por favor, rellene el campo \"EMAIL\"");
				marcar(EMAIL);
				return false;
			}
			if(e.indexOf("@")==-1 || e.indexOf(".")==-1){
				alert("Por favor, rellene el campo \"EMAIL\" con una dirección correcta");
				marcar(EMAIL);
				return false;
			}

			// FORMA DE PAGO
			if(FORMA_PAGO.value==""){
				alert("Por favor, seleccione una \"FORMA DE PAGO\"");
				return false;
			}
							
			// PEDIDO
			if(P_cesta.length<2){
				alert("Por favor, añada un producto a la cesta");
				return false;
			}
	}
	procesarDatos();
}

function marcar(campo){
	campo.focus();
	//campo.select();
	campo.className="input2";
}

function addOption(selectObject,optionText,optionValue) {
    var optionObject = new Option(optionText,optionValue)
    var optionRank = selectObject.options.length
    selectObject.options[optionRank]=optionObject
}

function deleteOption(selectObject,optionRank) {
    if (selectObject.options.length!=0) { selectObject.options[optionRank]=null }
}
