/*
 * Created on 29.05.2011
 * Copyright (c) Andreas Bauer <andreas.bauer@creaktif.de>
 */

function Ajax()
{
	this._ajaxRequest = null;
	
	try
	{
		// Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
		this._ajaxRequest = new XMLHttpRequest();
	}
	catch(ex)
	{
	    try
	    {
	        // MS Internet Explorer (ab v6)
	    	this._ajaxRequest  = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch(ex)
	    {
	        try
	        {
	            // MS Internet Explorer (ab v5)
	        	this._ajaxRequest  = new ActiveXObject("Msxml2.XMLHTTP");
	        }
	        catch(ex) { }
	    }
	}
}

//Dieses Objekt dem CustomEvent Object ableiten und ein Event abonnieren
Ajax.prototype = new CustomEvent("OnAjaxRequestComplete");

// Ein leeres Event abonnieren falls kein Custom Event abonniert wurde
Ajax.prototype.Subscribe(function(sender, eventArgs) { });

Ajax.prototype.Request = function(url, params)
{
	var objRef = this;
	this._ajaxRequest.open("POST", url, true);
	this._ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	this._ajaxRequest.onreadystatechange = function() {
		if (objRef._ajaxRequest.readyState == 4 && objRef._ajaxRequest.status == 200)
		{
			// Wenn der Ajax Request erfolgreich ist wird ein Event ausgelšst
			objRef.Fire(objRef._ajaxRequest, objRef._ajaxRequest.status);
		}
	};
	
	this._ajaxRequest.send(params);
}
