riptEngine;
var helper = null;
//var sigDigits = 5;
var numSteps;
var indexRow;
var indexCol;
var propAcc = null;

//Horizontal Text Alignment
var TE_HTA_LEFT   = 0;
var TE_HTA_RIGHT  = 1;
var TE_HTA_CENTER = 2;

//Grid cell edit styles
var DGC_ES_EDITBOX = 0;
var DGC_ES_COMBOBOX_DROPDOWNLIST = 1;
var DGC_ES_COMBOBOX_DROPDOWN = 2;

//Border style
var ccNone = 0;
var ccFixedSingle = 1;
var PI = 3.14159265358979323846;

function window_onload()
{
	var wbTemp = window.external;
	wb = wbTemp;
	ds = wbTemp.AppletList.Applet("DSApplet").App;
	obj = ds.Tree.GetObject(ds.Tree.FirstActiveObjectID);
	stringTable = window.external.AppletList.WBApplet("DSApplet").StringTable;
	scriptEngine = wb.ScriptEngine.Engine;

	if(ds.GUIState && ds.GUIState.GUIDriver)
		ds.GUIState.GUIDriver.MenuListener.AddListener(3, this);

	lvChangeCB = ds.Script.lv.OnChange;
	lvChangeCB.AddCOM(this, "lv_ChangeDelegate");
	processStateCB = ds.OnProcessingStateFinished;
	processStateCB.AddCOM(this, "Processing_State_FinishedDelegate");

	setTimeout("initializePage()", 250);
}

function initializePage()
{
    pageName.firstChild.nodeValue = obj.Name;
	
	SetVars();		
	SetGraphProps();
	SetGraphType();
	SizeGraph();		//size the graph by the window size
	ds.Script.CheckIfWorksheetInReport();
}

function simplerandom(seed)
{
    if (!seed)
    {
        seed = new Date().getTime();
    }
    return seed = (seed*9301+49297) % 233280;
    //return seed/(233280.0);
}

function SetGraphProps()
{
	Graph1.BackColor = wb.GetWindowColor();
	Graph1.Legend.Visible = false;
	Graph1.TitleClear();
	Graph1.Axes(0).LabelBold = true;		//make the x-axis title bold
	Graph1.Axes(1).LabelBold = true;		//make the y-axis title bold
	Graph1.Axes(2).LabelBold = true;		//make the z-axis title bold
	Graph1.AxesLabelPointSize = 16;			//set font size for the axis labels
	Graph1.Axes(0).LabelPointSize = 14;		//set font size for the axis labels
	Graph1.Axes(1).LabelPointSize = 14;		//set font size for the axis labels
	Graph1.ShowTriPlanes = false;
	Graph1.HistogramBarSizeAutomatic = false; 
	Graph1.HistogramBarSizeX = .9;
	Graph1.HistogramBarSizeY = .9;
	Graph1.CameraZoomFactor = 1.4;
    Graph1.Axes(1).LabelFieldSignificantDigits = ds.Script.DSSigDigitsForGraph;
    Graph1.Axes(0).LabelFieldSignificantDigits = ds.Script.DSSigDigitsForGraph;
}

function SetVars()
{    
	lowerVariation = obj.LowerVariation;
	upperVariation = obj.UpperVariation;
}

