quency,duration, arrLineColor, numLineColors;
var numPoints;
var ver = 5;
var scriptEngine;

function window_onload()
{
	var wbTemp = window.external;
	wb = wbTemp;
	ds = wbTemp.AppletList.Applet("DSApplet").App;
	phaseObj = 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);
	}

	ver = msieversion();

	if(ver >= 5)
	{
	    pageName.firstChild.nodeValue = phaseObj.Name;
	}
	else
	{
	    pageName.innerText = phaseObj.Name;
	}

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

	if(phaseObj.HasResults == true)
		Graph1.style.display = "inline";

	SetVars();
	SetGraphProps();
	SetGraphVals();

	SizeGraph();		//size the graph by the window size
	
	ds.Script.CheckIfWorksheetInReport();
}

function msieversion()
{
  var ua = window.navigator.userAgent
  var msie = ua.indexOf ( "MSIE " )

  if ( msie > 0 )      // If Internet Explorer, return version number
     return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
  else                 // If another browser, return 0
     return 0

}

function SetVars()
{
	frequency    = phaseObj.RequestedFrequency;
	duration     = phaseObj.Duration;
	xexpProgram1 = false;
	yexpProgram1 = false;
	numPoints	 = 100;
	
	numLineColors    = 12;
	arrLineColor     = new Array(numLineColors);
	arrLineColor[0]  =  color(0,   0,   255);      // blue
	arrLineColor[1]  =  color(255, 0,   255);    // magenta
	arrLineColor[2]  =  color(0,   255, 0);      // green
	arrLineColor[3]  =  color(0,   255, 255);    // cyan
	arrLineColor[4]  =  color(255, 255, 0);    // yellow
	arrLineColor[5]  =  color(0,   0,   0);
	arrLineColor[6]  =  color(172, 172, 172);
	arrLineColor[7]  =  color(255, 128, 64);
	arrLineColor[8]  =  color(0,   128, 0);
	arrLineColor[9]  =  color(128, 0,   64);
	arrLineColor[10] = color(128, 0,   128);
	arrLineColor[11] = color(0,   128, 128);
}

function color(red, green, blue)
{
	//returns an rgb color value
	return red + (256*green) + (65536*blue);
}

function SetGraphProps()
{
	Graph1.Legend.Alignment = 1;
	Graph1.ZoomPanCtrlEnable = false;
	Graph1.BackColor = wb.GetWindowColor();
	Graph1.Legend.Visible = true;
	Graph1.Legend.FrameColor = wb.GetWindowColor();
	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.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.Type = 4;						//2d line type
    Graph1.Axes(1).LabelFieldSignificantDigits = ds.Script.DSSigDigitsForGraph;
    Graph1.Axes(0).LabelFieldSignificantDigits = ds.Script.DSSigDigitsForGraph;
	//Graph1.Line2DType = 1;					//lines only
	
	restoreDefaultsGraph();
	restoreSavedProperties();
}

