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 || el.clientWidth || 0)
		: 0
	);
}

function getDivH(el)
{
	return forceInt(
		el ? (el.offsetHeight || el.style.pixelHeight || el.style.height || el.clientHeight || 0)
		: 0
	);
}

function nodeValue(node)
{
    return (node.firstChild == null) ? '' : node.firstChild.nodeValue
}

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};
}

// 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) == 'rn_')
    winId = 'rn_' + winId;
	var wtop = (screen.height - height) / 2 - 30;
	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=yes,menubar=no,copyhistory=no');
  ref.focus();
}

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);
}

// 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 searchFocus(f)
{
    if (f.value == f.title)
    {
        f.value = '';
    }
}

function searchBlur(f)
{
    if (f.value == '')
    {
        f.value = f.title;
    }
}

function returnFromPopup(url)
{
    window.opener.refreshPage(url);
    window.close();
}

function refreshPage(url)
{
    window.focus();
    if(url == null)
      window.location.replace(window.location.href);
    else
      window.location.replace(url);
}

function increaseFontSize(factor)
{    
    fontSize = parseInt(readCookie("fontsize"));
    if (fontSize == 'undefined' || !fontSize)
    {
        fontSize = document.body.style.fontSize;
    }
    
    factor = parseFloat(factor);
    if (factor == 'undefined' || factor == null || !factor)
    {
        factor = 1.1;   
    }        
        
    fontSize = parseInt(fontSize * factor);
    setFontSize(fontSize);
}

function decreaseFontSize(factor)
{
    fontSize = parseInt(readCookie("fontsize"));
    if (fontSize == 'undefined' || !fontSize)
    {
        fontSize = document.body.style.fontSize;
    }
    
    factor = parseFloat(factor);
    if (factor == 'undefined' || factor == null || !factor)
    {
        factor = 0.9;   
    }        
        
    fontSize = Math.max(parseInt(fontSize * factor), 10);
    setFontSize(fontSize);
}

function setFontSize(fontSize)
{    
    if (fontSize == 'undefined' || !fontSize)
    {
        fontSize = parseInt(readCookie("fontsize"));
    }
    
    if (!isNaN(fontSize))
    {
        changeClassFontSize("resize", fontSize);
    }
}

function changeClassFontSize(cssClass, fontSize)
{
    var elements = getElementsByAttribute(document.body, "*", "class", cssClass);
    for(var i in elements)
    {
        elements[i].style.fontSize = fontSize + "px";
    }
    
    createCookie("fontsize", fontSize, 7);
}
 
//Copyright Robert Nyman, http://www.robertnyman.com
//Free to use if this text is included 
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
{
    var arrElements = (strTagName == "*" && document.all) ? document.all : oElm.getElementsByTagName(strTagName);
    
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined") ? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    
    for(var i = 0; i < arrElements.length; i++)
    {
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttributeNode(strAttributeName);
        if (oAttribute)
        {
            if(typeof oAttribute.value == "string" && oAttribute.value.length > 0)
            {
                if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute.value)))
                {
                    arrReturnElements.push(oCurrent);
                }
            }
        }
    }
    return arrReturnElements;
}

function createCookie(name,value,days)
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}

//////////////////////////////////

function centerDivRelative(div, parentDiv)
{
    var d = document.getElementById(div);
    var p = document.getElementById(parentDiv);
        
    var dWidth = getDivWR(d);
    var pWidth = getDivWR(p);
    
    var offset = (pWidth - dWidth) / 2;
    d.style.left = offset;
    d.style.width = dWidth;    
}

var centerCacheH = new Array();

function getDivWR(el)
{   
    var maxW = 0;     
    var id = el.id;    
    if (centerCacheH[id])   return centerCacheH[id];    
    
    for(var i = 0; i < el.childNodes.length; i++)
    {
        var child = el.childNodes[i];
        if (child && child.style)        
            maxW = Math.max(maxW, getDivW(child));        
    }
    
    centerCacheH[id] = maxW;
    return maxW;
}

function notAuthenticated(url)
{
    var parent = window;
    while (parent.opener != null)
    {
      parent.close();
      parent = parent.opener;
    }
    parent.refreshPage(url);
}

function returnFromPhotoPopup(refresh, id)
{
    
    if (refresh == 'true') {
        window.opener.refreshPhotos(id);
        
    }
    window.close();
}