// JavaScript Document

/**/
var chidden;
//Objeto oNumero
function oNumero(numero) {
//Propiedades 
	this.valor = numero || 0
	this.dec = -1;
//Métodos 
	this.formato = numFormat;
	this.ponValor = ponValor;
	
//Definición de los métodos 
	function ponValor(cad) {
		if (cad =='-' || cad=='+') return
		if (cad.length ==0) return
		if (cad.indexOf('.') >=0)
			this.valor = parseFloat(cad);
		else 
			this.valor = parseInt(cad);
	} 

	function numFormat(dec, miles) {
		var num = this.valor, signo=3, expr;
		var cad = ""+this.valor;
		var ceros = "", pos, pdec, i;
		for (i=0; i < dec; i++)
		ceros += '0';
		pos = cad.indexOf('.')
		if (pos < 0)
			cad = cad+"."+ceros;
		else {
			pdec = cad.length - pos -1;
			if (pdec <= dec) {
				for (i=0; i< (dec-pdec); i++)
					cad += '0';
			} else {
				num = num*Math.pow(10, dec);
				num = Math.round(num);
				num = num/Math.pow(10, dec);
				cad = new String(num);
			}
		}
		pos = cad.indexOf('.');
		if (pos < 0) pos = cad.lentgh;
		if (cad.substr(0,1)=='-' || cad.substr(0,1) == '+') 
		 signo = 4;
		if ( (miles) && (pos > signo) )
			do{
				expr = /([+-]?\d)(\d{3}[\.\,]\d*)/;
				cad.match(expr);
//				cad=cad.replace(expr, RegExp.$1+'.'+RegExp.$2);
				cad=cad.replace(expr, RegExp.$1+','+RegExp.$2);
			}
		while (cad.indexOf(',') > signo)
			if (dec<0) cad = cad.replace(/\./,'')
		if (dec>0)
			if (cad.lastIndexOf('.') > 0) 
//				cad= cad.substr(0,cad.lastIndexOf('.'))+','+cad.substr(cad.lastIndexOf('.')+1,cad.length);
				cad= cad.substr(0,cad.lastIndexOf('.'))+'.'+cad.substr(cad.lastIndexOf('.')+1,cad.length);
		return cad;
	}
}

//----------------------

var ie5 = (document.getElementById && document.all); 
var ns6 = (document.getElementById && !document.all);

var timerID = null;
var opak = 0;
var index = 0;
var mouseX = 5;
var mouseY = 20;

function mozOpacity(){
	if(ns6){
		opacity = opak + 7;
		opak = opacity;
		timerID = setTimeout("mozOpacity()", 70);
		obj.style.MozOpacity = opacity + '%';
	}
}

function stopMozOpacity(){
	if(ns6){
		if(timerID!=null){
			clearTimeout(timerID);
			timerID=null;
		}
		obj.style.MozOpacity = '0%';
		opak = 0;
	}
}

function changeMsg(msg){
	if(ie5){
		obj.style.filter="blendTrans(Duration=1.2)";
		obj.filters[0].Apply();
		obj.style.display = "block";
		obj.filters[0].Play();
		obj.innerHTML = msg;
	}
	else if(ns6){
		obj.style.display = "block";
		obj.innerHTML = msg;
		if (opak < 100){
			mozOpacity();
		}
	}
}

function hideMsg(){
	if(ie5){
		obj.style.display="none";
	}
}

function getMouseMove(e){
	if(ie5){
		obj.style.left = document.body.scrollLeft + event.clientX + mouseX;
		obj.style.top = document.body.scrollTop + event.clientY + mouseY;
	}
	else if(ns6){
		obj.style.left = window.pageXOffset + e.clientX + mouseX;
		obj.style.top = window.pageYOffset + e.clientY + mouseY;
	}
}

function fadingTooltipsInit(object){
	if(ie5 || ns6){
		obj = document.getElementById('div_info');
		if(ie5){
			object.attachEvent("onmouseout", hideMsg);
		}
		else if(ns6){
			object.addEventListener("mouseout", stopMozOpacity, false);
		}
	}
}
/**/


function mostrar_informacion(cad,obj) {
	
	document.onmousemove=getMouseMove;
	fadingTooltipsInit(obj);
	changeMsg(cad);
}

function ajax_init() {
	
	ajaxEngine.registerRequest('sugestions', 'php/sugestions.php');

	ajaxEngine.registerRequest('calendarizacion', 'php/calendarizacion.php');	
	ajaxEngine.registerRequest('cal_guardar', 'php/cal_guardar.php');	

	ajaxEngine.registerRequest('cont_listado', 'php/cont_listado.php');
	
//	ELEMENTOS PARA LOS PEDIDOS
	ajaxEngine.registerRequest('ped_editar_pedido', 'php/pedidos/ped_editar_pedido.php');
	ajaxEngine.registerRequest('ped_comprobar_oferta', 'php/pedidos/ped_comprobar_oferta.php');
	//ajaxEngine.registerRequest('pedidos_guardar_pedido', 'php/pedidos/pedidos_guardar_pedido.php');
	ajaxEngine.registerRequest('pedidos_borrar_pedido', 'php/pedidos/pedidos_borrar_pedido.php');
	ajaxEngine.registerRequest('linea_regalo', 'linea_regalo.php');
	
	ajaxEngine.registerRequest('enviar_datos_acceso', 'enviar_datos_acceso.php');
	ajaxEngine.registerRequest('cambiar_postal', 'cambiar_postal.php');
	ajaxEngine.registerRequest('cambiar_postal_2', 'cambiar_postal_2.php');
	ajaxEngine.registerRequest('ped_comprobar_oferta', 'ped_comprobar_oferta.php');	
	ajaxEngine.registerRequest('cambiar_ayuda', 'cambiar_ayuda.php');	
	ajaxEngine.registerRequest('recordar_clave', 'recordar_clave.php');	
	ajaxEngine.registerRequest('actualizar_datos', 'actualizar_datos.php');	
	ajaxEngine.registerRequest('rellena_datos_cliente', 'rellena_datos_cliente.php');	
	ajaxEngine.registerRequest('caja_ofertas', 'caja_ofertas.php');	
	ajaxEngine.registerRequest('pedido1', 'pedido1.php');
	ajaxEngine.registerRequest('pedido2', 'pedido2.php');
	ajaxEngine.registerRequest('pedido3', 'pedido3.php');
	ajaxEngine.registerRequest('atras_pedido2', 'atras_pedido2.php');
	ajaxEngine.registerRequest('pedido4', 'pedido4.php');
	ajaxEngine.registerRequest('atras_pedido3', 'atras_pedido3.php');
	ajaxEngine.registerRequest('enviar_pedido', 'enviar_pedido.php');
	ajaxEngine.registerRequest('comprobar_postal', 'comprobar_postal.php');
	ajaxEngine.registerRequest('terminar_pedido', 'terminar_pedido.php');
	ajaxEngine.registerRequest('enviar_pedido_paypal', 'enviar_pedido_paypal.php');
	
	ajaxEngine.registerRequest('mostrar_mis_datos', 'mis_datos.php');
	ajaxEngine.registerRequest('cambiar_postal3', 'cambiar_postal3.php');
	ajaxEngine.registerRequest('cambiar_cuenta', 'cambiar_cuenta.php');
	ajaxEngine.registerRequest('guardar_datos', 'guardar_datos.php');
	ajaxEngine.registerRequest('mis_pedidos', 'mis_pedidos.php');
	ajaxEngine.registerRequest('cod_prom','cod_prom.php');
	
// ELEMENTOS PARA CALENDARIO ESPECIAL
	ajaxEngine.registerRequest('find_date', 'date_available.php');
}

function enviar_datos_acceso() {
//	alert("bienvenido");
	String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') }
	
	var usuario= document.getElementById('usuario').value.trim();
	var clave= document.getElementById('clave').value.trim();
	
	ajaxEngine.registerAjaxElement('datos_cliente');
	ajaxEngine.registerAjaxElement('datos_envio');
	ajaxEngine.registerAjaxElement('datos_acceso');
	ajaxEngine.sendRequest('enviar_datos_acceso','usuario='+usuario,'clave='+clave);
	

}

