function treeTooltipOn() { document.getElementById("tree-tooltip").innerHTML = treeTooltips[treeElements.indexOf(this.id)]; }
function treeTooltipOff() { document.getElementById("tree-tooltip").innerHTML = ""; }

var treeElements = ["tree-moveUp", "tree-moveDown", "tree-moveLeft", "tree-moveRight"];
var treeTooltips = ["Move Up", "Move Down", "Move Left", "Move Right"];

for (var i = 0; i < treeElements.length; i++) {
    document.getElementById(treeElements[i]).onmouseover = treeTooltipOn;
    document.getElementById(treeElements[i]).onmouseout = treeTooltipOff;
    //document.getElementById(treeElements[i]).onclick = treeInfo;
}

function treeMoveUp2() {
    if (tree.mayMoveUp()) {
        tree.moveUp();
    }
}
function treeMoveDown2() {
    if (tree.mayMoveDown()) {
        tree.moveDown();
    }
}

function treeMoveUp() {

	 var node = tree.getActiveNode();

	 if (node.isFirst()&&tree.mayMoveLeft())

	 {

	 tree.moveLeft();

	 }else if (tree.mayMoveUp()) {

     tree.moveUp();

   }

}

function treeMoveDown() {

     var node = tree.getActiveNode();

	 if (node.isLast()&&tree.mayMoveLeft())

	 {

	 tree.moveDownLeft();

	 }else if (tree.mayMoveDown()) {

     tree.moveDown();

    }

}
function treeMoveLeft() {
    if (tree.mayMoveLeft()) {
        tree.moveLeft();
    }
}
function treeMoveRight() {
    if (tree.mayMoveRight()) {
        tree.moveRight();
    }
}
function treeInsert() {
    treeHideInfo();
    document.getElementById("tree-insert-form").style.display = "block";
    document.getElementById("tree-insert-where-div").style.display = (tree.active ? "" : "none");
    if (tree.active) {
        var where = document.getElementById("tree-insert-where");
        if (tree.mayInsertInside()) {
            if (!where.options[2] && !where.options[3]) {
                where.options[2] = new Option("Inside at start", "inside_start");
                where.options[3] = new Option("Inside at end", "inside_end");
            }
        } else if (where.options[2] && where.options[3]) {
            where.options[2] = null;
            where.options[3] = null;
            where.options.length = 2;
        }
    }
}
function treeHideInsert() {
    var name = document.getElementById("tree-insert-name");
    var href = document.getElementById("tree-insert-href");
    var title = document.getElementById("tree-insert-title");
    var target = document.getElementById("tree-insert-target");
    name.value = "";
    href.value = "";
    title.value = "";
    target.value = "";
    document.getElementById("tree-insert-form").style.display = "none";
}
function treeInfo() {
    //treeHideInsert();
	 var published = document.getElementById("tree-info-published");
	  var params = document.getElementById("tree-info-params");

	  var access = document.getElementById("tree-info-access");
	  var linktype = document.getElementById("tree-info-linktype");
	   var type = document.getElementById("tree-info-type");
   
    var name = document.getElementById("tree-info-name");
    var href = document.getElementById("tree-info-href");
    var title = document.getElementById("tree-info-title");
    var target = document.getElementById("tree-info-target");

    name.value = "";
    href.value = "";
    title.value = "";
    target.value = "";
	published.value="";
    params.value="";
    access.value="";
	linktype.value="";
	type.value="";


	
   // document.getElementById("tree-info-form").style.display = "block";
    if (tree.active) {
        var node = tree.getActiveNode();
        name.value = node.text;
        href.value = node.href;
        title.value = node.title;
        target.value = node.target;
		published.value=node.published;
		//params.value=(node.params);
		params.value=URLDecode(node.params);
		access.value=node.access;
		linktype.value=node.linktype;
		if(node.isFolder){
			type.value="folder";
		}else{
			type.value="doc";
		}


	var varname="id";

		 switch(linktype.value)
	  {
	  	case('contact_item_link'):
		// varname="contact_id";
		break

	    case('submit_content' ):
		   varname="sectionid";
		break

		case('weblink_category_table'):
		  // varname="catid";
		break
		case('newsfeed_category_table' ):
		 //  varname="catid";
		break
		case('contact_category_table' ):
		   varname="catid";
		break

		case('newsfeed_link'  ):
		//   varname="feedid";
		break

		case('components'  ):
		   varname="option";
		
		break

	  }
		var url = href.value;
		var idx = url.indexOf( "&amp;" );
//alert(url);
if(linktype.value!="url" && linktype.value!="separator"){
    while ( idx > -1 ) {
        url = url.replace( '&amp;','&' );
        idx = url.indexOf('&amp;' );
    }
	var itemid=getValue(varname,url);
		if(linktype.value=="components"){itemid="option="+itemid;}
		if(linktype.value=="component_item_link"){itemid="option="+itemid;}
		
		document.getElementById("link_url2").innerHTML = url; 
		href.value=url;
}else{

document.getElementById("link_url2").innerHTML = ""; 
href.value=url;
}
		
       changeDynaList2("existlink",orders2,linktype.options[linktype.selectedIndex].value, linktype.options[linktype.selectedIndex].value, itemid,"update"); 
    }
}
function treeInfoUpdate() {
	var up_message = document.getElementById("update_message");
	var up_message2 = document.getElementById("update_message2");
	 var published = document.getElementById("tree-info-published");
      var params = document.getElementById("tree-info-params");

	  var access = document.getElementById("tree-info-access");
	  var linktype = document.getElementById("tree-info-linktype");
		 var type = document.getElementById("tree-info-type");

    var name = document.getElementById("tree-info-name");
    var href = document.getElementById("tree-info-href");
    var title = document.getElementById("tree-info-title");
    var target = document.getElementById("tree-info-target");
    name.value = name.value.trim();
    href.value = href.value.trim();
    if (!name.value) {
        return false;
    }
    if (tree.active) {
        var node = tree.getActiveNode();
   
     node.text = name.value;
        node.href = href.value;
        node.title = title.value;
        node.target = target.value;
		node.published=published.value;
		node.params=params.value;
		node.access=access.value;
		node.linktype=linktype.value;
		
		if (type.value=="folder")
		{
		node.className=type.value;
		node.isFolder=1;
		node.isDoc=0;
		if(!isArray(node.childNodes)){
		node.childNodes=new Array();
		}
		}else{ 
		node.className=type.value;	
		node.isFolder=0;
		node.isDoc=1;
		node.childNodes=null;
		}	
		
        tree.updateHtml();
    }
document.getElementById("update_message").innerHTML = node.text+" item updated"; 
document.getElementById("update_message2").innerHTML = node.text+" item params updated"; 
setTimeout('document.getElementById("update_message").innerHTML = "";',2000); 
setTimeout('document.getElementById("update_message2").innerHTML = "";',2000);
}
function docwarn() {
    var type = document.getElementById("tree-info-type");
	
	if (type.value=="doc")
	{
	if(confirm("Changing a folder to a document will delete any sub items that may have been in the folder. You should delete or move any submenu items first. Are you sure you want to continue?")){
			
		treeInfoUpdate();
	}else{

			type.value="folder";

			}
	}else{

		treeInfoUpdate();
	}


}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isFunction(a) {
    return typeof a == 'function';
}
function treeHideInfo() {
    var name = document.getElementById("tree-info-name");
    var href = document.getElementById("tree-info-href");
    var title = document.getElementById("tree-info-title");
    var target = document.getElementById("tree-info-target");
    name.value = "";
    href.value = "";
    title.value = "";
    target.value = "";
    document.getElementById("tree-info-form").style.display = "none";
}

