//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();

//Initiate the AJAX request
function makeRequest(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 = updatePage; 

   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 updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
   //Set the content of the DIV element with the response text
   document.getElementById('result').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = '/EWFormUtility/create_image.php?' + Math.random();
 }
}

//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = '/EWFormUtility/captcha.php';
 //Set up the parameters of our AJAX call
 var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}


/**
 *width=180&height=60&letter_count=5&min_size=35&max_size=45&noise=500&noiselength=9&bcolor=cccced&border=000000\"
 */

/**
 *A convenience method to set a Captcha challenge inside a form.
 *
 *Note this currently adds a row of a table. A better solution is in order.
 *
 * place  a Captcha object in a form within a page. The default arguments are set here
 * $formname is not optional, all other arguments are optional. ---required for the ajax implementation-- which is removed here.
 * The advantage of using this method is an easy way to set the variables for the image.
 * @input name="txtCaptcha"
 */
function printCatcha($width, $height, $letter_count, $min_size, $max_size, $noise, $noiselength, $bcolor) {
	if(typeof $width === undefined || $width === undefined) $width = 180;
	if(typeof $height === undefined ||$height === undefined) $height = 60;
	if(typeof $letter_count === undefined || $letter_count === undefined) $letter_count = 5;
	if(typeof $min_size === undefined ||$min_size === undefined) $min_size = 35;
	if(typeof $max_size === undefined ||$max_size === undefined) $max_size = 45;
	if(typeof $noise === undefined 	|| $noise === undefined ) $noise = 300;
	if(typeof $noiselength === undefined || $noiselength === undefined) $noiselength = 5;
	if(typeof $bcolor === undefined ||$bcolor === undefined) $bcolor = "ffffff";

	document.write("<img id=\"imgCaptcha\" src=\"/EWFormUtility/create_image.php?");
	document.write("width="  + $width + "&");
	document.write("height=" + $height + "&");
	document.write("letter_count=" + $letter_count + "&");
	document.write("min_size=" + $min_size + "&");
	document.write("max_size=" + $max_size + "&");
	document.write("noise=" + $noise + "&");
	document.write("noiselength=" + $noiselength + "&");
	document.write("bcolor=" + $bcolor + "&")
	document.write("border=000000\" ");
	document.write("width='" + $width + "' ");
	document.write("height='" + $height + "' ");
	document.write("id=\"captchaimg\" ");
	document.write("alt=\"security code\" ");
	document.write("border=\"0\" /><br />");
	document.write("Enter Security Code <br />");
	document.write ("<input id=\"txtCaptcha\" type=\"text\" name=\"txtCaptcha\" value=\"\" maxlength=\"10\" size=\"32\" />  <br />");
	document.write("<br /><input id=\"btnCaptcha\" type=\"button\" value=\"Submit security code\" name=\"btnCaptcha\" onclick=\"getParam(document.frmCaptcha)\" />");
}