/*function enviar_datos_acceso(){
	
	var usuario= document.getElementById('usuario').value.replace(/'/gi," ");
	var clave= document.getElementById('clave').value.replace(/'/gi," ");
	
	var options = {
		asynchronous : true,
		onComplete: function(){
			//window.location = 'pedido_realizar.php?';	
			alert('hola');
		},
		parameters: 'usuario='+usuario,'clave='+clave
	};
	
	ajaxEngine.registerAjaxElement('datos_cliente');
	ajaxEngine.registerAjaxElement('datos_envio');
	ajaxEngine.registerAjaxElement('datos_acceso');
	ajaxEngine.sendRequest('enviar_datos_acceso',options);	
}*/

function pedidos_copiar_pegar() {
	
	document.getElementById('envio_nombre').value= document.getElementById('nombre').value;
	document.getElementById('envio_direccion').value= document.getElementById('direccion').value;
	document.getElementById('envio_cp').value= document.getElementById('cp').value;
	document.getElementById('envio_poblacion').value= document.getElementById('localidad').value;
	document.getElementById('envio_provincia').value= document.getElementById('provincia').value;
	document.getElementById('envio_telefono').value= document.getElementById('telefono').value;
	
	if (document.getElementById('envio_cp').value!='') {
		document.getElementById('envio_poblacion').disabled= true;
		document.getElementById('envio_provincia').disabled= true;
	}
	
	ajaxEngine.registerAjaxElement('td_tarde');
	ajaxEngine.sendRequest('comprobar_postal','postal='+document.getElementById('envio_cp').value);
}

function cambiar_postal(postal,elem) {
	if(postal.substr(0,2)=='07'){
		$('envio_baleares').innerHTML='Los envíos a Baleares suelen tardar 48-72 horas, por lo que te llegarán unos dos días más tarde de la fecha seleccionada para la entrega.';
	}
	
	ajaxEngine.registerAjaxElement('provincia'+elem);
	ajaxEngine.registerAjaxElement('poblacion'+elem);
	ajaxEngine.registerAjaxElement('td_tarde');
	ajaxEngine.sendRequest('cambiar_postal','postal='+postal,'elem='+elem);
}

function cambiar_postal_2(postal,elem) {
	
	if(postal.substr(0,2)=='07'){
		$('envio_baleares2').innerHTML='Los envíos a Baleares suelen tardar 48-72 horas, por lo que te llegarán unos dos días más tarde de la fecha seleccionada para la entrega.';
	}
	
	ajaxEngine.registerAjaxElement('poblacion2');
	ajaxEngine.registerAjaxElement('provincia2');
	ajaxEngine.registerAjaxElement('td_tarde');
	
	ajaxEngine.sendRequest('cambiar_postal_2','postal='+postal,'elem='+elem);
}

function buscar_producto(linea) {
	
	var opciones="toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=no,width=650,height=350,top=50,left=80";
	buscador= window.open("buscar_producto.php?linea="+linea,"buscador",opciones);
}

function devuelve_buscador_pedidos(id,array_cd,array_valores,subsec) {
	
	var td_alias,td_precio,td_descuento,td_unidades,td_subtotal;
	
	td_precio= opener.document.getElementById('c_precio'+array_cd);
	td_unidades= opener.document.getElementById('c_unidades'+array_cd);
	td_subtotal= opener.document.getElementById('c_subtotal'+array_cd);
	
	var v_valores= new Array;
	v_valores= array_valores.split('**');
	var i;
	var campo;
	
//	alert(array_valores);
	
	//	pertenece a una oferta
	if (parseInt(v_valores[0])==1) {
		opener.document.getElementById('c_oferta'+array_cd).innerHTML= '<input type="button" id="check'+array_cd+'" class="boton_oferta" onmouseover="mostrar_informacion(\''+v_valores[5]+'\',this);" />';
	} else {
		opener.document.getElementById('c_oferta'+array_cd).innerHTML= '<input type="hidden" id="check'+array_cd+'" readonly="" value="" />';
	}
	
	//	obtenemos los datos leídos de la base de datos
	opener.document.getElementById('producto'+array_cd).value= id;
	opener.document.getElementById('aux'+array_cd).value= v_valores[1];

	td_precio.innerHTML= '<input type="hidden" name="precio'+array_cd+'" id="precio'+array_cd+'" value="'+v_valores[2]+'" />'+v_valores[2];
	td_unidades.innerHTML= '<input type="text" style="text-align:right" name="unidades'+array_cd+'" id="unidades'+array_cd+'" value="0" class="form_texto" size="10" onchange="pedidos_mod_cantidad(this.value,\''+array_cd+'\');" />';
	td_subtotal.innerHTML= '<input type="hidden" name="subtotal'+array_cd+'" id="subtotal'+array_cd+'" value="0" />0';
	window.close();
}

/*function pedidos_mod_cantidad(unidades,id) {
	
	var producto= document.getElementById('producto'+id).value;
	var precio= document.getElementById('precio'+id).value;
	var obj= document.getElementById('c_subtotal'+id);
	var descuento= 0;
	
//	alert(((unidades * precio) - ((unidades * precio * descuento)/100)));
	
	var options= {
		asynchronous: true,
		onComplete: function() {
			var aux= document.getElementById('d_oc').innerHTML.split('**');
			
			revisa_hijos(document.getElementById('producto'+id));
			
			switch(parseInt(aux[0])) {
				case 1:
					var obj= document.getElementById('c_subtotal'+id);
					obj.innerHTML= '<input type="hidden" name="subtotal'+id+'" id="subtotal'+id+'" value="'+((unidades * precio) - ((unidades * precio * aux[1])/100))+'" />';
					obj.innerHTML+= ((unidades * precio) - ((unidades * precio * aux[1])/100));
				break;
				case 2:
					var obj= document.getElementById('c_subtotal'+id);
					obj.innerHTML= '<input type="hidden" name="subtotal'+id+'" id="subtotal'+id+'" value="'+((unidades * precio) - ((unidades * precio * aux[1])/100))+'" />';
					obj.innerHTML+= ((unidades * precio) - ((unidades * precio * descuento)/100));

					var ultimo= document.getElementById('count').value;
					linea_regalo(parseInt(ultimo)+1,aux[1]);
				break;
				default:
					var obj= document.getElementById('c_subtotal'+id);
					obj.innerHTML= '<input type="hidden" name="subtotal'+id+'" id="subtotal'+id+'" value="'+((unidades * precio) - ((unidades * precio * descuento)/100))+'" />';
					obj.innerHTML+= ((unidades * precio) - ((unidades * precio * descuento)/100));
				break;
			}
			calcula_total_pedido();
		},
		parameters: "elem="+id+"&producto="+producto+"&unidades="+unidades+"&linea="+id
	};
	
	ajaxEngine.registerAjaxElement('d_oc');
	ajaxEngine.sendRequest('ped_comprobar_oferta',options);
	
}*/

function revisa_hijos(id) {
	
	var form= document.forms[0];
	
	var tabla = document.getElementById('tabla_lineas');
	var cont= 0;
	
	for (i=0; i<form.elements.length; i+=10) {
		if (id.value==form.elements[i+9].value) {
			var navegador = navigator.appName; 
			if (navegador == 'Microsoft Internet Explorer') 
				tabla.deleteRow(cont); 
			else {
				var myrows = tabla.getElementsByTagName('tr');
				tabla.removeChild(myrows[cont]);
			}
			document.getElementById('count').value--;
		}
		cont++;
	}
	
	document.getElementById('del_'+(form.elements.length/10)).style.display= 'block';
	document.getElementById('add_'+(form.elements.length/10)).style.display= 'block';
}