function SetGraphVals()
{
	Graph1.Datasets.Clear();
	//the first dataset is to hold values from the phase response item
	Graph1.Datasets.Add();
	Graph1.Datasets(0).Line2DType = 1;	

	//set the axis labels
	xlabel = stringTable.String("ID_SweepingPhase");
	if(ShouldShowDegrees())
		xlabel += " (" + stringTable.String("ID_Degrees") + ")";
	else
		xlabel += " (" + ds.UnitSystem.UnitString(ds.Script.id_utANGLE) + ")";  //angle units (radians)
	
	Graph1.Axes(0).Label = xlabel;
	
	ylabel = stringTable.String("ID_EnvironmentInsertDisplacement")
	ylabel += " (" + ds.UnitSystem.UnitString(ds.Script.id_utLENGTH) + ")";
	if(AllDisplacements() == true)
		Graph1.Axes(1).Label = ylabel;
	else
		Graph1.Axes(1).Label = "";

	if(phaseObj.HasResults == true)
	{
		//add data to the first dataset based on the results
		var phaseAngle, amplitude, angle, yVal;
		var showDegree = ShouldShowDegrees();
		var allDisp	   = AllDisplacements();
		
		var currentDuration = duration;
		var phaseAngle = phaseObj.PhaseAngleAtMaxAmp;
		if(showDegree == false)
			phaseAngle = phaseAngle*180/Math.PI;

		var amplitude  = phaseObj.MaxAmplitude;
		//we want to plot 100 points, so set increment
        if( showDegree == false )
            currentDuration = currentDuration*180/Math.PI;
		var increment  = currentDuration/numPoints;
            
		for(var i=0; i<=currentDuration; i=i+increment)
		{
			angle = i + (90.0 - phaseAngle);
			angle *= Math.PI/180.0;				//convert to radians
			
			if(allDisp == true)
				yVal  = amplitude * Math.sin(angle);
			else
			    yVal = Math.sin(angle);
			
			if (amplitude == 0)
			    yVal = 0;    
			
			var xVal = i;
			if( showDegree == false )  //convert i to radians
				xVal = xVal*Math.PI/180.0;
			Graph1.Datasets(0).Add(xVal, yVal, 0);
		}
		//make sure the last point is added
		angle = currentDuration + (90.0 - phaseAngle);
			angle *= Math.PI/180.0;				//convert to radians
		if(allDisp == true)
			yVal  = amplitude * Math.sin(angle);
		else
		    yVal = Math.sin(angle);
		
		if (amplitude == 0)
		    yVal = 0;
			
		var xVal = currentDuration;
		if( showDegree == false )  //convert i to radians
			xVal = xVal*Math.PI/180.0;
		Graph1.Datasets(0).Add(xVal, yVal, 0);
		
		Graph1.Datasets(0).Label = stringTable.String("ID_ResultChartOutput");
		Graph1.Datasets(0).LineWidth = 3;
		
		//now add any valid loads to the graph
		AddLoads();
	}

	SetAxisFormat();
	Graph1.Draw();
}

