/* ********************************************************************************************************** */
/* Entity Dialog */
/* ********************************************************************************************************** */

function joVMMEntityDlgClass (data_collection_name, data_name, data_id)
{
	this.data_collection_name 	= data_collection_name;
	this.data_id 						= data_id;
	this.data_name						= data_name;
	this.id								= (data_collection_name == "device" ? "dialog_" + data_name : "dialog_" + this.data_collection_name);
	
	this.data_collection			= joVMM.xmlData.FindNode (this.data_collection_name);
	this.data							= null; 

	this.form_node						= null;

	this.OpenDialog					= _joVMMEntityDlg_OpenDialog;
	this.CloseDialog					= _joVMMEntityDlg_CloseDialog;

	// init now
	
	if (data_id != "")
	{
		switch (data_id)
		{
			case "task":
				this.data = this.data_collection.FindNode (data_name);
				break;
			default:
				this.data = this.data_collection.FindNodeByAttribute ("", "id", data_id)
				break;
		}
	}
	else
	{
		switch (data_collection_name)
		{
			case "device":
				this.data = this.data_collection;
				break;
			default:
				this.data = null;
				break;
		}
	}
	
	this.OpenDialog();
}


var curr_id;

/* ********************************************************************************************************** */

function _joVMMEntityDlg_OpenDialog()
{
	var dialog_height = -1;
	var dialog_width = 360;
	var form_name;
	var dialog_title;
	
	var dialog_node, title_node, content_node, footer_node, button_node;

	switch (this.data_id)
	{
		case "task":
			form_name 		= this.data_name;
			dialog_title	= joGUI.GetLocaleText ("task") + " '" + this.data_name + "'";
			break;
		default:
			form_name = this.id;
			dialog_title	= joGUI.GetLocaleText (this.data_collection_name + (this.data_id != "" || this.data_collection_name == "device" ? "_edit" : "_new")) + (this.data_id != "" ? ": " + this.data_id : "");
			break;
	}
	
	curr_id = this.id;

	dialog_node									= joAddHTMLChildNode (document.body, "DIV", "joMovableDiv joDialog", this.id);
		dialog_node.onmousedown 			= joInitMoveDiv;
		this.form_node	 						= joAddHTMLChildNode (dialog_node, "FORM");
			joDOM.SetAttribute 				(this.form_node, "name", form_name + "_form");
			this.form_node.action 			= "";
			joAddEvent 							(this.form_node, "submit", function (event) { void(0); });
	
			title_node							= joAddHTMLChildNode (this.form_node, "DIV", "joMovableActivator joDialogHeader");
				title_node.onmousedown 	= joInitMoveDivActivator;
					joVMMApp.AddTitle 		(title_node, this.id, dialog_title);

			content_node						= joAddHTMLChildNode (this.form_node, "DIV");
	
			footer_node							= joAddHTMLChildNode (this.form_node, "DIV", "joDialogFooter");
			
				button_node						= joAddHTMLChildNode (footer_node, "BUTTON", "joGfxButton");
					button_node.onclick		= function() { joVMMApp.GetGuiDialog (curr_id).CloseDialog (true); return false; };
					button_node.name			= "button_ok";
					button_node.innerHTML	= joGUI.GetLocaleText ("ok");

				button_node						= joAddHTMLChildNode (footer_node, "BUTTON", "joGfxButton");
					button_node.onclick		= function() { joVMMApp.GetGuiDialog (curr_id).CloseDialog (false); return false; };
					button_node.name			= "button_cancel";
					button_node.innerHTML	= joGUI.GetLocaleText ("cancel");
	
	// show now
	joGUI.BuildForm (this.form_node.name, content_node, joVMM);
	joGUI.FillForm (this.form_node, this.data);
	joGUI.OpenModalDialog (this.id, dialog_width, dialog_height, 50, this.form_node.name);
}

/* ********************************************************************************************************** */