function pedidos_add_linea(linea,id_pedido,id_sec,id_subsec) {

		var newLinea= parseInt(document.getElementById('count').value)+1;
		
		var theTable = document.getElementById('tabla_lineas');
	
		var tr = document.createElement('tr');
		
		tr.id= 'tr'+newLinea;
		
		var td1=document.createElement('td');
		var td2=document.createElement('td');
		var td4=document.createElement('td');
		var td6=document.createElement('td');
		var td7=document.createElement('td');
		var td8=document.createElement('td');
		var td9=document.createElement('td');
		
		
		td1.width= '20';
		td1.align= 'center';
		td1.id= 'c_oferta'+newLinea;
		
		td2.width= '';
		td2.align= 'left';
		td2.id= 'c_producto'+newLinea;
		
		td4.width= '12%';
		td4.align= 'right';
		td4.id= 'c_precio'+newLinea;
		
		td6.width= '12%';
		td6.align= 'right';
		td6.id= 'c_unidades'+newLinea;
		
		td7.width= '12%';
		td7.align= 'right';
		td7.id= 'c_subtotal'+newLinea;
		
		td8.width= '20';
		td8.align= 'center';
		td8.id= 'c_del'+newLinea;
		
		td9.width= '20';
		td9.align= 'center';
		td9.id= 'c_add'+newLinea;
		
		td1.innerHTML= '<input type="hidden" id="check'+newLinea+'" value="" />';
		td2.innerHTML= '<input type="hidden" id="producto'+newLinea+'" name="producto'+newLinea+'" value="" />';
		td2.innerHTML+= '<input type="text" name="aux'+newLinea+'" id="aux'+newLinea+'" size="75" value="" readonly="readonly" class="form_texto" />';
		td2.innerHTML+= '&nbsp;<input id="bus_'+newLinea+'" type="button" value="" onclick="buscar_producto(\''+newLinea+'\');" class="boton_buscar" title="Buscar" />';
		
		td4.innerHTML= '<input type="hidden" name="precio'+newLinea+'" id="precio'+newLinea+'" value="0" />';
		td6.innerHTML= '<input type="text" style="text-align:right" name="unidades'+newLinea+'" id="unidades'+newLinea+'" value="0" class="form_texto" size="10" onchange="pedidos_mod_cantidad(this.value,\''+newLinea+'\');" />';
		td7.innerHTML= '<input type="hidden" name="subtotal'+newLinea+'" id="subtotal'+newLinea+'" value="0" />0';
		td8.innerHTML= '<input type="button" id="del_'+newLinea+'" title="Borrar l&iacute;nea" value="" class="boton_borrar" onclick="pedidos_del_linea(\''+newLinea+'\',\''+'\');" />';
		td9.innerHTML= '<input id="add_'+newLinea+'" type="button" title="A&tilde;adir l&iacute;nea" value="" class="boton_add" onclick="pedidos_add_linea(\''+newLinea+'\',\''+'\',\''+id_sec+'\',\''+id_subsec+'\');" />';
		td9.innerHTML+= '<input id="padre_'+newLinea+'" type="hidden" value="" />';
		
		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td4);
		tr.appendChild(td6);
		tr.appendChild(td7);
		tr.appendChild(td8);
		tr.appendChild(td9);
		
		var navegador = navigator.appName;
		if (navegador == 'Microsoft Internet Explorer')
			theTable.tBodies[0].appendChild(tr);
		else
			theTable.appendChild(tr);

		//	ocultamos los iconos anteriores
		document.getElementById('add_'+linea).style.display= 'none';
		document.getElementById('del_'+linea).style.display= 'none';
			
		document.getElementById('count').value= newLinea;
		
//		alert(document.getElementById('count').value);
//	}
}

function pedidos_del_linea(linea,id_pedido) {
	
	var tabla = document.getElementById('tabla_lineas');
	var form= document.getElementById('form_pedido');
	var contador;
	
	linea= document.getElementById('count').value;
	
	if (form.elements.length > 10) {
		var navegador = navigator.appName; 
		if (navegador == 'Microsoft Internet Explorer') 
			tabla.deleteRow(parseInt(linea)-1); 
		else {
			var myrows = tabla.getElementsByTagName('tr');
			tabla.removeChild(myrows[parseInt(linea)-1]);
		}
		
		//	visualizamos los iconos de la linea anterior
		document.getElementById('add_'+(linea-1)).style.display= 'block';
		document.getElementById('del_'+(linea-1)).style.display= 'block';
		
		document.getElementById('count').value--;
	}
//	alert(document.getElementById('count').value);
}

function linea_regalo(linea,producto,id_pedido,sec,subsec) {
	
	var aux= document.getElementById('d_oc').innerHTML.split('**');
	
	pedidos_add_linea(parseInt(linea)-1,id_pedido,sec,subsec);
	
	ajaxEngine.registerAjaxElement('c_oferta'+linea);
	ajaxEngine.registerAjaxElement('c_producto'+linea);
	ajaxEngine.registerAjaxElement('c_alias'+linea);
	ajaxEngine.registerAjaxElement('c_precio'+linea);
	ajaxEngine.registerAjaxElement('c_descuento'+linea);
	ajaxEngine.registerAjaxElement('c_unidades'+linea);
	ajaxEngine.registerAjaxElement('c_subtotal'+linea);
	ajaxEngine.registerAjaxElement('c_del'+linea);
	ajaxEngine.registerAjaxElement('c_add'+linea);
	
	var options= {
		asynchronous: true,
		onComplete: function() {
/*			var aux1= aux[1].split(',');
			alert(aux1[1]);*/
//			document.getElementById('padre'+linea).value= document.getElementById('producto'+(parseInt(linea)-1)).value;
//			revisa_hijos(document.getElementById('producto'+id));
		},
		parameters: "elem="+linea+"&linea="+linea+"&producto="+producto+"&id_pedido="+id_pedido+"&id_sec="+sec+"&id_subsec="+subsec
	};
	
	//= document.getElementById('producto'+id).value;
	ajaxEngine.sendRequest("linea_regalo",options);
}

function calcula_total_pedido() {
	
	var form= document.getElementById('form_pedido');
	var total= 0;

	for (i=0; i<form.elements.length; i+=10) {
		total+= parseInt(form.elements[i+6].value);
	}
	document.getElementById('total_pedido').innerHTML= total;
}

/*function pedidos_solicitar_pedido() {
	
	var id,nombre,tipo,direccion,cp,localidad,provincia,email,telefono,usuario,clave,fecha_entrega;
	var envio_nombre,envio_direccion,envio_cp,envio_poblacion,envio_provincia,envio_telefono;
	var conocio,regalo,comentarios_cliente,comentarios_internos,comentarios_envio;
	var form= document.getElementById('form_pedido');
	var valores= '';
	var mensaje= '';
	var producto,alias,precio,descuento,unidades,subtotal;
	
	//	datos del clientes
	id= document.getElementById('id').value;
	nombre= document.getElementById('nombre').value;
	tipo= document.getElementById('tipo').value;
	direccion= document.getElementById('direccion').value;
	cp= document.getElementById('cp').value;
	localidad= document.getElementById('localidad').value;
	provincia= document.getElementById('provincia').value;
	email= document.getElementById('email').value;
	telefono= document.getElementById('telefono').value;
	
	if (nombre=='' || tipo=='' || email=='' || telefono=='') {
		alert("Todos los campos obligatorios deben estar rellenos.");
		return;
	}
	
	//	datos del envio
	fecha_entrega= document.getElementById('envio_fecha_entrega').value;
	envio_nombre= document.getElementById('envio_nombre').value;
	envio_direccion= document.getElementById('envio_direccion').value;
	envio_cp= document.getElementById('envio_cp').value;
	envio_poblacion= document.getElementById('envio_poblacion').value;
	envio_provincia= document.getElementById('envio_provincia').value;
	envio_telefono= document.getElementById('envio_telefono').value;
	regalo= document.getElementById('regalo').value;
	
	if (envio_nombre=='' || envio_direccion=='' || envio_cp=='' || envio_poblacion=='' || envio_provincia=='' || envio_telefono=='' || fecha_entrega=='') {
		alert("Todos los campos obligatorios deben estar rellenos.");
		return;
	}
	
	//	otros datos
	conocio= document.getElementById('conocio').value;
	comentarios_cliente= document.getElementById('comentarios').value;
	
	//	obtenemos las lineas de pedido.
	for (i=0; i<form.elements.length; i+=10) {
		producto= form.elements[i+1].value;
		alias= form.elements[i+2].value
		unidades= form.elements[i+5].value;
		precio= form.elements[i+4].value;
		
		if (producto!='' && unidades!='' && unidades!='0') {
			if (valores=='') {
				valores= producto+"#"+alias+"#"+unidades+"#"+precio;
			} else {
				valores+= '**'+producto+"#"+alias+"#"+unidades+"#"+precio;
			}
		} else {
			alert("Ha dejado algún campo de una línea de pedido sin rellenar");
		}
	}
	
	document.getElementById('fp_id').value= id;
	document.getElementById('fp_nombre').value= nombre;
	document.getElementById('fp_tipo').value= tipo;
	document.getElementById('fp_direccion').value= direccion;
	document.getElementById('fp_cp').value= cp;
	document.getElementById('fp_localidad').value= localidad;
	document.getElementById('fp_provincia').value= provincia;
	document.getElementById('fp_email').value= email;
	document.getElementById('fp_telefono').value= telefono;
	document.getElementById('fp_fecha_entrega').value= fecha_entrega;
	document.getElementById('fp_envio_nombre').value= envio_nombre;
	document.getElementById('fp_envio_direccion').value= envio_direccion;
	document.getElementById('fp_envio_cp').value= envio_cp;
	document.getElementById('fp_envio_localidad').value= envio_poblacion;
	document.getElementById('fp_envio_provincia').value= envio_provincia;
	document.getElementById('fp_envio_telefono').value= envio_telefono;
	document.getElementById('fp_regalo').value= regalo;
	document.getElementById('fp_comentarios').value= comentarios_cliente;
	document.getElementById('fp_cad').value= valores;
	document.getElementById('fp_conocio').value= conocio;
	
	//	enviamos la información del pedido
	document.getElementById('formulario_pedidos').submit();
}*/

