var request = null;
/* Initialize a Request object that is already constructed */
function initReq(reqType,url,bool,respHandle){
//	alert("hello-1");
    try{
    //alert("hello0");
    	/* Specify the function that will handle the HTTP response */
        request.onreadystatechange=respHandle;
        //alert("hello2");
		
        request.open(reqType,url,bool);
        //alert("hello3");
        //if the reqType parameter is POST, then the
        //5th argument to the function is the POSTed data
        if(reqType.toLowerCase() == "post") {
        	//alert("hello4");
            request.setRequestHeader("Content-Type",
           	 	"application/x-www-form-urlencoded; charset=UTF-8");
/*                        "text/plain; charset=UTF-8");*/
            request.send(queryString);
        	return true;            
        }   else {
        //alert("hello4b");
            request.send(null);
                    //alert("hello4c");
        return true;
        }

    } catch (errv) {
        alert(
                "The application cannot contact "+
                "the server at the moment. "+
                "Please try again in a few seconds.\n"+
                "Error detail: "+errv.message);
        return false;
    }
}
/* 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.
  respHandle: The name of the function that will handle the response.
  Any fifth parameters represented as arguments[4] are the data a
  POST request is designed to send. */
function httpRequest(reqType,url,asynch,respHandle){
    //Mozilla-based browsers
//    alert("hello5");
    if(window.XMLHttpRequest){
//    alert("hello6");
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject){
  //  alert("hello7");
		request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
    //    alert("hello8");
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    //Very unlikely, but we test for a null request
    //if neither ActiveXObject was initialized
    if(request)  {
    //alert("hello9");
        //if the reqType parameter is POST, then the
        //5th argument to the function is the POSTed data
        if(reqType.toLowerCase() != "post") {
    //    alert("hello10");
            return (initReq(reqType,url,asynch,respHandle));
        }  else {
    //    alert("hello11");
            //the POSTed data
            var args = arguments[4];
            if(args != null && args.length > 0){
    //        alert("hello12");
                return initReq(reqType,url,asynch,respHandle,args);
            }
			else
			{
	//		alert("hello13");
			   return initReq(reqType,url,asynch,respHandle);
			}
        }
    }  else {
        alert("Your browser does not permit the use of all "+
              "of this application's features!");
              return false;
        }
}

function handleResponse(){
    try{
        if(request.readyState == 4){
            if(request.status == 200){
                /*Check the return value if true*/
                var newFileName = request.responseText;
                //alert(newFileName);
                try{
                    if(!newFileName) { 
                    	throw new Error("returnedFalse");
                    }
					else{
						var enlarged=parent.document.getElementById("enlargedImage");
						enlarged.src=enlarged.src.replace(/CIMG.*jpg/,newFileName);	
						centerImage();					
						return true;
					}                    
                } catch (err) {
                    alert(err);
                    //errorAction();
                    return false;                   
                }
            } 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. " +request.status);
                return false;
            }
        }//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);
		return false;
    }
}
