function createRequestObject() { 

   var req; 

   if(window.XMLHttpRequest){ 
      // Firefox, Safari, Opera... 
      req = new XMLHttpRequest(); 
   } else if(window.ActiveXObject) { 
      // Internet Explorer 5+ 
      req = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
      // There is an error creating the object, 
      // just as an old browser is being used. 
      alert('Problem creating the XMLHttpRequest object'); 
   } 

   return req; 

} 

// Make the XMLHttpRequest object 
var httpSave = createRequestObject();

function saveLists(id,list) { 
	
	document.getElementById('saving').style.display = 'block';
   // Open PHP script for requests 
   httpSave.open('get', 'save.php?id=' + id + '&list=' + list); 
   httpSave.onreadystatechange = handleSaveResponse; 
   httpSave.send(null); 

}

function handleSaveResponse() {
	if(httpSave.readyState == 4 && httpSave.status == 200){ 

    	// Text returned FROM the PHP script 
    	var response = httpSave.responseText; 
		document.getElementById('lists').innerHTML = response;
		updateSortableLists();
	}
}

function updateSortableLists(){
	
	Sortable.create("linkList1",{dropOnEmpty:true,handle:'handle',containment:["linkList1"],constraint:false,only:"userObj",onUpdate:function generateDBRep(){
	obj = document.getElementById('linkList1');
	CN = obj.childNodes; x = 0; result1 = ""; 
	while(x < CN.length){
		result1 = result1 + CN[x].id + "$$"; x++; 
	}
	saveLists('1',result1);
	}});
	
	Sortable.create("linkList2",{dropOnEmpty:true,handle:'handle',containment:["linkList2"],constraint:false,only:"userObj",onUpdate:function generateDBRep(){
	obj = document.getElementById('linkList2');
	CN = obj.childNodes; x = 0; result2 = ""; 
	while(x < CN.length){
		result2 = result2 + CN[x].id + "$$"; x++; 
	}
	saveLists('2',result2);
	}});
	   
	Sortable.create("linkList3",{dropOnEmpty:true,handle:'handle',containment:["linkList3"],constraint:false,only:"userObj",onUpdate:function generateDBRep(){
	obj = document.getElementById('linkList3');
	CN = obj.childNodes; x = 0; result3 = ""; 
	while(x < CN.length){
		result3 = result3 + CN[x].id + "$$"; x++; 
	}
	saveLists('3',result3);
	}});
	
	Sortable.create("linkList4",{dropOnEmpty:true,handle:'handle',containment:["linkList4"],constraint:false,only:"userObj",onUpdate:function generateDBRep(){
	obj = document.getElementById('linkList4');
	CN = obj.childNodes; x = 0; result4 = ""; 
	while(x < CN.length){
		result4 = result4 + CN[x].id + "$$"; x++; 
	}
	saveLists('4',result4);
	}});
	
	document.getElementById('saving').style.display = 'none';
}