/* only event - blur */
function treeInsertExecute() {
    var where = document.getElementById("tree-insert-where");
    var type = document.getElementById("tree-insert-type");
    var name = document.getElementById("tree-insert-name");
    var href = document.getElementById("tree-insert-href");
    var title = document.getElementById("tree-insert-title");
    var target = document.getElementById("tree-insert-target");
	 var itemid = document.getElementById("itemid");
	var linktype = document.getElementById("tree-insert-linktype");
	var published = document.getElementById("tree-insert-published");
	var access = document.getElementById("tree-insert-access");
	var params = document.getElementById("tree-info-params");
    name.value = name.value.trim();
    href.value = href.value.trim();
    if (!name.value) {
        return false;
    }
    var o = {"href": href.value, "title": title.value, "target": target.value, "linktype": linktype.value, "published": published.value, "access": access.value};
    if (tree.active) {
        switch (where.value) {
            case "before":
                tree.insertBefore("tree-"+(++tree.count), name.value, type.value, o);
                break;
            case "after":
                tree.insertAfter("tree-"+(++tree.count), name.value, type.value, o);
                break;
            case "inside_start":
                tree.insertInsideAtStart("tree-"+(++tree.count), name.value, type.value, o);
                break;
            case "inside_end":
                tree.insertInsideAtEnd("tree-"+(++tree.count), name.value, type.value, o);
                break;
        }
    } else {
        tree.insert("tree-"+(++tree.count), name.value, type.value, o);
    }
    name.value = "";
    href.value = "";
    title.value = "";
    target.value = "";
	published.value = "";
    linktype.value = "";
    access.value = "";
   params.value="0";
   // this.blur();
}
function treeRemove2() {
    if (tree.mayRemove()) {
        if (confirm("Delete current node ?")) {
            var node = tree.getActiveNode();
			removeFolder(node,node.getIndex());
			//tree.remove();
            if (document.getElementById("tree-insert-form").style.display == "block") {
                treeInsert();
            }
            if (document.getElementById("tree-info-form").style.display == "block") {
                treeInfo();
            }
        }
    }
}


