/**************************************************************************************************
* Goldfish Ajax Formular Mailer (works only with jQuery)
* This javascript is created by Goldfish from Fishbeam Software: http://www.fishbeam.com
* All rights reserved. © 2008 Yves Pellot
**************************************************************************************************/

function getFormValues(formId) {
	var res=new Object();
	$("#"+formId+" :input").each(function(){
  		if($(this).is("input") && ($(this).attr("type")=="checkbox" || $(this).attr("type")=="radio")) {
  			if($(this).attr("checked")) {
  				res[$(this).attr("name")]=$(this).attr("value"); //Checkbox & radiobox
  			}
  		}
  		else if($(this).is("select")) {
  			var key=$(this).attr("name");
  			key=key.substring(0, key.lastIndexOf("[]"));
  			var count=0;
  			$.each($(this).children(), function(){
  				if($(this).attr("selected")) {
  					res[key+"["+count+"]"]=$(this).attr("value"); //Select
  					count++;
  				}
  			});
  		}
  		else {
  		  	res[$(this).attr("name")]=$(this).attr("value"); //Textarea, other inputs
  		}
	});
	return res;
}

$(document).ready(function(){
	$(".form").submit(sendAjaxForm);
});

function sendAjaxForm() {
	var formDiv=$(this).parent();
	var res=getFormValues($(this).attr("id"));
	
	$.post(pathToSupport+"forms/"+$(this).attr("id")+".php", res, function(data) {
		showSpinner(function(){
			$(formDiv).fadeOut("slow", function(){
				$(formDiv).html(data);
				$(".form").submit(sendAjaxForm);
				$(formDiv).fadeIn("slow", function(){
					hideSpinner();
				});
			});
		}, $(formDiv));
   	});
	
	return false;
}