//CHECKJS  d:\BOB\PROMOTE\PROFWORK\SOFTWARE\FIRST\JS\acidbase\graph.js 6/7/02 2:45:45 AM
/* graphing.js
8:29 AM 10/10/2002 BH added scalable graph
interfaces to one of two gnuplot servers
and also divgraph.js for JavaScript graphing
quickgraph() creates a dynamic plot in a graphing window
quickpoint() adds a point to that graph
jsgraph() creates a new window with the graph
*/
_Sform=new Array()

var debug=false
var ispng=false
var isgif=false
var iseps=false
var isjs=true
var Labels=new Array()
var Data=new Array()
var Info=new Array()
var woptionsgnu="menubar,scrollbars,resizable,alwaysRaised"
var woptionsjs="menubar=yes,scrollbars,alwaysRaised,width=700,height=400,left=50"

function creategnuplot(_iform,stitle,colormode,sinfo){
	if(sinfo.length==0)return
	var s=stitle+"<p><hr WIDTH='100%'><br>"+strsub(_Sform[_iform],"%sinfo%",sinfo)
	var s=strsub(s,"%colormode%",colormode)
	// if(ismac)s=strsub(s,"\n","\r")
	putnewwindow(s,woptionsgnu)
}

function dograph()
{
	setserver()
	var usr=parent.fraStatus.document.info
	ispng=(Display["PNG"]<0?false:usr.o_display[Display["PNG"]].checked)
 	iseps=(Display["EPS"]<0?false:usr.o_display[Display["EPS"]].checked)
	isjs=(Display["CSS"]==-2?true:Display["CSS"]<0?false:usr.o_display[Display["CSS"]].checked)
	_iform=(ispng?1:0)
	if(isjs){
		jsgraph()
		return
	}
	var swhat="<h1>Titration Curve</h1>"
	swhat+="<p>"+npts+ " data points<br>"
	swhat+="<p>more about <a target=_blank href=http://www.stolaf.edu/people/hansonr/gnuplot/gnutest.htm>gnuplot</a>"
	swhat+="<h3>"+strsub(stitle,"|","<br>")+"</h3>"
	
	var sep=" "
	var ssrc='set tmargin 10;set bmargin 10;set lmargin 10;set rmargin 10'+nl
	ssrc+='set title "'+strsub(stitle,"|","\\n")+'"'+nl
	ssrc+='set data style lines'+nl
	ssrc+='set xzeroaxis;set tics out'+nl
	ssrc+='set xlabel "mL added"'+nl
	ssrc+='set ylabel "pH"'+nl
	ssrc+='set tics out'+nl
	ssrc+='set yrange [0:14]'+nl
	ssrc+="plot '-' title ''"+nl
	ssrc+=strsub(usr.xydata.value,","," ")
	ssrc+="e \n"
	creategnuplot(_iform,swhat,"color",ssrc)
}

function jsgraph(){
	var x=0
	var y=0
	var n=0
	var usr=parent.fraStatus.document.info
	var sdata=strsub(usr.xydata.value,"\n",",")
	sdata=strsub(sdata,"\r",",")+","
	sdata=strsub(sdata,",,",",")
	var sm=""+Math.random()
	sm=sm.substring(3,10)
	var newwin=open("","js_"+sm,woptionsjs)
	newwin.document.open()
	newwin.document.write("<html><head>")
	var D=sdata.split(",")
	Data=new Array()
	Labels=new Array() //global
	Info=new Array()
	for(i=0;i<D.length-1;i+=2){
		x=parseFloat(D[i])
		y=parseFloat(D[i+1])
		Data[n]=new Array(x,y,pHcolor(y))
		Labels[n++]="pH="+roundoff(y,2)+" at " + roundoff(x,1)+" mL"
	}
	Info.dopoints=false
	Info.doptlabels=Info.dopoints=true
	Info.doxticnums=Info.doyticnums=true
	Info.dogridx=Info.dogridy=true
	Info.title="<center><h3>"+strsub(stitle,"|"," ")+"</h3><a href=javascript:print()>Print</a>&nbsp;&nbsp;<a href=javascript:close()>Close</a></center>"
	Info.xaxislabel="mL added"
	Info.yaxislabel="pH"
	Info.xmin = 0
	Info.xmax = NaN
	Info.ymin = 0
	Info.ymax = 14
	Info.defaultcolor="blue"
	Info.graphwidth=600
	Info.graphheight=280
	Info.graphtop=85
	Info.graphleft=50
	Info.ptsize=2
	Info.ticwidth=1
	Info.xticfirst=0
	Info.xticmajor=(mL_2>=500?100:mL_2>=50?10:mL_2>5?1:mL_2>1?1:.1)
	Info.xticminor=(mL_2>=500?20:mL_2>=50?2:mL_2>5?.2:.1)
	Info.yticfirst=0
	Info.yticmajor=1
	Info.yticminor=1
	Info.maxaddpoints=2
	Info.ptsize=4
	Info.docurve=false
	Info.dopoints=true
	Info.xnumdigits=Info.ynumdigits=0
	GRinit()
	GRsetgraphwindow(newwin)
	GRdrawgraph("",Data,Labels,Info)
	newwin.document.write("</head><body bgcolor=white>")
	newwin.document.write("</body></html>")
	newwin.document.close()
}

function putnewwindow(s,opt){
	var sm=""+Math.random()
	sm=sm.substring(3,10)
	var newwin=open("","AB_"+sm,opt)
	dowrite(newwin.document,s)
	return newwin
}

