// ============================================ //
// Functionplot 1.0                                           11.07.1997                 //
// Test: program to test the XY-Meter Applet                                  //
//          : set the important public methods via JS                         //
//          : by pressing a button and change values                         //
//          : by using a form                                                                     //
// Ralf Moros                                                                                       //
// moros@sonne.tachemie.uni-leipzig.de                                      //
// =========================================== //
// http://cerius.tachemie.uni-leipzig.de/~jar/                                //
// http://cerius.tachemie.uni-leipzig.de/                                         //
// ==========================================  //
var  head     = "U= f(t)";
var  yunit     = "[V]";
var  xunit     = "[s]";
var  onlyy       = "false";
var  ymin        = 0;
var  ymax       = 100;
var  xmin        = 0;
var  xmax       = 100;
var maxpairs= 100;
var period     = 1;

//the colors
var   hbg    = "LightGray";
var   hfg     = "Black";
var   abg    = "LightGray";
var   afg     = "Black";
var   lincol  = "Blue";

//control parameters
var      Tk     = xmin;

var    DeltaT = (xmax-xmin)/maxpairs;
var    ParA    = 1;
var    ParB    = 1;
var    CIndex= 0;
var    CShortkind ="lin";


// (y,t)
var     WERTY = 50.0;
var     WERTX = 0;

function DelayInit()
{setTimeout("Init()",1000);
}

function Init()
{
	document.forms[0]._head.value       = head;
	document.forms[0]._xunit.value        = xunit;
	document.forms[0]._yunit.value        = yunit;
	document.forms[0]._ymin.value         = ymin;
	document.forms[0]._ymax.value        = ymax;
	document.forms[0]._xmin.value         = xmin;
	document.forms[0]._xmax.value        = xmax;
               document.forms[0]._maxpairs.value = maxpairs;
	
	document.utmeter.SetParameter(head,xunit,yunit,xmin,xmax,
		ymin,ymax,onlyy,period,maxpairs);
	
	document.utmeter.SetColors(hbg,hfg,abg,afg);
               document.utmeter.SetLineColor(parseInt(1),lincol);
	
	document.forms[0]._A.value= ParA;
	document.forms[0]._B.value= ParB;
	document.forms[0]._CIN.value="-----------------";
	
	WERTY = 50.0;
	WERTX = 0;
	DeltaT  = 1;                         // Time [s] between 2 y
	ParA     = 1;                         //Parameter A
	ParB     = 1;                        // Parameter B
	
}


function JSSetWertXY()
{
	var Y = parseFloat(WERTY);
	var X = parseFloat(WERTX);
	document.utmeter.SetMeterXY(parseInt(1),X,Y);
}

function JSPaintWertXY()
{ var Y = parseFloat(WERTY);
   var X = parseFloat(WERTX);
   document.utmeter.DrawMeterXY(parseInt(1),X,Y);
}

//Set the color of the xy-line
function JSSetLineColor(form)
{
	Idx = form._LINColor.selectedIndex;
	lincol = form._LINColor.options[Idx].value;
	document.utmeter.SetLineColor(parseInt(1),lincol);
}

//Set all parameters of the meter
function JSSetParameter(form)
{
	head        = form._head.value;
	xunit         = form._xunit.value;
	yunit        = form._yunit.value;
	ymin         = parseFloat(form._ymin.value);
	ymax        = parseFloat(form._ymax.value);
	xmin         = parseFloat(form._xmin.value);
	xmax        = parseFloat(form._xmax.value);
               maxpairs= parseInt(form._maxpairs.value);
	
	document.utmeter.SetParameter(head,xunit,yunit,xmin,xmax,
		ymin,ymax,onlyy,period,maxpairs);
	
}


//Reset the meter
function JSResetMeter()
{document.utmeter.ResetMeter();
}




//Set all the parameter of the function generator, time period, kind of curve,..
function SetFunction(form)
{
	Tk        = xmin;
               DeltaT= (xmax-xmin)/maxpairs;
	ParA   = parseFloat(form._A.value);
	ParB   = parseFloat(form._B.value);
	CIndex=form.WhichCurve.selectedIndex;
	CShortkind=form.WhichCurve.options[CIndex].value;
	if (CShortkind =="lin")
	{form._CIN.value=ParA+"+"+ParB+"* t";
		
	}
	if (CShortkind == "sin")
	{form._CIN.value=ParA+"* sin("+ParB+"* t )";
	}
	if (CShortkind == "ex")
	{form._CIN.value=ParA+"*( 1 - exp(- t / "+ParB+"))";
	}
	
}


//time function: calculate the values of y
//calculate the y-values
function StartCalculation()
{
        Tk         =  xmin;
        DeltaT = (xmax-xmin)/maxpairs;
        for (I=0;I<maxpairs;I++)
        {
              WERTX = parseFloat(Tk);
	//Calculate y
	if (CShortkind =="lin")
	{ WERTY= parseFloat(ParA+ParB*Tk);
	 }//End of Lin
	
	if (CShortkind=="sin")
 	{WERTY= parseFloat(ParA* Math.sin(ParB*Tk));
                }//End of Sin
	
	if (CShortkind =="ex")
 	{WERTY= parseFloat(ParA*(1-(Math.exp(-1.0*Tk/ParB))));
	}//End of Exp


               if (I == (maxpairs-1)) {JSPaintWertXY();}
               else JSSetWertXY();


             Tk = Tk + DeltaT;
     }
	
}
