 var request7352833 = createRequest(); // For Related Items
 var request5478 = createRequest(); // List of related communities
 var request347328 = createRequest(); // List of related communities that are found using PostTopic, Type=Direct

function Submit_query_For_List_of_Related_Communities_Posted_Directly (User_ID) {

	 var query = document.getElementById("Topic").value; 
	 var Tag_Lists = document.getElementById("Tag_Lists").value;

if ((query.length != "") || (Tag_Lists != "") || (User_ID != "")) {
	
	if (Tag_Lists == "") {
		Tag_Lists = "NotTagsHere";
	}
	 var url = "Ajax-2-Related-Content.php?query=" + query + "&XML_Request=List_of_Related_Communities_Posted_Directly&Tag_Lists=" + Tag_Lists + "&User_ID=" + User_ID;
	
		 request347328.open("GET", url, true); /* this line initiates the connection */
		 request347328.onreadystatechange = update_List_of_Related_Communities_Posted_Directly; /* when the server returns data, function updatepage is called */
		 request347328.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");		 
		 request347328.send(null); /* this sends the request (with zero data) */
}

} // Send request for a list of related communities posted using Type=Direct [ Submit query For List of Related Communities Directly ] 

function update_List_of_Related_Communities_Posted_Directly() {

	if (request347328.readyState == 4) {
		if (request347328.status == 200) {

		var xmlDoc = request347328.responseXML;

			if (xmlDoc.getElementsByTagName ("XML_Request")[0].firstChild.nodeValue == "List_of_Related_Communities_Posted_Directly") {
				show_List_of_Related_Communities_Posted_Directly (xmlDoc);
			}
  		}
		request347328.createRequest();    	
	}
} // [ update List of Related Communities Posted Directly ]

function show_List_of_Related_Communities_Posted_Directly (xmlDoc) {

XMLcount = xmlDoc.getElementsByTagName ("Community").length; 

if (document.getElementById("Ripple")) {
// remove all previous related communities
	var MsgParentb = document.getElementById("Add_Ripple").parentNode;
	MsgParentb.removeChild(document.getElementById("Ripple"));	 
}
		  // --	Create a div to add the related communities to -- //
		  if (XMLcount > 0) {
		  var Ripple = document.createElement("div");
			Ripple.id = 'Ripple';						
			Ripple.innerHTML = "Please choose a community to post this topic to:<div id = 'List_Communities'></div>"; 

 			var Add_Ripple = document.getElementById("Add_Ripple");	
			var MsgParent = Add_Ripple.parentNode;
			MsgParent.insertBefore(Ripple, Add_Ripple);
		  }

var Related_Community_Name = document.getElementById("List_Communities");
for (i=0; i < XMLcount; i++) {
				
		var XML_Related_Community = decodeURIComponent(xmlDoc.getElementsByTagName ("Community")[i].firstChild.nodeValue); 					
		//http://www.the-art-of-web.com/javascript/escape/
   		Related_Community_Name.innerHTML = Related_Community_Name.innerHTML + XML_Related_Community + "<Br />";
	}
} // [ show List of Related Communities Posted Directly]

function Submit_query_For_List_of_Related_Communities (Community_ID) {
	 var query = document.getElementById("Topic").value; 
	 var Tag_Lists = document.getElementById("Tag_Lists").value;

if ((query.length != "") || (Tag_Lists != "")) {
	
	if (Tag_Lists == "") {
		Tag_Lists = "NotTagsHere";
	}

	 var url = "Ajax-2-Related-Content.php?query=" + query + "&XML_Request=List_of_Related_Communities&Tag_Lists=" + Tag_Lists + "&Community_ID=" + Community_ID;
	
		 request5478.open("GET", url, true); /* this line initiates the connection */
		 request5478.onreadystatechange = update_List_of_Related_Communities; /* when the server returns data, function updatepage is called */
		 request5478.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");		 
		 request5478.send(null); /* this sends the request (with zero data) */
}

} // Send request for a list of related communities according to the topic name and its topic tags [ Submit query For List of Related Communities ] 

function update_List_of_Related_Communities() {

	if (request5478.readyState == 4) {
		if (request5478.status == 200) {

		var xmlDoc = request5478.responseXML;

			if (xmlDoc.getElementsByTagName ("XML_Request")[0].firstChild.nodeValue == "List_of_Related_Communities") {
				show_List_of_Related_Communities (xmlDoc);							
			}
  		}
		request5478.createRequest();    	
	}
} // [ update List of Related Communities ]

