function deleteItem(n, style){
	xmlRead('congooBuilder', 'http://' + location.host + '/builder/quickcongoo', 'action=deleteitem&n=' + n, style, true);
}

function newCongoo(){
	query = 'action=new';
	xmlRead('congooBuilder', 'http://' + location.host + '/builder/quickcongoo', query, 'quickbuilder.xsl', false);
	location.href="/index";
}

function refreshBuilder(id){
	xmlRead(id, 'http://' + location.host + '/builder/quickcongoo', 'action=refresh', 'quickbuilder.xsl', true);
}

function refreshCongoo(url){
	xmlRead('congooBuilder', 'http://' + location.host + '/builder/quickcongoo', 'action=load&CongooURL=' + url, 'quickbuilder.xsl', true);
}

function setRating(n, rating) {
	xmlRead('congooBuilder', 'http://' + location.host + '/builder/quickcongoo', 'action=setrating&n=' + n + '&rating=' + rating, 'fullbuilder.xsl', true);
}

function updateBuilder(id) {
	var title=""; 
	var summary=""; 
	var url=""; 
	var query=""; 
	var search="";
	var request="";
	var regExp=/<\S[^>]*>|[\r\n]*/g; 
	var next = 1;

	if (!validCheckRequired(frm.resultlist, 'Please select at least one site to add to your Congoo'))
	{
		return false;
	}

	// for each selected result, build a query
	
	query = "action=addresults&search=" + escape(frm.search.value);
	
	for (counter = 0; counter < frm.resultlist.length; counter++)
	{
		if (frm.resultlist[counter].checked) {
			if (document.getElementById("resultSummary" + counter)) { 
				summary = document.getElementById("resultSummary" + counter).innerHTML; 
			} else { 
				summary = ""; 
			}
			url = document.getElementById("resultLink" + counter).value; 
			title = document.getElementById("resultTitle" + counter).innerHTML; 
			title=title.replace(regExp,"");
			
			summary=summary.replace(regExp,""); 
			title=escape(title.replace("&","&")); 
			summary=escape(summary.replace("&","&")); 
			url=escape(url);

			query = query + "&link" + next + "=" + url + "&title" + next + "=" + title + "&summary" + next + "=" + summary;
			next = next + 1;
		}
	}
	xmlRead(id, 'http://' + location.host + '/builder/quickcongoo', query, 'quickbuilder.xsl', true);
}

function updateCongoo(id) {
	var cid = "";
	var congooname = "";
	var title=""; 
	var url=""; 
	var summary=""; 
	var comment=""; 
	var query=""; 
	var order="";
	var rating = "";
	var regExp=/<\S[^>]*>|[\r\n]*/g; 
	var i;

	query = "action=update";

	if (document.getElementById('CongooName') != null) {
		congooname = document.getElementById('CongooName').value;
		if (congooname == "") {
			alert("Please give your Congoo a name");
			document.getElementById('CongooName').focus();
			return;
		}
		query = query + "&congooname=" + escape(congooname);
	}
	
	query = query + "&cid=" + escape(document.getElementById('cid').value);

	i = 1;
	while (document.getElementById('order' + i))
	{
		order = document.getElementById('order' + i).value;
		comment = document.getElementById('comment' + i).value;
		comment=comment.replace(regExp,""); 
		comment=escape(comment.replace("&","&")); 

		title = document.getElementById("Title" + i).value;
		title=escape(title.replace("&","&")); 

		url = document.getElementById("Link" + i).value;
		url=escape(url);

		summary = document.getElementById("Summary" + i).value;
		summary=summary.replace(regExp,""); 
		summary=escape(summary.replace("&","&")); 

		rating = document.getElementById("Rating" + i).value;
		itemtype = document.getElementById("ItemType" + i).value;

		query = query + "&order" + i + "=" + order + "&comment" + i + "=" + comment + "&title" + i + "=" + title + "&link" + i + "=" + url + "&summary" + i + "=" + summary + "&rating" + i + "=" + rating + "&itemtype" + i + "=" + itemtype; 

		i = i + 1;
	}
	xmlRead(id, 'http://' + location.host + '/builder/quickcongoo', query, 'fullbuilder.xsl', true);
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function validCheckRequired(theField, alertMsg)
{
   var checkbox_choices = 0;
   if(theField != null) {
           if(theField.length == null) {
           	if(theField.checked)
           		checkbox_choices++;
           }
           else {
		   for (counter = 0; counter < theField.length; counter++)
		   {
			   if (theField[counter].checked)
			      { checkbox_choices = checkbox_choices + 1; }
		   }
	   }
   }
      
   if (checkbox_choices == 0 )
      {
        alert(alertMsg);
        return false;
      }
      
   return true;
}

function manualAdd(cmd) {
	var oManual;
	
	if (cmd == 'link') {
		oManual = document.getElementById('linkAddDiv');
		oManual.style.display = 'block';
		
		oManual = document.getElementById('linkAddSpan');
		oManual.style.display = 'none';
	}
	else {
		oManual = document.getElementById('linkAddDiv');
		oManual.style.display = 'none';

		oManual = document.getElementById('linkAddSpan');
		oManual.style.display = 'block';
	}
}

function addLink() {
	var title = ""; 
	var comment = ""; 
	var url = ""; 
	var rating = "";
	var query = ""; 
	var regExp=/<\S[^>]*>|[\r\n]*/g; 

	title = document.getElementById('manlinkTitle').value; 
	if (trim(title) == "")
	{
		alert('Please enter a title for this link.');
		return;
	}
	title=title.replace(regExp,"");
	title=escape(title.replace("&","&")); 

	url = document.getElementById('manlinkUrl').value; 
	if (trim(url) == "")
	{
		alert('Please enter a url for this link.');
		return;
	}
	url=escape(url);

	comment = document.getElementById('manlinkComment').value; 
	comment=comment.replace(regExp,""); 
	comment=escape(comment.replace("&","&")); 

	query = "action=addlink&link=" + url + "&title=" + title + "&comment=" + comment; 

	xmlRead('congooBuilder', 'http://' + location.host + '/builder/quickcongoo', query, 'quickbuilder.xsl', true);
}

function onCommentEnter(n) {
	comment = document.getElementById('Comment' + n).value; 
	if (comment == 'Add comments about this link here')
		document.getElementById('Comment' + n).value = '';
}

function onCommentUpdate() {
	updateCongoo('congooBuilder');
}

function encodeMyHtml(data) {
	data = escape(data);
	data = data.replace(/\//g,"%2F");
	data = data.replace(/\?/g,"%3F");
	data = data.replace(/=/g,"%3D");
	data = data.replace(/&/g,"%26");
	data = data.replace(/@/g,"%40");

	return data;
}

function gotoCongoo(frm) {
	location.href = "http://experts.congoo.com/" + frm.mycongoos.value;
}