function Set2DGraphVals()
{
	axisScale2d = obj.AxisScaleType;
 	
	Graph1.Datasets.Clear();						//clear any data
	
	Graph1.Axes(0).AutomaticLimits = true;
	Graph1.Axes(1).AutomaticLimits = true;
	Graph1.Axes(0).Logarithmic = false;
	Graph1.Axes(1).Logarithmic = false;
	Graph1.PickLines = true;
	
	var rand = 160000;
	var scale;
	
	if( obj.State != 5 )
	{
		// clear graph
		Graph1.Clear();
		
		// clear table
		CampbellDiagramDataGrid.RowsCount = 1;
		CampbellDiagramDataGrid.ColumnsCount = 1;            
		CampbellDiagramDataGrid.DeleteAllRows();
		CampbellDiagramDataGrid.Refresh();
		
		return true;
	}
	
	Graph1.Legend.Visible = true;
	var b = 255;
	var g = 225;
	var r = 219;
	
	b = 255;
	g = 255;
	r = 255;
	
	Graph1.BackColor = b*256*256 + g * 256 + r;

	var xmin = obj.CampbellXAxisMin;
	var xmax = obj.CampbellXAxisMax;
	var ymin = obj.CampbellYAxisMin;
	var ymax = obj.CampbellYAxisMax;
	var numMode = obj.CampbellNumMode;
	var numRotVel = obj.CampbellNumRotVel;
	var yDataType = obj.CampbellYAxisDataType;

	Graph1.Axes(0).AutomaticLimits = false;
	Graph1.Axes(0).ImposedLimitsMin = xmin;
	Graph1.Axes(0).ImposedLimitsMax = xmax;
	
	Graph1.Axes(1).AutomaticLimits = false;
	Graph1.Axes(1).ImposedLimitsMin = ymin;
	Graph1.Axes(1).ImposedLimitsMax = ymax;
	
	var icount = 0;
	var ExcitationRatio = 0;
	var iTotalLine = 0;
	var ix = 0;
	var iy = 0;
	var iz = 0;
	var iwidth = 0;
	var iWhirl = 0;
	var iStabilityKey = 0;
	var bi, gi, ri;
	
	if( obj.CampbellExcitationRatio > 0.0 && obj.CampbellYAxisDataType == 0 )
	{
		iTotalLine = numMode + 1;
	}
	else
	{
		iTotalLine = numMode;
	}
	
	for(var imode = 0; imode < iTotalLine; imode++)
	{
		Graph1.Datasets.Add();
		
		if( imode < numMode )
		{
			iWhirl = obj.CampbellWhirlVal(imode);
			iStabilityKey = obj.CampbellStabilityKey(imode);
			//iwidth = 2;
			
			rand = simplerandom(rand)
			scale = rand/(233280);
			b = scale*256;
			
			rand = simplerandom(rand)
			scale = rand/(233280);
			g = scale*256;
			
			rand = simplerandom(rand)
			scale = rand/(233280);
			r =scale*256;
			//b = (imode+1)*10;
			//g = (imode+1)*40;
			//r = (imode+1)*70;
			
			iwidth = scale*3;
		}
		else
		{
			iWhirl = 2;
			iwidth = 4;
			b = 0;
			g = 0;
			r = 0;
		}
		
		//Graph1.Datasets(imode).Line2DType=0;
		//Graph1.Datasets(imode).PointStyle = 2;
		Graph1.Datasets(imode).PointColor = b*256*256 + g * 256 + r;
		Graph1.Datasets(imode).LineColor = b*256*256 + g * 256 + r;
		Graph1.Datasets(imode).LineWidth = iwidth;
		if( iWhirl == -1 )
		{
			if( iStabilityKey == -1 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartDirectionBW") + " " + " - " + stringTable.String("ID_CampbellChartUNSTABLE");
			}
			else if( iStabilityKey == 1 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartDirectionBW") + " " + " - " + stringTable.String("ID_CampbellChartSTABLE");
			}
			else if( iStabilityKey == 0 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartDirectionBW") + " " + " - " + stringTable.String("ID_CampbellChartUNDETERMINED");
			}
		}
		else if( iWhirl == 1 )
		{
			if( iStabilityKey == -1 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartDirectionFW") + " " + " - " + stringTable.String("ID_CampbellChartUNSTABLE");
			}
			else if( iStabilityKey == 1 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartDirectionFW") + " " + " - " + stringTable.String("ID_CampbellChartSTABLE");
			}
			else if( iStabilityKey == 0 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartDirectionFW") + " " + " - " + stringTable.String("ID_CampbellChartUNDETERMINED");
			}
		}
		else if( iWhirl == 0 )
		{
			if( iStabilityKey == -1 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartUNDETERMINED") + " " + " - " + stringTable.String("ID_CampbellChartUNSTABLE");
			}
			else if( iStabilityKey == 1 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartUNDETERMINED") + " " + " - " + stringTable.String("ID_CampbellChartSTABLE");
			}
			else if( iStabilityKey == 0 )
			{
				Graph1.Datasets(imode).Label = stringTable.String("ID_Mode") + " - " + (imode + 1) + " " + " - " + stringTable.String("ID_CampbellChartUNDETERMINED") + " " + " - " + stringTable.String("ID_CampbellChartUNDETERMINED");
			}
		}
		else if( iWhirl == 2 )
		{
			var ratio = obj.CampbellExcitationRatio;
			Graph1.Datasets(imode).Label = stringTable.String("ID_CampbellChartRATIO") + " = " + ratio;
		}
		
		if( obj.CampbellYAxisDataType == 0 )
		{
			for(var i = 0; i < numRotVel; i++)
			{
				Graph1.Datasets(imode).Add(obj.XVal(icount), obj.YVal(icount), imode);  //since it's a 2d graph, z val is always zero
				icount = icount + 1;
			}
		}
		else if ( obj.CampbellYAxisDataType == 1 )
		{
			for(var i = 0; i < numRotVel; i++)
			{
				Graph1.Datasets(imode).Add(obj.XVal(icount), obj.YValImg(icount), imode);  //since it's a 2d graph, z val is always zero
				icount = icount + 1;
			}
		}
		else if ( obj.CampbellYAxisDataType == 2 )
		{
			var LogDecre = 0.0;
			for (var i = 0; i < numRotVel; i++) 
            {
                if (obj.YVal(icount) != 0.0) 
                {
                    LogDecre = (2.0 * PI * obj.YValImg(icount)) / obj.YVal(icount);
                    Graph1.Datasets(imode).Add(obj.XVal(icount), LogDecre, imode);  //since it's a 2d graph, z val is always zero		        
                }
                else
                    Graph1.Datasets(imode).Line2DType = 0;

				icount = icount + 1;
			}
        }
        else if ( obj.CampbellYAxisDataType == 3 )
        {
            var ModalDampingRatio = 0.0;
            for (var i = 0; i < numRotVel; i++)
            {
                ModalDampingRatio = -obj.YValImg(icount) / (Math.sqrt(Math.pow(obj.YValImg(icount), 2) + Math.pow(obj.YVal(icount), 2)));

                // replacing negative zero with positive zero
                ModalDampingRatio = ModalDampingRatio + 0;

                Graph1.Datasets(imode).Add(obj.XVal(icount), ModalDampingRatio, imode);  //since it's a 2d graph, z val is always zero
                icount = icount + 1;
            }
        }
	}
	
	// critical speed
	if( obj.CampbellExcitationRatio > 0.0 && obj.CampbellYAxisDataType == 0 )
	{
		Graph1.Datasets.Add();
		Graph1.Datasets(iTotalLine).Line2DType=0;
		Graph1.Datasets(iTotalLine).PointStyle = 2;
		Graph1.Datasets(iTotalLine).PointSize = 15;
		Graph1.Datasets(iTotalLine).Label = stringTable.String("ID_CampbellChartCRITICALSPEED");
	
		var iCrtSpdCount = 0;
		for(var iMode=0; iMode<numMode; iMode++)
		{
			var val = obj.CampbellCriticalSpeedFreq(iMode);
			
			 if( val > 0.0 )
//                    {
//                        iCrtSpdCount = iCrtSpdCount + 1;
//                    }
//                }
//                
//                for(var iCrtSpd=0; iCrtSpd<iCrtSpdCount; iCrtSpd++)
//                {
			Graph1.Datasets(iTotalLine).Add(obj.CampbellCriticalSpeed(iMode), obj.CampbellCriticalSpeedFreq(iMode), iTotalLine);  //since it's a 2d graph, z val is always zero
		}
	}
	
	// set critical speed table
	//setCriticalSpeedList();
	
	// set Campbell table
	setCampbellDataList();	
	
	Set2DAxisScale(axisScale2d);	
	Graph1.Draw();
}