function treeRemove() {

	 if (tree.active) {
            var node = tree.allNodes[tree.active];
            if (node.isDoc) { msg="Delete the \""+node.title+"\" menu item?"; }
            if (node.isFolder ) { msg="Delete the \""+node.title+"\" folder? (All child menu items will also be deleted)" }
			if (confirm(msg)) {
            removeFolder(node,node.getLevel());
			treeInfo();
			}
       }else{
			alert("Please select a menu item to delete");
       }
}


function removeFolder(node,level) {
     if(node.getLevel()>=level){
   		if (node.isDoc && !isArray(node.childNodes)){
				//alert(node.title);
				var node2=node.parentNode;
				tree.active=node.id;
				tree.remove();
				removeFolder(node2,level);
			}else if (node.childNodes.length==0){
				//alert(node.title);
				var node2=node.parentNode;
				tree.active=node.id;
				tree.remove();
				removeFolder(node2,level);
			}else{
				removeFolder(node.childNodes[0],level);
			}
     }	
}


document.getElementById("tree-moveUp").onclick    = treeMoveUp;
document.getElementById("tree-moveDown").onclick  = treeMoveDown;
document.getElementById("tree-moveLeft").onclick  = treeMoveLeft;
document.getElementById("tree-moveRight").onclick = treeMoveRight;

if (document.all && !/opera/i.test(navigator.userAgent)) {
    document.getElementById("tree-moveUp").ondblclick    = treeMoveUp;
    document.getElementById("tree-moveDown").ondblclick  = treeMoveDown;
    document.getElementById("tree-moveLeft").ondblclick  = treeMoveLeft;
    document.getElementById("tree-moveRight").ondblclick = treeMoveRight;
}

document.getElementById("tree-insert").onclick = treeInsert;
document.getElementById("tree-info").onclick = treeInfo;
//document.getElementById("tree-remove").onclick = treeRemove;

//document.getElementById("tree-insert-button").onclick = treeInsertExecute;
//document.getElementById("tree-insert-cancel").onclick = treeHideInsert;

//document.getElementById("tree-info-button").onclick = treeInfoUpdate;
//document.getElementById("tree-info-cancel").onclick = treeHideInfo;

