// modified from: www.phppatterns.com/index.php/article/articleview/82/1/2/
//      by Harry Fuecks

//Start phpRequest Object
function phpRequestInit(url)
{
   //Set some default variables
   this.parms = new Array();
   this.parmsIndex = 0;

   //Set the server url
   this.url = url;

   //Add two methods
   this.execute = phpRequestExecute;
   this.add = phpParamRequestAdd;

   //Default to returning text
   this.returnxml=false;
}

function phpParamRequestAdd(name,value)
{
   //Add a new pair object to the params
   this.parms[this.parmsIndex] = new Pair(name,value);
   this.parmsIndex++;
}

function phpRequestExecute(reqtype)
{
   //Set the server to a local variable
   var targetURL = this.url;

   var xmlhttp=false;

   /*@cc_on @*/
   /*@if (@_jscript_version >= 5)
        // JScript gives us Conditional compilation, we can cope with old IE versions.
        // and security blocked creation of the objects.
        try
        {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch(e)
        {
                try
                {
                        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
                }
                catch(E)
                {
                        xmlhttp = false;
                }
        }
        @end @*/
   if(!xmlhttp && typeof XMLHttpRequest!='undefined')
   {
      try
      {
         xmlhttp = new XMLHttpRequest();
      }
      catch (e)
      {
         alert('Error connecting to server!');
         return;
      }
   }
   //Make the connection and send our data
   try
   {
      var txt = "?1";
      for(var i in this.parms)
      {
         txt = txt+'&'+this.parms[i].name+'='+escape(this.parms[i].value);
      }

      //Two options here
      if(reqtype=='GET')
      {
         //GET REQUEST
         xmlhttp.open("GET", targetURL+txt, false, null, null);
         xmlhttp.send('');
      }
      else //default
      {
         //POST REQUEST EXAMPLE
         xmlhttp.open("POST", targetURL, false, null, null);
         xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         xmlhttp.setRequestHeader("Content-Length", txt.length);
         xmlhttp.send(txt);
      }
   }
   catch (e)
   {
      alert('An error has occured calling the external site: '+e);
      return false;
   }

   //Make sure we received a valid response
   switch(xmlhttp.readyState)
   {
      case 1,2,3:
      {
         alert('Server not ready! : '+xmlhttp.status);
         return false;
         break;
      }
      case 4:
      {
         if(xmlhttp.status !=200)
         {
            alert('The server responded with a bad status code: '+xmlhttp.status);
            return false;
         }
         else if(this.returnxml)
         {
            var response = xmlhttp.responseXML;
         }
         else
         {
            var response = xmlhttp.responseText;
         }
         break;
      }
   }
   return response;
}

function phpRequestReturnXML(state)
{
        this.returnxml=state;
}

//Utility Pair class
function Pair(name,value)
{
   this.name = name;
   this.value = value;
}