// library.js -- Library of site wide routines
//


/**
Dollar function -- used to easily reference a page element by id
*/
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
    }
  return elements;
}

function whsepriority() {
  alert("this is a test");
  var first_val=document.getElementById("whse_pri1").value;
  var second=document.getElementById("whse_pri2");
  var third=document.getElementById("whse_pri3");
  var second_val=second.options[second.selectedIndex].value;
  var third_val=third.options[third.selectedIndex].value;
  var priority=first_val+second_val+third_val;
}

function setCookie( name, value, expires, path, domain, secure ) {
  // Set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );

  /* If the expires variable is set, make the correct
     expires time, the current script below will set
     it for x number of days, to make it for hours,
     delete * 24, for minutes, delete * 60 * 24       */
  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
    }
  var expires_date = new Date( today.getTime() + ( expires) );

  document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}


// function to set cookie
function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/**
Show / hide functions which toggle display of element
*/
function show(id){ $(id).style.display = ''; }
function show_blk(id){ $(id).style.display = 'block'; }
function hide(id){ $(id).style.display = 'none'; }

// function to toggle display of login form
function ToggleLogin(id) {
  if ($(id).style.display == 'none') {
    $(id).style.display = 'block';
    document.loginform.user.focus();
  } else {
    $(id).style.display = 'none';
  }
}

// function to toggle display of forgot password form
function ToggleForgot(id) {
  if ($(id).style.display == 'none') {
    $(id).style.display = 'block';
    document.forgotform.email.focus();
  } else {
    $(id).style.display = 'none';
  }
}

// function to toggle display of advanced filters box
function ToggleFilter(id) {
  if ($(id).style.display == 'none') {
    $(id).style.display = 'block';
  } else {
    $(id).style.display = 'none';
  }
}

// function to toggle opening/closing of catalog tree selection
function ToggleCatalog(image,id) {
  if ($(id).style.display == 'none') {
    $(id).style.display = 'block';
    image.src = "images/icons/minus.gif";
  } else {
    $(id).style.display = 'none';
    image.src = "images/icons/plus.gif";
  }
}

// function to toggle display of prices
function TogglePrices() {
  alert ("This feature is not yet completed.");
  setCookie("hide_prices","FindThisCookie","","/","","")
setStyleByClass('*','price','visibility','hidden');
}

// function to jump to selected manufacturer page
function mfgChanged(sel) {
  mfg= sel.value;
  loc=top.location.href.substring(0,top.location.href.length-top.location.search.length)
  window.location = loc + "?wscdet_show="+mfg;
}

// function to toggle display of tracking number detail list
var openTracking="";
function ShowTracking(id) {
  if (openTracking!="") $(openTracking).style.display='none';
  if (id!="") $(id).style.display='block';
  openTracking=id;
  return false;
}

// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;

function setStyleByClass(t,c,p,v){
  var elements;
  if(t == '*') {
    // '*' not supported by IE/Win 5.5 and below
    elements = (ie) ? document.all : document.getElementsByTagName('*');
  } else {
    elements = document.getElementsByTagName(t);
  }
  for(var i = 0; i < elements.length; i++){
    var node = elements.item(i);
    for(var j = 0; j < node.attributes.length; j++) {
      if(node.attributes.item(j).nodeName == 'class') {
        if(node.attributes.item(j).nodeValue == c) {
          eval('node.style.' + p + " = '" +v + "'");
        }
      }
    }
  }
}

