/*
*    Clean AJAX Engine v4.1
*    Copyright (C) 2005-2006 Carlos Eduardo Goncalves (cadu.goncalves@gmail.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
* Provides a facade to all engine features.
* @author Carlos Eduardo Goncalves
*/

/**
* <p>Clean facade constructor.</p>
*/
function Clean() {
}

/**
* <p>Create a <code>Message</code> setting required properties.</p>
* @param url
*		Url to access.
* @param consumer
*		Consumer id.
* @param on_error
*		Function that should be called at the onError event.
* @return
*  		New <code>Message</code> instance.
*/
Clean.createSimpleMessage = function(url, consumer, on_error) {
  var msg = new Message();
  msg.address = url;
  msg.consumer = consumer;
  msg.onError = on_error;
  return msg;  
} 

/**
* <p>Create a <code>Message</code> setting main properties.</p>
* @param url
*		Url to access.
* @param xslt
* 		Url to a XSLT file used to parse the response.
* @param consumer
*		Consumer id.
* @param _refresh
*		Indicates if the consumer must be refreshed.
* @param on_error
*		Function that should be called at the onError event.
* @return
*  		New <code>Message</code> instance.
*/
Clean.createMessage = function(url, xslt, consumer, _refresh, on_error) {
  var msg = new Message();
  msg.address = url;
  msg.xslt = xslt;  
  msg.refresh = _refresh;    
  msg.consumer = consumer;
  msg.onError = on_error;  
  return msg;  
} 

/**
* <p>Create a <code>Message</code> setting almost all properties.</p>
* @param url
*		Url to access.
* @param xslt
*  		Url to a XSLT file used to parse the response.
* @param consumer
*		Consumer id.
* @param _refresh
*		Indicates if the consumer must be refreshed.
* @param on_error
*		Function that should be called at the onError event.
* @param on_change
*		Function that should be called at the onChange event.
* @param on_complete
*		Function that should be called at the onComplete event.
* @return
*  		New <code>Message</code> instance.
*/
Clean.createFullMessage = function(url, xslt, consumer, _refresh, on_error, on_change, on_complete) {
  var msg = new Message();
  msg.address = url;
  msg.xslt = xslt;  
  msg.consumer = consumer;
  msg.refresh = _refresh;  
  msg.onError = on_error; 
  msg.onChange = on_change;   
  msg.onComplete = on_complete;   
  return msg;
} 

/**
* <p>Transport message using HTTP GET method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.doGet = function(msg) {
  msg.method = "GET";
  return Connection.sendMessage(msg);  
} 

/**
* <p>Transport message using HTTP POST method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @param value
*		Value to post.
* @param encrypt
*		<code>Boolean</code> flag that defines if the value must be encrypted.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.doPost = function(msg, value, encrypt) {
  msg.method = "POST";
  if(encrypt) {
    var encryptor = new HashTool();
	msg.value = encryptor.encodeString(value);
  }
  else
    msg.value = value;
  return Connection.sendMessage(msg);  
} 

/**
* <p>Locates a form using its id and transport it inside a 
* message using HTTP POST method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @param form_id
*		<code>HTMLForm</code> id attribute.
* @param encrypt
*		<code>Boolean</code> flag that defines if form data must be encrypted.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.postFormById = function(msg, form_id, encrypt) {
  msg.method = "POST";	
  var iterator = new DomIterator(document);
  var form = iterator.getFormById(form_id);
  if(form != null)
    return Connection.sendFormByMessage(msg, form, encrypt);
} 

/**
* <p>Locates a form using its name and transport it inside a 
* message using HTTP POST method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @param form_name
*		<code>HTMLForm</code> name attribute.
* @param encrypt
*		<code>Boolean</code> flag that defines if form data must be encrypted.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.postFormByName = function(msg, form_name, encrypt) {
  msg.method = "POST";		
  var iterator = new DomIterator(document);  
  var form = iterator.getFormByName(form_name);
  if(form != null)	
    return Connection.sendFormByMessage(msg, form, encrypt);
} 

/**
* <p>Transport a <code>RemoteMethod</code> instance inside a message 
* using HTTP POST method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @param rpc
*		<code>RemoteMethod</code> instance used to identify the web 
*		service to call, including its name and parameters.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.callWebService = function(msg, rpc) {
  msg.method = "POST";		
  return Connection.callWebService(msg, rpc);
}