/*
*    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 all methods used to dispatch messages.
* @author Carlos Eduardo Goncalves
*/

/** 
* <p>AJAX connection constructor.
*/
function Connection() {
}

/** 
* <p>Send a message after to add it to the message queue.</p>
* @param msg
*		<code>Message</code> value object.
* @return 
*		<code>Message</code> id generated by the engine.
* @throws Exception that informs if the parameter is not a <code>Message</code> object.
*/
Connection.sendMessage = function(msg) {
  try {
	if(!Engine.assertType(msg, Message))
	  throw "Message required on paremeter 1";
	if(msg.progressBar != null) {  
	  if(!Engine.assertType(msg.progressBar, EmbeddedProgressBar))
	    msg.progressBar = null;
	  else
	    msg.silent = true;
	}
    var wrapper = MessageQueue.add(msg);
    msg.id = wrapper.id;	
	if(msg.method.toUpperCase() == "GET")
	  wrapper.request.setRequestHeader("If-Modified-Since", "Tue, 1 Jan 1980 00:00:00 GMT");
    wrapper.request.send(msg.value);  
	if(msg.progressBar != null)
  	  msg.progressBar.update(true);	
	Console.trace(msg, Console.REQUEST);		
	return msg.id; 	
  } 
  catch(e) { 
    Engine.reportException(msg.id, e);
  }
}

/** 
* <p>Copy a form to a message, and sent the message after to add it to the message queue.</p>
* @param msg
*		<code>Message</code> value object.
* @param form
*		<code>HTMLForm</code> to be sent with the message.
* @param encrypt
*		<code>Boolean</code> flag that defines if form data must be encrypted.
* @return 
*		<code>Message</code> id generated by the engine.
* @throws Exception that informs if the parameter is not a <code>Message</code> object.
*/
Connection.sendFormByMessage = function(msg, form, encrypt) {
  try {	
	if(!Engine.assertType(msg, Message))
	   throw "Message required on paremeter 1";	   
    msg.method = "POST";  
    msg.value = ParserTool.formToUrl(form);	
	if(msg.progressBar != null) {  
	  if(!Engine.assertType(msg.progressBar, EmbeddedProgressBar))
	    msg.progressBar = null;
	  else
	    msg.silent = true;
	}	
    if(encrypt) {
      var encryptor = new HashTool();
	  msg.value = encryptor.encodeQuery(msg.value);
    }	
    var wrapper = MessageQueue.add(msg);
    msg.id = wrapper.id;	
    wrapper.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
    wrapper.request.send(msg.value); 
	if(msg.progressBar != null)
  	  msg.progressBar.update(true);
	Console.trace(msg, Console.REQUEST);		
	return msg.id; 	
  } 
  catch(e) { 
    Engine.reportException(msg.id, e);
  }
}

/** 
* <p>Use a message to call a web service method, after to add the message to the message queue.</p>
* @param msg
*		<code>Message</code> value object.
* @param rpc
*		<code>RemoteMethod</code> instance that will be called.
* @return 
*		<code>Message</code> id generated by the engine.
* @throws Exception that informs if the parameters are not a <code>Message</code> object and a 
* <code>RemoteMethod</code> object respectively.
*/
Connection.callWebService = function(msg, rpc) {
  try {
	Engine.doAsPrivileged();  
	if(!Engine.assertType(msg, Message))
	  throw "Message required on paremeter 1";
	if(!Engine.assertType(rpc, RemoteMethod))
	  throw "RemoteMethod required on paremeter 2";	
	msg.method = "POST";
	msg.value = rpc;
	if(msg.progressBar != null) {  
	  if(!Engine.assertType(msg.progressBar, EmbeddedProgressBar))
	    msg.progressBar = null;
	  else
	    msg.silent = true;
	}	
    var wrapper = MessageQueue.add(msg);
    msg.id = wrapper.id;	
    wrapper.request.send(wrapper.parsedMethod);  
	if(msg.progressBar != null)
  	  msg.progressBar.update(true);	
	Console.trace(msg, Console.REQUEST);		
	return msg.id; 	
  } 
  catch(e) { 
    Engine.reportException(msg.id, e);
  }
}

/** 
* <p>Abort a message and remove it from the message queue.</p>
* @param msg_id
*		<code>Message</code> unique identifier on message queue.
*/
Connection.abortMessage = function(msg_id) {
  try {
	var wrapper = MessageQueue.getMessage(msg_id);
    if(wrapper != null) {
      wrapper.request.abort();
      MessageQueue.remove(msg_id);
    }
  } 
  catch(e) { 
    Engine.reportException(msg_id, e);
  }
}