function Set2DGraphLabels()
{
	var xUserDefinedLabel = " ";
	var yUserDefindeLabel = " ";
	xUserDefinedLabel = obj.CampbellXAxisLabel;
	yUserDefinedLabel = obj.CampbellYAxisLabel;
	Graph1.Axes(0).Label = " " + xUserDefinedLabel + " (" + ds.UnitSystem.UnitString(ds.Script.id_utANGULAR_VELOCITY) + ") ";
	
	// frequency or stability
	if( obj.CampbellYAxisDataType == 0 || obj.CampbellYAxisDataType == 1 )
	{
		Graph1.Axes(1).Label = " " + yUserDefinedLabel + " (" + ds.UnitSystem.UnitString(ds.Script.id_utFREQUENCY) + ") ";
    }
    // logarithmic decrement or modal damping ratio
    else if (obj.CampbellYAxisDataType == 2 || obj.CampbellYAxisDataType == 3 )
	{
		Graph1.Axes(1).Label = " " + yUserDefinedLabel;
	}
}

function Set2DAxisScale(scale)
{
	//find the min/max x and y values in the graph
	var count = Graph1.Datasets.Count;
	
	if(count>0)
	{
		if(Graph1.Datasets(0).Count > 0)
		{
			var ymax = Graph1.Datasets(0).Max(1);
			var ymin = Graph1.Datasets(0).Min(1);
			var xmax = Graph1.Datasets(0).Max(0);
			var xmin = Graph1.Datasets(0).Min(0);
		}
	}
	
	if(count > 1)
	{
		for(var i=1; i<count; i++)
		{
			if(Graph1.Datasets(i).Count > 0)
			{
				ymax = Math.max(ymax, Graph1.Datasets(i).Max(1));
				ymin = Math.min(ymin, Graph1.Datasets(i).Min(1));
				xmax = Math.max(xmax, Graph1.Datasets(i).Max(0));
				xmin = Math.min(xmin, Graph1.Datasets(i).Min(0));
			}
		}
	}
	
	var diff = ymax - ymin;
	Graph1.Axes(1).Divisions = 10;
	Graph1.Axes(0).Divisions = 5;
	Graph1.Axes(1).LabelFieldPrecision = 3;
	Graph1.Axes(0).LabelFieldPrecision = 3;
	
	if(Math.abs(ymax) >= 1000 || Math.abs(ymin) >= 1000 || diff < .01)
		Graph1.Axes(1).LabelUseExponential = true;
		
	switch(scale)
	{
		case 0:  //linear
			Graph1.Axes(0).Logarithmic = false;
			Graph1.Axes(1).Logarithmic = false;
			break;
		case 1:  //x axis is log, y axis linear
			Graph1.Axes(1).Logarithmic = false;
			if(lowerVariation > 0)
				Graph1.Axes(0).Logarithmic = true;
			else
				Graph1.Axes(0).Logarithmic = false;
			break;
		case 2: //y axis is log, x axis linear
			Graph1.Axes(0).Logarithmic = false;
			allZero = CheckForAllZeroValues();
			if(allZero == true)
			{
				Graph1.Axes(1).Logarithmic = false;
				Graph1.Datasets.Clear();
				Graph1.Datasets.Add();
				Graph1.Datasets(0).LineColor = color(255,0,0);
				Graph1.Datasets(0).PointColor = color(255,0,0);
							
				for(i=0; i<obj.NumX; i++)
					Graph1.Datasets(0).Add(obj.XVal(i), obj.YVal(i), 0);
			}
			else
				Graph1.Axes(1).Logarithmic = true;
			break;
		case 3:  //log-log
			if(lowerVariation > 0)
				Graph1.Axes(0).Logarithmic = true;
			else
				Graph1.Axes(0).Logarithmic = false;

			allZero = CheckForAllZeroValues();
			if(allZero == true)
			{
				//if all the points are zero, force y-axis to be linear and plot all of the points
				Graph1.Axes(1).Logarithmic = false;		
				Graph1.Datasets.Clear();
				Graph1.Datasets.Add();
				Graph1.Datasets(0).LineColor = color(255,0,0);
				Graph1.Datasets(0).PointColor = color(255,0,0);
				for(i=0; i<obj.NumX; i++)
					Graph1.Datasets(0).Add(obj.XVal(i), obj.YVal(i), 0);
			}
			else
				Graph1.Axes(1).Logarithmic = true;
			break;
		default:
			break;
	}

	if(Graph1.Axes(0).Logarithmic == true)
	{
		Graph1.Axes(0).LabelUseExponential = true;
		Graph1.Axes(0).LabelFieldPrecision = 1;
	}
	if(Graph1.Axes(1).Logarithmic == true)
	{
		Graph1.Axes(1).LabelUseExponential = true;
		Graph1.Axes(1).LabelFieldPrecision = 2;
	}	
	
	//if there is only one point, or if all points have the same value,
	//don't allow the user to click on the line to get a value
	if((xmin == xmax) && (ymin == ymax))
		Graph1.PickLines = false;
}

