var pre_tr = null;
var sel_tr = null;
var sel_title = '';
var listSeriesID = '';
var isNetscape = false;

if (window.ActiveXObject || "ActiveXObject" in window) { // ("ActiveXObject" in window) added to work with IE11
 // We're running IE
 isNetscape = false;
} else if (navigator.plugins) {
 // We're running something else
 isNetscape = true;
}
else {
 alert("not supported.");
}
/*
if(navigator.appName == "Netscape") {
 isNetscape = true;
}*/

function mouseOverRow(obj)
{
	obj.className = 'mouseOverRow';
	window.status = obj.description;
}
function mouseOutRow(obj)
{
	obj.className = 'mouseOutRow';		
	window.status = '';
}
function mouseClickRow(obj)
{
	
	if (pre_tr != null)
	{
		if(isNetscape == true)
		{
			pre_tr.style.backgroundColor = "white";
		}
		else
		{
			pre_tr.style.backgroundColor = pre_tr.prebgcolor;
		}			
	}
	pre_tr = obj;
	obj.className = 'mouseOverRow';
	obj.style.backgroundColor = '#D9D9D9';
	
	if(isNetscape == true)
	{
		sel_tr = obj.getAttribute("value");
		sel_title = obj.getAttribute("description");
	}
	else
	{
		sel_tr = obj.value;
		sel_title = obj.description;
	}
}

function switchDICOM(bDICOM)
{
	var xmlStudies = xmlDoc.selectNodes('//Study');
	for(ib=0;ib<xmlStudies.length;ib++)
	{
		var objTreeIcon = document.all['TIO_' + (ib+1)];
		var objTreeForm = document.all['O_' + (ib+1)];
		if (bDICOM)
		{
			objTreeIcon.style.display = 'none';
			objTreeForm.style.display = 'none';
		}
		else
		{
			objTreeIcon.style.display = '';
		}
	}
	
	if (bDICOM)
	{
		uncheckedAll();
	}
}
	
// 트리를 접거나 연다.
var prev_obj = new Array();
function fold_tree(strobj, index)
{
	var obj = null;
	var obj2 = null;
	
	if(isNetscape)
	{
		obj = document.getElementById(strobj);
		obj2 = document.getElementById('TI' + strobj);
	}
	else
	{	
		obj = document.all[strobj];
		obj2 = document.all['TI' + strobj];
	}

	if (obj != null)
	{
		if (obj.style.display == 'none')
		{
			obj.style.display = '';
			obj2.src='./DATA/MNU_MIN.GIF';
			if (prev_obj[index] != null && prev_obj[index].substring(0,2) == obj.id.substring(0,2) && prev_obj[index] != obj.id)
			{	
				if (index == 0)
				{
					collapse_tree(prev_obj[index]);	
					uncheckedAll();
				}
				prev_obj[index] = obj.id;
			}
			else
			{
				prev_obj[index] = obj.id;
			}
		}
		else
		{
			obj.style.display = 'none';
			obj2.src='./DATA/MNU_PLUS.GIF';
		}		
	}

}

function collapse_tree(strobj)
{
	if(isNetscape)
	{
		var obj = document.getElementById(strobj);
		var obj2 = document.getElementById('TI' + strobj);
		obj.style.display = 'none';
		obj2.src='./DATA/MNU_PLUS.GIF';

	}
	else
	{
		var obj = document.all[strobj];
		var obj2 = document.all['TI' + strobj];
		obj.style.display = 'none';
		obj2.src='./DATA/MNU_PLUS.GIF';
	}
}

function clickCheckBox(obj)
{
	if(isNetscape)
	{
		if (document.getElementById("radioViewTypeD").checked == true)
		{
			uncheckedAll();
			obj.checked = true;
		}
		
	}
	else
	{
		if (document.getElementById("radioViewTypeD").checked == true)
		{
			uncheckedAll();
			obj.checked = true;
		}
	}
}

function uncheckedAll()
{
	var objCol = eval('frmView.chkSelect');
	if(objCol.length)
	{
		for (c=0;c<objCol.length;c++)
		{
			objCol[c].checked = false;
		}
	}
	else
	{
		objCol.checked == false;
	}
}

