
function checkSearch(anId) {
  ok = false;
  term = document.getElementById(anId);
  if (term != null && term.value.length > 0) {
    var stuff = new RegExp("[^a-zA-Z0-9_ .*~-]","g");
    term.value = term.value.replace(stuff,"");
    term.value = term.value + "*";
    ok = true;
  }
  return ok;
}

function getById(aString) {
  return document.getElementById(aString);
}

function getByNames(aString) {
  return document.getElementsByName(aString);
}

function checkEmail(anId) {
  var e = document.getElementById(anId);
  var ok = false;
  if (e != null) {
    if (e.value.indexOf(".") != -1 && e.value.indexOf("@") != -1) {
      ok =  true;
    }
  }
  return ok;
}

// home page image rotation
var imageList = new Array();
var currentIndex = 0;
var hpImage;
var timer;
function rotateNextImage() {
  var lastIndex = currentIndex;
  currentIndex = (currentIndex >= imageList.length -1) ? 0 : currentIndex+1;
  var e = $('homePageImage');  
  if (e != null) {
    var ep = e.getParent();
    ep.setStyle('background','transparent url('+imageList[currentIndex].src+') no-repeat scroll 0 10px');
    new Fx.Tween(e).start('opacity', '1', '0').chain(function() {
      ep.setStyle('background','none');
      e.src = imageList[currentIndex].src;
      e.setStyle('opacity','1');
      timer = setTimeout("rotateNextImage()",5000);
    });
  }
}

// image swap - uses imageList and currentIndex
function swapImage(anIndex) {
  var e = document.getElementById("mainImage");
  if (e != null && anIndex < imageList.length && anIndex >= 0) {
    e.src = e.src = imageList[anIndex].src;
    currentIndex = anIndex
  }
}

