// JavaScript Document
function popUp(url, ancho, alto){
		parametros="width="+ ancho +", height="+ alto +",left=150,top=150, resizable=no, menubar=no,toolbar=no,directories=no,location=no,scrollbars=no,status=no";
		window.open(url,"popup",parametros);
}

function viewHide(id, nombre_formulario) {
	if(nombre_formulario != "") {
		var i;
		for(i=0;i<nombre_formulario.length;i++) { if(id != nombre_formulario[i].value) { document.getElementById(nombre_formulario[i].value).style.display = 'none'; } }
	}
	if(document.getElementById(id).style.display == 'block') { document.getElementById(id).style.display = 'none'; }
	else { document.getElementById(id).style.display = 'block'; } 
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validarFrm() {
	if(!document.frm['nombre'].value) {
		alert('El campo "Nombre" es obligatorio.');
		document.frm['nombre'].focus();		
	}
	else if(!document.frm['telefono'].value) {
		alert('El campo "Teléfono" es obligatorio.');
		document.frm['telefono'].focus();		
	}
	else if(!document.frm['consulta'].value) {
		alert('El campo "Consulta" es obligatorio.');
		document.frm['consulta'].focus();		
	}	
	else if(document.frm['politica'].checked == false){
		alert('Debe aceptar la política de privacidad.');
		document.frm['politica'].focus();
	}
	else { 
		document.frm['enviar_frm'].value = 1;
		document.frm.submit();
	}
	return false;
}

function validarFrmPre() {
	if(!document.frm_presupuesto['nombre'].value) {
		alert('El campo "Nombre" es obligatorio.');
		document.frm_presupuesto['nombre'].focus();		
	}
	else if(!document.frm_presupuesto['email'].value) {
		alert('El campo "E-mail" es obligatorio.');
		document.frm_presupuesto['email'].focus();		
	}
	else if(document.frm_presupuesto['politica'].checked == false){
		alert('Debe aceptar la política de privacidad.');
		document.frm_presupuesto['politica'].focus();
	}
	else { 
		var msg = 'Se va a proceder al envío del presupuesto. ¿Está seguro de continuar?';
		if(confirm(msg)) {
			document.frm_presupuesto['enviar_frm'].value = 1;
			document.frm_presupuesto.submit();
		}	
	}
	return false;
}

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

function calcularPrecioProducto(id_unidades, id_precio, precio){ document.frm[id_precio].value = 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 actualizarUnidades(input){
	document.frm['input_unidades'].value = input;
	document.frm.action = '/solicitud-presupuesto/actualizar-unidades/';
	document.frm.submit();	
}

function borrarRegistro(id, indice){
	document.frm['productos_id'].value = id;
	document.frm['indice_reg'].value = indice;
	document.frm.action = '/solicitud-presupuesto/borrar-registro/';
	document.frm.submit();	
}

function limpiarSeleccion(tipo){
	var anchors = document.getElementsByTagName('a');	
	for(var i = 0; i < anchors.length; i++) {	if(anchors[i].getAttribute('rel') == tipo) { anchors[i].className=''; }	}
}

function seleccionarColor(color, id, hex){
	if(document.frm['color'].value == color){		
		document.frm['hexadecimal'].value = '';
		document.frm['color'].value = '';
		document.getElementById(id).className = '';
	}
	else{
		document.frm['hexadecimal'].value = hex;
		document.frm['color'].value = color;
		limpiarSeleccion('color');
		document.getElementById(id).className = 'color-sel';
	}
}

function seleccionarTalla(talla, id){	
	if(document.frm['talla'].value == talla){
		document.frm['talla'].value = '';
		document.getElementById(id).className = '';
	}
	else{
		document.frm['talla'].value = talla;
		limpiarSeleccion('talla');
		document.getElementById(id).className = 'talla-sel';
	}
}

function agregarProducto(id, url){
	var talla_ok = 0, color_ok = 0;
	// Comprobamos la talla
	if(!document.frm['talla'].value) {
		if(confirm('No ha seleccionado ninguna talla. ¿Le da igual la talla?')) { talla_ok = 1; } 
		else { return false; }	
	}
	else{ talla_ok = 1; }
	// Comprobamos el color
	if(!document.frm['color'].value) {
		if(confirm('No ha seleccionado ningún color. ¿Le da igual l color?')) { color_ok = 1; } 
		else { return false; }
	}
	else{ color_ok = 1; }
	// Enviamos el producto al presupuesto
	if(color_ok == 1 && talla_ok == 1){
		document.frm['productos_id'].value = id;
		document.frm.action = url;
		document.frm.submit();	
	}
}
