
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 = 'includes/image_req.php';
	doRefresh(url, displayImg);
}

function doRefresh(url, callback)
{
	sess.open('POST', 'includes/newsession.php', true);
	sess.send(null);
	http.open('POST', url, true);
	http.onreadystatechange = callback;
	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 url = 'includes/process.php?captcha=' + submission;
	docheck(url, displaycheck);
}

function docheck(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = callback;
	http.send(null);
}

//SUBMIT THE FORM, IF THE CAPTCHA IS CORRECT
function submitform(){
	var name = document.getElementById("name").value;
	var tel = document.getElementById("tel").value;
	var email = document.getElementById("email").value;
	var msg = document.getElementById("msg").value;
	//
	document.getElementById('loading').style.display = 'block';
	document.captchaform.submit.disabled = 'true'; //DISABLE THE SUBMIT BUTTON
	//document.getElementById("submit").disabled = 'true';
	
	http.open('GET', 'includes/mailer.php?name=' +name +'&tel=' + tel +'&email=' +email+ '&msg='+escape(msg)); 
	http.onreadystatechange = printit;
	http.send(null);

}
//PRINT THE RESPONSE FROM PHP
function printit()
{
	if(http.readyState == 4)
	{
		document.getElementById('loading').style.display = 'none';
		document.getElementById('results').innerHTML = http.responseText; //PRINT THE PHP'S RESPONSE IN THE RESULTS DIV
		
		document.captchaform.submit.disabled = false; //ENABLE THE SUBMIT BUTTON
		
		document.getElementById("name").value = '';
		document.getElementById("name").focus();
		
		document.getElementById("tel").value = '';
		document.getElementById("email").value = '';
		document.getElementById("msg").value = '';
		document.getElementById("captcha").value = '';
		
		refreshImg()
	}
}	
function displaycheck()
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == '1') //CAPTCHA IS CORRECT
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#A8C903';
			document.getElementById('captchaerror').innerHTML = '';

			submitform(); //SUBMIT THE FORM
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#FF5500';
			document.captchaform.captcha.value = ''; //RESET THE CAPTCHA INPUT'S VALUE
			document.captchaform.captcha.focus(); //CHANGE THE FOCUS TO CAPTCHA INPUT
			document.getElementById('captchaerror').innerHTML = '<font color="#FFFFFF"><b>'+ errorCaptcha + '</b></font>';
		}
	}
}

//BYPASS CACHE...?
/*
var req = new XMLHttpRequest();
req.open("GET", url += (url.match(/\?/) == null ? "?" : "&") + (new Date()).getTime(), false);
req.send(null);
*/