function CheckForAllZeroValues()
{
	for(var z=0; z<obj.NumY; z++)
	{
		if(obj.YVal(z) != 0)
			return false;
	}
	return true;
}

function SetGraphType()
{
	//use a 2d line graph
	Graph1.style.display = "inline";
	Graph1.Type = 4;		//2d line graph
	Graph1.Line2DType = 2;	//line with points
	Set2DGraphVals();
	Set2DGraphLabels();
}
function color(red, green, blue)
{
	return red + (256*green) + (65536*blue);
}

function window_onunload()
{
	obj = null;
	
	if(ds && ds.GUIState && ds.GUIState.GUIDriver)
        ds.GUIState.GUIDriver.MenuListener.RemoveListener(3);

	if(lvChangeCB)
	{
		lvChangeCB.RemoveCOM(this, "lv_ChangeDelegate");
		lvChangeCB = null;
	}
	if(processStateCB)
	{
	    processStateCB.RemoveCOM(this, "Processing_State_FinishedDelegate");
		processStateCB = null;
	}
}

function lv_ChangeDelegate(sender, args)
{
    lv_Change();
}

function lv_Change()
{
	SetGraphType();
}

function Processing_State_FinishedDelegate(sender, args)
{
	Processing_State_Finished(args.Type);
}