/*function enviar_pedido() {
	
	var v1,v2,v3,v4;
	
	document.getElementById('fp_entidad').value= document.getElementById('entidad').value;
	document.getElementById('fp_oficina').value= document.getElementById('oficina').value;
	document.getElementById('fp_dig_control').value= document.getElementById('dig_control').value;
	document.getElementById('fp_cuenta').value= document.getElementById('cuenta').value;
	document.getElementById('fp_pago').value= document.getElementById('modo_pago').value;
	
	v1= document.getElementById('entidad').value;
	v2= document.getElementById('oficina').value;
	v3= document.getElementById('dig_control').value;
	v4= document.getElementById('cuenta').value;
	
	var pago= document.getElementById('modo_pago').value;
	
	if (parseInt(pago)==3) {
		if (v1=='' || v2=='' || v3=='' || v4=='') {
			alert("Debe rellenar los datos de la domiciliación.");
			return;
		}
	}
	
	document.getElementById('formulario_pedidos').submit();
}*/

function modificar_modo_pago(val) {
	
	switch(parseInt(val)) {
		case 1:
		break;
		case 2:
		break;
		case 3:
			document.getElementById('pago').style.display= 'block';
		break;
	}
}

// FUNCION PARA CALENDARIO ESPECIAL

function find_date(d,targetDateField, x, y) {

	var options= {
		asynchronous: true,
		onComplete: function() {
			drawDatePicker2(targetDateField, x, y);	
		},
		parameters: "fecha="+d
	};

	ajaxEngine.registerAjaxElement('avail');
	ajaxEngine.registerAjaxElement('davail');
	ajaxEngine.registerAjaxElement('dnotdel');
	ajaxEngine.sendRequest('find_date',options);
}

function find_date2(d,targetDateField, x, y) {

	var options= {
		asynchronous: true,
		onComplete: function() {
			drawDatePicker3(targetDateField, x, y);	
		},
		parameters: "fecha="+d
	};

	ajaxEngine.registerAjaxElement('avail');
	ajaxEngine.registerAjaxElement('davail');
	ajaxEngine.registerAjaxElement('dnotdel');
	ajaxEngine.sendRequest('find_date',options);
}


/*
*/

/*function paso_over(op,num) {
	if (parseInt(op)==1) {
		document.getElementById('paso'+num).src= 'img/paso'+num+'_act.gif';
		document.getElementById('t_paso'+num).style.color= '#FA9F1B';
	} else {
		document.getElementById('paso'+num).src= 'img/paso'+num+'.gif';
		document.getElementById('t_paso'+num).style.color= '#FFFFFF';
	}
}*/

function cambiar_ayuda(obj) {
	
	obj.style.backgroundColor= '#C4D9C8';
	ajaxEngine.registerAjaxElement('ayuda');
	ajaxEngine.sendRequest("cambiar_ayuda","texto="+obj.title);
}
function on_blur(obj) {
	obj.style.backgroundColor= '#FFFFFF';
}

function recordar_clave(op) {
	
	ajaxEngine.registerAjaxElement('datos_acceso');
	switch(parseInt(op)) {
		case 0:
			ajaxEngine.sendRequest('recordar_clave','op=0','email=');
		break;
		case 1:
			var email= document.getElementById('email_acceso').value;
			ajaxEngine.sendRequest('recordar_clave','op=1','email='+email);
		break;
		case 2:
			ajaxEngine.sendRequest('recordar_clave','op=2','email=');
		break;
	}
}

function mostrar_ayuda() {
	
	if (document.getElementById('contenido').style.display=='block') {
		document.getElementById('contenido').style.display= 'none';
		document.getElementById('contenido_ayuda').style.display= 'block';
	} else {
		document.getElementById('contenido_ayuda').style.display= 'none';
		document.getElementById('contenido').style.display= 'block';
	}
}

function poner_foco() {
	if (document.getElementById('usuario')) {
		document.getElementById('usuario').focus();
	} else {
		document.getElementById('nombre').focus();
	}
}

function pedido1() {
	
	var form= document.getElementById('form_productos');
	var valores= '';
	
	//	obtenemos las lineas de pedido.
	for (i=0; i<form.elements.length; i+=5) {
		unidades= form.elements[i+2].value;
		
		if (valores=='') {
			valores= unidades;
		} else {
			valores+= '#'+unidades;
		}
	}
	var options= {
		asynchronous: true,
		onComplete: function() {
			document.getElementById('datepicker').style.display='block';
			document.getElementById('datepicker2').style.display='block';
			document.getElementById('datepickeriframe').style.display='block';
			document.getElementById('datepickeriframe2').style.display='block';
		},
		parameters: "valores="+valores
	};	
	
	ajaxEngine.registerAjaxElement('hoja');
	ajaxEngine.registerAjaxElement('pasos');
	ajaxEngine.sendRequest("pedido1",options);
}

function validar_email(valor) {
	if (valor!='') {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)){
		 return (true);
		} else {
		 return (false);
		}
	}
	return (true);
}

