//Global variables
var WinOut,WinCount=0;	//Simulated output data (array and window), number of windows
var S_min=0, S_max=50, D_min=0.05, D_max=25; //limits for user-defined variables

function simchip1_main(){
//reads user-defined variables from input form f_inp
var s0=Math.round(parseFloat(document.f_inp1.S_old.value)*10)/10;	//initial synthesis constant (molecules per min per cell)
var d0=Math.round(parseFloat(document.f_inp1.D_old.value)*100)/10000;	//initial decay constant (per min, derived from a percentage)
var c0=Math.round(s0/d0);						//initial steady-state concentration (molecules per cell)
var s=Math.round(parseFloat(document.f_inp1.S_new.value)*10)/10;	//new synthesis constant
var d=Math.round(parseFloat(document.f_inp1.D_new.value)*100)/10000;	//new decay constant
var c=Math.round(s/d);							//new steady-state concentration
var hl=Math.round(10*(0-Math.log(0.5))/d)/10;				//new half-life
if (hl>10) hl=Math.round(hl);
var net=Math.round(Math.log(Math.abs(c0-c))/d);				//near-equilibrium time

var e=Math.exp(1);	//Euler's constant
var ct, t;		//concentration at time t, time t
var graph,graph1;	//graphical elements
var a,f1,f2;		//constants to rescale the graph
var x,x1,y,y1;		//coordinates
var tx;			//descriptive text

if (c0>c) tx="<i>Downregulation from " + c0 + " to " + c;
if (c0<c) tx="<i>Upregulation from " + c0 + " to " + c;
if (c0==c) tx="<i>Constant expression level of " + c;
tx+=" mRNA molecules per cell: The arrow indicates a half-life of " + hl + " minutes.<br>";
if (c0!=c) tx+="The process is completed within " + Math.round(net/hl*10)/10 + " half-lifes (" + net + " minutes).</i><br>";
tx+="<br>";

//generates grafics
tx+="<table border='1'><tr><td>Time<br>(min)</td><td>mRNA<br>(molecules/cell)</td></tr>";

//finds best y-axis (ordinate)
		a=375;	//length of ordinate
		f1=a/100000;
		graph='<img src=\"./images/ordinate100000.gif\" align=\"top\">';
	if(c<10001){
		f1=a/10000;
		graph='<img src=\"./images/ordinate10000.gif\" align=\"top\">';}
	if(c<1001 && c0<1001){
		f1=a/1000;
		graph='<img src=\"./images/ordinate1000.gif\" align=\"top\">';}	
	if(c<101 && c0<101){		
		f1=a/100;
		graph='<img src=\"./images/ordinate100.gif\" align=\"top\">';}
//finds best x-axis (abszissa)
		f2=100;
		graph1='<img src=\"./images/abscissa10000.gif\">';
	if(hl<=100){
		f2=10;
		graph1='<img src=\"./images/abscissa1000.gif\">';}
	if(hl<=10){
		f2=1;
		graph1='<img src=\"./images/abscissa100.gif\">';}
//calculates xy-positions for maximum 100 data points with ct<100000 according to y=a-f1*ct
	for(t=0;t<=100;t++){	  
	  ct=c+Math.pow(e,(0-d*f2*t))*(c0-c);
	  tx+="<tr><td>"+t*f2+"</td><td>"+Math.round(ct)+"</td></tr>";
	  if(c<100001){
	  	y=a-f1*ct;
	      if (t!=Math.ceil(hl/f2))
			{graph+='<img src=\"./images/blue_dot.gif\" align=\"top\" style=\"position:relative;top:'+y+'px;left:'+x+'px\">';}
		else {graph+='<img src=\"./images/red_dot.gif\" align=\"top\" style=\"position:relative;top:'+y+'px;left:'+x+'px\">';
			x1=-10;y1=y-30;
			graph+='<img src=\"./images/arrow.gif\" align\"top\" style=\"position:relative;top:'+y1+'px;left:'+x1+'px\">';
			x=-12;}
	  }	
	}
	tx+="</table>";
	graph = graph + "<br>" + graph1 + "<p>" + tx + "</p>";
	
   //Creates output window
   WinCount++;	
   WinOut=window.open("",WinCount,"width=800,height=600,left=10,top=10,resizable=yes,scrollbars=yes,menubar=yes");
   WinOut.document.writeln("<html><head><title>SimChip 1<\/title><\/head>\n<body>");
   WinOut.document.writeln(graph);
   
   WinOut.document.writeln("<\/body><\/html>");
   WinOut.document.close();
}//end of simchip_main


