//2008-01-08 added sync/async
var async;
var xmlhttp;
var response;
var recipient;
var Server = new Array();
//===========================Usage examples
//==============================POST method - async
//msg = 'cmd=postad';
//msg += '&headline='+encode(document.adform.headline.value);
//msg += '&country='+document.adform.country.value;
//msg += '&state='+document.adform.state.value;
//msg += '&city='+document.adform.city.value;
//msg += '&body='+encode(document.adform.body.value);
//msg += '&category='+document.adform.category.value;
//msg += '&email='+encode(document.adform.email.value);
//sendHttp("POST",'newad.php',msg,showres);
//}
//function showres()
//{
//alert (response);
//notice.innerHTML=decode(Server["body"]);
//}
//===========================================
function sendHttp(method,action,d,caller)
{
    if(caller!="")
    {
    recipient = caller;
    async=true;   //default async
    }
    else
    {
    async=false;
    }
    xmlhttp=null;
// code for Mozilla, etc.
    if (window.XMLHttpRequest)
    {
    xmlhttp=new XMLHttpRequest();
    }
    // code for IE
    else if (window.ActiveXObject)
    {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp!=null)
    {
      if(async)
      {
      xmlhttp.onreadystatechange=state_Change;
      }
    xmlhttp.open(method,action,async);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //alert("d="+d);  
    xmlhttp.send(d);
    }
    else
    {
    alert("Your browser does not support XMLHTTP.");
    }
//alert("async="+async);
    if (!async) {
        response = xmlhttp.responseText;
        parse();
    }
}

function state_Change()
{
if (xmlhttp.readyState==4)
  {
  response = xmlhttp.responseText;
//alert(response); 
//alert("async="+async); 
  parse();
  recipient();
  //httpReceive(xmlhttp.responseText)
  }
}
//replaces any non alpha numeric with %<hex> for transport over http
//this makes the '&' and '=' unique for parameter definitions
function encode(s) {
es = "";
    if(s != "")
    {
        for(var i=0;i<s.length;i++)
        {
            cc = s.charCodeAt(i);
            if(cc >= 48 && cc <= 57 || cc >= 64 && cc <= 90 || cc >= 97 && cc <= 122)
            {
            es += s.charAt(i);
            }
            else
            {
            es += "%"
            if (s.charCodeAt(i)<16){es += "0";}
            es += d2h(s.charCodeAt(i));
            }
        }
    }        
    return es;        
}

//replaces %<hex> with the character represented by <hex>
function decode(s)
{
es = "";
    try
    {
        if(s != "")
        {
    //alert(s);    
            for (i=0;i<s.length;i++)
            {
            cc = s.charAt(i);
                if(cc == "%")
                {
                es += unescape(s.substr(i,3));
                i = i +2;
                }
                else{es += cc;}
            }
        }
    }
    catch(err)
    {
    es = "";
    }
return es; 
}

//hex <-> dec conversion
function d2h(d) {return d.toString(16);}
function h2d(h) {return parseInt(h,16);}
 
//Create the global variables Server[status],Server[msg], etc.
//that return the values from the server
function parse()
{
    if(response.charCodeAt(0) == 0xFEFF){response=response.substr(1);}
    pairs = response.split("&");
        for (i = 0;i < pairs.length;i++)
        {
        pair = pairs[i].split("=",2);
        Server[pair[0]] = pair[1];
        }
    }
    //create the p1=x&p2=y& .....
//input is {p1:x,p2:x,etc.}
function posting(p) {
    var q = "";
    var sep = ""
    for (var k in p) {
        q += sep + k + "=" + p[k];
        sep = "&";
    }
    return q;
}