// zoom
function openZoom(anItem, aName) {
  window.open('/zoom.jsp?item='+anItem+'&name='+aName+'&selected='+currentIndex, 'Zoom', 
    'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=715,height=765')
}

  var limitDecimal = 2;
  var currency = "$";
  var itemList = new Array();

  function Item(aPrice, aSalePrice, priceTier1, priceTier2, priceTier3, priceTier4) {
    this.variances = new Array();
    this.price = aPrice;
    this.aSalePrice = aSalePrice;
    this.finalPrice = aPrice;
    if (aSalePrice != null && aSalePrice > 0) {
      this.finalPrice = aSalePrice;
    }
    if (priceTier1 == null) {
      priceTier1 = 0;
    }
    this.price1 = priceTier1;
    if (priceTier2 == null) {
      priceTier2 = 0;
    }
    this.price2 = priceTier2;
    if (priceTier3 == null) {
      priceTier3 = 0;
    }
    this.price3 = priceTier3;
    if (priceTier4 == null) {
      priceTier4 = 0;
    }
    this.price4 = priceTier4;            
  }

  Item.prototype.addVariance = function(anItemVariance) {
    this.variances.push(anItemVariance);
  }

  Item.prototype.getVariance = function(anIndex) {
    var ind = anIndex;
    return this.variances[anIndex];
  }

  Item.prototype.getVariancesSize = function () {
    return this.variances.length;
  }

  Item.prototype.getPrice = function(aTier) {
    var adjustedPrice = this.finalPrice;
    var finalPrice = this.finalPrice;
    if (aTier != null) {
      if (aTier == 1) {
        adjustedPrice = this.price1;        
      } else if (aTier == 2) {
        adjustedPrice = this.price2;
      } else if (aTier == 3) {
        adjustedPrice = this.price3;
      } else if (aTier == 4) {
        adjustedPrice = this.price4;
      }
      finalPrice = adjustedPrice;
    }
    for (var i = 0; i < this.getVariancesSize(); i++) {
      var aVariance = this.variances[i];
      var code = null;
      var e = document.getElementsByName("option"+aVariance.code)[0];
      if (e != null && aVariance != null) {
        if (e.type == "select-one") {
          if (e.selectedIndex >= 0) {
            code = e.options[e.selectedIndex].value;
          }
        } else {
          code = e.value;
        }
        if (code != null) {
          var aVarianceOption = aVariance.getVarianceOption(code)
          if (aVarianceOption != null) {
            if (aVarianceOption.flatRate) {
              adjustedPrice += aVarianceOption.value;
            } else {
              adjustedPrice += finalPrice * aVarianceOption.value / 100;
            }
          }
        }
      }
    }
    return adjustedPrice;
  }

  function ItemVariance(aLabel, aCode) {
    this.label = aLabel;
    this.code = aCode;
    this.varianceOptions = new Array();
  }

  ItemVariance.prototype.getVarianceOption = function (aCode) {
    var option = null;
    for (var i = 0; i < this.varianceOptions.length; i++) {
      var someCode = this.varianceOptions[i].code;
      if (someCode == aCode) {
        option = this.varianceOptions[i];
        break;
      }
    }
    return option;
  }

  function ItemVarianceOption(aLabel, aValue, isFlatRate, aCode) {
    this.label = aLabel;
    this.value = aValue;
    this.flatRate = isFlatRate;
    this.code = aCode;
  }

  function AdjustPriceLabel(anIndex) {
    var postFix = anIndex;
    if (postFix == null) {
      postFix = "";
      anIndex = 0;
    }
    var e = document.getElementById("masterSalePrice"+postFix);
    if (e == null) {
      e = document.getElementById("masterPrice"+postFix);
    }
    if (e != null) {
      e.innerHTML = currency+itemList[anIndex].getPrice().toFixed(limitDecimal);
    }
    var list = ["a","b","c","d","e"];
    for (var i = 0; i < 5; i++) {
      e = document.getElementById("masterPrice"+postFix+list[i]);
      if (e != null) {
        e.innerHTML = currency+itemList[anIndex].getPrice(i).toFixed(limitDecimal);
      }    
    }
  }

  function SetDefaultPrices() {
    for (var i = 0; i < itemList.length; i++) {
      AdjustPriceLabel(i);
    }
  }
  

  /*
    Copy Value

    Parameters: source aFormObject, destination aFormObjectDestination

    Copies one set of form object values to a secondary form.
  */
  
  function copyValue(aFormObject, aFormObjectDestination) {
    var value = null;
    if (aFormObject.type == "select-one") {
      aFormObjectDestination.selectedIndex = aFormObject.selectedIndex;
    } else if (aFormObject.type == "radio") {
      value = getRadioButtonValue(document.getElementsByName(aFormObject.name));
      var buttons = getByNames(aFormObject.name);
      for (var i = 0; i < buttons.length; i++) {
        if (button[i].checked) {
          getByNames[aFormObjectDestination][i].checked = true;
        } else {
          getByNames[aFormObjectDestination][i].checked = false;
        }
      }
    } else {
      aFormObjectDestination.value = aFormObject.value;
    }
  }
  
  
  /*
    Set Value
    
    Parameters source Object, value
    
    Sets the value of an object
  */  
  
  function setValue(aSourceFormObject, aValue) {
    var value = null;
    if (aSourceFormObject.type == "select-one") {
      aSourceFormObject.selectedIndex = 0;
      for (var i = 0; i < aSourceFormObject.length; i++) {
        if (aSourceFormObject.options[i].value == aValue) {
          aSourceFormObject.selectedIndex = i;
          break;
        }
      }
    } else if (aSourceFormObject.type == "radio") {
      value = getRadioButtonValue(document.getElementsByName(aSourceFormObject.name));
      var buttons = getByNames(aSourceFormObject.name);
      for (var i = 0; i < buttons.length; i++) {
        if (button[i].value == aValue) {
          button[i].checked = true;
        } else {
          button[i].checked = false;
        }
      }
    } else {
      aSourceFormObject.value = aValue;
    }  
  }  
  
  /*
    copySameAs

    Parameters: billing/shipping string, form name string, checkbox object

    Copies billing/shipping form back and forth.  Set aTypeString to billing to
    copy billing to shipping.  Set aTypeString to shipping to copay shipping to
    billing.  Calls changeState() to make changes appear and work correctly.
  */
  function copySameAs(aTypeString, aForm, aCheckbox) {
    function someCheckbox() {
      this.checked = true;
    }
    if (aCheckbox == null) {
      aCheckbox = new someCheckbox();
    }  
    if (aCheckbox.checked) {
      var otherString = "";
      if (aTypeString == "billing") {
        otherString = "shipping";
      } else {
        otherString = "billing";
      }
      var theForm = getByNames(aForm)[0];
      for (var i = 0; i < theForm.elements.length; i++) {
        var anElement = theForm.elements[i];
        if (anElement.name.indexOf(aTypeString) == 0 && anElement.name.indexOf("Ind") == -1) {
          var otherField = anElement.name.substring(aTypeString.length, anElement.name.length);
          var destElements = getByNames(otherString+otherField);
          for (var j = 0; j < destElements.length; j++) {
            var destId = destElements[j].id;
            var sourId = anElement.id;
            if (sourId != null && sourId.length > 0) {
              if (destId != null && destId.length > 0) {
                if (sourId.substring(aTypeString.length, sourId.length) == destId.substring(otherString.length, destId.length)) {
                  copyValue(anElement, destElements[j]);
                }
              }
            } else {
              copyValue(anElement, destElements[j]);
            }
          }
        }
      }

      changeState(otherString+"Country");
      aCheckbox.checked = false;
    }
  }  

  function limitText(aText, aLimit) {
    if (aText.length > aLimit) {
      return true;
    } else {
      return false;
    }
  }
  
  function checkText(aTextElement, aLimit, aMsg) {
    if (limitText(aTextElement.value, aLimit)) {
      aTextElement.value = aTextElement.value.substring(0, aLimit);
      document.getElementById(aMsg).style.display = "inline";
    } else {
      document.getElementById(aMsg).style.display = "none";
    }    
  }

  window.addEvent('domready', function() {
    var orderForms = $$('.orderForm');//document.getElementsByName('orderForm');
    if (orderForms != null && orderForms.length > 0) {
      orderForms.each(function(orderForm) {
        orderForm.addEvent('submit', function(event) {
          var ok = true;
          var optionString = '';
          var selects = orderForm.getElements('select');
          selects.each(function(select) {
            if (select.options[select.selectedIndex].value == 'INVALID') {
              ok = false;
              optionString += select.options[0].innerHTML + ', ';
            }
          });
          if (!ok) {
            event.stop();
            var theBox = orderForm.getElement('.pleaseSelectAnOption'); 
            if (theBox != null) {
              theBox.set('html', 'Please Select ' + optionString.substring(0, optionString.length -2));          
            } else {
              new Element('div', {
                'html': 'Please Select ' + optionString.substring(0, optionString.length -2),
                'class': 'attention pleaseSelectAnOption',
                'events': {
                  'click': function() {
                    this.dispose();
                  }
                }
              }).inject(orderForm.getElement('input.button').getParent(),'bottom');
            }
          }
        });
      });
    }
  });