function simchip1_help(){
var WinHelp;
WinHelp=window.open("","","width=500,height=250,left=10,top=10,resizable=yes,scrollbars=yes,menubar=no");
	WinHelp.document.writeln("<html><head><title>SimChip 1<\/title><\/head>\n<body>");
	WinHelp.document.writeln("<h3>Up- and down-regulation of individual genes<\/h3>"); 
	WinHelp.document.writeln("<p>For a first try just click on GO. Then click on NEW for the next experiment and again on GO. ");
	WinHelp.document.writeln("mRNA steady-state concentrations before and after any change of synthesis and decay will be recalculated. ");
	WinHelp.document.writeln("Each time you click on GO, a new window will pop up, ");
	WinHelp.document.writeln("which visualizes the time course of genetic up- or down-regulation.<\/p>"); 
	WinHelp.document.writeln("You will find that the majority of expression levels is between 10<sup>1</sup> and 10<sup>3</sup> molecules per cell.");  
	WinHelp.document.writeln("Extreme values are only observed when S or D are close to zero. "); 
	WinHelp.document.writeln("Decay rates are inversely correlated with mRNA levels, according to the equation c=S/D."); 
	WinHelp.document.writeln("<h4>Mathematical Background<\/h4>");
	WinHelp.document.writeln("<i>SimChip<\/i> is based on the well accepted assumption ");
	WinHelp.document.writeln("that observed gene expression is largely reflected by mRNA concentrations ");
 	WinHelp.document.writeln("that are in a steady-state of linear synthesis and non-linear decay. ");
	WinHelp.document.writeln("The underlying differential equation is:<br>");
	WinHelp.document.writeln("<a href=\"./downloads/mathematical_background.pdf\" target=\"SimChip 1\">");
	WinHelp.document.writeln("<img style=\"border:none\" src=\"./images/formula.jpg\"></a><br>");
	WinHelp.document.writeln("where c is the mRNA concentration, S and D are synthesis and decay constants and t is the time.<br> ");
	WinHelp.document.writeln("Theoretically, the steady-state is reached at infinity, but in practice the process is completed, ");
	WinHelp.document.writeln("when the difference is less than one molecule per cell (so-called near-equilibrium time).<br>");
	WinHelp.document.writeln("For further details please click on the formula.");
	WinHelp.document.writeln("\n<\/body><\/html>");
WinHelp.document.close();
}//end of simchip1_help