function Processing_State_Finished(processCode)
{
	if(processCode == 2)
	{
		lowerVariation = obj.LowerVariation;
		SetGraphType();  //histogram or xyplots
	}
}

function window_onresize() 
{
	SizeGraph();
}

function SizeGraph()
{
    pageWidth = document.body.offsetWidth;
    pageHeight = document.body.offsetHeight;
	if( obj.ResultChartType == ds.Script.id_RotordynamicCampbellDiagram )
	{
	    graphWidth = parseInt(pageWidth*.90);
	    graphHeight = parseInt(pageHeight * .80);
	    gridWidth = parseInt(pageWidth * .90);
	    gridHeight = parseInt(pageHeight * .20);
	    CampbellDiagramDataGrid.style.width = gridWidth;
	    CampbellDiagramDataGrid.style.height = gridHeight;	    
	}
	else
	{
	    graphWidth = parseInt(pageWidth*.85);
	    graphHeight = parseInt(graphWidth*.9);
	}
	Graph1.style.height = graphHeight;
	Graph1.style.width = graphWidth;
}

function WriteGraphToFile(imageMode, fileName)
{
	var height = parseInt(Graph1.style.height);
	var width = parseInt(Graph1.style.width);
	
	switch( imageMode )
	{
		case 0:  //png
			Graph1.WritePngFile( fileName, width, height, true );
			break;
			
		case 1:  //jpg
			Graph1.WriteJpgFile( fileName, width, height, true );
			break;
			
		case 3:  //bmp
			Graph1.WriteBmpFile( fileName, width, height, true );
			break;
			
	    default :
			break;
	}
}

