function forceInt(x, y)
{
	return isNaN(y = parseInt(x))? 0 : y;
}

function getDivW(el)
{
	return forceInt(
		el ? (el.offsetWidth || el.style.pixelWidth || el.style.width || 0)
		: 0
	);
}

function getDivH(el)
{
	return forceInt(
		el ? (el.offsetHeight || el.style.pixelHeight || el.style.height || 0)
		: 0
	);
}

function nodeValue(node)
{
    return (node.firstChild == null) ? '' : node.firstChild.nodeValue
}

// CALENDAR
//

function calendarFocus(divID)
{
    // Mark as focused
    eval('window.focus_' + divID + ' = true;');

    // Show popup
    showDiv(divID);
}

function calendarBlur(divID)
{
    // Mark as blurred
    eval('window.focus_' + divID + ' = false;');

    // If not moused over, hide popup
    eval('var over = window.over_' + divID + ';');
    if (!over)
        hideDiv(divID);
}

function calendarOver(divID)
{
    // Mark as moused over
    eval('window.over_' + divID + ' = true;');
}

function calendarOut(divID, e)
{
    if (window.event)
        e = window.event;
        
    var div = document.getElementById(divID);
    if (!div)
        return;

    var eventX = e.clientX + document.body.scrollLeft;
    var eventY = e.clientY + document.body.scrollTop;
    var abs = getAbsolutePosition(div);
    var divW = getDivW(div);
    var divH = getDivH(div);

    // Ignore this event if the cursor is inside the div
    if (eventX > abs.x && eventX < abs.x + divW && eventY > abs.y && eventY < abs.y + divH)
        return;
    
    // Mark as moused out
    eval('window.over_' + divID + ' = false;');

    // If not focused, hide popup
    eval('var focus = window.focus_' + divID + ';');
    if (!focus)
        hideDiv(divID);
}

function calendarClose(divID)
{
  // Mark as blurred
    eval('window.focus_' + divID + ' = false;');
  
  // Close popup  
  hideDiv(divID);
}

// HOVER
//

function hoverOver(o)
{
    _hoverColor = o.style.backgroundColor;
    o.style.backgroundColor = '#CCCCCC';
}

function hoverOut(o)
{
    o.style.backgroundColor = _hoverColor;
}

function hoverClick(o)
{
    _hoverColor = '#EEEEEE';
}

// SHOW/HIDE

function showDiv(divID)
{
    var div = document.getElementById(divID);
    if (!div)
        return;

    div.style.visibility = 'visible';
}

function hideDiv(divID)
{
    var div = document.getElementById(divID);
    if (!div)
        return;

    div.style.visibility = 'hidden';
}

function hideDivTimeout(divID, time)
{
    setTimeout("hideDiv('" + divID + "')", time);
}

// POPUP
//

