// javascript for interfacing the form and the applet in Transit Kalculator// rev 2002-1-27// global varsvar debugallowed = true, initdebug = false;var debugflag = initdebug, ckptflag = initdebug;var ce, tk;var chartarray; //  = new Array();var maxcharts = 10;var currsel = "(new chart)";var expDays = 90*86400000;// debug routinesfunction titleline(){  w('<H1 ALIGN="CENTER">Transit Kalculator 1.0.5</H1>');}function debugWidgits(){  if (debugallowed)  {    var f = ( (initdebug)? " CHECKED" : "");    w('<B><I>Do not change these:</I></B><BR>');    w('<INPUT TYPE="BUTTON" VALUE="Props" onClick="showProperties();">');    w('<INPUT TYPE="CHECKBOX" NAME="debugmode"'+f+' onClick="setDebug();">Debug Mode');    w('<INPUT TYPE="CHECKBOX" NAME="ckptmode" '+f+' onClick="setDebug();">Ck Pts');  }}function ignoreWidgits(){  if (debugallowed)  {    w('<P>(The buttons and checkboxes after "<B>Do not change these</B>" are for my ');    w('use in tracking down problems.  Do not mess with them!)');  }}function setDebug(){  debugflag = ce.debugmode.checked;  ckptflag = ce.ckptmode.checked;}function st(s) // for debug msgs{  if (debugflag) alert("*"+s+"*");}function ckpt(s) // for ckpt msgs{  if (ckptflag) alert("* Ckpt "+s+" *");}function showProperties(){  var htmldoc = window.document;  var w = window.open("", "propwin", "width=400,height=200,scrollbars=yes" );  var d = w.document;  var p;  d.open();  d.write('<H2>JavaScript Object Properties</H2>');  d.write('<H4>tk:</H4><P>');  for (p in tk) { d.write(p +" * "); }  d.write('<H4>chartarray:</H4><P>');  for (p in chartarray) { d.write(p+" = "+chartarray[p].chartname+" * "); }  d.write('<H4>pulldown menu:</H4><P>');  for (p in ce.chartsel.options) { d.write(p+" = "+ce.chartsel.options[p]+" * "); }  d.close();}function errorbox(msg, url, linenum){  if (!debugflag) return false;  var s = msg+"\n"+url+"\n"+linenum;  alert(s);  return true;}/*function showCurrsel() // debug{  st(currsel+", "+ce.chartsel.selectedIndex);}*/// global functionsfunction initFormData(){  window.onerror = errorbox;  chartarray = new Array();  ce = document.chartdataform;  // st("initChart #"+chartarray.length); // debug  tk = document.tk;  ckpt("init 1");  readCookie();  var i;  for (i=0; i<ce.chartsel.options.length; i++)  {    if (ce.chartsel.options[i].text == currsel)     {       // ce.chartsel.options[i].defaultSelected = true;       // ce.chartsel.options[i].selected = true;       ce.chartsel.selectedIndex = i;       selectChart();       //   st(ce.chartsel.selectedIndex);       break;     }  }  ckpt("init 2");  minorAsp();  uploadChart();  ckpt("init 3");}function cleanup(){  ckpt("cleanup 1");  sortandtrim();  writeCookie();}function erase(){  var i;  for (i = 0; i<12; i++) ce.elements[i].value = ""; // erase all text fields  ce.chartsel.selectedIndex = 0;}function saveChart(){  var c = createChart();  st("saveChart #"+chartarray.length); // debug  chartarray[chartarray.length] = c;  var opt = new Option(c.chartname);  ce.chartsel.options[ce.chartsel.options.length] = opt;  st("menu length = "+ ce.chartsel.options.length); // debug}function uploadChart(){  ckpt("upload 1");  // upload chart from form to applet  tk.pSun = ce.pSun.value; tk.pMoon = ce.pMoon.value; tk.pMerc = ce.pMerc.value;  tk.pVen = ce.pVen.value; tk.pMars = ce.pMars.value; tk.pJup = ce.pJup.value;  tk.pSat = ce.pSat.value; tk.pUran = ce.pUran.value; tk.pNep = ce.pNep.value;  tk.pPlu = ce.pPlu.value; tk.pChi = ce.pChi.value;  tk.chartname = ce.chartname.value;  // kick off the applet to free-running mode  ckpt("upload 2");  tk.newChart();  ckpt("upload 3");  // echo back planets to the form  ce.pSun.value = tk.pSun ; ce.pMoon.value = tk.pMoon ; ce.pMerc.value = tk.pMerc ;  ce.pVen.value = tk.pVen ; ce.pMars.value = tk.pMars ; ce.pJup.value = tk.pJup ;  ce.pSat.value = tk.pSat ; ce.pUran.value = tk.pUran ; ce.pNep.value = tk.pNep ;  ce.pPlu.value = tk.pPlu ; ce.pChi.value = tk.pChi ;  ckpt("upload 4");}function selectChart(){  var sel = ce.chartsel.selectedIndex;  if (sel == 0) return;  var si = ce.chartsel.options[sel];  currsel = si.text;  // st(currsel); // debug  // find the chart in the chartarray  var i;  for (i=0; i<chartarray.length; i++)    { if (chartarray[i].chartname == currsel) break; }  if (i >= chartarray.length) { st("Cant find selected chart"); return; }  // testing  // copy from chartarray to the form  var c = chartarray[i]; // or a Chart constructor ???  ce.pSun.value = c.pSun; ce.pMoon.value = c.pMoon; ce.pMerc.value = c.pMerc;  ce.pVen.value = c.pVen; ce.pMars.value = c.pMars; ce.pJup.value = c.pJup;  ce.pSat.value = c.pSat; ce.pUran.value = c.pUran; ce.pNep.value = c.pNep;  ce.pPlu.value = c.pPlu; ce.pChi.value = c.pChi;  ce.chartname.value = c.chartname;  ckpt("select 1");  c.timestamp = getTimestamp();  ckpt("select 2");}function minorAsp(){  var flag = ce.cbminasp.checked;  tk.setMinorAspectFlag(flag);  // set flags  st("minorAsp = "+flag); // debug}// service functionsfunction createChart(){  ckpt("create 1");  var c = new Object(); // or a Chart constructor ???  c.pSun = ce.pSun.value; c.pMoon = ce.pMoon.value; c.pMerc = ce.pMerc.value;  c.pVen = ce.pVen.value; c.pMars = ce.pMars.value; c.pJup = ce.pJup.value;  c.pSat = ce.pSat.value; c.pUran = ce.pUran.value; c.pNep = ce.pNep.value;  c.pPlu = ce.pPlu.value; c.pChi = ce.pChi.value;  c.chartname = ce.chartname.value;  ckpt("create 2");  c.timestamp = getTimestamp(); // controversial stmt...  ckpt("create 3");  return c;}function getTimestamp(){  return tk.getTimestamp();}function sortandtrim(){  var i, tc;  var flag=(chartarray.length>1);  while (flag)  {    flag = false;    for (i=chartarray.length-2; i>=0; i--)    if (chartarray[i].timestamp < chartarray[i+1].timestamp)    {      tc = chartarray[i+1]; chartarray[i+1] = chartarray[i]; chartarray[i] = tc;      flag = true;    }  }  var deltime = tk.getDeleteTime();  // trim "too many" and expired charts  if (chartarray.length >maxcharts) chartarray.length = maxcharts;  while ( (chartarray.length>0) && (chartarray[chartarray.length-1].timestamp < deltime) )     chartarray.length--;}function encodeChart(c){  var s ="*"+escape(c.pSun)+e(c.pMoon)+e(c.pMerc)+e(c.pVen)+e(c.pMars)+e(c.pJup)+             e(c.pSat)+e(c.pUran)+e(c.pNep)+e(c.pPlu)+e(c.pChi)+             e(c.chartname)+e(c.timestamp);  // st(s); // debug  return s;}function e(s){ return "$"+escape(s); }function readCookie(){  var k = document.cookie;  // st("Kookie length = "+k.length); // debug  var a = k.split(";");  var i, j=-1, opt, c, sel;  for (i=0; i<a.length; i++)  { if (a[i].substr(0,12) == "astrocharts=") { j=i; break; } }  if (j<0) return; // no cookie  // st(a[j]); // debug  k = a[j].substr(12);  a = k.split("*");  currsel = unescape(a[0]);  ce.chartsel.options.length = 1; // reset select to receive cookie  for (i=1; i<a.length; i++)  {    c = decodeChart(a[i]);    chartarray[chartarray.length] = c;    sel = (c.chartname == currsel);    opt = new Option(c.chartname, c.chartname , sel, sel);    ce.chartsel.options[ce.chartsel.options.length] = opt;  }  // ce.chartsel.options[0] = null;    // selectChart();    //       st(ce.chartsel.selectedIndex); // debug   // if (chartarray.length>0) timest = chartarray[0].timestamp+1; // going away??  // select dflt chart?? no}function writeCookie(){  var k = "astrocharts=" + escape(currsel);  if ( chartarray.length > 0)  {    var i;    for (i=0; i<chartarray.length; i++) { k += encodeChart(chartarray[i]); }    var expms = (new Date()).getTime()+expDays;    k += "; expires="+(new Date(expms)).toGMTString(); // +"; secure";  }  document.cookie = k;  // st("Kookie length = "+k.length); // debug}function decodeChart(s){  var a = s.split("$");  var i;  for (i=0; i<a.length; i++) a[i] = unescape(a[i]);  var c = new Object(); // or constructor??  c.pSun = a[0]; c.pMoon = a[1]; c.pMerc = a[2]; c.pVen = a[3]; c.pMars = a[4]; c.pJup = a[5];  c.pSat = a[6]; c.pUran = a[7]; c.pNep = a[8]; c.pPlu = a[9]; c.pChi = a[10];  c.chartname = a[11]; c.timestamp = a[12]-0;  return c;}
