// JavaScript Document
function cambiarIdioma(id){
	document.frm_idiomas['idiomas_id'].value = id;
	document.frm_idiomas.submit();
}

function cambiarElemento(ind, tot){
	for(var i = 0; i <= tot; i++){
		document.getElementById('div_con_'+ i).className = i == ind ? 'div_con_vis' : 'div_con_ocu';
		//alert('div_con_'+ i +": "+ document.getElementById('div_con_'+ i).className)
	}	
}

function ampliarImagen(src, ind){
//	alert(document.getElementById('imagen_grande').src);
	document.getElementById('imagen_grande').src = src;		
	document.getElementById('imagen_grande').longdesc = ind;		
}

function reducirImagen(tot){
//	alert(document.getElementById('imagen_grande').src);
	var ind = document.getElementById('imagen_grande').longdesc;
	document.getElementById('div_con_'+ tot).className = 'div_con_ocu';
	document.getElementById('div_con_'+ ind).className = 'div_con_vis';
	document.getElementById('imagen_grande').src = '/imagenes/genericas/pixel.gif';		
}

function cambiarImagen(ind, accion, imagenes){
	/* Tratamos los datos recibidos */
	var array_imgs = imagenes.split('-');
	var nimg = array_imgs.length;	
	var array_vars = document.getElementById('dd_img_txt_'+ ind).innerHTML.split(' ');
	var nvar = array_vars.length;
	
	/* Controlamos los datos que se introducirán */
	if(accion == 'sig'){
		var img_1_act = parseInt(array_vars[1]) + 2;
		var img_2_act = parseInt(array_vars[3]) + 2;
		if(img_1_act > nimg){ img_1_act = img_1_act - nimg; }
		if(img_2_act > nimg){ img_2_act = img_2_act - nimg; }
	}
	else{
		var img_1_act = parseInt(array_vars[1]) - 2;
		var img_2_act = parseInt(array_vars[3]) - 2;
		if(img_1_act <= 0){ img_1_act = img_1_act + nimg; }
		if(img_2_act <= 0){ img_2_act = img_2_act + nimg; }
	}
	
	var txt_new = array_vars[0] +' '+ img_1_act +' '+ array_vars[2] +' '+ img_2_act +' '+ array_vars[4] +' '+ array_vars[5];
	
	/* Cargamos los datos */
	document.getElementById('dd_img_'+ ind +'_'+ array_vars[1]).className = 'dd_img_ocu';
	document.getElementById('dd_img_'+ ind +'_'+ array_vars[3]).className = 'dd_img_ocu';
	document.getElementById('dd_img_'+ ind +'_'+ img_1_act).className = 'dd_img_vis';
	document.getElementById('dd_img_'+ ind +'_'+ img_2_act).className = 'dd_img_vis';
	document.getElementById('dd_img_txt_'+ ind).innerHTML = txt_new;
	
	//alert("imagen 1: "+ array_vars[1] +" => "+ img_1_act +" de "+ nimg +"\nImagen 2: "+ array_vars[3] +" => "+ img_2_act +" de "+ nimg);
	//alert("nvar: "+ nvar);
		
}

function validarFrm() {
	if(!document.frm['nombre'].value) { alert('El campo nombre es obligatorio...'); document.frm['nombre'].focus(); }
	else if(!document.frm['email'].value) { alert('El campo e-mail es obligatorio...');	document.frm['email'].focus(); }
	else if(!document.frm['consulta'].value) { alert('El campo consulta es obligatorio...'); document.frm['consulta'].focus(); }
	else { document.frm['enviar_frm'].value = 1; document.frm.submit(); }
	return false;
}

function validarFrmEve() {
	if(!document.frm['nombre'].value) { alert('El campo nombre es obligatorio...'); document.frm['nombre'].focus(); }
	else if(!document.frm['email'].value) { alert('El campo e-mail es obligatorio...');	document.frm['email'].focus(); }
	else { document.frm['enviar_frm'].value = 1; document.frm.submit(); }
	return false;
}

function validarFrmCompra(msg, msg2) {
	if(!document.frm['nombre'].value){ alert(msg); document.frm['nombre'].focus(); }
	else if(!document.frm['apellidos'].value){ alert(msg); document.frm['apellidos'].focus(); }
	else if(!document.frm['telefono'].value){ alert(msg); document.frm['telefono'].focus(); }
	else if(!document.frm['email'].value){ alert(msg); document.frm['email'].focus(); }
	else if(!document.frm['direccion'].value){ alert(msg); document.frm['direccion'].focus(); }
	else if(!document.frm['cod_postal'].value){ alert(msg); document.frm['cod_postal'].focus(); }
	else if(!document.frm['localidad'].value){ alert(msg); document.frm['localidad'].focus(); }
	else if(!document.frm['provincia'].value){ alert(msg); document.frm['provincia'].focus(); }
	else if(!document.frm['pais'].value){ alert(msg); document.frm['pais'].focus(); }
	else if(document.frm['pago_contra'].checked == false && document.frm['pago_paypal'].checked == false ){ alert(msg2); }
	else { document.frm['enviar_frm'].value = 1; document.frm.submit(); }
	return false;
}

function validar(e, permiso, destino) { 
	var tecla, patron, te;
	tecla = (document.all) ? e.keyCode : e.which; 
	if (tecla == 46 || tecla == 8 || tecla == 0 ) { return true; }
	else if( tecla == 13 ){ if(destino != ''){ eval(destino); } }
	patron = permiso; 
	te = String.fromCharCode(tecla); 
	return patron.test(te); 
}

function calcularPrecioProducto(id_unidades, id_precio, precio){ document.getElementById(id_precio).innerHTML = roundNumber(document.frm[id_unidades].value * precio, 2) + ' €'; }
function roundNumber(num, dec) { return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec); }
function comprobarUnidades(id_unidades, id_precio, precio){ 
	if(!document.frm[id_unidades].value || document.frm[id_unidades].value == 0){ 
		document.frm[id_unidades].value = 1; 
		calcularPrecioProducto(id_unidades, id_precio, precio);
	} 
}

function comprarProducto(msg){ if(confirm(msg)){ document.frm.submit(); } }

