<!--//
	
  /************************************************************\
  *
  *    PHP Captcha Email Protection Copyright 2007 Derek Harvey
  *		 www.lotsofcode.com
  *
  *    This file is part of PHP Captcha Email Protection.
  *
  *    PHP Captcha Email Protection is free software; you can redistribute it and/or modify
  *    it under the terms of the GNU General Public License as published by
  *    the Free Software Foundation; either version 2 of the License, or
  *    (at your option) any later version.
  *
  *    PHP Captcha Email Protection is distributed in the hope that it will be useful,
  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *    GNU General Public License for more details.
  *
  *    You should have received a copy of the GNU General Public License
  *    along with PHP Captcha Email Protection; if not, write to the Free Software
  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  *
  \************************************************************/
	
	var useMailtoLink = true; // Should the end result be plain text or a mailto link?
	
  // The Element
  var theElement = false, theElementVerify = false, j, newElement;
  
  // onLoad the function
  window.onload = function()
  {
      // Get all the links on the page
      theElement = document.getElementsByTagName("a");
      if (theElement)
      {
        // Loop through all the anchors
        for (j = 0; j < theElement.length; j++)
        {
          if (theElement[j].className == 'email-protection')
          {
            theElement[j].id = j;
            theElement[j].onclick = showBox;
          }
        }
      }
  }
  
  // Search For a DIV with the specific class name
  function classNameExists(objectClassName)
  {
      // Get all the DIV's
      var searchElements = document.getElementsByTagName("div");
      for (c = 0; c < searchElements.length; c++)
      {
        // If we find a class 
        if (searchElements[c].className == objectClassName)
          return true;
      }
      // If not then return bool
      return false;
  }
  
  function showBox()
  {
      // Look for the elements in the document.
      if (!classNameExists('captcha-verify'))
      {
        // Get the element we want to use
        wrapperElement = document.getElementById("email-protection-container");
        
        // Create the element
        newElement = document.createElement("div");
        
        // Specify the class name of the element, we decided "captcha-verify" was good enough ;-)
        newElement.className = 'captcha-verify';
        
        // Store the HTML value for the new element
        theHTML  = '<form method="post">';
        theHTML += '<img id="freecap" src="./freecap/freecap.php">';
        theHTML += '<div class="word"><span class="text">Word Above</span> <input type="text" name="word" id="word"><input type="button" name="verify" id="btn_verify" value="verify"></div>';
        theHTML += '</form>';
        
        // Set the HTML value of the soon to be element
        newElement.innerHTML = theHTML;
        
        // Display the elements
        wrapperElement.appendChild(newElement);
        
        // Focus on the text field
        document.getElementById("word").focus();
        
        // Handle Verification Process
        theElementVerify = document.getElementById("btn_verify");
        if (theElementVerify)
        {
          // Add the event handler
          theElementVerify.onclick = verifyWord;
        }
      }
  }
  
  // Verify we have a word present. If not show the message, if so then send the HTTPRequest.
  function verifyWord()
  {
    // Get the value of the field
    var theWord = document.getElementById("word").value;
    if (!theWord)
      alert("Please enter the word into the box below");
    else
    {
      // Send the request
      var checkCaptcha = new sendHTTPRequest('./captcha-email_verify.php', '&word=' + theWord, displayResults);
      checkCaptcha.sendPostData();
    }
  }
  
  function sendHTTPRequest(theURL, sendString, callbackFunction)
	{
    // Create the Object
		var thisRequestObject;
		
		// Initiate the request and specify the ready state and callback function wrapper.
		thisRequestObject = initiateRequest();
		thisRequestObject.onreadystatechange = processRequest;
		
		function initiateRequest()
		{
			if (window.XMLHttpRequest)
				return new XMLHttpRequest();
			else if (window.ActiveXObject)
				return new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		function processRequest()
		{
			if (thisRequestObject.readyState == 4)
			{
				if (thisRequestObject.status == 200)
				{
					if (callbackFunction)
						callbackFunction(thisRequestObject, sendString);
				}
				else
					alert("There was an error: (" + thisRequestObject.status + ") " + thisRequestObject.statusText);
			}
		}
		
		this.sendPostData = function(stringToSend)
		{
			if (theURL)
			{
				thisRequestObject.open("POST", theURL, true);
				thisRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				thisRequestObject.send(sendString);
			}
		}
	}
  
  // Handle the input
  function displayResults(data)
  {
    var freecapText = data.responseText;
    if (freecapText < 0)
    {
      alert("Sorry, the word you entered was incorrect.");
      theWord = document.getElementById("word");
      theWord.value = '';
      theWord.focus();
      new_freecap();
    }
    else
    {
      if (useMailtoLink)
        wrapperElement.innerHTML = '<a href="mailto:' + freecapText + '">' + freecapText + '</a>';
      else
        wrapperElement.innerHTML = freecapText;
    }
  }
  
  // @new_frecap
  // Written by Howard Yeend @ puremango.co.uk
  function new_freecap()
  {
    if (document.getElementById)
    {
      thesrc = document.getElementById("freecap").src;
      thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
      document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
    }
    else
    {
      alert("Sorry, cannot autoreload freeCap image\nSubmit the form and a new freeCap will be loaded");
    }
  }
	
//-->

