// stores new window object, so that all openWin calls reuse the same physical window
var winRef;

/* opens 'newUrl' in new stripped down window w/ dimensions of winWidth x winHeight (or 80% of screen size) */
function openWin(newUrl,winWidth,winHeight,scrBars)
{
  // close window is already open
  //if(winRef) { winRef.close() }
  winRef = '';
  // include scrollbars by default
  if(!scrBars || (scrBars != 'yes' && scrBars != 'no') ) scrBars = 'yes';
  // if no dimensions, size to 80% of screensize
  if(!winWidth) { winWidth = screen.width*0.8 }
  if(!winHeight) { winHeight = screen.height*0.8 }
  winRef = window.open(newUrl,'_blank','width='+winWidth+',height='+winHeight+',resizable=yes,scrollbars='+scrBars+',toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
}

/* prompts user with 'promptMsg', and if they click OK, directs them to 'link' */
function confirmLink(link,promptMsg)
{
  var result = confirm(promptMsg);
  if(result)
    window.location=link;
}

/* given a DOM object, grabs the value property, removes non alphanumeric characters, and (if upc), whitespace */
function filterSearch(searchObj)
{
  // remove non alphanumeric/non-whitespace chars, remove leading/trailing space
  var searchString = searchObj.value;
  searchString = searchString.replace(/[^a-z0-9 ]/ig, ' '); // replace with space
  
  // if, with no spaces, its still NOT a number
  var testString = searchString.replace(/\s+/g, '');
  if( isNaN(testString) )
  {
    searchString = searchString.replace(/\s+/ig, ' '); // remove multiple spaces
    searchString = searchString.replace(/^\s+/ig, ''); // remove leading space
    searchString = searchString.replace(/\s+$/ig, ''); // remove trailing space
  }
  else // otherwise, assume its a upc
  {
    searchString = testString; // remove ALL spaces
  }
  
  // drop result back into object
  searchObj.value = searchString;
  return true;
}

// global boolean indicative of whether browser 'is Newer than Netscape 4'
// true = IE, Mozilla, Opera, Netscape 6, etc.
// false = Netscape 4 or other antiquated browser
var isNN = (document.getElementById || document.all);

/* returns object compatible with multiple browsers by id */
// x = new getObj('example');
// if(isNN) {x.style.visibility = 'hidden} else {x.style.visibility = 'hide'}
function getObj(name)
{
  if (document.getElementById)
  {
    this.obj = document.getElementById(name);
    if(this.obj.style) this.style = this.obj.style;
    else this.style = this.obj;
  }
  else if (document.all)
  {
    this.obj = document.all[name];
    if(this.obj.style) this.style = this.obj.style;
    else this.style = this.obj;
  }
  else if (document.layers)
  {
    this.obj = getObjNN4(document,name);
    this.style = this.obj;
  }
}

/* used internally by getObj() */
function getObjNN4(obj,name)
{
  var x = obj.layers;
  var foundLayer;
  for (var i=0;i<x.length;i++)
  {
    if (x[i].id == name)
       foundLayer = x[i];
    else if (x[i].layers.length)
      var tmp = getObjNN4(x[i],name);
    if (tmp) foundLayer = tmp;
  }
  return foundLayer;
}

/* validates a numeric non-negative quantity */
function checkValidQty(formObj)
{
  // remove any whitespace
  formObj.value = formObj.value.replace(/\s+/g, "");

  // remove positive sign (if one)
  formObj.value = formObj.value.replace(/^\+(\d+)$/g, "$1");
  
  // alert if a negative number OR not a valid number
  if( formObj.value.match(/^-\d+$/) )
  {
    alert("Cannot enter a negative quantity!");
    return false;
  }
  else
  {
    if( formObj.value.match(/^\d+$/) )
    { return true; }
    else
    {
      alert("Not a valid number!");
      return false;
    }
  }
}

/* returns array of DOM nodes with given classname under given parent DOM node matching given HTML tag
NOTE: requires that target browser supports document.getElementsByTagName() */
function getElementsByClass(searchClass,node,tag) {
  // array to store matched nodes
  var classElements = new Array();
  
  // if dependant getElement(s)By methods not supported, return empty array
  if (!document.getElementsByTagName) {
    return classElements;
  }
  
  // otherwise do our magic
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