function quickgraph(iend){
	divdoc=parent.fraGraph.document
	divdoc.open()
	divdoc.write("<html><body bgcolor=white>")
	Labels=new Array()
	Data=new Array([NaN,NaN])
	mL_2=iend
	Info.doptlabels=Info.dopoints=true
	Info.docurve=Info.doxticnums=Info.doyticnums=false
	Info.dogridx=Info.dogridy=true
	Info.title=""
	Info.xaxislabel=""
	Info.yaxislabel=""
	Info.xmin = 0
	Info.xmax = mL_2
	Info.ymin = 0
	Info.ymax = 14
	Info.defaultcolor="blue"
	Info.graphwidth=(parent.fraGraph.innerWidth?parent.fraGraph.innerWidth:divdoc.body && divdoc.body.clientWidth?divdoc.body.clientWidth:480)-80
	Info.graphheight=(parent.fraGraph.innerHeight?parent.fraGraph.innerHeight:divdoc.body && divdoc.body.clientHeight?divdoc.body.clientHeight:90)-40
	Info.graphtop=10
	Info.graphleft=20
	Info.ptsize=3
	Info.ticwidth=1
	Info.xticfirst=0
	Info.xticmajor=(mL_2>=500?100:mL_2>=50?10:mL_2>5?1:mL_2>1?1:.1)
	Info.xticminor=(mL_2>=500?20:mL_2>=50?2:mL_2>5?.2:.1)
	Info.yticfirst=0
	Info.yticmajor=7
	Info.yticminor=1
	Info.maxaddpoints=titrationpts*2
	Info.append=false
	Info.keepopen=true
	GRinit()
	GRsetgraphwindow(parent.fraGraph)
	GRdrawgraph("",Data,Labels,Info)
	divdoc=parent.fraGraph.document
	divdoc.write("</body></html>")
	divdoc.close()
	GRaddtext(iend*1.02,1,""+iend+" mL")
}

function quickpoint(mL,pH){
	GRaddpointref(mL,pH,"pH "+roundoff(pH,2)+" at "+roundoff(mL,2)+" mL",pHcolor(pH),"javascript:parent.fraMain.showpt("+mL+")")
}

function setserver(){
	//gif server
	//gnuserver="http://onsager.bd.psu.edu/cgi-bin/gnuplot.cgi"
	//_Sform[0]="<form method=POST action="+gnuserver+">"
	//_Sform[0]+="<textarea name=commands rows=10 cols=60>"
	//_Sform[0]+="set term pbm %colormode%\n"
	//_Sform[0]+="%sinfo%</textarea></td></tr>"
	//_Sform[0]+="<tr><th><td><input type=submit value=Plot></td></tr>"
	//_Sform[0]+="</form>"
	//png server
	var s=document.URL
	var gserver="http://www.stolaf.edu/cgi-bin/gnuplot.pl"
	//EPS server
	_Sform[0]="<form method=POST action="+gserver+">"
	_Sform[0]+="<table><tr><th>Terminal<td><select name=_term><option value=postscript>PostScript Format</select></td></tr>"
	_Sform[0]+="<tr><th>Terminal Options<td><input type=text name=_termoptions size=30 value=%colormode%></td></tr>"
	_Sform[0]+="<tr><th>Debug?<td><input type=text name=_debug size=10 value=no></td></tr>"
	_Sform[0]+="<tr><th>Gnuplot Commands<td><textarea name=_data rows=10 cols=75 wrap=virtual>"
	_Sform[0]+="set lmargin 10; set rmargin 10;set bmargin 10; set tmargin 10\n"
	_Sform[0]+="%sinfo%</textarea></td></tr>"
	_Sform[0]+="<tr><th><td><input type=submit value=Plot></td></tr>"
	_Sform[0]+="</table></form>"
	if(s.indexOf("stolaf.edu")>0){
		gserver="http://www.stolaf.edu/cgi-bin/gnuplot.pl"
		_Sform[1]="<form method=POST action="+gserver+">"
		_Sform[1]+="<table><tr><th>Terminal<td><select name=_term><option value=png>PNG Image</select></td></tr>"
		_Sform[1]+="<tr><th>Terminal Options<td><input type=text name=_termoptions size=30 value=%colormode%></td></tr>"
		_Sform[1]+="<tr><th>Debug?<td><input type=text name=_debug size=10 value=no></td></tr>"
		_Sform[1]+="<tr><th>Gnuplot Commands<td><textarea name=_data rows=10 cols=75 wrap=virtual>"
		_Sform[1]+="set lmargin 10; set rmargin 10;set bmargin 10; set tmargin 10\n"
		_Sform[1]+="%sinfo%</textarea></td></tr>"
		_Sform[1]+="<tr><th><td><input type=submit value=Plot></td></tr>"
		_Sform[1]+="</table></form>"
	}else{
		gserver="http://onsager.bd.psu.edu/cgi-bin/gnuplot.cgi"
		_Sform[1]="<form method=POST action="+gserver+">"
		_Sform[1]+="<textarea name=commands rows=10 cols=60>"
		_Sform[1]+="set term png %colormode%\n"
		_Sform[1]+="%sinfo%</textarea></td></tr>"
		_Sform[1]+="<tr><th><td><input type=submit value=Plot></td></tr>"
		_Sform[1]+="</table></form>"
	}
}


