/*
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
var _createHiddenDivDemo = true;						// If set to false, you will need to add this to your template
var _redirectToThankYouTemplateDemo = false;				// if you want to redirect to a thank you template path to redirect to
var _toggleThankYouDivDemo = true;						// set to true, the form will be replaced by the thank you message
var _thankYouTemplatePathDemo = 'site/thankyou';			// if _redirectToThankYou is set to true - thank you template path to redirect to
var _thankYouDivDemo = 'demothankyouhidden';					// Div to display thank you message
var _formDivDemo = 'demodownloadform';						// Div containing the form
var _UseFormIDsDemo = true;						// if true, the form_id attr needs to be added to the exp:freeform:form. False for just one from per page
var _formIDDemo = 'videodemo'; 						// this is the ID of the freeform :: add attribute form_id="" to the exp:freeform:form
var _showErrorMessagesDemo = false;						// Show error messages generated by FreeForm
var _showErrorFormClassDemo = false;						// Changes class on field to show an error
var _showErrorLabelsDemo = true;						// Changes class on label to show an error
var _errorClassDemo = 'error'; 						// css class to show the form field had an error
var _errorLabelClassDemo = 'errorlabel'; 					// css class to show the form field had an error
var _onSubmitMessageDemo = 'Submitting';
var _validateFieldDemo = 'rel';						// this is the customer attribute added to the form fields that is required
var _labelPrefixDemo = 'label_'; 						// this is the id of the element to display the error message for that field
var _fieldErrorMessagePrefixDemo = 'error_'; 				// this is the id of the element to display the error message for that field
var _hiddenDivWithFFErrorsDemo = 'ffajaxreturn';				// this is the hidden div that the ajax call will load the FreeForm errors into


$(document).ready(function(){
	// Create Array Of Required Fields
	var _formFieldsRequired = new Array();
	// Store Submit Button Value;
	var _submitButtonValueOrg = $('input[type=submit]').val();
	
	// Create Hidden Div for FreeForm Error Messages
	if (_createHiddenDivDemo) $('body').append('<div id="'+_hiddenDivWithFFErrorsDemo+'" style="display:none;"></div>');
	// Get required fields - build array		
	$(':input['+ _validateFieldDemo +']').each(function(i) {
		_formFieldsRequired[i] = $(this).attr(_validateFieldDemo);
	});		
	// Submit form
	if (_UseFormIDsDemo) {_theFormIs='#'+_formIDDemo} else {_theFormIs='form'}
	$(_theFormIs).ajaxForm({  
		target: '#'+_hiddenDivWithFFErrorsDemo, 
		beforeSubmit:function() {			
			$(':input['+ _validateFieldDemo +']').each(function(i) {
				$(this).removeClass(_errorClassDemo);
				_field = $(this).attr(_validateFieldDemo);
				$('#' + _labelPrefixDemo + _field).removeClass(_errorLabelClassDemo);
				$('#' + _fieldErrorMessagePrefixDemo + _field).empty();
				// Disable Submit Button
				
				$('input[type=submit]').attr('disabled', 'disabled').val(_onSubmitMessageDemo);
				
				
				//$('input[type=submit]').attr('disabled', 'disabled').addClass(_onSubmitMessageDemo)
			});	
	},
	success:function(response) {
		if (response=="success") {
		
			// do salesforce web-to-lead ::
			var _first_name = $("#first_name").val(); 
			var _last_name = $("#last_name").val(); 
			var _title = $("#title").val(); 
			var _phone = $("#phone").val(); 
			var _email = $("#email").val(); 
			var _company = $("#company").val(); 
			var _description = $("#description").val();
			
			$.post("/ROI/gosalesforce", { 
					first_name:_first_name,
					last_name:_last_name, 
					title:_title, 
					phone:_phone, 
					email:_email, 
					company:_company, 
					description:_description
				},
				function(data){
				
					$(_theFormIs).resetForm();
					if (_redirectToThankYouTemplateDemo) {
						window.location = _thankYouTemplatePathDemo;
					} else {
						if (_toggleThankYouDivDemo) {
							$('#'+_thankYouDivDemo).load(_thankYouTemplatePathDemo,
								function () {
									$('#'+_formDivDemo).html('');
									$('#'+_formDivDemo).html($('#'+_thankYouDivDemo).fadeIn(800));
							}); 
						} else {
							//$('#'+_thankYouDivDemo).load(_thankYouTemplatePathDemo);
							//$('#'+_thankYouDivDemo).css({'display' : 'inline'});
						}
					}
					
			});		
		
			
			
		} else {
			// Loop through required fields
			$(_formFieldsRequired).each(function(e) {			
				// Set Field Checking
				var _check = _formFieldsRequired[e];
				var myRegExp = new RegExp(_check, 'gi');
				// Loop through error list from freeform
				$('#'+_hiddenDivWithFFErrorsDemo+' #content ul li').each(function(i) {
					// Compare Field to Error Message
					if ( $(this).html().replace(/ /g,'_').search(myRegExp) > 0 ) {
						// Show Errors on Form
						_errormessage = $(this).html();
						if (_showErrorFormClassDemo) $('input[name='+_check+']').addClass(_errorClassDemo).fadeIn('slow');
						if (_showErrorMessagesDemo) $('#' + _fieldErrorMessagePrefixDemo + _check).html(_errormessage).fadeIn('slow');
						if (_showErrorLabelsDemo) $('#' + _labelPrefixDemo + _check).addClass(_errorLabelClassDemo).fadeIn('slow');
						return false;
					} 
				});
			});
			// Enable Submit Button
			$('input[type=submit]').attr('disabled', '').val(_submitButtonValueOrg);
			
			// Clear the hidden div
			$('#'+_hiddenDivWithFFErrorsDemo).empty();		
		}		
	  }
	});
});

