/*
  Scrub the Search
*/
  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;
  }


/*
  Get Id

  Parameters: anId

  Returns the DOM object associated to the Id passed into the function.
*/
  function getById(aString) {
    return document.getElementById(aString);
  }


/*
  Get Names

  Parameters: names

  Returns the DOM objects associated to a partial match of the name string
  passed into the function.
*/
  function getByNames(aString) {
    return document.getElementsByName(aString);
  }

/*
  ----------------------------------------------------------------------------
    Check for Valid Email

    checks for a period and the at symbol

    anId is the ID of the input field

    return true if passed, false if failed

    Ex: <form action="#" onsubmit="return checkEmail('emailA')">
          <input name="emailA" id="emailA" type="text">
  ----------------------------------------------------------------------------
*/

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;
}

/*
  ----------------------------------------------------------------------------
    Display Pricing   
  ----------------------------------------------------------------------------
*/
  var limitDecimal = 2;
  var currency = "$";
  var itemList = new Array();
  
  function Item(aPrice, aSalePrice) {
    this.variances = new Array();
    this.price = aPrice;
    this.aSalePrice = aSalePrice;
    this.finalPrice = aPrice;
    if (aSalePrice != null && aSalePrice > 0) {
      this.finalPrice = aSalePrice;
    }
  }
  
  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() {
    var adjustedPrice = this.finalPrice;
    for (var i = 0; i < this.getVariancesSize(); i++) {
      var aVariance = this.variances[i];
      var code = null;
      //var e = document.getElementById("variance"+i);
      var e = document.getElementsByName("option"+aVariance.code)[0];
      if (e != null && aVariance != null) {
        if (e.type == "select-one") {
          code = e.options[e.selectedIndex].value;
        } else {
          code = e.value;
        }
        var aVarianceOption = aVariance.getVarianceOption(code)
        if (aVarianceOption.flatRate) {
          adjustedPrice += aVarianceOption.value;
        } else {
          adjustedPrice += this.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);
    }
  }
  
  function SetDefaultPrices() {
    for (var i = 0; i < itemList.length; i++) {
      AdjustPriceLabel(i);
    }
  }
