/**
 *
 * RePromotion - Forms.js
 * Copyright © Fluid Creativity 2008
 *
 */

Forms = {

	init: function() {
		if ($('contact')) $('contact').addEvent('submit', Forms.checkContact);
		else if ($('reseller')) $('reseller').addEvent('submit', Forms.checkReseller);
		else if ($('support')) $('support').addEvent('submit', Forms.checkSupport);
		else if ($('purchase_media')) $('purchase_media').addEvent('submit', Forms.checkPurchase);
		else if ($('purchase_package')) $('purchase_package').addEvent('submit', Forms.checkPurchase);
		else if ($('download_media')) $('download_media').addEvent('submit', Forms.checkDownload);
		else if ($('download_package')) $('download_package').addEvent('submit', Forms.checkDownload);
	},

	checkPurchase: function(e) {
		if ($(e.target).get('id').test(/_([a-z]+)/)) {
			var prefix = $(e.target).get('id').match(/_([a-z]+)/)[1] + '_';
		} else {
			var prefix = '';
		}
				
		if (Validator.isEmpty($(prefix + 'name'), "Please enter your name")) e.stop();
		else if (Validator.isEmpty($(prefix + 'address'), "Please enter your address")) e.stop();
		else if (Validator.isEmpty($(prefix + 'city'), "Please enter your city")) e.stop();
		else if (Validator.isEmpty($(prefix + 'country'), "Please enter your country")) e.stop();
		else if (Validator.isEmpty($(prefix + 'zip'), "Please enter your postcode")) e.stop();
		else if (Validator.isEmpty($(prefix + 'phone'), "Please enter your phone number")) e.stop();
		else if (!Validator.emailValidator($(prefix + 'email'), "Please enter a valid email address")) e.stop();
		else if (!Validator.madeCheck($(prefix + 'terms'), "Please accept our Terms and Conditions")) e.stop();
	},
	
	checkContact: function(e) {
		if ($(e.target).get('id').test(/_([a-z]+)/)) {
			var prefix = $(e.target).get('id').match(/_([a-z]+)/)[1] + '_';
		} else {
			var prefix = '';
		}
				
		if (Validator.isEmpty($(prefix + 'name'), "Please enter your name")) e.stop();
		else if (Validator.isEmpty($(prefix + 'message'), "Please enter your message")) e.stop();
		else if (!Validator.emailValidator($(prefix + 'email'), "Please enter a valid email address")) e.stop();
		else if (Validator.isEmpty($(prefix + 'phone'), "Please enter your phone number")) e.stop();
		
	},
	
	checkReseller: function(e) {
		if ($(e.target).get('id').test(/_([a-z]+)/)) {
			var prefix = $(e.target).get('id').match(/_([a-z]+)/)[1] + '_';
		} else {
			var prefix = '';
		}
			
		if (Validator.isEmpty($(prefix + 'name'), "Please enter your name")) e.stop();
		else if (Validator.isEmpty($(prefix + 'company'), "Please enter your company")) e.stop();
		else if (!Validator.emailValidator($(prefix + 'email'), "Please enter a valid email address")) e.stop();
		else if (Validator.isEmpty($(prefix + 'phone'), "Please enter your phone number")) e.stop();
	},
	
	checkSupport: function(e) {
		if ($(e.target).get('id').test(/_([a-z]+)/)) {
			var prefix = $(e.target).get('id').match(/_([a-z]+)/)[1] + '_';
		} else {
			var prefix = '';
		}
				
		if (Validator.isEmpty($(prefix + 'name'), "Please enter your name")) e.stop();
		else if (Validator.isEmpty($(prefix + 'message'), "Please enter your message")) e.stop();
		else if (!Validator.emailValidator($(prefix + 'email'), "Please enter a valid email address")) e.stop();
		else if (Validator.isEmpty($(prefix + 'phone'), "Please enter your phone number")) e.stop();
		else if (!Validator.madeSelection($(prefix + 'supplier'), "Please select a supplier")) e.stop();
	},
	
	checkDownload: function(e) {
		if ($(e.target).get('id').test(/_([a-z]+)/)) {
			var prefix = $(e.target).get('id').match(/_([a-z]+)/)[1] + '_';
		} else {
			var prefix = '';
		}
		
		if (Validator.isEmpty($(prefix + 'name'), "Please enter your name")) e.stop();
		else if (Validator.isEmpty($(prefix + 'company'), "Please enter your company")) e.stop();
		else if (!Validator.emailValidator($(prefix + 'email'), "Please enter a valid email address")) e.stop();
		else if (Validator.isEmpty($(prefix + 'phone'), "Please enter your phone number")) e.stop();
		else if (!Validator.madeCheck($(prefix + 'terms'), "Please accept our Terms and Condition")) e.stop();
	}
};

window.addEvent('domready', Forms.init);

Validator = {
	isEmpty: function(elem, helperMsg) {
		if (elem.value.length == 0) {
			alert(helperMsg);
			elem.focus();
			return true;
		} else{
			return false;
		}
	},

	isNumeric: function(elem, helperMsg) {
		var numericExpression = /^[0-9]+$/;
		
		if (!elem.value.match(numericExpression)) {
			alert(helperMsg);
			elem.focus();
			return false;	
		} else {
			return true;
		}
	},
	
	isAlphanumeric: function(elem, helperMsg) {
		var alphaExp = /^[0-9a-zA-Z]+$/;
		
		if (!elem.value.match(alphaExp)) {
			alert(helperMsg);
			elem.focus();
			return false;
		} else {
			return true;
		}
	},
	
	emailValidator: function(elem, helperMsg) {
		var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
		
		if (!elem.value.match(emailExp)){
			alert(helperMsg);
			elem.focus();
			return false;
		} else {
			return true;
		}
	},
	
	madeSelection: function(elem, helperMsg) {
		if (elem.value == "") {
			alert(helperMsg);
			elem.focus();
			return false;
		} else {
			return true;
		}
	},
	
	madeCheck: function(elem, helperMsg) {
		if (elem.checked == false){
			alert(helperMsg);
			elem.focus();
			return false;
		} else {
			return true;
		}
	},
	
	checkPostalcode: function(elem, helperMsg) {
		var size = elem.value.length;
		var test = elem.value.toUpperCase();
		
		while (test.slice(0,1) == " ") {
			test = test.substr(1,size-1);
		}
		
		while(test.slice(size-1,size) == " ") {
			test = test.substr(0,size-1);
		}
		
		elem.value = test;

		count1 = test.indexOf(" ");
		count2 = test.lastIndexOf(" ");
		
		if (size < 6 || size > 8){ 
			alert(helperMsg);
			elem.focus();
			return false;
		} else if (!isNaN(test.charAt(0))) { 
			alert(helperMsg);
			elem.focus();
			return false;
		} else if (isNaN(test.charAt(size-3))) { 
			alert(helperMsg);
			elem.focus();
			return false;
		} else if (!isNaN(test.charAt(size-2))) { 
			alert(helperMsg);
			elem.focus();
			return false;
		} else if (!isNaN(test.charAt(size-1))) { 
			alert(helperMsg);
			elem.focus();
			return false;
		} else if (!test.charAt(size-4) == " ") {
			alert(helperMsg);
			elem.focus();
			return false;
		} else if (count1 != count2) {
			alert(helperMsg);
			elem.focus();
			return false;
		} else {
			return true;
		}
	}
};
