// Load multiple functions on window onload and onresize
// Source: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent (func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


// Populates label text into an input field (i.e. search box)
function populateSearchFieldText (field_id, label) {
    if (!document.getElementById) return false;
    
    // On startup copy label text to field
    var field = document.getElementById(field_id);
    field.value = label;
    
    // On focus blank out field if label text is value
    field.onfocus = function () {
        if (this.value == label) {
            this.value = '';
        }
    }
    
    // On blur copy label text if value is blank
    field.onblur = function () {
        if (this.value == '') {
            this.value = label;
        }
    }
    
}

// Switch to another subcategory from select list
function switchCategory() {
    if (!document.getElementById) return false;
    
    // get the select element
    var catselect = document.getElementById('select-prod');
    // have we got the element on this page
    if (! catselect ) {
        return false;
    }
    // Hide the button
    var catbutton = document.getElementById('change-submit');
    

    // IE Attribute work around
    var classAtt = document.createAttribute("class");
    classAtt.nodeValue = "hide";
    catbutton.setAttributeNode(classAtt);
    
    // Add onchange event to this select element
    catselect.onchange = function() {
        // If this changes, submit the form
        this.form.submit()    
    }

}

// Load functions at startup
addLoadEvent(function(){
    populateSearchFieldText('product-search-input', 'Part Number search');
})

addLoadEvent(function(){
    switchCategory();
})
