function MyAjaxRequest(target_div,file,check_div)
{
	var MyHttpRequest = false;
//	var MyHttpLoading = '<img src=\"images/ajaxLaden.gif\" border=\"0\">';     voorals nog geen grote data dus nauwelijks laadtijd
	var MyHttpLoading = '';
	var ErrorMSG = 'Sorry - No XMLHTTP support in your browser';

	if(check_div)
	{
		var check_value = document.getElementById(check_div).value;
	}
	else
	{
		var check_value = '';
	}



	if(window.XMLHttpRequest)
	{
		try
		{
			MyHttpRequest = new XMLHttpRequest();
		}
		catch(e)
		{
			MyHttpRequest = false;
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				MyHttpRequest = false;
			}
		}
	}
	else
	{
		MyHttpRequest = false;
	}



	if(MyHttpRequest)
	{
		var random = Math.random() * Date.parse(new Date());
		var file_array = file.split('.');
		
		if(file_array[1] == 'php')
		{
			var query_string = '?rand=' + random;
		}
		else if(file_array[1] == 'htm' || file_array[1] == 'html')
		{
			var query_string = '';
		}
		else // we have presumable a php file with a query string attached
		{
			var query_string = check_value + '&rand=' + random;
		}
		
		
		if(target_div != '')
		{
			MyHttpRequest.open("get", url_encode(file + query_string), true);
			MyHttpRequest.onreadystatechange = function ()
			{
				if(MyHttpRequest.readyState == 4)
				{
					if(target_div != '')
					{
						document.getElementById(target_div).innerHTML = MyHttpRequest.responseText;
					}
				}
				else
				{
					if(target_div != '')
					{
						document.getElementById(target_div).innerHTML = MyHttpLoading;
					}
				}
			}
			MyHttpRequest.send(null);
		}
		else
		{
			try
			{
				MyHttpRequest.open("get", url_encode(file + query_string), false);
				MyHttpRequest.send(null);
				return MyHttpRequest.responseText;
			}
			catch(e)
			{
				//error	
			}
		}
	}
	else 
	{
		if(target_div != '')
		{
			document.getElementById(target_div).innerHTML = ErrorMSG;
		}
	}
}

function MyAjaxReturn(file){
    
    var MyHttpRequest = false;
//    var MyHttpLoading = '<img src=\"images/ajaxLaden.gif\" border=\"0\">';     voorals nog geen grote data dus nauwelijks laadtijd
    var MyHttpLoading = '';
    var ErrorMSG = 'Sorry - No XMLHTTP support in your browser';
    
    if(window.XMLHttpRequest)
    {
        try
        {
            MyHttpRequest = new XMLHttpRequest();
        }
        catch(e)
        {
            MyHttpRequest = false;
        }
    }
    else if(window.ActiveXObject)
    {
        try
        {
            MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                MyHttpRequest = false;
            }
        }
    }
    else
    {
        MyHttpRequest = false;
    }
    
    if(MyHttpRequest)
    {
        var random = Math.random() * Date.parse(new Date());
        var file_array = file.split('.');
        
        if(file_array[1] == 'php')
        {
            var query_string = '?rand=' + random;
        }
        else if(file_array[1] == 'htm' || file_array[1] == 'html')
        {
            var query_string = '';
        }
        else // we have presumable a php file with a query string attached
        {
            var query_string = check_value + '&rand=' + random;
        }
        
        MyHttpRequest.open("get", url_encode(file + query_string), false);
        MyHttpRequest.send(null);
        return MyHttpRequest.responseText;

    }
    
}

function makePOSTRequestGuestbook(url, parameters, strType, intType) {
    http_request = false;
    if (window.XMLHttpRequest)
    { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
        {
            // set type accordingly to anticipated content type
            http_request.overrideMimeType('text/xml'); 
            //http_request.overrideMimeType('text/html');
        }
    }
    else if (window.ActiveXObject)
    { // IE
        try
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {}
        }
    }
    if (!http_request)
    {
        alert('Cannot create XMLHTTP instance');
        return false;
    }

    http_request.onreadystatechange = postSucces;
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}

function postSucces()
{
    if (http_request.readyState == 4)
    {
        if (http_request.status == 200)
        {
            var text    = http_request.responseText;
            arr_result  = text.split(":");
            var strType = arr_result[0];
            var intType = arr_result[1];
            
            if(intType == 'fail')
            {
                alert('U kunt geen leeg bericht plaatsen!');
            }
            else
            {
                $('#commentFormDiv').empty().append('Uw reactie is geplaatst.');
                setTimeout("showAllGbComments('" + intType + "');",1000);
            }
            
        }
    }
}

function makePOSTRequest(url, parameters, strType, intType) {
    http_request = false;
    if (window.XMLHttpRequest)
    { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
        {
            // set type accordingly to anticipated content type
            http_request.overrideMimeType('text/xml'); 
            //http_request.overrideMimeType('text/html');
        }
    }
    else if (window.ActiveXObject)
    { // IE
        try
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {}
        }
    }
    if (!http_request)
    {
        alert('Cannot create XMLHTTP instance');
        return false;
    }

    http_request.onreadystatechange = showLastReactions;
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}

function showLastReactions()
{
    if (http_request.readyState == 4)
    {
        if (http_request.status == 200)
        {
            var text    = http_request.responseText;
            arr_result  = text.split(":");
            
            var strType = arr_result[0];
            var intType = arr_result[1];
            
            document.getElementById('commentForm').style.display='none';
            setTimeout("showAllComments('" + strType + "', '" + intType + "');",1000);
            
        }
    }
}

function showAllComments(strType, intType)
{
    MyAjaxRequest('commentaarItems','/js/ajaxdata.php?showReactions=true&type=' + strType + '&typeid=' + intType);
    document.getElementById('reageerMediaBtn').style.display='block';
}

function showAllGbComments(intClub)
{
    MyAjaxRequest('gastBoekItems','/js/ajaxdata.php?showGbReactions=true&clubid=' + intClub);
}

function url_encode(string)
{
	var string;
	var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?=";
	var hex = "0123456789ABCDEF";
	var encoded_string = "";
	
	for(var i = 0; i < string.length; i++)
	{
		var character = string.charAt(i);
		
		if(character == " ")
		{
			encoded_string += "+";
		}
		else if(safechars.indexOf(character) != -1)
		{
			encoded_string += character;
		}
		else
		{
			var hexchar = character.charCodeAt(0);
			
			if(hexchar > 255)
			{
				encoded_string += "+";
			}
			else
			{
				encoded_string += "%";
				encoded_string += hex.charAt((hexchar >> 4) & 0xF);
				encoded_string += hex.charAt(hexchar & 0xF);
			}
		}
	}
	return encoded_string;
}