//tree.textClickListener.add(function() { if (document.getElementById("tree-insert-form").style.display == "block") { treeInsert(); } });
//tree.textClickListener.add(function() { if (document.getElementById("tree-info-form").style.display == "block") { treeInfo(); } });

/* Finds the index of the first occurence of item in the array, or -1 if not found */
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(item) {
        for (var i = 0; i < this.length; ++i) {
            if (this[i] === item) { return i; }
        }
        return -1;
    };
}

// ---------
// ! PLUGINS
// ---------

function treePluginImportHtml() {
    document.getElementById("tree-plugin").style.display = "block";
    document.getElementById("tree-plugin-header").innerHTML = "Import from Html";
    document.getElementById("tree-plugin-button-import-html").style.display = "block";
}
function treePluginImportHtmlExecute() {
    var html = document.getElementById("tree-plugin-textarea");
    tree.importFromHtml(html.value);
}
function treePluginExportHtml() {
    var w = window.open("", "exportToHtml", "width=600,height=600,scrollbars=yes,resizable=yes");
    w.document.write('<html><body><pre>'+tree.exportToHtml().replace(/</g, "&lt;").replace(/>/g, "&gt;")+'</pre></body></html>');
}
function treePluginExportPhp() {

     document.getElementById("php_out").value = tree.exportToSql().replace(/</g, "&lt;").replace(/>/g, "&gt;");
      document.getElementById("task").value = "savemenu";

//document.mm.target="win1";
document.mm.action="index2.php";

 //window.open('', 'win1', 'status=no,toolbar=no,scrollbars=auto,titlebar=no,menubar=no,resizable=yes,width=600,height=500,directories=no,location=no');
 setTimeout('document.mm.submit()',200) ;
 setTimeout('document.mm.target="_self"',300);
// setTimeout('document.adminForm.action="index2.php"',300);

//var w = window.open("", "exportToPhp", "width=600,height=600,scrollbars=yes,resizable=yes");
 //   w.document.write('<pre>'+tree.exportToPhp().replace(/</g, "&lt;").replace(/>/g, "&gt;")+'</pre>');
}
function treePluginExportSql() {
    var w = window.open("", "exportToSql", "width=600,height=600,scrollbars=yes,resizable=yes");
    w.document.write('<pre>'+tree.exportToSql().replace(/</g, "&lt;").replace(/>/g, "&gt;")+'</pre>');
}
function treePluginHide() {
    document.getElementById("tree-plugin").style.display = "none";
    document.getElementById("tree-plugin-header").innerHTML = "";
    document.getElementById("tree-plugin-textarea").value = "";
    document.getElementById("tree-plugin-button-import-html").style.display = "none";
}

document.getElementById("tree-plugin-import-html").onclick = function() { this.blur(); treePluginHide(); treePluginImportHtml(); };
document.getElementById("tree-plugin-button-import-html").onclick = treePluginImportHtmlExecute;
document.getElementById("tree-plugin-export-html").onclick = function() { this.blur(); treePluginHide(); treePluginExportHtml(); };
document.getElementById("tree-plugin-export-php").onclick = function() { 
	
	if (!document.getElementById("menuname").value)
	{
		alert("Please enter a name for your menu");
	}else if (document.getElementById("menutype").value=="mainmenu" && document.getElementById("menuname").value != "mainmenu" )
	{
		alert("You cannot rename the main menu module.  It is required for correct operation.");
		document.getElementById("menuname").value = "mainmenu"
	}else{
	
	this.blur(); 
	treePluginHide(); 
	treePluginExportPhp(); 

	}
	
	
	
	};
document.getElementById("tree-plugin-export-sql").onclick = function() { this.blur(); treePluginHide(); treePluginExportSql(); }





function link_message_on(message,type2) { 
			
		
		if(type2=="new"){
	document.getElementById("link_message").innerHTML = "<b>"+message+"</b>"; 
	}else{
		
		document.getElementById("link_exist_message").innerHTML = "<b>"+message+"</b>"; 
		
		
	}
		}
