function createRequestObject() {
    var ro;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
            ro = new XMLHttpRequest();
            if (ro.overrideMimeType) {
              // ro.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
               ro = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                	
                    ro = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!ro) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
    return ro;
}

var http = createRequestObject();

function sndReq(param,action) {
	switch(action)
		{
		case 'buildSelectLibri':
		http.open('get','utility/getLibri.php?idLista='+param,true);
    		http.onreadystatechange = buildSelectLibri;
   			http.send(null);
			break;
		case 'buildNews':
		http.open('get','http://'+window.location.hostname+'/gestione.php?'+param,true);
    		http.onreadystatechange = buildNews;
   			http.send(null);
			break;
		case 'form':
			void(0);
			break;
		default:
			http.open('post','http://'+window.location.hostname+'/gestione.php?id='+param+'&response=1',true);
    		http.onreadystatechange = changePage;
   			http.send(null);
   			break;
		}
}
function buildSelectLibri()
	{
		
	 if(http.readyState == 4){
    	  if (http.status == 200) {
    	  	try{
    	  		
                //var response = eval('('+http.responseText+')');
                var response = http.responseXML.getElementsByTagName('libri')[0].firstChild.nodeValue; 
                document.getElementById('divLibri').innerHTML = response;
             /* for (options in response)
              	{
              		 alert(response[options].value);
              	//document.getElementByID('libri').add(options[0],option[1]);
              	}*/
    	  		} catch(e) {
    	  			
    	  		  alert('errore nella scrittura della select: '+e.message);
    	  		 }
            	
            } else {
                alert('errore nella risposta del server');
            }
    	  }
	}


function changePage() {
    if(http.readyState == 4){
    	  if (http.status == 200) {
    	  		try{
                var response = http.responseXML.getElementsByTagName('content')[0].firstChild.nodeValue; 
                document.getElementById('content').innerHTML = response;  
                var title = http.responseXML.getElementsByTagName('title')[0].firstChild.nodeValue;
                document.title=title;
				var stylesheet = http.responseXML.getElementsByTagName('style')[0].firstChild.nodeValue;
				
				if(stylesheet)
                	document.styleSheet.addImport(stylesheet);
             
    	  		} catch(e) {
    	  		  
    	  		 }
            	
            } else {
                alert('There was a problem with the request.');
            }
       
        }
    }

function buildNews()
	{
		
	 if(http.readyState == 4){
    	  if (http.status == 200) {
    	  	try{
    	  		
                var response = eval('('+http.responseText+')');
                //var response = http.responseXML.getElementsByTagName('libri')[0].firstChild.nodeValue; 
                document.getElementById('evidenzaNews').innerHTML = response.content;
             /* for (options in response)
              	{
              		 alert(response[options].value);
              	//document.getElementByID('libri').add(options[0],option[1]);
              	}*/
    	  		} catch(e) {
    	  			
    	  		  alert('errore nella scrittura della select: '+e.message);
    	  		 }
            	
            } else {
                alert('errore nella risposta del server');
            }
    	  }
	}

