/***************************************
****** This file is dependent on  ******
****** OrderPad.ex, products.ex   ******
****** and ShoppingCart.ex        ******
***************************************/

// Global Variables for adding something
var fastAdd = new Object();
fastAdd.sku = null;
fastAdd.qty = null;
fastAdd.qtyPkg = null;
fastAdd.description = null;
fastAdd.price = null;
fastAdd.prodId = null;
fastAdd.partId = null;

addLoadEvent(function() {
  var fop = getParameter("fop");
  
  if (fop) {
    fastenal.toggle('fastOrderPadContainer',document.getElementById('fastToggle')); setInitialFocus();
  }
});


function checkArray() {
    for (var i in fastAdd) {
        if (fastAdd[i] == null) {
            return true;
        }
    }

    return false;
}

function resetArray() {
    for (var i in fastAdd) {
        fastAdd[i] = null;
    }

    document.getElementById("fastSku").value = '';
    document.getElementById("fastQty").value = '';

    document.getElementById("fastDescription").value = '';
    document.getElementById("fastQtyPkg").value = '';
    document.getElementById("fastPrice").value = '';
}

function partFound(part, qtypkg, price, prodid, unk2) {
    fastAdd['description'] = part;
    fastAdd['qtyPkg'] = qtypkg;
    fastAdd['price'] = price;
    fastAdd['prodId'] = prodid;
    fastAdd['partId'] = 1 + "|" + fastAdd.prodId + "|" + fastAdd.sku; // 1 used to be 'fastAdd.qtyPkg'

    document.getElementById("fastDescription").value = fastAdd['description'];
    document.getElementById("fastQtyPkg").value = fastAdd['qtyPkg'];
    
    if (fastAdd['price'].indexOf(".") + 2 >= fastAdd['price'].length) {
        document.getElementById("fastPrice").value = parseFloat(fastAdd['price']).toFixed(2);
    } else {
        document.getElementById("fastPrice").value = fastAdd['price'];
    }
}

function partNotFound() {
    togglePopup('divFastOrderPadPopup',false);
}

function promoFound() {

}

function promoNotFound() {
    
}

function setInitialFocus() {
  if (document.getElementById && document.getElementById("fastSku") != null) {
    if (isVisible('fastSku')) {
      document.getElementById("fastSku").focus();
    }
  }
}


function togglePopup(obj,cancel) {
  obj = document.getElementById(obj);
    
  var fastSku = document.getElementById("fastSku");
  var fastQty = document.getElementById("fastQty");
  var submit = document.getElementById("fastSubmit");
    
  if (obj.style.display != 'none') {
    obj.style.display = 'none';
    
    if (cancel) {
      fastQty.setAttribute("disabled","disabled");
      submit.setAttribute("disabled","disabled");
      fastSku.value = "";
      fastSku.focus();
    }
  } else { 
    obj.style.display = '';
    document.getElementById('popupFastenal').focus();
  }
}

function formToggle(e, element) {
  e = e || window.keyup;
  var code = e.keyCode || e.which;
  
  if ((code == 1) || (e.type == 'click')) code = 13; // Checks to see if user clicks, then changes code to 13

  var fastSku = document.getElementById("fastSku");
  var fastQty = document.getElementById("fastQty");
  var fastDesc = document.getElementById("fastDescription");
  
  if ( code == 13 ) {
    if (element.id == 'fastSku') {
      if (fastSku.value != '') {
        fastAdd.sku = fastSku.value;
        ajaxGetProdDescription();
      } else {
        alert("You must enter a SKU");
        fastSku.focus();
      }
    } else {
      fastAdd.qty = fastQty.value;
      fastSku.focus();
      
      if (trim(fastAdd.qty) == '' || (fastAdd.qty) < 1) {
        fastQty.value = '';
        fastAdd.qty = null;
        alert("You must enter a quantity");
        fastQty.focus();
      } 
    }
    
    if (fastAdd.sku != null && fastAdd.qty != null) {
      if (fastAdd.partId == null) {
        alert("Wait for the description to load, then add the item to the cart.");
      } else {
        ajaxAddProdToCart();
      }
    }
    
    return false;
  }
  
  return true;
}

