
	var xmlhttp,alerted
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
			// JScript gives us Conditional compilation, we can cope with old IE versions.
			try {
				xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
			} catch (e) {
				try {
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
				} catch (E) {
					alert("You must have Microsofts XML parsers available")
				}
			}
		@else
			//alert("You must have JScript version 5 or above.")
			xmlhttp=false
			alerted=false
		@end @*/	
	if (!xmlhttp && !alerted) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false
			alerted=false
			alert("You need a browser which supports an XMLHttpRequest Object.\nMozilla build 0.9.5 has this Object and IE5 and above, others may do, I don't know, any info david@iquadrat.com")
		}
	}

	
	function loginear() {
		var d = new Date();
		txtlogin=document.frmlogin.txtlogin.value;
		txtpassword=document.frmlogin.txtpassword.value;

		xmlhttp.open("GET", "/login.asp?login="+txtlogin+"&password="+txtpassword+"&lah="+d.getTime() );
		xmlhttp.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
		xmlhttp.onreadystatechange=actualizacontenido
		xmlhttp.send(null)
	}

	function actualizacontenido() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.responseText.length>0) {

				respuesta = xmlhttp.responseText;
				//alert(respuesta)
				switch (respuesta) {
					case "OK": 
					document.location = '/catalogo-categorias'//document.location//='/default.asp';
					break;
					case "MAL":
					document.getElementById('respuesta_login').innerHTML=" Login y/o password incorrectos";
					break;
					case "OKEMAIL":
					document.getElementById('respuesta_login').innerHTML="Su contraseña ha sido enviada.";
					break;
					case "MALEMAIL":
					document.getElementById('respuesta_login').innerHTML="Su email no es válido.";	
					break;
				}
			}
		}	
	}
	
	function recordar() {
		txtemail=document.getElementById('txtemail').value;
		
		document.getElementById('respuesta_login').innerHTML="enviando..."

		xmlhttp.open("GET", "/recordar.asp?email="+txtemail);
		xmlhttp.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
		xmlhttp.onreadystatechange=actualizacontenido
		xmlhttp.send(null)
	}

 	function recuerdate() {
	
	document.getElementById('respuesta_login').innerHTML="email: <input type='text' class='input_text' name='txtemail' id='txtemail' style='background-color:#FFFFFF;'>&nbsp;<a href='javascript: recordar();' class='enviarRecordar'>enviar</a>";
	
	}
	
	function desloginear() {
		var d = new Date();
		
		document.location = '/login.asp?deslogin=1'+"&lah="+d.getTime()
/*		txtlogin=document.frmlogin.txtlogin.value;
		txtpassword=document.frmlogin.txtpassword.value;

		xmlhttp.open("GET", "login.asp?deslogin=1");
		xmlhttp.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
		xmlhttp.onreadystatechange=actualizacontenido
		xmlhttp.send(null)		*/
	}
	
	// PEDIDO ONLINE *************************************************************************************************
	
	var g_idusuario = 0;
	var g_producto = 0;
		
	var g_act_estado = false;
	var g_act_pedido = false;
	
	var g_idprovincia = 0;
	
	function comprarProducto(idusuario,idproducto,unidades,actestado,actpedido) {
		g_idusuario = idusuario;
		g_producto = idproducto;
		g_act_estado = actestado;
		g_act_pedido = actpedido;
		
		var today=new Date();
		xmlhttp.open("GET", "/compras.asp?tipo=comprar_producto&idusuario="+idusuario+"&idproducto="+idproducto+"&unidades="+unidades+"&pp="+today);
		xmlhttp.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
		xmlhttp.onreadystatechange=rcv_comprarProducto
		xmlhttp.send(null)
	}

	function rcv_comprarProducto() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.responseText.length>0) {
				respuesta = xmlhttp.responseText;

				//Actualizamos el estado
				if(g_act_estado==true){actualizarEstado(g_idusuario)}
				else{
					if(g_act_pedido==true){actualizarPedido(g_idusuario)}	
				}
				//actualizarPedido(g_idusuario);
				//alert(respuesta)
				if(respuesta=="KO"){
					alert('Error '+ respuesta);
				}else if(respuesta=="OK") {
					if(document.getElementById('buy'+g_producto)){
						document.getElementById('buy'+g_producto).innerHTML = '<div id="buy'+g_producto+'"><img src="/images/bt_buy_disabled.gif" width="32" height="32" border="0"></div>';
					}
				}else if(respuesta=="DEL"){
					if(document.getElementById('buy'+g_producto)){
						document.getElementById('buy'+g_producto).innerHTML = '<div id="buy'+g_producto+'"><a href="javascript:comprarProducto('+g_idusuario+','+g_producto+',1,true,true)"><img src="/images/bt_buy.gif" width="32" height="32" border="0"></div></a>';	
					}
				} //else 	{ alert('Error '+ respuesta); }

			}
		}	
	}
	
	function actualizarEstado(idusuario) {
		var today=new Date();
		xmlhttp.open("GET", "/compras.asp?tipo=actualizar_estado&idusuario="+idusuario+"&pp="+today);
//		alert("compras.asp?tipo=actualizar_estado&idusuario="+idusuario+"&pp="+today)
		xmlhttp.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
		xmlhttp.onreadystatechange=rcv_actualizarEstado
		xmlhttp.send(null)
	}
	
	function rcv_actualizarEstado() {
		//alert(xmlhttp.readyState);
		if (xmlhttp.readyState==4) {
			if (xmlhttp.responseText.length>0) {
				respuesta = xmlhttp.responseText;
				//Escribimos en el estado
				//alert(respuesta)
				if(g_act_pedido==true){actualizarPedido(g_idusuario)}
				document.getElementById('status_pedido').innerHTML = respuesta;
			} else { alert(respuesta); }
		}	
		else
		{
		//alert("No entramos");
		}
	}
	
	/*
	function actualizarPedido(idusuario, idprovincia, formapago) {
		var today=new Date();
		
		if(idprovincia==undefined){idprovincia=""}
		if(document.getElementById('cesta_provincia')!=null){
			var indice = document.getElementById('cesta_provincia').selectedIndex;
			idprovincia = document.getElementById('cesta_provincia').options[indice].value;
		}
		
		if(formapago==undefined){formapago=""}
		if(document.getElementById('cesta_pago')!=null){
			var indice = document.getElementById('cesta_pago').selectedIndex;
			formapago = document.getElementById('cesta_pago').options[indice].value;
		}

		//document.getElementById('listado_pedido').innerHTML = "<img src='js/loading2.gif' alt='' border='0' />";
		

		xmlhttp.open("GET", "compras.asp?tipo=ver_pedido&idusuario="+idusuario+"&idprovincia="+idprovincia+"&formapago="+formapago+"&pp="+today);
		xmlhttp.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
		xmlhttp.onreadystatechange=rcv_actualizarPedido
		xmlhttp.send(null)
	}

	function rcv_actualizarPedido() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.responseText.length>0) {
				respuesta = xmlhttp.responseText;
				//alert(respuesta);
				//Actualizamos el estado
				document.getElementById('listado_pedido').innerHTML = respuesta;
				
				actualizarPago()
			}
		}	
	}
	*/
	
	function actualizarPedido(idusuario, idprovincia, formapago) {
		var today=new Date();
		
		if(idprovincia==undefined){idprovincia=""}
		if(document.getElementById('cesta_provincia')!=null){
			var indice = document.getElementById('cesta_provincia').selectedIndex;
			idprovincia = document.getElementById('cesta_provincia').options[indice].value;
		}

		if(formapago==undefined){formapago=""}
		if(document.getElementById('cesta_pago')!=null){
			var indice = document.getElementById('cesta_pago').selectedIndex;
			formapago = document.getElementById('cesta_pago').options[indice].value;
		}
		
		$("#bt_efectuarpedido_on").hide();
		$("#bt_efectuarpedido_off").show();
		mostrarLoadImg();
		//mostrarBloqueo();
//alert("compras.asp?tipo=ver_pedido&idusuario="+idusuario+"&idprovincia="+idprovincia+"&formapago="+formapago)
		$.get("/compras.asp?tipo=ver_pedido&idusuario="+idusuario+"&idprovincia="+idprovincia+"&formapago="+formapago+"&pp="+today, function(data){
			if(data != "") {
				//document.getElementById('listado_pedido').innerHTML = data;
				$("#listado_pedido").html(data);
				actualizarPago()
			}
		});
	}
	
	function efectuarPedido(idusuario,totalpvp,totaltransporte,totalneto,totaliva,totalnum,campos) {
		g_idusuario = idusuario;
		
		mostrarProgreso("Efectuando pedido...<br /><span style='color:#545454;'>Espere confirmación</span>")
		
		var today=new Date();
		xmlhttp.open("GET", "/compras.asp?tipo=efectuar_pedido&idusuario="+idusuario+"&totalpvp="+totalpvp+"&totaltransporte="+totaltransporte+"&totalneto="+totalneto+"&totaliva="+totaliva+"&totalnum="+totalnum+""+campos+"&pp="+today);
		xmlhttp.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
		xmlhttp.onreadystatechange=rcv_efectuarPedido
		xmlhttp.send(null)
	}

	function rcv_efectuarPedido() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.responseText.length>0) {
				respuesta = xmlhttp.responseText;
				if(respuesta=="OK"){
					mostrarProgreso("Pedido realizado con éxito<br>")
					setTimeout("location.reload(true)",1500);
					//setTimeout("actualizarEstado("+g_idusuario+")",3000);
					//setTimeout("ocultarPedido()",3000);
				}else{alert(respuesta)}
			}
		}else{
			mostrarProgreso("Efectuando pedido...<br /><span style='color:#545454;'>Espere confirmación</span>")
		}
	}
	
	function efectuarPedidoTARJ(idusuario,totalpvp,totaltransporte,totalneto,totaliva,totalnum,campos) {
		g_idusuario = idusuario;
		
		mostrarProgreso("Efectuando pedido...<br /><span style='color:#545454;'>Espere confirmación</span>")
		
		var today=new Date();
		xmlhttp.open("GET", "/compras.asp?tipo=efectuar_pedido&idusuario="+idusuario+"&totalpvp="+totalpvp+"&totaltransporte="+totaltransporte+"&totalneto="+totalneto+"&totaliva="+totaliva+"&totalnum="+totalnum+""+campos+"&pp="+today);
		xmlhttp.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
		xmlhttp.onreadystatechange=rcv_efectuarPedidoTARJ
		xmlhttp.send(null)
	}

	function rcv_efectuarPedidoTARJ() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.responseText.length>0) {
				respuesta = xmlhttp.responseText;
				if(respuesta=="OK"){
					//mostrarProgreso("Pedido realizado con éxito<br>")
					//setTimeout("location.reload(true)",1500);
					//setTimeout("actualizarEstado("+g_idusuario+")",3000);
					//setTimeout("ocultarPedido()",3000);
				}else{alert(respuesta)}
			}
		}
	}
	
	function isEmail(txt) {  
		var RegExPattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
		if ((txt.match(RegExPattern)) && (txt!='')) {  
			return true;
		} else {  
			return false; 
		}   
	} 
		
	function isNumeric(txt) {  
		var RegExPattern = /^(?:\+|-)?\d+$/;  
		if ((txt.match(RegExPattern)) && (txt!='')) {  
			return true;
		} else {  
			return false; 
		}   
	} 

	function comprobarDatosPedido(idusuario,totalpvp,totaltransporte,totalneto,totaliva,totalnum,tipo) {	
		//alert(totalneto)
		var direccion = document.getElementById('direccion');
		var provincia = document.getElementById('provincia');
		var poblacion = document.getElementById('poblacion');
		var cp = document.getElementById('cp');
		var atencionde = document.getElementById('atencionde');
		var telefono = document.getElementById('telefono');
		var email = document.getElementById('email');

		var confir = document.getElementById('confir');
		var acepto = document.getElementById('aceptoCondiciones');
		
		var pago = document.getElementById('cesta_pago');
		var provincia_sel = document.getElementById('cesta_provincia');
		
		msg=''
		if(pago.value==''){msg+='Es obligatorio especificar una forma de pago.\n'}
		if(provincia_sel.value==''){msg+='Es obligatorio especificar un destino.\n'}
		if(direccion.value==''){msg+='El campo "Dirección" es obligatorio.\n'}
		if(provincia.value==''){msg+='El campo "Provincia" es obligatorio.\n'}
		if(poblacion.value==''){msg+='El campo "Población" es obligatorio.\n'}
		if(cp.value==''){msg+='El campo Código "Postal" es obligatorio.\n'}
		if(atencionde.value==''){msg+='El campo "Atención de" es obligatorio.\n'}
		if(telefono.value==''){msg+='El campo "Teléfono" es obligatorio.\n'}
		if(email.value==''){msg+='El campo "Email" es obligatorio.\n'}
		if(acepto.checked==false){msg+='Necesitas aceptar las Condiciones.\n'}
		
		if(!isEmail(email.value)){msg+='Especifique una dirección de email válida.\n'}
		
		if(msg==''){
			if(confir.checked==false){mostrarGlobo()}
			else{
				//if(confirm("¿Estás seguro de que quieres finalizar el pedido?")){
				//jConfirm("¿Estás seguro de que quieres finalizar el pedido?", "Dimasa", function(r) {
					//document.getElementById('block_layer').style.display='block';
					mostrarBloqueo();
					if(tipo=="TARJ"){
						efectuarPedidoTARJ(idusuario,totalpvp,totaltransporte,totalneto,totaliva,totalnum,'&direccion='+direccion.value+'&provincia='+provincia.value+'&poblacion='+poblacion.value+'&cp='+cp.value+'&atencionde='+atencionde.value+'&telefono='+telefono.value+'&email='+email.value+'&finalizado=0')
						tpvSend();
					}else if(tipo=="TRAN"){
						efectuarPedido(idusuario,totalpvp,totaltransporte,totalneto,totaliva,totalnum,'&direccion='+direccion.value+'&provincia='+provincia.value+'&poblacion='+poblacion.value+'&cp='+cp.value+'&atencionde='+atencionde.value+'&telefono='+telefono.value+'&email='+email.value+'&finalizado=1')
					}else{
						alert("Es obligatorio especificar una forma de pago.")	
					}
				//});
				//}
			}
		}else{
			alert(msg)
		}

	}
	
	function comprobarDatosPedidoPresupuesto(idusuario) {	
		//alert(totalneto)
		var importetotal = document.formpresu.importetotal;
		var presupuesto = document.formpresu.presupuesto;
		var nombre = document.formpresu.nombre;
		var telefono = document.formpresu.telefono;
		var email = document.formpresu.email;

		var acepto = document.formpresu.aceptoCondiciones;
			
		msg=''
		if(presupuesto.value==''){msg+='El campo "Presupuesto" es obligatorio.\n'}
		if(importetotal.value==''){msg+='El campo "Importe" es obligatorio.\n'}
		if(nombre.value==''){msg+='El campo "Nombre y Apellidos" es obligatorio.\n'}
		if(telefono.value==''){msg+='El campo "Teléfono" es obligatorio.\n'}
		if(email.value==''){msg+='El campo "Email" es obligatorio.\n'}
		if(acepto.checked==false){msg+='Necesitas aceptar las Condiciones.\n'}
	
		if(!isEmail(email.value)){msg+='Especifique una dirección de email válida.\n'}
		if(isNumeric(importetotal.value)){
			if(importetotal.value <= 0){ msg+='Especifique un importe superior a 0.\n'; }
		}else{
			msg+='Especifique un importe válido.\n';
		}
			
//		HACER LA VALIDACION DEL EMAIL Y DEL NUMERICO
				
		if(msg==''){
			//mostrarBloqueo();
			
			mostrarBloqueo();
			tpvSend();
			
			//actualizarPedidoPresupuesto(idusuario, presupuesto.value, importetotal.value, nombre.value, telefono.value, email.value, 1);
			
			/*
			$("#bt_efectuarpedido_on").hide();
			$("#bt_efectuarpedido_off").show();
			mostrarLoadImg();
			*/
			//$.get("/compras.asp?tipo=efectuar_pedido_presupuesto&idusuario="+idusuario+"&importetotal="+importetotal+"&presupuesto="+presupuesto+"&nombre="+nombre+"&telefono="+telefono+"&email="+email+"&pp="+today, function(data){
				
			//});
			
		}else{
			alert(msg)
		}
	}
	
	
	function actualizarPedidoPresupuesto(idusuario, presupuesto, importetotal, nombre, telefono, email, envio) {
		var today=new Date();
		
		if(presupuesto==null){ presupuesto=""; }
		if(importetotal=="" || importetotal==null){ importetotal=0; }
		if(nombre==null){ nombre=""; }
		if(telefono==null){ telefono=""; }
		if(email==null){ email=""; }
		
		$("#bt_efectuarpedido_on").hide();
		$("#bt_efectuarpedido_off").show();
		mostrarLoadImg();

		$.get("/compras.asp?tipo=pedido_presupuesto&idusuario="+idusuario+"&presupuesto="+presupuesto+"&importetotal="+importetotal+"&nombre="+nombre+"&telefono="+telefono+"&email="+email+"&envio="+envio+"&pp="+today, function(data){
			if(data != "") {
				$("#listado_pedido2").html(data);
			}
		});
	}

	function actualizarPago(){	
		if(document.getElementById('cesta_pago')){
			indice = document.getElementById('cesta_pago').selectedIndex;
			valor = document.getElementById('cesta_pago').options[indice].value;
			
			switch (valor){
				case "TARJ":
					document.getElementById('forma_pago_TARJ').style.display='list-item';
					document.getElementById('forma_pago_TRAN').style.display='none';
					break;
				case "TRAN":
					document.getElementById('forma_pago_TRAN').style.display='list-item';
					document.getElementById('forma_pago_TARJ').style.display='none';
					break;
				default:
					document.getElementById('forma_pago_TRAN').style.display='none';
					document.getElementById('forma_pago_TARJ').style.display='none';
			}
		}
	}
	
	function mostrarBloqueo(){
		$("#block_layer").show();
		//$("#block_layer").css("height",$("#listado_pedido").css("height"));
		$("#block_layer").css("width",$(document).width());
		$("#block_layer").css("height",$(document).height());
		$("#block_layer").css("top","0");
		$("#block_layer").css("left","0");
		$("#block_layer").css("opacity",0.5)	
	}
	
	function ocultarBloqueo(){
		$("#block_layer").hide();	
	}
	
	function mostrarLoadImg(){
		$("#loader_img").show();
	}
	
	function ocultarLoadImg(){
		$("#loader_img").hide();	
	}
	
	function mostrarGlobo(){
		document.getElementById('globo').style.display='block';
	}
	
	function ocultarGlobo(){
		document.getElementById('globo').style.display='none';
	}
	
	function mostrarProgreso(mensaje){
		document.getElementById('progreso').style.display='block';
		document.getElementById('progreso_texto').innerHTML=mensaje;
	}
	
	function ocultarProgreso(){
		document.getElementById('progreso').style.display='none';
	}

	function verPedido(idusuario) {
		g_idusuario = idusuario;

		ocultarPedido();

		//actualizarPedido(g_idusuario);
		//document.getElementById('listado_pedido').style.display='block'; 
		$("#listado_pedido").slideDown();
		$("#ver_pedido_presupuesto").css("display","none");
		$("#ver_pedido").css("display","none");
		//document.getElementById('ver_pedido_presupuesto').style.display='none';
		//document.getElementById('ver_pedido').style.display='none';
	}
	
	function verPedidoPresupuesto(idusuario) {
		g_idusuario = idusuario;
		
		ocultarPedido();
		//actualizarPedido(g_idusuario);
		//document.getElementById('listado_pedido').style.display='block'; 
		actualizarPedidoPresupuesto(idusuario);
		
		$("#listado_pedido2").slideDown();
		$("#texto_pedido").html("Pago presupuesto enviado por Dimasa");
		
		$("#ver_pedido_presupuesto").css("display","none");
		$("#ver_pedido").css("display","none");
		
		//document.getElementById('ver_pedido_presupuesto').style.display='none';
		//document.getElementById('ver_pedido').style.display='none';
	}
	
	function ocultarPedido(){
		$("#listado_pedido").slideUp();
		$("#listado_pedido2").slideUp();
		
		//document.getElementById('listado_pedido').style.display='none'; 
		
		//document.getElementById('ver_pedido').style.display='inline';	
		//document.getElementById('ver_pedido_presupuesto').style.display='inline';	
		
		$("#ver_pedido_presupuesto").css("display","inline");
		$("#ver_pedido").css("display","inline");
		
	}
	

	//FIN PEDIDO ONLINE **********************************************************************************************