function show_List_of_Related_Communities (xmlDoc) {

XMLcount = xmlDoc.getElementsByTagName ("Community_Name").length; 

if (document.getElementById("Ripple")) {
// remove all previous related communities
	var MsgParentb = document.getElementById("Add_Ripple").parentNode;
	MsgParentb.removeChild(document.getElementById("Ripple"));	 
}
		  // --	Create a div to add the related communities to -- //
		  if (XMLcount > 0) {
		  var Ripple = document.createElement("div");
			Ripple.id = 'Ripple';						
			Ripple.innerHTML = "You may also choose to post this topic in one of the following communities:<div id = 'List_Communities'></div>"; 

 			var Add_Ripple = document.getElementById("Add_Ripple");	
			var MsgParent = Add_Ripple.parentNode;
			MsgParent.insertBefore(Ripple, Add_Ripple);
		  }
		  
for (i=0; i < XMLcount; i++) {

		var XML_Related_Community_Name = decodeURIComponent(xmlDoc.getElementsByTagName ("Community_Name")[i].firstChild.nodeValue); 			
		var XML_Related_Community_ID = decodeURIComponent(xmlDoc.getElementsByTagName ("Community_ID")[i].firstChild.nodeValue); 					
		//http://www.the-art-of-web.com/javascript/escape/

   		var Related_Community_Name = document.getElementById("List_Communities");
   		Related_Community_Name.innerHTML = Related_Community_Name.innerHTML + "<input type='radio' name='Select_Related_Community' value='" + XML_Related_Community_ID + "' />";
		Related_Community_Name.innerHTML += XML_Related_Community_Name + "<Br />";

	}

} // [ show List of Related Communities ]

function Submit_Search_query(Type) { 
if (Type == "Topic") {
	 var query = document.getElementById("Topic").value; 
	 var Tag_Lists = document.getElementById("Tag_Lists").value;
		 if (Tag_Lists == "") {
			Tag_Lists = "NoTagsHere";
		 }

	 // Erase previous finds
		for (i=0; i < 5; i++) {
		    var Related_Topic_Name = document.getElementById("Related_Topic_Count" + i);
			Related_Topic_Name.innerHTML = "";
		}
}
else if (Type == "Community") {
	 var query = document.getElementById("Community_Name").value; 
	 var Tag_Lists = document.getElementById("Tag_Lists").value; 
		 if ((Tag_Lists == "") || (Tag_Lists == "example: math, algebra, geometry, calculus")) {
			Tag_Lists = "NoTagsHere";
		 }

		// Erase previous finds
		for (i=0; i < 5; i++) {
		    var Related_Topic_Name = document.getElementById("Related_Topic_Count" + i);
			Related_Topic_Name.innerHTML = "";
		}
}

if (query.length > 2) {
	if (Type == "Topic") {
	 var url = "Ajax-2-Related-Content.php?query=" + query + "&XML_Request=Related_Topics&Tag_Lists=" + Tag_Lists;
	}
	else if (Type == "Community") {
	 var url = "Ajax-2-Related-Content.php?query=" + query + "&XML_Request=Related_Communities&Tag_Lists=" + Tag_Lists;		
	}
			
		 request7352833.open("GET", url, true); /* this line initiates the connection */
		
		 request7352833.onreadystatechange = update_Search_query; /* when the server returns data, function updatepage is called */
		 request7352833.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");		 
		 request7352833.send(null); /* this sends the request (with zero data) */
}
		 
} // Each time the search textbox loses focus, this automatically contacts the server [ Submit Search query ]

function update_Search_query() {  

    if (request7352833.readyState == 4) {
		if (request7352833.status == 200) {

		var xmlDoc = request7352833.responseXML;

			if (xmlDoc.getElementsByTagName ("XML_Request")[0].firstChild.nodeValue == "Related_Topics") {

				show_related_topics (xmlDoc);							
			}
			else if (xmlDoc.getElementsByTagName ("XML_Request")[0].firstChild.nodeValue == "Related_Communities") {

				show_related_communities (xmlDoc);							
			}


  		}
		request7352833.createRequest();    	
	}
  }	// Update the page with either Related Topics or Related Communities [ Update Search Query ]
  
function show_related_communities (xmlDoc) {

		XMLcount = xmlDoc.getElementsByTagName ("Community").length; 

		for (i=0; i < XMLcount; i++) {

			var XML_Related_Topic_Name = decodeURIComponent(xmlDoc.getElementsByTagName ("Community")[i].firstChild.nodeValue); 			
			//http://www.the-art-of-web.com/javascript/escape/

			var Related_Topics_Container = document.getElementById("Related_Topics");
			Related_Topics_Container.style.visibility = "visible";
			
		    var Related_Topic_Name = document.getElementById("Related_Topic_Count" + i);
			Related_Topic_Name.innerHTML = XML_Related_Topic_Name; //+ " - " + XML_Related_Community_Name;			
				
		}
} // Display the extracted Related Topics on the page [ show related topics ]

function show_related_topics (xmlDoc) {

		XMLcount = xmlDoc.getElementsByTagName ("Topic").length; 

		for (i=0; i < XMLcount; i++) {

			var XML_Related_Topic_Name = decodeURIComponent(xmlDoc.getElementsByTagName ("Topic")[i].firstChild.nodeValue); 			
			//http://www.the-art-of-web.com/javascript/escape/

			var Related_Topics_Container = document.getElementById("Related_Topics");
			//Related_Topics_Container.style.visibility = "visible";
			
		    var Related_Topic_Name = document.getElementById("Related_Topic_Count" + i);
			Related_Topic_Name.innerHTML = XML_Related_Topic_Name; //+ " - " + XML_Related_Community_Name;			
				
		}
} // Display the extracted Related Topics on the page [ show related topics ]