/**
 * requires:
 * 		Prototype.js, or other JS-library with basic set of extended built-in classes
 */

/** 
 * @namespace User Account
 */
if (typeof(u_account) == 'undefined') {
	var u_account = {};
}

/**
 * The custom event.
 * 
 * @namespace UserAccount
 * @property CUSTOMEVENTPAYMENT {String} the custom event
 * @constant
 */
u_account.CUSTOMEVENTPAYMENT = "u_account:activatePayment";

/**
 * The default value for select list.
 * 
 * @namespace UserAccount
 * @property DEFAULTVALUESELECTLISTCARDTYPE {String} the default value for card type select list
 * @constant
 */
u_account.DEFAULTVALUESELECTLISTCARDTYPE = "org.jboss.seam.ui.NoSelectionConverter.noSelectionValue";

/**
 * u_account.PaymentManager manages 'Payment' tab logic.
 * @namespace UserAccount
 * @class u_account.PaymentManager
 */
(function (){
	
	/**
	 * u_account.PaymentManager constructor.
	 * @return {u_account.PaymentManager} the new instance
	 * @constructor
	 */
	u_account.PaymentManager = function() {
		var me = this;
		this.payments = [];
		this.keys = [];
		return this;
	};
	
	u_account.PaymentManager.prototype = {
		addPaymentType: function PaymentManager_addPaymentType(paytype) {
			this.keys.push(paytype.toString());
			this.payments[paytype.toString()] = paytype;
		},
		
		initHandlers: function PaymentManager_initHandlers() {
			document.observe(u_account.CUSTOMEVENTPAYMENT, this.activatePayment.bindAsEventListener(this));
		},
		
		activatePayment: function PaymentManager_activatePayment(event) {			
			this.getPaymentByKey(event.memo.key).activate();
			for (var i = 0, len = this.keys.size(); i < len; i++) {
				var item = this.getPaymentByKey(this.keys[i]);
				if (event.memo.key != item.toString()) {
					item.inactivate();
				}
			};		
		},
		
		getPaymentByKey: function PaymentManager_getPaymentByKey(key) {
			return this.payments[key];
		}
	};
	
	/**
	 * u_account.CreditCard component logic.
	 * @namespace UserAccount
	 * @class u_account.CreditCard
	 * @return {u_account.CreditCard} the new instance
	 * @constructor
	 */
	u_account.CreditCard = function(radioId, typeId, numberId, monthId, yearId, holderId, cwNumberId) {
		this.name = "CreditCard";
		this.radioId = radioId;
		this.typeId = typeId;
		this.numberId = numberId;
		this.monthId = monthId;
		this.yearId = yearId;
		this.holderId = holderId;
		this.cwNumberId = cwNumberId;
		this.storeInputs = [];
		this.selItem = $F(this.typeId);
		this.initHandlers();
		return this;
	};
	
	u_account.CreditCard.prototype = {
		initHandlers: function CreditCard_initHandlers() {			
			$(this.typeId).onchange = this.changeSelectTypeCardList.bind(this);
			Event.observe($(this.typeId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.radioId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.numberId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.monthId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.yearId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.holderId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.cwNumberId), 'click', this.changePayment.bindAsEventListener(this));		
		},
		
		changeSelectTypeCardList: function CreditCard_changeSelectTypeCardList() {
			var type = $F(this.typeId);
			if (this.selItem != type) {							
				if (this.getStoredInputsByKey(this.selItem) == null) {
					this.storeCardInputs();
					this.clearInputs();														
				}
				this.selItem = type;				
				this.restoreInputs();				
			}
		},
		
		changePayment: function CreditCard_changePayment(event) {
			event.target.fire(u_account.CUSTOMEVENTPAYMENT, {key: this.toString()});
		},
		
		activate: function CreditCard_activate() {
			$(this.radioId).checked = true;
			this.restoreInputs();
			$(this.typeId).value = this.selItem;
		},
		
		inactivate: function CreditCard_inactivate() {
			$(this.radioId).checked = false;
			this.storeCardInputs();
			this.clearInputs();
			$(this.typeId).value = u_account.DEFAULTVALUESELECTLISTCARDTYPE;
		},
		
		clearInputs: function CreditCard_clearInputs() {			
			$(this.numberId).value = '';
			$(this.monthId).value = u_account.DEFAULTVALUESELECTLISTCARDTYPE;
			$(this.yearId).value = u_account.DEFAULTVALUESELECTLISTCARDTYPE;
			$(this.holderId).value = '';
			$(this.cwNumberId).value = '';
		},
		
		storeCardInputs: function CreditCard_storeCardInputs() {
			var tempObj = {
				number: $F(this.numberId),
				month: $F(this.monthId),
				year: $F(this.yearId),
				holder: $F(this.holderId),
				cwNumber: $F(this.cwNumberId)
			};
			this.storeInputs[this.selItem] = tempObj;
		},
		
		restoreInputs: function CreditCard_restoreInputs() {
			var tempObj = this.getStoredInputsByKey(this.selItem);
			if (tempObj) {
				$(this.numberId).value = tempObj.number;
				$(this.monthId).value = tempObj.month;
				$(this.yearId).value = tempObj.year;
				$(this.holderId).value = tempObj.holder;
				$(this.cwNumberId).value = tempObj.cwNumber;
				this.storeInputs[this.selItem] = null;
			}
		},
		
		getStoredInputsByKey: function CreditCard_getStoredInputsByKey(key) {
			return this.storeInputs[key];
		},
		
		toString: function CreditCard_toString() {
			return this.name;
		}
	};
	
	/**
	 * u_account.DirectDebit component logic.
	 * @namespace UserAccount
	 * @class u_account.DirectDebit
	 * @return {u_account.DirectDebit} the new instance
	 * @constructor
	 */
	u_account.DirectDebit = function(radioId, numberId, holderId, bankNumberId, bankNameId) {
		this.name = "DirectDebit";
		this.radioId = radioId;
		this.numberId = numberId;
		this.holderId = holderId;
		this.bankNumberId = bankNumberId;
		this.bankNameId = bankNameId;
		this.storedObject = {};
		this.initHandlers();		
		return this;
	};
	
	u_account.DirectDebit.prototype = {
		initHandlers: function DirectDebit_initHandlers() {			
			Event.observe($(this.radioId), 'click', this.changePayment.bindAsEventListener(this));			
			Event.observe($(this.numberId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.holderId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.bankNumberId), 'click', this.changePayment.bindAsEventListener(this));
			Event.observe($(this.bankNameId), 'click', this.changePayment.bindAsEventListener(this));					
		},
		
		activate: function DirectDebit_activate() {
			$(this.radioId).checked = true;
			this.restoreInputs();
		},
		
		changePayment: function CreditCard_changePayment(event) {
			event.target.fire(u_account.CUSTOMEVENTPAYMENT, {key: this.toString()});
		},
		
		inactivate: function DirectDebit_inactivate() {
			$(this.radioId).checked = false;
			this.storeDebitInputs();
			this.clearInputs();
		},
		
		toString: function DirectDebit_toString() {
			return this.name;
		},
		
		clearInputs: function DirectDebit_clearInputs() {
			$(this.numberId).value = '';
			$(this.holderId).value = '';
			$(this.bankNumberId).value = '';
			$(this.bankNameId).value = '';
		},
		
		storeDebitInputs: function DirectDebit_storeDebitInputs() {
			this.storedObject.numberId = $F(this.numberId);
			this.storedObject.holderId = $F(this.holderId);
			this.storedObject.bankNumberId = $F(this.bankNumberId);
			this.storedObject.bankNameId = $F(this.bankNameId);
		},
		
		restoreInputs: function DirectDebit_restoreInputs() {
			if (this.storedObject.numberId) {
				$(this.numberId).value = this.storedObject.numberId;
				$(this.holderId).value = this.storedObject.holderId;
				$(this.bankNumberId).value = this.storedObject.bankNumberId;
				$(this.bankNameId).value = this.storedObject.bankNameId;
			}
		}
	};
	
})();

