// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//
// Functions for requesting user confirmation if form data has not been saved yet
//

var FormWatch = Class.create();
FormWatch.prototype = {
	initialize : function(form, options)
	{
	  this.form = $(form);
		if (this.form == null)
			return;
	  this.submitted = false;
		this.formcontents = Form.serialize(this.form);
    this.submitFunction = function() {this.submitted = true;}.bind(this);
    this.beforeunloadFunction = this.confirmExit.bind(this);
		
	  Event.observe(this.form, 'submit', this.submitFunction);
	  Event.observe(window, 'beforeunload', this.beforeunloadFunction);
	},
	confirmExit : function(ev)
	{
		if (this.form == null)
			return;
	  this.newcontents = Form.serialize(this.form);
	  if (this.contentsChanged())
	     ev.returnValue = window.text_ml_11595;
	},
	confirmSetID : function()
	{
		if (this.contentsChanged())
			return (confirm(window.text_ml_11595));
		else
			return(true);
	},
	contentsChanged : function()
	{
		if (this.form == null)
			return;
		this.newcontents = Form.serialize(this.form);
		return ((this.formcontents != this.newcontents) && ! (this.submitted)) 
	},
	updateContents : function()
	{
		if (this.form == null)
			return;
	  this.submitted = false;
		this.formcontents = Form.serialize(this.form);
	},
	stopObserving : function()
	{
		if (this.form == null)
			return;
	  Event.stopObserving(this.form, 'submit', this.submitFunction);
	  Event.stopObserving(window, 'beforeunload', this.beforeunloadFunction);
	}
}

function set_planning(planning_id)
{
	if (window.entriesFormWatcher.confirmSetID())
	{
		window.entriesFormWatcher.stopObserving();
		new Ajax.Updater('content_newentry_frame', '/list/set_planning/' + planning_id,
			{ asynchronous:true,
				evalScripts:true,
			  onComplete: function(transport)
				{
					window.entriesFormWatcher = new FormWatch('entries_form');
				}
			} );
	}
}


//
// Functions for table selection and inserting of new status 
//

function clickableTable(enable, tableId, callbackFunction)
{
  $$('#'+tableId+' tr').each(function(item)
    {
      if (enable) 
      {
        item.listener_mouseover = function() {item.addClassName('highlight');} 
        item.listener_mouseout = function() {item.removeClassName('highlight');} 
        item.listener_click = function() {callbackFunction(item);} 
        item.observe('mouseover', item.listener_mouseover);
        item.observe('mouseout', item.listener_mouseout);
        item.observe('click', item.listener_click);
      }
      else
      {
        item.removeClassName('highlight');
        item.stopObserving('mouseover', item.listener_mouseover);
        item.stopObserving('mouseout', item.listener_mouseout);
        item.stopObserving('click', item.listener_click);
      }
  } );
}

function status_click(selectionElement)
{
  this.selectionElement = selectionElement;
  this.invoke = function (rowElement) 
  {
    $('new_status_button').show();
    $('status_selection').hide(); 
    clickableTable(false, 'status_list_table', null);

    var params = new Hash();
    params.set('afterId', rowElement.id);
    params.set('newStatus', selectionElement.value);
    new Ajax.Updater({ success: rowElement.id, failure: 'notice_insert_after' }, '/servicedoc/insert_status', {asynchronous:true, evalScripts:true, parameters:params, insertion:'after'});
  }
}

