QuickSale.RegistrationForm = function(){
	var that;
	var interestsRequired;
	var fundingAndDealSizeRequired;
	
	return {
		init: function(){
			that = this;
			this.interestsRequired = true;
			this.fundingAndDealSizeRequired = false;
			
			jQuery('#registrationForm').submit(function(){
				return that.validate();
			});
		},
		
		preselectCheckbox: function(id){
			var theCheckbox = jQuery('#rg_int_'+id);
			if(theCheckbox){
				theCheckbox.attr('checked', true);
			}
		},
		
		setInterestsRequired: function(req){
			that.interestsRequired = req;
		},
		
		enableFundingAndDealSize: function(req){
			that.fundingAndDealSizeRequired = req;
		},
		
		validate: function(){
			var email = jQuery("#rg_email").val();
			var emailv = jQuery("#rg_emailv").val();
			var fname = jQuery("#rg_fname").val();
			var lname = jQuery("#rg_lname").val();
			var hphone = jQuery("#rg_hphone").val();
			var wphone = jQuery("#rg_wphone").val();
			
			errors = [];
			if( email != emailv ){
				errors.push("Email addresses do not match");
			}
			
			if( ! /^((?:(?:(?:[a-zA-Z0-9][\.\-\+_]?)*)[a-zA-Z0-9])+)\@((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$/.test(email) ){
				errors.push("Please enter a valid email address.");
			}
			
			if( jQuery.trim(fname) == '' ){
				errors.push("Please enter your first name.");
			}
			
			if( jQuery.trim(lname) == '' ){
				errors.push("Please enter your last name.");
			}
			
			if( jQuery.trim(hphone) == '' && jQuery.trim(wphone) == '' ){
				errors.push("Please enter at least one of phone or mobile phone.");
			}
			
			if( that.interestsRequired ){
				if(jQuery('#rg_int_agent').attr('checked') == false &&
					jQuery('#rg_int_buyer').attr('checked') == false &&
					jQuery('#rg_int_seller').attr('checked') == false){
						errors.push("Please select your areas of interest");
				}
			}
			
			if( that.fundingAndDealSizeRequired ){
				var funding = jQuery('#rg_availablefunding').val();
				var dealSize = jQuery('#dealsize').val();
				if( jQuery.trim(funding) == '' || jQuery.trim(funding) == '$'){
					errors.push("Please enter you available funding.");
				}
				if( dealSize == "0" ){
					errors.push("Please select the deal size you are interested in.");
				}
			}
			
			if( errors.length > 0 ){
				var errorDiv = jQuery('#rg_errors');
				errorDiv.empty();
				jQuery(errors).each(function(i){
					errorDiv.append("<li> - "+errors[i]+"</li>");
				});
				errorDiv.show("fast");
				return false;
			}
		}
	}
}();
