// JScript source code
var wb=null;		      // Main workbench object
var edApplet = null;    // Engg Data applet object
var eds = null;         // Engg Data script engine
var eda = null;         // Engg Data namespace object (main.js)

var PropHierInterval = 0;
var PropSearchInterval = 0;

function edPropHierScope ()
{
   this.xmlDoc = null;
   this.index = [];
   this.ActiveCrumb = null;


   this.currentTab = "kSummary";           // The default tab page will be the summary
   this.currentCategory = "kProjMaterial";    // The default category will be materials
   this.ActiveItem = null;
   // This var for testing only
	this.TBHierBuilt = false;
}
function edPropHierScope.prototype.destructor ()
{
   this.xmlDoc = null;
   this.index = null;
   this.ActiveCrumb = null;
   this.currentTab = null;           // The default tab page will be the summary
   this.currentCategory = null;    // The default category will be materials
   this.ActiveItem = null;
   // This var for testing only
	this.TBHierBuilt = null;
	this.document = null;
	return null;
}

var edph = new edPropHierScope();
var edps = new edPropSummaryScope();

function window_onload( )
{
//debugger;
   // Go get the WB, Engg Data Applet; Script; and namespace
	wb			=  window.external;
	edApplet =  wb.AppletList.Applet( "EDApplet").App;
	eds = edApplet.Script;
   eda = eds.eda;

   eda.OverloadDefaultEvents(document);

   eda.PropHierScript = edph;
   eda.PropSummaryScript = edps;
   edps.window = window;

   edph.document = document;
   edps.document = document;

   // This determines Internet Explorer's major version number,
   // or 0 for others. It works by finding the "MSIE " string and
   // extracting the version number following the space, up to the decimal
   // point, ignoring the minor version number
   var ieVersion = 0;
   var ua = window.navigator.userAgent;
   var msie = ua.indexOf ( "MSIE " );

   if ( msie > 0 )      // The version for Internet Explorer is available
      ieVersion = parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
      
   if(ieVersion < 6)
   {
	   PropHierInterval = window.setInterval("edph.EDAPropHierLoaded()", 250);
	}
	else
	{
	   edph.EDAPropHierLoaded();
	}
}

function window_onunload( )
{
   eda.PropHierScript = null;
   edph = edph.destructor();
   eda.PropSummaryScript = null;
   edps = edps.destructor();
}

edPropHierScope.prototype.EDAUpdateSearch = function ()
{
		if( !isNaN(PropHierInterval) )
			window.clearInterval(PropSearchInterval);

		edph.updateSearchResults();
}

edPropHierScope.prototype.EDAPropHierLoaded = function ()
{
	try
	{
		if( !isNaN(PropHierInterval) )
			window.clearInterval(PropHierInterval);
		PropHierInterval = 0;
      if ( EDA_PSV_BODY ) {
         // The Engg Data Project Form is loaded
        	//SearchBox.onkeyup = this.onkeyup;

         //edph.createHierarchy(eda.installDir + eda.str_PathSeparator + "xml" + eda.str_PathSeparator + "PropertyHierarchy.xml");
          edps.createHierarchy(eda.installDirNoLocale + eda.str_PathSeparator + "xml" + eda.str_PathSeparator + "PropertyHierarchy.xml");
         //Call after a delay to fill in the search results
		 //PropSearchInterval = window.setInterval(this.EDAUpdateSearch, 250);

      }
      this.TBHierBuilt = true;
      
        try
        {
            var ds = getAppletInMemory("DSApplet");
            ds.Script.ReportCallbackEDPageLoaded("PropertySummary", document);
        }
        catch(e)
        {
        }      
	}
	catch (e)
	{
		if( !isNaN(PropHierInterval))
			window.clearInterval(PropHierInterval);
		PropHierInterval = window.setInterval("edph.EDAPropHierLoaded()", 250);
		this.TBHierBuilt = true;
	}
}


edPropHierScope.prototype.createHierarchy = function ( xmlFile )
{
   try
   {
      this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

      eda.BreadCrumbScript.xmlDoc = this.xmlDoc;

      // Is this necessary
      this.xmlDoc.async = false;
      var bSuccess = this.xmlDoc.load(xmlFile);
      if (!bSuccess)
      {
         return;
      }
     	this.walk(this.xmlDoc.documentElement);

	   this.index.sort(this.alphabetically);

      try
      {
	      eda.PropHierScript.updateBreadcrumbs(this.xmlDoc.documentElement);
	   }
	   catch(error)
	   {
	   }
	   //this.updateSearchResults();
   }
   catch(error)
   {
//debugger;
   }
}

edPropHierScope.prototype.walk = function (element)
{
	var children = element.childNodes;
	for ( var child = children.nextNode(); child != null; child = children.nextNode() )
	{
		var obj = { text:child.getAttribute("name"), node:child };

		// Check if this is a property
		var propEnum = { text:child.getAttribute("propEnum"), node:child };
		if (propEnum.text && (propEnum.text != "kMatlPropUndefined"))
      {
 		   this.index.push(obj);
 		   // Set the property to suppressed/unsuppressed from the XML
		   var propSuppressed = { text:child.getAttribute("suppressed"), node:child };
         if (propSuppressed.text != null)
         {
		      var prop = this.getCurrentProperty(propEnum.text);
		      if (!prop) return;
             prop.isSuppressed = true;
/*
*/
		   }

      }

		this.walk(child);
	}
}

edPropHierScope.prototype.alphabetically = function (one,two)
{
	return (one.text < two.text) ? -1 : (one.text > two.text) ? 1 : 0;
}

