function nuevoAjax()
{
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp = false;
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest != 'undefined') xmlhttp = new XMLHttpRequest();
		}
	}
	return xmlhttp;
}

function cargarImagenes(subcarpeta) {
	var ajax=nuevoAjax();
	ajax.open("GET","inc/articulos.ajax.php?func=img&subcarpeta=" + subcarpeta);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//alert(ajax.responseText);
			if (ajax.responseText != '') {
				imagenes = document.getElementById('imagenes');
				imagenes.length = 0;

				arrayImagenes = ajax.responseText.split(";");

				cont = 0;
				for (i=0;i<arrayImagenes.length;i=i+2) {
					imagenes.length++;
					imagenes.options[cont].value = arrayImagenes[i];
					imagenes.options[cont].text = arrayImagenes[i+1];
					cont++
				}
			}
		}
	}
	ajax.send(null);
}

function agregarQuitarPedido(id) {
	var ajax=nuevoAjax();
	var func;
	if (document.getElementById("ped_"+id).checked == true) func = 'add';
	else func = 'del';
	ajax.open("GET","inc/web/pedidos.ajax.php?func=" + func + "&id=" + id);
	ajax.send(null);
}

function agregarQuitarPedidoFicha(id,func) {
	var ajax=nuevoAjax();
	ajax.open("GET","inc/web/pedidos.ajax.php?func=" + func + "&id=" + id);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			location.href = location.href;
		}
	}
	ajax.send(null);
}

function actualizarLineaPedido(linea,cajas,pales,descuento) {
	var ajax=nuevoAjax();
	ajax.open("GET","inc/web/pedidos.ajax.php?func=upd&linea=" + linea + "&cajas=" + cajas + "&pales=" + pales + "&descuento=" + descuento);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//alert(ajax.responseText);
			if (ajax.responseText != '') {
				arrayDatos = ajax.responseText.split(";");

				document.getElementById("cajas_totales_"+linea).value = arrayDatos[0];
				document.getElementById("precio_total_"+linea).value = arrayDatos[1];
				document.getElementById("total").value = arrayDatos[2] + " €";
				if (descuento != 0) {
					document.getElementById("descuento").value = "-" + arrayDatos[3] + " €";
				}
			}
		}
	}
	ajax.send(null);
}

function cargarClientes(codigo) {
	var ajax=nuevoAjax();
	if (codigo != '') {
		ajax.open("GET","inc/web/pedidos.ajax.php?func=load_cli&codigo=" + codigo);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				//alert(ajax.responseText);
				if (ajax.responseText != '') {
					sel_clientes = document.getElementById('clientes');
					sel_clientes.length = 0;

					sel_clientes.length++;
					sel_clientes.options[0].value = '';
					sel_clientes.options[0].text = 'Seleccione un cliente...';

					arrayClientes = ajax.responseText.split(";");

					cont = 1;
					for (i=0;i<arrayClientes.length;i=i+2) {
						sel_clientes.length++;
						sel_clientes.options[cont].value = arrayClientes[i];
						sel_clientes.options[cont].text = arrayClientes[i] + ' - ' + arrayClientes[i+1];
						cont++
					}
				}
			}
		}
	}
	else {
		ajax.open("GET","inc/web/pedidos.ajax.php?func=save_del&codigo=" + codigo);
		sel_clientes = document.getElementById('clientes');
		sel_clientes.length = 0;

		sel_clientes.length++;
		sel_clientes.options[0].value = '';
		sel_clientes.options[0].text = 'Seleccione un cliente...';
	}
	ajax.send(null);
}

function cargarClientesPR(codigo) {
	var ajax=nuevoAjax();
	if (codigo != '') {
		ajax.open("GET","inc/web/pedidos.ajax.php?func=load_cli_pr&codigo=" + codigo);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				//alert(ajax.responseText);
				if (ajax.responseText != '') {
					sel_clientes = document.getElementById('clientes');
					sel_clientes.length = 0;

					sel_clientes.length++;
					sel_clientes.options[0].value = '';
					sel_clientes.options[0].text = 'Seleccione un cliente...';

					arrayClientes = ajax.responseText.split(";");

					cont = 1;
					for (i=0;i<arrayClientes.length;i=i+2) {
						sel_clientes.length++;
						sel_clientes.options[cont].value = arrayClientes[i];
						sel_clientes.options[cont].text = arrayClientes[i] + ' - ' + arrayClientes[i+1];
						cont++
					}
				}
			}
		}
	}
	else {
		ajax.open("GET","inc/web/pedidos.ajax.php?func=save_del&codigo=" + codigo);
		sel_clientes = document.getElementById('clientes');
		sel_clientes.length = 0;

		sel_clientes.length++;
		sel_clientes.options[0].value = '';
		sel_clientes.options[0].text = 'Seleccione un cliente...';

		location.href = 'pedidos_realizados.php?clientes='+document.getElementById('clientes').value+'&delegados='+document.getElementById('delegados').value;
	}
	ajax.send(null);
}

function guardarCliente(codigo) {
	var ajax=nuevoAjax();
	ajax.open("GET","inc/web/pedidos.ajax.php?func=save_cli&codigo=" + codigo);
	ajax.send(null);
}

function enviarPedido() {
	var ajax=nuevoAjax();
	ajax.open("GET","inc/web/pedidos.ajax.php?func=val_ped");
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//alert(ajax.responseText);
			if (ajax.responseText != '') {
				if (ajax.responseText == 0) {
					alert("No es posible enviar el pedido. No tiene asociado ningún cliente o no contiene productos.");
				}
				else {
					//enviar = confirm("Se va a enviar su pedido a Fedinsa. ¿Desea continuar?");
					//if (enviar == true) location.href = 'save_pedido.php';
					location.href = 'confirmar_pedido.php';
				}
			}
		}
	}
	ajax.send(null);
}

function confirmarPedido(msg,msgno) {
	var id_direccion_envio;
	var fecha;
	var observaciones;
	id_direccion_envio = document.getElementById('id_direccion_envio').value;
	fecha = document.getElementById('fecha_servicio').value;
	observaciones = document.getElementById('observaciones').value;
	var ajax=nuevoAjax();
	ajax.open("GET","inc/web/pedidos.ajax.php?func=con_ped&id_direccion_envio=" + id_direccion_envio + "&fecha_servicio=" + fecha + "&observaciones=" + observaciones);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if (ajax.responseText != '') {
				if (ajax.responseText == 0) {
					alert(msgno);
				}
				else {
					enviar = confirm(msg);
					if (enviar == true) location.href = 'save_pedido.php';
					//location.href = 'confirmar_pedido.php';
				}
			}
		}
	}
	ajax.send(null);
}

function guardarComentarioLinea(linea,comentario) {
	var id_imagen;
	id_imagen = "img_cmt_"+linea;
	var ajax=nuevoAjax();
	ajax.open("GET","inc/web/pedidos.ajax.php?func=save_cmt&linea=" + linea + "&comentario=" + comentario);
	if (comentario != '') {
		opener.document.getElementById(id_imagen).src = 'img/comment_edit_16x16.gif';
	}
	else {
		opener.document.getElementById(id_imagen).src = 'img/comment_16x16.gif';
	}
	window.close();
	ajax.send(null);
}