function viewSeries()
{
	listSeriesID = '';

	if(isNetscape)
	{
		var objCol = document.getElementsByName("chkSelect");
		if(document.getElementById("chkAllView").checked == false)
		{
			if(objCol.length)
			{
				for (c=0;c<objCol.length;c++)
				{
					if (objCol[c].checked == true)
					{	
						listSeriesID += objCol[c].value + '|';
					}
				}
			}
			else
			{
				if (objCol.checked == true)
				{	
					listSeriesID += objCol.value + '|';
				}
			}			
		}
		else
		{
			if(objCol.length)
			{
				for (c=0;c<objCol.length;c++)
				{
					listSeriesID += objCol[c].value + '|';
				}
			}
			else
			{
				listSeriesID += objCol.value + '|';
			}
		}

		
			if (listSeriesID == '' && sel_tr == null)
			{
				alert('There are no selected study/series');
				return false;
			}
			else if (listSeriesID == '')
			{ 
				try
				{
						
					var nodes = xmlDoc.getElementsByTagName('Study');
					for(var n = 0; n < nodes.length; n++)
					{
						var subnodes = nodes[n].getElementsByTagName('StudyInstanceUid');
						if(subnodes.length > 0)
						{
							var strSUID = subnodes[0].firstChild.nodeValue;
							
							if(strSUID == sel_tr)
							{
								var seriesNodes = nodes[n].getElementsByTagName('Series');
								var seriesLen = seriesNodes.length;
								
								for(i=0; i<seriesLen; i++)
								{
									var subsnodes = seriesNodes[i].getElementsByTagName('SeriesInstanceUid');
									if(subsnodes.length > 0)
									{
		 							   var strSeriesUID = subsnodes[0].firstChild.nodeValue;
									   listSeriesID += strSeriesUID + '|';
									}	
								}
							}
										
						}
					}	
				}
				catch(E)
				{
					//alert(E);	
				}
			}
		

		var winreswidth=window.screen.availWidth-8; 
		var winresheight=window.screen.availHeight-28; 
		
		var viewerSrc = './WEBVIEW/WEBVIEWN.HTM';		
		var winViewer = window.open(viewerSrc, 'WebViewer', 'height='+winresheight+',width='+winreswidth+',top=0,left=0,resizable=yes,scrollbars=yes');

	}
	else
	{
		var objCol = eval('frmView.chkSelect');
	
		if (document.all.chkAllView.checked==false)
		{
			if(objCol.length)
			{
				for (c=0;c<objCol.length;c++)
				{
					if (objCol[c].checked == true)
					{	
						listSeriesID += objCol[c].value + '|';
					}
				}
			}
			else
			{
				if (objCol.checked == true)
				{	
					listSeriesID += objCol.value + '|';
				}
			}
		}
		else
		{
			if(objCol.length)
			{
				for (c=0;c<objCol.length;c++)
				{
					listSeriesID += objCol[c].value + '|';
				}
			}
			else
			{
				listSeriesID += objCol.value + '|';
			}
		}
	
		
			if (listSeriesID == '' && sel_tr == null)
			{
				alert('There are no selected study/series');
				return false;
			}
			else if (listSeriesID == '')
			{ 
				var xmlStudy = xmlDoc.selectSingleNode('//Study[StudyInstanceUid = \'' + sel_tr + '\']');
				var xmlSeriesNodes = xmlStudy.selectNodes('Series');
				for(i=0;i<xmlSeriesNodes.length;i++)
				{
					listSeriesID += xmlSeriesNodes[i].selectSingleNode('SeriesInstanceUid/text()').text + '|';
				}
			}
		

		var winreswidth=window.screen.availWidth-8; 
		var winresheight=window.screen.availHeight-28; 
		
		var viewerSrc = './WEBVIEW/WEBVIEW.HTM';		
		var winViewer = window.open(viewerSrc, 'WebViewer', 'height='+winresheight+',width='+winreswidth+',top=0,left=0,resizable=yes,scrollbars=yes');
	}
}

function getSelectedMess()
{
	return listSeriesID;
}

function getSelectedStudy()
{
	var objChk = null;

	if(isNetscape)
	{
		objChk = document.getElementById("chkAllView");
	}
	else
	{	
		objChk = document.all.chkAllView;
	}
	
	if (objChk.checked == false)
	{
		return sel_tr;
	}
	else
	{
		return 'ALL';
	}	
}

function getSelectedStudyTitle()
{
	return sel_title;
}



//nName : 'Series'
//eName : 'StudyInstanceUid'
function nnSelectSingleNode(objXml, nName, eName, eValue)
{
   var bFind = false;
   var objOut = document.implementation.createDocument("", "xml", null);	
   try
   {
	var nodes = objXml.getElementsByTagName(nName);
	var nLen = nodes.length;
	
	if(nLen > 0)
	{
		for(var n = 0; n < nLen; n++)
		{
			if(bFind) break;
				
			var subnodes = nodes[n].getElementsByTagName(eName);
			var nSub = subnodes.length;
			if(nSub > 0)
			{
				for(var s = 0; s < nSub; s++)
				{
					var strValue = subnodes[s].firstChild.nodeValue;
					
					if(eValue.length > 0)
					{
						if(strValue == eValue)
						{
						   objOut = nodes[n];
						   
						   bFind = true;
						   break;
						}								
					}
				}							
			}
		}	
	}				
   }
   catch(e)
  {
	//alert(e);	
  }
  
  return objOut;
}


function nnSelectSingleNodes(objXml, nName)
{
   try
   {
	var nodes = objXml.getElementsByTagName(nName);
	return nodes;			
   }
   catch(e)
  {
	//alert(e);	
  }
  
  return null;
}

function nnSelectSingleNodeValue(objXml, nName)
{
   var strNodeValue = "";
   try
   {
	var nodes = objXml.getElementsByTagName(nName);
	if(nodes.length > 0)
	{
	    strNodeValue = nodes[0].firstChild.nodeValue;
	}	
	
   }
   catch(e)
  {
	//alert(e);	
  }
  
  return strNodeValue;
}


function nnSelectSingleNodeAtt(objXml, nName, attName)
{
   var strNodeAtt = "";
   try
   {
	var nodes = objXml.getElementsByTagName(nName);
	if(nodes.length > 0)
	{
	    strNodeAtt = nodes[0].attributes.getNamedItem(attName).value;
	}	
	
   }
   catch(e)
  {
//	alert(e);	
  }
  
  return strNodeAtt;
}