document.write("<form enctype='multipart/form-data' method='post' name='myferry'><input type='hidden' name='id'><input type='hidden' name='request'><input type='hidden' name='obj'><input type='hidden' name='opt'><input type='hidden' name='item'></form>");

/**********************
 * get a xmlhttp object
 **********************/
function getXMLHTTP()
{

    if ((typeof XMLHttpRequest) != "undefined") {
        /* XMLHTTPRequest present, use that */
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        /*  there are several versions of IE's Active X control, use the most recent one available */
        var xmlVersions = ["MSXML2.XMLHttp.5.0",
                           "MSXML2.XMLHttp.4.0",
                           "MSXML2.XMLHttp.3.0",
                           "MSXML2.XMLHttp",
                           "Microsoft.XMLHTTP"];

        for (var x = 0; x < xmlVersions.length; x++) {
            try {
                var xmlHTTP = new ActiveXObject(xmlVersions[x]);
                return xmlHTTP;      
            } catch (e) {
                //continue looping
            }
        }
    }

    /* if none of that worked, return false, to indicate failure */
    return false;
}


/********************
 * myAjax set up the xresponse for the xmlhttp call
 ********************/

var xmlhttpx = null;
function myAjax(server,postval,rhandle){

//alert('myAjax '+server+'\n'+ postval);
//window.status = 'url length is '+ postval.length;

   xmlhttpx = getXMLHTTP();
   
   if(xmlhttpx){
     var async;
     if(document.recalc){
          async = false;
     } else {
          async = true;
     }
     
     async = true;
     
     try{
     	xtimeoutId = window.setTimeout(xtimeout,3000);
     	xmlhttpx.open("POST",server,async);
     	xmlhttpx.onreadystatechange = mycallback;
     	xmlhttpx.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
     	xmlhttpx.send(postval);
     } catch(e){
       	//alert("Error in calling xmlhttpx " + e.message);

	window.status = "Error in calling xmlhttpx " + e.message;
  	xmlhttpx.abort();
  	xmlhttpx = null;
  	xtimeout();
  	//myAjax(server,postval,rhandle); 
  	alert('Unable to complete this request.  Please try again.');

     }
     
   } else {
     alert("Unable to create a xmlhttp connection object");
   }
   
   function mycallback()
   {
   	window.status = "readyState is "+ xmlhttpx.readyState;

    	try{
   	  	if(xmlhttpx.readyState==4){
   	  		window.clearTimeout(xtimeoutId);
   			if (xmlhttpx.status==200){  // if OK
   			   //alert("OK "+ xmlhttpx.responseText)
   			   return rhandle(xmlhttpx.responseText);
   			} else {
   			   alert("Problem retrieving XML data\nStatus is "+xmlhttpx.status+' - '+xmlhttpx.statusText)
   			}
     	   
   	  	} 
   	} catch(e){  // this is to eliminate firefox xmlhttp error messages when the annotate tool closes.
   	  	window.status = 'mycallback error ' + e.message;
   	  	xmlhttpx.abort();
   	  	xmlhttpx = null;
   	  	xtimeout();
   	  	//alert('mycallback error '+ e.message);
   	  	//myAjax(server,postval,rhandle);
   	}     
   }
};

var xtimeoutId = null;
function xtimeout(){
	clearTimeout(xtimeoutId);
}

var postval;
function callToServer(theFormName, Server, theHandle){
   postval = buildQueryString(theFormName);
   myAjax(Server, postval, theHandle);	
}

function buildQueryString(theFormName) {
  theForm = document.forms[theFormName];
  var qs = '';
  for (e=0;e<theForm.elements.length;e++) {
    if (theForm.elements[e].name!='') {
      qs+=(qs=='')?'':'&';
      qs+=theForm.elements[e].name+'='+ escape(theForm.elements[e].value);
    }
  }
 // alert(qs);  
  return qs;
};

var xmlcheck = null;
/********************
 * myAjaxURLCheck set up the xresponse for the xmlhttp call
 ********************/
function myAjaxURLCheck(url,rhandle){
window.status = 'calling '+url;
//alert('calling '+url);

   if(!xmlcheck) xmlcheck = getXMLHTTP();

   if(xmlcheck){
     xmlcheck.open("HEAD",url,true);
     xmlcheck.onreadystatechange=function()
     {

	try{
		//alert('calling '+url+ ' readystate '+xmlcheck.readyState);

		if(xmlcheck.readyState==4){
  		// if "OK"
  		   	if(xmlcheck.status == 200){
				rhandle('OK');
			} else {
				rhandle('Bad');
			} 	   
  	   
  		} else {
  			window.status = "readyState is "+ xmlhttpx.readyState;
  		}
   	  } 
	  catch(e){  // this is to eliminate firefox xmlhttp error messages when the annotate tool closes.
  		window.status = e.message;
   	  }
     }  
     
     xmlcheck.send(null);
   } else {
     alert("Unable to create a xmlhttp connection object for URL Check");
   }
};