// JPOLE.JS          started: 03/02/2001       Last update: 07/11/2001

// 05/16/2003 - Ver 2.20 - Added feature to print the data window.
//                         Added DispWin.document.close(); to finish
//                         the writing operation to the data window.

var cookie_array = new Array();

// -------------------------------------------------------------------
// This function calculates the jpole lengths and then displays them.
// inputs: the form data from JPOLE.HTM
// output: Displays computed results to a new window
// Action: Waits for a click of the mouse on the [X]
// return: nothing
// 03/22/2001 - Added code to save the Velocityfactor and the Center
//              frequency in a cookie. Expiration date = 1 month.
// 07/11/2001 - Modified/Added code to get and save ALL cookie data
// -------------------------------------------------------------------
function JPCalcDisp() {

 Units = new Array(4);
   Units[0] = " in";
   Units[1] = " ft";
   Units[2] = " cm";
   Units[3] = " m";

 labels1 = new Array(6);
   labels1[0] = "DESIGN";
   labels1[1] = "LENGTH OF";
   labels1[2] = "LENGTH OF";
   labels1[3] = "TAP";
   labels1[4] = "GAP";
   labels1[5] = "";
 labels2 = new Array(6);
   labels2[0] = "FREQ";
   labels2[1] = "stub(1/4 WAVE)";
   labels2[2] = "RADIATOR";
   labels2[3] = "DISTANCE";
   labels2[4] = "DISTANCE";
   labels2[5] = "Total length";

 labels3 = new Array(6);
   labels3[0] = "(MHz)";
   for(i=1; i<labels3.length; ++i)
      labels3[i] = "(inches)";


     // Velocity Factor 0.81-.85, twin-lead
 vf=document.Jpole.VelFact.value;

     // Center design Frequency in MHz
 var freq=document.Jpole.CentFreq.value;
 var Rtn = JPValidate(vf, freq);
 
 if(Rtn == false)
    return Rtn;  // Die if Rtn is false, some input error.

 // The toolbar dosen't allow the user to print out the ressults!
 // No use having it then!  Makes a seperate window!
 DispWin = window.open('','NewWin', 'toolbar=no,status=no,width=650,height=475,resizable=yes,screenX=50,screenY=40,left=50,top=40');

     // Center design Frequency in MHz
 var Us;
 for(i=0; i<document.Jpole.units.length; i++) {
    if(document.Jpole.units[i].checked)
       Us = i;
 }
 // OK This works, we get a number 0 - 3, need to convert or use that.


     // on if user is putting antenna inside PVC
 var PVC=document.Jpole.PVC.checked;  // This works now, get true/false.

 var air="air";   // Antenna designed for air

 if(PVC==true) {
    air = "PVC";
 }

 var gap = .25;                   // Gap width in English, inches
 var units=" in";
 var delta = 0.25;                // 0.25 inches, change in antenna length
 var factor = 11811;     // Conversion factor for English inches, default



 // Now get the correct units
 if(Us == 1 || (Us == 0 && freq < 60.0)) { // Want in ft OR freq < 60 MHz
   labels3[1] = "(feet)";
   labels3[2] = "(feet)"; // No change in [3] & [4]
   labels3[5] = "(feet)";
   Us = 1; // Force the issue because it's < 60 MHz
 }
 else if (Us == 3 || (Us == 2 && freq < 60.0)) { // Want in m OR freq < 60 MHz
   factor = 30000;     // change to Conversion factor for Metric, cm
   gap *= 2.54;                 // Gap is about 6mm or 0.6 cm, Metric
   delta = 5.0;                   // 5.0 cm, change in antenna length
   labels3[1] = "(m)";
   labels3[2] = "(m)";
   labels3[3] = "(cm)";
   labels3[4] = "(cm)";
   labels3[5] = "(m)";
   Us = 3; // Force the issue because it's < 60 MHz
 }
 else if (Us == 2) {
   factor = 30000;     // change to Conversion factor for Metric, cm
   gap *= 2.54;                 // Gap is about 6mm or 0.6 cm, Metric
   delta = 5.0;                   // 5.0 cm, change in antenna length
   for(i=1; i<labels3.length; ++i)
      labels3[i] = "(cm)";
 }

 var Adjust = 0.95;          // Adjustment for being inside PVC
 if(PVC == true) {           // If antenna in PVC then adjust the lengths
    factor *= Adjust;
 }


 var lambda = (factor/freq);    // Wavelength calc

 var stub = vf * lambda/4;      // Stub length
     stub = Math.round(100*stub)/100;

 var radiator = vf * lambda/2;  // Radiator length
     radiator = Math.round(100*radiator)/100; 

 var tap = lambda * .0155;      // Tap position
     tap = Math.round(100*tap)/100;

 var total = stub + radiator;   // Total length of J-pole
     total = Math.round(100*total)/100;

 var change = freq - ((vf * factor/4)/(stub+delta)); // change in freq
     change = Math.round(10*change)/10;


 if(Us == 3) {        // convert from cm to m
    stub /= 100;      // Stub length
      stub = Math.round(100*stub)/100;
    radiator /= 100;  // Radiator length
      radiator = Math.round(100*radiator)/100;
    total /= 100;     // Total length of J-pole
      total = Math.round(100*total)/100;
 }
 else if(Us == 1) {    // convert from in to ft
    stub /= 12;       // Stub length
      stub = Math.round(100*stub)/100;
    radiator /= 12;   // Radiator length
      radiator = Math.round(100*radiator)/100;
    total /= 12;      // Total length of J-pole
      total = Math.round(100*total)/100;
 }


 // --------------------------------------------------------------------
 // Set the cookie to expire in one month, not needed longer I'm sure 
 // --------------------------------------------------------------------
 expireDate= new Date;
 expireDate.setMonth(expireDate.getMonth()+1);

 // ----------------------------------------------------------
 // Write the velocity factor & center frequency to the cookie 
 // ----------------------------------------------------------
 var txt = ""; // empty cookie string // pk01
 if(!cookie_array.length) { // No cookie for us, make one.
   txt += "vf="+vf+";Freq="+freq;
   txt += ";expires=" + expireDate.toGMTString(); // Add the expiration
 }
 else {
   var donevf = 0;
   var donefreq = 0;
   for(j=0; j<cookie_array.length; ++j) {
      var tmpsplit = cookie_array[j].split("=");
      if (tmpsplit[0] == "vf") { // find our stuff
         txt += tmpsplit[0]+"="+vf+";"
         ++donevf;
      }
      else if (tmpsplit[0] == "Freq") { // find our stuff
         txt += tmpsplit[0]+"="+freq+";"
         ++donefreq;
      }
      else if (tmpsplit[0] == "expires") { // find our stuff
         if(!donevf && !donefreq) { // New for this program
           txt += "vf="+vf+";Freq="+freq+";"; // Add our cookie data
         }
         txt += tmpsplit[0]+"="+expireDate.toGMTString();
      }
      else { // build cookie string back up
         txt += tmpsplit[0]+"="+tmpsplit[1]+";"
      }
   }
 }
 document.cookie = escape(txt); // Write the cookie
// window.alert(txt); // display full cookie to me too!
 // --------------------------------------------------------------------


 var message = "<html>";
 message += "<body bgcolor=#ffddff>";
 message += "<center>";
 message += "<H1> Wire J-Pole Calculation Program </H1>";
 message += "<h3> &copy copyright Circle Software 2001, 2003 </h3><br>";

 message += "<table width = 600 border=\"1\"> <!-- Labels & results table -->";
 message += "  <tr>";
 message += "    <td align=\"center\">";
 message += "       <b>"+labels1[0]+"</b><br>";
 message += "       <b>"+labels2[0]+"</b><br>";
 message += "       <b>"+labels3[0]+"</b>";
 message += "    </td>";
 message += "    <td align=\"center\">";
 message += "       <b>"+labels1[1]+"</b><br>";
 message += "       <b>"+labels2[1]+"</b><br>";
 message += "       <b>"+labels3[1]+"</b>";
 message += "    </td>";
 message += "    <td align=\"center\">";
 message += "       <b>"+labels1[2]+"</b><br>";
 message += "       <b>"+labels2[2]+"</b><br>";
 message += "       <b>"+labels3[2]+"</b>";
 message += "    </td>";
 message += "    <td align=\"center\">";
 message += "       <b>"+labels1[3]+"</b><br>";
 message += "       <b>"+labels2[3]+"</b><br>";
 message += "       <b>"+labels3[3]+"</b>";
 message += "    </td>";
 message += "    <td align=\"center\">";
 message += "       <b>"+labels1[4]+"</b><br>";
 message += "       <b>"+labels2[4]+"</b><br>";
 message += "       <b>"+labels3[4]+"</b>";
 message += "    </td>";
 message += "    <td align=\"center\">";
 message += "       <b>"+labels1[5]+"</b><br>";
 message += "       <b>"+labels2[5]+"</b><br>";
 message += "       <b>"+labels3[5]+"</b>";
 message += "    </td>";
 message += "  </tr>";
 message += "  <tr>";
 message += "    <td align=\"center\">";

 var str = "<font color=#0000ff>";
 message += str + freq + "</font></td><td align=\"center\">";
 message += str + stub + "</font></td><td align=\"center\">";
 message += str + radiator + "</font></td><td align=\"center\">";
 message += str + tap + "</font></td><td align=\"center\">";
 message += str + gap + "</font></td><td align=\"center\">";
 message += str + total + "</font></td></tr>";


 message +=  "</table><br>";   // end of the display results table //1
 message += "</center>";

 message += "<table> <!-- Places Information in table order -->";
 message += "<TR>";
 message += " <P ALIGN=\"JUSTIFY\">";
 message += "   <TH>";
 message += "      Designed for: " + air + "<br>";

 str = " MHz  for changes of ";
 message += "      Shift: " + change + str + delta + Units[Us] + "<br>";
 message += "      vf: " + vf + " <br>";
 message += "      &nbsp;";
 message += "   </TH>";
 message += "<!-- ================================== -->";
 message += "<!--       J-pole Antenna Graphic       -->";
 message += "<!-- ================================== -->";
 message += "   <TH>";
 message += "   <center>";
 message += "   <img src=\"../gif/JPOLE.GIF\">";
 message += "   </center>";
 message += "   </TH>";
 message += " </P>";
 message += "</TR>";
 message += "</table>";   // end of the display results table //1
 message += "<!-- ================================== -->";

 message += "<!-- ******************************************************** -->";
 message += "<!--       This is the (return to previous page) section      -->";
 message += "<!-- ******************************************************** -->";
 message += "   <form>";
 message += "      Click: <input type=\"button\" name=\"CloseMe\" value=\"Close\" onClick=\"window.close();\">";
 message += "   To Close this window";
 message += " &nbsp; ";
 message += "      <input type=\"button\" name=\"Print\" value=\"Print\" onClick=\"javascript:window.print();\">";
 message += "   To Print this window";
 message += "   </form> ";
 message += "<!-- ******************************************************** -->";
 message += "<br>";
 message += "</body>";
 message += "</html>";

 DispWin.document.write(message); // Makes the different window
 DispWin.document.close(); // "closes" the page for writing
// document.write(message); // Uses the same window, we lose inputs tho
 return Rtn;  // line 225

} // End of: JPClacDisp


