﻿var xmlHttp;           //Main AJAX object.
var entryPoint;        //Generic place to attach html
var secureText;        //Security Code Text

///Get Persistent AJAX Objects

function checkSecurityCode(func)
{
 getResponse(new getSecureResponse(func));
}


function getSecureResponse(func)
{
 this.URL="BotBlock.aspx";
 this.Method="Get";
 this.QueryString=function()
 {
  return "secureText="+secureText.value+"&key="+randomString();
 }
 
 this.Handler=function()
 {
  if(xmlHttp.readyState==4)
  {
   if(xmlHttp.responseText == undefined)
   {
    alert("A problem finding " + URL + ".");
    return;
   }
   //alert(xmlHttp.responseText.length);
   if(xmlHttp.responseText.length > 0) entryPoint.innerHTML=xmlHttp.responseText;
   else
   {
    entryPoint.innerHTML=""; 
    if (func != undefined && func != null)  func();
   }
  }
 }
}

//Basic random string generator
function randomString() 
{
 var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
 var string_length = 25;
 var randomstring = '';
 for (var i=0; i<string_length; i++) 
 {
  var rnum = Math.floor(Math.random() * chars.length);
  randomstring += chars.substring(rnum,rnum+1);
 }
 return randomstring;
}


///Gets all responses for each AJAX Object
function getResponse(responseObject)
{
  var URL;
  var Method;

  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }


    xmlHttp.onreadystatechange=responseObject.Handler;
    URL=responseObject.URL
    Method=responseObject.Method;

    if(Method.toUpperCase() == "GET")
    { 
     if(responseObject.QueryString != undefined)
     { 
      if(responseObject.QueryString() != undefined)
      {
       URL+="?"+responseObject.QueryString();
      }
     }
    } 

    xmlHttp.open(Method,URL,true);

    if(Method.toUpperCase() == "GET") 
    {
     xmlHttp.send(null);
    }
    else
    {
     if(Method.toUpperCase() == "POST")
     {

      if(responseObject.QueryString != undefined)
      { 
       if(responseObject.QueryString() != undefined)
       {
        var parameters=responseObject.QueryString();
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", parameters.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.send(parameters);
       }
      }
     }
    }
}