function link_message_off() { 
	document.getElementById("link_message").innerHTML = ""; 


}
		

function getValue(varname,url)
{
 
	
  var qparts = url.split("?");
  if (qparts.length == 0)
  {
    return "";
  }
  var query = qparts[1];
  var vars = query.split("&");
  var value = "";

  for (i=0;i<vars.length;i++)
  {
    var parts = vars[i].split("=");
    
    if (parts[0] == varname)
    {
      value = parts[1];
      break;
    }
  }
  //value = unescape(value);

  return value;
}
	
	function changeDynaList2( listname, source, key, orig_key, orig_val,type2 ) {
	var list = eval( 'document.mm.' + listname );
	
	if(type2=="new"){
	var id = document.getElementById("tree-insert-href");
	 var type = document.getElementById("tree-insert-linktype");
	 var link_url=document.getElementById("link_url");
	  document.getElementById("parentid").style.display="block";
	 document.getElementById("tree-insert-href").style.display="none";
	 link_url.innerHTML="";
	 id.value="";
	}else if(type2=="update"){
		
		var id = document.getElementById("tree-info-href");
	 var type = document.getElementById("tree-info-linktype");
	 var link_url=document.getElementById("link_url2");
	  document.getElementById("existlink").style.display="block";
	 document.getElementById("tree-info-href").style.display="none";
		
		
	}
	
	
	  switch(type.value)
	  {
	  	case('content_category'):
		  link_message_on("Select category to be shown in a list format:",type2);
		break

	    case('content_blog_category' ):
		   link_message_on("Select category to be shown in a blog format:",type2);
		break

		case('content_section'):
		  link_message_on("Select section to be shown in a list format:",type2);
		break

	    case('content_blog_section' ):
		  link_message_on("Select section to be shown in a blog format:",type2);
		break
		
		 case('content_item_link' ):
		  link_message_on("Select an article to link to:",type2);
		break
		
		 case('components' ):
		    link_message_on("Select component to link to:",type2);
		break
		
		 case('component_item_link' ):
		   link_message_on("Select component to link to:",type2);
		break
		
		// case('content_archive_category' ):
		//    link_message_on("Select Content Archive Category to Link To:",type2);
		//break
		
		 case('content_archive_section' ):
			//  document.getElementById("existlink").style.display="none";
		  if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_content&view=archive";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_content&view=archive";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_content&view=archive";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_content&view=archive";
		
		
	}
		    link_message_on("Display all archived content in a list:",type2);
		break

		 case('newsfeed_category_list' ):
			  if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_newsfeeds&view=categories";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_newsfeeds&view=categories";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_newsfeeds&view=categories";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_newsfeeds&view=categories";
		
		
	}
		    link_message_on("Display all newsfeeds in a list:",type2);
		break
		
		 case('user_login' ):
			  if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_user&view=login";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=login";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_user&view=login";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=login";
		
		
	}
		    link_message_on("Display a login page:",type2);
		break

		case('user_registration' ):
			 if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_user&view=register";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=register";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_user&view=register";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=register";
		
		
	}
		    link_message_on("Display a user registration page:",type2);
		break

			case('user_reminder' ):
			  if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_user&view=remind";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=remind";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_user&view=remind";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=remind";
		
		
	}
		    link_message_on("Display a password reminder page:",type2);
		break
			case('user_reset' ):
			  if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_user&view=reset";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=reset";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_user&view=reset";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=reset";
		
		
	}
		    link_message_on("Reset current user:",type2);
		break

			case('user_default' ):
			  if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_user&view=user";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=user";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_user&view=user";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=user";
		
		
	}
		    link_message_on("Display the default users page:",type2);
		break

			case('user_form' ):
			 if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_user&view=user&layout=form";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=user&layout=form";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_user&view=user&layout=form";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_user&view=user&layout=form";
		
		
	}
		    link_message_on("Display users edit form:",type2);
		break

		case('content_frontpage' ):
			  if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_content&view=frontpage";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_content&view=frontpage";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_content&view=frontpage";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_content&view=frontpage";
		
		
	}
		    link_message_on("Display front page in a blog format:",type2);
		break

		case('contact_item_link' ):
		     link_message_on("Select contact to link To:",type2);
		break
		
		 case('contact_category_table' ):
		   link_message_on("Select contact category to be shown in a list:",type2);
		break
		
		case('newsfeed_link' ):
		   link_message_on("Select newsfeed to link to:",type2);
		break
		
		 case('newsfeed_category_table' ):
		    link_message_on("Select newsfeeds category to link to:",type2);
		break

			 case('poll_link' ):
		    link_message_on("Select a poll to link to:",type2);
		break
		
		 case('weblink_category_table' ):
		    link_message_on("Select weblink category to link to:",type2);
		break

			 case('weblink_link' ):
		    link_message_on("Select a weblink to link to:",type2);
		break

			 case('weblink_category_list' ):
				   if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_weblinks&view=categories";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_weblinks&view=categories";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_weblinks&view=categories";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_weblinks&view=categories";
		
		
	}
		    link_message_on("List of all weblinks categories:",type2);
		break
		
		 case('weblink_submit' ):
				    if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_weblinks&view=weblink&layout=form";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_weblinks&view=weblink&layout=form";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_weblinks&view=weblink&layout=form";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_weblinks&view=weblink&layout=form";
		
		
	}
		    link_message_on("submit a new weblink:",type2);
		break
		 case('submit_content' ):
			 if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_content&view=article&layout=form";
		     document.getElementById("parentid").style.display="none";
			 link_url.innerHTML = "index.php?option=com_content&view=article&layout=form";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_content&view=article&layout=form";
		     document.getElementById("existlink").style.display="none";
			 link_url.innerHTML = "index.php?option=com_content&view=article&layout=form";
		
		
	}
		   link_message_on("Users submit new content:",type2);
		break
		
		 case('content_typed' ):
		  link_message_on("Select Static Content Item to Link To:",type2);
		break
		
	  	 case('wrapper' ):
	  	 if(type2=="new"){
	  document.getElementById("tree-insert-href").value="index.php?option=com_wrapper";
		     document.getElementById("parentid").style.display="none";
	}else{
		
		  document.getElementById("tree-info-href").value="index.php?option=com_wrapper";
		     document.getElementById("existlink").style.display="none";
		
		
	}
		  
			link_message_on("You need to set the url for this wrapper in the parammeters.  eg. url=www.yoursite.com",type2);
			link_url.innerHTML = "index.php?option=com_wrapper"; 
		    break
		    
		     case('url' ):
				// alert("hello2");
		    if(type2=="new"){
	  document.getElementById("tree-insert-href").style.display="block";
		     document.getElementById("parentid").style.display="none";
	}else{
		//alert("hello");
		  document.getElementById("tree-info-href").style.display="block";
		     document.getElementById("existlink").style.display="none";
			// document.getElementById("tree-info-href").value="hello";
		
		
	}
			link_message_on("Manually type or paste the URL into the link field below:",type2);
			link_url.innerHTML = ""; 
		    break
		
		 case('separator' ):
			  if(type2=="new"){
		    document.getElementById("tree-insert-href").value="javascript:void(0);";
		      document.getElementById("parentid").style.display="none";
		 }else{
			document.getElementById("tree-info-href").value="javascript:void(0);";
		      document.getElementById("existlink").style.display="none";

		 }
			
			
			
			
			link_message_on("You have chosen a seperator, It does not link to anything.",type2);
		    link_url.innerHTML = "javascript:void(0);"; 
		break
	  
	  }
	  
	// empty the list
	for (i in list.options.length) {
		list.options[i] = null;
	}
	i = 0;
	for (x in source) {
		if (source[x][0] == key) {
			opt = new Option();
			opt.value = source[x][1];
			opt.text = source[x][2];

			if ((orig_key == key && orig_val == opt.value) || i == 0) {
				opt.selected = true;
			}
			list.options[i++] = opt;
		}
	}
	list.length = i;
}