function AddLoads()
{
	//find the environment node in the tree
	var branch = ds.Tree.FirstActiveBranch;
	var plotCosCurve = false;
	if(branch)
	{
		environment = branch.Environment;
		if(environment)
		{
			var lineIndex = 1;	//the 0 index refers to the line for the phase response, the next index will be 1
			var colorIndex = 0;
			var envSimType = environment.SimulationType;
			var harmLoads = environment.HarmonicLoads;
			var count = harmLoads.Count;
			if (count > 0) 
			{
			    var maxAmp = MaxAmpInLoads(harmLoads);
			    var maxAmpPressure = MaxAmpInLoadsPressure(harmLoads);
			    var maxAmpForce = MaxAmpInLoadsForce(harmLoads);
			    var maxAmpDisplacement = MaxAmpInLoadsDisplacement(harmLoads);
			}
			var showDegree = ShouldShowDegrees();
			var allDisp	   = AllDisplacements();
			for(var j=1; j<=count; j++)
			{
				curLoad = harmLoads(j);
				if(curLoad)
				{
					if(envSimType == ds.Script.id_SimTypeHarmonic && curLoad.Suppressed == false)	//harmonic, not suppressed
					{
						var loadType = curLoad.LoadType;
						if( loadType == ds.Script.id_EdgeForce ||
							loadType == ds.Script.id_VertexForce ||
							loadType == ds.Script.id_SurfaceDisplacement ||
							loadType == ds.Script.id_EdgeDisplacement ||
							loadType == ds.Script.id_VertexDisplacement ||
							loadType == ds.Script.id_SurfaceForce ||
							loadType == ds.Script.id_SurfacePressure )
						{
							Graph1.Datasets.Add();
							Graph1.Datasets(lineIndex).Line2DType = 1;
						    var currentDuration = duration;
						    
						    var requestedFrequency = phaseObj.RequestedFrequency;
							var loadAmplitudePhase = curLoad.AmplitudePhaseAtFreq(requestedFrequency);		
							var amplitude = 0.0;
							var phaseAngle = 0.0;							
							if (loadAmplitudePhase != null) 
							{
								amplitude = loadAmplitudePhase(1);
								phaseAngle = loadAmplitudePhase(2);		
							}
							if(showDegree == false)
							    phaseAngle = phaseAngle * 180 / Math.PI;

							
							//we want to plot 100 points, so set increment
							if( showDegree == false )
								currentDuration = currentDuration*180/Math.PI;
							var increment  = currentDuration/numPoints;
							for(var i=0; i<=currentDuration; i=i+increment)
							{
								angle = i + (90.0 - phaseAngle);
								angle *= Math.PI/180.0;				//convert to radians

								if (allDisp == true)
								    yVal = amplitude * Math.sin(angle);
								else if (maxAmp != 0) 
								{
								    if (loadType == ds.Script.id_SurfacePressure)
								        yVal = amplitude / maxAmpPressure * Math.sin(angle);
								    else if (loadType == ds.Script.id_EdgeForce || loadType == ds.Script.id_VertexForce || loadType == ds.Script.id_SurfaceForce)
								        yVal = amplitude / maxAmpForce * Math.sin(angle);
								    else
								        yVal = amplitude / maxAmpDisplacement * Math.sin(angle);
								}
								else
								    yVal = 0;
								var xVal = i;
								if( showDegree == false )  //convert i to radians
									xVal = xVal*Math.PI/180.0;
								Graph1.Datasets(lineIndex).Add(xVal, yVal, 0);
							}
							//make sure the last point is added
							angle = currentDuration + (90.0 - phaseAngle);
							angle *= Math.PI/180.0;				//convert to radians

							if (allDisp == true)
							    yVal = amplitude * Math.sin(angle);
							else if (maxAmp != 0) 
							{
							    if (loadType == ds.Script.id_SurfacePressure)
							        yVal = amplitude / maxAmpPressure * Math.sin(angle);
							    else if (loadType == ds.Script.id_EdgeForce || loadType == ds.Script.id_VertexForce || loadType == ds.Script.id_SurfaceForce)
							        yVal = amplitude / maxAmpForce * Math.sin(angle);
							    else
							        yVal = amplitude / maxAmpDisplacement * Math.sin(angle);
							}
							else
							    yVal = 0;

							var xVal = currentDuration;
							if( showDegree == false )  //convert i to radians
								xVal = xVal*Math.PI/180.0;
							Graph1.Datasets(lineIndex).Add(xVal, yVal, 0);

							Graph1.Datasets(lineIndex).LineColor = arrLineColor[colorIndex];
							colorIndex ++;
							if(colorIndex >= numLineColors)
								colorIndex = 0;
								
							Graph1.Datasets(lineIndex).Label = curLoad.Name;
							lineIndex++;					
						}
						else
						{
							//add another line for output (other than edge, vertex loads and displacements)
							plotCosCurve = true;
						}
					}
				}
			}
		}
	}
	
	if(plotCosCurve == true)
	{
		Graph1.Datasets.Add();
		var increment = duration / numPoints;
		var halfPI = 90.0;
		if (showDegree == false)
		    halfPI *= Math.PI / 180.0; 
		for(var i=0; i<=duration; i=i+increment)
		{
			angle = i + halfPI;
			if(showDegree == true)
				angle *= Math.PI/180.0;				//convert to radians
				
			yVal = Math.sin(angle);
			Graph1.Datasets(lineIndex).Add(i, yVal, 0);
		}
		//make sure the last point is added
		angle = i + halfPI;
		if(showDegree == true)
			angle *= Math.PI/180.0;				//convert to radians

		yVal = Math.sin(angle);
		Graph1.Datasets(lineIndex).Add(duration, yVal, 0);
		
		Graph1.Datasets(lineIndex).LineColor = arrLineColor[colorIndex];
		colorIndex ++;
		if(colorIndex >= numLineColors)
			colorIndex = 0;
									
		Graph1.Datasets(lineIndex).Label = stringTable.String("ID_ResultChartInput");
		lineIndex++;
	}
}

