// 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', 'captcha/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(path) {
	var submission = document.getElementById('captcha').value;
    var url = 'captcha/process.php?captcha=' + submission;
	//docheck(url, displaycheck);
	http.open('GET', url, true);
	if(path == 'registration') {
		http.onreadystatechange = displaycheck;
	}
	else if(path == 'password') {
		http.onreadystatechange = displaycheck1;
	}
	else if(path == 'username') {
		http.onreadystatechange = displaycheck2;
	}
	else if(path == 'contact') {
		http.onreadystatechange = displaycheck3;
	}
	http.send(null);
}

function docheck(url, callback) {
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

function displaycheck() {
	if(http.readyState == 4) {
		document.getElementById('captchadiv').innerHTML = '';
		var showcheck = http.responseText;
		if(showcheck == '1') {
            document.getElementById('temp').value = 'Valid';
            document.user_add_frm.eee.disabled = true;
			document.user_add_frm.eee.value = "Submitting...";
			document.user_add_frm.method="post";
			document.user_add_frm.action="registration.php";
		    document.user_add_frm.submit();
		}
		if(showcheck == '0') { 
            document.getElementById('temp').value = 'Invalid';
            alert("Verification Code  is Invalid");
		    return false;
		}
	}
	else {
		document.getElementById('captchadiv').innerHTML = '<img src="indicator.gif" width="16" height="16">';
	}
}

function displaycheck1() {
	if(http.readyState == 4) {
		document.getElementById('captchadiv').innerHTML = '';
		var showcheck = http.responseText; 
		if(showcheck == '1') { 
            document.getElementById('temp').value = 'Valid';
            document.forgot_password_frm.method="post";
	    	document.forgot_password_frm.action="forgot_password.php?act=noQ";
	    	document.forgot_password_frm.submit();
		}
		if(showcheck == '0') { 
            document.getElementById('temp').value = 'Invalid';
            alert("Verification Code  is Invalid");
		    return false;
		}
	}
	else {
		document.getElementById('captchadiv').innerHTML = '<img src="indicator.gif" width="16" height="16">';
	}
}

function displaycheck2() {
	if(http.readyState == 4) {
		document.getElementById('captchadiv').innerHTML = '';
		var showcheck = http.responseText; 
		if(showcheck == '1') {
            document.getElementById('temp').value = 'Valid';
            document.forgot_password_frm.method="post";
    		document.forgot_password_frm.action="forgot_username.php?act=noQ";
    		document.forgot_password_frm.submit();
		}
		if(showcheck == '0') {
            document.getElementById('temp').value = 'Invalid';
            alert("Verification Code  is Invalid");
		    return false;
		}
	}
	else {
		document.getElementById('captchadiv').innerHTML = '<img src="indicator.gif" width="16" height="16">';
	}
}
function displaycheck3() {
	if(http.readyState == 4) {
		document.getElementById('captchadiv').innerHTML = '';
		var showcheck = http.responseText; 
		if(showcheck == '1') { 
            document.getElementById('temp').value = 'Valid';
            document.webform.submit();
		}
		if(showcheck == '0') { 
            document.getElementById('temp').value = 'Invalid';
            alert("Verification Code is Invalid");
		    return false;
		}
	}
	else {
		document.getElementById('captchadiv').innerHTML = '<img src="indicator.gif" width="16" height="16">';
	}
}