/* <![CDATA[ */
// Function to allow one JavaScript file to be included by another.
// Copyright (C) 2006-08 www.cryer.co.uk
if(typeof(path_to_captcha) == 'undefined')
{
    var path_to_captcha = 'captcha';
}

function IncludeJavaScript(jsFile)
{
    document.write('<script type="text/javascript" src="'+ jsFile + '"></scr' + 'ipt>'); 
}
   
 
IncludeJavaScript(path_to_captcha+'/yui/build/yahoo/yahoo.js');
IncludeJavaScript(path_to_captcha+'/yui/build/event/event.js');
IncludeJavaScript(path_to_captcha+'/yui/build/connection/connection.js');
   
   
   
   function drawWidget()
   {
       var widget = document.getElementById('captcha_widget');
       var widget_code = "";
       
       // Edit this to change the look //
       widget_code += '<table cellpadding=5 cellspacing=0 bgcolor="#383737" class="greytext">';
       widget_code += '<tr>';
       widget_code += '<td colspan="2" class="greytext"><div id="captcha_error"><b>Image Verification</b></div></td>';
       widget_code += '</tr>';
       widget_code += '<tr>';
       widget_code += '<td style="padding: 2px;" width="10"><img src="'+path_to_captcha+'/captchac_code.php" id="captcha"></td>';
       widget_code += '<td valign="top" class="greytext">Please enter the text from the image &nbsp; <br><input type="text" id="Turing" name="Turing" value="" maxlength="100" size="10">';
       widget_code += '[ <a href="#captcha_place" onclick=" document.getElementById(\'captcha\').src = document.getElementById(\'captcha\').src + \'?\' + (new Date()).getMilliseconds()">Refresh Image</a> ] [ <a href="'+path_to_captcha+'/whatisturing.html" onClick="window.open(\''+path_to_captcha+'/whatisturing.html\',\'_blank\',\'width=400, height=400, left=\' + (screen.width-450) + \', top=100\');return false;">What\'s This?</a> ]';
       widget_code += '</td>';
       widget_code += '</tr>';
       widget_code += '</table>';
       widget_code += '<a name="captcha_place"/>';
       //widget_code += '<form><input type="button" value="Send a POST Request" onClick="checkCaptcha(document.getElementById(\'Turing\').value);"></form>';
       
       widget.innerHTML = widget_code;
   }
   
   // captcha functions below //
   
   var sUrl = path_to_captcha+"/captcha_checker.php";
   
   var handleSuccess = function(o){
   	//YAHOO.log("The success handler was called.  tId: " + o.tId + ".", "info", "example");
   	var div = document.getElementById('captcha_error');
   	if(o.responseText !== undefined){
    
   	    if(o.responseText == "false")
   	    {
   	        
   	        if(div)
   	        {
   	            //alert(o.responseText);
   	            div.innerHTML = "Sorry, you didn't type the right code verification. Please try again.";
   	        } else { alert("There is no captcha_error div tag to tell you that the code is invalid."); }
   	    
   	    }
   	    else if(o.responseText == "true")
   	    {
   	        //alert("Submitting");
   	        eval(form_callback+"();");
   	    } 
   	    else {
   	        //alert(o.responseText);
   	        alert("There was no good response from the captcha script. It errored out most likely.");
   	        
   	    }
   	    // Good stuff to leave in here for reference
   	    //alert(document.getElementById('captcha_error').innerHTML);
   		//div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
   		//div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
   		//div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
   		//div.innerHTML += "<li>HTTP headers received: <ul>" + o.getAllResponseHeaders + "</ul></li>";
   		//div.innerHTML += "<li>PHP response: " + o.responseText + "</li>";
   		//div.innerHTML += "<li>Argument object: Array ([0] => " + o.argument[0] +
   		//				 " [1] => " + o.argument[1] + " )</li>";
   	}
   };

   var handleFailure = function(o){
    alert("There was an error talking to the captcha verification script.");
   };

   var callback =
   {
     success:handleSuccess,
     failure:handleFailure,
     argument:['foo','bar']
   };
   
   
   
   function checkCaptcha(captcha_code){
    //alert("Entering checkCaptcha() function");
    var postData = "Turing="+captcha_code;   
   	//alert("postData: " + postData);
   	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
   	//alert("asyncRequest Result: " + request);
    //eval(form_callback+"();");
   	//YAHOO.log("Initiating request; tId: " + request.tId + ".", "info", "example");

   }
   
   /* ]]> */