var TV;
var LOADING = '<img src="/assets/images/themePopup/loading.gif"/>';
function toggleGetRecordTable(caption,path)
{
	var t = caption.parentNode.getElementsByTagName('TBODY')[0];
	if(t.style.display!='none')
	{
		caption.style.borderWidth='0px';
		t.style.display='none';
	}
	else
	{
		caption.style.borderWidth='1px';
		t.style.display='';
	}

	if(null!=path && caption.LOADED==null)
	{
		if(caption.LOADING!=null)
		{
			caption.LOADING.style.display = 'block';
		}
		getNode(path,caption);

	}
	//Hack for IE
	caption.parentNode.style.borderWidth=caption.style.borderWidth;


	try{
		var li = caption.parentNode.parentNode;

		if(li.START_CLASS==null)
		{
			li.START_CLASS = li.className;
		}
		switch(t.style.display)
		{
			case 'none':
			li.className = li.START_CLASS;
			try{li.NODE.UL.style.display = 'none';li.NODE.IMG.src = li.NODE.PATH+'plus.gif';}catch(e){}
			break;

			default:
			li.className = li.START_CLASS+'_open';
			try{li.NODE.UL.style.display = li.NODE.UL.START_DISPLAY;li.NODE.IMG.src = li.NODE.PATH+'minus.gif';}catch(e){}
			break;
		}
	}catch(e){
		alert(e);
	}

}
function getNode(positions,caption)
{
	xmlhttp=null;
	var url = '/assets/largeRecordHandler/index.php?p='+escape(positions);

	//document.write(url);return;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=function(){

			try{
				if (xmlhttp.readyState==4)
				{
					// if "OK"
					//alert(xmlhttp.responseText);
					if (xmlhttp.status==200)
					{
						caption.LOADED = true;
						try{
							//caption.LOADING.style.display = 'none';
						}catch(e){
							
						}

						var containers = caption.parentNode.parentNode.getElementsByTagName('ul');
						if(containers[0] && null==document.all)
						{
							containers[0].innerHTML = '';
							containers[0].style.display = '';
							copyNodes(xmlhttp.responseXML,containers[0]);
						}
						else
						{
							containers[0].innerHTML = '';
							containers[0].innerHTML+=xmlhttp.responseText;
						}


						try{
							var lis = containers[0].getElementsByTagName('UL')[0].getElementsByTagName('li');
							for(var j=0;j<lis.length;j++)
							{
								try{
									if(lis[j].className.indexOf('hasChildren')!=-1)
									{
										caption.LOADING = document.createElement('UL');
										caption.LOADING.innerHTML = '<li>'+LOADING+'</li>';
										lis[j].appendChild(caption.LOADING);
										var tvnode = new MOB.treeviewNode(lis[j],TV.ICON_PATH);
										TV.NODES[TV.NODES.length] = tvnode;
										caption.LOADING.style.display = 'none';
									}
								}catch(e){
									alert(e);
								}
							}
						}catch(e){
							
						}

						//alert(containers[0].innerHTML);

					}
					else
					{
						alert("Problem retrieving XML data")
					}
				}
			}
			catch(e)
			{
				alert('Error in httpRequest change handler: '+e);
			}
		}
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
	}
	else
	{
		alert("Your browser does not support XMLHTTP.")
	}
}
function copyNodes (ajaxnode, copiednode) {
	
	try{
	for (var node=ajaxnode.firstChild; node != null; node = node.nextSibling) {
		if (node.nodeType == 3){ //text
			copiednode.appendChild(document.createTextNode(node.data));
		}
		if (node.nodeType == 1){ //element
			var subnode = document.createElement(node.nodeName);
			var attlist = node.attributes;
			if (attlist != null) {
				for (var i=0; i<attlist.length; i++){
					subnode.setAttribute(attlist[i].name, attlist[i].value);
				}
			}
			copiednode.appendChild(subnode);
			copyNodes(node, subnode);
		}
	}
	}catch(e){
		
	}
}
/**
* Extending the treeview
*/
MOB.treeviewNode.prototype.toggle = function(){
	
		
	
	/**
	* Extra handler to fire the onclick handler for the caption
	* and therefore load the the items/series
	*/
	try{
		var caption = this.EL.getElementsByTagName('caption')[0];

		
		
		if(this.UL!=null)
		{
			
			
			//if(null==this.UL.START_DISPLAY){this.UL.START_DISPLAY='block';}
			///if(null==this.EL.START_CLASS){this.EL.START_CLASS='li';}
			//alert(this.UL.style.display);
			
			
			switch(this.UL.style.display)
			{
				case 'none':
				this.UL.style.display = this.UL.START_DISPLAY;
				this.EL.className = this.EL.START_CLASS+'_open';
				this.IMG.src = this.PATH+'minus.gif';
				/**
				* Hide the matches info as this just clutters the layout
				*/
				try{
					var matches = caption.getElementsByTagName('span')[0];
					matches.style.display = 'none';
				}catch(e){

				}
				break;

				default:
				this.UL.style.display = 'none';
				this.EL.className = this.EL.START_CLASS;
				this.IMG.src = this.PATH+'plus.gif';
				/**
				* Reshow the matches info
				*/
				try{
					var matches = caption.getElementsByTagName('span')[0];
					matches.style.display = '';
				}catch(e){

				}
				break;
			}
		}
		else
		{
			
		}
		
		
		if(caption.parentNode.parentNode==this.EL || caption.parentNode.parentNode.parentNode==this.EL)
		{
			caption.onclick();
		}
	}catch(e){
	}
}