// 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()
{
    document.getElementById('invalidcaptcha').style.display='none';  
    document.getElementById('validcaptcha').style.display='none';  
	var url = '/shared/captcha/image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback)
{   


    var browser     = '';
    var version     = '';
    var entrance    = '';
    var cond        = '';
    // BROWSER?
    if (browser == ''){
    if (navigator.appName.indexOf('Microsoft') != -1)
        browser = 'IE'
    else if (navigator.appName.indexOf('Netscape') != -1)
        browser = 'Netscape'
    else browser = 'IE';
    }
    if (version == ''){
        version= navigator.appVersion;
        paren = version.indexOf('(');
        whole_version = navigator.appVersion.substring(0,paren-1);
        version         = parseInt(whole_version);
    }  
    if (browser=='IE')
        method='POST';
    else
        method='GET';
	sess.open(method, '/shared/captcha/newsession.php', true);
	sess.send(null);
	http.open(method, 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 = '/shared/captcha/process.php?captcha=' + submission;
	docheck(url, displaycheck);
    if (document.getElementById('validcaptcha').style.display=='')
    {
        return true;
    }
    else
    {
        return false;
    }
    
    
}

function docheck(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;        
	http.send(null);
}

function displaycheck()
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == '1')
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			//document.getElementById('captcha').style.background = '#bcffbf';
            document.getElementById('validcaptcha').style.display=''; 
            document.getElementById('invalidcaptcha').style.display='none'; 
            return true;
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			//document.getElementById('captcha').style.background = '#ffbcbc';
            document.getElementById('invalidcaptcha').style.display='';
            document.getElementById('validcaptcha').style.display='none';        
			//document.getElementById('captcha').value = '';
           return false; 

		}
	}
}