// JavaScript Document

//################
//##### AJAX #####
//################
function ajax() {

	//PROPERTIES
	try {
		this.XMLHTTP = new XMLHttpRequest ();
	} catch (ee) {
		try {
			this.XMLHTTP = new ActiveXObject ("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.XMLHTTP = new ActiveXObject ("Microsoft.XMLHTTP");
			} catch (E) {
				this.XMLHTTP = false;
				throw new Error(AJAX_ERRORS['xmlhttp'][0])
			}
		}
	}
	this.PARAMSTOSEND = new Array()
	this.REQUESTURL = new String()
	this.METHOD = new String()
	this.AJAX_ERRORS = new Array()


	//METHODS
	this.setparam = __setParam
	this.seturl = __setURL
	this.post = __methodPOST
	this.get = __methodGET
	this.mystatereader
	this.gorequest = __request

	
	function __setParam(name, vl) {
		if (name)
			this.PARAMSTOSEND.push(name+'='+vl)
	}

	function __setURL(url) {
		if (url)
			this.REQUESTURL = url
	}
	
	function __methodPOST() {
		this.METHOD = 'POST'
	}
	
	function __methodGET() {
		this.METHOD = 'GET'
	}

	function __request() {

		if (this.REQUESTURL == '')
			throw new Error(this.AJAX_ERRORS['requesturl'][0])

		var Params = this.PARAMSTOSEND.length > 0 ? this.PARAMSTOSEND.join('&') : null


		//array de parametro é zerado para ser preenchido novamente numa outra requisição
		this.PARAMSTOSEND.length = 0


		if (this.METHOD == 'undefined')
			this.METHOD = 'GET'
		
		if (this.METHOD == 'POST') {
			this.XMLHTTP.open('POST', this.REQUESTURL+'?uniq='+Math.random(), true)
			this.XMLHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
			this.XMLHTTP.setRequestHeader('Content-Type', "application/x-www-form-urlencoded; charset=iso-8859-1")
			this.XMLHTTP.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate")
			this.XMLHTTP.setRequestHeader("Cache-Control", "post-check=0, pre-check=0")
			this.XMLHTTP.setRequestHeader("Pragma", "no-cache")
			this.XMLHTTP.send(Params)
		}
		else {
			var URL = Params == null ? this.REQUESTURL+'?uniq='+Math.random() : this.REQUESTURL+'?'+Params+'&uniq='+Math.random()
			this.XMLHTTP.open('GET', URL, true)
			this.XMLHTTP.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate")
			this.XMLHTTP.setRequestHeader("Cache-Control", "post-check=0, pre-check=0")
			this.XMLHTTP.setRequestHeader("Pragma", "no-cache")
			this.XMLHTTP.send(null)
		}

/*		if (this.onreadystate1 != 'undefined') XMLHTTP.onreadystate1 = this.onreadystate1
		if (this.onreadystate2 != 'undefined') XMLHTTP.onreadystate2 = this.onreadystate2
		if (this.onreadystate3 != 'undefined') XMLHTTP.onreadystate3 = this.onreadystate3
		if (this.onreadystate4 != 'undefined') XMLHTTP.onreadystate4 = this.onreadystate4
		if (this.status200 != 'undefined') XMLHTTP.status200 = this.status200
		if (this.withresponsetext != 'undefined') XMLHTTP.withresponsetext = this.withresponsetext
		if (this.withresponsexml != 'undefined') XMLHTTP.withresponsexml = this.withresponsexml

		XMLHTTP.onreadystatechange = function() {
			if (XMLHTTP.readyState == 1) {
				if (this.onreadystate1 != 'undefined') this.onreadystate1()
			}

			if (XMLHTTP.readyState == 4) {
				if (XMLHTTP.status == 200) {
					if (this.withresponsetext != 'undefined') this.withresponsetext()
					if (this.withresponsexml != 'undefined') this.withresponsexml()
				}
			}
		}*/

		this.XMLHTTP.onreadystatechange = this.mystatereader
	}


	// BIBLIOTECA DE ERROS
	// err xmlhttp
	this.AJAX_ERRORS['xmlhttp'] 	= new Array()
	this.AJAX_ERRORS['xmlhttp'][0] = 'Your browser does not support XMLHTTP'
	// err request url
	this.AJAX_ERRORS['requesturl']	   = new Array()
	this.AJAX_ERRORS['requesturl'][0] = 'Nenhuma URL informada'
}
