﻿// Harl;equin Solutions Support Javascript File for Chinacraft ASPDOTNET
// Storefront Sites
//
// Written by Keith Plunkett, July 2009

//************************************************************************
// Prototype a Trim method for the String Class
// Called "hstrim" because FireFox 3.5 actually introduces a Trim method
// which is at least 10 years too late!
//************************************************************************

String.prototype.hstrim = function() { return this.replace(/^\s+|\s+$/g, ''); }

//************************************************************************
// Function  : COWarningsCheck
// Purpose   : Checks whether any visible warning check boxes have been
//             checked in the checkout process.
// No Arguments
// Returns   : false if any one of the checked warning checkboxes is
//             present and not checked. Otherwise returns true
//************************************************************************
//
function COWarningsCheck()
{
  if (document.getElementById("chkKnifeWarning") != null)
  {
    if (!document.getElementById("chkKnifeWarning").checked)
    {
      alert("Please indicate that you are over 18 to purchase the items " +
        "which require you to be 18 or over");
      return false;
    }
  } 

  if (document.getElementById("chkSpecialWarning") != null)
  {
    if (!document.getElementById("chkSpecialWarning").checked)
    {
      alert("Please indicate that you accept the conditions of " +
        "purchasing a special order item");
      return false;
    }
  }

  if (document.getElementById("chkCaliforniaWarning") != null)
  {
    if (!document.getElementById("chkCaliforniaWarning").checked)
    {
      alert("Please indicate that you have read and understood the " +
        "California Prop65 warning by ticking the corresponding checkbox.");
      return false;
    }
  }

  return true;
}

//************************************************************************
// Function  : OrderHelpPopup
// Purpose   : Pops up the Order Help Page
// No Arguments
//************************************************************************
//
function OrderHelpPopUp()
{

  woString = 'height=400,width=500,' +
    'resizable=no,status=yes,toolbar=no,menubar=no,location=no';

  window.open('/hshelpwithordering.htm', '_blank', woString);
}

//************************************************************************
// Function  : ProductImagePopup
// Purpose   : Pops up the Product Image
// Arguments : piUrl : Product Image URL
//             piW   : Product Image Width
//             piH   : Product Image Height
//************************************************************************
//
function ProductImagePopup(piUrl, piW, piH)
{
  var wString;                          // Window Characteristcs String

  woString = 'height=' + piH + ',width=' + piW + ',' + 
    'resizable=no,status=yes,toolbar=no,menubar=no,location=no';

  window.open(piUrl, '_blank', woString);
}

//************************************************************************
// Function  : SCQtyValidate
// Purpose   : Validates the quantity to be added to the Shopping Cart
// Arguments : productName : Product Name
//             txtID       : ID of input field containing the quantity to 
//                           be validated
//             maxQty      : Max Quantity Permitted
// Returns   : True if it deems the quantity entered in the Input Box ID
//             to be valid, False if not.
// Details   : Displays an alert when False detailing the error
//************************************************************************
//
function SCQtyValidate(productName, txtID, maxQty)
{
  // Get the current entered quantity and trim it
  
  var qty = document.getElementById(txtID).value;
  qty = qty.hstrim();

  // Ensure that a quantity is entered

  if (qty.length == 0)
  {
    alert('Please enter a quantity of product "' + productName + 
      '" to be added to the Shopping Cart!');
    return false;
  }

  // Ensure that a valid quantity is entered
  
  if (!qty.match(/^\d+$/))
  {
    alert('Please enter a valid quantity of product "' + productName +
      '" to be added to the Shopping Cart!');
    return false;
  }

  // Ensure that the number is not ridiculous

  var iQty = parseInt(qty);
  if ((iQty < 1) || (iQty > maxQty))
  {
    alert('Product "' + productName + '" is a Retired Item - ' +
      'Maximum Available ' + maxQty);
    return false;
  }

  return true;
}


