var request = null;
var queryString;   //will hold the POSTed data

function setQueryString(formobj){
    queryString="";
    //var frm = document.forms[0];
    var frm = formobj;
    var numberElements =  frm.elements.length;
    for(var i = 0; i < numberElements; i++){
            if(i < numberElements-1)  {
              queryString += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value)+"&";
            }else{
              queryString += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value);
            }
            //queryString += "\n";

    }
    //alert(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,action){
    //Mozilla-based browsers
    //if(request==null){
      //alert('create new request object');
      if(window.XMLHttpRequest){
          request = new XMLHttpRequest();
      }else if(window.ActiveXObject){
          request=new ActiveXObject("Msxml2.XMLHTTP");
          if(!request){
              request=new ActiveXObject("Microsoft.XMLHTTP");
          }
      }
    //}
    if(request){
      //if the reqType parameter is POST, then the
      //5th argument to the function is the POSTed data
      if(reqType.toLowerCase() != "post") {
        initReq(reqType,url,asynch,action);
      }else{
        //the POSTed data
        var args = arguments[4];
        if(args != null && args.length > 0){
            initReq(reqType,url,asynch,action,args);
        }
      }
    }else{
        alert("Your browser does not permit the use of all "+
              "of this application's features!");
    }
}

/* Initialize a Request object that is already constructed */
function initReq(reqType,url,bool,action){
    try{
        /* Specify the function that will handle the HTTP response */
        request.onreadystatechange=action;
        request.open(reqType,url,bool);
        //if the reqType parameter is POST, then the
        //5th argument to the function is the POSTed data
        if(reqType.toLowerCase() == "post") {
            request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
            request.send(arguments[4]);
        }else{
            request.send(null);
        }
    }catch(errv){
        alert(
                "The application cannot contact "+
                "the server at the moment. "+
                "Please try again in a few seconds.\n"+
                "Error detail: "+errv.message);
    }
}
//event handler for XMLHttpRequest
function Response_ChangeFormElement(){
    try{
        if(request.readyState == 4){
            if(request.status == 200){
                var resp =  request.responseText;
                if(resp != null){
                    //Generate new form elements
                    generateFormElements(resp)
                }
            } else {
                //request.status is 503
                //if the application isn't available;
                //500 if the application has a bug
                alert("A problem occurred with communicating between"+" the XMLHttpRequest object and the server program.");
            }
        }//end outer if
    }catch (err){
        alert("It does not appear that the server "+
              "is available for this application. Please"+
              " try again very soon. \nError: "+err.message);
    }
}

function Response_SendEmail(){
    try{
        if(request.readyState == 4){
            if(request.status == 200){
                var resp =  request.responseText;
                if(resp != null){
                  //Generate new form elements
                  document.getElementById("message").value="";
                  document.getElementById("message").style.color="red";
                  //document.getElementById("message").style.textAlign="center";
                  document.getElementById("message").value=resp;
                }
            }else{
                alert("A problem occurred with communicating between"+" the XMLHttpRequest object and the server program.");
            }
        }
    }catch(err){
        alert("It does not appear that the server "+
              "is available for this application. Please"+
              " try again very soon. \nError: "+err.message);
    }
}

function Response_ListRecord(){
    try{
        if(request.readyState == 4){
            if(request.status == 200){
                var resp =  request.responseText;
                if(resp != null){
                  //Generate new form elements
                  document.getElementById("display").innerHTML="";
                  //document.getElementById("display").style.color="red";
                  //document.getElementById("message").style.textAlign="center";
                  document.getElementById("display").innerHTML=resp;
                }
            }else{
                alert("A problem occurred with communicating between"+" the XMLHttpRequest object and the server program.");
            }
        }
    }catch(err){
        alert("It does not appear that the server "+
              "is available for this application. Please"+
              " try again very soon. \nError: "+err.message);
    }

}

function Response_SwitchRecord(){
    try{
        if(request.readyState == 4){
            if(request.status == 200){
                var resp =  request.responseText;
                if(resp != null){
                  document.getElementById("display").innerHTML="";
                  document.getElementById("display").innerHTML=resp;
                }
            }else{
                alert("A problem occurred with communicating between"+" the XMLHttpRequest object and the server program.");
            }
        }
    }catch(err){
        alert("It does not appear that the server "+
              "is available for this application. Please"+
              " try again very soon. \nError: "+err.message);
    }

}

function Response_DeleteRecord(){
    try{
        if(request.readyState == 4){
            if(request.status == 200){
                var resp =  request.responseText;
                if(resp != null){
                  document.getElementById("display").innerHTML="";
                  document.getElementById("display").innerHTML=resp;
                }
            }else{
                alert("A problem occurred with communicating between"+" the XMLHttpRequest object and the server program.");
            }
        }
    }catch(err){
        alert("It does not appear that the server "+
              "is available for this application. Please"+
              " try again very soon. \nError: "+err.message);
    }

}