function setCriticalSpeedList()
{
    var ccFixedSingle = 1;
    var helper = ds.Script.WBUtilsHelper;
    var sigDigits = ds.AccessControl.ValueLength;
    
    var numMode = obj.CampbellNumMode;
    
    CriticalSpeedDataGrid.RowsCount = numMode+1;
    CriticalSpeedDataGrid.ColumnsCount = 2;
    CriticalSpeedDataGrid.FixedRowsCount = 1;
    CriticalSpeedDataGrid.FixedColumnsCount = 0;
    
    CriticalSpeedDataGrid.ColumnWidth(1) = 120;
    var cell = CriticalSpeedDataGrid.Cell(1,1);
    cell.HorizontalTextAlignment = TE_HTA_CENTER;
    cell.Text = stringTable.String("ID_Mode");
    
    CriticalSpeedDataGrid.ColumnWidth(2) = 120;
    var cell = CriticalSpeedDataGrid.Cell(1,2);
    cell.HorizontalTextAlignment = TE_HTA_CENTER;
    cell.Text = stringTable.String("ID_CampbellExcitationOption");
    
    // fill in mode numbers
    var iTotalRow = numMode+1;
    for(var iMode=2; iMode<=iTotalRow; iMode++)
    {
        var cell = CriticalSpeedDataGrid.Cell(iMode, 1);
        cell.HorizontalTextAlignment = TE_HTA_CENTER;
        var val = iMode-1;
        val = helper.GetFixedLengthFormat(val, sigDigits);
        cell.Text=val;
        
        var cell = CriticalSpeedDataGrid.Cell(iMode, 2);
        cell.HorizontalTextAlignment = TE_HTA_CENTER;
        //var val = iMode-1;
        //val = helper.GetFixedLengthFormat(val, sigDigits);
        cell.Text = stringTable.String("ID_CampbellChartNONE");
    }
    
    pageDone = true;
}

