function activarDistribuidor() {
//Si es distribuidor
	var codDist = document.getElementById('Distribuidor');
	var pagaDist = document.getElementById('eligeDist');
	var cuadro = document.getElementById('cuadroDist');
	var formulario = document.usuario;
	if(codDist.value > 30000 && codDist.value < 40000) {
		pagaDist.style.display = '';
		cuadro.style.border = "solid 1px #999";
	}else{
		pagaDist.style.display = 'none';
		cuadro.style.border = 'none';
	}
}
function calcularTotal() { //Calcula el total del pedido
	cadTotalUnitario = new String();
	//var iva = parseFloat("0." + document.getElementById('importIva').innerHTML);
	var iva = 0.18;
	var subTotal2 = 0;
	var subTotal = 0;
	var instalacion = 0;
	var envio = 0;
	var peso = 0;
	var pesoprecio = 0;
	var toltalCampos = 37; //Este es el nº de campos que se van a calcular
	for(i=1; i<=toltalCampos; i++) {
		if(document.getElementById("checkbox" + i).checked) {
			cadTotalUnitario = document.getElementById("Total" + i).innerHTML;
			cadTotalUnitario = cadTotalUnitario.replace(",", ".");
			totalUnitario= parseFloat(cadTotalUnitario.substring(0, cadTotalUnitario.indexOf("€")));
			if(esInstalacion(i)) {
				instalacion += totalUnitario;
			}else{
				subTotal += totalUnitario;
			}
			peso += pesar(i, peso);
		}
	}
	if(peso > 2) { peso += 0.25 }
	if(document.getElementById("Provincia_de_entrega").value == "") {
		valProv = document.getElementById("Prov").value;
	}else{
		valProv = document.getElementById("Provincia_de_entrega").value;
	}
	switch(valProv) {
		case 'Islas Baleares':
			if(peso <= 2) { pesoprecio = 13 }
			if(peso > 2) {
				contador = 0;
				for (bucle=2; bucle<=peso; bucle+=2) {
					contador++;
					pesoprecio = (contador * 5) + 13;
				}
			}
			document.getElementById('gCanarias').style.visibility = 'hidden';
			document.getElementById("reemb").style.visibility = 'visible';
			document.getElementById('Canti').innerHTML = convertir2Decimales(rounddecimal(pesoprecio));
			document.usuario.Agencia[1].value = convertir2Decimales(rounddecimal(pesoprecio)) + ' Agencia';
			document.getElementById('Agen').innerHTML = 'Agencia (48H*)';
			if(document.getElementsByName('Forma_de_pago')[0].checked) { document.getElementsByName('Forma_de_pago')[3].checked = true; }
			document.getElementById("reemb").style.visibility = 'hidden';
			break;
		case 'Las Palmas':
		case 'Santa Cruz de Tenerife':
		case 'Ceuta':
		case 'Melilla':
		case 'Andorra':
			if (peso <= 1) { pesoprecio = 11 }
			if (peso > 1) {
				contador = 0;
				for (bucle=1;bucle<=peso;bucle+=1) {
					contador++;
					pesoprecio = (contador * 5) + 11;
				}
			}
			document.getElementById('Canti').innerHTML = convertir2Decimales(rounddecimal(pesoprecio));
			document.usuario.Agencia[1].value = convertir2Decimales(rounddecimal(pesoprecio)) + ' Agencia';
			document.getElementById("iva").innerHTML = '0,00 €';
			document.getElementById('Agen').innerHTML = 'Agencia (72H*)';
			document.getElementById('gCanarias').style.visibility = 'visible';
			if(document.getElementsByName('Agencia')[0].checked) { document.getElementsByName('Agencia')[1].checked = true; }
			document.getElementById("idAgencia").style.visibility = 'hidden';
			if(document.getElementsByName('Forma_de_pago')[0].checked) { document.getElementsByName('Forma_de_pago')[3].checked = true; }
			document.getElementById("reemb").style.visibility = 'hidden';
			break;
		default:
			if (peso <= 5) { pesoprecio = 7 }
			if (peso > 5) {
				contador = 0;
				for (bucle=5;bucle<=peso;bucle+=5) {
					contador++;
					pesoprecio = (contador * 4) + 7;
				}
			}
			document.getElementById("idAgencia").style.visibility = 'visible';
			document.getElementById("reemb").style.visibility = 'visible';
			document.getElementById('gCanarias').style.visibility = 'hidden';
			document.getElementById('Canti').innerHTML = convertir2Decimales(rounddecimal(pesoprecio));
			document.usuario.Agencia[1].value = convertir2Decimales(rounddecimal(pesoprecio)) + ' Agencia';
			document.getElementById('Agen').innerHTML = 'Agencia (24H*)';
	}
	if(esDigital()) {//Registros digitales
		if(document.getElementsByName('Forma_de_pago')[0].checked) { document.getElementsByName('Forma_de_pago')[3].checked = true; }
		document.getElementById("reemb").style.visibility = 'hidden';
	}
	if (document.getElementsByName('Forma_de_pago')[0].checked) {
		document.getElementsByName('Agencia')[1].checked = true;
		document.getElementById('Agen').innerHTML = 'Agencia Reembolso';
	}
	if (document.getElementsByName('Agencia')[1].checked) {
		if(peso > 0) {
			envio = document.getElementById('Canti').innerHTML;
		}else{
			envio = 0;
		}
	}
//Subtotal
	document.getElementById("subTotal").innerHTML = convertir2Decimales(subTotal + instalacion);
	if(subTotal > 0) {
		subTotal2 = subTotal;
		subTotal2 = rounddecimal(parseFloat(subTotal2) + parseFloat(envio));
		document.getElementById("cantid").innerHTML = convertir2Decimales(parseFloat(envio));
	}else{
		document.getElementById("cantid").innerHTML = "0,00 €";
	}
//Oferta
	if(document.getElementById('oferta').value == 20) {
		varimponible = parseFloat(subTotal) - parseFloat(subTotal) * 0.2 + instalacion + parseFloat(envio);
		document.getElementById("imponible").innerHTML = convertir2Decimales(rounddecimal(varimponible));
		document.getElementById('ofertacantidad').innerHTML = '-' + convertir2Decimales(rounddecimal(parseFloat(subTotal) * 0.2));
	}else if(document.getElementById('promocion').value == 1) {
		var DescPromo = document.getElementById('DescPromo').innerHTML.replace(",", ".");
		promoporcentaje = parseFloat(DescPromo.substring(0, DescPromo.indexOf("%")));
		varimponible = parseFloat(subTotal) - (parseFloat(subTotal) * promoporcentaje / 100) + instalacion + parseFloat(envio);
		document.getElementById("imponible").innerHTML = convertir2Decimales(rounddecimal(varimponible));
		if(promoporcentaje > 0) {
			document.getElementById('promodescuento').innerHTML = '-' + convertir2Decimales(rounddecimal(parseFloat(subTotal) * promoporcentaje / 100));
		}else{
			document.getElementById('promodescuento').innerHTML = convertir2Decimales(rounddecimal(parseFloat(subTotal) * promoporcentaje / 100));
		}
	}else{
		varimponible = parseFloat(subTotal2) + instalacion;
		document.getElementById("imponible").innerHTML = convertir2Decimales(varimponible);
	}
//IVA
	if(valProv != 'Las Palmas' && valProv != 'Santa Cruz de Tenerife' && valProv != 'Ceuta' && valProv != 'Melilla' && valProv != 'Andorra') {
		document.getElementById("iva").innerHTML = convertir2Decimales(rounddecimal(varimponible * iva));
		document.getElementById("totalTotal").innerHTML = convertir2Decimales(rounddecimal(varimponible * (iva + 1)));
	}else{
		document.getElementById("iva").innerHTML = "0,00 €";
		document.getElementById("totalTotal").innerHTML = convertir2Decimales(rounddecimal(varimponible));
	}
	document.getElementById("baseImponible").value = document.getElementById("imponible").innerHTML;
	document.getElementById("totalIva").value = document.getElementById("iva").innerHTML;
	document.getElementById("totalRegistro").value = document.getElementById("totalTotal").innerHTML;
}
function cambiarEjemplo(valor) {
	if (valor == 'american') {
		document.images['ejemplo'].src = "../comun/imagenes/american.jpg";
	}else{
		document.images['ejemplo'].src = "../comun/imagenes/mastercard.jpg";
	}
}
function convertir2Decimales(numero) { //rellena a 0 lo que falte hasta llenar los 2 decimales
	numero= String(numero);
	numero = numero.replace('.',',');
	indice = numero.indexOf(',');
	if(indice == -1) {
		return numero + ',00 €';
	}else{
		while (indice + 3 > numero.length)
			numero = numero + '0';
	}
	return numero + ' €';
}
function esDigital() {
	if( (document.getElementById('checkbox1').checked) || (document.getElementById('checkbox7').checked) || (document.getElementById('checkbox13').checked) || (document.getElementById('checkbox19').checked) || (document.getElementById('checkbox24').checked) || (document.getElementById('checkbox27').checked) || (document.getElementById('checkbox32').checked) || (document.getElementById('checkbox35').checked) || (document.getElementById('checkbox36').checked) || (document.getElementById('checkbox37').checked) ) {
		return true;
	}else{
		return false;
	}
}
function esFecha(cadena) {
	var cadena, resultado ;
	var cadena = new Date(cadena);
	isNaN(cadena)? resultado=false : resultado=true ;
	return resultado ;
}
function esInstalacion(i) {
	if(i==6 || i==12 || i==18 || i==23 || i==26 || i==31 || i==34) {//Instalación y puesta en marcha por distribuidor
		return true;
	}else{
		return false;
	}
}
function longtext(ident) {
	if(ident == 'tlf') { taman = 9; }
	if(ident == 'cp') { taman = 4; }
	if(document.getElementById(ident).value.length < taman) { document.getElementById(ident).value = ""; }
}
function pesar(i, peso) {
	switch(i) {
		case 2:
		case 3:
		case 4:
		case 5: peso += 0.26; break//ContaSol
		case 8:
		case 9:
		case 10:
		case 11: peso += 0.26; break//FactuSol
		case 14:
		case 15:
		case 16:
		case 17: peso += 0.26; break//NominaSol
		case 20:
		case 21:
		case 22: peso += 0.26; break//MovilSol
		case 25: peso += 0.16; break//TPVsol
	}
	return peso;
}
function validar(frm) {
	calcularTotal();
	var validarmsn = "Falta por introducir los siguientes datos:\n\n";
	var prod;
	frm.detalle_compra.value = '';
	for(prod=0; prod<frm.length; prod++) {
		if(frm[prod].id.indexOf('checkbox')==0) {
			if (frm[prod].checked) {
				frm.detalle_compra.value += frm[prod].name + ': ' + frm[prod].value + '\n';
			}
		}
	}
//valida el detalle de la compra
	if(frm.detalle_compra.value.length < 1) { validarmsn += "Productos deseados.\n"; }
//Datos de cliente
	if(frm.Nombre.value.length < 3) { validarmsn += "Nombre/razón social.\n"; }
	if(frm.NIF.value.length < 7) { validarmsn += "NIF.\n"; }
	if(frm.Domicilio.value.length < 3) { validarmsn += "Domicilio.\n"; }
	if(frm.CPostal.value.length < 4) { validarmsn += "Código postal.\n"; }
	if(frm.Provincia.value.length < 3) { validarmsn += "Provincia.\n"; }
	if(frm.Poblacion.value.length < 3) { validarmsn += "Población.\n"; }
	if(frm.Telefono.value.length < 8) { validarmsn += "Teléfono.\n"; }
	if(frm.Persona_de_contacto.value.length < 3) { validarmsn += "Persona de contacto.\n"; }
	if(frm.Agencia[0].checked){
		if(frm.Agencia_cliente.value.length < 3) { validarmsn += "Agencia deseada.\n"; }
		if(frm.Tlf_Agencia_cliente.value.length < 8) {validarmsn += "Teléfono de su agencia\n"; }
	}
	if(frm.Email.value.length < 5) { validarmsn += "E-Mail\n"; }
//Domiciliacion bancaria
	if (frm.Forma_de_pago[3].checked) {
		if(frm.Entidad.value.length < 4) { validarmsn += "Entidad bancaria.\n"; }
		if(frm.Oficina.value.length < 4) { validarmsn += "Oficina bancaria.\n"; }
		if(frm.DC.value.length < 2) { validarmsn += "Dígito de Control.\n"; }
		if(frm.Cuenta.value.length < 10) { validarmsn += "Cuenta bancaria.\n"; }
		frm.cuentabancaria.value = frm.Entidad.value + '-' + frm.Oficina.value + '-' + frm.DC.value + '-' + frm.Cuenta.value;
		frm.Numero_tarjeta.value = '';
		frm.Fecha_expiracion.value = '';
		frm.Numero_posterior.value = '';
	}
//Tarjeta de credito
	if(frm.Forma_de_pago[4].checked) {
		frm.Nombre_Banco.value = '';
		frm.cuentabancaria.value = '';
		var radios = document.getElementsByName('Tipo_tarjeta');
		var chequeado = false;
		for(var i=0; i<radios.length; i++) {
			if(radios[i].checked) { chequeado = true; }
		}
		if(chequeado == false) { validarmsn += "Seleccionar su tarjeta.\n"; }
		if(document.getElementsByName('Tipo_tarjeta')[2].checked) {
			if(frm.Numero_tarjeta.value.length != 15) { validarmsn += "Número de su tarjeta.\n"; }
			if(frm.Numero_posterior.value.length != 4) { validarmsn += "Código de validación de su tarjeta.\n\tPuede fijarse en la imagen de ejemplo para saber cual es su código.\n"; }
		}else{
			if(frm.Numero_tarjeta.value.length != 16) { validarmsn += "Número de su tarjeta.\n"; }
			if(frm.Numero_posterior.value.length != 3) { validarmsn += "Código de validación de su tarjeta.\n\tPuede fijarse en la imagen de ejemplo para saber cual es su código.\n"; }
		}
		if (frm.Fecha_expiracion.value.length != 5) { validarmsn += "Fecha (mm/aa) de expiración de su tarjeta.\n"; }
	}
	var codDist = document.getElementById('Distribuidor');
	if( (codDist.value > 30000 && codDist.value < 40000) && (!frm.Paga[0].checked && !frm.Paga[1].checked) && (frm.Forma_de_pago[3].checked || frm.Forma_de_pago[4].checked) ) { validarmsn += "Forma de pago, perteneciente a: (Cliente o Distribuidor).\n"; }
	if(validarmsn.length > 46) {
		alert(validarmsn);
		return false;
	}else{
		if(document.getElementById('promocion').value == 1 && document.getElementById('DescPromo').innerHTML != '0 %') {
			document.getElementById('CodPreRegistro').value = 'CP:' + document.getElementById('CodPreRegistro').value;
		}else{
			document.getElementById('CodPreRegistro').value = '';
		}
		var codDist = document.getElementById('Distribuidor');
		if( (codDist.value > 30000 && codDist.value < 40000) && (frm.Paga[0].checked || frm.Paga[1].checked) ) {
			if(frm.Paga[0].checked) { pagador = 'Cliente'; }
			if(frm.Paga[1].checked) { pagador = 'Distribuidor'; }
			frm.Observaciones.value += "\nLos datos de pago son del: " + pagador;
		}
		return true;
	}
}
function rounddecimal(numero) { //Redondea a 2 decimales
	var original = parseFloat(numero);
	var result = String(Math.round(original  *100) / 100);
	return result;
}
function ventanaSecundaria(URL) {
	window.open("../comun/imagenes/mostrar.php?imagen=" + URL, "Aplisoft", "width=480, height=350, top=" + (screen.height/2-175) + ", left=" + (screen.width/2-240) + ", dependent=yes, directories=no, hotkeys=no, location=no, menubar=no, personalbar=no, resizable=NO, scrollbars=NO, status=no");
}
