/**
 *	Class to submit a form according to a specific action.
 *
 *	@author		Unknown
 *	@version	1.0
 *	@copyright	Katana Productions Pty Ltd 2002
 *
 *	@param	form    The form object to be submitted.
 *	@param	idName  [optional] The name of the field to be used as the ID field.
*/
function Action(form, idName)
{
//alert(this.form.outerHTML);
  this.form = form;
  this.idName = idName;
}

// Action constants
Action.ACTION_SEARCH = 'SearchForObj'
Action.ACTION_DELETE = 'DeleteObj'
Action.ACTION_ORDER = 'OrderColl'
Action.ACTION_VIEWNEW = 'ViewNewObj'
Action.ACTION_VIEWEXISTING = 'ViewExistingObj'
Action.ACTION_ADD = 'AddObj'
Action.ACTION_UPDATE = 'UpdateObj'
Action.ACTION_INCREMENT = 'Increment'
Action.ACTION_DECREMENT = 'Decrement'
Action.ACTION_CHOOSE_PLANS = 'ChoosePlans'
Action.ACTION_BUY_PLAN = 'BuyPlan'
Action.ACTION_TERMS_AND_CONDITIONS = 'TermsAndConditions'
Action.ACTION_SHOW_SHOPPING_CART = 'ShowShoppingCart'
Action.ACTION_SETUP_ACCOUNT = 'SetupAccount'
Action.ACTION_CANCEL = 'Cancel'
Action.ACTION_MOVE_UP = 'MoveUp'
Action.ACTION_MOVE_DOWN = 'MoveDown'
Action.ACTION_ORDERTYPE_ASC = 'ASC'
Action.ACTION_ORDERTYPE_DESC = 'DESC'
Action.ACTION_GETXML = 'GetXml'
Action.ACTION_GETXML_ITEM = 'GetXmlItem'
Action.ACTION_PRINT = 'Print'
Action.ACTION_VIEWCONTEXT = 'ViewContextObj'
Action.ACTION_SEND_VIEW = 'SendView'
Action.ACTION_VIEW_PRODUCT = 'viewProduct'

/**
 *	Causes a general action submit of the form.
 *
 *	@param action A string representing the action to be taken on the server.
 *	@param id [optional] A string representing the id of the object causing the action.
 *	@param returnToPage [optional] A boolean which will result in the return to the current page once the action is completed
*/
Action.prototype.doAction = function(action, id, returnToPage)
{
//alert('test1');
//alert(this.form.outerHTML);
  if (this.form)
  {
    this._setHiddenField('Action', action);
	  if (id != null) this._setHiddenField(this.idName, id);
    if (returnToPage == true)
    {
      this._setHiddenField('ReturnToPage', document.URL);
    }
	  this.form.submit();
    return false;
  }
  else
  {
    alert('The form object does not exist!\n\nPlease ensure that the form HTML id valid. Have a nice day!');
  }
}

/**
 *	Causes a general action submit of the form which also does a direct to a url.
 *
 *	@param url           A string representing the url to direct to.
 *	@param action        A string representing the action to be taken on the server.
 *	@param id [optional] A string representing the id of the object causing the action.
*/
Action.prototype.doRedirectAction = function(url, action, id)
{
  if (this.form)
  {
    this.form.action = url;
    this.doAction(action, id);
  }
  else
  {
    alert('The form object does not exist!\n\nPlease ensure that the form HTML id valid. Have a nice day!');
  }
}

/**
 *	Causes a delete action submit of the form.
 *
 *	@param id A string representing the id of the object causing the action.
*/
Action.prototype.doDelete = function(id,returnToPage)
{
  if (confirm('Are you sure you want to delete this record?'))
  {
    this.doAction(Action.ACTION_DELETE, id,returnToPage);
  }
}

/**
 *	Causes a paging action submit of the form.
 *
 *	@param page An integer representing the new page number.
*/
Action.prototype.doPage = function(page)
{
  if (this.form)
  {
    this._insertSearchFields();
    if (this.form.Page) this.form.Page.value = page;
    this.doAction(Action.ACTION_SEARCH);
  }
}

/**
 *	Causes an order by action submit of the form.
 *
 *	@param orderBy A string representing the field to order by.
 *	@param orderType A string representing the field order type (i.e. 'ASC' or 'DESC').
*/
Action.prototype.doOrderBy = function(orderBy, orderType)
{
  if (this.form)
  {
    if (this.form.OrderBy) this.form.OrderBy.value = orderBy;
    if (this.form.OrderType) this.form.OrderType.value = orderType;
    this.doAction(Action.ACTION_SEARCH);
  }
}

/**
 *	Sets the value of a hidden field into the form.
 *
 *	@param name A string representing the name of the field to insert.
 *	@param value A string representing the value of the field to insert.
*/
Action.prototype._setHiddenField = function(name, value)
{
  var hiddenField = eval('this.form.' + name);
  if (hiddenField)
  {
    hiddenField.value = value;
  }
  else
  {
    this._insertHiddenField(name, value);
  }
} 

/**
 *	Inserts a hidden field into the form.
 *
 *	@param name A string representing the name of the field to insert.
 *	@param value A string representing the value of the field to insert.
*/
Action.prototype._insertHiddenField = function(name, value)
{
  newField = document.createElement("input");
  newField.type = "hidden";
  newField.name = name;
  newField.value = value;
  this.form.appendChild(newField);
}

/**
 *	Inserts the search hidden field into the form.
*/
Action.prototype._insertSearchFields = function()
{
  var searchForm = document.forms.search_form;
  if (searchForm)
  {
    var elms = searchForm.elements;
    for (var i = 0; i < elms.length; i++)
    {
	  if(elms[i].name)
	  {
	    this._setHiddenField(elms[i].name, elms[i].value);
	  }
    }
  }
}