//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();

function makeXmlHttpRequest()
{
    var xmlhttp = false;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(E)
            {
                window.location.reload('uploadFile.aspx');
            }
        }
    }
    return xmlhttp;
}


function validateTool()
{
    if(document.getElementById("txtSecurityCode") && document.getElementById("txtSecurityCode").value == '')
    {
	    alert("Please enter Security Code");
   		document.getElementById("txtSecurityCode").focus();
   		return false;
	}
	else if(document.getElementById("txtSecurityCode") &&  document.getElementById("txtSecurityCode").value != '')
    {
	         
			var url = './validate-captcha.php';
			//Set up the parameters of our AJAX call
			var postStr = document.getElementById("txtSecurityCode").name + "=" + encodeURIComponent( document.getElementById("txtSecurityCode").value );
			//Call the function that initiate the AJAX request
			makeToolRequest(url, postStr);
	}
	else
	{		
		document.getElementById('frmTool').submit();
		return true;
	}
}

//Initiate the AJAX request
function makeToolRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updateToolPage; 

   //Add HTTP headers to the request
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updateToolPage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {	 
	 if(receiveReq.responseText == 'Invalid security code')
	 {
		 //Set the content of the DIV element with the response text
		   document.getElementById('security_code').innerHTML = receiveReq.responseText;
		   //Get a reference to CAPTCHA image
		   img = document.getElementById('imgCaptcha'); 
		   //Change the image
		   img.src = './imageblob.php?' + Math.random();
	 }
	 else if(receiveReq.responseText == '<h1>Test successful!</h1>')
	 {
		 //Set the content of the DIV element with the response text
		 //document.getElementById('security_code').innerHTML = receiveReq.responseText;
		 document.getElementById('frmTool').submit();
		 return true;
	 }
   
 }
 
}