$(document).ready(function(){
	initForm();
	
	$(".findform").each(function() {
		$(this).click(function() {
			
			geturl = "/?module=form&form_id=" + $(this).attr("rel");
			
			var me = this;
			$.get(geturl, function(da) {
				$(".getform").html($("#small", da).html());
				$(".getform").show();
				if($.browser.msie) {
					$(".quote").hide();
				}
				initForm();
			});
			
			return false;
		});
	});
	
	$(".getform").each(function() {
		
		geturl = "?module=form&form_id=" + $(this).attr("rel");
		
		var me = this;
		$.get(geturl, function(da) {
			$(me).html($(".theform", da).html());
			initForm();
		});
	});
});
function getNum() {
	var rand_no = Math.random();
	rand_no = Math.floor(rand_no * 10000);
	return rand_no;
}
function initForm() {
	$(".getform").each(function(i) {
		fn = getNum();
		$(this).attr("id", "randform" + fn);
		var tabs = $(this).find(".formtabs");
		var frm = $(this).find("form");
		var submits = frm.find(".fsubmit");
		var fieldsets = frm.find("fieldset");
		
		var trg = $(this).attr("id");
		frm.attr("reltheform", trg);
		
		var options = { 
	        //target: $(this)
	          beforeSubmit:  initRequest
	        , success:       showResponse  // post-submit callback 
	    }; 
	    
		$(frm).ajaxForm(options); 
	});
}
// post-submit callback 
var formtarget;
function initRequest(formData, jqForm, options) { 
	formtarget = $("#" + jqForm.attr("reltheform"));
}
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
	
	out = $("#small", responseText).html();
    $(formtarget).html(out);
    
    initForm();
} 
