//
//  Copyright Manta Technologies Inc., 2010.  All rights reserved.  
//
//


//
// Google checkout exit
//

var alreadyDidCleanUp = false;

function googlecartOnCheckoutClick(items,form) {
   // Called when customer clicks the Checkout button.
   // Computes shipping and taxes based on what was ordered.
   if (alreadyDidCleanUp) {
	   return true;
   }
   updateShippingPrices();
   fixTaxes();
   alreadyDidCleanUp = true;
   googlecart.checkout();         
}

function fixTaxes() {
   googlecart.saveCartAndRefreshWidget();
   var items = googlecart.getItems(); 
   var productName;
   for (var i=0; i < items.length; i++) {
      productName  = "" + items[i].getTitle();
      productName = productName.toUpperCase();
      if ((!contains(productName,"H)")) && (!contains(productName,"SRGSET"))) {
         addTaxExempt(i);
         //alert("Added tax exempt table.")
      }
   }
}

function updateShippingPrices() {  
   var standardShipNode = document.getElementById("stdShip");
   //alert("standard was " +  standardShipNode.value);
   standardShipNode.value = standardShipping() + ".00";
   //alert("standard changed to " +  standardShipNode.value);
   var overnightShipNode = document.getElementById("ovrShip");
   //alert("overnight was " +  overnightShipNode.value);
   overnightShipNode.value = overnightPrice() + ".00";
   //alert("overnight changed to " +  overnightShipNode.value);
}

function standardShipping() {
   // This function computed the shipping rate from the cart.
   var items = googlecart.getItems();
   var shippingFirst = 0;
   var shippingRest = 0;
   var itemFirst;
   var itemUnit;
   var itemQuan;
   
   for (var i=0; i < items.length; i++) {
      itemFirst = items[i].getShippingFirst();
      if (isNull(itemFirst)) itemFirst = 0;
      itemUnit  = items[i].getShippingPerUnit();      
      if (isNull(itemUnit)) itemUnit = 0;
      itemQuan = items[i].getQuantity();
     if (isNull(itemQuan)) itemQuan = 0;
     
      //alert("next " + itemFirst + "  " + itemUnit)     
      if (shippingFirst < itemFirst) {
         shippingFirst = itemFirst;
         if (items[i].getQuantity() > 1) {
         shippingRest = shippingRest + ( itemUnit * (itemQuan-1));
         }
      } else {
         shippingRest = shippingRest + (itemUnit * itemQuan);
      }
   }
   return shippingFirst + shippingRest;
}

function overnightPrice() {
    var s = standardShipping();
    if (s > 0) {
       return 25 + ((s-10)*2);
    } else {
       return 0;
    }
}
                    
function contains(x,y)  {
    // Returns true if string x contains string y
    var index1 = x.indexOf(y);
    if (index1 > -1) {
       return true;
    } else {
       return false;
    }
}

function isNull(x) {
//This utility function returns true if string x is null, undefined, or empty.
   if ((x == 'undefined') || (x == "") || (x == null) || (x == "null")) {
       return true;
   } else {
       return false;   
   }
}

 function dollarize(n) {
 //This function converts an integer to the form $xx,xxx.00.
    var str = "" + n;
    var len = str.length;
    if (len > 3) {
       var commapsn = len - 3;
       return "$" + str.substring(0,commapsn) + "," + str.substring(commapsn) + ".00";
    } else {
       return "$" + str + ".00";
    }
 }

function addTaxExempt(n) {
   //  This adds a line to the previous tax table.
   //  The "n" causes the new row to apply to the specified line of the shopping cart.
   n ++;
   var ctlForm = document.getElementById("googlecart-checkout-config");
    if (document.uniqueID) {
        var input = document.createElement('<input type=hidden name=shopping-cart.items.item-' + n + '.tax-table-selector value=tax-exempt/>');
    }
    else {   
      var input = document.createElement("input");
      input.setAttribute("type","hidden");
      input.setAttribute("name","shopping-cart.items.item-" + n + ".tax-table-selector");
      input.setAttribute("value","tax_exempt");
    }
    ctlForm.appendChild(input);
}

function glossary()  {
	var GlsWindow = window.open("glossary.htm","_blank","height=500px,width=620px");
}

//This stuff is for Google analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19849536-1']);
_gaq.push(['_trackPageview']);
(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

