<!--//
	
  /************************************************************\
  *
  *    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("span");
      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("span");
        
        // 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 name="captcha-form" id="captcha-form" method="post">';
        theHTML += '<br /><img id="freecap" src="/captcha/freecap/freecap.php" /><br />';
        theHTML += '<span class="word"><span class="text">Wpisz powyższe słowo (<a href="http://pl.wikipedia.org/wiki/CAPTCHA" target="_blank">co to?</a>)</span><br />';
	theHTML += '<input type="text" name="word" id="word" onKeyPress="return submitenter(this,event)" /><br />';
	theHTML += '<input type="button" name="verify" id="btn_verify" value="pokaż" /></span>';
        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("Proszę przepisać słowo z obrazka.");
    else
    {
      // Send the request
      var checkCaptcha = new sendHTTPRequest('/captcha/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("Wystąpił błąd: (" + 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("Niestety wpisane słowo jest niepoprawne.");
      theWord = document.getElementById("word");
      theWord.value = '';
      theWord.focus();
      new_freecap();
    }
    else
    {
      if (useMailtoLink)
        wrapperElement.innerHTML = 'email: <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("Niestety nie można przeładować obrazka automatycznie.\nKliknij przycisk formularza aby załadować nowy obrazek.");
    }
  }

  function submitenter(myfield,e)
  {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    
    if (keycode == 13)
       {
       verifyWord();
       return false;
       }
    else
       return true;
  }
  
//-->