function simchip1_input(){
//Creates a user form to enter simchip input data

var text="<h3>Up- and downregulation of individual genes</h3>"
+"<form name=\'f_inp1\' action=\'\'>"
+"<table style=\'font-size:9pt\';  border=\'1\'; text-align: left; cellpadding=\'2\' cellspacing=\'2\'>"
+"<tr bgcolor=\'080880\' style=\'color: white\'>"
+"<td style=\'width: 180px\'><b>mRNA</b></td>"
+"<td style=\'text-align: center; width: 135px;\'><b>Current Status</b></td>"
+"<td style=\'text-align: center; width: 135px;\'><b>New Status</b></td>"
+"</tr><tr>"
+"<td>Synthesis (molecules/minute)</td>"
+"<td style=\'text-align: center\'><input name=\'S_old\' value=\'2.5\'type=\'text\' title=\'0-50\' size=\'5\' onblur=\'check_S_old(this.value)\'></td>"
+"<td style=\'text-align: center\'><input name=\'S_new\'  value=\'8.5\'type=\'text\' title=\'0-50\' size=\'5\' onblur=\'check_S_new(this.value)\'></td>"
+"</tr><tr>"
+"<td>Decay (percent/minute)</td>"
+"<td style=\'text-align: center\'><input name=\'D_old\' value=\'10\'type=\'text\' title=\'0.05-25\' size=\'5\' onblur=\'check_D_old(this.value)\'></td>"
+"<td style=\'text-align: center\'><input name=\'D_new\'  value=\'10\'type=\'text\' title=\'0.05-25\' size=\'5\' onblur=\'check_D_new(this.value)\'></td>"
+"</tr></table>"
+"<table style=\'font-size:9pt\';  border=\'1\'; bgColor=\'BBBBBB\'; text-align: left; cellpadding=\'2\' cellspacing=\'2\'>"
+"<tr>"
+"<td style=\'width: 180px;\'>Steady-state (molecules/cell)</td>"
+"<td style=\'text-align: center; width: 135px;\'><input name=\'Ss_old\' type=\'text\' title=\'read only\' size=\'5\' readonly></td>"
+"<td style=\'text-align: center; width: 135px;\'><input name=\'Ss_new\' type=\'text\' title=\'read only\' size=\'5\' readonly></td>"
+"</tr><tr>"
+"<td>Half-life (minutes)</td>"
+"<td style=\'text-align: center\'><input name=\'Hl_old\' type=\'text\' title=\'read only\' size=\'5\' readonly></td>"
+"<td style=\'text-align: center\'><input name=\'Hl_new\' type=\'text\' title=\'read only\' size=\'5\' readonly></td>"
+"</tr></table><br>"
+"<table style=\'font-size:9pt\';  border=\'0\'; text-align: left; cellpadding=\'2\' cellspacing=\'2\'>"
+"<tr>"
+"<td style=\'width: 50px\'><input type=\'button\' value=\'GO\' onClick=\'simchip1_main()\'></td>"
+"<td style=\'width: 250px\'><input type=\'button\' value=\'NEW\' onClick=\'simchip1_rand()\'></td>"
+"<td style=\'width: 50px\'><input type=\'button\' value=\'HELP\' onClick=\'simchip1_help()\'></td>"
+"</tr></table>"
+"</form><br>"
+"<a href=\"./downloads/hoffmann_2008.pdf\" target=\"Literature\">Original publication (Hoffmann et al. 2008)</a><br>"
+"<a href=\"./downloads/simchip1_shortcode.html\" target=\"Source Code\">Simplified source code example</a><br>"
+"<a href=\"./downloads/mathematical_background.pdf\" target=\"Background\">Mathematical background</a><br>";
document.getElementById("divTxt").innerHTML=text;
calculate();
}//end of simchip1_input

function check_S_old(tx){
//replaces decimal komma by decimal point
tx=tx.replace(/,/, ".");
   with (document.f_inp1.S_old) {
	value = parseFloat(tx);
	if(isNaN(value)) value="1";
	if(value<S_min) value=S_min;
	if(value>S_max) value=S_max;
   }
   calculate();
}

function check_S_new(tx){
tx=tx.replace(/,/, ".");
   with (document.f_inp1.S_new) {
	value = parseFloat(tx);
	if(isNaN(value)) value="1";
	if(value<S_min) value=S_min;
	if(value>S_max) value=S_max;
   }
   calculate();
}

function check_D_old(tx){
tx=tx.replace(/,/, ".");
   with (document.f_inp1.D_old) {
	value = Math.round(parseFloat(tx)*100)/100;
	if(isNaN(value)) value="1";
	if(value<D_min) value=D_min;
	if(value>D_max) value=D_max;
   }
   calculate();
}

function check_D_new(tx){
tx=tx.replace(/,/, ".");
   with (document.f_inp1.D_new) {
	value = Math.round(parseFloat(tx)*100)/100;
	if(isNaN(value)) value="1";
	if(value<D_min) value=D_min;
	if(value>D_max) value=D_max;
   }
   calculate();
}

function calculate() {
var s1, s2, d1, d2;
   with (document.f_inp1) {
	s1=S_old.value;
	s2=S_new.value;
	d1=D_old.value/100;
	d2=D_new.value/100;
	Ss_old.value=Math.round(s1/d1);
	Ss_new.value=Math.round(s2/d2);
	Hl_old.value=Math.round(10*(0-Math.log(0.5))/d1)/10;
	Hl_new.value=Math.round(10*(0-Math.log(0.5))/d2)/10;
	if (Hl_old.value>10) Hl_old.value=Math.round(Hl_old.value);
	if (Hl_new.value>10) Hl_new.value=Math.round(Hl_new.value);

   }
}

function simchip1_rand(){
   with (document.f_inp1) {
	S_old.value=S_new.value;
	S_new.value=Math.round((Math.random()*Math.random()*Math.random()*(S_max-S_min)+S_min)*10)/10;
	D_old.value=D_new.value;
	D_new.value=Math.round((Math.random()*Math.random()*Math.random()*(D_max-D_min)+D_min)*100)/100;
   }
   calculate();
}
