Site.Payment = Class.create();

/**
 * All available payment methods
 */
Site.Payment.Methods = [
	'credit_card',
	'cheque'	
];

/**
* Implements user interface event handling for 
* payment method selection.
*/ 
Site.Payment.Section = Class.create(
	{
		formId : null,
		
		
		/**
		* Creates a new payment section handler
		*
		* @param formId the ID of the form to which the handler will be
		*		attached
		*/
		initialize : function(formId, enableCardBeholderType) {
			this.formId = formId;
			this.form = $(formId);
            this.enableCardBeholderType = false;
            
            if(enableCardBeholderType)
                this.enableCardBeholderType = enableCardBeholderType;
            
			this.attachEvents();
            this.form.form.addValidator(
                this.validate.bind(this)
            );
		},
		
		
		/**
		* Attaches event handlers to form elements
		*/
		attachEvents : function() {
			$A(Site.Payment.Methods).each(
				function(paymentMethod) {
					Event.observe(
						this.getButton(paymentMethod),
						'click',
						function(e) {
							this.select(paymentMethod)
						}.bindAsEventListener(this)
					);
				}.bind(this)
			);
		},
        
    	getPaymentMethod : function() {
			var paymentMethodButtons = this.form.elements['payment_method'];
			var paymentMethod = null;
			$A(paymentMethodButtons).each(
				function(button) {
					if (button.checked) {
						paymentMethod = button.value;
						throw $break;
					}
				}
			);
			return paymentMethod;
		},
        
        getCreditCardBeholderType : function() {
            var cardBeholderTypeButtons = this.form.elements['card_beholder_type'];
            var cardBeholderType = null;
            $A(cardBeholderTypeButtons).each(
                function(button) {
                    if (button.checked) {
                        cardBeholderType = button.value;
                        throw $break;
                    }
                }
            );
            return cardBeholderType;
        },
        
        /**
        * Validates the payment information
        */
        validate : function() {
        	var paymentMethod = this.getPaymentMethod();
            var result;

            if (paymentMethod === null) {
            	$(this.formId + '-payment-method-missing-message').show();
            	result = false;
			} else if (paymentMethod == 'credit_card') {
            	$(this.formId + '-payment-method-missing-message').hide();
            	if ($(this.formId + '-card-number')) {
                	result = this.validateCreditCard();
				} else {
					result = true;
				}
                
                if(this.enableCardBeholderType){
                    var cardBeholderType = this.getCreditCardBeholderType();
                
                    if( cardBeholderType === null ){
                        $(this.formId + '-beholder-card-type-missing-message').show();
                        result = false;
                    } else {
                        $(this.formId + '-beholder-card-type-missing-message').hide();
                    }
                }
                
            } else {
            	$(this.formId + '-payment-method-missing-message').hide();
                if(this.enableCardBeholderType){
                    $(this.formId + '-beholder-card-type-missing-message').hide();
                }
                result = true;
            }
            
            return result;
        },
        
        validateCreditCard : function() {
            var valid = true;
            
            if ($(this.formId + '-card-number').value == '') {
                $(this.formId + '-card-number-missing-message').show();
                valid = false;
            } else {
                $(this.formId + '-card-number-missing-message').hide();
            }
            
            if ($(this.formId + '-cardholder-name').value == '') {
                $(this.formId + '-cardholder-name-missing-message').show();
                valid = false;
            } else {
                $(this.formId + '-cardholder-name-missing-message').hide();
            }
            
            if ($(this.formId + '-security-code').value == '') {
                $(this.formId + '-security-code-missing-message').show();
                valid = false;
            } else {
                $(this.formId + '-security-code-missing-message').hide();
                
                if (!$(this.formId + '-security-code').value.match(/^[0-9]{3,4}$/)) {
                    $(this.formId + '-security-code-invalid-message').show();
                    valid = false;
                } else {
                    $(this.formId + '-security-code-invalid-message').hide();
                }            
            }            


            
            var now = new Date();
            var expiryDate = new Date();
            
            expiryDate.setDate(1);
            expiryDate.setMonth($(this.formId + '-expiry-month').value - 1);
            expiryDate.setYear($(this.formId + '-expiry-year').value);
            
            if (expiryDate.getTime() < now.getTime()) {
                $(this.formId + '-card-expired-message').show();
                valid = false;
            } else {
                $(this.formId + '-card-expired-message').hide();                
            }
            
            
            return valid;
        },
        		
		
		/**
		* Selects a payment method
		*
		* @param string selectedMethod the identifier of the method to select
		*/
		select : function(selectedMethod) {
			$A(Site.Payment.Methods).each(
				function(paymentMethod) {
					var subSection = this.getSubSection(paymentMethod);
					if (subSection) {
						if (paymentMethod == selectedMethod) {
							subSection.show();
						} else {
							subSection.hide();
						}
					}
				}.bind(this)
			);	
		},
		
		
		/**
	 	* Returns the DOM object corresponding to the radio
	 	* button for the specified payment method
	 	*
	 	* @param string paymentMethod the payment method identifier
	 	*
	 	* @return HTMLElement
	 	*/
		getButton : function(paymentMethod) {
			return $(this.form.id + '-payment-method-' + Site.toId(paymentMethod));
		},
		
		/**
	 	* Returns the DOM object corresponding to the form
	 	* section for the specified payment method
	 	*
	 	* @param string paymentMethod the payment method identifier
	 	*
	 	* @return HTMLElement
	 	*/
		getSubSection : function(paymentMethod) {
			return $(this.form.id + '-payment-section-' + Site.toId(paymentMethod));
		}
	}
);