// Copyright 2008 by Frank Koenen, Finkle Enterprises, LLC

window.quickquote9_docalcs =
{
  'singleitemcost': 0.00,
  'shippingcost': 0.00,
  'material': 'PVC/Sintra', // human readable material description
  'size': '',
  'sides': '',
  'orderitemclasscode': "900", // set this to a number to differentiate from other quoters.

  'calc': function()
  {
     var d = document.getElementById('quickquote9_pricetotal');
     var d2 = document.getElementById('quickquote9_priceeach');
     var f = document.forms.quickquote9;
     if ( ! d || ! f ) return;

     var cost = 0.00;

     // Size
     this.size = '';
     {
       var s = document.getElementById('quickquote9_sizes');
       if ( s.selectedIndex > 0 )
       {
         this.size = s[s.selectedIndex].value;
       }
     }

     {
       var mcpsi = window.quickquote9_configs.materialcostfactor;
       if ( mcpsi && this.size != '' ) {
         var width = 1 * this.size.replace(/^a/,'').replace(/x.*$/,'');
         var height = 1 * this.size.replace(/^a[^x]*x/,'');
         cost = cost + (width * height * mcpsi);
       }
     }

     // Sides
     {
       var s = document.getElementById('quickquote9_sides');
       if ( s.selectedIndex > 0 )
       {
         this.sides = s[s.selectedIndex].value;
         cost = cost * window.quickquote9_configs.sidescosts[this.sides];
       }
     }

     // Corners
     {
       var s = document.getElementById('quickquote9_corners');
       if ( s.selectedIndex > 0 )
       {
         this.cornering = s[s.selectedIndex].value;
         cost = cost + window.quickquote9_configs.coneringcosts[this.cornering];
       }
     }

     // Holes
     {
       var s = document.getElementById('quickquote9_holes');
       if ( s.selectedIndex > 0 )
       {
         this.holes = s[s.selectedIndex].value;
         cost = cost + window.quickquote9_configs.holescosts[this.holes];
       }
     }

     this.singleitemcost = cost;

     // Quantity
     {
       var quantity = document.getElementById('quickquote9_quantity').value;
       if ( quantity == "" ) quantity = 1;
       else quantity = quantity * 1;

       cost = cost * quantity;
       
     }

     // Upload files
     {
       if ( document.getElementById('quickquote9_uploadA').value != '' )
        cost = cost + window.quickquote9_configs.fileUploadCost;
       if ( document.getElementById('quickquote9_uploadB').value != '' )
        cost = cost + window.quickquote9_configs.fileUploadCost;
       if ( document.getElementById('quickquote9_uploadA').value != '' || document.getElementById('quickquote9_uploadB').value != '' )
        this.singleitemcost += ( window.quickquote9_configs.fileUploadCost / quantity );
     }

     // Turn Around
     {
       var tas = document.getElementById('quickquote9_turnaround');
       var sc = tas[tas.selectedIndex].value;
       sc = sc.replace(/^.*_/,'');
       if ( sc == '' ) sc = 1.99;
       this.shippingcost = sc * 1;
       cost = cost + this.shippingcost;

       // reset shipping cost to be per/qty. for nopcart to use properly.
       this.shippingcost = this.shippingcost / quantity;

     }

     this.singleitemcost = this.singleitemcost * 1;

     d.innerHTML = cost.toFixed(2);
     d2.innerHTML = this.singleitemcost.toFixed(2);
  },

  'submit': function()
  {

     if ( window.quickquote9_docalcs.sides == "" )
     {
        alert('Please select the sides.');
        return false;
     }

     if ( window.quickquote9_docalcs.size == "" )
     {
        alert('Please select a size');
        return false;
     }

     var d = document.getElementById('quickquote9_pricetotal');
     var f = document.forms.quickquote9;
     var nopform = document.forms.quickquote9_nopcartform;
     if ( ! d || ! f || ! nopform ) return;

     var totalcost = d.innerHTML;

     if ( window.confirm("The total cost for this item is $"+totalcost+"\nContinue?") ) {

      var r = new RandomStr();
      r.number = true;
      r.lower  = false;
      r.upper  = true;
      r.other  = false;
      rr = r.randomize(8);

      var ss = document.getElementById('quickquote9_sizes');

      // load up the nop cart form...
      nopform.QUANTITY.value = document.getElementById('quickquote9_quantity').value;
      nopform.PRICE.value = window.quickquote9_docalcs.singleitemcost;
      nopform.NAME.value = "A "+ document.getElementById('quickquote9_sides').value +" sided, "+ ss[ss.selectedIndex].text +" aluminum sign.";
      nopform.ID_NUM.value = this.orderitemclasscode+"."+rr // used to identify the type of product. Nopcart uses this for uniq lines.
      nopform.SHIPPING.value = window.quickquote9_docalcs.shippingcost;

      AddToCart(nopform); // actually add the item to the cart.

      f.ID_NUM.value = this.orderitemclasscode+"."+rr  // the ID for the item, used in conjunction with staging upload files.
      f.submit(); // submit the main form to perform the upload of the file, etc.

     }
  }
};

