function quantityUp() {
  $('quantity').value = parseInt($('quantity').value) + 1;
}

function quantityDown() {
  if (parseInt($('quantity').value) > 1) {
    $('quantity').value = parseInt($('quantity').value) - 1;
  }
}

function checkSwitch() {
  if($('ccType').value == 'switch') {
    Effect.Appear('ccSwitch');
  } else {
    Effect.Fade('ccSwitch');
  }
}

function checkAgree() {
  if($('agree').checked == true) {
    $('submit').disabled = false;
  } else {
    $('submit').disabled = true;
  }
}

// function $(element) {
//   if (arguments.length > 1) {
//     for (var i = 0, elements = [], length = arguments.length; i < length; i++) {
//       elements.push($(arguments[i]));
//     }
//     return elements;
//   }
//   if (typeof(element) == 'string') {
//     element = document.getElementById(element);
//   }
//   return element;
// }

function quantityForm(value, formId) {
  $('itemQuantity' + formId).value = value;
  $('formQuantity' + formId).submit();
}


window.onload = function() {
  qs = $('quickSearch');
  qs.onblur = function() {
    if (this.value == '') {
      this.value = 'Search...';
    }
  }
  qs.onfocus = function() {
    if (this.value == 'Search...') {
      this.value = '';
    }
  }
}

function runQuickSearch() {
  var s = $('quickSearch').value;
  $('suggest').empty();
  
  if (s.length >= 3) {
    // we need 3 characters to mysql search
    new Request.HTML({
      url: '/suggest/?q=' + escape(s),
      method: 'get',
      onRequest: function() {
        new Element('p').addClass('loading').appendText('Searching, please wait...').inject($('suggest'));
        showQuickSearch();
      },
      update: $('suggest')
    }).send();

  } else {
    new Element('p').appendText('Please enter 3 or more characters to search').inject($('suggest'));
    showQuickSearch();
  }
  
  return true;
}

function showQuickSearch() {
  $('suggest').setStyle('display', 'block');
  
  return false;
}

function hideQuickSearch() {
  window.setTimeout(function() {
    $('suggest').setStyle('display', 'none');
  }, 500);
  
  return false;
}

window.addEvent('domready', function() {
  $('quickSearch').addEvent('change', function() {
    runQuickSearch();
  });

  $('quickSearch').addEvent('keyup', function() {
    runQuickSearch();
  });

  $('quickSearch').addEvent('focus', function() {
    this.value = (this.value == 'Search') ? '' : this.value;
    showQuickSearch();

    return true;
  });

  $('quickSearch').addEvent('blur', function() {
    this.value = (this.value == '') ? 'Search' : this.value;
    hideQuickSearch();

    return true;
  });
});

// ----------------------
// form copying functions
// ----------------------
//
function copyDetails(copy) {
  // if we are copying the details
  if (copy.checked) {
    // loop through all the fields
    for (var i = fields.length - 1; i >= 0; i--) {
      // we need different logic for select boxes
      if ($('delivery_' + fields[i]).tagName.toLowerCase() == 'select') {
        // match up the selectedIndex
        $('delivery_' + fields[i]).selectedIndex = $(fields[i]).selectedIndex;
        
        // add an onchange function to keep the value the same (fake readonly)
        $('delivery_' + fields[i]).onchange = function() {
          this.selectedIndex = $(this.id.replace(/^delivery_/, '')).selectedIndex;
        }
        
      // otherwise if it's a text box or textarea
      } else if (($('delivery_' + fields[i]).tagName.toLowerCase() == 'input' && $('delivery_' + fields[i]).type == 'text') || $('delivery_' + fields[i]).tagName.toLowerCase() == 'textarea') {
        // make the postcode uppercase
        if (fields[i].match(/postcode/)) {
          if ($(fields[i]).value != $(fields[i]).value.toUpperCase()) {
            $(fields[i]).value = $(fields[i]).value.toUpperCase();
          }
        }
        
        // just set the value
        $('delivery_' + fields[i]).value = $(fields[i]).value;
      
        // set the field to readonly
        $('delivery_' + fields[i]).readOnly = true;
        
      // if it's a checkbox
      } else if ($('delivery_' + fields[i]).tagName.toLowerCase() == 'input' && $('delivery_' + fields[i]).type == 'checkbox') {
        // copy the checked status and the value too
        $('delivery_' + fields[i]).checked = $(fields[i]).checked;
        $('delivery_' + fields[i]).value = $(fields[i]).value;
      
        // set the field to readonly
        $('delivery_' + fields[i]).readOnly = true;
      }
      
      // we want an onfocus event for each field that highlights the checkbox
      $('delivery_' + fields[i]).onfocus = function() {
        highlightField($$('p.deliverySame')[0]);
      }
    }

  // if we're not copying the details, set all the fields to editable
  } else {
    // loop through all the fields
    for (var i = fields.length - 1; i >= 0; i--) {
      // if it's a select
      if ($('delivery_' + fields[i]).tagName.toLowerCase() == 'select') {
        // return the onchange
        $('delivery_' + fields[i]).onchange = function() {
          return true;
        }
      
      // otherwise
      } else {
        // make the postcode uppercase
        if (fields[i].match(/postcode/)) {
          if ($(fields[i]).value != $(fields[i]).value.toUpperCase()) {
            $(fields[i]).value = $(fields[i]).value.toUpperCase();
          }
        }

        // set the field to readonly
        $('delivery_' + fields[i]).readOnly = false;
        
      }

      // we don't want the onfocus event anymore
      $('delivery_' + fields[i]).onfocus = function() {
        return true;
      }
    }
  }

  return true;
}

var fields = [];

function addCheckoutEvents(array) {
  if (array) {
    fields = array;

  } else {
    fields = [
      'name',
      'address',
      'town',
      'county',
      'country',
      'postcode'
    ];
  }
  
  for (var i = fields.length - 1; i >= 0; i--) {
    $(fields[i]).onchange = function() {
      copyDetails($('delivery_same'));
    }

    $(fields[i]).onkeyup = function() {
      copyDetails($('delivery_same'));
    }
  }

  $('checkoutForm').onsubmit = function() {
    copyDetails($('delivery_same'));
  }

  window.onload = function() {
    copyDetails($('delivery_same'));
  }
}

var highlightTimeout = null;

function highlightField(el, focus) {
  if (focus) {
    new Fx.Scroll(el);
    el.focus();
  }
  
  window.clearTimeout(highlightTimeout);

  new Fx.Tween(el).start('background-color', '#ffc');
  highlightTimeout = window.setTimeout(function() {
    new Fx.Tween(el).start('background-color', '#fff');
  }, 2500);
  
  return false;
}