/*function pedido2() {
	
	var id,nombre,tipo,direccion,cp,localidad,provincia,email,telefono;
	var envio_nombre,envio_direccion,envio_cp,envio_poblacion,envio_provincia,envio_telefono,envio_fecha_entrega,envio_regalo,envio_tarde;
	var conocio,comentarios;
	var error= 0;
	var primero= '';
	var aux;
	
	//	datos del clientes
	id= document.getElementById('id').value;
	nombre= document.getElementById('nombre').value.replace(/'/gi," ");
	tipo= document.getElementById('tipo').value;
	direccion= document.getElementById('direccion').value.replace(/'/gi," ");
	cp= document.getElementById('cp').value;
	localidad= document.getElementById('localidad').value.replace(/'/gi," ");
	provincia= document.getElementById('provincia').value.replace(/'/gi," ");
	email= document.getElementById('email').value;
	telefono= document.getElementById('telefono').value;
	
	if (nombre=='') {
		error= 1;
		document.getElementById('nombre').style.backgroundColor= '#FFBDBD';
		primero= 'nombre';
	}
	if (email=='') {
		error= 1;
		document.getElementById('email').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'email';
		}
	}
	if (!validar_email(email)) {
		error= 1;
		document.getElementById('email').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'email';
		}
	}
	if (telefono=='') {
		error= 1;
		document.getElementById('telefono').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'telefono';
		}
	}
	if (cp=='') {
		error= 1;
		document.getElementById('cp').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'cp';
		}
	}
	if (direccion=='') {
		error= 1;
		document.getElementById('direccion').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'direccion';
		}
	}
	if (localidad=='') {
		error= 1;
		document.getElementById('localidad').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'localidad';
		}
	}
	if (provincia=='') {
		error= 1;
		document.getElementById('provincia').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'provincia';
		}
	}
	
	//	datos del envio
	envio_fecha_entrega= document.getElementById('envio_fecha_entrega').value;
	envio_nombre= document.getElementById('envio_nombre').value.replace(/'/gi," ");
	envio_direccion= document.getElementById('envio_direccion').value.replace(/'/gi," ");
	envio_cp= document.getElementById('envio_cp').value;
	envio_poblacion= document.getElementById('envio_poblacion').value.replace(/'/gi," ");
	envio_provincia= document.getElementById('envio_provincia').value.replace(/'/gi," ");
	envio_telefono= document.getElementById('envio_telefono').value;
	envio_regalo= document.getElementById('envio_regalo').value;
	envio_tarde= document.getElementById('envio_tarde').value;
	
	if (envio_nombre=='') {
		envio_nombre= nombre;
		document.getElementById('envio_nombre').value= nombre;
	}
	if (envio_direccion=='') {
		envio_direccion= direccion;
		document.getElementById('envio_direccion').value= direccion;
	}
	if (envio_cp=='') {
		envio_cp= cp;
		document.getElementById('envio_cp').value= cp;
	}
	if (envio_poblacion=='') {
		envio_poblacion= localidad;
		document.getElementById('envio_poblacion').value= localidad;
	}
	if (envio_provincia=='') {
		envio_provincia= provincia;
		document.getElementById('envio_provincia').value= provincia;
	}
	if (envio_telefono=='') {
		envio_telefono= telefono;
		document.getElementById('envio_telefono').value= telefono;
	}
	
	if (envio_nombre=='') {
		error= 1;
		document.getElementById('envio_nombre').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_nombre';
		}
	}
	if (envio_direccion=='') {
		error= 1;
		document.getElementById('envio_direccion').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_direccion';
		}
	}
	if (envio_cp=='') {
		error= 1;
		document.getElementById('envio_cp').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_cp';
		}
	}
	if (envio_poblacion=='') {
		error= 1;
		document.getElementById('envio_poblacion').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_poblacion';
		}
	}
	if (envio_provincia=='') {
		error= 1;
		document.getElementById('envio_provincia').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_provincia';
		}
	}
	if (envio_telefono=='') {
		error= 1;
		document.getElementById('envio_telefono').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_telefono';
		}
	}
	if (envio_fecha_entrega=='') {
		error= 1;
		document.getElementById('envio_fecha_entrega').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_fecha_entrega';
		}
	}
	
	//	otros datos
	conocio= document.getElementById('conocio').value;
	comentarios= document.getElementById('comentarios').value.replace(/'/gi," ");
	
	//	enviamos la información del pedido
	if (error) {
		document.getElementById('errores').style.display= 'block';
		document.getElementById(primero).focus();
	}
	
	if (!error) {
		document.getElementById('errores').style.display= 'none';*/
		/*document.getElementById('datepicker').style.display='none';
		document.getElementById('datepicker2').style.display='none';
		document.getElementById('datepickeriframe').style.display='none';
		document.getElementById('datepickeriframe2').style.display='none';*/
	/*	ajaxEngine.registerAjaxElement('hoja');
		ajaxEngine.registerAjaxElement('pasos');
		ajaxEngine.registerAjaxElement('confirmacion');
		ajaxEngine.sendRequest("pedido2","id="+id,"nombre="+nombre,"tipo="+tipo,"direccion="+direccion,"cp="+cp,"localidad="+localidad,"provincia="+provincia,"email="+email,"telefono="+telefono,"envio_nombre="+envio_nombre,"envio_direccion="+envio_direccion,"envio_cp="+envio_cp,"envio_poblacion="+envio_poblacion,"envio_provincia="+envio_provincia,"envio_telefono="+envio_telefono,"envio_fecha_entrega="+envio_fecha_entrega,"envio_regalo="+envio_regalo,"envio_tarde="+envio_tarde,"conocio="+conocio,"comentarios="+comentarios);
	}
}

function pedido3() {
	
	var form= document.getElementById('form_productos');
	var valores= '';
	var error= 1;
	
	//	obtenemos las lineas de pedido.
	for (i=0; i<form.elements.length; i+=5) {
		unidades= form.elements[i+2].value;
		
		if (parseInt(unidades)!=0) {
			error= 0;
		}
		
		if (valores=='') {
			valores= unidades;
		} else {
			valores+= '#'+unidades;
		}
	}
	
	if (error==0) {
		ajaxEngine.registerAjaxElement('hoja');
		ajaxEngine.registerAjaxElement('pasos');
		ajaxEngine.sendRequest("pedido3","valores="+valores);
	} else {
		document.getElementById('erroresp').style.display= 'block';
	}
}*/

function atras_pedido2() {
	
	var forma_pago= document.getElementById('forma_pago').value;
	var entidad,dc,oficina,cuenta;
	
	entidad= dc= oficina= cuenta= '';
	
	if (document.getElementById('entidad')) {
		entidad= document.getElementById('entidad').value;
	}
	if (document.getElementById('dc')) {
		dc= document.getElementById('dc').value;
	}
	if (document.getElementById('oficina')) {
		oficina= document.getElementById('oficina').value;
	}
	if (document.getElementById('cuenta')) {
		cuenta= document.getElementById('cuenta').value;
	}
	
	ajaxEngine.registerAjaxElement('hoja');
	ajaxEngine.registerAjaxElement('pasos');
	ajaxEngine.sendRequest("atras_pedido2","forma_pago="+forma_pago,"entidad="+entidad,"dc="+dc,"oficina="+oficina,"cuenta="+cuenta);
}

function actualizar() {
	ajaxEngine.sendRequest("actualizar_datos");
	pedido2();
}

function rellena_datos_cliente(id) {
	
	ajaxEngine.registerAjaxElement('datos_cliente');
	ajaxEngine.registerAjaxElement('datos_acceso');
	ajaxEngine.sendRequest("rellena_datos_cliente","id="+id);
	document.getElementById('confirmacion').style.display= 'none';
}

function pedidos_mod_cantidad(unidades,id) {
	
	var producto= document.getElementById('producto'+id).value;
	var precio= document.getElementById('precio'+id).value;
	var obj= document.getElementById('c_subtotal'+id);
	var i;
	var cad_prod,cad_pvp,cad_uni;
	
	obj.innerHTML= '<input type="hidden" name="subtotal'+id+'" id="subtotal'+id+'" value="'+(unidades * precio)+'" />'+(unidades * precio)+'&#8364;';
	
	var form= document.getElementById('form_productos');
	cad_prod= cad_pvp= cad_uni= '';
	for (i=0; i<form.elements.length; i+=5) {
		if (cad_prod=='') {
			cad_prod= form.elements[i].value;
			cad_pvp= form.elements[i+1].value;
			cad_uni= form.elements[i+2].value;
		} else {
			cad_prod+= ','+form.elements[i].value;
			cad_pvp+= ','+form.elements[i+1].value;
			cad_uni+= ','+form.elements[i+2].value;
		}
	}
	
	var options= {
		asynchronous: true,
		onComplete: function() {
//			calcular_sup_baleares();
			calcular_total_pedido();
		},
		parameters: "productos="+cad_prod+"&unidades="+cad_uni+"&precios="+cad_pvp
	};	
	
	ajaxEngine.registerAjaxElement('caja_ofertas');
	ajaxEngine.sendRequest('caja_ofertas',options);	
}