function InterpolatedValue(numfreqpoints, loadVariation)
{
    // locate interpolation interval
    var amplitude = 0.0;
    var frequency = 0.0;
    var ind = null;
    var dep = null;
    var unitSystemId = ds.UnitSystemID;
    frequency = phaseObj.RequestedFrequency;
    ind = loadVariation.GraphObject.PrimaryIndependent;
    dep = loadVariation.GraphObject.Dependents.Item(1);
    if (ind != null && dep != null)
    {
        var dFreqLow = 0.0;
        var dFreqHigh = 0.0;
        var dLoadValLow = 0.0;
        var dLoadValHigh = 0.0;

        if (frequency <= ind.Value(1, unitSystemId))
        {
            return ind.Value(1, unitSystemId);
        }
        if (frequency >= ind.Value(numfreqpoints, unitSystemId))
        {
            return ind.Value(numfreqpoints, unitSystemId);
        }
        for (var i = 1; i < numfreqpoints; i++)
        {
            if ((frequency >= ind.Value(i, unitSystemId)) && (frequency <= ind.Value(i + 1, unitSystemId)))
            {
                dFreqLow = ind.Value(i, unitSystemId);
                dFreqHigh = ind.Value(i + 1, unitSystemId);
                dLoadValLow = dep.Value(i, unitSystemId);
                dLoadValHigh = dep.Value(i + 1, unitSystemId);
                break;
            }
        }

        // interpolate force magnitude based on requested frequency
        amplitude = dLoadValLow + (((dLoadValHigh - dLoadValLow) / (dFreqHigh - dFreqLow)) * (frequency - dFreqLow));
    }

    return amplitude
}

function MaxAmpInLoads(harmLoads)
{
	var loadCount = harmLoads.Count;
	var maxAmp = 0;

	for(var i=1; i<=loadCount; i++)
	{
		var loadType = harmLoads(i).LoadType;
		if( loadType == ds.Script.id_EdgeForce ||
			loadType == ds.Script.id_VertexForce ||
			loadType == ds.Script.id_SurfaceDisplacement ||
			loadType == ds.Script.id_EdgeDisplacement ||
			loadType == ds.Script.id_VertexDisplacement ||
			loadType == ds.Script.id_SurfaceForce || 
			loadType == ds.Script.id_SurfacePressure )
		{
		    var isSuppressed = harmLoads(i).Suppressed;
		    if (isSuppressed)
		        continue;

		    var currentLoad = harmLoads(i);
		    var loadAmp = GetLoadAmplitude(currentLoad);
			maxAmp = Math.max(maxAmp, Math.abs(loadAmp));
		}
	}
	
	return maxAmp;
}

function MaxAmpInLoadsPressure(harmLoads) 
{
    var loadCount = harmLoads.Count;
    var maxAmp = 0;
    for (var i = 1; i <= loadCount; i++) 
    {
        var loadType = harmLoads(i).LoadType;
        if (loadType == ds.Script.id_SurfacePressure) 
		{
            var isSuppressed = harmLoads(i).Suppressed;
            if (isSuppressed)
                continue;

            var currentLoad = harmLoads(i);
            var loadAmp = GetLoadAmplitude(currentLoad);
            maxAmp = Math.max(maxAmp, Math.abs(loadAmp));
        }
    }
    return maxAmp;
}