/**
 * The utils object
 * @namespace u_account.utils
 */
u_account.utils = {};

/**
 * The numbers only allow.
 * 
 * @namespace u_account.utils
 * @property SIMPLENUMBERPATTERN {String} the pattern
 * @constant
 */
u_account.utils.SIMPLENUMBERPATTERN = "0123456789";

/**
 * The numbers or special symbol only allow.
 * 
 * @namespace u_account.utils
 * @property SPECIALNUMBERPATTERN {String} the pattern
 * @constant
 */
u_account.utils.SPECIALNUMBERPATTERN = "0123456789-";

/**
 * The 'keypress' event observer.
 * Allows input only numbers.
 * 
 * @param event {object} the 'onkeypress' event
 * @param pattern {String} the valid format, for example(####-####-####-#### or ##########)
 * @namespace u_account.utils
 * @method numberOnly
 */
u_account.utils.numberOnly = function(event, pattern) {
	var key;
	var keychar;
	
	if (window.event) {
	   key = window.event.keyCode;
	} else if (event) {
	   key = event.which;
	} else {
	   return true;
	}
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) ) {
	   return true;
	}	
	// numbers
	else if (((pattern).indexOf(keychar) > -1)) {
	   return true;
	} else {
	   return false;
	}		
}

/**
 * Creates credit card object
 * @method createCreditCard
 */
function createCreditCard(radioId, typeId, numberId, monthId, yearId, holderId) {
	return new u_account.CreditCard(radioId, typeId, numberId, monthId, yearId, holderId);
}

/**
 * Creates direct debit object
 * @method createDirectDebit
 */
function createDirectDebit(radioId, numberId, holderId, bankNumberId, bankNameId) {
	return new u_account.DirectDebit(radioId, numberId, holderId, bankNumberId, bankNameId);
}

/**
 * Creates manager object
 * @method createPaymentManager
 */
function createPaymentManager() {
	var manager = new u_account.PaymentManager();
}
