$(document).ready(function()
{
	$("div[class=\"rtepad_site_layout_navigation_tab\"]").click(function()
	{
		window.location.replace($(this).children().attr("href"));
	});
	$("div[class=\"rtepad_site_layout_navigation_tab_on\"]").click(function()
	{
		window.location.replace($(this).children().attr("href"));
	});
	$("div[class=\"rtepad_content_input_button_left\"]").click(function()
	{
		$(this).parent().children("div[class=\"rtepad_content_input_button_middle\"]").children("input").click();
	});
	$("div[class=\"rtepad_content_input_button_right\"]").click(function()
	{
		$(this).parent().children("div[class=\"rtepad_content_input_button_middle\"]").children("input").click();
	});
});
function submit_form(elm)
{
	$("input[type=checkbox]").each(function()
	{
		if ($(this).is(":checked"))
		{
			$(this).attr("value", "on");
		}
		else
		{
			$(this).attr("value", "off");
		}
	});
	json = "json = {";
	$(elm).find("input").each(function()
	{
		if ($(this).attr("id"))
		{
			json += $(this).attr("id") + ": \"" + escape($(this).val()) + "\", ";
		}
	});
	$(elm).find("select").each(function()
	{
		if ($(this).attr("id"))
		{
			json += $(this).attr("id") + ": \"" + escape($(this).val()) + "\", ";
		}
	});
	$(elm).find("textarea").each(function()
	{
		if ($(this).attr("id"))
		{
			value = $(this).val().replace(/\r/g, "\\r");
			value = $(this).val().replace(/\n/g, "\\n");
			json += $(this).attr("id") + ": \"" + escape(value) + "\", ";
		}
	});
	json = json.slice(0, -2);
	json += "}";
	eval(json);
	$.ajax({type: "post", url: $(elm).attr("action"), data: json, dataType: "json", success: function(out)
	{
		$("div#form").hide();
		var errors = 0;
		for (var i in out)
		{
			if (out[i] != "" & i != "invoice")
			{
				errors++;
			}
			$("span#" + i + "_error").text(out[i]);
		}
		if (errors > 0)
		{
			$("div#form").fadeIn("slow");
		}
		else
		{
			$("div#form").hide();
			if ($("div#success").length > 0)
			{
				$("div#success").fadeIn("slow");
			}
			else
			{
				window.location.replace($("input#redirect").val());
			}
		}
	}, error: function(request, error)
	{
		alert(request.responseText);
		alert(error);
	}
	});
	return false;
}
function file_upload(path)
{
	$("span#file_message").ajaxStart(function()
	{
		$(this).show();
		$(this).text("Please wait, uploading...");
		$("span#file_message").hide();
	});
	$.ajaxFileUpload(
	{
		url: path,
		secureuri: false,
		fileElementId: "file",
		dataType: "json",
		success: function (data, status)
		{
			if (data.error != "")
			{
				if(data.error != "")
				{
					$("span#file_message").hide();
					$("span#file_error").show();
					$("span#file_error").text(data.error);
				}
			}
			else
			{
				$("span#file_message").show();
				$("span#file_error").hide();
				$("div#file_upload").hide();
				$("div#file_remove").show();
				$("input#file_remove_button").attr("value", "Remove: " + data.name);
				$("input#md5").attr("value", data.msg);
			}
		}
	});
	return false;
}
function file_remove()
{
	$("div#file_upload").show();
	$("div#file_remove").hide();
	$("span#file_message").hide();
	md5 = $("input#md5").val();
	$("input#md5").attr("value", "");
	$("input#file").attr("value", "");
	$.ajax({type: "get", url: "/support/bugs/uploads/remove/" + md5 + "/"});
}