/*-----------------------------------------------------------------------------------------------
form validation
Author: Andy Sherman
Client: dekalb
Descrip: certForm w/ajax submission [js enabled browsers]
-------------------------------------------------------------------------------------------------*/

function formSubmit(formObj) {
	jQuery(document).ready(function() {
		//html for feedback  container that is appended
		var feedbackHTML = "<div class=\"formFeedback\"><span></span></div>";
		var nullAllowList = new Array(); //array of elements that are allowed to be null -> data gathered from rel="m"(m=mandatory)
		var errorList = new Array(); //array of elements that fail validate test and not in nullAllowList
		var nullAllow = true; //variable for determining error element in nullAllow loop
	
		//get all input fields of type text
		var formFields = $(formObj).find("input:text");
		
		//loop to assign clearField function to elements + add non-required elements to nullAllowList
		jQuery.each(formFields, function() {
			//assign onfocus="clearField" funct to each attribute (if not already present)
			if(!$(this).attr("onfocus")) {
				$(this).attr("onfocus","clearField(this);");
			}
			
			//check if element has class=m [mandatory field]-> if false add element to nullAllowList
			if(!$(this).is("."+"m")) {
            	nullAllowList.push($(this));
            }
		});
		
		//loop through inputs to check for validity and add error class if not complete
		jQuery.each(formFields, function() {
			if($(this).attr("value") == "" || $(this).attr("value") == "Required") {
				//current loop element has no value so loop through nullAllowList to see if its allowed null
				for(x=0;x<= (nullAllowList.length -1);x++){
					if($(this).attr("id") == $(nullAllowList[x]).attr("id")) {
						//this input is on the nullAllowList so break loop and set nullAllow to true
						nullAllow = true;
						break;	//bomb out of loop as no need to do further checks
						//alert("elementNullAllow: "+$(this).attr("id"));
					}else{
						//this element is null and not on nullAllowList so fail it
						nullAllow = false;
						//alert("this field is NOT allowed to be null: "+$(this).attr("id")+" "+$(nullAllowList[x]).attr("id"));
					}
				}
				//assign error based attr if input fails null test
				if(!nullAllow) {
					$(this).addClass("error");
					$(this).attr("value", "Required");
					//assign this element to the array holding elements that failed validation
					errorList.push($(this));
				}
			}
		});
		//alert(errorList.length);
		
		if(errorList.length == 0) { //no elements in error list means no elements errored out [were null]
			//alert("inside ajax");
			//append feedback
			$(formObj).append(feedbackHTML);
			$('.formFeedback span').text("Sending Information...");
			//form data
			var formData = $(formObj).serialize();
			//formType->check which form this is and send to correct handler
			var handlerPath;
			if($(formObj).attr("id") == "raForm") {
				handlerPath =  "/reqQuoteHandler.php";
			}else{
				handlerPath = "/contactHandler.php";
			}
			
			//ajax
			$.ajax({
				type: "POST",
				url: handlerPath,
				//url: "https://www.formstack.com/api/submit?api_key=77A4A67B16B48C9F071700F36C16EECB&id=956684&field_9185949=testFromForm",
				data: formData,
				success: function(html){
					//show success message
					$('.formFeedback').addClass('sent');
					$('.formFeedback span').text(html);
					//hide box, reset field
					//setTimeout('resetForm(formObj)',2000);
					//use 'closure'
					setTimeout(function(){resetForm(formObj); parameter = null},2000);
				}
			});
			//call google analytics function to log which form was accessed
			switch ($(formObj).attr("id")) {
				case "raForm":
					//requestQuoteForm
					gaPageTrackCall('Quote','Quote-Submit');
					break;
				case "contactForm":
					//contact page
			  		gaPageTrackCall('Contact-Page','Contact-Submit');
			  		break;
				default:
					//cta form include
			  		gaPageTrackCall('Secondary-Contact','Secondary-Submit');
			}			
		}else{
			//errors in form so do nothing->error fields are marked from .each loop above
		}
	});
	//stop from moving to handler
	return false;
}

function resetForm(formObj) {
	jQuery(document).ready(function() {
		//re-declare formFields variable [separate function can read submits var]
		var formFields = $(formObj).find("input:text");
		var textareas = $(formObj).find("textarea");
		
		//reset all input values
		jQuery.each(formFields, function() {
			$(this).attr("value","");
			if($(this).hasClass('error')){
				$(this).removeClass('error');
			}
		});
		jQuery.each(textareas, function() {
			$(this).attr("value","");
		});
		
		//remove html for feedback div
		$('.formFeedback').remove();
		//check which form is requesting and close if raForm
		if($(formObj).attr("id") =="raForm") {
			$('#reqQuoteBox').toggle();
		}
	});
}

