var http = createRequestObject();
var sess = createRequestObject();

// CREATING THE REQUEST

function createRequestObject()
{
		if (typeof XMLHttpRequest == 'undefined') {
		objects = Array(
			'Microsoft.XmlHttp',
			'MSXML2.XmlHttp',
			'MSXML2.XmlHttp.3.0',
			'MSXML2.XmlHttp.4.0',
			'MSXML2.XmlHttp.5.0'
		);
		for (i = 0; i < objects.length; i++) {
			try {
				return new ActiveXObject(objects[i]);
			} catch (e) {}
		}
	} else {
		return new XMLHttpRequest();
	}
	
}

// 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 = 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 url = 'includes/process.php?captcha=' + submission;
	docheck(url, displaycheck);
}

function docheck(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

//SUBMIT THE FORM, IF THE CAPTCHA IS CORRECT
function submitform(){
var name = document.getElementById("name").value;
var subject = document.getElementById("subject").value;
var email = document.getElementById("email").value;
var msg = document.getElementById("msg").value;
document.getElementById('loading').style.display = 'block';
//loading.innerHTML="<center><img src='images/ajax-loader.gif'><br /><b>Sending the mail...</b><br /></center>";
http.open('GET', 'includes/mailer.php?name=' +name +'&subject=' +subject +'&email=' +email+ '&msg='+msg); 
	http.onreadystatechange = printit;
	http.send(null);
	document.captchaform.submit.disabled = 'true'; //DISABLE THE SUBMIT BUTTON	
}
//PRINT THE RESPONSE FROM PHP
function printit(){
	try {
		//if(http.readyState == 4){
		if((http.readyState == 4)&&(http.status == 200)){	
			document.getElementById('loading').style.display = 'none';
			document.getElementById('results').innerHTML = http.responseText; //PRINT THE PHP'S RESPONSE IN THE RESULTS DIV
		}
  	}
	catch(e){}
	finally{}
	
}	
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 = '#bcffbf';
			document.getElementById('captchaerror').innerHTML = '';
	
			submitform(); //SUBMIT THE FORM
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';
			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="#c24949"><b>Vui lòng nhập lại Mã bảo vệ</b></font>';
		}
	}
}