function calcular_total_pedido() {
	
	var form= document.getElementById('form_productos');
	var sup_tarde= document.getElementById('sup_tarde');
	var sup_baleares= document.getElementById('sup_baleares');
	var desc_cod_prom= document.getElementById('desc_cod_prom');
	var form_descuentos= document.getElementById('form_descuentos');
	var total= 0;
	var total_kilos= 0;
	
	for (i=0; i<form.elements.length; i+=5) {
		total+= form.elements[i+1].value * form.elements[i+2].value;
		total_kilos= parseInt(form.elements[i+2].value) * parseInt(form.elements[i+4].value);
	}
	
	if (form_descuentos) {
		for (i=0; i<form_descuentos.elements.length; i+=3) {
			total-= parseFloat(form_descuentos.elements[i+2].value);
		}
	}
	
	if (desc_cod_prom) {
		total-= parseFloat(total)*parseFloat(desc_cod_prom.value)/100;
		var num_prom= new oNumero(parseFloat(total)*parseFloat(desc_cod_prom.value)/100);
		document.getElementById('td_todal_cod_prom').innerHTML= num_prom.formato(2,false)+'&#8364;';
	}
	
	if (sup_tarde) {
		total+= parseFloat(sup_tarde.value);
	}
	
	if (sup_baleares) {
		if (parseInt(total_kilos)>=10 && parseInt(total_kilos)<15) {
			total+= 3;
			document.getElementById('td_desc_baleares').innerHTML= '<input type="hidden" id="sup_baleares" name="sup_baleares" value="3" />Entrega en Baleares peso mayor a 10kg';
			document.getElementById('td_sup_baleares').innerHTML= '3.00&#8364';
			document.getElementById('td_desc_baleares').style.display= 'block';
			document.getElementById('td_sup_baleares').style.display= 'block';
		} else if (parseInt(total_kilos)>= 15) {
			total+= 3;
			document.getElementById('td_desc_baleares').innerHTML= '<input type="hidden" id="sup_baleares" name="sup_baleares" value="3" />Entrega en Baleares peso mayor a 15kg';
			document.getElementById('td_sup_baleares').innerHTML= '3.00&#8364';
			document.getElementById('td_desc_baleares').style.display= 'block';
			document.getElementById('td_sup_baleares').style.display= 'block';
		} else {
			document.getElementById('sup_baleares').value= '';
			document.getElementById('td_desc_baleares').style.display= 'none';
			document.getElementById('td_sup_baleares').style.display= 'none';
		}
	}
	
	var num= new oNumero(total);
	var td_total= document.getElementById('td_total');
	td_total.innerHTML= '<input type="hidden" id="total" name="total" value="'+total+'" />'+num.formato(2,false)+'&nbsp;&#8364;';
}

function modifica_forma_pago(val) {
	
	switch(val) {
		case 'DOMICILIACION BANCARIA':
			document.getElementById('dom_ban').style.display= 'block';
		break;
		case 'PAYPAL':
			document.getElementById('div_sig').innerHTML= '<input type="button" class="boton_enviar" title="Ir a Paso 4" value="Ir a Paso 4" onclick="pedido4();" />';
			document.getElementById('dom_ban').style.display= 'none';
		break;
		default:
			document.getElementById('dom_ban').style.display= 'none';
		break;
	}
}

/*function pedido4() {
	
	var forma_pago= document.getElementById('forma_pago').value;
	var entidad,dc,oficina,cuenta;
	var error= 0;
	var primero= '';
	
	entidad= dc= oficina= cuenta= '';
	
	if (!forma_pago) {
		error= 1;
		document.getElementById('forma_pago').style.backgroundColor= '#FFBDBD';
		primero= 'forma_pago';
	}
	
	if (document.getElementById('entidad')) {
		entidad= document.getElementById('entidad').value;
	}
	if (document.getElementById('dc')) {
		dc= document.getElementById('dc').value;
	}
	if (document.getElementById('oficina')) {
		oficina= document.getElementById('oficina').value;
	}
	if (document.getElementById('cuenta')) {
		cuenta= document.getElementById('cuenta').value;
	}
	
	if (forma_pago=='DOMICILIACION BANCARIA') {
		entidad=entidad.replace(/ /g, '');
		oficina=oficina.replace(/ /g, '');
		dc=dc.replace(/ /g, '');
		cuenta=cuenta.replace(/ /g, '');
		if (entidad=='' || entidad.length!=4 || isNaN(entidad)) {
			error= 1;
			document.getElementById('entidad').style.backgroundColor= '#FFBDBD';
			if (primero=='') {
				primero= 'entidad';
			}
		}
		if (oficina=='' || oficina.length!=4 || isNaN(oficina)) {
			error= 1;
			document.getElementById('oficina').style.backgroundColor= '#FFBDBD';
			if (primero=='') {
				primero= 'oficina';
			}
		}
		if (dc=='' || dc.length!=2 || isNaN(dc)) {
			error= 1;
			document.getElementById('dc').style.backgroundColor= '#FFBDBD';
			if (primero=='') {
				primero= 'dc';
			}
		}
		if (cuenta=='' || cuenta.length!=10 || isNaN(cuenta)) {
			error= 1;
			document.getElementById('cuenta').style.backgroundColor= '#FFBDBD';
			if (primero=='') {
				primero= 'cuenta';
			}
		}
	}
	
	if (error) {
		document.getElementById('errores').style.display= 'block';
		document.getElementById(primero).focus();
	}
	
	if (!error) {
		ajaxEngine.registerAjaxElement('hoja');
		ajaxEngine.registerAjaxElement('pasos');
		ajaxEngine.sendRequest("pedido4","forma_pago="+forma_pago,"entidad="+entidad,"dc="+dc,"oficina="+oficina,"cuenta="+cuenta);
	}
}
*/
function atras_pedido3() {
	
	ajaxEngine.registerAjaxElement('hoja');
	ajaxEngine.registerAjaxElement('pasos');
	ajaxEngine.sendRequest("atras_pedido3");
}

function enviar_pedido() {
	
	document.location= 'enviar_pedido.php';
}

function enviar_pedido_paypal() {
	
	ajaxEngine.registerAjaxElement('hoja');
	ajaxEngine.sendRequest("enviar_pedido_paypal");
}

function terminar_pedido() {
	
	var options= {
		asynchronous: true,
		onComplete: function() {
			location= 'home.htm';
		}
	};
	document.getElementById('result').style.display='none';
	ajaxEngine.registerAjaxElement('result');
	ajaxEngine.sendRequest("terminar_pedido",options);
}

function mostrar_mis_datos() {
	document.getElementById('hoja').style.display= 'none';
	
	if(document.getElementById('datepicker')){
		document.getElementById('datepicker').style.display='none';
		document.getElementById('datepicker2').style.display='none';
		document.getElementById('datepickeriframe').style.display='none';
		document.getElementById('datepickeriframe2').style.display='none';
	}
	
	var options= {
		asynchronous: true,
		onComplete: function() {
			document.getElementById('hoja1').style.display= 'block';
		}
	};
	ajaxEngine.registerAjaxElement('hoja1');
	ajaxEngine.sendRequest("mostrar_mis_datos", options);
}

function volver_pedido() {
	
	document.getElementById('hoja').style.display= 'block';
	document.getElementById('hoja1').style.display= 'none';
}

function cambiar_postal3(postal,elem) {
	
	ajaxEngine.registerAjaxElement('provincia1_new');
	ajaxEngine.registerAjaxElement('poblacion1_new');
	ajaxEngine.sendRequest('cambiar_postal3','postal='+postal);
}

function cambia_cuenta(val) {
	ajaxEngine.sendRequest('cambiar_cuenta','cuenta='+val);
	chidden= val;
}

