//Javascript for winexpert gateway page
var Next = 0;
function resetdebug()
{
	var d = document.getElementById('debugbox');
	if ((d) && (d.firstChild)) 
	{
		while (d.firstChild) 
		{
			d.removeChild(d.firstChild);
		}
	}
}

function debugout(debugstr)
{
	var d = document.getElementById('debugbox');
	if ((d) && (d.insertBefore))
	{
		nc = document.createElement('br');
		d.insertBefore(nc,d.firstChild);
		var nc = document.createTextNode(Next++ +' '+debugstr);
		d.insertBefore(nc,d.firstChild);
	}
	var d = document.getElementById('debugboxswitch');
	if ((d)&&(d.style) )
		d.style.display='';
}

function addclass(classname,element)
{
	removeclass(classname,element);
	element.className = element.className + ' '+classname;
}

function removeclass(classname,element)
{
	var re = new RegExp(classname);
	element.className = element.className.replace(re,'');
	
}

function hideelem(element,selclass,unselclass)
{
		if (element)
		{
			child = element.firstChild;
			while (child) 
			{
				if (child.style)
				{	
					child.style.display = 'none'
				}
				child = child.nextSibling;
			}
		}
}

function selectButton(buttonID,childdivID,selclass,unselclass)
{	
	//resetdebug();
	var button = document.getElementById(buttonID);
	if (button)
	{
		var container = button.parentNode;
		//assign a class to all buttons in the same container that DIDNT get clicked
		addclass(selclass,button);
		var sibling = button.previousSibling;
		while (sibling) 
		{
			
			if (sibling.tagName == button.tagName)
				addclass(unselclass,sibling);
			
			sibling = sibling.previousSibling;
		}
		sibling = button.nextSibling;
		while (sibling)
		{
			
			if (sibling.tagName == button.tagName)
				addclass(unselclass,sibling);
		
			sibling = sibling.nextSibling;
	
		}
		removeclass(unselclass,button);
		//show the child container that matches (hide all others)
		//when hiding, remove selclass and unselclass
		
	}
}
var xmlPage = '';
var useXML = true;
function setXMLPage(axmlpage)
{
	xmlPage = axmlpage;
}
function setUseXML(useXML)
{
	var xmlHttpReq = false;
		 // Mozilla/Safari
        if (window.XMLHttpRequest) 
		{
                xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) 
		{
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
	if (xmlHttpReq)
		useXML = true;
	else useXML = false;
	return useXML;
}

//delete the contents of the remaining divs at the current depth in the table (
//ie: <table><tbody><tr><td><DIV id='attachnodeid'><DELETE CONTENTS></DIV><DIV><DELETE CONTENTS></DIV></td>
//											<td><DIV><DELETE CONTENTS></DIV> etc. </TD></TR></TBODY></TABLE> 
function deleteNodes(aNode,includeself)
{
	var nodetype = 0;
	var deleteNode = aNode;
	while (deleteNode) 
	{	
		if ((deleteNode.nodeName.toUpperCase() == aNode.nodeName.toUpperCase()) &&
			( (aNode != deleteNode) || (includeself) ) )
		{
			while (deleteNode.firstChild)
				deleteNode.removeChild(deleteNode.firstChild);
			if (deleteNode.style.overflowY)
				deleteNode.style.overflowY='hidden';
			else deleteNode.style.overflow='hidden';
			deleteNode.style.height='auto';
			deleteNode.style.width='auto';
		}
		if (!deleteNode.nextSibling) // jump to the next DIV inside the TD
		{
				deleteNode = deleteNode.parentNode;
				nodetype = deleteNode.nodeType;
				deleteNode = deleteNode.nextSibling;
				while ((deleteNode) && (deleteNode.nodeType != nodetype))
				{
					deleteNode = deleteNode.nextSibling;
				}
				
				if (deleteNode) 
					deleteNode = deleteNode.firstChild;
			
		}
		else deleteNode = deleteNode.nextSibling;
	}
}

function attachNodes(attachnodeid,xmlNodes,xmlText,query)
{	
	var rootNode = false;
	var valueNode = false;
	var newNode = false;
	var newA = false;
	var deleteNode = false;
	var caption = false;
	var aNode = document.getElementById(attachnodeid);	
	//delete the 'loading' node
	while (aNode.firstChild) aNode.removeChild(aNode.firstChild);
	rootNode = xmlNodes.firstChild;
	while ((rootNode) && (rootNode.nodeName != 'root'))
	{
		rootNode = rootNode.nextSibling;
	}
	if ((aNode) && (rootNode))
	{
		valueNode = rootNode.firstChild;
		while (valueNode)
		{
			if (valueNode.nodeName.toLowerCase() == 'value')
			{
				newNode =document.createElement('DIV');
				//IE likes 'className' mozilla likes 'class'
				newNode.setAttribute('className',valueNode.getAttribute('class'));
				newNode.setAttribute('class',valueNode.getAttribute('class'));
				newNode.setAttribute('id',valueNode.getAttribute('id'));
				newA = newNode.appendChild(document.createElement('A'));
				newA.style.cursor='hand';
				if (valueNode.getAttribute('caption'))
				{
					caption = unescape(valueNode.getAttribute('caption'));
					//HACK FOR SAFARI
					caption = caption.replace(/&#38;/g,'&');
					newA.appendChild(document.createTextNode(caption));
					newNode.setAttribute('title',valueNode.getAttribute('caption'))
				}
				if (valueNode.getAttribute('onclick'))
				{
					newA.setAttribute('doonclick',new Function(valueNode.getAttribute('onclick')));
					if (newA.addEventListener)
					{	//FKN OPERA (eval("eval('blah')") works in opera but not eval('blah')
						newA.addEventListener('click',new Function(valueNode.getAttribute('onclick')),false);//function (evt) { evt.target.getAttribute('doonclick')();return false; }
						/*																							 
						newA.addEventListener('click',function (event) {eval(event.target.getAttribute('doonclick'));},false);
						newA.addEventListener('click',function () {eval("eval(\"if (event) event.target.getAttribute('doonclick')\");");},false);
						newA.addEventListener('click',function () {return false;},false);*/
					}
					else if (newA.attachEvent)
					{
						newA.attachEvent('onclick',new Function(valueNode.getAttribute('onclick')));
								
															//eval(event.srcElement.getAttribute('doonclick'));return false; } 
															
					}
					else
					{	
						newA.onclick = valueNode.getAttribute('onclick')+'return false;';
					}
					newA.setAttribute('click',valueNode.getAttribute('onclick'));
				}
				if (valueNode.getAttribute('href'))
					newA.setAttribute('href',valueNode.getAttribute('href'));
				else newA.setAttribute('href','#bottom');
				aNode.appendChild(newNode);
			}
			if (valueNode.nodeName.toLowerCase() == 'header')
			{
				newNode = document.createElement('DIV');
				newNode.setAttribute('className',valueNode.getAttribute('class'));
				newNode.setAttribute('class',valueNode.getAttribute('class'));
				if (valueNode.getAttribute('caption'))
					newNode.appendChild(document.createTextNode(unescape(valueNode.getAttribute('caption'))));
				aNode.appendChild(newNode);
			}
			if (valueNode.nodeName.toLowerCase() == 'exec')
			{
				eval(unescape(valueNode.getAttribute('value')));
			}
			valueNode = valueNode.nextSibling;
		}
		var childnodes = aNode.getElementsByTagName('DIV');
		if ((childnodes.length > 15) && (childnodes[childnodes.length-1].offsetWidth > 100))
		{
			if (aNode.style.overflowY)
				aNode.style.overflowY='scroll';
			else aNode.style.overflow='scroll';
			aNode.style.height='200px';
		}
		else
		{	if (aNode.style.overflowY)
				aNode.style.overflowY='hidden';
			else aNode.style.overflow='hidden';
			aNode.style.height='auto';
		}
		if (aNode.offsetWidth)
		{
			childnodes = aNode.getElementsByTagName('DIV');
			if ((childnodes.length > 0) && (aNode.offsetWidth > childnodes[childnodes.length-1].offsetWidth *3))
			{	
				aNode.style.width = ((childnodes[childnodes.length-1].offsetWidth+10) *2)+'px';
			}
			else (aNode.style.width='auto');
			
		}
		hackNode = aNode.parentNode.parentNode;
		//big fat hack!!! (ie is buggy as crap)
		hackNode.style.height='100px';
		hackNode.style.height='auto';
		hackNode.style.display='none';
		hackNode.style.display='';
		window.location = '#bottom';
	}
	else
	{
		if (rootNode)
			debugout('couldn\'t find node '+attachnodeid);
		else 
		{
			debugout('couldn\'t find rootnode');
			debugout(query);
			debugout(xmlText);
		}
	}
}

function loadXML(querytype,querydata,selectbuttonID,attachnodeid,navHides)
{
		var xmlHttpReq = false;
		 // Mozilla/Safari/Opera
        if (window.XMLHttpRequest) 
		{
                xmlHttpReq = new XMLHttpRequest();
                if (xmlHttpReq.overrideMimeType)//doesn't exist in opera
					xmlHttpReq.overrideMimeType('text/xml');
        }
        // IE
        else if (window.ActiveXObject) 
		{
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }//copy from http://www.onlamp.com/pub/a/onlamp/2005/05/19/xmlhttprequest.html
		if (xmlHttpReq) 
		{
			xmlHttpReq.open('POST',xmlPage,true);
			xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xmlHttpReq.onreadystatechange = function() 
				{
					if (xmlHttpReq.readyState == 4) 
					{
							eval('attachNodes("'+attachnodeid+'",xmlHttpReq.responseXML,xmlHttpReq.responseText,"querytype='+escape(querytype)+'&querydata='+escape(querydata)+'");');
					}
				}
			xmlHttpReq.send('querytype='+escape(querytype)+'&querydata='+escape(querydata));
			aNode = document.getElementById(selectbuttonID);
			if (aNode)
			{
				aNode = aNode.parentNode;
				deleteNodes(aNode,false);
			}
			var aNode = document.getElementById(attachnodeid);
			if (aNode)
			{	
				deleteNodes(aNode,true);//delete nodes lower in the visual tree
				aNode.appendChild(document.createElement('DIV'));
				aNode.firstChild.setAttribute('id','gwLoading');
				aNode.firstChild.appendChild(document.createTextNode('Loading'));
				imgNode = document.createElement('IMG');
				imgNode.setAttribute('src','/assets/images/global/load.gif');
				imgNode.setAttribute('align','absmiddle');
				aNode.firstChild.appendChild(imgNode);
			}
		}
		selectButton(selectbuttonID,'','focus','blur');
}


//document.addEventListener('load',resetdebug,false);

