var request = "";
var queryString = "";
var url= ""

function htmlEncodeString (inputString)
{
  return escape(inputString);
}

function setQueryString(){
 //initialize the top-level variable; also reset the variable to cover when
 //the user clicks multiple times
 queryString="";
 var frm = document.newsletter;
 var numberElements = frm.elements.length;
 for(var i = 0; i < numberElements; i++) {
 if(i < numberElements-1) {
 queryString += frm.elements[i].name+"="+htmlEncodeString(frm.elements[i].value)+"&";
 } else {
 queryString += frm.elements[i].name+"="+frm.elements[i].value;
 }
 }
}

 //will hold the POSTed data
function sendData(urltxt){
 url=urltxt;
 httpRequest("POST",url,true);
}

/* Initialize a Request object that is already constructed
 reqType: The HTTP request type such as "GET" or "POST." 
 url: The URL of the server program.
 isAsynch: Whether to send the request asynchronously or not. */
 function initReq(reqType,url,isAsynch){
 /* Specify the function that will handle the HTTP response */
 request.onreadystatechange=handleResponse;
 request.open(reqType,url,isAsynch);
 /* set the Content-Type header for a POST request */
 request.setRequestHeader("Content-Type",
 "application/x-www-form-urlencoded; charset=UTF-8");
 request.send(queryString);
}

/* Wrapper function for constructing a Request object.
 Parameters:
 reqType: The HTTP request type such as GET or POST.
 url: The URL of the server program.
 asynch: Whether to send the request asynchronously or not. */

function httpRequest(reqType,url,asynch){
 //Mozilla-based browsers
 if(window.XMLHttpRequest){
 request = new XMLHttpRequest();
 initReq(reqType,url,asynch);
 } else if (window.ActiveXObject){
 request=new ActiveXObject("Msxml2.XMLHTTP");
 if (! request){
 request=new ActiveXObject("Microsoft.XMLHTTP");
 }
 if(request){
 initReq(reqType,url,asynch);
 /* Unlikely to branch here, as IE uses will be able to use either 
 one of the constructors*/
 } else {
 alert("Your browser does not permit the use of all "+
 "of this application's features!");}
 } else {
 alert("Your browser does not permit the use of all "+
 "of this application's features!");}
}
//event handler for XMLHttpRequest
function handleResponse(){
 if(request.readyState == 4){
 if(request.status == 200){
	 result = request.responseText;
  //document.getElementById('savetxt').innerHTML = result; 
  alert(result);
  document.newsletter.email.select();
  
 } else {
 alert("A problem occurred with communicating between "+
 "the XMLHttpRequest object and the server program.");
 }
 }//end outer if
}
