isDOM = (document.getElementById) ? true : false;
isOpera = isOpera5 = window.opera && isDOM;
isOpera6 = isOpera && window.print;
isOpera7 = isOpera && navigator.userAgent.indexOf("Opera 7") > 0 || navigator.userAgent.indexOf("Opera/7") >= 0;
isMSIE = isIE = document.all && document.all.item && !isOpera;
isIE4 = isIE && !isDOM;
isNC = navigator.appName == "Netscape";
isNC4 = isNC && !isDOM;
isNC6 = isMozilla = isNC && isDOM;

function findPos(obj) {
	var temp, x = 0, y = 0;
	if(obj.offsetParent){
		temp = obj;
		while(temp.offsetParent) {
			temp = temp.offsetParent;
			x += temp.offsetLeft;
			y += temp.offsetTop;
		}
	}
	x += obj.offsetLeft;
	y += obj.offsetTop;
	if(isIE)
		x--;
	else
		y--;
  return [x,y];
}

function CategoryDisplayControl_ShowCategories(div, parentDiv)
{
    var pos = findPos(parentDiv);
    div.style.left = pos[0];
    div.style.top = pos[1];
    div.style.display = 'block';
}

function CategoryDisplayControl_ScheduleHiding(div)
{
    div.timer = window.setTimeout('CategoryDisplayControl_HideCategories(document.getElementById("' + div.id +
        '"))', 500);
}

function CategoryDisplayControl_CancelHiding(div)
{
    if (div.timer)
    {
        window.clearTimeout(div.timer);
    }
}

function CategoryDisplayControl_HideCategories(div)
{
    div.style.display = 'none';
}

function CategoryDisplayControl_HandleCheck(obj, selectedLabel, IdsHidden, ValuesHidden)
{
    CategoryDisplayControl_FormatSelectionLabelAndValuesHidden(selectedLabel, IdsHidden, ValuesHidden);
}

function CategoryDisplayControl_GetCategoryName(obj)
{
    var categoryName = obj.category ? obj.category : obj.attributes['category'].nodeValue;
    return categoryName;
}

function CategoryDisplayControl_FormatSelectionLabelAndValuesHidden(selectedLabel, IdsHidden, ValuesHidden)
{
    var ids = IdsHidden.value.split(',');
    var selectedMsg = '';
    var valueHidden = '';
    for(var i = 0; i < ids.length; i++)
    {
        var obj = document.getElementById(ids[i]);
        if (obj.checked == true)
        {
            selectedMsg += CategoryDisplayControl_GetCategoryName(obj) + ", ";
            valueHidden += obj.value + ",";
        }
    }
    if (selectedMsg == '')
    {
        selectedMsg += ' None';
    }
    else
    {
        selectedMsg = selectedMsg.substr(0, selectedMsg.length - 2);
        valueHidden = valueHidden.substr(0, valueHidden.length - 1);
    }
    
    if (selectedLabel != null) selectedLabel.innerHTML = "Selected: " + selectedMsg;
    
    ValuesHidden.value = valueHidden;
}

function ClickControl(ctrl)
{
    if (isIE)
    {
        ctrl.click();
    }
    else
    {
        //this is a [horrible] way to raise click event in FF.
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0, false, false, false, false, 0, null);
        ctrl.dispatchEvent(evt);
    }
}

function SwapItemDisplay(objId, visibleValue)
{
    var obj = document.getElementById(objId);
    if (!obj)
        return false;
    if (obj.style.display == 'none')
    {
        obj.style.display = visibleValue;
    }
    else
    {
        obj.style.display = 'none';
    }
    return true;
}

function urlencode(str)
{
    return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}

function urldecode(str)
{
    return unescape(str.replace('+', ' '));
}

function htmlEscape(str) {
    return String(str)
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;');

}
function htmlDivEncode(value) {
    return htmlEscape($('<div/>').text(value).html());
}
function htmlDecode(value) {
    return $('<div/>').html(value).text();
}
function htmlEscape1(str) {
    return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');

}
