$.fn.KForm = function()
{
	var m_this = $(this);
	
	$(".kform-success").hide();
	$(".kform-error").hide();
	m_this.find(".kform-fieldMessage").hide();
	
	m_this.bind("submit",doStuff);
	
	function doStuff()
	{
		$(".kform-success").hide();
		$(".kform-error").hide();
		m_this.find(".kform-fieldMessage").hide();
		if (checkRequired())
			doSubmit();
		return false;
	}
	
	function doSubmit()
	{
		var action = m_this.attr("action");
		var method = m_this.attr("method");
		var encType = m_this.attr("enctype");
		if (action && action!="")
		{
			if (encType!="multipart/form-data")
			{
				try
				{
					showModal();
					$.ajax
					(
						{
							"url": action,
							"type": method,
							"data": m_this.serialize(),
							"error": function() { onDoSubmit(false); },
							"success": function() { onDoSubmit(true); }
						}
					)
				}
				catch (e)
				{
					onDoSubmit(false);
				}
			}
			else
			{	
				showModal();
				m_this.after("<iframe id=\"kformtarget\" style=\"display:none\" name=\"kformtarget\" src=\"javascript:false;\"></iframe>");
				m_this.attr("target", "kformtarget");
				m_this.unbind("submit");
				m_this.submit();
				setTimeout(function()
				{
					m_this.bind("submit",doStuff);
					onDoSubmit(true);
				},500);
			}
		}
	}
	
	function onDoSubmit(success)
	{
		hideModal();
		if (success)
		{
			$(".kform-success").show();
			$(".kform-error").hide();
			m_this.hide();
		}
		else
		{
			$(".kform-success").hide();
			$(".kform-error").show();
			m_this.show();
		}
	}
	
	function checkRequired()
	{
		var retVal = true;
		m_this.find(".kform-required").each(function()
		{
			var val = getFieldValue($(this).get(0));
			if (val=="")
			{
				retVal = false;
				$(this).next(".kform-fieldMessage").show();
			}
		})
		return retVal;
	}
	
	function getFieldValue(el)
	{
		var n = el.name || el.id
		var t = el.type
		var tag = el.tagName.toLowerCase();

		if (!n)
			return "";
			
		if (t=='radio' || t=='checkbox')
		{
			if (el.form[n].length>1)
			{
				for (var i=0;i<el.form[n].length;i++)
				{
					if (el.form[n][i].checked)
						return el.form[n][i].value;
				}
			}
		}

		if (el.disabled || t == 'reset' || t == 'button' ||
			(t == 'checkbox' || t == 'radio') && !el.checked ||
			(t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
			tag == 'select' && el.selectedIndex == -1)
			return "";

		if (tag == 'select')
		{
			var index = el.selectedIndex;
			if (index < 0) return "";
			var a = [], ops = el.options;
			var one = (t == 'select-one');
			var max = (one ? index+1 : ops.length);
			for(var i=(one ? index : 0); i < max; i++)
			{
				var op = ops[i];
				if (op.selected) 
				{
					var v = op.value;
					if (!v)
					v = (op.attributes && op.attributes['value'] && !(op.attributes['value'].specified)) ? op.text : op.value;
					if (one) return v;
					a.push(v);
				}
			}
			return a;
		}

		return el.value;
	}
	
	function showModal()
	{
		var modalHtml = "<div class=kformModalDialog style=\"position:absolute;left:0;top:0;display:none;background-color:#000;z-index:100;\">&nbsp;</div>";					

		if ($(".kformModalDialog").length==0)
		{
			$(document.body).append(modalHtml);
			$(window).resize(sizeAndPosition);
			$(window).scroll(sizeAndPosition);
		}
			
		var dlg = $(".kformModalDialog");
		dlg.css("opacity",0.5);
		dlg.css("filter", "alpha(opacity='50')");
		sizeAndPosition();
		dlg.show();
		
		function sizeAndPosition()
		{
			var dlg = $(".kformModalDialog");
			dlg.width($(window).width());
			dlg.height($(window).height()+$(document).scrollTop());
		}
	}
	
	function hideModal()
	{
		$(".kformModalDialog").hide();
	}
	
	return this;
}

$(document).ready(function()
{
	$("form.kform").each(function(){$(this).KForm()});
})
