/**
 * This file is a part of Javascript ToolKit
 * 
 * Javascript ToolKit is a cross-browser javascript/DOM framework 
 * used to build rich web-based client interfaces with basic controls 
 * (dialog, button, imageButton, ...), complex controls 
 * (htmlEditor, colorChooser, grid, ...) and RPC components.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Copyright (C) 2005 Frédéric LECOINTRE <frederic.lecointre@burnweb.net>
 *
 * $Id: jtkXMLHttpRequest.js,v 1.1 2005/04/07 08:04:23 gabriel_ Exp $ 
 */
 
/**
 * Modified from original file by Frédéric LECOINTRE  2007/08
 */

/** 
 * @class jtkXMLHttpRequest
 * @package rpc
 * @version 1.0.0 $Date: 2005/04/07 08:04:23 $ $Revision: 1.1 $
 * @author: Frédéric LECOINTRE<frederic.lecointre@burnweb.net>
 * 
 */
jtkXMLHttpRequest = function (){
	
	this._requestObject = null;
	this._listeners = new Array();
	this._asynchronous = false;
	this._state = this.UNINITIALIZED;
	this.lastError = null;
	this.initialize();
	
}//end function

jtkXMLHttpRequest.prototype._requestObject = null;
jtkXMLHttpRequest.prototype._listener = null;
jtkXMLHttpRequest.prototype._asynchronous = false;
jtkXMLHttpRequest.prototype._state = 0;
jtkXMLHttpRequest.prototype.lastError = null;

var __jtkXMLHttpRequestEnumState = 0x00;

jtkXMLHttpRequest.prototype.UNINITIALIZED = __jtkXMLHttpRequestEnumState++;
jtkXMLHttpRequest.prototype.LOADING = __jtkXMLHttpRequestEnumState++;
jtkXMLHttpRequest.prototype.LOADED = __jtkXMLHttpRequestEnumState++;
jtkXMLHttpRequest.prototype.INTERACTIVE = __jtkXMLHttpRequestEnumState++;
jtkXMLHttpRequest.prototype.COMPLETED = __jtkXMLHttpRequestEnumState++;
jtkXMLHttpRequest.prototype.ERROR = __jtkXMLHttpRequestEnumState++;

var __jtkXMLHttpRequestEnumMethod = __jtkXMLHttpRequestEnumState++;

jtkXMLHttpRequest.prototype.GET = __jtkXMLHttpRequestEnumMethod++;
jtkXMLHttpRequest.prototype.POST = __jtkXMLHttpRequestEnumMethod++;
jtkXMLHttpRequest.prototype.HEAD = __jtkXMLHttpRequestEnumMethod++;