function MaxAmpInLoadsDisplacement(harmLoads) 
{
    var loadCount = harmLoads.Count;
    var maxAmp = 0;
    for (var i = 1; i <= loadCount; i++) 
    {
        var loadType = harmLoads(i).LoadType;
        if (loadType == ds.Script.id_SurfaceDisplacement ||
			loadType == ds.Script.id_EdgeDisplacement ||
			loadType == ds.Script.id_VertexDisplacement) 
		{
            var isSuppressed = harmLoads(i).Suppressed;
            if (isSuppressed)
                continue;

            var currentLoad = harmLoads(i);
            var loadAmp = GetLoadAmplitude(currentLoad);
            maxAmp = Math.max(maxAmp, Math.abs(loadAmp));
        }
    }
    return maxAmp;
}

function MaxAmpInLoadsForce(harmLoads) 
{
    var loadCount = harmLoads.Count;
    var maxAmp = 0;
    for (var i = 1; i <= loadCount; i++) 
    {
        var loadType = harmLoads(i).LoadType;
        if (loadType == ds.Script.id_EdgeForce ||
			loadType == ds.Script.id_VertexForce ||
			loadType == ds.Script.id_SurfaceForce) 
		{
            var isSuppressed = harmLoads(i).Suppressed;
            if (isSuppressed)
                continue;

            var currentLoad = harmLoads(i);
            var loadAmp = GetLoadAmplitude(currentLoad);
            maxAmp = Math.max(maxAmp, Math.abs(loadAmp));
        }
    }
    return maxAmp;
}

function GetLoadAmplitude(curLoad)
{ 
	var requestedFrequency = phaseObj.RequestedFrequency;
	var loadAmplitudePhase = curLoad.AmplitudePhaseAtFreq(requestedFrequency);		
	var amplitude = 0.0;
	var phaseAngle = 0.0;							
	if (loadAmplitudePhase != null) 
	{
		amplitude = loadAmplitudePhase(1);
		phaseAngle = loadAmplitudePhase(2);		
	}
	return amplitude;
}

function AllDisplacements()
{
	if( phaseObj.ResultChartType == ds.Script.id_XComponentDisplacement ||
	    phaseObj.ResultChartType == ds.Script.id_YComponentDisplacement ||
	    phaseObj.ResultChartType == ds.Script.id_ZComponentDisplacement )
	{
		var branch = ds.Tree.FirstActiveBranch;
		if(branch)
		{
			environment = branch.Environment;
			if(environment)
			{
				var harmLoads = environment.HarmonicLoads;
				var count = harmLoads.Count;
				if (!count)
				    return false;
				    
				for(var i=1; i<=count; i++)
				{
				    var isSuppressed = harmLoads(i).Suppressed;
				    if (isSuppressed)
				        continue;
					var loadType = harmLoads(i).LoadType;
					if (loadType != ds.Script.id_SurfaceDisplacement &&
						loadType != ds.Script.id_EdgeDisplacement &&
						loadType != ds.Script.id_VertexDisplacement)
					    return false;
				}
			}
		}
	}
	else
		return false;
	return true;
}

