$(document).ready(function() {
	var formConfig = {
 	    contact : [['looking', 'I am looking to' ],
	              ['timeframe','My timeframe is'],
	              ['first_name', 'First Name', 't' ],
	              ['last_name', 'Last Name', 't'],
	              ['email', 'Email', 'e'],
	              ['home_phone', 'Home Phone', 'n'],
	              ['work_phone ', 'Work Phone'],
	              ['cell_phone', 'Cell Phone'],
	              ['comments', 'Comments' ]],

		   dream_house_finder : [['first_name', 'First Name', 't'],
								['last_name', 'Last Name', 't'],
								['email', 'Email', 'e'],

								['street', 'Street'],
								['city', 'City'],
								['state', 'State'],
								['zip_code', 'ZIP Code'],

								['home_phone', 'Home Phone' ],
								['work_phone', 'Work Phone'],
								['cell_phone', 'Cell Phone'],

								['looking', 'I am looking to' ],
								['timeframe','My timeframe is'],

								['Property Search City', 'Property Search City'],
								['Price Range',' Price Range'],
								['Property Type', 'Property Type'],

								['bedrooms','# of Bedrooms'],
								['bathrooms','# of Bathrooms'],
								['type_of_garage','Type of Garage'],
								['property_sq_ft','Property Sq. Ft.'],
								['lot_size','Lot Size (acres)'],
								['age_in_years','Age in Years'],
								['additional_comments','Additional Comments']],

	          market_analysis : [['first_name', 'First Name', 't'],
								['last_name', 'Last Name', 't'],
								['email', 'Email', 'e'],

								['street', 'Street'],
								['city', 'City'],
								['state', 'State'],
								['zip_code', 'ZIP Code'],

								['home_phone', 'Home Phone' ],
								['work_phone', 'Work Phone'],
								['cell_phone', 'Cell Phone'],

								['looking', 'I am looking to' ],
								['timeframe','My timeframe is'],

								['Property Search City', 'Property Search City'],
								['Price Range',' Price Range'],
								['Property Type', 'Property Type'],

								['bedrooms','# of Bedrooms'],
								['bathrooms','# of Bathrooms'],
								['type_of_garage','Type of Garage'],
								['property_sq_ft','Property Sq. Ft.'],
								['lot_size','Lot Size (acres)'],
								['age_in_years','Age in Years'],
								['additional_comments','Additional Comments']],

		buyer_reports: [['first_name', 'First Name', 't'],
						['last_name', 'Last Name', 't'],
						['email', 'Email', 'e'],

						['home_phone', 'Home Phone' ],
						['work_phone', 'Work Phone'],
						['cell_phone', 'Cell Phone'],

						['common_home_buying_mistakes', 'Common Home Buying Mistakes'],
						['finding_your_home', 'Finding Your Home'],
						['moving_and_packing_tips', 'Moving And Packing Tips'],
						['thinking_about_buying_your_first_home', 'Thinking About Buying Your First Home'],
						['what_happens_at_closing', 'What Happens At Closing']],

      seller_reports: [['first_name', 'First Name', 't'],
						['last_name', 'Last Name', 't'],
						['email', 'Email', 'e'],

						['home_phone', 'Home Phone' ],
						['work_phone', 'Work Phone'],
						['cell_phone', 'Cell Phone'],

						['common_home_buying_mistakes', 'Common Home Buying Mistakes'],
						['finding_your_home', 'Finding Your Home'],
						['moving_and_packing_tips', 'Moving And Packing Tips'],
						['thinking_about_buying_your_first_home', 'Thinking About Buying Your First Home'],
						['what_happens_at_closing', 'What Happens At Closing']],

		schedule_showing : [['first_name', 'First Name', 't' ],
							['last_name', 'Last Name', 't'],
							['email', 'Email', 'e'],
							['phone', 'Phone', 'n']]




	};
	var location = '' + window.location;


	var formName = location.replace(/\.html$/,'').replace(/^.*\//,'');
	var items = formConfig[formName];
	var form = $($('form[name="' + formName + '_form"]')[0]);
		
	if(!form || !items)
		return;

	function validate(){
		var invalid = [];

	   $(items).each(function(){
		   var nodeName = this[0];
		   var require = this[2];
		   if(require){
			 var value = $(form.find('[name="' + nodeName + '"]')[0]).val();
			 var isValid;

			 switch(require){
			 	case 't': isValid = value.match(/\w/); break;
			 	case 'e': isValid = value.match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/); break;
			 	case 'n': isValid = value.length > 0 && !value.match(/[^0-9\s-]/); break;
			 }
			if(!isValid)
				invalid.push(this[1]);
		   }
	   });

	   $('#error').empty();
	   if(invalid.length > 0){
		   $('#error').html('<br/><div>Please, specify correctly following fields:</div>' + invalid.join(', '));
		    return false;
		   
	   } else
		   return true;
	   	};

	$(form.find("input[type='submit']")[0]).bind('click', function(){
		 return validate();
	});

});

