// JavaScript Document
	var oEcommerce = new function(){
		//
		var req = new Request();
		var oTrigger = null;
		var sType = null;
		var sCode = null;
		var sData = null;
		var bWait = false;
		var sAction = '';
		var sCloseLay = '';
		var iPos = 0;
		this.sMsjConfirm = '';
		var bIni = false;
		var bCustomer = false;
		
		
		//
		this.selectPrimaryDomain = function(){
			var r1 = $('inpComPaqDomNew'), r2 = $('inpComPaqDomOth');
			if(r1.checked){
				r2.parentNode.nextSibling.style.display = 'none';
				r1.parentNode.nextSibling.style.display = 'inline';
				r1.checked = true;
			}
			else{
				r1.parentNode.nextSibling.style.display = 'none';
				r2.parentNode.nextSibling.style.display = 'inline';
				r2.checked = true;
			}
		}
		
		this.setUserType = function(){
			var r1 = $('inpComUserNew'), r2 = $('inpComUserOld'), d = $('inpComUserOldCont');
			if(r1.checked){
				d.style.display = 'none';
				r1.checked = true;
			}
			else{
				d.style.display = 'block';
				r2.checked = true;
			}
		}
		
		this.getTrigger = function(){ return oTrigger; }
		
		var getData = function(){
			var v = '';
			switch(sType){
				case 'hos':{
					v += 'fpago' + SEP_IGUAL + $('inpComPaqMet').value + SEP_AND;
					if($('inpComPaqDomNew').checked){
						v += 'nuevo' + SEP_IGUAL + 'si' + SEP_AND;
						v += 'nombre' + SEP_IGUAL + trim($('inpComPaqDomNewName').value) + SEP_AND;
						v += 'tld' + SEP_IGUAL + $('inpComPaqDomNewTLD').value + SEP_AND;
					}
					else{
						v += 'nuevo' + SEP_IGUAL + 'no' + SEP_AND;
						v += 'nombre' + SEP_IGUAL + trim($('inpComPaqDomOthName').value) + SEP_AND;
						v += 'tld' + SEP_IGUAL + trim($('inpComPaqDomOthTLD').value) + SEP_AND;
					}
					break;
				}
				case 'dom':{
					v += 'nombre' + SEP_IGUAL + trim($('inpComDomName').value) + SEP_AND;
					v += 'tld' + SEP_IGUAL + $('inpComDomTLD').value + SEP_AND;
					v += 'fpago' + SEP_IGUAL + $('inpComDomMet').value + SEP_AND;
					break;
				}
				case 'cli':{
					v += 'nombre' + SEP_IGUAL + trim($('nombreCliente').value) + SEP_AND;
					v += 'apellido' + SEP_IGUAL + trim($('apellidoCliente').value) + SEP_AND;
					v += 'empresa' + SEP_IGUAL + trim($('empresaCliente').value) + SEP_AND;
					v += 'email' + SEP_IGUAL + trim($('emailCliente').value) + SEP_AND;
					v += 'telefono' + SEP_IGUAL + trim($('telefonoCliente').value) + SEP_AND;
					v += 'fax' + SEP_IGUAL + trim($('faxCliente').value) + SEP_AND;
					v += 'celular' + SEP_IGUAL + trim($('celularCliente').value) + SEP_AND;
					v += 'direccion' + SEP_IGUAL + trim($('direccionCliente').value) + SEP_AND;
					v += 'cp' + SEP_IGUAL + trim($('cpCliente').value) + SEP_AND;
					v += 'ciudad' + SEP_IGUAL + trim($('ciudadCliente').value) + SEP_AND;
					v += 'pais' + SEP_IGUAL + $('paisCliente').value + SEP_AND;
					if($('paisCliente').value == 1){
						v += 'cuit' + SEP_IGUAL + trim($('cuitCliente').value) + SEP_AND;
						v += 'ingB' + SEP_IGUAL + trim($('ingBrutosCliente').value) + SEP_AND;
						v += 'afip' + SEP_IGUAL + $('afipCliente').value + SEP_AND;
					}
					v += 'contrasena' + SEP_IGUAL + $('contrasenaCliente').value + SEP_AND;
					v += 'contrasena2' + SEP_IGUAL + $('contrasena2Cliente').value + SEP_AND;
					break;
				}
				default: break;
			}
			return v;
		}
		
		this.setData = function(key, value){
			sData += key + SEP_IGUAL + value + SEP_AND;
		}
		
		this.unsetData = function(){
			sData = '';
		}
		
		var send = function(v){
			req.pedir(DIR_ROOT + 'requests/comprar.php', v);
		}
		
		this.selectCountry = function(){
			$('datComCliente').style.display = ($('paisCliente').value == 1)? 'block':'none';
		}
		
		//
		this.confirmClose = function(event, layer){
			if(!!event){ StopEvent(event); }
			//
			sCloseLay = layer;
			Confirmar.onAccept = closeAccept;
			Confirmar.onCancel = null;
			Confirmar.show(this.sMsjConfirm);
		}
		
		this.close = function(){
			sCloseLay = 'cliente';
			closeAccept();
			bWait = false;
			bIni = false;
			bCustomer = false;
		}
		
		var closeAccept = function(){
			var o = (sCloseLay == 'carro')? oCarro:((sCloseLay == 'resumen')? oResumen:oCliente);
			o.setBackground($('bgComprar'));
			o.close(false);
			o.onClose = closeEnd;
			bIni = false;
			bCustomer = false;
		}
		
		var closeEnd = function(){
			var o = (sCloseLay == 'carro')? oCarro:((sCloseLay == 'resumen')? oResumen:oCliente);
			o.setBackground(null);
			oComprar.setBackground($('bgComprar'));
		}
		
		this.backToCart = function(event){
			if(!!event){ StopEvent(event); }
			oResumen.close();
			oCarro.open(oTrigger, 0, false);
		}
		
		this.backToResumen = function(event){
			if(!!event){ StopEvent(event); }
			oCliente.close();
			oResumen.open(oTrigger, 0, false);
		}
		
		//
		this.buy = function(ele, type, code, event){
			if(!!event){ StopEvent(event); }
			if(bWait){ return false; }
			if(bIni){ return false; }
			bWait = true;
			//
			oTrigger = ele;
			sType = type;
			sCode = code;
			//
			var v = 'accion' + SEP_IGUAL + 'comprar' + SEP_AND;
			v += 'tipo' + SEP_IGUAL + sType + SEP_AND;
			v += 'codigo' + SEP_IGUAL + sCode + SEP_AND;
			if(!!sData){ v += sData; }
			sAction = 'buy';
			//
			send(v);
		}
		
		this.addToCart = function(event){
			if(!!event){ StopEvent(event); }
			if(bWait){ return false; }
			bWait = true;
			//
			var v = 'accion' + SEP_IGUAL + 'agregarAlCarro' + SEP_AND;
			v += 'tipo' + SEP_IGUAL + sType + SEP_AND;
			v += 'codigo' + SEP_IGUAL + sCode + SEP_AND;
			v += getData();
			sAction = 'addToCart';
			//
			send(v);
		}
		
		this.edit = function(event, pos, type){
			if(!!event){ StopEvent(event); }
			if(bWait){ return false; }
			bWait = true;
			iPos = pos;
			sType = type;
			//
			var v = 'accion' + SEP_IGUAL + 'editar' + SEP_AND;
			v += 'pos' + SEP_IGUAL + iPos + SEP_AND;
			sAction = 'edit';
			//
			send(v);
		}
		
		this.update = function(event){
			if(!!event){ StopEvent(event); }
			if(bWait){ return false; }
			bWait = true;
			//
			var v = 'accion' + SEP_IGUAL + 'actualizar' + SEP_AND;
			v += 'pos' + SEP_IGUAL + iPos + SEP_AND;
			v += getData();
			sAction = 'update';
			//
			send(v);
		}
		
		this.resumen = function(event){
			if(!!event){ StopEvent(event); }
			if(bWait){ return false; }
			//
			var v = 'accion' + SEP_IGUAL + 'resumen' + SEP_AND;
			sAction = 'resumen';
			//
			send(v);
		}
		
		this.continuation = function(event){
			if(!!event){ StopEvent(event); }
			if(bWait){ return false; }
			if(bCustomer && $('inpComUserNew').checked){
				oResumen.close();
				oCliente.open(oTrigger, 0, false);
			}
			else{
				bWait = true;
				//
				var v = 'accion' + SEP_IGUAL + 'continuar' + SEP_AND;
				v += 'cliente' + SEP_IGUAL + (($('inpComUserNew').checked)? 'nuevo':'viejo') + SEP_AND;
				v += 'usu' + SEP_IGUAL + trim($('inpComUserOldAcc').value) + SEP_AND;
				v += 'pas' + SEP_IGUAL + trim($('inpComUserOldPass').value) + SEP_AND;
				sAction = 'continuation';
				//
				send(v);
			}
		}
		
		this.finish = function(event){
			if(!!event){ StopEvent(event); }
			if(bWait){ return false; }
			bWait = true;
			//
			var v = 'accion' + SEP_IGUAL + 'terminar' + SEP_AND;
			sType = 'cli';
			v += getData();
			sAction = 'finish';
			//
			send(v);
		}
		
		//
		req.listener = function(){
			var d = req.respuestaXML;
			bWait = false;
			
			if(!d){ alert(req.respuestaHTML); }
			else if(d.getAttribute('exito') == 'si'){
				if(sAction == 'buy'){
					$('contComprar').innerHTML = d.firstChild.data;
					$('contCarro').innerHTML = '';
					$('contModificar').innerHTML = '';
					oComprar.open(oTrigger, 0, false);
				}
				//
				else if(sAction == 'addToCart'){
					bIni = true;
					$('contCarro').innerHTML += d.firstChild.data;
					oComprar.setBackground(null);
					oComprar.close();
					oCarro.open(oTrigger, 0, false);
				}
				//
				else if(sAction == 'edit'){
					$('contComprar').innerHTML = '';
					$('contModificar').innerHTML = d.firstChild.data;
					oCarro.close();
					oModificar.open(oTrigger, 0, false);
				}
				//
				else if(sAction == 'update'){
					$('contCarro').childNodes[iPos].innerHTML = d.firstChild.data;
					oModificar.close();
					oCarro.open(oTrigger, 0, false);
				}
				//
				else if(sAction == 'resumen'){
					$('contResumen').innerHTML = d.firstChild.data;
					oCarro.close();
					oResumen.open(oTrigger, 0, false);
				}
				//
				else if(sAction == 'continuation'){
					if(d.getAttribute('redir') == 'si'){
						bWait = false;
						document.location.href = d.getAttribute('dir');
					}
					else{
						$('contCliente').innerHTML = d.firstChild.data;
						oResumen.close();
						oCliente.open(oTrigger, 0, false);
						bCustomer = true;
					}
				}
				//
				else if(sAction == 'finish'){
					bWait = true;
					Exito.onAccept = this.close;
					Exito.show(d.firstChild.data);
				}
			}
			else{
				Error.onAccept = function(){  };
				Error.show(d.firstChild.data);
			}
		}.closure(this);
	}