// -------------------------------------------------------------------
// This function Checks the inputs to see if they are numbers
// inputs: a string
// output: nothing
// Action: none
// return: (0/1) = (Not a number / Is a number)
// -------------------------------------------------------------------
function JPValidate(vf1, freq1) {

   var Rtn = true;
   var me = -1;
   var m0 = 30, m1 = 999;  // min/max for freq
   var M0 = .7, M1 = .99;  // min/max for vf

   if(isNaN(vf1)) {
       alert("The Velocity Factor, MUST be a number, please try again.");
       Rtn = false;
       me = 1;
    }
    else if(isNaN(freq1)) {
       alert("The Design Frequency, MUST be a number, please try again.");
       Rtn = false;
       me = 0;
    }

   if(Rtn == true) {
     var vf = Number(vf1);
     var freq = Number(freq1);

     if (document.Jpole.CentFreq.value.length < 1) {
         alert("Please enter the Design Frequency.");
         Rtn = false;
         me = 0;
     }
     else if (freq < m0 || freq > m1) {
         str = "The Design Frequency is out of the allowed range";
         alert(str + ", please try again.");
         Rtn = false;
         me = 0;
     }

     if (document.Jpole.VelFact.value.length < 1 && me == -1) {
         alert("Please enter the Velocity Factor.");
         Rtn = false;
         me = 1;
     }
     else if ((vf < M0 || vf > M1) && me == -1) {
         str = "The Velocity Factor is out of the allowed range";
         alert(str + ", please try again.");
         Rtn = false;
         me = 1;
     }
   }
   if (me >= 0) {
      document.Jpole.elements[me].focus();
   }
   return Rtn;
}