function _joVMMEntityDlg_CloseDialog (save_data)
{
	var dialog_result = JO_VMM_EVENT_DATA_CHANGE_NONE;
	var result = true;
	
	if (save_data && this.data_collection_name != "device")
	{
		var id_field = joDOM.GetFormElement (this.form_node, "id");
		if (id_field)
		{
			var old_virtual_value 	= false;
			var new_virtual_value 	= false;
			var is_virtual_field 	= joDOM.GetFormElement (this.form_node, "is_virtual");
			if (is_virtual_field)
			{
				new_virtual_value = is_virtual_field.checked ? true : false;
			}
			
			if (this.data)
			{
				old_virtual_value = joIsBoolean (this.data.GetAttribute ("is_virtual", "false"));
			}
			
			result = joVMMApp.EntityDlgChangesAllowed (this.data_id, id_field.value, this.data_name, old_virtual_value, new_virtual_value);
		}
	}

	//......................................................................................ready to close dialog
	if (result)
	{	
		if (save_data)
		{
			joVMM.LastEditedEntity = joGUI.SaveForm (this.form_node, this.data_collection, this.data_name, this.data);
			dialog_result = (this.data) ? JO_VMM_EVENT_DATA_CHANGE_EDIT : JO_VMM_EVENT_DATA_CHANGE_NEW;
		}
		else if (copy_entity_list_obj)
		{
			copy_entity_list_obj.HandleRemoveDlg (joGUI_ALERT_BUTTON_OK, copy_entity_list_obj);
			copy_entity_list_obj = null;
		}
		
		joGUI.CloseModalDialog (this.id);
		joVMMApp.OnCloseDialog (this.id, this.data_name, dialog_result);
		
		var del_dlg 	= document.body.removeChild (joFIND (this.id));
		del_dlg 			= null;
	}
			
	return result;
}

/* ********************************************************************************************************** */
/* Save / Load Dialog */
/* ********************************************************************************************************** */

function joInitFileListTable()
{
	var new_table, new_body, new_head, new_tr, new_td;
	var container = joFIND ('dialog_loadsave_folder_container');
	joDOM.RemoveChildNodes (container);	
	
	new_table = document.createElement("TABLE");
	joDOM.SetAttribute (new_table, "id", "dialog_loadsave_folder");
	container.appendChild (new_table);

	new_head = document.createElement("THEAD");
	new_table.appendChild (new_head);
	
	new_tr = document.createElement("TR");
	new_head.appendChild (new_tr);
	
	new_td = document.createElement("TD");
	new_tr.appendChild (new_td);
	joDOM.AddText (new_td, joGUI.GetLocaleText ("file_name"));
	
	new_td = document.createElement("TD");
	new_tr.appendChild (new_td);
	joDOM.AddText (new_td, joGUI.GetLocaleText ("file_date"));
	
	new_td = document.createElement("TD");
	new_tr.appendChild (new_td);
	joDOM.AddText (new_td, joGUI.GetLocaleText ("file_size"));

	new_body = document.createElement("TBODY");
	new_table.appendChild (new_body);
	joDOM.SetAttribute (new_body, "id", "dialog_load_save_folder_list");
	joDOM.SetAttribute (new_body, "class", "scrollingContent");
}

/* ********************************************************************************************************** */

var dlg_for_load;
var dlg_load_save_open = false;

function joOpenLoadSaveDialog (for_load)
{
	if (!dlg_load_save_open)
	{
		if (!for_load) joVMM.SaveTaskFormData();
		
		dlg_for_load 			= for_load;
		dlg_load_save_open 	= true;
		
		joVMMApp.SetAutoSave ("0");
		
		joGetFormField (document.dialog_loadsave_form, "ok").disabled = true;
		joDOM.SetTextByID ("dialog_loadsave_header", joGUI.GetLocaleText (dlg_for_load ? "openfile" : "savefileas"));
				
		joGetFormField (document.dialog_loadsave_form, "filename").value = "";
		joInitFileListTable();
		joVMM.PostData ("file_list.php", "path=" + joVMM.UserDir, joFillFileList);
	}
}

/* ********************************************************************************************************** */

