//depends upon jquery.js & common.js

//dirty global variable to help dialog routing issues
var rewrite_buttons=false;

//create a popup in traditional alert() format - default size is 300x200
function message(title,content,width,height)
{
	if(!title)
		title="Application Message";
	if(!content)
		content="This is an application message or alert.";
	if(!width)
		width=300;
	if(!height)
		height=200;
	$("#application_dialog").dialog("destroy");
	$("#application_dialog").html("<div class=\"ui-dialog-content-container\">" + content + "</div>").css("display","inline");
	$("#application_dialog").dialog(
	{
		title: title,
		height: height,
		width: width,
		resizable: false,
		modal: true, 
		bgiframe: true, 
		overlay:
		{ 
			opacity: 0.5, 
			background: "black" 
		},
		buttons:
		{
			"Ok": function()
			{
				$(this).dialog("destroy");
			}
		}
	});
	window.scrollTo(0,0);
}
//create a popup in traditional alert() format - default size is 300x200
function form(title,content,width,height)
{
	if(!title)
		title="Application Form";
	if(!content)
		content="This is an application form, used to add or update system information.";
	if(!width)
		width=650;
	if(!height)
		height=550;
	$("#application_dialog").dialog("destroy");
	$("#application_dialog").html("<div class=\"ui-dialog-content-container\">" + content + "</div>").css("display","inline");
	format_form("#application_dialog .ui-dialog-content-container");
	$("#application_dialog").dialog(
	{
		title: title,
		height: height,
		width: width,
		resizable: false,
		modal: true,
		closeOnEscape: false,
		bgiframe: true, 
		open: function() {
  		$(document).unbind('keydown.dialog-overlay');
		},  
		overlay:
		{ 
			opacity: 0.5, 
			background: "black" 
		},
		buttons:
		{
			"Save": function()
			{
				var url=$("#application_dialog .ui-dialog-content-container form").attr("action");
				var data=$("#application_dialog .ui-dialog-content-container form").serialize();
				$("#application_dialog").html("<div class=\"application_loading\">" + $("#application_loading").html() + "</div>");
				//$("#dialog_save_button").hide();
				//$("#dialog_cancel_button").hide();
				$(":button").hide();
				$.ajax(
				{
					type: "POST",
					url: url,
					data: data,
					success: function(html)
					{
						$("#dialog_save_button").show();
						$("#dialog_cancel_button").show();
						dynamic_args[5]=html;
						form_links();
					}
				});
			},
			"Cancel": function()
			{
				$(this).dialog("destroy");
				if($("#application_dialog .return_url").length>0&&$("#application_dialog .return_url").html()!="")
				{
					if($("#application_dialog .return_location").html() == "#application_content")
						reload_page($("#application_dialog .return_url").html().replace("popupdate:1","divupdate:1"));
					else
						reload_page($("#application_dialog .return_location").html(),$("#application_dialog .return_url").html() + "/clear_return:1",dynamic_args[2],true,content_links);
				}
				else if($("#application_dialog .continue_url").length>0&&$("#application_dialog .continue_url").html()!="")
					dynamic_form("#application_dialog",$("#application_dialog .continue_url").html(),dynamic_args[2],dynamic_args[3],dynamic_args[4]);
				$("#application_overlay").hide();
			}
		}
	});
	$(".ui-dialog-titlebar-close").click(function()
	{
		$("#dialog_cancel_button").trigger("click");
		if(document.getElementById("iColorPicker"))
		{
			$("#iColorPicker").hide();
		}
	});

	window.scrollTo(0,0);
}
//dynamically load data from an external file, & pass it into the message function above
function dynamic_message(container,href,failureMessage,width,height)
{
	if(!failureMessage)
		failureMessage=arguments[2]="<p>There has been an error loading this document.</p><p>Please consult your System Administrator.</p>";
	message("Loading...","<div class=\"application_loading\">" + $("#application_loading").html() + "</div>",width,height);
	dynamic_args=arguments;
	dynamic_args[3]=false;


	dynamic_args[4]=popup_links;
	$.ajax(
	{
		type: "GET",
		url: href,
		success: function(content)
		{
			dynamic_args[5]=content;
			popup_links(dynamic_args);
		},
		error: function()
		{
			dynamic_args[5]=dynamic_args[2];
			popup_links(dynamic_args);
		}
	});
}
//dynamically load data from an external file, & pass it into the message function above
function dynamic_form(container,href,loadingError,width,height)
{
	if(!loadingError)
		loadingError=arguments[2]="<p>There has been an error loading this document.</p><p>Please consult your System Administrator.</p>";
	form("Loading...","<div class=\"application_loading\">" + $("#application_loading").html() + "</div>",width,height);
	dynamic_args=arguments;
	dynamic_args[1] = dynamic_args[1].replace("divupdate:1","popupdate:1") + "/?rand=" + rand();
	$(":button").hide();
	$.ajax(
	{
		type: "GET",
		url: dynamic_args[1],
		success: function(content)
		{
			$(":button").show();
			dynamic_args[5]=content;
			form_links(dynamic_args);
		},
		error: function()
		{
			$(":button").show();
			dynamic_args[5]=dynamic_args[2];
			form_links(dynamic_args);
		}
	});
}
function format_form()
{
	var isUpload=true;
	$(":button:contains('Save')").attr("id","dialog_save_button");
	$(":button:contains('Cancel')").attr("id","dialog_cancel_button");
	$(".cancel_button_override").attr("id","cancel_button");
	
	$(":button:contains('Ok')").attr("id","dialog_ok_button");
	if(!dynamic_args)
	{
		var dynamic_args=new Array();
		dynamic_args[0]="#application_dialog";
	}
	if(dynamic_args[0]!="#application_content")
	{
		$(":button").show();
		var redo_buttons=false;
		if(rewrite_buttons)
		{
			if(!document.getElementById('dialog_save_button'))
			{
				$(".ui-dialog-buttonpane").html('<button id="dialog_save_button">Save</button>');
				redo_buttons=true;
			}
			if(!document.getElementById('dialog_cancel_button'))
			{
				$(".ui-dialog-buttonpane").html($(".ui-dialog-buttonpane").html() + '<button id="dialog_cancel_button">Cancel</button>');
				redo_buttons=true;
			}
		}
		if(redo_buttons)
		{
			$("#dialog_save_button").click(function()
			{
				var url=$("#application_dialog .ui-dialog-content-container form").attr("action");
				var data=$("#application_dialog .ui-dialog-content-container form").serialize();
				$("#application_dialog").html("<div class=\"application_loading\">" + $("#application_loading").html() + "</div>");
				$(":button").hide();
				$.ajax(
				{
					type: "POST",
					url: url,
					data: data,
					success: function(html)
					{
						$("#dialog_save_button").show();
						$("#dialog_cancel_button").show();
						dynamic_args[5]=html;
						form_links(html);
					}
				});
			});
			$("#dialog_cancel_button").click(function()
			{
				$("#application_dialog").dialog("destroy");
				if($("#application_dialog .return_url").length>0&&$("#application_dialog .return_url").html()!="")
				{
					if($("#application_dialog .return_location").html() == "#application_content")
						reload_page($("#application_dialog .return_url").html().replace("popupdate:1","divupdate:1"));
					else
						reload_page($("#application_dialog .return_location").html(),$("#application_dialog .return_url").html() + "/clear_return:1",dynamic_args[2],true,content_links);
				}
				else if($("#application_dialog .continue_url").length>0&&$("#application_dialog .continue_url").html()!="")
					dynamic_form("#application_dialog",$("#application_dialog .continue_url").html(),dynamic_args[2],dynamic_args[3],dynamic_args[4]);
				$("#application_overlay").hide();
			});
		}
		if($(dynamic_args[0] + " form").attr("action"))
		{
			$(dynamic_args[0] + " form").attr("action",$(dynamic_args[0] + " form").attr("action").replace("divupdate:1","popupdate:1"));
			$(dynamic_args[0]).find("form").map(function()
			{
				if($(this).hasClass("pager"))
					return;
				if($(this).attr("enctype"))
				{
					isUpload=true;
					if($(this).attr("enctype").search(/multipart/i)>=0)//so that ie works properly!!!
						isUpload=true;
					else
						isUpload=false;
				}
				else
					isUpload=false;
				if(isUpload)
					return;
				$(this).attr("onsubmit","return false");
				$(this).find("input").map(function()
				{
					if($(this).attr("type")=="submit")
						$(this).remove();
				});
			});
			$("#dialog_save_button").text($("#application_dialog .save_button").html());
			$("#dialog_save_button").show();
			$("#dialog_cancel_button").text($("#application_dialog .cancel_button").html());
			$("#dialog_ok_button").text($("#application_dialog .cancel_button").html());
		}
		else
		{
			$("#dialog_save_button").hide();
			$("#dialog_cancel_button").text($("#application_dialog .ok_button").html());
			if($("#application_dialog .continue_url").length>0)
			{
				if($("#application_dialog .continue_url").attr("title")=="redirect")
				{
					$(":button").hide();
					setTimeout('load_content("#application_dialog",$("#application_dialog .continue_url").html(),dynamic_args[2],true,form_links)',1500);
				}
			}
		}
	}
}
