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

window.quickquote20_docalcs =
{
  'singleitemcost': 0.00,
  'shippingcost': 0.00,
  'checkstyle': '', // human readable style description
  'size': '',
  'orderitemclasscode': "610", // set this to a number to differentiate from other quoters.

  'calc': function()
  {
     var d = document.getElementById('quickquote20_pricetotal');
     var d2 = document.getElementById('quickquote20_priceeach');
     var f = document.forms.quickquote20;
     if ( ! d || ! f ) return;

     var cost = 0.00;

     // Size
     this.size = '';
     var width = 0;
     var height = 0;
     {
       var s = document.getElementById('quickquote20_sizes');
       if ( s.selectedIndex > 0 )
       {
         this.size = s[s.selectedIndex].value;
         width = 1 * this.size.replace(/^a/,'').replace(/x.*$/,'');
         height = 1 * this.size.replace(/^a[^x]*x/,'');
       }
     }

     // Check Style
     this.checkstyle = '';
     {
       var s = document.getElementById('quickquote20_checkstyle');
       var checkstyle = s[s.selectedIndex].value;
       var mcpsi = window.quickquote20_configs.checkstylecosts[checkstyle];
       if ( mcpsi ) {
         cost = cost + (width * height * mcpsi);
         this.checkstyle = s[s.selectedIndex].text;
       }
     }

     this.singleitemcost = cost;

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

       cost = cost * quantity;
       
     }

     // Upload files
     {
       if ( document.getElementById('quickquote20_uploadA').value != '' )
       {
        cost = cost + window.quickquote20_configs.fileUploadCost;
        this.singleitemcost += ( window.quickquote20_configs.fileUploadCost / quantity );
       }
     }

     // Turn Around
     {
       var tas = document.getElementById('quickquote20_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.quickquote20_docalcs.checkstyle == "" )
     {
        alert('Please select a Check Style');
        return false;
     }

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

     var d = document.getElementById('quickquote20_pricetotal');
     var f = document.forms.quickquote20;
     var nopform = document.forms.quickquote20_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('quickquote20_sizes');

      // load up the nop cart form...
      nopform.QUANTITY.value = document.getElementById('quickquote20_quantity').value;
      nopform.PRICE.value = window.quickquote20_docalcs.singleitemcost;
      nopform.NAME.value = "A "+ ss[ss.selectedIndex].text + " Large Check, in " + window.quickquote20_docalcs.checkstyle ;
      nopform.ID_NUM.value = this.orderitemclasscode+"."+rr // used to identify the type of product. Nopcart uses this for uniq lines.
      nopform.SHIPPING.value = window.quickquote20_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.

     }
  }
};

