// common scripts
  
  function scaleToFit(img, w, h){
    // reset style to auto so img.width reflects raw image size
    img.style.width="auto";
    img.style.height="auto";
    var scale = 0;
    var hScale =  0;
    if(w > 0 && img.width > w) scale = w/img.width;
    if(h > 0 && img.height > h) {
      hScale = h/img.height;
      if(scale==0 || hScale < scale) scale=hScale;
    }
    if(scale > 0){
      /*alert(img.src + "\n"
           + scale + "\n"
           + img.width + " -> " + img.width*scale + "\n"
           + img.height + " -> " + img.height*scale
           );*/
      img.style.height = "auto"
      img.style.width = (img.width*scale) + "px";
      //img.height = img.height*scale;
      // //img.width = img.width*scale;
    }
  }

  function showUSDollars(){
    if(!global) return;
    if(global.cdnDollarInUS <= 0) return;
    // for all products
    for(var i=0; i< selectorIds.length; i++){
      if(!selectorIds[i]) continue; // skip
      var selector = document.getElementById(selectorIds[i])
      // for all priced options
      for(var j =0; j < data[i].length; j++){
        var record = data[i][j]
        if (!record) continue;
        var cdnPrice = record.price;
        var usPrice = cdnPrice * global.cdnDollarInUS;
        if(selector.options[j]){
          selector.options[j].text 
            = dollarize(cdnPrice) + " / " + dollarize(usPrice) + "  " + record.description;
        }
      }
    }
  }
  
  // generic positive number decimal formatting function
  function format (expr, decPlaces) {
    var str = "" + Math.round (eval(expr) * Math.pow(10,decPlaces))
    while (str.length <= decPlaces) {
      str = "0" + str}
    var decPoint = str.length - decPlaces
    return str.substring(0,decPoint) + "." + str.substring(decPoint,str.length);
  }

  function dollarize (expr) {
    return "$" + format(expr,2)
  }


