// quick order form validation
function validateQuickOrder(form) {        
  if ((form.ProductNumber.value == "")|| (form.ProductNumber.value == "Enter Product #")){
        alert("Please enter an item number.");
        form.ProductNumber.focus();
        return false;
 }
        return true;
}
// Update purchase order on click value 
function onclickPurchaseOrder(form) {        
  if (form.PurchaseOrder.checked){
        alert(form.PurchaseOrder.value);
       form.PurchaseOrder.value = "yes";}
  else{
        form.PurchaseOrder.value = "no";       
      }
 }

// used on one-page check out to populate the shipping address from the billing address
function UseBillingAsShipping(form)
{

  if (form.ship_to_bill_address.checked)
  {
    form.FName_SHIP.value = form.FirstName.value;
    form.LName_Ship.value = form.LastName.value;
    form.Company_Ship.value = form.Company.value;
    form.Address1_Ship.value = form.Address1.value;
    form.Address2_Ship.value = form.Address2.value;
    form.City_Ship.value = form.City.value;
    form.State_SHIP.value = form.State.value;
    form.COUNTRY_SHIP.value = form.COUNTRY.value;
    form.PostalCode_SHIP.value = form.PostalCode.value;
    form.Phone_Ship.value = form.Phone.value;

    populateCartTotal();
//alert('Test Click');
  }
  else
  {
    form.Company_Ship.value = '';
    form.Address1_Ship.value = '';
    form.Address2_Ship.value = '';
    form.City_Ship.value = '';
    form.State_SHIP.value = '';
    form.PostalCode_SHIP.value = '';
    form.FName_SHIP.value = '';
    form.LName_Ship.value = '';
    form.Phone_Ship.value = '';

    populateCartTotal();
  }
}

// used on one-page check out to update order total (for example after state, country, or shipping method change)
function populateCartTotal()
{
  var state_ship = document.getElementsByName("State_SHIP")[0].value;
  var country_ship = document.getElementsByName("COUNTRY_SHIP")[0].value;
  var zip_ship = document.getElementsByName("PostalCode_SHIP")[0].value;
   
  //alert('pop cart total');

  if (state_ship != " - Select - " && country_ship != " - Select - " && document.getElementById("shipping_method") != null)
  {
    //shipping_method dropdown wont exist if page portion is being refreshed by AJAX call
    var ship_method = document.getElementsByName("shipping_method")[0].value;

    DmiAjaxFetch('CheckoutCartTotalContent', 'DmiAjaxSecure.aspx', 'request=CheckoutCartTotalContent&state_ship=' + state_ship + '&country_ship=' + country_ship + '&zip_ship=' + zip_ship + '&shipping_method=' + ship_method + '&extra=', rnd());
  }
}

//global variable
var giftCertificateCount = 0;

// used on one-page check out to apply the promo code the customer provides
function applyKeyCodeRefresh()
{
  var keycode = document.getElementsByName("keycode")[0].value;
 // var giftcert1 = document.getElementsByName("giftcert1")[0].value;
  var giftcert1 = '';

 // alert("giftcert1:"+giftcert1);
  DmiAjaxFetch('CheckOutCartItemsContent', 'DmiAjaxSecure.aspx', 'request=CheckOutCartItemsContent&keycode=' + keycode + '&giftcert1=' + giftcert1 + '&extra=', rnd());

}

// used on one-page check out to apply the promo code the customer provides
function applyKeyCode()
{
  var keycode = document.getElementsByName("keycode")[0].value;
//  var giftcert1 = document.getElementsByName("giftcert1")[0].value;
  var giftcert1 = '';

  //alert('DEBUG: gift cert:'+ giftcert1);

  DmiAjaxFetch('CheckOutCartItemsContent', 'DmiAjaxSecure.aspx', 'request=CheckOutCartItemsContent&keycode=' + keycode + '&giftcert1=' + giftcert1 + '&extra=', rnd());
   
  if(giftCertificateCount <= 0 && keycode == '') {
   // alert('DEBUG: ct = '+ giftCertificateCount );
    setTimeout("applyKeyCodeRefresh()", 1000);  //repaint with new cart object
  }

  if(keycode == '')
    giftCertificateCount = giftCertificateCount + 1;

  return false;
}

// used to generate random string that will be appended to AJAX requests to avoid getting cached response
function rnd() { 
  return String((new Date()).getTime()).replace(/\D/gi,'') 
}

// used on one-page check out to update billing state or country and order total
function updateBillingInfo()
{
  var state_bill = document.getElementsByName("State")[0].value;
  var country_bill = document.getElementsByName("COUNTRY")[0].value;
  var zip_bill = document.getElementsByName("PostalCode")[0].value;
   
  if (state_bill != " - Select - " && country_bill != " - Select - ")
  {
    DmiAjaxFetch('CheckoutCartTotalContent', 'DmiAjaxSecure.aspx', 'request=CheckoutCartTotalContent&state_bill=' + state_bill + '&country_bill=' + country_bill + '&zip_bill=' + zip_bill + '&extra=', rnd());
  }

}