function guardar_datos() {
	
	var nombre,direccion,postal,poblacion,provincia,telefono,email,tipo;
	var usuario,clave;
	var entidad,oficina,dc,cuenta,cuenta_bk;
	
	nombre= document.getElementById('nombre_new').value.replace(/'/gi," ");
	direccion= document.getElementById('direccion_new').value.replace(/'/gi," ");
	postal= document.getElementById('cp_new').value;
	poblacion= document.getElementById('localidad_new').value.replace(/'/gi," ");
	provincia= document.getElementById('provincia_new').value.replace(/'/gi," ");
	telefono= document.getElementById('telefono_new').value;
	email= document.getElementById('email_new').value;
	tipo= document.getElementById('tipo_new').value;
	
	usuario= document.getElementById('usuario_new').value.replace(/'/gi," ");
	clave= document.getElementById('clave_new').value.replace(/'/gi," ");
	
	entidad= document.getElementById('entidad_new').value;
	oficina= document.getElementById('oficina_new').value;
	dc= document.getElementById('dc_new').value;
	cuenta= document.getElementById('cuenta_new').value;
	cuenta_bk= document.getElementById('cuenta_bk').value;
	
	var options= {
		asynchronous: true,
		onComplete: function() {
			if (document.getElementById('result').innerHTML!='OK') {
				document.getElementById('errores2').style.display= 'block';
			}
		},
		parameters: 'nombre='+nombre+'&direccion='+direccion+'&postal='+postal+'&poblacion='+poblacion+'&provincia='+provincia+'&telefono='+telefono+'&email='+email+'&tipo='+tipo+'&usuario='+usuario+'&clave='+clave+'&entidad='+entidad+'&oficina='+oficina+'&dc='+dc+'&cuenta='+cuenta+'&cuenta_bk='+cuenta_bk
	};
	
	ajaxEngine.registerAjaxElement('hoja1');
	ajaxEngine.registerAjaxElement('result');
	ajaxEngine.sendRequest('guardar_datos',options);
}

function mis_pedidos() {
	
	document.getElementById('hoja').style.display= 'none';
	
	var options= {
		asynchronous: true,
		onComplete: function() {
			document.getElementById('hoja1').style.display= 'block';
		}
	};
	
	ajaxEngine.registerAjaxElement('hoja1');
	ajaxEngine.sendRequest("mis_pedidos",options);
}

function ver_datos_envio() {
	
	document.getElementById('datos_envio').style.display= 'block';
	document.getElementById('ver_d_entrega').innerHTML= '<a class="enlace_negrita_p" href="javascript:ocultar_datos_entrega();">- Ocultar datos de entrega</a>';
	
	document.getElementById('div_movil_1').style.top= '-595px';
}

function ocultar_datos_entrega() {
	
	document.getElementById('datos_envio').style.display= 'none';
	document.getElementById('ver_d_entrega').innerHTML= '- Si los datos de entrega son distintos a los datos del comprador haga click <a class="enlace_negrita_g" href="javascript:ver_datos_envio();">aqu&iacute;</a> para poder introducirlos.';
	
	document.getElementById('div_movil_1').style.top= '-455px';
}

function enviar_solicitud() {
	
	var nombre,email,tipo;
	var error= 0;
	var primero= '';
	
	nombre= document.getElementById('nombre').value;
	email= document.getElementById('email').value;
	tipo= document.getElementById('tipo').value;
	
	if (nombre=='') {
		error= 1;
		document.getElementById('nombre').style.backgroundColor= '#FFBDBD';
		primero= 'nombre';
	}
	
	if (email=='') {
		error= 1;
		document.getElementById('email').style.backgroundColor= '#FFBDBD';
		primero= 'email';
	}
	
	if (!validar_email(email)) {
		error= 1;
		document.getElementById('nombre').style.backgroundColor= '#FFBDBD';
		primero= 'email';
	}
	
	//	enviamos la información del pedido
	if (error) {
		document.getElementById('errores').style.display= 'block';
		document.getElementById(primero).focus();
		return;
	}
	
	if (nombre!='' && email!='' && tipo!='') {
		document.getElementById('boton_enviar').disabled= true;
		document.getElementById('datos_solicitante').submit();
	}
}


//
// FORMULARIO LYDIA
//


function ir_paso1(){
	var form= document.getElementById('form_productos');
	var valores= '';
	
	//	obtenemos las lineas de pedido.
	for (i=0; i<form.elements.length; i+=5) {
		unidades= form.elements[i+2].value;
		
		if (valores=='') {
			valores= unidades;
		} else {
			valores+= '#'+unidades;
		}
	}
	var options= {
		asynchronous: true,
		onComplete: function() {
			document.getElementById('datepicker').style.display='block';
			document.getElementById('datepicker2').style.display='block';
			document.getElementById('datepickeriframe').style.display='block';
			document.getElementById('datepickeriframe2').style.display='block';
		},
		parameters: "valores="+valores
	};	
	
	ajaxEngine.registerAjaxElement('hoja');
	ajaxEngine.registerAjaxElement('pasos');
	ajaxEngine.registerRequest('atras_pedido1', 'atras_pedido1.php');
	ajaxEngine.sendRequest("atras_pedido1",options);
}

//eleccion de productos
function ir_paso2(){
	var id,nombre,tipo,direccion,cp,localidad,provincia,email,telefono;
	var envio_nombre,envio_direccion,envio_cp,envio_poblacion,envio_provincia,envio_telefono,envio_fecha_entrega,envio_regalo,envio_tarde;
	var conocio,comentarios;
	var error= 0;
	var primero= '';
	var aux;

	
	//	datos del clientes
	id= document.getElementById('id').value;
	nombre= document.getElementById('nombre').value;
	tipo= document.getElementById('tipo').value;
	direccion= document.getElementById('direccion').value.replace(/'/gi," ");
	cp= document.getElementById('cp').value;
	localidad= document.getElementById('localidad').value.replace(/'/gi," ");
	provincia= document.getElementById('provincia').value.replace(/'/gi," ");
	email= document.getElementById('email').value;
	telefono= document.getElementById('telefono').value;
	
	if (nombre=='') {
		error= 1;
		document.getElementById('nombre').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'nombre';
		}
	}
	
	if (email=='') {
		error= 1;
		document.getElementById('email').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'email';
		}
	}
	
	if (!validar_email(email)) {
		error= 1;
		document.getElementById('email').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'email';
		}
	}	
	
	if (telefono=='') {
		error= 1;
		document.getElementById('telefono').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'telefono';
		}
	}
	
	if (cp=='') {
		error= 1;
		document.getElementById('cp').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'cp';
		}
	}
	
	if (direccion=='') {
		error= 1;
		document.getElementById('direccion').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'direccion';
		}
	}
	if (localidad=='') {
		error= 1;
		document.getElementById('localidad').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'localidad';
		}
	}
	if (provincia=='') {
		error= 1;
		document.getElementById('provincia').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'provincia';
		}
	}	
	
	//	datos del envio
	envio_fecha_entrega= document.getElementById('envio_fecha_entrega').value;
	envio_nombre= document.getElementById('envio_nombre').value;
	envio_direccion= document.getElementById('envio_direccion').value;
	envio_cp= document.getElementById('envio_cp').value;
	envio_poblacion= document.getElementById('envio_poblacion').value;
	envio_provincia= document.getElementById('envio_provincia').value;
	envio_telefono= document.getElementById('envio_telefono').value;
	envio_regalo= document.getElementById('envio_regalo').value;
	envio_tarde= document.getElementById('envio_tarde').value;
	
	if (envio_nombre=='') {
		envio_nombre= nombre;
		document.getElementById('envio_nombre').value= nombre;
	}
	if (envio_direccion=='') {
		envio_direccion= direccion;
		document.getElementById('envio_direccion').value= direccion;
	}
	if (envio_cp=='') {
		envio_cp= cp;
		document.getElementById('envio_cp').value= cp;
	}
	if (envio_poblacion=='') {
		envio_poblacion= localidad;
		document.getElementById('envio_poblacion').value= localidad;
	}
	if (envio_provincia=='') {
		envio_provincia= provincia;
		document.getElementById('envio_provincia').value= provincia;
	}
	if (envio_telefono=='') {
		envio_telefono= telefono;
		document.getElementById('envio_telefono').value= telefono;
	}
	
	if (envio_nombre=='') {
		error= 1;
		document.getElementById('envio_nombre').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_nombre';
		}
	}
	if (envio_direccion=='') {
		error= 1;
		document.getElementById('envio_direccion').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_direccion';
		}
	}
	if (envio_cp=='') {
		error= 1;
		document.getElementById('envio_cp').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_cp';
		}
	}
	if (envio_poblacion=='') {
		error= 1;
		document.getElementById('envio_poblacion').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_poblacion';
		}
	}
	if (envio_provincia=='') {
		error= 1;
		document.getElementById('envio_provincia').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_provincia';
		}
	}
	if (envio_telefono=='') {
		error= 1;
		document.getElementById('envio_telefono').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_telefono';
		}
	}
	if (envio_fecha_entrega=='') {
		error= 1;
		document.getElementById('envio_fecha_entrega').style.backgroundColor= '#FFBDBD';
		if (primero=='') {
			primero= 'envio_fecha_entrega';
		}
	}
	
	//	otros datos
	conocio= document.getElementById('conocio').value;
	comentarios= document.getElementById('comentarios').value.replace(/'/gi," ");
	//	mostramos capa de error en caso de que lo hubiera
	if (error) {
		document.getElementById('errores').style.display= 'block';
		document.getElementById(primero).focus();
	}else{
		var a=document.getElementById('boton_siguiente');
		a.disabled=true;
		a.value="Enviando";
		document.getElementById('errores').style.display= 'none';
		ajaxEngine.registerAjaxElement('hoja');
		ajaxEngine.registerAjaxElement('pasos');
		ajaxEngine.registerAjaxElement('confirmacion');
		ajaxEngine.registerRequest('pedido2', 'pedido2.php');
		ajaxEngine.sendRequest("pedido2","id="+id,"nombre="+nombre,"tipo="+tipo,"direccion="+direccion,"cp="+cp,"localidad="+localidad,"provincia="+provincia,"email="+email,"telefono="+telefono,"envio_nombre="+envio_nombre,"envio_direccion="+envio_direccion,"envio_cp="+envio_cp,"envio_poblacion="+envio_poblacion,"envio_provincia="+envio_provincia,"envio_telefono="+envio_telefono,"envio_fecha_entrega="+envio_fecha_entrega,"envio_regalo="+envio_regalo,"envio_tarde="+envio_tarde,"conocio="+conocio,"comentarios="+comentarios);	
	}

}

