/*
 * Sub-Navigation links
 */
function attachSubnavAnchors() {
    $('sg-subnav').select('a').each(function(obj) {
        obj.observe('click', function(event) {
            // Stop the event from propgating
            event.stop();

            // Retrieve the ID of the anchor
            var anchor = obj.href.substring(obj.href.indexOf('#') + 1);

            // Scroll the anchor into view
            Effect.ScrollTo(anchor, { offset: -20 });
        });
    });
}
document.observe('dom:loaded', attachSubnavAnchors);


/*
 * Order Form launcher
 */
var orderForm = 0;
function openOrderForm() {
    if (orderForm && !orderForm.closed) {
        // Focus the existing order form window
        orderForm.focus();
    } else {
        // Display a new order form window
        orderForm = open('https://www.snowgoose.com.au/online_ordering/order.php', 'orderForm',
          'toolbar=no,location=no,directories=no,status=no,menubar=no,' +
          'scrollbars=1,resizable=yes,copyhistory=no,' +
          'width=700,height=500');
        orderForm.focus();
    }
}




/*
 * Mailing List subscription
 */

// Check to ensure a field's value is not an empty string
function validateNotEmpty(value) {
  if (value.length == 0) return false;
  return true;
}

function validateListForm(f) {
  //
  // Verify the following fields have been filled in
  //
  //    - Name
  //    - Street Address
  //    - Suburb
  //    - Postcode
  //    - Phone
  //

  var validateErrors = "";
  if (!validateNotEmpty(f.name.value))
    validateErrors += "- Name\n";
  if (!validateNotEmpty(f.address.value))
    validateErrors += "- Street Address\n";
  if (!validateNotEmpty(f.suburb.value))
    validateErrors += "- Suburb\n";
  if (!validateNotEmpty(f.postcode.value))
    validateErrors += "- Postcode\n";

  if (validateErrors != "") {
    alert("The following fields are required to subscribe to our mailing list, but were " +
      "left blank. Please fill them in before submitting your subscription request.\n\n" +
      validateErrors);
    return false;
  }

  return true;
}
