// CREATING THE REQUEST

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 httpObj = createRequestObject();
var sess = createRequestObject();

// IMAGE REFRESHING

function refreshimg()
{
	var url = 'do/includes/image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback)
{
	sess.open('POST', 'do/includes/newsession.php', true);
	sess.send(null);
	httpObj.open('POST', url, true);
	httpObj.onreadystatechange = displayimg;
	httpObj.send(null);
}

function displayimg()
{
	if(httpObj.readyState == 4)
	{
		var showimage = httpObj.responseText;
		document.getElementById('captchaimage').innerHTML = showimage;
	}
}

// SUBMISSION

function check()
{
	var submission = document.getElementById('captcha').value;
	var url = 'do/includes/process.php?captcha=' + submission;
	docheck(url, displaycheck);
}

function docheck(url, callback)
{
	httpObj.open('GET', url, true);
	httpObj.onreadystatechange = displaycheck;
	httpObj.send(null);
}

//SUBMIT THE FORM, IF THE CAPTCHA IS CORRECT
function submitform(){

var first_name = document.getElementById("first_name").value;
var last_name = document.getElementById("last_name").value;
var email = document.getElementById("email").value;
var phone = document.getElementById("phone").value;
var title = document.getElementById("title").value;
var company = document.getElementById("company").value;
var website = document.getElementById("website").value;
var address = document.getElementById("address").value;
var city = document.getElementById("city").value;
var state_province = document.getElementById("state_province").value;
var advertise = document.getElementById("advertise").value;

document.getElementById('loading').style.display = 'block';
//document.captchaform.submit.disabled = 'true'; //DISABLE THE SUBMIT BUTTON
httpObj.open('GET', 'do/includes/mailer-advertise.php?first_name=' +first_name+ '&last_name=' +last_name+ '&email=' +email+ '&phone=' +phone+ '&title=' +title+ '&company=' +company+ '&website=' +website+ '&address=' +address+ '&city=' +city+ '&state_province=' +state_province+ '&advertise=' +escape(advertise));
	httpObj.onreadystatechange = printix;
	httpObj.send(null);
}



//PRINT THE RESPONSE FROM PHP
function printix()
{
	if (httpObj.readyState == 4) {
		document.getElementById('loading').style.display = 'none';
		document.getElementById('results').innerHTML = httpObj.responseText.substr(1);
	}
	if (httpObj.responseText.substr(0, 1)=='1') {
		document.captchaform.submit.disabled = 'true';
	}
}	
function displaycheck()
{
	if(httpObj.readyState == 4)
	{
		var showcheck = httpObj.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>Please Re-enter the CAPTCHA</b></font>';
		}
	}
}