function $(s) {
  return document.getElementById(s);
}

function doLoad() {
  // does nothing
}

function addEvent( obj, type, fn ){  // the add event function
  if (obj.addEventListener) obj.addEventListener( type, fn, false );
  else if (obj.attachEvent) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() {
      obj["e"+type+fn]( window.event );
    };
    obj.attachEvent( "on"+type, obj[type+fn] );
  }
}

var firstEl;

function validateForm(frm) {
  errs = "";
  firstEl = null;
  els = frm.elements;
  for(i=0; i<els.length; i++) {
    if(els[i].id == "req")
      errs += checkVal(els[i]);
  }
  if(errs != "") {
    alert("Please make the following changes:\n\n" + errs);
    if(firstEl)
      firstEl.focus();
    return false;
  }
  return true;
}

function checkVal(el) {
  if(el) {
    if(((el.tagName == "INPUT" || el.tagName == "TEXTAREA") && el.value == "")
       || (el.tagName == "SELECT" && el.options[el.selectedIndex].value == "")) {
      if(firstEl == null)
        firstEl = el;
      return "- " + el.title + "\n";
    }
  }
  return "";
}

/** MOUSE OVER EFFECTS **/
function preloadImgs() {
  imgs = document.images;
  for(i=0; i<imgs.length; i++) {
    src = imgs[i].src;
    if(imgs[i].className != 'no-hover' && src.indexOf('_off') != -1) {
      document.onImg = new Image();
      document.onImg.src = src.replace('_off', '_on');

      addEvent(imgs[i], "mouseover", toggleImage);
      addEvent(imgs[i], "mouseout", toggleImage);
    }
  }
  imgs = document.getElementsByTagName("input");
  for(i=0; i<imgs.length; i++) {
    if(imgs[i].type.toLowerCase() == 'image') {
      src = imgs[i].src;
      if(imgs[i].className != 'no-hover' && src.indexOf('_off') != -1) {
        document.onImg = new Image();
        document.onImg.src = src.replace('_off', '_on');

        addEvent(imgs[i], "mouseover", toggleImage);
        addEvent(imgs[i], "mouseout", toggleImage);
      }
    }
  }
}

addEvent(window, "load", preloadImgs);

function toggleImage() {
  src = this.src;
  if(src.indexOf('_off') != -1)
    this.src = src.replace('_off', '_on');
  else if(src.indexOf('_on') != -1)
    this.src = src.replace('_on', '_off');
}

function toggleImg(img) {
  src = img.src;
  if(src.indexOf('_off') != -1)
    img.src = src.replace('_off', '_on');
  else if(src.indexOf('_on') != -1)
    img.src = src.replace('_on', '_off');
}

function preloadImg(img) {
  src = img.src;
  document.onImg = new Image();
  document.onImg.src = src.replace('_off', '_on');
}
function goTo(u) {
  window.location = u;
}

function openPopupWindow(url, width, height, scrolling, name) {
  if (!width) width = 350;
  if (!height) height = 350;
  if (!scrolling) scrolling = "no"; 
  if (!name) name = "win"+Math.round(Math.random()*1000); 
  
  features = "width="+width+","
           + "height="+height+","
           + "toolbar=no,"
           + "location=no,"
           + "status=no,"
           + "menubar=no,"
           + "scrollbars="+scrolling+","
           + "top=50,"//+(window.screen.height-height)/2+","
           + "left=50";//+(window.screen.width-width)/2;
  window.open(url,name,features);
}

function enlargeImg(u) {
  openPopupWindow('/popup.jsp?url='+u, 350, 350, 'no', 'imgWin');
}

function checkMe(frm) {
  q = parseInt(frm.quantity.value);
  if(""+q == "NaN" || q < 1) {
    alert("Please enter a valid quantity");
    frm.price.focus();
    return false;
  }
  return true;
}

var p = new Array;
function updatePrice(sel, ind, price) {
  p[ind] = sel.options[sel.selectedIndex].title;
  pAdjust = 0;
  for(i=0; i<p.length; i++) {
    if(p[i])
      pAdjust += parseFloat(p[i]);
  }
  nPrice = price + pAdjust;
  document.forms['orderForm'].price.value = nPrice;
  $('thePrice').innerHTML = formatCurrency(nPrice);
}

function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
  cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}