//forma de pago
function ir_paso3(){
	var form= document.getElementById('form_productos');
	var valores= '';
	var error= 1;	
	
	//	obtenemos las lineas de pedido.
	for (i=0; i<form.elements.length; i+=5) {
		unidades= form.elements[i+2].value;
		
		if (parseInt(unidades)!=0) {
			error= 0;
		}
		
		if (valores=='') {
			valores= unidades;
		} else {
			valores+= '#'+unidades;
		}
	}
	
	if (error==0) {
		var a=document.getElementById('boton_siguiente');
		a.disabled=true;
		a.value="Enviando";
		ajaxEngine.registerAjaxElement('hoja');
		ajaxEngine.registerAjaxElement('pasos');
		ajaxEngine.registerRequest('pedido3', 'pedido3.php');
		ajaxEngine.sendRequest("pedido3","valores="+valores);
	} else {
		document.getElementById('erroresp').style.display= 'block';
	}
}

//muestra datos del pedido
function ir_paso4(){
	var forma_pago= document.getElementById('forma_pago').value;
	var entidad,dc,oficina,cuenta;
	var error= 0;
	var primero= '';
	
	entidad= dc= oficina= cuenta= '';
	
	if (!forma_pago) {
		error= 1;
		document.getElementById('forma_pago').style.backgroundColor= '#FFBDBD';
		primero= 'forma_pago';
	}
	
	if (document.getElementById('entidad')) {
		entidad= document.getElementById('entidad').value;
	}
	if (document.getElementById('dc')) {
		dc= document.getElementById('dc').value;
	}
	if (document.getElementById('oficina')) {
		oficina= document.getElementById('oficina').value;
	}
	if (document.getElementById('cuenta')) {
		cuenta= document.getElementById('cuenta').value;
	}
	
	if (forma_pago=='DOMICILIACION BANCARIA') {
		entidad=entidad.replace(/ /g, '');
		oficina=oficina.replace(/ /g, '');
		dc=dc.replace(/ /g, '');
		cuenta=cuenta.replace(/ /g, '');
		
		/*if(!validaLibreta(entidad, oficina, dc, chidden)){
			error= 1;
			document.getElementById('entidad').style.backgroundColor= '#FFBDBD';
			document.getElementById('oficina').style.backgroundColor= '#FFBDBD';
			document.getElementById('dc').style.backgroundColor= '#FFBDBD';
			document.getElementById('cuenta').style.backgroundColor= '#FFBDBD';
		}*/
		if (entidad=='' || entidad.length!=4 || isNaN(entidad)) {
			error= 1;
			document.getElementById('entidad').style.backgroundColor= '#FFBDBD';
			if (primero=='') {
				primero= 'entidad';
			}
		}
		if (oficina=='' || oficina.length!=4 || isNaN(oficina)) {
			error= 1;
			document.getElementById('oficina').style.backgroundColor= '#FFBDBD';
			if (primero=='') {
				primero= 'oficina';
			}
		}
		if (dc=='' || dc.length!=2 || isNaN(dc)) {
			error= 1;
			document.getElementById('dc').style.backgroundColor= '#FFBDBD';
			if (primero=='') {
				primero= 'dc';
			}
		}
		if (cuenta=='' || cuenta.length!=10 || isNaN(cuenta)) {
			error= 1;
			document.getElementById('cuenta').style.backgroundColor= '#FFBDBD';
			if (primero=='') {
				primero= 'cuenta';
			}
		}
	}
	
	if (error) {
		document.getElementById('errores').style.display= 'block';
		document.getElementById(primero).focus();
	}
	
	if (!error) {
		var a=document.getElementById('boton_siguiente');
		a.disabled=true;
		a.value="Enviando";
		ajaxEngine.registerAjaxElement('hoja');
		ajaxEngine.registerAjaxElement('pasos');
		ajaxEngine.registerRequest('pedido4', 'pedido4.php');
		ajaxEngine.sendRequest("pedido4","forma_pago="+forma_pago,"entidad="+entidad,"dc="+dc,"oficina="+oficina,"cuenta="+cuenta);
	}
}

//envio del pedido
function enviar_pedido2(){
	document.location= 'enviar_pedido.php';	
}
function validar_cod(){
	var cod = document.getElementById('cod_prom').value;
	ajaxEngine.registerAjaxElement('msg_cod_prom');	
	ajaxEngine.sendRequest("cod_prom","c="+cod);
}
function try_again(){
	ajaxEngine.registerAjaxElement('msg_cod_prom');	
	ajaxEngine.sendRequest("cod_prom","c=000");
}
function validaLibreta(i_entidad,i_oficina,i_digito,i_cuenta){
	// VALIDACIÓN DE CUALQUIER LIBRETA DE CUALQUIER ENTIDAD BANCARIA.
	// Funcion recibe como parámetro la entidad, la oficina,
	// el digito (concatenación del de control entidad-oficina y del de control entidad)
	  // y la cuenta. Espera los valores con 0's a la izquierda.
	// Devuelve true o false.
	// NOTAS:
	// Formato deseado de los parámetros:
	// - i_entidad (4)
	// - i_oficina (4)
	// - i_digito (2)
	// - i_cuenta (10)
	var wtotal,wcociente, wresto;
	if (i_entidad.length != 4){
		return false;
	}
	if (i_oficina.length != 4){
		return false;
	}
	if (i_digito.length != 2){
		return false;
	}
	if (i_cuenta.length != 10){
		return false;
	}
	wtotal = i_entidad.charAt(0) * 4;
	wtotal += i_entidad.charAt(1) * 8;
	wtotal += i_entidad.charAt(2) * 5;
	wtotal += i_entidad.charAt(3) * 10;
	wtotal += i_oficina.charAt(0) * 9;
	wtotal += i_oficina.charAt(1) * 7;
	wtotal += i_oficina.charAt(2) * 3;
	wtotal += i_oficina.charAt(3) * 6;
	// busco el resto de dividir wtotal entre 11
	wcociente = Math.floor(wtotal / 11);
	wresto = wtotal - (wcociente * 11);

	wtotal = 11 - wresto;
	if (wtotal == 11){
		wtotal=0;
	}
	if (wtotal == 10){
		wtotal=1;
	}
	if (wtotal != i_digito.charAt(0)){
		return false;
	}
	//hemos validado la entidad y oficina
	//-----------------------------------
	wtotal = i_cuenta.charAt(0) * 1;
	wtotal += i_cuenta.charAt(1) * 2;
	wtotal += i_cuenta.charAt(2) * 4;
	wtotal += i_cuenta.charAt(3) * 8;
	wtotal += i_cuenta.charAt(4) * 5;
	wtotal += i_cuenta.charAt(5) * 10;
	wtotal += i_cuenta.charAt(6) * 9;
	wtotal += i_cuenta.charAt(7) * 7;
	wtotal += i_cuenta.charAt(8) * 3;
	wtotal += i_cuenta.charAt(9) * 6;

	// busco el resto de dividir wtotal entre 11
	wcociente = Math.floor(wtotal / 11);
	wresto = wtotal - (wcociente * 11);
	
	wtotal = 11 - wresto;
	if (wtotal == 11){wtotal=0;}
	if (wtotal == 10){wtotal=1;}

	if (wtotal != i_digito.charAt(1)){
		//alert(wtotal+' y no '+i_digito.charAt(1));
		return false;
	}
	// hemos validado la cuenta corriente
	return true;
}
