function createRequestObject()
{
	try
	{
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		alert('Sorry, but your browser doesn\'t support XMLHttpRequest.');
	}
	return xmlhttp;
}

var http = createRequestObject();
var sess = createRequestObject();

// IMAGE REFRESHING

function refreshimg()
{
	var url = 'image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback)
{
	sess.open('POST', 'newsession.php', true);
	sess.send(null);
	http.open('POST', url, true);
	http.onreadystatechange = displayimg;
	http.send(null);
}

function displayimg()
{
	if(http.readyState == 4)
	{
		var showimage = http.responseText;
		document.getElementById('captchaimage').innerHTML = showimage;
	}
}

// SUBMISSION

function check()
{
	var submission = document.getElementById('captcha').value;
	var yourname = document.getElementById('yourname').value;
	var yourcomments = document.getElementById('yourcomments').value;
	var youremail = document.getElementById('youremail').value;
	var owner = document.getElementById('owner').value;
	var url = 'http://www.mapsgrancanaria.com/capt2/captcha/process.php?captcha=' + submission + '&yourname=' + yourname + '&owner=' + owner + '&youremail=' + youremail + '&yourcomments=' + yourcomments;
	docheck(url, displaycheck);
}

function docheck(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

function displaycheck()
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == '1')
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
			document.getElementById('captcha').value = 'thank you, your message is sent';
			document.getElementById('submit').value = 'thank you for sending...';
			document.getElementById('submit').disabled = 'true';
			document.getElementById('boven').innerHTML = '<i>thank you, the following message is sent:</i>';
			document.getElementById('eerst').innerHTML = document.getElementById('yourcomments').value;
			
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';
			document.getElementById('captcha').value = 'wrong numbers try again';

		}
		if(showcheck == '3')
		{
			document.getElementById('yourname').style.border = '1px solid #c24949';
			document.getElementById('yourname').style.background = '#ffbcbc';
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
			document.getElementById('yourcomments').style.border = '1px solid #49c24f';
			document.getElementById('yourcomments').style.background = '#bcffbf';
			document.getElementById('youremail').style.border = '1px solid #49c24f';
			document.getElementById('youremail').style.background = '#bcffbf';
		}
		if(showcheck == '4')
		{
			document.getElementById('youremail').style.border = '1px solid #c24949';
			document.getElementById('youremail').style.background = '#ffbcbc';
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
			document.getElementById('yourcomments').style.border = '1px solid #49c24f';
			document.getElementById('yourcomments').style.background = '#bcffbf';
		}
		if(showcheck == '5')
		{
			document.getElementById('yourcomments').style.border = '1px solid #c24949';
			document.getElementById('yourcomments').style.background = '#ffbcbc';
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
		}
	}
}

// CREATING THE REQUEST
function jah(url,target) {
    // native XMLHttpRequest object
    document.getElementById(target).innerHTML = 'sending...';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {jahDone(target);};
        req.open("GET", url, true);
        req.send(null);
    // IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {jahDone(target);};
            req.open("GET", url, true);
            req.send();
        }
    }
}    

function jahDone(target) {
    // only if req is "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            results = req.responseText;
            document.getElementById(target).innerHTML = results;
        } else {
            document.getElementById(target).innerHTML="jah error:\n" +
                req.statusText;
        }
    }
}
