var surveyForm = "surveyForm";
var surveyStatusSpeed = "normal";
var surveyStatus = "surveyStatus";
var surveyForms = [];

function surveySubmit(form, status){
    var formObj = null;
	var success = false;
    if(form != null && status != null){
		surveyForm = form;
		surveyStatus = status;
		
		if(typeof(surveyForm) == "string"){
			formObj = $("#"+surveyForm);
		}else if(typeof(surveyForm) == "object"){
			formObj = $(surveyForm);
		}
		if(formObj != null){ // SUBMITS A FORM
			if(!surveyForms.inArray(formObj[0])){ // PREVENTS DOUBLE SUBMISSION
				surveyForms[surveyForms.length] = formObj[0];
				
				// SUBMITS FORM VIA AJAX
				var query = "ajax=1";
				var statusPane = $("#"+surveyStatus);
				if(statusPane != null){
					query += "&ajaxFormat=1";
					statusPane.hide();
				}
				query += "&"+formObj.formSerialize();
				$.ajax({
					type: "POST",
					data: query,
					success: surveyResponse
				});
				success = true;
			}else{
				success = true;
				if(typeof window.console != "undefined" && typeof window.console.log != "undefined"){
					console.log('Survey: Double Submission Prevented...');
				}
			}
		}
	}
	if(!success){
		alert("The survey could not be sent at this time.");
	}
}

function surveyResponse(response){
	if(response.indexOf("location=") === 0){
		response = $.query(response);
		var url = urldecode(response.location);
		document.location.href = url;
	}else if(response.indexOf("success") >= 0){
		surveyReset();
	}else{
		var status = $("#"+surveyStatus);
		if(status.length > 0){
			status.html(response);
			status.fadeIn(surveyStatusSpeed);
			status.oneTime(4000, function(){
				$(this).fadeOut(surveyStatusSpeed);
			});
		}else{
			alert(response);
		}
	}   
}

function surveyReset(){}
