/**
 * @version		1.0.7 - 26 november 2010
 * 
 * @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 = false;

/**
 * De functie waar het resultaat van het ajax verzoek heen wordt gestuurd
 */
var hAjaxSimpleReceiveFunction = null;

/**
 * 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) {
	var sMimeType = (a_sMimeType ? a_sMimeType : 'text/xml');
	try
    {
    	// Firefox, Opera 8.0+, Safari
		if (oSimpleAjaxRequestObject) {
			oSimpleAjaxRequestObject.abort();
		}
	    oSimpleAjaxRequestObject = new XMLHttpRequest();
	    if (oSimpleAjaxRequestObject.overrideMimeType) {
	    	oSimpleAjaxRequestObject.overrideMimeType(sMimeType);
	    }
    }
  	catch (e)
    {
    	// Internet Explorer
		try
		{
			if (oSimpleAjaxRequestObject) {
				oSimpleAjaxRequestObject.abort();
			}
		    oSimpleAjaxRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				if (oSimpleAjaxRequestObject) {
					oSimpleAjaxRequestObject.abort();
				}
			    oSimpleAjaxRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				hErrorFunction(2, 3, "Deze browser ondersteunt geen Ajax");
				return false;
			}
		}
	}
	hAjaxSimpleReceiveFunction = a_hReceiveFunction;
	oSimpleAjaxRequestObject.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.open('POST', a_sURL, true);
    oSimpleAjaxRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	a_sPost = a_sPost + (a_sPost == '' ? '?' : '&' ) + 'iAjaxRequestId=' + iAjaxSimpleRequestId;
	iAjaxSimpleRequestId++;
    oSimpleAjaxRequestObject.send(sURLEncode(a_sPost));
	return true;
}

/**
 * Controleerd of het verzoek (succesvol) is afgerond en geeft het resultaat aan de ingestelde functie
 */
function vSimpleAjaxProccesState() {
	if (oSimpleAjaxRequestObject.readyState == 4) {
		if (oSimpleAjaxRequestObject.status == 200) {
			if (hAjaxSimpleReceiveFunction !== null) hAjaxSimpleReceiveFunction(oSimpleAjaxRequestObject);
		} else {
			if (oSimpleAjaxRequestObject.status > 0) hErrorFunction(0, 2, "Opdracht kon niet worden uitgevoerd. Status: " + oSimpleAjaxRequestObject.status + ", " + oSimpleAjaxRequestObject.responseText);
		}
	}	
}

/**
 * Breekt het huidige verzoek af
 *
 * @since 1.0.2 - 9 september 2008
 */
function vSimpleAjaxClear() {
	oSimpleAjaxRequestObject = false;
	hAjaxSimpleReceiveFunction = null;
}