function joFillFileList (dirliststr, only_refresh)
{
	var dialog_height = 300;
	var dialog_width = 400;
	var table_obj = joFIND ('dialog_load_save_folder_list');
	if (table_obj)
	{
		joDOM.RemoveChildNodes (table_obj);
		var dirlist = dirliststr.split ("\n");
		var datestr, filename, dirfile;
		
		var count_files = dirlist.length;
		for (var i = 0; i < dirlist.length; i++)
		{
			dirfile = dirlist[i].split(";")
			if (dirfile[2] == "file")
			{
				filename = dirfile[0].split(".");
				if (filename.length == 2 && filename[1] == "xml")
				{
					joAddFileToList (table_obj, filename[0], dirfile[3], dirfile[1] + "B");
				}
			}
		}
	}
	
	initTableWidget ('dialog_loadsave_folder','100%',dialog_height - 80, Array ('S','S','N'));
	
	if (!only_refresh)
	{
		joGUI.OpenModalDialog ('dialog_loadsave', dialog_width, dialog_height, 50, "dialog_loadsave_form", "cancel");
		joFIND ("editbox_filename").style.display = (dlg_for_load ? "none" : "inline");
	}

	var button_container_node = joFIND ("file_button_delete_container");
	if (button_container_node)
	{
		joDOM.RemoveChildNodes (button_container_node);
		
		if (dlg_for_load)
		{
			var button_node				= joAddHTMLChildNode (button_container_node, "BUTTON", "joGfxButton", "load_button_delete");
			button_node.name				= "delete";
			button_node.title				= joGUI.GetLocaleText ("delete");
			button_node.onclick			= function() { joAskRemoveFile(); return false; };
			button_node.disabled		= true;
			
			joAddImageChildNode (button_node, 	"images/button_disabled.gif");
			
		}
	}
	
	var help_link_node 		= joFIND ("dialog_loadsave_help").lastChild;
	var help_link_anchor 	= dlg_for_load ? "form_project_load" : "form_project_save_as";
	help_link_node.href 	= "javascript:ShowHelp ('" + help_link_anchor + "')";
	
	help_link_node.firstChild.style.display = "inline";
	help_link_node.firstChild.style.visibility = "visible";
	
	if (ie) 
	{
		joFIND ("dialog_loadsave_header_table").style.display = "block";
	}
}

/* ********************************************************************************************************** */

function CheckFileName (editbox_filename)
{
	var value 		= joGetFormField (document.dialog_loadsave_form, "filename").value.replace (/\s*/g, "");
	var re 			= new RegExp (REGEX_FILENAME);
	var result	 	= (re.exec (value)) + "";
	result			= result.replace ("\\", "");
	
	if (value != result)
	{
		joGetFormField (document.dialog_loadsave_form, "filename").value = result;
	}
	
	result = (result == "" ? false : true);
	
	joGetFormField (document.dialog_loadsave_form, "ok").disabled = !result;
	if (dlg_for_load)
	{
		var button_node = joFIND ("load_button_delete");
		button_node.disabled = !result;
	}
	var image = joDOM.GetSubNode (button_node, "IMG");
	if (image)
	{
		image.src = "images/button_" + (result ? "delete" : "disabled") + ".gif";
	}
}

/* ********************************************************************************************************** */

function joAddFileToList (tbody_node, file_name, file_size, file_date)
{
	var tr_node, td_node;
	
	tr_node 							= joAddHTMLChildNode (tbody_node, "TR");
		tr_node.onclick 			= function() { joSelectFileFromList (this) };
		if (dlg_for_load)
		{
			tr_node.ondblclick 	= function() { joSelectFileFromList (this); joCloseLoadSaveDialog (true); };
		}
										
			td_node 					= joAddHTMLChildNode (tr_node, "TD");
				joDOM.AddText 		(td_node, file_name);
			td_node 					= joAddHTMLChildNode (tr_node, "TD");
				joDOM.AddText (td_node, file_size);
			td_node 					= joAddHTMLChildNode (tr_node, "TD");
				joDOM.AddText (td_node, file_date);
} 

/* ********************************************************************************************************** */

var prev_row_node = null;

function joSelectFileFromList (row_node)
{
	var editbox_filename 	= joGetFormField (document.dialog_loadsave_form, "filename");
	editbox_filename.value = row_node.getElementsByTagName ("TD")[0].firstChild.nodeValue;
	
	CheckFileName (editbox_filename);
	
	if (prev_row_node) prev_row_node.style.backgroundColor = "";
	row_node.style.backgroundColor 	= "#F5F2E1";
	prev_row_node = row_node;
}

/* ********************************************************************************************************** */

