//<script language="JavaScript"> 
//<!-- Hide the script from old browsers -- 
var myimage,image='specimage';
var imagelayer='spectrum', boxlayer='cursor1', displaylayer='display';
var clickcount,specaction="Select Frequency",mybox
var ppm,hz,evt,windowblocked
var plottext='<B>Move mouse over spectrum display <I>ppm</i> values</b>'
var outlimits='<B><I>Cursor outside limits</i></b>'
var clickleftplot='SELECT FREQUENCY: Click on spectrum to select first frequency'
var clickrightplot='SELECT FREQUENCY: Click on spectrum to select second frequency'
var clicktoexpand='Click on <B>[Set New Plot Limits]</b> to expand'
var setintsinitmsg='No region defined'
var imgwidth,imgheight,imgleft,imgtop
var imgwidth0,imgheight0,imgleft0,imgtop0
var leftmarg0=41,rightmarg0=45,topmarg0=70,bottommarg0=145;
var nilines, intcount
var expandinit='',expandinitnew=none,expandinitold=none,expandcurfunc=none
ivalues= new Array()
iselect=new Array()
nivalues= new Array()
//onload = init();


function init() {
    myspectrum = xGetElementById('specimage')
    myspeclayer = xGetElementById(imagelayer) 
    mycurppm=xGetElementById('curppm')
    if (myspectrum != null & mycurppm != null) {
      xAddEventListener(myspectrum,'mousemove',viewxvalue)
//     xAddEventListener(myspectrum,'mouseover',viewxvalue)
     xAddEventListener(myspectrum,'click',putvalue)
     xAddEventListener(myspectrum,'mouseout',mouseoutinit)
   }

    mybox=xGetElementById(boxlayer)
    if (mybox != null) {
      xAddEventListener(mybox,'mousemove',viewxvalue)
      xAddEventListener(mybox,'click',putvalue)
    }

    mybutton = xGetElementById('specaction1')
    if (mybutton != null &  xDef(document.form3))  {xAddEventListener(mybutton,'click',setaction)}
    mybutton = xGetElementById('specaction2')
    if (mybutton != null &  xDef(document.form3))  xAddEventListener(mybutton,'click',setaction)
    xInnerHtml(displaylayer,plottext)
    clickcount=0;
    intcount=0

//    mybutton=xGetElementById('expand')
//    if (mybutton != null) {
//         if (xDef(document.form3.sf)) {xAddEventListener(mybutton,'click',zoom);}
//         mybutton.disabled=true}

    mybutton=xGetElementById('cancel')
   if (mybutton != null) {
     if ( xDef(document.form3.sf)) ChangeButton(mybutton,'same','click',none,none,true)
// xAddEventListener(mybutton,'click',ResetCursor)
//      mybutton.disabled=true 
      }
   
    mybutton=xGetElementById('setints')
    xAddEventListener(mybutton,'click',none)

    myimage=xGetElementById('specimage')
      if  (myimage) {
      imgwidth0=xWidth(myimage); imgheight0 =xHeight(myimage);
      imgleft0=xLeft(myimage); imgtop0=xTop(myimage);
      ResetImage();
      if (xDef(document.form2.integtype)) {
        if (document.form2.integtype[2].checked) {DrawCurrentInts() }
     }  
  }
//    alert(myimage+': '+imgwidth+' x '+imgheight)
    InitBox(boxlayer,'hide',0,0,1,imgheight)
//    ResetCursor(); 
//   moveable = new Array()
//   moveable=xGetElementsByClassName('moveable');
//   if (moveable.length > 0)
 {InitWindows('moveable');   }
}
function mouseoutinit(evt) {
var e = new xEvent(evt)
if (e.target.id == 'specimage') {
   drawtext();
   InitBox(boxlayer,'hide',0,0,0,0)
   xInnerHtml(displaylayer,'Move Mouse over Spectrum to '+specaction)
//   alert(xDisplay(boxlayer)) 
   }
}
function drawtext() {

phrase = 'Move Mouse Over Spectrum to '+specaction
xInnerHtml(displaylayer,phrase)
//document.form3.text.value = phrase;
document.form3.curppm.value="-";
document.form3.dppm.value="-";
document.form3.hz.value="-";
document.form3.dhz.value="-";
}
function viewxvalue(evt) {
//alert('x-values')
var e = new xEvent(evt)
//alert(e.target.id)
var x,y;
if (e.target.id == 'specimage') {
  x = e.offsetX-10
  y = e.offsetY 
  }
else if (e.target.id == boxlayer) {
     x = xLeft(boxlayer)
  } 
// alert(e.type+' on '+e.target.id+' at x='+x)
//document.form3.curppm.value=x;
//document.form3.dppm.value=y
//width=612
//count = document.form3.clickcount.value;
//specaction = document.form3.specaction.value
sf = document.form3.sf.value;
if (specaction == "Select Frequency") {
if (x > leftmarg & x < imgwidth-rightmarg) {
   pl1 = document.form3.plotf2pl1.value;
   pl2 =  document.form3.plotf2pl2.value;
   lastppm = document.form2.plotf2pl1.value;
   var xfract = (x-leftmarg)/(imgwidth-leftmarg-rightmarg);
   deltappm = pl1 - pl2;
   ppm = pl1 - (xfract * deltappm);
   hz = ppm * sf;
   expon = 5-(Math.round(Math.log(Math.abs(deltappm))*Math.LOG10E)); accppm=Math.pow(10,expon);
//    xMoveTo(boxlayer,x,0)
   InitBox(boxlayer,'blackbar',x,0,3,imgheight)
   if (clickcount == "0") { 
      document.form3.curppm.value=Math.round(ppm*accppm)/accppm;
      document.form3.dppm.value="-";
      document.form3.hz.value=Math.round(hz*accppm/100)/(accppm/100);
      document.form3.dhz.value="-";
//      document.form3.text.value="Click on Spectrum to set left plot limit";
      xInnerHtml(displaylayer,clickleftplot)     
     }
   if (clickcount == "1") {
      document.form3.curppm.value=Math.round(ppm*accppm)/accppm;
      document.form3.dppm.value = lastppm - ppm;
      document.form3.hz.value=Math.round(hz*accppm/100)/(accppm/100);;
      document.form3.dhz.value= lastppm*sf - hz;
      xInnerHtml(displaylayer,clickrightplot)
//      document.form3.text.value="Click on Spectrum to set right plot limit";}
       }
}
else { 
// document.form3.text.value="Cursor outside spectrum"; 
 xInnerHtml(displaylayer,outlimits)               }
}
txt=xInnerHtml(displaylayer)+' x='+x+' event='+e.type+' layer='+e.target.id+'<BR>'
//xInnerHtml(displaylayer,txt)
}
function viewyvalue(evt) {
//alert('y-values')
var e = new xEvent(evt)
//alert(e.target.id)
var x,y;
if (e.target.id == 'specimage') {
   x = e.offsetX
   y = e.offsetY+10 
   }
else if (e.target.id == boxlayer) {
   x = xLeft(boxlayer)
   } 
if (y < imgheight-bottommarg) {
   InitBox(boxlayer,'blackbar',0,y,imgwidth,3)
   zerocm = document.form3.zerocm.value
   var yfract = (y-topmarg)/(imgheight-topmarg-bottommarg);
   cm = 12*(1. - yfract) - zerocm
   document.form3.curppm.value=cm
   if (specaction == "Set y-scaling") {
     if (cm < 0) {scale = Math.abs(zerocm/cm)}
     if (cm > 0) {scale = Math.abs((12-zerocm)/cm)}
     if (cm == 0) {scale = 1} 
     document.form3.hz.value=scale
     xInnerHtml(displaylayer,'Click on height to scale to top of page')
     }
   else {xInnerHtml(displaylayer,'Click on height to set as minimum peak pick')}
    }
else {
//   document.form3.text.value="Out of limits"
   xInnerHtml(displaylayer,'Cursor below spectrum')
   }
}
function putvalue() {
//specaction=document.form3.specaction.value
if (specaction == "Select Frequency") {
  ppm = document.form3.curppm.value;
  x = xLeft(mybox);
  xprev = xLeft('cursor2')
  //count=document.form3.clickcount.value;
 if (clickcount == 0) {
   //document.form3.clickcount.value="1";
   clickcount=1 

   document.form2.plotf2pl1.value=ppm;
//   document.form3.text.value="Click on Spectrum to set right plot limit";
    
   document.form3.dppm.value=0;
   document.form3.dhz.value=0;
   InitBox('cursor2','redbar',x,0,3,imgheight)
   mybutton=xGetElementById('cancel')
   ChangeButton('cancel','same','click',none,ResetCursor,false)
//   mybutton.disabled=false
   ChangeButton('expand','Calibrate Spectrum','click',expandcurfunc,GetReference,false) 
   expandcurfunc=GetReference 
   ChangeButton('setints',' ','click',DeleteIntegral,none)
   ChangeButton('setints',setintsinitmsg,'click',AddIntegral,none)

}
else if (clickcount == 1) {
//   document.form3.clickcount.value="0";
   clickcount=0
   xInnerHtml(displaylayer,clicktoexpand)
   document.form3.dppm.value="-";
   document.form3.dhz.value="-";
   mybutton=xGetElementById('expand')
   ChangeButton('expand','Zoom Image','click',expandcurfunc,zoom,false)
   expandcurfunc=zoom
   if (x < xprev) {
       document.form2.plotf2pl2.value=document.form2.plotf2pl1.value;
       document.form2.plotf2pl1.value=ppm;
       InitBox('cursor2','zoombox',x,topmarg,xprev-x,imgheight-bottommarg) 
       if ((xDisplay(xGetElementById('edints'))) == 'block') {CheckInts(ppm,document.form2.plotf2pl2.value)}      
    }
   else { 
        document.form2.plotf2pl2.value=ppm;
//   document.form3.text.value="Click [Set new Plot Limits] to expand";
         InitBox('cursor2','zoombox',xprev,topmarg,x-xprev,imgheight-bottommarg)
         if ((xDisplay(xGetElementById('edints'))) == 'block') {CheckInts(document.form2.plotf2pl1.value,ppm)}
         }  
    }
}
else {
   y = xTop(mybox)
   InitBox('cursor2','zoombox',leftmarg,y,imgwidth-leftmarg-rightmarg,imgheight-y-bottommarg+5) 
   mybutton=xGetElementById('cancel')
   ChangeButton(mybutton,'Reset Selection','click',none,ResetCursor,false)
  if (specaction == "Set y-scaling") {
     mybutton=xGetElementById('expand')
     ChangeButton(mybutton,'Zoom Image','click',expandcurfunc,zoom,false)
     expandcurfunc=zoom
     scale = document.form3.hz.value
     document.form2.factor.value=scale
     xInnerHtml(displaylayer,'Click [-times] to rescale')
   }
  else if (specaction == "Set minimum peak picking") {
    ChangeButton('expand','Pick Peaks','click',expandcurfunc,submitform,false)
    expandcurfunc=submitform
     mi = Math.abs(document.form3.curppm.value)
     document.form2.mi.value=mi
     document.form4.mi.value=mi
     xInnerHtml(displaylayer,'Redo Peak picking (xwp_pp)')
    }
  }
}
function setaction(evt) {
e = new xEvent(evt) 
mybutton=xGetElementById(e.target.id)
lastspecaction=specaction
specaction=e.target.value
mybutton.value=lastspecaction
ChangeButton('expand','','click',expandcurfunc,none,true)
ChangeButton('cancel','Reset Selection','click',none,none,true)
InitBox('cursor2','hide')
if (specaction == "Select Frequency") {
   InitBox(boxlayer,'blackbar',leftmarg,0,3,imgheight)
   ChangeButton(image,'dummy','mousemove',viewyvalue,viewxvalue)  
   clickcount="0";
   document.form3.curppmun.value="ppm"; document.form3.dppmun.value="ppm"; 
   document.form3.hzun.value="Hz"; document.form3.dhzun.value="Hz"; }
else {
   InitBox(boxlayer,'blackbar',0,imgheight-bottommarg,imgwidth,3)
   ChangeButton(image,'dummy','mousemove',viewyvalue,viewxvalue) 
   ChangeButton(image,'dummy','mousemove',viewxvalue,viewyvalue)
   document.form3.curppmun.value="cm"; document.form3.dppmun.value=""; 
   document.form3.hzun.value=""; document.form3.dhzun.value=""; 
   if (specaction == "Set y-scaling") {document.form3.hzun.value="times"}}
drawtext()
}
function ResetCursor() {
   x=xLeft('cursor2'); y=xTop('cursor2');
   InitBox(boxlayer,'restore',x,y)
   InitBox('cursor2','hide',0,0)
   clickcount=0;
   mybutton=xGetElementById('cancel')
   if (mybutton != null) ChangeButton(mybutton,'same','click',ResetCursor,none,true)
   mybutton=xGetElementById('expand')
   if (mybutton != null) {ChangeButton('expand','','click',expandcurfunc,none,true);
                           expandcurfunc=none} //mybutton.disabled=true
   mybutton=xGetElementById('setints')
   if (mybutton != null) {
       ChangeButton('setints',setintsinitmsg,'click',AddIntegral,none)
       ChangeButton('setints','same','click',DeleteIntegral,none)
   }
  
   document.form2.plotf2pl1.value=document.form3.plotf2pl1.value;
   document.form2.plotf2pl2.value=document.form3.plotf2pl2.value;
   document.form2.factor.value=1;
   document.form4.mi.value=document.form3.mi.value
}
function InitInts() {
mytext=xGetElementById('intrng')
integraltext=mytext.value
//alert(integraltext)
ilines=integraltext.split('\n')
nilines=ilines.length
bla=nilines+'lines\n'
for (i=0;i<nilines;i++) {
   if (xDef(ilines[i]) & ilines[i] != '') {
     temp=ilines[i].toString()
     ivalues[i]=temp.split("  ")
     nivalues[i] = ivalues[i].length
     bla=bla+'line'+i+' - '+nivalues[i]+'entries: '
     for (j=0;j<nivalues[i];j++) {
       if (ivalues[i][j] == '') {
         for (k=j;k<nivalues[i]-1;k++) {
            ivalues[i][k] = ivalues[i][k+1]
            ivalues[i][k+1] = ''
            }
         ivalues[i].length=ivalues[i].length-1 
         }
       nivalues[i] = ivalues[i].length
       bla=bla+' ('+j+')'+ivalues[i][j] 
       } 
     }
   else {nilines=nilines-1}   
   }
bla=bla+'\n'+nilines+' nonzero lines'
//alert(bla)
}
function CheckInts(ppmleft,ppmright) {
icount=0
for (i=1;i<nilines;i++) {
   if (Number(ppmleft) > Number(ivalues[i][0]) & Number(ppmright) < Number(ivalues[i][1])) {
      iselect[i]='selected'
      icount=icount+1
      }
   }
//alert(icount)
if (icount > 0) {ChangeButton('setints','Delete '+icount+' selected integrals','click',none,DeleteIntegral)}
else {ChangeButton('setints','Add Region as Integral','click',none,AddIntegral)}
}
function DrawCurrentInts() {
InitInts()
ppmleft=Number(document.form3.plotf2pl1.value);
ppmright=Number(document.form3.plotf2pl2.value);
deltappm = ppmleft - ppmright;
specpixels=imgwidth-leftmarg-rightmarg;
factor=imgwidth/(specpixels+rightmarg+leftmarg)
ileft='undef'; iright='undef'; ileftpx='undef'; irightpx='undef';
for (i=1;i<nilines;i++) {
   ileft=Number(ivalues[i][0]);
   iright=Number(ivalues[i][1]);
   if (xNum(ileft) & ppmleft > ileft & ppmright < iright) {
     var ileftpx = leftmarg+specpixels*((ppmleft-ileft)/deltappm)
     var iwidthpx = factor*specpixels*((ileft-iright)/deltappm)
     newmarker='<DIV  class="curint" STYLE="left:'+ileftpx+'; width:'
+iwidthpx+';"> </div>'
     markertext=xInnerHtml('intmarkers')+newmarker
     xInnerHtml('intmarkers',markertext)
   }
//alert(ppmleft+'-'+ppmright+' : '+ileft+'  '+iright+' = '+ileftpx+'  '+iwidthpx+'\n'+)
}
}
function DeleteIntegral() {
//alert('Delete Integral')
mytext=xGetElementById('intrng')
inttext=ivalues[0].join('  ')
if (confirm('Are you sure ?')) {
for (i=1;i<nilines;i++) {
   if (iselect[i] != 'selected') {inttext=inttext+'\n'+ivalues[i].join('  ')} 
   }
mytext.value=inttext
marker='<DIV  class="deleteint" STYLE="left:'+xLeft('cursor2')+'px; width:'
+xWidth('cursor2')+'px;"> </div>'
text=xInnerHtml('intmarkers')+marker
xInnerHtml('intmarkers',text)
ChangeButton('setints',setintsinitmsg,'click',DeleteIntegral,none)
InitInts()
}
}
function AddIntegral() {
//alert('Add Integral')
mytext=xGetElementById('intrng')
if (nilines==0) {inttext='P 0\n'; itype='P'; newlines=1}
else {inttext=''; newlines=0; itype=ivalues[0][0].substr(0,1)}
//alert('initially:'+nilines+' lines \n'+inttext)
leftppm=document.form2.plotf2pl1.value
rightppm=document.form2.plotf2pl2.value
i=0; j=0
while (i<nilines) {
   if (Number(leftppm) < Number(ivalues[i][0]) | isNaN(ivalues[i][0]) )   {
//     alert(i+'/'+nilines+' '+' '+leftppm+'<'+ivalues[i][0])
     inttext=inttext+ivalues[i].join('  ')+'\n'
//     alert(inttext) 
     j++
     }
   i++
   }
//alert(j+'/'+nilines+'Now the new line is added')
inttext=inttext+leftppm+'  '+rightppm
newlines++
//alert(inttext)
if (itype == 'A' ) {inttext=inttext+'  0  0  #Added in webplot \n' }
else { inttext=inttext+'  1  #Added in webplot \n'}
for (i=j;i<nilines;i++) {
//   alert(i+'/'+nilines+' '+leftppm+'>'+ivalues[i][0])
   inttext=inttext+ivalues[i].join('  ')+'\n'
//   alert(inttext)
   }
intcount++
//inttext=inttext+'\n'
marker='<DIV  class="addint" STYLE="left:'+xLeft('cursor2')+'px; width:'
+xWidth('cursor2')+'px;"> </div>'
text=xInnerHtml('intmarkers')+marker
xInnerHtml('intmarkers',text)
nilines=nilines+newlines
mytext.value=inttext
ChangeButton('setints',setintsinitmsg,'click',AddIntegral,none)
InitInts()
}
function RestoreIntegral() {
currentints=xGetElementById('intrng')
currentints.value=currentints.defaultValue
xInnerHtml('intmarkers','')
InitInts()
}
function GetReference() {
//alert('Hello Get Reference')
mybutton=xGetElementById('expand')
//alert(mybutton.id)
myform=mybutton.form
submitname='js_select'
curppm=Number(document.form2.plotf2pl1.value)
var newppm=curppm
newppm=Number(prompt('Enter new ppm value',curppm))
oldoffset=Number(document.form3.offset.value)
if (newppm) {var diff = newppm-curppm } else {return false}
//alert(diff+' = '+newppm+' - '+curppm)
newoffset=oldoffset+diff
if (diff == 0) {return false}
else {
  if (confirm('Left offset will be changed from '+oldoffset+' to '+newoffset)) {
//   mybutton.type='submit'
   myform.elements['offset'].value=newoffset
   myform.elements[submitname].value="Set Reference"
   myform.submit()
  }
 }
}
//--> 
//</SCRIPT>