/**
 * 
 * @param boolean asynchronous
 * @return void
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.setAsynchronous = function(asynchronous){
	this._asynchronous = (asynchronous || (asynchronous == null))? true : false;
}//end function

/**
 * 
 * @return boolean
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.isAsynchronous = function(bool){
	return this._asynchronous;
}//end function

/**
 * 
 * @param string url
 * @param integer method
 * @param string data
 * @param string user
 * @param string password
 * @return boolean
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.open = function(url, method, user, password){

	if(url == null){
		return false;
	}//end if
	
	switch(method){
		case this.POST:
			method = 'POST';
		break;
		case this.GET:
			method = 'GET';
		break;
		case this.HEAD:
			method = 'HEAD';
		break;
		default:
			method = 'GET';
	}//end switch
	
	try{
		this._requestObject.open(method, url, this._asynchronous, user, password);
		return true;
	}//end try
	catch(e){  
		this.lastError = 'Unable to open ' + url + ' Exception:' + e.message;
		this.onreadystatechange(this.ERROR);
		return false;
	}//end catch
	
}//end function

/**
 * 
 * @param string data
 * @return boolean
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.send = function(data){

	if(this._requestObject.readyState == this.LOADING){
		try{
			this._requestObject.send(data);
			
			//gecko does'nt fire event with synchronous call
			if(!this._asynchronous && !window.browser.isIe()){
				this.onreadystatechange(this.COMPLETE);
			}//end if
			return true;
		}//end try
		catch(e){  
			this.lastError = 'Unable to send request Exception:' + e.message;
			this.onreadystatechange(this.ERROR);
			return false;
		}//end catch
	}//end if
	else{  
		this.lastError = 'Unable to send request Invalid state: ' + this.getStateText(this._requestObject.readyState);
		this.onreadystatechange(this.ERROR);
		return false;
	}//end catch
		
}//end function

/**
 * 
 * @return void
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.initialize = function(){

	this._requestObject = null;
	this._listener = null;
	this._asynchronous = false;
	this._state = this.UNINITIALIZED;
	this.lastError = null;
	
	switch(window.browser.type){
		case 'msie':
			this._requestObject = new ActiveXObject("Microsoft.XMLHTTP");
		break;
		case 'firefox':
		case 'mozilla':
		case 'safari':
		case 'opera':
			this._requestObject = new XMLHttpRequest();
		break;
		default:
			this.lastError = 'Unsupported browser for XMLHttpRequest';
			this._state = this.ERROR;
			throw Error(this.lastError);
			return;
	}//end switch
	
	var obj = this;
	(
	this._requestObject.onreadystatechange = function () {obj.onreadystatechange();}
	)();
											
}//end function

/**
 * 
 * @param string header
 * @param string value
 * @return void
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.setHeader = function(header, value){
	this._requestObject.setRequestHeader(header, value);
}//end function

/**
 * 
 * @param string header
 * @return string
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.getResponseHeader = function(header){
	return this._requestObject.getResponseHeaderr(header);
}//end function

/**
 * 
 * @return string
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.getAllResponseHeaders = function(){
	return this._requestObject.getAllResponseHeaders();
}//end function

/**
 * 
 * @return void
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.abort = function(){
	this._requestObject.abort();
}//end function

/**
 * 
 * @return integer
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.getState = function(){
	return this._state;
}//end function

/**
 * @param integer state
 * @return string
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.getStateText = function(state){
	
	if(state == undefined)
		state = this._requestObject.readyState;
		
	switch(state){
		case this.UNINITIALIZED : return 'UNINITIALIZED';
		case this.LOADING : return 'LOADING';
		case this.LOADED : return 'LOADED';
		case this.INTERACTIVE : return 'INTERACTIVE';
		case this.COMPLETED : return 'COMPLETED';
		case this.ERROR : return 'ERROR';
		default : return 'UNKNOWN STATE';
	}//end switch
	
}//end function

/**
 * 
 * @return integer
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.getStatus = function(){
	return this._requestObject.status;
}//end function

/**
 * 
 * @return string
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.getStatusText = function(){
	return this._requestObject.statusText;
}//end function

/**
 * 
 * @return string
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.getResponseText = function(){
	return this._requestObject.responseText;
}//end function

/**
 * 
 * @return DOMObject
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.getResponseXML = function(){
	if(window.browser.isIe()){
		return this._requestObject.responseXML;
	}//end if
	else{
		//gecko throw error whil using responseXML as DOMdocument ???
		//return this._requestObject.responseXML.documentElement
		return new DOMParser().parseFromString(this._requestObject.responseText, 'text/xml');
	}//end else
}//end function

/**
 * 
 * @return void
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.onreadystatechange = function (state) {
	this._state = (state != undefined) ? state : this._requestObject.readyState;
	this.callListener();
}//end function

/**
 * 
 * @param string handler
 * @param jtkObject obj
 * @return void
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.addListener = function(handler, obj){
	this._listener = (obj)? new Array(obj, handler) : new Array(null, handler);
}//end function

/**
 * 
 * @param string id
 * @return void
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.removeListener = function(){
	this._listener = null;
}//end function

/**
 * 
 * @return void
 * @since 1.0.0
 */
jtkXMLHttpRequest.prototype.callListener = function(){
	if(this._listener != null){
		callFunction(this._listener[1], this._listener[0], Array(this));
	}//end if
}//end function