function popup(url, width, height, winId, scroll)
{
    if (winId == null) {
        time = new Date();
        winId = time.getTime().toString();
    }
    if (!winId.substr(0, 3) == 'tn_')
        winId = 'tn_' + winId;

    var wtop = (screen.height - height) / 2 - 75;
    var wleft = (screen.width - width) / 2;
    var ref = window.open(url,winId,'width='+width+',height='+height+',top='+wtop+',left='+wleft+',resizable=yes,scrollbars=' + (scroll ? 'yes' : 'no') + ',toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
    ref.focus();

    return ref;
}

// UPLOAD
//

function preUpload(id)
{
    var form = document.forms['aspnetForm'];
    var action = form.elements[id + '_Action'];
    var target = form.target;

    // Set values
    action.value = 'upload';    
    form.target = id + '_Target';
    
    // Submit form
    form.submit();

    // Restore values
    action.value = '';
    form.target = target;
}

function postUpload(id, filename)
{
    var hidden = document.getElementById(id + '_SlickBack');
    var slickback = hidden.value.replace("__FILE__", filename);

    eval(slickback);
}

// UPS ADDRESS VERIFICATION
//

var _avUPSWindow = null;

function openUPSAddressVerification()
{
    _avUPSWindow = popup('UPSAddressEmpty.aspx', 450, 350, 'av', true);
}

function viewUPSAddressVerification(address)
{
    if (!_avUPSWindow)
        return;

    _avUPSWindow.location = 'UPSAddressVerification.aspx?Address=' + address;
}

// USPS ADDRESS VERIFICATION
//

var _avUSPSWindow = null;

function openUSPSAddressVerification()
{
    _avUSPSWindow = popup('USPSAddressEmpty.aspx', 600, 350, 'av', true);
}

function viewUSPSAddressVerification(address)
{
    if (!_avUSPSWindow)
        return;

    _avUSPSWindow.location = 'USPSAddressVerification.aspx?Address=' + address;
}

// TREE VIEW
//

function copyAttributes(source, target)
{
    var i;
    var attrs = source.attributes;
    for (i=0; i<attrs.length; i++)
    {
        var attr = attrs.item(i);

        if (attr.name == 'style')
            target.style.cssText = attr.value;
        else
            target.setAttribute(attr.name, attr.value);
    }
}

function treeClick(img)
{
    img.className = (img.className == 'treeOpen') ? 'treeClosed' : 'treeOpen';
}

function treeViewExpand(node)
{
    var dataList = node.getElementsByTagName('data');
    if (dataList.length == 0)
        return;
        
    var data = dataList[0];
    var id = data.getAttribute('id');
    var parent = document.getElementById(id);
    if (!parent)
        return;
        
    var root = parent.parentNode;
    var target = parent.nextSibling;
    
    var i;
    for (i=0; i<data.childNodes.length; i++)
    {
        var row = data.childNodes[i];
        if (row.nodeType != 1)
            continue;

        if (row.nodeName == 'div')
        {
            var div = document.createElement('div');
            copyAttributes(row, div);
            div.innerHTML = nodeValue(row);
            
            root.insertBefore(div, target);
        }
        else
        {
            var tr = document.createElement('tr');
            copyAttributes(row, tr);

            root.insertBefore(tr, target);
            
            var j;
            for (j=0; j<row.childNodes.length; j++)
            {
                var cell = row.childNodes[j];
                
                var td = document.createElement('td');
                copyAttributes(cell, td);
                td.innerHTML = nodeValue(cell);

                tr.appendChild(td);
            }
        }
    }
}

function treeViewCollapse(node)
{
    var dataList = node.getElementsByTagName('data');
    if (dataList.length == 0)
        return;
        
    var data = dataList[0];
    var id = data.getAttribute('id');
    var parent = document.getElementById(id);
    if (!parent)
        return;
        
    var root = parent.parentNode;
        
    var current;
    var next = parent.nextSibling;
    while (next)
    {
        current = next;
        next = current.nextSibling;
    
        if (current.nodeType != 1)
            continue;
            
        var childID = current.getAttribute('id');
        if (!childID)
            continue;
        
        if (childID.substring(0, id.length) != id)
            break;
 
        root.removeChild(current); 
    }
}

function changeColorSample(txtBox, spanID)
{
  var span = document.getElementById(spanID);
  var txt = document.getElementById(txtBox);
  if(txt.value.length == 0)
  return;
  var values = txt.value.split("-");
  var R;
  var G;
  var B;
  var error = false;
  
  if(values.length == 3)
  {
    if(values[0] > 255)
    {
      R = toHex(0);
      error = true;
    }
    else
    {
      R = toHex(values[0]);
    }
      
    if(values[1] > 255)
    {
      G = toHex(0);
      error = true;
    }
    else  
    {
      G = toHex(values[1]);
    }
      
    if(values[2] > 255)
    {
      B = toHex(0);
      error = true;
    }
    else
    {
      B = toHex(values[2]);
    }
      
    if(R.toString().length == 1)
      R = "0" + R;  
    if(G.toString().length == 1)
      G = "0" + G;
    if(B.toString().length == 1)
      B = "0" + B;
  }  
  
  if(error == true)
  {
    txt.value = "";
    alert("Each of the numbers must be below 255.");
  }
  else
  {
    span.style.backgroundColor = R + G + B;
  }
}

function toHex(d) {
  var r;
   try
   {
      r = d % 16;
   }
   catch(err)
   {
      return '00';
   }
    var result;
    if(d-r==0) 
            result = toChar(r);
    else 
            result = toHex( (d-r)/16 )+toChar(r);
    return result;
}

function toChar(n) {
        var alpha = "0123456789ABCDEF";
        return alpha.charAt(n);
}

function scrollBottom()
{
var height;
if(document.all)
  height = document.body.offsetHeight;
else if(document.layers)
  height = document.body.document.height;
  window.scrollTo(0, height);
}

var productVisible = null;
function popupProduct(divID, thumbnailID)
{
  if(productVisible == divID)
    return;
    
  if(productVisible != null)
  {
    var visibleProduct = document.getElementById(productVisible);
    visibleProduct.style.display = "none";
  }
  
  var newPopup = document.getElementById(divID);
  var thumbnail = document.getElementById(thumbnailID);
  productVisible = divID;
  newPopup.style.display = "block";
  newPopup.style.top = "-60";
  newPopup.style.left = "0";
  var width = getDivW(newPopup);
  var height = getDivH(newPopup);
  var x = getAbsolutePosition(newPopup).x;
  
  var winW = getViewportWidth();
  
  var leftArrow = document.getElementById(divID + "_leftArrow");
  var rightArrow = document.getElementById(divID + "_rightArrow");
  
  rightArrow.style.display = "none";
  leftArrow.style.display = "none";
  
  var rightSideofWindow = x + width + 60;
  
  var thumbnailPosition = getAbsolutePosition(thumbnail);
  newPopup.style.top = getAbsolutePosition(thumbnail).y - 53;// + getScrollPosition().scrollTop;
    
  if(thumbnailPosition.x + width > winW - 50)
  {
    //point right
    var leftSlide = thumbnailPosition.x - 500;
    newPopup.style.left = leftSlide;
    rightArrow.style.display = "block";
  }
  else
  {
    //point left
    var rightSlide = thumbnailPosition.x + 58;
    newPopup.style.left = rightSlide;
    leftArrow.style.display = "block";
  }
}

function closeProductPopup(divID)
{
  productVisible = null;
  var product = document.getElementById(divID);
  product.style.display = "none";
}

var helpPopup = null;
function showHelp(divID)
{
  if(helpPopup != null)
  {
    var help = document.getElementById(divID);
    help.style.display = "none";
    helpPopup = null;
  }
  else
  {
    helpPopup = divID;
    var help = document.getElementById(divID);
    help.style.display = "block";
  }
}

var tooltipPopup = null;
function showTooltip(divID, thumbnailID)
{
  var thumbnail = document.getElementById(thumbnailID);
  var thumbnailPos = getAbsolutePosition(thumbnail);
  if(tooltipPopup != null)
  {
    var tooltip = document.getElementById(divID);
    tooltip.style.display = "none";
    tooltipPopup = null;
  }
  else
  {
    tooltipPopup = divID;
    var tooltip = document.getElementById(divID);
    tooltip.style.display = "block";
    tooltip.style.top = thumbnailPos.y + 5;// - getScrollPosition().scrollTop;
    tooltip.style.left = thumbnailPos.x + 15;
  }
}

var colorChartItemPopup = null;
function showColorChartItem(divID, thumbnailID)
{
  var thumbnail = document.getElementById(thumbnailID);
  var thumbnailPos = getAbsolutePosition2(thumbnail);
  if(colorChartItemPopup != null)
  {
    var colorChartItem = document.getElementById(divID);
    colorChartItem.style.display = "none";
    colorChartItemPopup = null;
  }
  else
  {
    colorChartItemPopup = divID;
    var colorChartItem = document.getElementById(divID);
    colorChartItem.style.display = "block";
    colorChartItem.style.position = "absolute";
    colorChartItem.style.top = thumbnailPos.y - 52;
    colorChartItem.style.left = thumbnailPos.x + 43;
  }
}

function showChart(divID)
{
  var colorChart = document.getElementById(divID);
  colorChart.style.display = "block";
  colorChart.style.position = "absolute";
  var top = (getViewportHeight() - getDivH(colorChart)) / 2;
  if(top < 0)
    colorChart.style.top = 0;
  else
    colorChart.style.top = ((getViewportHeight() - getDivH(colorChart)) / 2) + 'px';
  colorChart.style.left = ((getViewportWidth() - getDivW(colorChart)) / 2) + 'px';
  
}

function hideChart(divID)
{
  var colorChart = document.getElementById(divID);
  colorChart.style.display = "none";
}

function changeColorThumbnail(chartID, dropdownID, dropdownUniqueID, thumbnailID, value, id, popupid)
{
  var dropdown = document.getElementById(dropdownID);
  var thumbnail = document.getElementById(thumbnailID);
  
  if(value != '')
  {
    var swatchPopup = document.getElementById(popupid);
    swatchPopup.style.display = "none";
    hideChart(chartID);
    SlickBack.fire(dropdownUniqueID, "s:" + id);
    dropdown.value = id;
  }
  
  var values = value.split("-");
  var R = toHex(values[0]);
  var G = toHex(values[1]);
  var B = toHex(values[2]);
  if(R.toString().length == 1)
    R = "0" + R;  
  if(G.toString().length == 1)
    G = "0" + G;
  if(B.toString().length == 1)
    B = "0" + B;

    var color = new String();  
    color += R + G + B;
    thumbnail.style.backgroundColor = "#" + color;
}

function changeSeparate(chartID, dropdownID, dropdownUniqueID, value, popupid)
{
  var dropdown = document.getElementById(dropdownID);
  if(value != '')
  {
    var swatchPopup = document.getElementById(popupid);
    swatchPopup.style.display = "none";
    hideChart(chartID);
    SlickBack.fire(dropdownUniqueID, "s:" + value);
    dropdown.value = value;
  }
}

function selectSize(chartID, dropdownID, dropdownUniqueID, value)
{
  var dropdown = document.getElementById(dropdownID);
  hideChart(chartID);
  SlickBack.fire(dropdownUniqueID, "s:" + value);
  dropdown.value = value;
}

function emailClick(popupID, divID, position)
{
  var linkEl = document.getElementById(divID);
  var linkPosition = getAbsolutePosition(linkEl);
  var popupDiv = document.getElementById(popupID);
  
  var top = linkPosition.y;
  var left = linkPosition.x;
  
  switch(position)
  {
    case "Left":
      left = top + 100;
      break;
    case "Right":
      left = top - 100;
      break;
    case "BottomLeft":
      top = top + 24; 
      left = left - 200;
      break;
    case "BottomRight":
      top = top + 24; 
      break;
    case "TopLeft":
      top = top - 24;
      break;
    case "TopRight":
      top = top + 24;
      break;
  }
  popupDiv.style.top = top;
  popupDiv.style.left = left;
  popupDiv.style.Display = "block";
  popupDiv.style.visibility = "visible";
}

function calHide(popupID)
{
  hideDiv(popupID);
}


function getViewportWidth()
{
    return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
}

function getViewportHeight()
{
    return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
}

function getScrollPosition()
{
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
  return {"scrollTop": scrollTop, "scrollLeft": scrollLeft};
}

function getAbsolutePosition(el)
{
  var x = 0;
  var y = 0;
          
  while (el)
  {
    x += forceInt(el.offsetLeft);
    x -= forceInt(el.scrollLeft);

    y += forceInt(el.offsetTop);
    y -= forceInt(el.scrollTop);
    
    el = el.offsetParent || null;
  }

  return {"x": x, "y": y};
}

function getAbsolutePosition2(el)
{
  var x = 0;
  var y = 0;
          
  while (el)
  {     
    
    x += forceInt(el.offsetLeft);
    x -= forceInt(el.scrollLeft);

    y += forceInt(el.offsetTop);
    y -= forceInt(el.scrollTop);
    
    el = el.offsetParent || null;
    
    if(el != null)
    {
        if(el.style.position.length > 0)
        {
          if(el.style.position == "absolute")
          {
            return {"x": x, "y": y};
          }
        }
    }
  }

  return {"x": x, "y": y};
}

function getOffsetPosition(el)
{
    var x = forceInt(el.offsetLeft);
    var y = forceInt(el.offsetTop);

    return {"x": x, "y": y};
}

function searchFocus(f)
{
    if (f.value == f.title)
    {
        f.value = '';
        //f.className = 'input';
    }
}

function searchBlur(f)
{
    if (f.value == '')
    {
        f.value = f.title;
        //f.className = 'inputGrey';
    }
}

var saveClientID = null;
function saveFocus(clientID)
{
  if(saveClientID == null)
  {
    saveClientID = clientID;
  }
}

function resetFocus()
{
  setTimeout("setFocus()", 1000);
}

function setFocus()
{
  if(saveClientID != null)
  {
    var el = document.getElementById(saveClientID);
    el.focus();
    saveClientID = null;
  }
}

function addEvent(obj, type, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(type, fn, false);
        EventCache.add(obj, type, fn);
    }
    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]);
        EventCache.add(obj, type, fn);
    }
    else {
        obj["on" + type] = obj["e" + type + fn];
    }
}

