var Form = {
	form: null,
	forceCapital: function (elementID) {
		$(elementID).addEvent('keyup', function () { Form.upperCase(this)});
		$(elementID).addEvent('blur', function () { Form.upperCase(this)});
	},
	upperCase: function (element) {
		element.setProperty('value', element.getProperty('value').toUpperCase());
	},
	init: function () {
		// Needed for smoothbox closing
		if ($('TB_closeWindowButton')) {
			$('TB_closeWindowButton').onclick = TB_remove;
		}

		$$('.btnSubmit').each ( function (el) {
			el.addEvent('click', function (event) {
				event = new Event(event);
				event.stop();
				// Retrieve the Form
				el = Form.getForm(el)
				el.fireEvent('submit');
			});
			
			//Retrieve the Form
			el = Form.getForm(el);
			
			//Add event catching on submit
			el.addEvent('submit', function () {
				//Send the form
				form = this
				this.send({
					onComplete: function(response) {
						var response = eval( '(' + response + ')' );
						if (response.result == 0) {
							$('error').setStyle('display','block');
							$('error').setHTML(response.message);
						} else {
							$('content').setHTML(response.message);
						}
					}
				});
			});
		});
	},
	
	addToCart: function () {
		if ($('addToCart')) {
			$('addToCart').addEvent('click', function (event) {
				event = new Event(event);
				event.stop();

				// Retrieve the form
				var form = Form.getForm($('addToCart'))
				form.submit();
				
				return false;
			});
		}
	},
	
	selectAll: function () {
		if ($('selectAll')) {
			$('selectAll').addEvent('click', function (event) {
			
				var form = Form.getForm($('addToCart'));
				var checkValue = this.checked;
				form.getElements('input').each( function(element) {
					element.checked = checkValue;
				});
			});
		}
	},

	validForm: function () {
		if ($('validForm') || $('validForm2')) {
			$$('#validForm input, #validForm2 input').each(function (element) {
				element.addEvent('keyup', function (event) {
					event = new Event(event);
					
					if (event.key == 'enter') {
						event.stop();
					
						// Retrieve and submit the form
						var form = Form.getForm(element)
						form.submit();
	
						return false;
					}
				});
			});

			$('validForm').addEvent('click', function (event) {
				event = new Event(event);
				event.stop();

				// Retrieve and submit the form
				var form = Form.getForm($('form'))
				form.submit();
								
				return false;
			});

			if ($('validForm2')) {
				$('validForm2').addEvent('click', function (event) {
					event = new Event(event);
					event.stop();
					
					// Change PDF value
					$('pdf').setProperty('value', 1);
					
					// Retrieve and submit the form
					var form = Form.getForm($('form'))
					form.submit();
									
					return false;
				});
			}
		}
	},
	
	validLoginForm : function () {
		if ($('validLoginForm')) {
		
			$$('input').each(function (element) {
				element.addEvent('keyup', function (event) {
					event = new Event(event);
					
					if (event.key == 'enter') {
						event.stop();
					
						// Retrieve and submit the form
						var form = Form.getForm(element)
						form.submit();
	
						return false;
					}
				});
			});

			$('validLoginForm').addEvent('click', function (event) {
				event = new Event(event);
				event.stop();

				// Retrieve the form
				var form = Form.getForm($('loginForm'))
				form.submit();
				
				return false;
			});
		}
	},
	
	/**
	* Return the parent formular
	*/
	getForm: function (el) {
		//Retrieve the Form
		try {					
			while (el.tagName != 'FORM') {
				el = el.getParent();
			}
		} catch(e){
			console.log(e.message)
			console.debug(el)
		}
		return el;
	},

	displayAddress: function () {
		if ($('billingFormButton')) {
			$('billingFormButton').addEvent('click', function (event) {
				event = new Event(event);
				event.stop();
				if ($('billingForm').getStyle('display') == 'block') {
					$('billingForm').setStyle('display', 'none');
				} else {
					$('billingForm').setStyle('display', 'block');
				}
			});

			$$('#billingForm input, #shippingForm input').each( function (el) {
				el.addEvent('keyup', function (event) {
					event = new Event(event);
					if (event.key == 'enter') {
						event.stop();

						el = Form.getForm(el)
						el.fireEvent('submit');
					}
				});
			});
		}
		if ($('shippingFormButton')) {
			$('shippingFormButton').addEvent('click', function (event) {
				event = new Event(event);
				event.stop();

				if ($('shippingForm').getStyle('display') == 'block') {
					$('shippingForm').setStyle('display', 'none');
				} else {
					$('shippingForm').setStyle('display', 'block');
				}
			});
		}
		
		$$('.submitAddress').each ( function (el) {
			el.addEvent('click', function (event) {
				event = new Event(event);
				event.stop();
				// Retrieve the Form
				el = Form.getForm(el)
				el.fireEvent('submit');
			});

			//Retrieve the Form
			el = Form.getForm(el);

			//Add event catching on submit
			el.addEvent('submit', function (event) {
				//Send the form
				this.send({
					onComplete: function(response) {
						var response = eval( response );
						if (response.result == 0) {
							$(response.type+'error').setStyle('display', 'block');
							$(response.type+'error').setHTML(response.message);
						} else {
							$(response.type+'error').setStyle('display', 'none');
							document.location = response.message;
						}
					}
				});
			});
		});
	}
}