function addFastenalPart() {
  fastAdd.qtyPkg = "1";
  fastAdd.description = "Custom Fastenal Part Number";
  fastAdd.price = "0";
  fastAdd.partId = "";
  
  var fastQty = document.getElementById("fastQty");
  var submit = document.getElementById("fastSubmit");
  
  fastQty.removeAttribute("disabled");
  submit.removeAttribute("disabled");
  fastQty.focus();
  
  document.getElementById("fastDescription").value = fastAdd.description;
  document.getElementById("fastQtyPkg").value = "NA";
  document.getElementById("fastPrice").value = "NA";
}

function addCustomerPart() {
  fastAdd.qtyPkg = "1";
  fastAdd.description = "Customer Part Number";
  fastAdd.price = "0";
  fastAdd.partId = "";
  
  var fastQty = document.getElementById("fastQty");
  var submit = document.getElementById("fastSubmit");
  
  fastQty.removeAttribute("disabled");
  submit.removeAttribute("disabled");
  fastQty.focus();
  
  document.getElementById("fastDescription").value = fastAdd.description;
  document.getElementById("fastQtyPkg").value = "NA";
  document.getElementById("fastPrice").value = "NA";
}

function cancelPart() {
  resetArray();
  setInitialFocus();
}

function ajaxGetProdDescription() {
    var sExec = "OrderPad.ex";
    var sParam = "action=lookup";
    
    var path = setPath( sExec, sParam );
    var params = "add=NO&Sku=" + fastAdd.sku;
    
    var fastQty = document.getElementById("fastQty");
    var submit = document.getElementById("fastSubmit");
    
    dojo.xhrGet( {
        url: path + "&" + params,
        handleAs: "text",
        timeout: 60000,
        load: function(response, ioArgs) {
            eval(response);
            fastQty.removeAttribute("disabled");
            submit.removeAttribute("disabled");
            fastQty.focus();
            return response;
        },
        error: function(response, ioArgs) {
            dojo.byId("fastResponse").innerHTML = "Sorry, there has been an error.";
            console.error("HTTP status code", ioArgs.xhr.status);
            return response;
        }
    } );
}

function ajaxAddProdToCart() {
    if (fastAdd.description == "Custom Fastenal Part Number") {
        var sExec = "OrderPad.ex";
        var sParam = "action=add";
        var path = setPath( sExec, sParam );
        var params = "add=YES&Sku=" + fastAdd.sku + "&Quantity=" + fastAdd.qty + "&CustomType=Fastenal";
    } else if (fastAdd.description == "Customer Part Number") {
        var sExec = "OrderPad.ex";
        var sParam = "action=add";
        var path = setPath( sExec, sParam );
        var params = "add=YES&Sku=" + fastAdd.sku + "&Quantity=" + fastAdd.qty + "&CustomType=Customer";
    } else {
        var sExec = "products.ex";
        var sParam = "dispatch=addToCartSingleSku";
        var path = setPath( sExec, sParam );
        var params = "productDetailId=" + fastAdd.partId + "&qty=" + fastAdd.qty + "&qtyperpkg=" + fastAdd.qtyPkg;
    }
    
    var fastQty = document.getElementById("fastQty");
    var submit = document.getElementById("fastSubmit");
    
    dojo.xhrGet( {
      url: path + "&" + params,
      handleAs: "text",
      timeout: 60000,
      load: function(response, ioArgs) {
        resetArray();
        ajaxUpdateCart();
        fastQty.setAttribute("disabled","disabled");
        submit.setAttribute("disabled","disabled");
        return response;
      },
      error: function(response, ioArgs) {
        dojo.byId("fastResult").innerHTML = "<font style='color:red;'>An error occurred updating your shopping cart.  Try again.</font>";
        console.error("HTTP status code", ioArgs.xhr.status);
        return response;  
      }
    } );
}

function ajaxUpdateCart() {
    var sExec = "ShoppingCart.ex";
    var sParam = "printable=true";
    var path = setPath( sExec, sParam );

    dojo.xhrGet( {
      url: path,
      handleAs: "text",
      timeout: 60000,
      load: function(response, ioArgs) {
              dojo.byId("shoppingCartContainer").innerHTML = response;
              updateCartPopup(SITE_ROOT + 'ShoppingCart.ex?type=display');
              return response;
            },
      error: function(response, ioArgs) {
        alert(response);
               dojo.byId("fastResponse").innerHTML = "<font style='color:red;'>An error occurred updating your shopping cart.  Try again.</font>";
               console.error("HTTP status code", ioArgs.xhr.status);
               return response;  
             }
    } );
}