// 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 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 = 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 address = document.getElementById("address").value;
		var zip = document.getElementById("zip").value;
		var email = document.getElementById("email").value;
		var phone = document.getElementById("phone").value;
		var fax = document.getElementById("fax").value;
		var www = document.getElementById("www").value;
		var area = document.getElementById("area").value;
		var info = document.getElementById("info").value;
	var url = 'includes/mailer.php?name=' + name +'&address=' +address +'&zip=' +zip +'&email=' +email  +'&phone=' +phone +'&fax=' + fax +'&www=' + www +'&area=' + area + '&info=' + info;
	docheck2(url, displaycheck2);
}

function docheck2(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck2;
	http.send(null);
}


function displaycheck2()
{

if(http.readyState == 4)
	{
		var name = document.getElementById("name").value;
		var address = document.getElementById("address").value;
		var zip = document.getElementById("zip").value;
		var email = document.getElementById("email").value;
		var phone = document.getElementById("phone").value;
		var fax = document.getElementById("fax").value;
		var www = document.getElementById("www").value;
		var area = document.getElementById("area").value;
		var info = document.getElementById("info").value;

		document.getElementById('nameerror').style.display = 'none';
		document.getElementById('name').style.border = '1px solid #999999';
		document.getElementById('name').style.background = '#fff';
		
		document.getElementById('addresserror').style.display = 'none';
		document.getElementById('address').style.border = '1px solid #999999';
		document.getElementById('address').style.background = '#fff';
		
		document.getElementById('ziperror').style.display = 'none';
		document.getElementById('zip').style.border = '1px solid #999999';
		document.getElementById('zip').style.background = '#fff';
		
		document.getElementById('emailerror').style.display = 'none';
		document.getElementById('email').style.border = '1px solid #999999';
		document.getElementById('email').style.background = '#fff';
		
		document.getElementById('phoneerror').style.display = 'none';
		document.getElementById('phone').style.border = '1px solid #999999';
		document.getElementById('phone').style.background = '#fff';
		
		document.getElementById('faxerror').style.display = 'none';
		document.getElementById('fax').style.border = '1px solid #999999';
		document.getElementById('fax').style.background = '#fff';
		
		document.getElementById('wwwerror').style.display = 'none';
		document.getElementById('www').style.border = '1px solid #999999';
		document.getElementById('www').style.background = '#fff';
		
		document.getElementById('areaerror').style.display = 'none';
		document.getElementById('area').style.border = '1px solid #999999';
		document.getElementById('area').style.background = '#fff';
		
		document.getElementById('infoerror').style.display = 'none';
		document.getElementById('info').style.border = '1px solid #999999';
		document.getElementById('info').style.background = '#fff';
		
		var showcheck = http.responseText;
		
		if(showcheck == '1') //name
		{
			document.getElementById('nameerror').style.display = 'block';
			document.getElementById('name').style.border = '1px solid #c24949';
			document.getElementById('name').style.background = '#ffbcbc';
			document.captchaform.name.focus();
			document.getElementById('nameerror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type Your Name</font>';
	
		}
		
		if(showcheck == '2') //name
		{
			document.getElementById('addresserror').style.display = 'block';
			document.getElementById('address').style.border = '1px solid #c24949';
			document.getElementById('address').style.background = '#ffbcbc';
			document.captchaform.address.focus();
			document.getElementById('addresserror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type Your Street Address</font>';
	
		}
		
		if(showcheck == '3') //name
		{
			document.getElementById('ziperror').style.display = 'block';
			document.getElementById('zip').style.border = '1px solid #c24949';
			document.getElementById('zip').style.background = '#ffbcbc';
			document.captchaform.zip.focus();
			document.getElementById('ziperror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type Your Zip Code</font>';
	
		}
		if(showcheck == '4') //name
		{
			document.getElementById('emailerror').style.display = 'block';
			document.getElementById('email').style.border = '1px solid #c24949';
			document.getElementById('email').style.background = '#ffbcbc';
			document.captchaform.email.focus();
			document.getElementById('emailerror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type eMail Address</font>';
	
		}
		if(showcheck == '5') //name
		{
			document.getElementById('emailerror').style.display = 'block';
			document.getElementById('email').style.border = '1px solid #c24949';
			document.getElementById('email').style.background = '#ffbcbc';
			document.captchaform.email.focus();
			document.getElementById('emailerror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Not correct eMail Address</font>';
	
		}
			if(showcheck == '6') //name
		{
			document.getElementById('phoneerror').style.display = 'block';
			document.getElementById('phone').style.border = '1px solid #c24949';
			document.getElementById('phone').style.background = '#ffbcbc';
			document.captchaform.phone.focus();
			document.getElementById('phoneerror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type Your Phone Number</font>';
	
		}
			if(showcheck == '7') //name
		{
			document.getElementById('faxerror').style.display = 'block';
			document.getElementById('fax').style.border = '1px solid #c24949';
			document.getElementById('fax').style.background = '#ffbcbc';
			document.captchaform.fax.focus();
			document.getElementById('faxerror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type Your Fax Number</font>';
	
		}
		
		if(showcheck == '8') //name
		{
			document.getElementById('wwwerror').style.display = 'block';
			document.getElementById('www').style.border = '1px solid #c24949';
			document.getElementById('www').style.background = '#ffbcbc';
			document.captchaform.www.focus();
			document.getElementById('wwwerror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type Your Fax Number</font>';
	
		}
		if(showcheck == '9') //name
		{
			document.getElementById('areaerror').style.display = 'block';
			document.getElementById('area').style.border = '1px solid #c24949';
			document.getElementById('area').style.background = '#ffbcbc';
			document.captchaform.area.focus();
			document.getElementById('areaerror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type Area of Your Work</font>';
	
		}
		if(showcheck == '10') //name
		{
			document.getElementById('infoerror').style.display = 'block';
			document.getElementById('info').style.border = '1px solid #c24949';
			document.getElementById('info').style.background = '#ffbcbc';
			document.captchaform.info.focus();
			document.getElementById('infoerror').innerHTML = '<font color="#c24949"><strong>ERROR</strong>: Please Type Infor about Your Service</font>';
	
		}
		
		
		if(showcheck == '11')
		{
			document.captchaform.submit();
		}
	}


}
//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
	}
}	
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>Please Re-enter the CAPTCHA</b></font>';
		}
	}
}