var EventCache = function() {
    var listEvents = [];
    return {
        listEvents: listEvents,
        add: function(node, sEventName, fHandler) {
            listEvents.push(arguments);
        },
        flush: function() {
            var i, item;
            for (i = listEvents.length - 1; i >= 0; i = i - 1) {
                item = listEvents[i];
                if (item[0].removeEventListener) {
                    item[0].removeEventListener(item[1], item[2], item[3]);
                };
                if (item[1].substring(0, 2) != "on") {
                    item[1] = "on" + item[1];
                };
                if (item[0].detachEvent) {
                    item[0].detachEvent(item[1], item[2]);
                };
                item[0][item[1]] = null;
            };
        }
    };
} ();
addEvent(window, 'unload', EventCache.flush);


// FLY-OUT LIST
//

var _flyOutListCur = null;
var _flyOutListInit = false;

function flyOutListOver(div) {
    div.className = 'flyOutListOver';
}

function flyOutListOut(div) {
    div.className = 'flyOutListOut';
}

function flyOutListClose() {
    if (_flyOutListCur)
        flyOutListToggle2(_flyOutListCur);
}

function flyOutListToggle(id) {
    if (!_flyOutListInit) {
        addEvent(document, 'click', flyOutListClose);
        _flyOutListInit = true;
    }

    if (_flyOutListCur && _flyOutListCur != id)
        flyOutListToggle2(_flyOutListCur);

    _flyOutListCur = id;
}

