var SignupForm = Form.extend({

	// CONSTRUCTOR
	initialize: function() {
		this.parent();
		this.currentStep				= 0;
		this.triggerSubmit				= $("dom__triggerSubmit");
		this.formContainer				= $("dom__formContainer");
		this.mandatoryFields			= new Array();
		this.mandatoryFields[0]			= new Object();
		var statusContainerId			= "dom__registrationStatusMessage";
		//this.statusContainerStart		= $(statusContainerId+"__start");
		this.statusContainerComplete	= $(statusContainerId+"__complete");
		this.fieldPassword				= $("customerPassword");
		this.fieldConfirmPassword		= $("customerPassword2");
		this.fieldEmail					= $("customerEmail");
		this.fieldConfirmEmail			= $("customerEmail2");
		
		this.__createMandatoryFields();
		this.__checkFields();
		
		if(this.validateFormFields.length > 0) {
			this.__checkStep();
		}
		
		if(this.triggerCompletion) {
			this.triggerCompletion.addEvents({
				"change": 	function() { this.__unlockCompletion(); }.bind(this),
				"click":	function() { this.__unlockCompletion(); }.bind(this)
			});
		}
		
		if(this.triggerSubmit) {
			this.triggerSubmit.addEvent("click", function() {
				var form = $("dom__prefsForm");
				form.action = "index.php";
				form.submit();
			});
		}
		
		if(this.fieldEmail && this.fieldConfirmEmail) {
			this.fieldEmail.addEvents({
				"focus": function() { this.__checkEmail(); }.bind(this),
				"blur"	: function() { this.__checkEmail(); }.bind(this)
			});
			this.fieldConfirmEmail.addEvents({
				"focus": function() { this.__checkEmail(); }.bind(this),
				"blur"	: function() { this.__checkEmail(); }.bind(this)
			});
		}
			
		if(this.fieldPassword && this.fieldConfirmPassword) {
			this.fieldConfirmPassword.addEvents({
				"focus": function() { this.__checkPassword(); }.bind(this),
				"blur"	: function() { this.__checkPassword(); }.bind(this)
			});
		}
		
	},
	
	// PUBLIC MEMBER FUNCTIONS
	
	
	// PROTECTED MEMBER FUNCTIONS
	__checkStep: function() {
		var self		= this;
		var validator	= new Validator();
		
		this.validateFormFields.each(function(field) {
			field.addEvents({
				'keyup': function() {
					self.mandatoryFields[self.currentStep][field.id] = (validator.validateInput(this, true)) ? true : false;
					//self.__checkEmail();
					self.__checkPassword();
					//self.__unlockCompletion();
				},
				'change': function() {
					self.mandatoryFields[self.currentStep][field.id] = (validator.validateInput(this, true)) ? true : false;
					//self.__checkEmail();
					self.__checkPassword();
					//self.__unlockCompletion();
				},
				'click': function() {
					self.mandatoryFields[self.currentStep][field.id] = (validator.validateInput(this, true)) ? true : false;
					//self.__checkEmail();
					self.__checkPassword();
					//self.__unlockCompletion();
				}
			});
		});
	},
	
	__checkFields: function() {
		var self		= this;
		var validator	= new Validator();
		
		this.validateFormFields.each(function(field) {
			self.mandatoryFields[self.currentStep][field.id] = (validator.validateInput(field, false)) ? true : false;
			//self.__unlockCompletion();
		});
	},
	
	
	__unlockCompletion: function() {
		var self 					= this;
		var isComplete				= true;
		
		for(var field in this.mandatoryFields[this.currentStep]) {
			if(this.mandatoryFields[this.currentStep][field] == false)
				isComplete = false;
		}
		
		if(isComplete == true) {
			this.statusContainerStart.style.display		= "none";
			this.statusContainerComplete.style.display	= "block";
		} else {
			this.statusContainerStart.style.display		= "block";
			this.statusContainerComplete.style.display	= "none";
		}
	},
	
	
	__showCompletion: function() {
		this.statusContainerStart.style.display		= "none";
		this.statusContainerComplete.style.display	= "block";
	},
	
	__checkEmail: function() {
		if(this.fieldConfirmEmail.value != this.fieldEmail.value)
			this.mandatoryFields[this.currentStep][this.fieldConfirmEmail.id] = false;
		
	},
	
	__checkPassword: function() {
		if(this.fieldConfirmPassword.value != this.fieldPassword.value)
			this.mandatoryFields[this.currentStep][this.fieldConfirmPassword.id] = false;
	},
	
	// PRIVATE MEMBER FUNCTIONS
	__createMandatoryFields: function() {
		this.mandatoryFields[0]["customerTitle"]			= false;
		this.mandatoryFields[0]["customerName"]				= false;
		this.mandatoryFields[0]["customerFirstname"]		= false;
		this.mandatoryFields[0]["customerPassword"]			= false;
		this.mandatoryFields[0]["customerPassword2"]		= false;
		
		this.mandatoryFields[0]["customerAddress"]			= false;
		this.mandatoryFields[0]["customerCity"]				= false;
		this.mandatoryFields[0]["customerZIP"]				= false;
		this.mandatoryFields[0]["customerState"]			= false;
		this.mandatoryFields[0]["customerCountryId"]		= false;
		
		this.mandatoryFields[0]["customerJobFunction"]		= false;
		this.mandatoryFields[0]["customerCompany"]			= false;
		
		this.mandatoryFields[0]["customerEmail"]			= false;
		this.mandatoryFields[0]["customerEmail2"]			= false;
		this.mandatoryFields[0]["customerDirectPhone"]		= false;
	}
});

