// 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 = 'captcha/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()
{

	//validate require fields
	var err="";

	
	if(document.contactForm.Name.value==""){
		err+="-Your Name\n";
	}

	if(document.contactForm.Company.value==""){
		err+="-Your Company name\n";
	}
		if(!checkMail(document.contactForm.EMail.value)){
		err+="-Email Address\n";
	}
	if(document.contactForm.Phone.value==""){
		err+="-Phone\n";
	}
		
	if(document.contactForm.Comments.value==""){
		err+="-Comments\n";
	}
	
	if(err!=""){
		alert("Please fill out required field(s):\n"+err);
		return false;
	}


	var submission = document.getElementById('captcha').value;
	var url = 'captcha/process.php?captcha=' + submission;
	http.open('GET', url,false);
	http.send(null);
	var showcheck = http.responseText;

		if(showcheck == '1')
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
			return true;
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';
			return false;

		}
}

function checkMail(email)
{
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