function setCampbellDataList()
{
    var ccFixedSingle = 1;
    var helper = ds.Script.WBUtilsHelper;
    var sigDigits = ds.AccessControl.ValueLength;
    
    var numMode = obj.CampbellNumMode;
	var numRotVel = obj.CampbellNumRotVel;
	
	CampbellDiagramDataGrid.AllowEdit = 0;
    
    CampbellDiagramDataGrid.RowsCount = numMode+1;
    CampbellDiagramDataGrid.ColumnsCount = numRotVel+1+1+1+1;
    CampbellDiagramDataGrid.FixedRowsCount = 1;
    CampbellDiagramDataGrid.FixedColumnsCount = 0;
    
    CampbellDiagramDataGrid.ColumnWidth(1) = 100;
    var cell = CampbellDiagramDataGrid.Cell(1,1);
    cell.HorizontalTextAlignment = TE_HTA_CENTER;
    cell.Text = stringTable.String("ID_Mode");
    
    CampbellDiagramDataGrid.ColumnWidth(2) = 100;
    var cell = CampbellDiagramDataGrid.Cell(1,2);
    cell.HorizontalTextAlignment = TE_HTA_CENTER;
    cell.Text = stringTable.String("ID_CampbellChartWhirlDirection");
    
    CampbellDiagramDataGrid.ColumnWidth(3) = 100;
    var cell = CampbellDiagramDataGrid.Cell(1,3);
    cell.HorizontalTextAlignment = TE_HTA_CENTER;
    cell.Text = stringTable.String("ID_CampbellChartModeStability");
    
    CampbellDiagramDataGrid.ColumnWidth(4) = 100;
    var cell = CampbellDiagramDataGrid.Cell(1,4);
    cell.HorizontalTextAlignment = TE_HTA_CENTER;
    cell.Text = stringTable.String("ID_CampbellExcitationOption");
    
    // fill in rotational velocities
    var iTotalCol = numRotVel+1+1+1+1;
    for(var iRotVel=5; iRotVel<=iTotalCol; iRotVel++)
    {
        CampbellDiagramDataGrid.ColumnWidth(iRotVel) = 100;
        var cell = CampbellDiagramDataGrid.Cell(1, iRotVel);
        cell.HorizontalTextAlignment = TE_HTA_CENTER;
        var val = obj.XVal(iRotVel-5);
        val = helper.GetFixedLengthFormat(val, sigDigits);
        cell.Text=val + " " + ds.UnitSystem.UnitString(ds.Script.id_utANGULAR_VELOCITY);
    }
    
    // fill in mode numbers
    var iTotalRow = numMode+1;
    var icount = 0;
    for(var iMode=2; iMode<=iTotalRow; iMode++)
    {
        // mode number
        var cell = CampbellDiagramDataGrid.Cell(iMode, 1);
        cell.HorizontalTextAlignment = TE_HTA_CENTER;
        var val = iMode-1;
        val = helper.GetFixedLengthFormat(val, sigDigits);
        cell.Text=val;
	    
	    // whirl direction
	    var cell = CampbellDiagramDataGrid.Cell(iMode, 2);
        cell.HorizontalTextAlignment = TE_HTA_CENTER;
        var val = obj.CampbellWhirlVal(icount);
        if( val == 1 )
        {
            cell.Text = stringTable.String("ID_CampbellChartDirectionFW");
        }
        else if( val == -1 )
        {
            cell.Text = stringTable.String("ID_CampbellChartDirectionBW");
        }
        else
        {
            cell.Text = stringTable.String("ID_CampbellChartUNDETERMINED");
        }
        
        // mode stability
	    var cell = CampbellDiagramDataGrid.Cell(iMode, 3);
        cell.HorizontalTextAlignment = TE_HTA_CENTER;
        var val = obj.CampbellStabilityKey(icount);
        if( val == 1 )
        {
            cell.Text = stringTable.String("ID_CampbellChartSTABLE");
        }
        else if( val == -1 )
        {
            cell.Text = stringTable.String("ID_CampbellChartUNSTABLE");
        }
        else if( val == 0 )
        {
            cell.Text = stringTable.String("ID_CampbellChartUNDETERMINED");
        }
        
        // critical speed
        if( obj.CampbellExcitationRatio > 0.0 )
        {
            var cell = CampbellDiagramDataGrid.Cell(iMode, 4);
            cell.HorizontalTextAlignment = TE_HTA_CENTER;
            var val = obj.CampbellCriticalSpeed(icount);
            val = helper.GetFixedLengthFormat(val, sigDigits);
            var YAxisDataType = obj.CampbellYAxisDataType;
            
            // show NONE if zero, stability, logarithmic decrement or modal damping ratio
            if (val == 0.0 || YAxisDataType == 1 || YAxisDataType == 2 || YAxisDataType == 3)
            {
                cell.Text = stringTable.String("ID_CampbellChartNONE"); ;
            }
            else
            {
                cell.Text=val + " " + ds.UnitSystem.UnitString(ds.Script.id_utANGULAR_VELOCITY);;
	        }
	    }
	    else
	    {
	        var cell = CampbellDiagramDataGrid.Cell(iMode, 4);
            cell.HorizontalTextAlignment = TE_HTA_CENTER;
            cell.Text = stringTable.String("ID_CampbellChartNONE");
	    }
        
	    icount = icount + 1;
    }
    
    // get Y-axis data type
    var yDataType = obj.CampbellYAxisDataType;
    
    // fill in frequencies
    icount = 0;
    for( var iMode=1; iMode<=numMode; iMode++)
    {
        for( var iRotVel=4; iRotVel<iTotalCol; iRotVel++)
        {
            if( obj.CampbellYAxisDataType == 0 )
            {
                var val = obj.YVal(icount);
            }
            else if( obj.CampbellYAxisDataType == 1 )
            {
                var val = obj.YValImg(icount);
            }
            else if( obj.CampbellYAxisDataType == 2 )
            {
                var val = (2.0*PI*obj.YValImg(icount))/obj.YVal(icount);
            }
            else if (obj.CampbellYAxisDataType == 3)
            {
                var val = -obj.YValImg(icount) / (Math.sqrt(Math.pow(obj.YValImg(icount), 2) + Math.pow(obj.YVal(icount), 2)));

                // replacing negative zero with positive zero
                val = val + 0;
            }
            var cell = CampbellDiagramDataGrid.Cell(iMode+1, iRotVel+1);
            cell.HorizontalTextAlignment = TE_HTA_CENTER;
            val = helper.GetFixedLengthFormat(val, sigDigits);
            
            // not to print Hz for logarithmic decrement
            if (yDataType != 2 && yDataType != 3)
            {
                cell.Text=val + " " + ds.UnitSystem.UnitString(ds.Script.id_utFREQUENCY);
            }
            else
            {
				if(isFinite(val))
                {
					cell.Text = val;
				}
				else
				{
					cell.Text = stringTable.String("ID_NotApplicable");
				}
            }
	        icount = icount + 1;
        }
    }
    
    CampbellDiagramDataGrid.Refresh();
    
    pageDone = true;
}