edPropHierScope.prototype.updateBreadcrumbs = function (node)
{

   try {
      if (!eda.DataViewScript.ActiveItem)
            return;

      if (!node) node = this.ActiveCrumb;
      this.ActiveCrumb = node;
	   EDPropHierDrillDown.innerHTML = "";
	   var children = node.childNodes;
	   for(var child = children.nextNode(); child != null; child = children.nextNode())
	   {
		   var e = this.getElement(document, "WB:ITEM",child.getAttribute("name"),this.searchResultClicked);
		   e.node = child;
		   EDPropHierDrillDown.appendChild(e);
	   }

      var breadcrumbs =	eda.BreadCrumbScript.HTMLBreadCrumbs;
      var curdoc = breadcrumbs.document;
      breadcrumbs.innerHTML = "";
	   for( ; node.nodeType==1 /*NODE_ELEMENT*/; node = node.parentNode)
	   {
		   var tag = (breadcrumbs.innerHTML=="") ? "WB:ACTIVECRUMB" : "WB:BREADCRUMB";
		   var text = (node==this.xmlDoc.documentElement) ? eda.DataViewScript.ActiveItem.Name : node.getAttribute("name");

		   // This will set the current property to edit
		   var propEnum = node.getAttribute("propEnum");
		   if (propEnum)
		   {
		      var enumStr = "eda.matPropEnums." + propEnum;
	         var enumValue = eval(enumStr);
		      this.ActiveItem = enumValue;
            eda.DataViewScript.ActiveSubItem = enumValue;
            eda.MaterialScope.AddProperty(enumValue);
		   }
		   crumb = eda.BreadCrumbScript.getElement(curdoc, tag, text, eda.BreadCrumbScript.crumbClick);
		   crumb.node = node;
		   breadcrumbs.insertBefore(crumb, breadcrumbs.firstChild);
		   if(breadcrumbs.childNodes.length>1)
		   {
			   var arrow = eda.BreadCrumbScript.getElement(curdoc, "WB:BREADSPAN", " > ");
			   breadcrumbs.insertBefore(arrow, crumb.nextSibling);
		   }
	   }
   }
   catch (error)
   {
      //debugger;
   }
}

edPropHierScope.prototype.getCurrentProperty = function ( propEnum )
{
   var prop = null;

   if (!eda.DataViewScript.ActiveItem) return prop;

   if (!material) material = eda.DataViewScript.ActiveItem;

   try
   {
	   var propStr = "material.getProperty(eda.matPropEnums." + propEnum + ")";
	   var prop = eval(propStr);
   }
   catch(error)
   {
      // The propEnum may be the actual enum value so try again
	   var prop = material.getProperty(propEnum);
   }
   return prop;
}

edPropHierScope.prototype.crumbClick = function ()
{
	eda.PropHierScript.updateBreadcrumbs(this.node);
}

edPropHierScope.prototype.getElement = function (curdoc, tagName, text, clickHandler)
{
	var element = curdoc.createElement(tagName);
	if(text) element.innerText = text;
	if(clickHandler)
	{
		element.onmouseover = this.mouseover;
		element.onmouseout = this.mouseout;
		element.onclick = clickHandler;
	}
	return element;
}


//
edPropHierScope.prototype.onkeyup = function ()
{
   // "this" in a CB from the document isn't the namespace object
	edph.updateSearchResults();
}

//
edPropHierScope.prototype.mouseover = function ()
{
	this.style.textDecoration = "underline";
}

//
edPropHierScope.prototype.mouseout = function ()
{
	this.style.textDecoration = "none";
}

//
edPropHierScope.prototype.searchResultClicked = function ()
{
	eda.PropHierScript.updateBreadcrumbs(this.node);
}

//
edPropHierScope.prototype.SearchNodes = function ( enumValue )
{
	var node = null;
	for(var i=0;i<this.index.length; i++)
	{
		var propEnum = this.index[i].node.getAttribute("propEnum");
		if (propEnum)
		{
		   var enumStr = "eda.matPropEnums." + propEnum;
	      var lclEnumValue = eval(enumStr);
		   if ( lclEnumValue == enumValue )
	      {
		      node = this.index[i].node;
		      break;
	      }
	   }
   }
   return node;
}

//
edPropHierScope.prototype.updateSearchResults = function ()
{

	SearchResults.innerHTML = "";

	//var inclSupp = chkEDPHSuppressed.checked;
	var inclSupp = 1;
	//var exclValid = chkEDPHInvalid.checked;
	var exclValid = 0;

	for(var i =0 ; i < this.index.length; i++)
	{
	   var stringMatch = this.index[i].text.toLowerCase().indexOf(SearchBox.value.toLowerCase());
	   var searchOption = true;
	   if ( !inclSupp )
	   {
	   	// Get the isSuppressed value from the property and if suppressed then show
	      var propEnum = this.index[i].node.getAttribute("propEnum");
	      if (propEnum)
	      {
		      var prop = this.getCurrentProperty(propEnum);
		      if (prop)
		      {
		         searchOption = !(prop.isSuppressed);
		      }
		      else
		      {
		         searchOption = false;
		      }
		   }
	      else
	      {
		      searchOption = false;
	      }

	   }
	   if ( exclValid == 1)
	   {
	      // Check the validity of the property and if valid then DON'T show
	   }
		if ( ( stringMatch != -1 || SearchBox.value == "*") && searchOption )
		{
			var element = this.getElement(document, "WB:SEARCHRESULT", this.index[i].text, this.searchResultClicked)
			element.node = this.index[i].node;
			SearchResults.appendChild(element);
		}
	}
}
