// Global Variables
var prevAreas = "";
var prevTotal = 0;
var totalAreas = 0;

var measDim = 0;
var measDimText = "FT";
var measAreaDimText = "SQ FT";
  
function funcOnLoad() { // shell for preload functions
}
  function addArea () {
    
    var isSquare = 0;
    var isRect   = 0;
    var isCircle = 0;
    var isSomething = 0;
    
    var atmps = 0;
    var atmpr = 0;
    var atmpc = 0;
    
    var tmpName = "";
    var tmp_xy = 0;
    var tmp_x  = 0;
    var tmp_y  = 0;
    var tmp_d  = 0;
    
    newAreas = "";
    newTotal = 0;
    
    var FullCir = "Full";
    var QtrCir = "1/4";
    var HalfCir = "1/2";
    var TQtrCir = "3/4";
    var typeCir = "";
    
    // Determine what type of area has been entered
    // Do this by checking if the area name starts with a space. If that
    // doesn't get it check for non-null dimension data
    tmpName = document.Inputs.aName1.value;
    if ((tmpName.indexOf(' ') != 0) && (tmpName != "")) { isSquare = 1; isSomething += 1; }
    tmpName = document.Inputs.aName2.value;
    if ((tmpName.indexOf(' ') != 0) && (tmpName != "")) { isRect   = 1; isSomething += 1; }
    tmpName = document.Inputs.aName3.value;
    if ((tmpName.indexOf(' ') != 0) && (tmpName != "")) { isCircle = 1; isSomething += 1; }
    
    if (isSomething == 0) {
      if (document.Inputs.dim_xy.value != "") { isSquare = 1; isSomething += 1; }
      if ((document.Inputs.dim_x.value != "") && (document.Inputs.dim_y.value != "")) { isRect = 1; isSomething += 1; }
      if (document.Inputs.dim_d.value != "" ) { isCircle = 1; isSomething += 1; }
    }
    
    if (isSomething > 0) {
      // Calculate the area based on the dimension(s) entered
      if (isSquare == 1) {
        tmp_xy = document.Inputs.dim_xy.value;
        atmps = Math.round(tmp_xy * tmp_xy);
        totalAreas += 1;
      }
    
      if (isRect == 1) {
        tmp_x = document.Inputs.dim_x.value;
        tmp_y = document.Inputs.dim_y.value;
        atmpr = Math.round(tmp_x * tmp_y);
        totalAreas += 1;
      }
    
      if (isCircle == 1) {
        if (document.Inputs.dim_d.value != "") {
          tmp_d = document.Inputs.dim_d.value;
          if ((tmp_d != null) && (tmp_d != NaN)) {
            atmpc = 3.14159 * ((tmp_d/2) * (tmp_d/2));
            if (document.Inputs.CirclePart.options[document.Inputs.CirclePart.selectedIndex].value == 2) {
              atmpc = atmpc / 2; // 1/2 circle
            }
            if (document.Inputs.CirclePart.options[document.Inputs.CirclePart.selectedIndex].value == 3) {
              atmpc = atmpc / 4; // 1/4 circle
            }
            if (document.Inputs.CirclePart.options[document.Inputs.CirclePart.selectedIndex].value == 4) {
              atmpc = (atmpc * 3)/4; // 3/4 circle
            }
            atmpc = Math.round(atmpc);
            totalAreas += 1;
          }
        }
      }
        
      // Build a new currentArea result for display
      if (prevAreas == "") { newAreas = "Dimensions are in " + measDimText + "\r\n"; }
      
      if (isSquare) {
        if (document.Inputs.dim_xy.value > 0) {
          newAreas = document.Inputs.aName1.value + " = " +
            atmps + " " + measAreaDimText + 
            " (Square " + document.Inputs.dim_xy.value + " x " + document.Inputs.dim_xy.value + ")\r\n";
          newTotal += atmps;
        }
        else {
          newAreas = document.Inputs.aName1.value + " = " + atmps + measAreaDimText +
            " (Missing or zero L/W dimension)\r\n";
        }
      }
    
      if (isRect) {
        if ((document.Inputs.dim_x.value > 0) && (document.Inputs.dim_y.value > 0)) {
          newAreas = newAreas + document.Inputs.aName2.value + " = " +
            atmpr + " " + measAreaDimText +
            " (Rectangle " + document.Inputs.dim_x.value + " x " + document.Inputs.dim_y.value + ")\r\n";
          newTotal += atmpr;
        }
        else {
          newAreas = newAreas + document.Inputs.aName2.value + " = " + atmpr + " " + measAreaDimText +
            " (Missing or zero L or W dimension)\r\n";
        }
      }
    
      if (isCircle) {
        if (document.Inputs.dim_d.value != "") {
          if (document.Inputs.CirclePart.options[document.Inputs.CirclePart.selectedIndex].value == 1) {
            typeCir = FullCir;
          }
          else if (document.Inputs.CirclePart.options[document.Inputs.CirclePart.selectedIndex].value == 2) {
            typeCir = HalfCir;
          }
          else if (document.Inputs.CirclePart.options[document.Inputs.CirclePart.selectedIndex].value == 3) {
            typeCir = QtrCir;
          }
          else if (document.Inputs.CirclePart.options[document.Inputs.CirclePart.selectedIndex].value == 4) {
            typeCir = TQtrCir;
          }
          newAreas = newAreas + document.Inputs.aName3.value + " = " +
            atmpc + " " + measAreaDimText + 
            " (" + typeCir + " Circle w/diameter " + document.Inputs.dim_d.value + ")\r\n";
          newTotal += atmpc;
        }
        else {
          newAreas = newAreas + document.Inputs.aName3.value + " = 0 " + measAreaDimText +
            "(Missing diameter measurement)\r\n";
        }
      }
      prevAreas = prevAreas + newAreas;
      prevTotal = prevTotal + newTotal;

      // Output the new answers
      document.Answers.Result.value      = prevAreas;
      document.Answers.ResultTotal.value = prevTotal + " " + measAreaDimText;
    
      // Reset the area name fields and dimension fields
      document.Inputs.aName1.value = " Area " + (totalAreas + 1);
      document.Inputs.dim_xy.value = "";
      document.Inputs.aName2.value = " Area " + (totalAreas + 1);
      document.Inputs.dim_x.value = "";
      document.Inputs.dim_y.value = "";
      document.Inputs.aName3.value = " Area " + (totalAreas + 1);
      document.Inputs.dim_d.value = "";
    }
    else {
      newAreas = "No valid areas have been entered, please try again."
      document.Answers.Result.value = prevAreas + newAreas;
    }
  }
  
  function clearResults () {
  // This function just resets all the form fields back to the starting point
  // Plus it resets the "previous" variables
  
  prevAreas = "";
  prevTotal = 0;
  totalAreas = 0;
    
  measDim = 0;
  measDimText = "FT";
  measAreaDimText = "SQ FT";

  document.Inputs.aName1.value = " Area 1";
  document.Inputs.dim_xy.value = "";
  document.Inputs.aName2.value = " Area 1";
  document.Inputs.dim_x.value = "";
  document.Inputs.dim_y.value = "";
  document.Inputs.aName3.value = " Area 1";
  document.Inputs.dim_d.value = "";
  document.Inputs.CirclePart.value = "Full";
  
  document.Answers.Result.value = "";
  document.Answers.ResultTotal.value = "";
  
  // Don't reset the dimensions radiobuttons 
  }
  
  function setDim (d) { 
    if (measDim != d) {
      if (prevAreas != "") {
        // Means areas have already been entered so changing dimensions should be documented
        if (d == 0) { // Converting to feet from meters
          prevTotal = convToSQFeet( prevTotal );
          prevAreas = prevAreas + "Switched dimensions from Meters to Feet\r\n";
        }
        else { // Converting to meters from feet
          prevTotal = convToSQMeters( prevTotal );
          prevAreas = prevAreas + "Swicthed dimensions from Feet to Meters\r\n";
        }
      }
    }
    if (d == 0) {
      measDimText = "FT";
      measAreaDimText = "SQ FT";
    }
    if (d == 1) {
      measDimText = "Meters";
      measAreaDimText = "SQ Meters"
    }
    measDim = d;
    document.Answers.ResultTotal.value = prevTotal + " " + measAreaDimText;
  }
  
  function convToSQMeters (val) {
    return Math.round(val * 0.0929);
  }
  
  function convToSQFeet (val) {
    return Math.round(val * 10.7639);
  }
    
  function sendRFPQ () { // Send the ResultTotal amount to the contact-pda page
    if (totalAreas > 0) { // If areas have been entered, then allow request of quotation
      if (measDim == 1) { // Need to convert to square feet for a request for quote
        setDim (0);
      }
      self.location="http://www.pattenseed.com/contact-pda.html?YACTotal=" + prevTotal + "&";
    }
    else { // If no areas have been entered, display a message and do nothing
      document.Answers.Result.value = "To request a price quote, enter your Yard Area data first, then push the Request a Price Quote button";
    }
  }