// JavaScript Document
oCarro = new function(){
	
	//A

	this.analizarTeclaRegistro = function(e){
		if(e.keyCode == 13)FireEvent($('enviarRegistroFormCarro'),'click');
	}
	this.analizarTeclaEmailPrevio = function(e){
		if(e.keyCode == 13)this.comprobarEmail();
	}	
	this.analizarTeclaDireccion = function(e){
		if(e.keyCode == 13){
			this.enviarDireccion('nueva');
		}
	}		
	this.analizarTeclaLogin = function(e){
		if(e.keyCode == 13){
			this.loginUserCarro();
		}
	}		


	
	//C
	this.confirmarCompra = function(e){
		if(e)StopEvent(e);
		if(!listo){ return false; }

		listo = false;
		reqType = 'conectarAPayPal';
		mostrarLoader(true);		
		var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
		v += enviarDatos();		
		req.pedir(DIR_ROOT + 'requests/carro.php', v);
	}
	this.confirmarPagoFinal = function(token,idPay,e,desdeCuenta){
		if(e)StopEvent(e);
		if(!listo){ return false; }

		listo = false;
		reqType = 'pagoFinal';
		mostrarLoader(true);		
		var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
		v += enviarDatos();
		v += 'token' + SEP_IGUAL + token + SEP_AND;
		v += 'PayerID' + SEP_IGUAL + idPay + SEP_AND;
		if(desdeCuenta)v += 'desdeCuenta' + SEP_IGUAL + 1 + SEP_AND;		

		req.pedir(DIR_ROOT + 'requests/carro.php', v);
		
		
	}
	this.comprobarEmail = function(e){
		if(e)StopEvent(e);
		if(!listo)return false;
		if(!erEmail.test(trim($('emailPrevioFormCarro').value))){
			$('errorEmailErroneoRegistroFormCarro').style.display = 'block';
		}
		else{
			listo = false;			
			reqType = 'comprobarUsuario';
			$('errorEmailErroneoRegistroFormCarro').style.display = 'none';
			$('errorEmailYaRegistradoFormCarro').style.display = 'none';
			var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
			v += 'emailUsuario' + SEP_IGUAL + $('emailPrevioFormCarro').value + SEP_AND;
			req.pedir(DIR_ROOT + 'requests/carro.php', v);
		}
	}	
	this.cambiarCantidad = function(clave){
		var cookProducto = oCookie.get('productosCarro');
		var ArrProducto = new Array();
		if(cookProducto)ArrProducto = unserialize(cookProducto);
		
		var input = $('cantidadCompra' + clave);
		input.value = trim(input.value);
		if(ER_NATURAL_NOCERO.test(input.value)){
			var total = parseFloat($('totalCarro').value) - (parseFloat(ArrProducto[clave]['cantidad'])*parseFloat($('precioProducto' + clave).value)) + (parseFloat(input.value)*parseFloat($('precioProducto' + clave).value));
			$('totalCarro').value = total;
			$('totalCarroForm').innerHTML = formatoNumero(total,ID_IDIOMA);
			$('importeProducto' + clave).innerHTML = formatoNumero((parseFloat(input.value)*parseFloat($('precioProducto' + clave).value)),ID_IDIOMA);			
			ArrProducto[clave]['cantidad'] = input.value;
			var serCook = serialize(ArrProducto);
			oCookie.set('productosCarro', serCook);
			
			//recalcularTotal();	
		}
		else{
			input.value = ArrProducto[clave]['cantidad'];	
		}
		
	}	
	var cargarFormulario = function(){
		if(!$('layPopCarro')){
			if(!req)req = new Request(listener);
			accion = 'cargarPop';
			req.pedir(DIR_ROOT + 'requests/cargar.php','file' + SEP_IGUAL +'../includes/popCarroCompra.php' + SEP_AND);
		}
	}
	var cuandoTerminoMostrar = function(){
		
		if(!!$('bgCarro')){
			$('bgCarro').style.height = ((HBody() > HScreen())? HBody():HScreen()) + 'px';
			$('bgCarro').style.width = ((WBody() > WScreen())? WScreen():WBody()) + 'px';
		}
	
		setOpacity(70, $('bgCarro'));
		$('bgCarro').style.display = 'block';
		
		//Blocker.show();	
	}
	var cuandoTerminoOcultar = function(){
		$('bgCarro').style.display = 'none';
	//	Blocker.hide();
	}
	var cambiarLetras = function(){
		FLIR.replace( 'a.faceliftMT' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'h2.slogan' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'a.btnBuscar' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'h4.titCh' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'h4.titMe' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'h4.titGr' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'h4.titBgCh' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'h4.titBgMe' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'h4.titEspecial' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		FLIR.replace( 'h4.titBgGr' , new FLIRStyle({ cFont:'bellerose' , mode:'wrap' }) );
		
	}

	//E
	this.enviarDireccion = function(tipo,e){
		var continuar = false;
		if(e)StopEvent(e);
		tipoDireccion = tipo;
		if(tipo != 'nueva'){
			errorDireccion = false;
			continuar = true;
		}
		else{
			errorDireccion = false;
			
			$('nombreCompletoFormCarro').onblur();
			if($('nombreCompletoFormCarro').parentNode.className == errorClass){ errorDireccion = true; }

			$('direccionFormCarro').onblur();
			if($('direccionFormCarro').parentNode.className == errorClass){ errorDireccion = true; }


			$('provinciaFormCarro').onblur();
			if($('provinciaFormCarro').parentNode.className == errorClass){ errorDireccion = true; }

			$('ciudadFormCarro').onblur();
			if($('ciudadFormCarro').parentNode.className == errorClass){ errorDireccion = true; }

			$('cpFormCarro').onblur();
			if($('cpFormCarro').parentNode.className == errorClass){ errorDireccion = true; }

			$('telefonoFormCarro').onblur();
			if($('telefonoFormCarro').parentNode.className == errorClass){ errorDireccion = true; }
			
			if(errorDireccion){
				$('errorDireccionFormCarro').style.display = 'block';
				continuar = false;
			}
			else{
				$('errorDireccionFormCarro').style.display = 'none';
				var datosDireccion = new Array();
				datosDireccion['nombreCompleto'] = $('nombreCompletoFormCarro').value;
				datosDireccion['idPais'] = $('paisFormCarro').value;
				datosDireccion['idProvincia'] = $('provinciaFormCarro').value;
				datosDireccion['nombreProvincia'] = $('provinciaNombreFormCarro').value;
				datosDireccion['idCiudad'] = $('ciudadFormCarro').value;
				datosDireccion['nombreCiudad'] = $('ciudadNombreFormCarro').value;
				datosDireccion['direccion'] = $('direccionFormCarro').value;
				datosDireccion['cp'] = $('cpFormCarro').value;
				datosDireccion['telefono'] = $('telefonoFormCarro').value;				
				
				var serCook = serialize(datosDireccion);
				oCookie.set('datosDireccionCarro', serCook);
				continuar = true;
			}
		}
		if(continuar)this.leerPaso(4);		
	}
	this.enviarRegistro = function(evt){
			var error = false, t = '';
			
			if(evt){ StopEvent(evt); }
			if(!listo){ return false; }

			$('nombreRegistroFormCarro').onblur();
			if($('nombreRegistroFormCarro').parentNode.className == errorClass){ error = true; }
			
			$('apellidoRegistroFormCarro').onblur();
			if($('apellidoRegistroFormCarro').parentNode.className == errorClass){ error = true; }

			$('direccionRegistroFormCarro').onblur();
			if($('direccionRegistroFormCarro').parentNode.className == errorClass){ error = true; }

			$('telefonoRegistroFormCarro').onblur();
			if($('telefonoRegistroFormCarro').parentNode.className == errorClass){ error = true; }


			$('emailRegistroFormCarro').onblur();
			if($('emailRegistroFormCarro').parentNode.className == errorClass){ error = true; }

			$('cpRegistroFormCarro').onblur();
			if($('cpRegistroFormCarro').parentNode.className == errorClass){ error = true; }


			$('provinciaRegistroFormCarro').onblur();
			if($('provinciaRegistroFormCarro').parentNode.className == errorClass){ error = true; }
			
			$('ciudadRegistroFormCarro').onblur();
			if($('ciudadRegistroFormCarro').parentNode.className == errorClass){ error = true; }

			$('contrasenaRegistroFormCarro').onblur();
			if($('contrasenaRegistroFormCarro').parentNode.className == errorClass){ error = true; }

			$('confirmarContrasenaRegistroFormCarro').onblur();
			if($('confirmarContrasenaRegistroFormCarro').parentNode.className == errorClass){ error = true; }
			$('errorYaRegistradoRegistroFormCarro').style.display = 'none';
			
			if(trim($('contrasenaRegistroFormCarro').value) != trim($('confirmarContrasenaRegistroFormCarro').value)){
				$('errorContrasenaRegistroFormCarro').style.display = 'block';
			}
			else{
				$('errorContrasenaRegistroFormCarro').style.display = 'none';
			}
			
			
			if(error){
				$('errorCamposRegistroFormCarro').style.display = 'block';
				return false;
			}
			else{
				$('errorCamposRegistroFormCarro').style.display = 'none';
			}
			
			
			listo = false;
			reqType = 'registro';

			t += 'tipo' + SEP_IGUAL + reqType + SEP_AND;
			t += 'nombre' + SEP_IGUAL + trim($('nombreRegistroFormCarro').value) + SEP_AND;
			t += 'apellido' + SEP_IGUAL + trim($('apellidoRegistroFormCarro').value) + SEP_AND;
			t += 'email' + SEP_IGUAL + trim($('emailRegistroFormCarro').value) + SEP_AND;
			t += 'idPais' + SEP_IGUAL + trim($('paisRegistroFormCarro').value) + SEP_AND;
			t += 'idProvincia' + SEP_IGUAL + trim($('provinciaRegistroFormCarro').value) + SEP_AND;
			t += 'idCiudad' + SEP_IGUAL + trim($('ciudadRegistroFormCarro').value) + SEP_AND;
			t += 'nombreProvincia' + SEP_IGUAL + trim($('provinciaNombreRegistroFormCarro').value) + SEP_AND;
			t += 'nombreCiudad' + SEP_IGUAL + trim($('ciudadNombreRegistroFormCarro').value) + SEP_AND;
			t += 'direccion' + SEP_IGUAL + trim($('direccionRegistroFormCarro').value) + SEP_AND;
			t += 'cp' + SEP_IGUAL + trim($('cpRegistroFormCarro').value) + SEP_AND;
			t += 'telefono' + SEP_IGUAL + trim($('telefonoRegistroFormCarro').value) + SEP_AND;

			t += 'usuario' + SEP_IGUAL + trim($('usuarioRegistroFormCarro').value) + SEP_AND;
			t += 'contrasena' + SEP_IGUAL + trim($('contrasenaRegistroFormCarro').value) + SEP_AND;
			t += 'confirmarContrasena' + SEP_IGUAL + trim($('confirmarContrasenaRegistroFormCarro').value) + SEP_AND;			

			mostrarLoader(true);			
			req.pedir(DIR_ROOT + 'requests/carro.php', t);
		}		
	var enviarDatos = function(){
		var v = '';
		v += 'tipoPedido' + SEP_IGUAL + tipoPedido + SEP_AND;		
		if(tipoPedido == 'producto'){
			var ArrDir = new Array();
			var cookDir = oCookie.get('datosDireccionCarro');		
			if(cookDir){
				ArrDir = unserialize(cookDir);
				if(ArrDir['nombreCompleto'])v += 'nombreCompleto' + SEP_IGUAL + ArrDir['nombreCompleto'] + SEP_AND;
				if(ArrDir['idPais'])v += 'idPais' + SEP_IGUAL + ArrDir['idPais'] + SEP_AND;
				if(ArrDir['idProvincia'])v += 'idProvincia' + SEP_IGUAL + ArrDir['idProvincia'] + SEP_AND;
				if(ArrDir['nombreProvincia'])v += 'nombreProvincia' + SEP_IGUAL + ArrDir['nombreProvincia'] + SEP_AND;
				if(ArrDir['idCiudad'])v += 'idCiudad' + SEP_IGUAL + ArrDir['idCiudad'] + SEP_AND;
				if(ArrDir['nombreCiudad'])v += 'nombreCiudad' + SEP_IGUAL + ArrDir['nombreCiudad'] + SEP_AND;
				if(ArrDir['direccion'])v += 'direccion' + SEP_IGUAL + ArrDir['direccion'] + SEP_AND;
				if(ArrDir['cp'])v += 'cp' + SEP_IGUAL + ArrDir['cp'] + SEP_AND;
				if(ArrDir['telefono'])v += 'telefono' + SEP_IGUAL + ArrDir['telefono'] + SEP_AND;			
				
			}
			v += 'tipoDireccion' + SEP_IGUAL + tipoDireccion + SEP_AND;
			var cookProducto = oCookie.get('productosCarro');
			if(cookProducto){
				v += 'productosSeleccionados' + SEP_IGUAL + cookProducto + SEP_AND;	
			}
		}
		else if(tipoPedido == 'pista'){
			var ArrProd = new Array();
			var i;
			var idsPistas = $('idsPistas').value.split(',');
			for(i in idsPistas)ArrProd[idsPistas[i]] = 1;
			v += 'productosSeleccionados' + SEP_IGUAL + serialize(ArrProd) + SEP_AND;	
		}
		else if(tipoPedido == 'albumMp3'){
			var ArrProd = new Array();
			ArrProd[$('idAlbum').value] = 1;
			v += 'productosSeleccionados' + SEP_IGUAL + serialize(ArrProd) + SEP_AND;	
		}


		
		return(v);
		
	}
	
	//I
	var inicializarDireccion = function(){
			

		if(!$('nombreCompletoFormCarro').onblur){
			$('nombreCompletoFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('nombreCompletoFormCarro'));
		}
		if(!$('direccionFormCarro').onblur){
			$('direccionFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('direccionFormCarro'));
		}
		if(!$('cpFormCarro').onblur){
			$('cpFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('cpFormCarro'));
		}
		if(!$('telefonoFormCarro').onblur){
			$('telefonoFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('telefonoFormCarro'));
		}
			
			
		if(!$('paisFormCarro').onchange){
			$('paisFormCarro').onchange = function(){
				$('provinciaFormCarro').disabled = $('ciudadFormCarro').disabled = true;
								
				$('provinciaNombreFormCarro').style.display = $('ciudadNombreFormCarro').style.display = 'none';
				
				reqType = 'provincias';
				
				var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
				v += 'idPais' + SEP_IGUAL + $('paisFormCarro').value + SEP_AND;
				
				req.pedir(DIR_ROOT + 'requests/zonas_geograficas.php', v);
			};
		}
		if(!$('provinciaFormCarro').onchange){
			$('provinciaFormCarro').onchange = function(){
				
				var v = trim($('provinciaFormCarro').value);
				
				$('ciudadFormCarro').disabled = true;
				$('provinciaNombreFormCarro').style.display = (v == '0')? '' : 'none';
				$('ciudadNombreFormCarro').style.display = 'none';
				
				if(erNumero.test(v) && v != ''){
					
					reqType = 'ciudades';
					
					var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
					v += 'idProvincia' + SEP_IGUAL + $('provinciaFormCarro').value + SEP_AND;
					
					req.pedir(DIR_ROOT + 'requests/zonas_geograficas.php', v);
				}
			};
		}
		if(!$('provinciaFormCarro').onblur){
			$('provinciaFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || !erNumero.test(v)){ this.parentNode.className = errorClass; }
				else{ $('provinciaNombreFormCarro').onblur(); }
			}.closure($('provinciaFormCarro'));
		}
		
		if(!$('provinciaNombreFormCarro').onblur){
			$('provinciaNombreFormCarro').onblur = function(){
				var v = trim(this.value);
				if($('provinciaFormCarro').value == '0' && (v == '' || v.length < 3)){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('provinciaNombreFormCarro'));
		}
		
		if(!$('ciudadFormCarro').onchange){
			$('ciudadFormCarro').onchange = function(){
				
				var v = trim($('ciudadFormCarro').value);
				
				$('ciudadNombreFormCarro').style.display = (v == '0')? '' : 'none';

				
			};
		}
		if(!$('ciudadFormCarro').onblur){
			$('ciudadFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || !erNumero.test(v)){ this.parentNode.className = errorClass; }
				else{ $('ciudadNombreFormCarro').onblur(); }
			}.closure($('ciudadFormCarro'));
		}
		
		if(!$('ciudadNombreFormCarro').onblur){
			$('ciudadNombreFormCarro').onblur = function(){
				var v = trim(this.value);
				if($('ciudadFormCarro').value == '0' && (v == '' || v.length < 3)){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('ciudadNombreFormCarro'));
		}
	}
	var inicializarRegistro = function(){
			

		if(!$('nombreRegistroFormCarro').onblur){
			$('nombreRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('nombreRegistroFormCarro'));
		}
		if(!$('apellidoRegistroFormCarro').onblur){
			$('apellidoRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('apellidoRegistroFormCarro'));
		}
		if(!$('emailRegistroFormCarro').onblur){
			$('emailRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
				$('usuarioRegistroFormCarro').value = trim($('emailRegistroFormCarro').value);
			}.closure($('emailRegistroFormCarro'));
		}		
		if(!$('telefonoRegistroFormCarro').onblur){
			$('telefonoRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('telefonoRegistroFormCarro'));
		}		
		
		if(!$('direccionRegistroFormCarro').onblur){
			$('direccionRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('direccionRegistroFormCarro'));
		}
		if(!$('cpRegistroFormCarro').onblur){
			$('cpRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('cpRegistroFormCarro'));
		}
		if(!$('telefonoRegistroFormCarro').onblur){
			$('telefonoRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('telefonoRegistroFormCarro'));
		}		
				
		if(!$('paisRegistroFormCarro').onchange){
			$('paisRegistroFormCarro').onchange = function(){
				$('provinciaRegistroFormCarro').disabled = $('ciudadRegistroFormCarro').disabled = true;
				
				$('provinciaNombreRegistroFormCarro').style.display = $('ciudadNombreRegistroFormCarro').style.display = 'none'
				
				reqType = 'provincias';
				
				var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
				v += 'idPais' + SEP_IGUAL + $('paisRegistroFormCarro').value + SEP_AND;
				
				req.pedir(DIR_ROOT + 'requests/zonas_geograficas.php', v);
			};
		}
		if(!$('provinciaRegistroFormCarro').onchange){
			$('provinciaRegistroFormCarro').onchange = function(){
				
				var v = trim($('provinciaRegistroFormCarro').value);
				
				$('ciudadRegistroFormCarro').disabled = true;
				$('provinciaNombreRegistroFormCarro').style.display = (v == '0')? '' : 'none';
				$('ciudadNombreRegistroFormCarro').style.display = 'none';
				
				if(erNumero.test(v) && v != ''){
					
					reqType = 'ciudades';
					
					var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
					v += 'idProvincia' + SEP_IGUAL + $('provinciaRegistroFormCarro').value + SEP_AND;
					
					req.pedir(DIR_ROOT + 'requests/zonas_geograficas.php', v);
				}
			};
		}
		if(!$('provinciaRegistroFormCarro').onblur){
			$('provinciaRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || !erNumero.test(v)){ this.parentNode.className = errorClass; }
				else{ $('provinciaNombreRegistroFormCarro').onblur(); }
			}.closure($('provinciaRegistroFormCarro'));
		}
		
		if(!$('provinciaNombreRegistroFormCarro').onblur){
			$('provinciaNombreRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if($('provinciaRegistroFormCarro').value == '0' && (v == '' || v.length < 3)){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('provinciaNombreRegistroFormCarro'));
		}
		
		if(!$('ciudadRegistroFormCarro').onchange){
			$('ciudadRegistroFormCarro').onchange = function(){
				
				var v = trim($('ciudadRegistroFormCarro').value);
				
				$('ciudadNombreRegistroFormCarro').style.display = (v == '0')? '' : 'none';

				
			};
		}
		if(!$('ciudadRegistroFormCarro').onblur){
			$('ciudadRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if(v == '' || !erNumero.test(v)){ this.parentNode.className = errorClass; }
				else{ $('ciudadNombreRegistroFormCarro').onblur(); }
			}.closure($('ciudadRegistroFormCarro'));
		}
		
		if(!$('ciudadNombreRegistroFormCarro').onblur){
			$('ciudadNombreRegistroFormCarro').onblur = function(){
				var v = trim(this.value);
				if($('ciudadRegistroFormCarro').value == '0' && (v == '' || v.length < 3)){ this.parentNode.className = errorClass; }
				else{ this.parentNode.className = ''; }
			}.closure($('ciudadNombreRegistroFormCarro'));
		}
		if(!$('contrasenaRegistroFormCarro').onblur){
				$('contrasenaRegistroFormCarro').onblur = function(){
					var v = trim(this.value);
					if(v == '' || v.length < 3|| $('confirmarContrasenaRegistroFormCarro').value != $('contrasenaRegistroFormCarro').value){ 
						this.parentNode.className = errorClass; 
						if(trim($('contrasenaRegistroFormCarro').value) != trim($('confirmarContrasenaRegistroFormCarro').value)){
							$('errorContrasenaRegistroFormCarro').style.display = 'block';
						}
						else{
							$('errorContrasenaRegistroFormCarro').style.display = 'none';
						}
						
					}
					else{ 
						this.parentNode.className = '';
						$('confirmarContrasenaRegistroFormCarro').parentNode.className = '';
						$('errorContrasenaRegistroFormCarro').style.display = 'none';
					}
				}.closure($('contrasenaRegistroFormCarro'));
			}
		if(!$('confirmarContrasenaRegistroFormCarro').onblur){
				$('confirmarContrasenaRegistroFormCarro').onblur = function(){
					var v = trim(this.value);
					if(v == '' || $('confirmarContrasenaRegistroFormCarro').value != $('contrasenaRegistroFormCarro').value){ 
						this.parentNode.className = errorClass; 
						if(trim($('contrasenaRegistroFormCarro').value) != trim($('confirmarContrasenaRegistroFormCarro').value)){
							$('errorContrasenaRegistroFormCarro').style.display = 'block';
						}
						else{
							$('errorContrasenaRegistroFormCarro').style.display = 'none';
						}
						
					}
					else{ 
						this.parentNode.className = '';
						$('contrasenaRegistroFormCarro').parentNode.className = '';
						$('errorContrasenaRegistroFormCarro').style.display = 'none';						
					}

				}.closure($('confirmarContrasenaRegistroFormCarro'));
			}
		
	}	
	
	//L
	this.leerPaso = function(paso,e){
		if(e){ StopEvent(e); }		

		if((!listo || paso == pasoActual || !puedoCambiarPaso(paso))){ return false; }


		listo = false;
		reqType = 'pasos';
		pasoActual = paso;
		
		mostrarLoader(true);
				
		var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
		v += 'paso' + SEP_IGUAL + paso + SEP_AND;
		v += enviarDatos();		
		req.pedir(DIR_ROOT + 'requests/carro.php', v);
	}
	this.loginUserCarro = function(e){
		var error = false;
		if(trim($('usuarioFormCarro').value) == ''){
			$('errorUsuarioFormCarro').style.display = 'block';
			error = true;
		}
		else $('errorUsuarioFormCarro').style.display = 'none';

		if(trim($('contrasenaFormCarro').value) == ''){
			$('errorContrasenaFormCarro').style.display = 'block';
			error = true;
		}
		else $('errorContrasenaFormCarro').style.display = 'none';
		
		if(!error){
			reqType = 'logueo';
			var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
			v += 'usuario' + SEP_IGUAL + trim($('usuarioFormCarro').value) + SEP_AND;
			v += 'contrasena' + SEP_IGUAL + trim($('contrasenaFormCarro').value) + SEP_AND;			
			mostrarLoader(true);
			req.pedir(DIR_ROOT + 'requests/carro.php', v);
		}

		if(e)StopEvent(e);
	}
	var listener = function(){
		listo = true;
		var d = req.respuestaXML;
		if(d){

			if(reqType == 'pasos'){
				if(d.getAttribute('logueado') == 1)logueado = true;
				else logueado = false;
				$('contDatosCarro').innerHTML = d.firstChild.data;
				cambiarLetras();
				if(pasoActual == 3)inicializarDireccion();
				if(pasoActual == 2)inicializarRegistro();				
				
				mostrarLoader(false);
			}
			else if(reqType == 'pagoFinal'){
				if(d.getAttribute('tipo') == 'exito'){
					$('contDatosCarro').innerHTML = d.firstChild.data;
					cambiarLetras();
					logueado = true;
				}
				else{
					Error.onAccept = Error.onCancel = function(){
						Error.hide();
						Blocker.hide();
					}
					Error.show(nodo.firstChild.data);
					Blocker.show();
					
				}
				
				mostrarLoader(false);
			}
			else if(reqType == 'logueo'){
				if(d.getAttribute('tipo') == 'exito'){
					logueado = true;	
					
					if($('btnDireccionCarro'))this.leerPaso(3);
					else this.leerPaso(4);

				}
				else{
					var i;
					var errores = d.getAttribute('errores').split(',');
					for(i=0;i<errores.length;i++){
						if($('error' + errores[i] + 'FormCarro')){
							if(errores[i] == 'Inexistente')$('error' + errores[i] + 'FormCarro').innerHTML = d.firstChild.data;							
							$('error' + errores[i] + 'FormCarro').style.display = 'block';
						}
					}
					mostrarLoader(false);	
				}
				
			}
			else if(reqType == 'comprobarUsuario'){
				if(d.getAttribute('tipo') == 'exito'){
					$('contEmailPrevio').style.display = 'none';					
					$('contFormRegistro').style.display = 'block';
					$('emailRegistroFormCarro').value = trim($('emailPrevioFormCarro').value);
					$('usuarioRegistroFormCarro').value = trim($('emailPrevioFormCarro').value);					
				}
				else{
					if(d.getAttribute('tipoError') == 'email')$('errorEmailErroneoRegistroFormCarro').style.display = 'block';
					else $('errorEmailYaRegistradoFormCarro').style.display = 'block';
					mostrarLoader(false);	
				}
				
			}
			else if(reqType == 'provincias'){
				
				while($('provinciaRegistroFormCarro').hasChildNodes()){ $('provinciaRegistroFormCarro').removeChild($('provinciaRegistroFormCarro').firstChild); }
				
				for(var i = 0, t = d.childNodes.length; i < t; i++){
					var opt = document.createElement('option');
					opt.value = d.childNodes[i].getAttribute('value');
					opt.innerHTML = d.childNodes[i].firstChild.data;
					
					$('provinciaRegistroFormCarro').appendChild(opt);
				}
				$('provinciaRegistroFormCarro').disabled = false;
			}
			else if(reqType == 'ciudades'){
				
				while($('ciudadRegistroFormCarro').hasChildNodes()){ $('ciudadRegistroFormCarro').removeChild($('ciudadRegistroFormCarro').firstChild); }
				
				for(var i = 0, t = d.childNodes.length; i < t; i++){
					var opt = document.createElement('option');
					opt.value = d.childNodes[i].getAttribute('value');
					opt.innerHTML = d.childNodes[i].firstChild.data;
					
					$('ciudadRegistroFormCarro').appendChild(opt);
				}
				$('ciudadRegistroFormCarro').disabled = false;
			}
			else if(reqType == 'registro'){
				if(d.getAttribute('tipo') == 'exito'){
					$('contDatosCarro').innerHTML = d.firstChild.data;
					cambiarLetras();
					logueado = true;
				}
				else if(d.getAttribute('tipo') == 'pasos'){
					$('contDatosCarro').innerHTML = d.firstChild.data;
					cambiarLetras();
				}
				else if(d.getAttribute('tipo') == 'error'){
					Error.onAccept = Error.onCancel = function(){
						Error.hide();
						Blocker.hide();
					}
					Error.show(d.firstChild.data);
					Blocker.show();
	
				}
				mostrarLoader(false);
				
				
			}
			else if(reqType == 'conectarAPayPal'){
				if(d.getAttribute('tipo') == 'exito'){
					document.location = d.firstChild.data;
				}
				else if(d.getAttribute('tipo') == 'error'){
					Error.onAccept = Error.onCancel = function(){
						Error.hide();
						Blocker.hide();
					}
					Error.show(d.firstChild.data);
					Blocker.show();
					mostrarLoader(false);					
				}

			}			
			else if(reqType != 'cargarPop'){
				$('contVideo').innerHTML = d.firstChild.data;
				$('loaderVideo').style.display = 'none';
				$('contVideo').style.display = 'block';
			}
			else{
				cargarHTMLRequest(d.firstChild.data);
				accion = '';
			}
		}
		else{ 
			Error.onAccept = Error.onCancel = function(){
				Error.hide();
				Blocker.hide();
			}
			Error.show('Surgieron problemas en el registro de la operación, los administradores ya han sido notificados');
			Blocker.show();
			req.pedir(DIR_FIX + 'requests/errorPublico.php','error' + SEP_IGUAL + req.respuestaHTML + SEP_AND);
			mostrarLoader(false);						
		}
	}.closure(this);
	
	//M
	var mostrarLoader = function(mostrar){
		if(mostrar){
			$('blockerCarro').style.height = $('contDatosCarro').offsetHeight + 'px';
			$('blockerCarro').style.width = $('contDatosCarro').offsetWidth + 'px';			
			if(!$('blockerCarro').seteado){
				setOpacity(70,$('blockerCarro'));
				$('blockerCarro').seteado = true;
			}
			$('blockerCarro').style.display = 'block';
			$('loadCarro').style.display = 'block';			
			$('loadCarro').style.marginTop = Math.round(($('contDatosCarro').offsetHeight - $('loadCarro').offsetHeight)/2) + 'px';
			$('loadCarro').style.marginLeft = Math.round(($('contDatosCarro').offsetWidth - $('loadCarro').offsetWidth)/2) + 'px';			
		}
		else{
			$('blockerCarro').style.display = 'none';
			$('loadCarro').style.display = 'none';			
		}
	}

	//N
	var formatoNumero = function(precio,idIdioma){
		if(idIdioma == 1) return number_format(precio,2,',','.');
		else return number_format(precio,2,'.',',');
	}
	var number_format = function (number, decimals, dec_point, thousands_sep) {
		// Formats a number with grouped thousands
		//
		// version: 906.1806
		// discuss at: http://phpjs.org/functions/number_format    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
		var n = number, prec = decimals;
	 
		var toFixedFix = function (n,prec) {
			var k = Math.pow(10,prec);        return (Math.round(n*k)/k).toString();
		};
	 
		n = !isFinite(+n) ? 0 : +n;
		prec = !isFinite(+prec) ? 0 : Math.abs(prec);    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
		var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
	 
		var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
		 var abs = toFixedFix(Math.abs(n), prec);
		var _, i;
	 
		if (abs >= 1000) {
			_ = abs.split(/\D/);        i = _[0].length % 3 || 3;
	 
			_[0] = s.slice(0,i + (n < 0)) +
				  _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
			s = _.join(dec);    } else {
			s = s.replace('.', dec);
		}
	 
		var decPos = s.indexOf(dec);    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
			s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
		}
		else if (prec >= 1 && decPos === -1) {
			s += dec+new Array(prec).join(0)+'0';    }
		return s;
	}
	
	//P
	var puedoCambiarPaso = function(paso){

		if(paso > 2 && !logueado){ 

			return false; 
		}
		else if(paso > 3 && (tipoDireccion == 'nueva' && errorDireccion)){ 
			return false; 
		}		
		else{ return true; }
	}
	
	//Q
	this.quitarProducto = function(clave,e){
		var cookProducto = oCookie.get('productosCarro');
		var ArrProducto = new Array();
		if(cookProducto)ArrProducto = unserialize(cookProducto);
		
		var total = parseFloat($('totalCarro').value) - (parseFloat(ArrProducto[clave]['cantidad'])*parseFloat($('precioProducto' + clave).value)) ;
		$('totalCarro').value = total;
		$('totalCarroForm').innerHTML = formatoNumero(total,ID_IDIOMA);
		$('lineaProducto' + clave).parentNode.removeChild($('lineaProducto' + clave));
		delete(ArrProducto[clave]);
		var serCook = serialize(ArrProducto);
		oCookie.set('productosCarro', serCook);
		var i,indexTemp = 0;
		
		for(i in ArrProducto){
			indexTemp++;
		}

		if(indexTemp == 0){
			$('contConProductos').style.display = 'none';
			pasoActual = '';			
			this.leerPaso(1);
			
		}
		
		if(e)StopEvent(e);
		
	}	
	
	//R	
	var recalcularTotal = function(){
		var i;
		var cookProducto = oCookie.get('productosCarro');
		var ArrProducto = new Array();
		if(cookProducto){
			ArrProducto = unserialize(cookProducto);
		}
		/*
		var total = 0;
		for(i in ArrProducto){
			total += ($('cantidadCompra') + )
		}*/
		
	}
	
	//S
	this.setLogueado = function(){
		logueado = true;	
	}
	this.setTipoPedido = function(tipo){
		tipoPedido = tipo;
	}
	
	//U
	this.unsetProductos = function(){
		var cookProducto = oCookie.get('productosCarro');
		var ArrProducto = new Array();
		var serCook = serialize(ArrProducto);
		oCookie.set('productosCarro', serCook);
	}
	
	
	var archivo = DIR_ROOT + 'requests/carroCompra.php';
	

	var oZoom = null;
	var req = null;
	var cargando = false;
	var accion = '';
	var intervaloMostrar = 0;
	var productosIniciados = false;
	var pasoActual = 1;
	var listo = true;
	var logueado = false;
	var tipoDireccion = null;
	var errorClass = 'fieldsetError';
	var errorDireccion = false;
	var erEmail = RegExp('^[a-z0-9_\.\-]+@[a-z0-9_\-]+(\.[a-z0-9_\-]{2,5})*\.[a-z]{2,4}$');
	var erNumero = RegExp('^[0-9]+$');
	var erFecha = RegExp('^(0[1-9]|[1-2][0-9]|3[0-1])/(0[1-9]|1[0-2])/[1-2][0-9]{3}$');
	var tipoPedido = 'producto';
	
	var req = new Request(listener);

}
