/*
GreenJack v1.0 - Expression Engine FreeForm with AJAX
by Mark Cianciulli of Greenhouse Studio - Jacksonville, Florida

Copyright 2009 Greenhouse Studio

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
	
Read all about this script and check for updates here:
http://www.gogreenhouse.com/software/greenjack

Send us any modifications you do to the script, so we can add it to the base code.

- Requires Expression Engine - http://expressionengine.com
- Requires the FreeForm module for EE - http://www.solspace.com/software/detail/freeform/
- Requires on JQuery Min : http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
- Requires on JQuery Form : http://jquery.malsup.com/form/jquery.form.js
*/

// Settings

jQuery(document).ready(function(){
	// Create Array Of Required Fields
	var _mailing_formFieldsRequired = new Array();
	// Store Submit Button Value;
	// Create Hidden Div for FreeForm Error Messages
	jQuery('body').append('<div id="mailinglistajaxreturn" style="display:none;"></div>');
	// Get required fields - build array		
	jQuery(':input[rel]').each(function(i) {
		_mailing_formFieldsRequired[i] = jQuery(this).attr('rel');
	});		
	// Submit form
	jQuery('#mailinglist_form').ajaxForm({  
		target: '#mailinglistajaxreturn', 
		beforeSubmit:function() {		
			jQuery(':input[rel]').each(function(i) {
				jQuery(this).removeClass('fielderror');
				_field = jQuery(this).attr('rel');
				jQuery('#mllabel_' + _field).removeClass('labelerror');
				jQuery('#mlerror_' + _field).empty();
				jQuery('input[type=submit]').attr('disabled', 'disabled')
			});	
	},
	success:function(response) {
	
		if (response=="success") {
			//jQuery('#mailinglist_form').resetForm();
			jQuery('#hiddenthankyouformailinglist').load('/_embeds/thankyou',
				function () {
					jQuery('#signupfooter').fadeOut('slow',
						function () {
							jQuery('#hiddenthankyouformailinglist').fadeIn('slow');
				}); 
			}); 
				
		} else {
		
			// Loop through required fields
			jQuery(_mailing_formFieldsRequired).each(function(e) {			
				// Set Field Checking
				var _check = _mailing_formFieldsRequired[e];
				var myRegExp = new RegExp(_check, 'gi');
				// Loop through error list from freeform
				jQuery('#mailinglistajaxreturn #contentleft ul li').each(function(i) {
					// Compare Field to Error Message
					if ( jQuery(this).html().search(myRegExp) > 0 ) {
						// Show Errors on Form
						_errormessage = jQuery(this).html();
						jQuery('input[name='+_check+']').addClass('fielderror').fadeIn('slow');
						jQuery('#mlerror_' + _check).html(_errormessage).fadeIn('slow');
						jQuery('#mllabel_' + _check).addClass('labelerror').fadeIn('slow');
						return false;
					} 
				});
			});
			// Enable Submit Button			
			jQuery('input[type=submit]').attr('disabled', '');
			// Clear the hidden div
			jQuery('#mailinglistajaxreturn').empty();		
		}		
	  }
	});
});



testing = function() {

jQuery('#mailinglist_form').resetForm();
			jQuery('#hiddenthankyouformailinglist').load('/_embeds/thankyou',
				function () {
					jQuery('#signupfooter').fadeOut('slow',
						function () {
							jQuery('#hiddenthankyouformailinglist').fadeIn('slow');
				}); 
			});

}