function exportDataToFile(fileName)
{
    var fileText = createTextForFile();
    ds.Script.WriteTextInFile(fileName, fileText);
}

function createTextForFile()
{
    var fileText = "";

    var numRows = CampbellDiagramDataGrid.RowsCount;
    var numCols = CampbellDiagramDataGrid.ColumnsCount;

    var cell = CampbellDiagramDataGrid.Cell(1, 1);

    for (var iRow = 1; iRow <= numRows; iRow++) {
        for (var iCol = 1; iCol <= numCols; iCol++) {
            cell = CampbellDiagramDataGrid.Cell(iRow, iCol);
            fileText += cell.Text;
            fileText += "\t";
        }
        fileText += "\n";
    }

    return fileText;
}

function refreshPage()
{
    SetGraphType();
    Set2DGraphVals();
}

//-->
            </SCRIPT>
    </head>
    <body LANGUAGE="javascript" onload="return window_onload()" onunload="return window_onunload()"
        onresize="return window_onresize()">        
        <DIV class="gradient">
            <H1 id="pageName">&nbsp;</H1>
        </DIV>
        <table width="96%" height="95%" cellspacing="30">
            <tr>
                <td width="80%" height="70%">
                    <OBJECT id="Graph1" classid="clsid:F113A120-C83C-47B5-847C-179B339C42B7" width="100%" height="100%" VIEWASTEXT></OBJECT>
                </td>
                <!--
                <td width="20%">
                    <OBJECT id=CriticalSpeedDataGrid classid="CLSID:0B696001-60F3-4E4D-8EDD-E9EAE6DBDF12" width="100%" height="100%" VIEWASTEXT></OBJECT>
                </td>
                -->
            </tr>
            <tr>
                <td colspan=2 height="30%"><OBJECT id=CampbellDiagramDataGrid classid="CLSID:0B696001-60F3-4E4D-8EDD-E9EAE6DBDF12" width="100%" height="100%" VIEWASTEXT></OBJECT></td>
            </tr>
        </table>
    </body>
</html>
