
var Password = Class.create( {
	initialize: function(container,type ,validated,eventCode,priceTypeCode,callback ,initCallback ) {
		this.container = container;
		this.type = type;	
		this.eventCode = eventCode;
		this.priceTypeCode = priceTypeCode;
		this.callback = callback;
		this.validated = validated;
		this.clones = new Array();
		this.password = this.container.select('[type="password"]').first().value;
		if(initCallback&&!validated)
			initCallback(priceTypeCode);
		Event.observe(this.container.select('[type="password"]').first(),'keypress',this.submitOnEnter.bindAsEventListener(this));
		Event.observe(this.container.select('[type="button"]').first(),'click',this.validatePassword.bindAsEventListener(this));
	},
	

	submitOnEnter: function(evt){
		if (evt.keyCode==13){
			Event.stop(evt);
			Event.element(evt).parentNode.select('[type="button"]').first().click();
			return false;
		}
	},
	
	
	onSuccess: function(transport){
	  	var response = transport.responseText ;
	  	var successful;
	  	
		if("success"==response){
			this.getContainers().each(this.showPasswordCorrect.bindAsEventListener(this));
			successful = true;
			this.validated=true;
		}  else if("failure"==response){
			this.getContainers().each(this.showPasswordWrong.bindAsEventListener(this));
			successful = false;
		}
		else {
	  		this.getContainers().each(this.showPasswordError.bindAsEventListener(this));
	  		successful = false;
	  	}
		this.callback(successful,this.type,this.priceTypeCode,this.password);
	},

	onFailure: function(transport){
	  	this.getContainers().each(this.showPasswordError.bindAsEventListener(this));

	},	 
	showPasswordCorrect: function(container){
		container.select('[id="password_form"]').first().hide();
		container.select('[type="password"]').first().enable();
		container.select('[id="password_accepted"]').first().show();	
	},
	
	showPasswordWrong: function(container){
		container.select('[id="password_message_polling"]').first().hide();
		container.select('[id="password_message_wrong"]').first().show();
		container.select('[type="password"]').first().addClassName('invalid-password');
		container.select('[type="password"]').first().enable();
		container.select('[type="button"]').first().enable();
	},	
	showPolling: function(container){
		container.select('[type="password"]').first().disable();	
		container.select('[type="button"]').first().disable();
		container.select('[id="password_message_start"]').first().hide();
		container.select('[id="password_message_error"]').first().hide();
		container.select('[id="password_message_wrong"]').first().hide();
		container.select('[type="password"]').first().removeClassName('invalid-password');
		container.select('[id="password_message_polling"]').first().show();
	},
	validatePassword: function(evt){
		this.getContainers().each(this.showPolling.bindAsEventListener(this));
		this.password = Event.element(evt).parentNode.select('[type="password"]').first().value;
		this.getContainers().each(function(container){
			container.select('[type="password"]').first().value = this.password;
		}.bind(this));
		new Ajax.Request("/checkout/ajax/validatePassword.php", {
		  parameters:{
					password: this.password,
					passwordType: this.type,
					eventCode : this.eventCode,
					priceTypeCode : this.priceTypeCode
					}, 
		  onSuccess: this.onSuccess.bindAsEventListener(this),
		  onFailure: this.onFailure.bindAsEventListener(this)});	 
	},
	
	showPasswordError: function(container){
	  	container.select('[id="password_message_start"]').first().hide();
	  	container.select('[id="password_message_wrong"]').first().hide();
	  	container.select('[id="password_message_polling"]').first().hide();
		container.select('[id="password_message_error"]').first().show();
		container.select('[type="password"]').first().enable();
		container.select('[type="button"]').first().enable();
	},	  

	createPasswordClone: function(popupContainer){
		var clone = this.container.cloneNode(true);

		this.clones.push(clone);
		popupContainer.insert(clone);
		Event.observe(clone.select('[type="password"]').first(),'keypress',this.submitOnEnter.bindAsEventListener(this));
		Event.observe(clone.select('[type="button"]').first(),'click',this.validatePassword.bindAsEventListener(this));
				
	},
	getContainers: function(){
		var containers = new Array();
		containers.push(this.container);
		this.clones.each(function(clone){containers.push(clone)});
		return containers;	
	}
	
});

var PasswordPopup = Class.create({

	initialize: function(box,closeFunction){
		this.box = box;
		Event.observe(this.box.select('[class="popup-close"]').first(),'click',closeFunction);
	},
	
	togglePopup: function(){		
		var overlay = $$('.overlay').first();
		overlay.style.height = document.body.clientHeight + 'px';
		overlay.style.width = document.body.clientWidth + 'px';
		overlay.toggle();
		this.box.toggle();
	}

});