function flyOutListToggle2(id) {

    var root = document.getElementById(id);
    if (!root)
        return false;

    var drop = document.getElementById(id + '_Drop');
    if (!drop)
        return false;

    var list = document.getElementById(id + '_List');
    if (!list)
        return false;

    if (drop.style.visibility == '') {
        // Show
        var rootPos = getAbsolutePosition(root);
        var rootW = getDivW(root);
        var rootH = getDivH(root);

        var listW = getDivW(list);

        var flyW = listW;
        if (flyW < rootW)
            flyW = rootW;

        var listH = getDivH(list);
        if (listH > 100) {
            if (listW + 30 > rootW)
                flyW = flyW + 30;

            list.style.height = '100px';
            list.style.overflow = 'auto';
        }

        list.style.width = (flyW - 2) + 'px';

        drop.style.top = rootPos.y + rootH - 1;
        //drop.style.width = flyW + 'px';
        drop.style.visibility = 'visible';

        _flyOutListCur = id;
    }
    else {
        // Hide
        drop.style.visibility = '';
        //drop.style.width = '';

        list.style.width = '';
        list.style.height = '';
        list.style.overflow = '';

        _flyOutListCur = null;
    }
  }

  function positionDiv(divID, posX, posY) {
    var div = document.getElementById(divID);

    div.style.left = posX;
    div.style.top = posY;
  }

  function divColorChange(divID, color) {
    div = document.getElementById(divID);
    div.style.borderColor = color;
  }

  function imageMapClick(ctlID, imageID, e) {
    var image = document.getElementById(imageID);
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
      posx = e.pageX;
      posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
      posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
      posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;

      posx = posx - 2;
      posy = posy - 3;
    }

    var imagePos = getAbsolutePosition(image);
    var scrollPos = getScrollPosition()

    posx = posx - imagePos.x - scrollPos.scrollLeft;
    posy = posy - imagePos.y - scrollPos.scrollTop;

    SlickBack.fire(ctlID, 'NP:' + posx + ":" + posy);
  }

  // AUTO TAB STUFF
  var isNN = (navigator.appName.indexOf("Netscape") != -1);

  function autoTab(input, len, e) {
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0, 8, 9] : [0, 8, 9, 16, 17, 18, 37, 38, 39, 40, 46];
    if (input.value.length >= len && !containsElement(filter, keyCode)) {
      input.value = input.value.slice(0, len);
      input.form[(getIndex(input) + 1) % input.form.length].focus();
    }

    function containsElement(arr, ele) {
      var found = false, index = 0;
      while (!found && index < arr.length)
        if (arr[index] == ele)
        found = true;
      else
        index++;
      return found;
    }

    function getIndex(input) {
      var index = -1, i = 0, found = false;
      while (i < input.form.length && index == -1)
        if (input.form[i] == input) index = i;
      else i++;
      return index;
    }
    return true;
}

function testShowPopup(w, h, divID, parentID, offset) {
    var popUp = document.getElementById(divID);
    var parent = document.getElementById(parentID);
    popUp.style.display = "block";
    popUp.style.visibility = "visible";
    var x = getAbsolutePosition(parent).x;
    var y = getAbsolutePosition(parent).y + offset;
    popUp.style.left = x + "px";
    popUp.style.top = y + "px";
}

function testHidePopup(divID) {
    var popUp = document.getElementById(divID);
    popUp.style.visibility = "hidden";
}