/**
 * ajax class v1.0
 * (c) 2008-01-15 Studio Programistyczne codeX Dominik Raś dominik.ras@codex.net.pl

 attribute readyState values:
 0 - Uninitialised
 1 - Loading
 2 - Loaded
 3 - Interactive
 4 - Completed

 for text results use:
 this.xmlHttpReq.responseText variable or
 this.GetTextResults() method

 for XML results use:
 this.xmlHttpReq.responseXML variable or
 this.GetXMLResults() method

 read ajax error number from attribute status
 read ajax error text from attribute statusText

 EXAMPLE USE:

<script type="text/javascript" src="ajax_class.js"></script>
<script type="text/javascript">

function ShowText() {

 el_loading=document.getElementById('loading');

 el=document.getElementById('result');
 el.innerHTML = '';
 test=new ajax('test.txt');
 test.OnReadyStateChange = function() {
    if (test.xmlHttpReq.readyState == (1 || 0)) {
       el_loading.innerHTML = '<h1 style="color:#f00;">Ładowanie...<'+'/h1>';
    }

    if (test.xmlHttpReq.readyState == 4 && test.xmlHttpReq.status == 200) {
       el_loading.innerHTML = '';
       el.innerHTML = test.xmlHttpReq.responseText;
    }

    if (test.xmlHttpReq.readyState != (1 || 0 || 4)) { el_loading.innerHTML = ''; }
 }
 test.Open();

}

</script>

<form action="#" id="f1">
  <input value="Go" type="button" onclick="ShowText();">
</form>

<div id="loading"></div>

<div id="result"></div>


 */

if (typeof ajax == "undefined") var ajax = new Object();

ajax = function(sourceURL) {

	this.xmlHttpReq = false;
	if (window.XMLHttpRequest) {
		this.xmlHttpReq = new XMLHttpRequest();
	} else
	if (window.ActiveXObject) {
		this.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); /* for IE */
	}

	this.sourceURL = sourceURL;

	/* default settings */
	this.openAsynchronously = true;

	this.sendingPOSTmethod = true;

	this.AuthenticationUserName = '';
	this.AuthenticationPassword = '';

	this.queryString = '';
	
	this.SendConnectionCloseHeader = false;
	this.SendContentLengthHeader = false;

}

ajax.prototype = {

	/* sourceURL methods */
	GetSourceURL: function() {
		return (this.sourceURL);
	},

	SetSourceURL: function(sourceURL) {
		this.sourceURL = sourceURL;
	},


	/* sending methods */
	IsSendingPOSTmethod: function() {
		return (this.sendingPOSTmethod) ? true : false;
	},

	SetSendingPOSTmethod: function() {
		this.sendingPOSTmethod = true;
	},

	IsSendingGETmethod: function() {
		return (this.sendingPOSTmethod) ? false : true;
	},

	SetSendingGETmethod: function() {
		this.sendingPOSTmethod = false;
	},


	/* AuthenticationUserName methods */
	GetAuthenticationUserName: function() {
		return (this.AuthenticationUserName);
	},

	SetAuthenticationUserName: function(AuthenticationUserName) {
		this.AuthenticationUserName = AuthenticationUserName;
	},


	/* AuthenticationPassword methods */
	GetAuthenticationPassword: function() {
		return (this.AuthenticationPassword);
	},

	SetAuthenticationPassword: function(AuthenticationPassword) {
		this.AuthenticationPassword = AuthenticationPassword;
	},


	/* SendConnectionCloseHeader methods */
	GetSendConnectionCloseHeader: function() {
		return (this.SendConnectionCloseHeader);
	},

	SetSendConnectionCloseHeader: function(SendConnectionCloseHeader) {
		this.SendConnectionCloseHeader = SendConnectionCloseHeader;
	},


	/* SendContentLengthHeader methods */
	GetSendContentLengthHeader: function() {
		return (this.SendContentLengthHeader);
	},

	SetSendContentLengthHeader: function(SendContentLengthHeader) {
		this.SendContentLengthHeader = SendContentLengthHeader;
	},


	/* query string methods */
	GetQueryString: function() {
		return (this.queryString);
	},
	SetQueryString: function(queryString) {
		this.queryString=queryString;
	},


	/* Result methods */
	GetTextResults: function() {
		if (typeof(this.xmlHttpReq.responseText)!='undefined') {
			return (this.xmlHttpReq.responseText);
		} else {
			return '';
		}
	},

	GetXMLResults: function() {
		if (typeof(this.xmlHttpReq.responseXML)!='undefined') {
			return (this.xmlHttpReq.responseXML);
		} else {
			return '';
		}
	},


	/* OnReadyStateChange method */
	OnReadyStateChange: function() {
	},


	/* Open methods */
	GetOpenSynchronouslyType: function() {
		return (this.openAsynchronously) ? true : false;
	},

	SetOpenAsynchronously: function() {
		this.openAsynchronously = true;
	},

	SetOpenSynchronously: function() {
		this.openAsynchronously = false;
	},

	Open: function() {
		if (!this.xmlHttpReq) return false;

		var sendingMethod = (this.sendingPOSTmethod) ? 'POST' : 'GET';

		var sourceURL = this.sourceURL;
		if ( !this.sendingPOSTmethod && this.GetQueryString() ) {
			sourceURL+='?'+this.GetQueryString();
		}

		this.xmlHttpReq.open( sendingMethod, sourceURL, this.GetOpenSynchronouslyType(), this.AuthenticationUserName, this.AuthenticationPassword );

		var queryString = null;
		if (this.sendingPOSTmethod) {
			this.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			if (this.GetQueryString()) {
				queryString = this.GetQueryString();
				if (this.GetSendContentLengthHeader()) {
					this.xmlHttpReq.setRequestHeader("Content-Length", queryString.length);
				}
			}

			if (this.GetSendConnectionCloseHeader()) {
				this.xmlHttpReq.setRequestHeader("Connection", "close");
			}
		}

		this.xmlHttpReq.onreadystatechange = this.OnReadyStateChange;

		this.xmlHttpReq.send(queryString);
	}

}