// -------------------------------------------------------------------
// This function checks to see if the cookie exists and if it does it
// then gets the saved data
// ------------
// inputs: none
// output: saved values to correct form variables
// Action: none
// return: none
// -------------------------------------------------------------------
// NOTE: If we have two programs that save cookies then they will
//       over-write each other. The only way to protect other cookie
//       info from other programs of ours is to first READ all of our
// cookie information saving the order and titles and values of each.
// Then change just this programs values and resave the entire 
// cookie.
//
// QUICK FIX: Check to be sure these are our data & only if they are
//            set them into the form for use.
// -------------------------------------------------------------------
function LoadMe() {  // pk01
   window.status="Wire J-Pole Calculation Program Ver 2.10";
   if(document.cookie != "") {

      var tmptxt = "Our Cookie:\n\n";  // For debugging the cookie decoder

      // Copy the cookie info to a new string
      var cookie_string = unescape(document.cookie);

      // Split the string into a new array
      cookie_array = cookie_string.split(";");

      for(j=0; j<cookie_array.length; ++j) {
         tmptxt += cookie_array[j]+"\n"; // For debugging the cookie decoder

         var tmpsplit = cookie_array[j].split("=");
         if (tmpsplit[0] == "vf") { // find our stuff
            document.Jpole.VelFact.value = tmpsplit[1];
         }
         else if (tmpsplit[0] == "Freq") { // find our stuff
            document.Jpole.CentFreq.value = tmpsplit[1];
         }
      }


      // Further split the first entry into the title and value
      //var velfacts = cookie_array[0].split("=");

      // Further split the second entry into the title and value
      //var centfreq = cookie_array[1].split("=");

      //window.alert(tmptxt);  // For debugging the cookie decoder

   }
}


// -------------------------------------------------------------------
// This function swaps two images so we can do a roll-over
// inputs: two image file names
// output: none
// Action: New image put in place of image being used
// return: none
// -------------------------------------------------------------------
function swapImages(ImageName, NewImage) {

   // test to see if the browser understands rollover
   // If it does swap one image for the other
   if (document.images) {
      document[ImageName].src = NewImage;
   }
}

// -------------------------------------------------------------------
// On Mouse Over we display the txt in the status bar, and myimage at
// the lnk position.
// inputs: txt, lnk, image
// output: none
// Action: calls swapImages to place the sent image at the lnk position
// return: none
// -------------------------------------------------------------------
function RollMeOver(txt, lnk, myimage) {
   window.status=txt;
   swapImages(lnk, myimage);
//   return true;  // is this needed? Seems to work without it.
}

// -------------------------------------------------------------------
// On Mouse Over we clear the status bar, and put myimage at
// the lnk position.
// inputs: lnk, image
// output: none
// Action: calls swapImages to place the sent image at the lnk position
// return: none
// -------------------------------------------------------------------
function ClearMe(lnk, myimage) {
   window.status="";
   swapImages(lnk, myimage);
}