function update_link(name,type2){
	
	 
	if(type2=="new"){
	var id = document.getElementById("tree-insert-href");
	 var type = document.getElementById("tree-insert-linktype");
	 var link_url=document.getElementById("link_url");
	}else{
		
		var id = document.getElementById("tree-info-href");
	 var type = document.getElementById("tree-info-linktype");
	 var link_url=document.getElementById("link_url2");
		
		
	}
	 
	 var link="";
	 
	 
	 
	 if(name.value){
	    switch(type.value)
	  {
		case('content_category'):
		  link="index.php?option=com_content&view=category&id="+name.value;
		  	
		break

	    case('content_blog_category' ):
		   link="index.php?option=com_content&view=category&layout=blog&id="+name.value;
		  
		break

		case('content_section'):
		  link="index.php?option=com_content&view=section&id="+name.value;
		break

	    case('content_blog_section' ):
		   link="index.php?option=com_content&view=section&layout=blog&id="+name.value;
		break
		
		 case('content_item_link' ):
		   link="index.php?option=com_content&view=article&id="+name.value;
		break
		
		 case('components' ):
		   link="index.php?"+name.value;
		break
		
		 case('component_item_link' ):
		   link="index.php?"+name.value;
		break
		
		 case('content_archive_category' ):
		    link="index.php?option=com_content&view=archive";
		break
		
		 case('user_login' ):
		    link="index.php?option=com_user&view=login";
		break
			case('user_registration' ):
		    link="index.php?option=com_user&view=register";
		break
			case('user_reminder' ):
		    link="index.php?option=com_user&view=remind";
		break
			case('user_reset' ):
		    link="index.php?option=com_user&view=reset";
		break
			case('user_default' ):
		    link="index.php?option=com_user&view=user";
		break
			case('user_form' ):
		    link="index.php?option=com_user&view=user&layout=form";
		break

		 case('content_archive_section' ):
		    link="index.php?option=com_content&view=archive";
		break

		case('contact_item_link' ):
		    link="index.php?option=com_contact&view=contact&id="+name.value;
		break
		
		 case('contact_category_table' ):
		    link="index.php?option=com_contact&view=category&catid="+name.value;
		break
		
		 case('poll_link' ):
		    link="index.php?option=com_poll&view=poll&id="+name.value;
		break

		case('newsfeed_link' ):
		    link="index.php?option=com_newsfeeds&view=newsfeed&id="+name.value;
		break

			case('weblink_link' ):
		    link="index.php?option=com_weblinks&view=weblink&id="+name.value;
		break
		
		 case('newsfeed_category_table' ):
		    link="index.php?option=com_newsfeeds&view=category&id="+name.value;
		break
		 case('newsfeed_category_list' ):
		    link="index.php?option=com_newsfeeds&view=categories";
		break
		 case('weblink_category_table' ):
		    link="index.php?option=com_weblinks&view=category&id="+name.value;
		break
		case('weblink_category_list' ):
		    link="index.php?option=com_weblinks&view=categories";
		break
			case('weblink_submit' ):
		    link="index.php?option=com_weblinks&view=weblink&layout=form";
		break
		
		 case('submit_content' ):
		    link="index.php?option=com_content&view=article&layout=form";
		break
		
		 case('content_typed' ):
		  link="index.php?option=com_content&task=view&id="+name.value;
		break

	  	 case('wrapper' ):
		    link="index.php?option=com_wrapper";
		break
	  
	  }
	 }
	 
	id.value=link;
	if(type2=="new"){
	link_url.innerHTML = link; 
	}else{
		
	link_url.innerHTML = link; 
		
		
	}
	
}
function URLEncode(url )
{
	var SAFECHARS = "0123456789" +					
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					
	var HEX = "0123456789ABCDEF";

	var plaintext = url;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} 
	
	return encoded;
}

function URLDecode(url)
{
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = url?url:"";
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	}
      return plaintext;
}