function SetAxisFormat()
{
	Graph1.Axes(0).AutomaticLimits = true;
	Graph1.Axes(1).AutomaticLimits = true;
	Graph1.PickLines = true;
	
	var minX1 = Graph1.Datasets(0).Min(0);
	var maxX1 = Graph1.Datasets(0).Max(0);
	var difX1 = Math.abs(maxX1 - minX1);
	var minY1 = Graph1.Datasets(0).Min(1);
	var maxY1 = Graph1.Datasets(0).Max(1);
	var difY1 = Math.abs(maxY1 - minY1);
	
	//since the user can change the number of decimals displayed, the number used to determine
	//whether exponential notation should be used changed based on the number of decimals
	var yCompare =	(1.0/(Math.pow(10, Graph1.Axes(1).LabelFieldPrecision))) * Graph1.Axes(1).Divisions;
	var xCompare =	(1.0/(Math.pow(10, Graph1.Axes(0).LabelFieldPrecision))) * Graph1.Axes(0).Divisions;
	var yCompare2 = Math.pow(10, Graph1.Axes(1).LabelFieldPrecision) * Graph1.Axes(1).Divisions;
	
	if(minX1 == maxX1)
	{
		Graph1.Axes(0).AutomaticLimits = false;
		if(maxX1 == 0)
		{
			Graph1.Axes(0).ImposedLimitsMin = maxX1-1;
			Graph1.Axes(0).ImposedLimitsMax = maxX1+1;
		}
		else
		{
			Graph1.Axes(0).ImposedLimitsMin = maxX1-maxX1/2;
			Graph1.Axes(0).ImposedLimitsMax = maxX1+maxX1/2;
		}
	}
	if(minY1 == maxY1)
	{
		Graph1.Axes(1).AutomaticLimits = false;
		if(maxY1 == 0)
		{
			Graph1.Axes(1).ImposedLimitsMin = maxY1-1;
			Graph1.Axes(1).ImposedLimitsMax = maxY1+1;
		}
		else
		{		
			Graph1.Axes(1).ImposedLimitsMin = maxY1-maxY1/2;
			Graph1.Axes(1).ImposedLimitsMax = maxY1+maxY1/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((minX1 == maxX1) && (minY1 == maxY1))
		Graph1.PickLines = false;
	
	if(Graph1.Axes(0).Logarithmic == true || (((Math.abs(minX1) > 1000 || Math.abs(maxX1) > 1000) && difX1 < xCompare) || (Math.abs(minX1) < 1.0 && Math.abs(maxX1) < 1.0 )))
	{
		xexpProgram1 = true;
		if(xprogSel1 == true)
			Graph1.Axes(0).LabelUseExponential = true;
	}
	else
	{
		xexpProgram1 = false;
		if(xprogSel1 == true)
			Graph1.Axes(0).LabelUseExponential = false;
	}
	if( Graph1.Axes(1).Logarithmic == true || 
		((Math.abs(minY1) > 1000 || Math.abs(maxY1) > 1000) && difY1 < yCompare) || (Math.abs(minY1) < 1.0 && Math.abs(maxY1) < 1.0 ) ||
		((Math.abs(minY1) > 1000 || Math.abs(maxY1) > 1000) && difY1 > yCompare2) )
	{
		yexpProgram1 = true;
		if(yprogSel1 == true)
			Graph1.Axes(1).LabelUseExponential = true;
	}
	else
	{
		yexpProgram1 = false;
		if(yprogSel1 == true)
			Graph1.Axes(1).LabelUseExponential = false;
	}
}

function ShouldShowDegrees()
{
	if(ds.ShowAngleAsDegrees == true)
		return true;
	else
		return false;
}

function window_onunload()
{
	setGraphStrings();
	phaseObj = 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 restoreSavedProperties()
{
	if(phaseObj.LineProps != "")
	{
		try
		{
			//restore the line properties
			lineProperties = phaseObj.LineProps;
		
			lineArray = lineProperties.split("|");
			Graph1.Line2DType = lineArray[0];
			Graph1.Datasets(0).LineColor = lineArray[1];
			Graph1.Datasets(0).PointColor = lineArray[2];
			Graph1.Datasets(0).PointBorderColor = lineArray[3];
			Graph1.Datasets(0).LineWidth = lineArray[4];
		}
		catch(e)
		{
		}
	}
	if(phaseObj.XAxisProps != "")
	{
		try
		{
			//restore the xaxis properties
			xprops = phaseObj.XAxisProps;
		
			xarray = xprops.split("|");
			Graph1.Axes(0).Logarithmic = xarray[0];
			xexpProgram1 = xarray[1];
			xprogSel1 = xarray[2];
			Graph1.Axes(0).LabelUseExponential = xarray[3];
			Graph1.Axes(0).LabelFieldPrecision = xarray[4];
			Graph1.Axes(0).Divisions = xarray[5];
		}
		catch(e)
		{
		}
	}
	if(phaseObj.YAxisProps != "")
	{
		try
		{
			//restore the xaxis properties
			yprops = phaseObj.YAxisProps;
		
			yarray = yprops.split("|");
			Graph1.Axes(1).Logarithmic = yarray[0];
			yexpProgram1 = yarray[1];
			yprogSel1 = yarray[2];
			Graph1.Axes(1).LabelUseExponential = yarray[3];
			Graph1.Axes(1).LabelFieldPrecision = yarray[4];
			Graph1.Axes(1).Divisions = yarray[5];
		}
		catch(e)
		{
		}
	}
}

function setGraphStrings()
{
	line =  Graph1.Line2DType;
	line += "|";
	line += Graph1.Datasets(0).LineColor;
	line += "|";
	line += Graph1.Datasets(0).PointColor;
	line += "|";
	line += Graph1.Datasets(0).PointBorderColor;
	line += "|";
	line += Graph1.Datasets(0).LineWidth;
	line += "|";
	line += Graph1.Line2DType;
	line += "|";
	line += Graph1.Datasets(0).LineColor;
	line += "|";
	line += Graph1.Datasets(0).PointColor;
	line += "|";
	line += Graph1.Datasets(0).PointBorderColor;
	line += "|";
	line += Graph1.Datasets(0).LineWidth;
	phaseObj.LineProps = line;
	
	
	if(Graph1.Axes(0).Logarithmic)
		x = 1;
	else
		x = 0;
	x += "|";
	if(xexpProgram1)
		x += 1;
	else
		x += 0;
	x += "|";
	if(xprogSel1)
		x += 1;
	else
		x += 0;
	x += "|";
	if(Graph1.Axes(0).LabelUseExponential)
		x += 1;
	else
		x += 0;
	x += "|";
	x += Graph1.Axes(0).LabelFieldPrecision;
	x += "|";
	x += Graph1.Axes(0).Divisions;
	x += "|";
	if(Graph1.Axes(0).Logarithmic)
		x += 1;
	else
		x += 0;
	x += "|";
	if(xexpProgram1)
		x += 1;
	else
		x += 0;
	x += "|";
	if(xprogSel1)
		x += 1;
	else
		x += 0;
	x += "|";
	if(Graph1.Axes(0).LabelUseExponential)
		x += 1;
	else
		x += 0;
	x += "|";
	x += Graph1.Axes(0).LabelFieldPrecision;
	x += "|";
	x += Graph1.Axes(0).Divisions;

	phaseObj.XAxisProps = x;
	
	
	if(Graph1.Axes(1).Logarithmic)
		y = 1;
	else
		y = 0;
	y += "|";
	if(yexpProgram1)
		y += 1;
	else
		y += 0;
	y += "|";
	if(yprogSel1)
		y += 1;
	else
		y += 0;
	y += "|";
	if(Graph1.Axes(1).LabelUseExponential)
		y += 1;
	else
		y += 0;
	y += "|";
	y += Graph1.Axes(1).LabelFieldPrecision;
	y += "|";
	y += Graph1.Axes(1).Divisions;
	y += "|";
	if(Graph1.Axes(1).Logarithmic)
		y += 1;
	else
		y += 0;
	y += "|";
	if(yexpProgram1)
		y += 1;
	else
		y += 0;
	y += "|";
	if(yprogSel1)
		y += 1;
	else
		y += 0;
	y += "|";
	if(Graph1.Axes(1).LabelUseExponential)
		y += 1;
	else
		y += 0;
	y += "|";
	y += Graph1.Axes(1).LabelFieldPrecision;
	y += "|";
	y += Graph1.Axes(1).Divisions;

	phaseObj.YAxisProps = y;
}

function refreshPage()
{
	frequency = phaseObj.RequestedFrequency;
	duration = phaseObj.Duration;
	SetGraphVals();
}

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

function lv_Change()
{
	if( frequency != phaseObj.RequestedFrequency ||
		duration != phaseObj.Duration)
	{
		frequency = phaseObj.RequestedFrequency;
		duration = phaseObj.Duration;
		SetGraphVals();
	}
}

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

function Processing_State_Finished(processCode)
{
	if(processCode == 2)
	{
		SetGraphVals();
	}
}

function window_onresize() 
{
	SizeGraph();
}

function SizeGraph()
{
	var height = .9 * (document.body.offsetHeight - pageName.offsetHeight);
	var width = .9 * document.body.offsetWidth;
	
	if(height > 0 && width > 0)
	{
		Graph1.style.height = height;
		Graph1.style.width = width;
	}
}

function GraphProperties(dlg)
{	
	this.dialog				= dlg;
	this.Graph				= Graph1;
	this.stringTable		= stringTable;
	this.linecolor			= Graph1.Datasets(0).LineColor;
	this.pointcolor			= Graph1.Datasets(0).PointColor;
	this.pointbordercolor	= Graph1.Datasets(0).PointBorderColor;
	this.xdivisions			= Graph1.Axes(0).Divisions;
	this.ydivisions			= Graph1.Axes(1).Divisions;
	this.linetype			= Graph1.Line2DType;
	this.xlog				= Graph1.Axes(0).Logarithmic;
	this.ylog				= Graph1.Axes(1).Logarithmic;
	this.xshowexp			= Graph1.Axes(0).LabelUseExponential;
	this.yshowexp			= Graph1.Axes(1).LabelUseExponential;
	this.xdecimals			= Graph1.Axes(0).LabelFieldPrecision;
	this.ydecimals			= Graph1.Axes(1).LabelFieldPrecision;	
	this.xuseProg			= xprogSel1;
	this.yuseProg			= yprogSel1;
	this.xProgExpValue		= xexpProgram1;
	this.yProgExpValue		= yexpProgram1;
	this.linethick			= Graph1.Datasets(0).LineWidth;
	this.restore			= false;
	this.type				= "phase";
}

function restoreDefaultsGraph()
{
	Graph1.Axes(0).LabelUseExponential = xexpProgram1;
	Graph1.Axes(1).LabelUseExponential = yexpProgram1;
	Graph1.Axes(0).LabelFieldPrecision = 2;
	Graph1.Axes(1).LabelFieldPrecision = 3;
	Graph1.Axes(0).Divisions = 8;
	Graph1.Axes(1).Divisions = 8;
	xprogSel1 = true;
	yprogSel1 = true;
	Graph1.Draw();
}

function createTextForFile()
{
	var fileText = "";
	var count = Graph1.Datasets.Count;
	
	//write the column titles to the file
	fileText += Graph1.Axes(0).Label + "\t";
	for(var i=0; i<count; i++)
	{
		fileText += Graph1.DataSets.Item(i).Label;
		fileText += "\t";
	}
	fileText += "\n";
	
	var itemCount = numPoints;
	for(i=1; i<=itemCount; i++)
	{
		for(var j=0; j<count; j++)
		{
			if(j == 0)
				fileText += Graph1.Datasets(j).ItemAt(i, 0) + "\t";
			fileText += Graph1.Datasets(j).ItemAt(i, 1) + "\t";
		}
		fileText += "\n";
	}

	return fileText;
}

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;
	}
}
//-->
            </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>
        <P>
            <center>
                <OBJECT id="Graph1" style="DISPLAY: none; LEFT: 0px; WIDTH: 806px; TOP: 0px; HEIGHT: 617px"
                    height="617" 
                    classid="clsid:F113A120-C83C-47B5-847C-179B339C42B7">
                </OBJECT>
            </center>
            <center>
            </center>
    </body>
</html>
