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 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 getOffsetPosition(el)
{
    var x = forceInt(el.offsetLeft);
    var y = forceInt(el.offsetTop);

    return {"x": x, "y": y};
}

function nodeValue(node)
{
    return (node.firstChild == null) ? '' : node.firstChild.nodeValue
}

function toggleDiv(id)
{
    var div = document.getElementById(id);
    if (!div)
        return;
        
    div.style.display = (div.style.display == 'none') ? '' : 'none';
}

// 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);
}

// 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;
}

function popupImage(url, width, height)
{
    popup(url, width, height, 'img', false);
}

// 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); 
    }
}

// SEARCH FIELDS
// 

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';
    }
}

function passwordFocus(t, p) 
{
    p = document.getElementById(p);
    p.style.display = '';
    p.focus();
    t.style.display = "none";
}

function passwordBlur(p, t) 
{
    if (p.value == '') 
    {
        t = document.getElementById(t);
        t.style.display = '';
        p.style.display = 'none';
    }
}

// CONTINUE SHOPPING

function continueSave(url)
{
    window.name = 'cs:' + url;
}

function continueGo(fallback)
{
    if (window.name && window.name.substring(0,3) == 'cs:')
    {
        window.location = window.name.substring(3);
        return;
    }
    
    window.location = fallback;
}

// DISABLE BUTTON

function disableGet(id)
{
    var f = null;
    if (document.getElementById)
        f = document.getElementById(id);
    else if(document.all)
        f = document.all[id];
    else if(document.layers)
        f = document.layers[id];    

    return f;
}

function disableButton(id)
{
    var f = disableGet(id);
    if (!f)
        return false;

    if (f.disabled == true)
    {
        return false;
    }
    else
    {
        // f.value = 'Processing';
        setTimeout("disableTimeout('" + id + "')", 5);
        return true;
    }
}
 
function disableTimeout(id)
{
    var f = disableGet(id);
    if (!f)
        return;
     
    f.disabled = true;
}




// FLY-OUT LIST
//

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);

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;

        drop.style.top = rootPos.y + rootH + getScrollPosition().scrollTop + 1;
        //drop.style.top = rootPos.y + rootH + 1;
        drop.style.left = rootPos.x - 1;        
        drop.style.visibility = 'visible';
        drop.style.display = 'inline';
        
        var listH = getDivH(list);
        if (listH > 100) {
            if (listW + 30 > rootW)
                flyW = flyW + 30;

            list.style.height = '225px';
            list.style.overflow = 'auto';
        }

        list.style.width = (flyW - 2) + 'px';

        _flyOutListCur = id;
    }
    else 
    {
        // Hide
        drop.style.visibility = '';
        drop.style.display = 'none';        

        list.style.width = '';
        list.style.height = '';
        list.style.overflow = '';

        _flyOutListCur = null;
    }
}

function getScrollPosition() 
{
    var iev = getInternetExplorerVersion();
    
    var scrollTop;
    var scrollLeft;

    if (iev >= 8)
    {
        scrollTop = 0;
        scrollLeft = 0;
    }
    else 
    {
        scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
        scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
    }    
    
    return { "scrollTop": scrollTop, "scrollLeft": scrollLeft };
}

function getInternetExplorerVersion() 
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') 
    {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function getElementIndex(e)
{
    var nodes = e.parentNode.childNodes, node;
    var i = count = 0;
    while ((node = nodes.item(i++)) && node != e)
        if (node.nodeType == 1)
            count++;
    return count;
}