function joCloseLoadSaveDialog (confirm_dlg)
{
	var images = joFIND ('dialog_loadsave').getElementsByTagName ("IMG");
	for (i = 0; i < images.length; i++) joHideDiv (images[i]);

	//..................................................................................................load file
	if (dlg_for_load)
	{
		joGUI.CloseModalDialog ('dialog_loadsave');
		
		if (confirm_dlg)
		{
			joVMM.LoadData (joGetFormField (document.dialog_loadsave_form, "filename").value + ".xml");
		}
	}
	
	//..................................................................................................save file
	else 	if (confirm_dlg)
	{
		var filename 		= joGetFormField (document.dialog_loadsave_form, "filename").value;
		var tbody_node 	= joFIND ('dialog_load_save_folder_list');
		var line_count 	= tbody_node.childNodes.length;
		var file_exists 	= false;
		var existing_filename, td_node;
		
		for (var i = 0; i < line_count && !file_exists; i++)
		{
			td_node					= tbody_node.childNodes[i].childNodes[0];
			existing_filename		= td_node.innerHTML;
			if (existing_filename == filename)
			{
				file_exists = true;
			}
		}

		if (file_exists)
		{
			joGUI.Alert (joGUI.GetLocaleText ("overwritefile"), joGUI_ALERT_TYPE_NOTE, joGUI_ALERT_BUTTON_OK | joGUI_ALERT_BUTTON_CANCEL, joOverwriteFile, filename);
		}
		else
		{
			joVMM.project_name = filename;
			joVMMApp.SaveFile();
			joGUI.CloseModalDialog ('dialog_loadsave');
		}
		joVMMApp.ActivateMenu();
	}
	else
	{
		joGUI.CloseModalDialog ('dialog_loadsave');
	}

	dlg_load_save_open = false;
	joVMMApp.SetAutoSave (joFIND ("auto_save_cycle").value);
}

/* ********************************************************************************************************** */

function joOverwriteFile (result, filename)
{
	if (result == joGUI_ALERT_BUTTON_OK)
	{
		joVMM.project_name = filename;
		joVMMApp.SaveFile();
		joGUI.CloseModalDialog ('dialog_loadsave');
		joVMMApp.ActivateMenu();
	}
}

/* ********************************************************************************************************** */

function joOpenLoginDialog()
{
	var dialog_width = 200;
	var dialog_height = 130;

	joGUI.OpenModalDialog ('dialog_login', dialog_width, dialog_height, 200, "dialog_login_form", "button_ok");
}

/* ********************************************************************************************************** */

function joCloseLoginDialog()
{
	joVMM.SubmitLogin (joGetFormField (document.dialog_login_form, "username").value, joGetFormField (document.dialog_login_form, "password").value);
}

/* ********************************************************************************************************** */

function joOpenResultDialog (filename)
{
	var dialog_width = Math.floor (joGetWinInfo ("width") * .8);
	var dialog_height = Math.floor (joGetWinInfo ("height") * .8);

	var iframe = joFIND ("dialog_result_container");
	iframe.style.width = dialog_width - 30;
	iframe.style.height = dialog_height - 80;
	iframe.src = filename;

	var title = joFIND ("dialog_result_header");
	title.innerHTML = filename.substr (filename.lastIndexOf ("/") + 1);
	
	joGUI.OpenModalDialog ('dialog_result', dialog_width, dialog_height, 200, "dialog_result_form", "cancel");
}

/* ********************************************************************************************************** */

function joCloseResultDialog()
{
	joGUI.CloseModalDialog ('dialog_result');
}

/* ********************************************************************************************************** */

function joAskRemoveFile()
{
	var filename 		= joGetFormField (document.dialog_loadsave_form, "filename").value;
	
	if (filename == joVMM.project_name)
	{
		joGUI.Alert (joGUI.GetLocaleText ("error_delete_curr_project"), joGUI_ALERT_TYPE_ERROR, joGUI_ALERT_BUTTON_OK);
		
	}
	else
	{
		joGUI.Alert (joGUI.GetLocaleText ("deletefile"), joGUI_ALERT_TYPE_NOTE, joGUI_ALERT_BUTTON_OK | joGUI_ALERT_BUTTON_CANCEL, joRemoveFile, filename);
	}
	
}

/* ********************************************************************************************************** */

function joRemoveFile (result, filename)
{
	if (result == joGUI_ALERT_BUTTON_OK)
	{
		joVMM.RemoveFile (joGetFormField (document.dialog_loadsave_form, "filename").value);
	}
}

/* ********************************************************************************************************** */

function RefreshFileList (update_result)
{
	if (update_result == 1)
	{
		var editbox_filename 		= joGetFormField (document.dialog_loadsave_form, "filename");
		editbox_filename.value 	= "";
		prev_row_node 					= null;
		
		joInitFileListTable();
		joVMM.PostData ("file_list.php", "path=" + joVMM.UserDir, joFillFileList, true);
		CheckFileName (editbox_filename);
	}
	else
	{
		joGUI.Alert (joGUI.GetLocaleText ("joGUI_error"), joGUI_ALERT_TYPE_ERROR, joGUI_ALERT_BUTTON_OK);
	}
}
