/**
 * @version		1.0.8 - 27 november 2010
 * 
 * @copyright	1.0.8,	27 november 2010,	Jan Niemantsverdriet,	Kan meerdere verzoeken tegelijk versturen en ontvangen
 * @copyright	1.0.7,	26 november 2010,	Bas Verweij,			vSimpleAjaxSend controleert het protocol van het URL (HTTP/HTTPS)
 * @copyright	1.0.6,	1 februari 2010,	Jan Niemantsverdriet,	vSimpleAjaxSend geeft nu een requestid mee
 * @copyright	1.0.5,	24 september 2009,	Jan Niemantsverdriet,	vSimpleAjaxSend geeft nu altijd een waarde terug
 * @copyright	1.0.4,	15 september 2009,	Rob Ruigrok,			Abort-functie aanroepen voordat een lopend ajaxverzoek wordt verwijderd
 * @copyright	1.0.3,	6 oktober 2008,		Jan Niemantsverdriet,	vSimpleAjaxSend geeft nu altijd een boolean terug (naam zo gehouden voor backwords compatibility)
 * @copyright	1.0.2,	9 september 2008,	Jan Niemantsverdriet,	Ajax verzoek kan weer geannuleerd worden
 * @copyright	1.0.1,	1 juli 2008,		Jan Niemantsverdriet,	Door als callback functie null op te geven kan je eenrichtingsverkeer behalen
 * @copyright	1.0.0,	29 mei 2008,		Jan Niemantsverdriet,	Gemaakt
 * 
 * dependency: /BB/JSDivers/webcoding.js
 */

/**
 * Het ajax object
 */
var oSimpleAjaxRequestObject = new Object();

/**
 * De functie waar het resultaat van het ajax verzoek heen wordt gestuurd
 */
var aAjaxSimpleReceiveFunction = new Object();

/**
 * Een teller
 */
var iAjaxSimpleRequestId = 10000;

/**
 * Verstuurd een ajax verzoek
 * 
 * @param string a_sURL					de url waar het verzoek heen wordt verzonden
 * @param string a_sPost				de gegevens die naar dat adres moeten worden verzonden
 * @param handle a_hReceiveFunction		de functie die het resultaat af zal handelen
 */
function vSimpleAjaxSend(a_sURL, a_sPost, a_hReceiveFunction, a_sMimeType, a_iNr) {
	var sMimeType = (a_sMimeType ? a_sMimeType : 'text/xml');
	var iNr = (a_iNr ? a_iNr : 0);
	try {
    	// Firefox, Opera 8.0+, Safari
		if (oSimpleAjaxRequestObject[iNr]) {
			oSimpleAjaxRequestObject[iNr].abort();
		}
	    oSimpleAjaxRequestObject[iNr] = new XMLHttpRequest();
	    if (oSimpleAjaxRequestObject[iNr].overrideMimeType) {
	    	oSimpleAjaxRequestObject[iNr].overrideMimeType(sMimeType);
	    }
    }
  	catch (e) {
    	// Internet Explorer
		try
		{
			if (oSimpleAjaxRequestObject[iNr]) {
				oSimpleAjaxRequestObject[iNr].abort();
			}
		    oSimpleAjaxRequestObject[iNr] = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				if (oSimpleAjaxRequestObject[iNr]) {
					oSimpleAjaxRequestObject[iNr].abort();
				}
			    oSimpleAjaxRequestObject[iNr] = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				hErrorFunction(2, 3, "Deze browser ondersteunt geen Ajax");
				return false;
			}
		}
	}
	aAjaxSimpleReceiveFunction[iNr] = a_hReceiveFunction;
	oSimpleAjaxRequestObject[iNr].onreadystatechange = vSimpleAjaxProccesState;

	// check URL protocol (HTTP vs HTTPS)
	protocolLength = a_sURL.indexOf(":") + 1;
	if ( window.location.protocol.length != protocolLength )
	{
		a_sURL = window.location.protocol + a_sURL.substr(protocolLength);
	}
	
	oSimpleAjaxRequestObject[iNr].open('POST', a_sURL, true);
    oSimpleAjaxRequestObject[iNr].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	a_sPost = a_sPost + (a_sPost == '' ? '?' : '&' ) + 'iAjaxRequestId=' + iAjaxSimpleRequestId;
	iAjaxSimpleRequestId++;
    oSimpleAjaxRequestObject[iNr].send(sURLEncode(a_sPost));
	return true;
}

/**
 * Controleerd of het verzoek (succesvol) is afgerond en geeft het resultaat aan de ingestelde functie
 */
function vSimpleAjaxProccesState() {
	var iAjaxNr = -1;
	for (var iNr in oSimpleAjaxRequestObject) {
		if (oSimpleAjaxRequestObject.hasOwnProperty(iNr)) {
			if (oSimpleAjaxRequestObject[iNr].readyState == 4) {
				iAjaxNr = iNr;
				break;
			}
		}
	}
	if (iAjaxNr < 0) return;
	
	if (oSimpleAjaxRequestObject[iAjaxNr].status == 200) {
		if (aAjaxSimpleReceiveFunction[iAjaxNr] !== null) aAjaxSimpleReceiveFunction[iAjaxNr](oSimpleAjaxRequestObject[iAjaxNr]);
	} else {
		if (oSimpleAjaxRequestObject[iAjaxNr].status > 0) hErrorFunction(0, 2, "Opdracht kon niet worden uitgevoerd. Status: " + oSimpleAjaxRequestObject[iAjaxNr].status + ", " + oSimpleAjaxRequestObject[iAjaxNr].responseText);
	}
	vSimpleAjaxClear(iAjaxNr);
}

/**
 * Breekt het huidige verzoek af
 *
 * @since 1.0.2 - 9 september 2008
 */
function vSimpleAjaxClear(a_iNr) {
	var iNr = (a_iNr ? a_iNr : 0);
	delete oSimpleAjaxRequestObject[iNr];
	delete aAjaxSimpleReceiveFunction[iNr];
}
