///Select nodes implementation for Firefox - http://www.wrox.com/WileyCDA/Section/id-291861.html
if(navigator.appName.indexOf("Microsoft") < 0) {
    if(!Element.prototype.selectNodes) {
        Element.prototype.selectNodes = function (sXPath) {
            var oEvaluator = new XPathEvaluator();
            var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
            var aNodes = new Array;
            if (oResult != null) {
                var oElement = oResult.iterateNext();
                while(oElement) {
                    aNodes.push(oElement);
                    oElement = oResult.iterateNext();
                }
            }
            return aNodes;
        };
    }

    if(!Element.prototype.selectSingleNode) {
        Element.prototype.selectSingleNode = function (sXPath) {
            var oEvaluator = new XPathEvaluator();
            var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
            if (oResult != null) {
                return oResult.singleNodeValue;
            } else {
                return null;
            }
        };
    }
}

function createXMLDocument() {
    var xmlDoc = null;
    if (document.implementation && document.implementation.createDocument) {
        xmlDoc = document.implementation.createDocument("", "", null);
    } else {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    }
    return xmlDoc;
}

function serializeXML(elem) {
    if(typeof XMLSerializer != 'undefined') {
        return new XMLSerializer().serializeToString(elem);
    } else {
        return elem.xml;
    }
}


/******************general inheritance convenience (taken from http://phrogz.net/JS/Classes/OOPinJS2.html) ************************/
Function.prototype.inheritsFrom = function( parentClassOrObject ){
	if(parentClassOrObject.constructor == Function) {
		//Normal Inheritance
		this.prototype = new parentClassOrObject;
		this.prototype.constructor = this;
		this.prototype.parent = parentClassOrObject.prototype;
	}
	else {
		//Pure Virtual Inheritance
		this.prototype = parentClassOrObject;
		this.prototype.constructor = this;
		this.prototype.parent = parentClassOrObject;
	}
	return this;
}

function getCommonStyleHTML() {
    return "<link href=\"styles-universal.css\" rel=\"stylesheet\" type=\"text\/css\">";
}

function displayFrameMessage(frameName, message) {
    try {
        var doc = frames[frameName].document;
        doc.open();
        doc.writeln("<html><head>");
       	doc.writeln(getCommonStyleHTML());
        doc.writeln("</head><body id=\"loader\">");
		doc.writeln("<div id=\"messageholder\">");
        doc.writeln(message);
		doc.writeln("<\/div>");
		doc.writeln("</body></html>");
    } catch(e) {
    }
}

function findAncestorObject(funcName) {
    var win = window;
    try {
        while(win != null) {
            var func = eval("win." + funcName);
            if(func != undefined) {
                return func;
            }
            if(win == win.parent) {
                break;
            }
            win = win.parent;
        }
    } catch(e1) {
    }
    return null;
}

window._local_cached_resource_bundle = null;
function getResource(key, substitutionArgs) {
    if(window._local_cached_resource_bundle == null) {
        window._local_cached_resource_bundle = ("resourceBundle");
    }
    if(window._local_cached_resource_bundle == null) {
        return null;
    }
    return window._local_cached_resource_bundle.getResource(key, substitutionArgs);
}

function setGlobalResourceBundle(resourceBundleObj) {
    window.resourceBundle = resourceBundleObj;
}

// functions for enabling/disabling hrefs--------------------
function enableLink(linkObj) {
    if (typeof linkObj.prevHref != 'undefined') {
          linkObj.href = linkObj.prevHref;
    }
    if (typeof linkObj.enabledClassName != 'undefined') {
        linkObj.className = linkObj.enabledClassName;
    }
}

function disableLink(linkObj) {
    if (typeof linkObj.href != 'undefined') {
        linkObj.prevHref = linkObj.href;
        linkObj.removeAttribute("href");
    }
    linkObj.enabledClassName = linkObj.className;
    linkObj.className = 'disabledTask';
}

function doNothing() {
    try {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    } catch(e) {}
    return false;

}

function disableChildElements(elem) {
    try {
        if(typeof elem.isAlreadyDisabled != 'undefined' && elem.isAlreadyDisabled) {
            return false;
        }

        if (elem.nodeType == 1 /*Node.ELEMENT_NODE*/) {
            if(elem.tagName == 'A') {
                disableLink(elem);
            }
            elem.onclick = doNothing;

            var children = elem.childNodes;
            for(var i = 0; i < children.length; i++) {
                disableChildElements(children[i]);
            }
            elem.isAlreadyDisabled = true;
        }
    } catch(e) {
    }
}

//-----------------------------------------------------------

// functions for general HTML utilities
function getRadioValue(radioObj) {
    for(var i = 0; i < radioObj.length; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
}


// for help links [?]

function openHelp(url) {
      options = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=598,height=550,top=200,left=300";
//      window.open("launchHelp.jsp?helpTopicId=" + top.helpTopicId, "IBM Information Server", options);
      var helpWin = newwindow = window.open(url,'MyHelp',options);      
      helpWin.focus();
    }

var _global_tool_tip_handle = null;
try {
    _global_tool_tip_handle = window.createPopup();
} catch(e1) {

}
function showToolTip(text, style)
{
    var lefter = -5;
    var topper = 18;
//    _global_tool_tip_handle.document.open();
//    _global_tool_tip_handle.document.writeln(getCommonStyleHTML());
//    _global_tool_tip_handle.document.close();
//    _global_tool_tip_handle.document.body.className = style;
    var oPopBody = _global_tool_tip_handle.document.body;
    oPopBody.style.backgroundColor = "#eaeff3";
    oPopBody.style.border = "solid #9fabbb 1px";
    oPopBody.style.fontSize = "11px";
    oPopBody.style.fontFamily = "Tahoma,Verdana, Arial, Helvetica, sans-serif";
    oPopBody.style.padding = "7px";

    oPopBody.innerHTML = text;

    //make the popup render itself ofscreen to get the correct width and height
    _global_tool_tip_handle.show(lefter, topper, 1, 1, event.srcElement);
    var scrollWidth = oPopBody.scrollWidth;
    var scrollHeight = oPopBody.scrollHeight;
    _global_tool_tip_handle.show(lefter, topper, scrollWidth, scrollHeight, event.srcElement);

}

function showAssetToolTip(elem, attr, style) {
    var nodes = elem.childNodes;
    for(var i = 0; i < nodes.length; i++) {
         var node = nodes[i];
         if ((node.nodeType == 1) && (node.nodeName == 'DIV')) {
            return showToolTip(node.innerHTML, style);
         }
    }
 }

function hideToolTip()
{
    _global_tool_tip_handle.hide();
}


function compareAllIgnoreCase(str1,str2) {
	str1 = str1.toUpperCase();
	str2 = str2.toUpperCase();

	for(var i = 0; i < str1.length && i < str2.length; i++)	{
		var code1 = str1.charCodeAt(i);
		var code2 = str2.charCodeAt(i);

		//LOWER VALUE OF UNDERSCORE---------------
		code1 = (code1 == 95) ? 64 : code1;
		code2 = (code2 == 95) ? 64 : code2;
		//----------------------------------------

		if(code1 > code2) {
			return 1;
		}
		else if(code1 < code2) {
			return -1;
		}
	}
	if(str1.length < str2.length) {
		return -1;
	}
	else if(str1.length > str2.length) {
		return 1;
	}
	return 0;
}

function binarySearch(opts, text, compareFunc) {
	var low = 0;
	var high = opts.length -1;
	var mid;
	var opt;
	var found = false;
	while (low <= high)	{
		mid = parseInt((low + high)/2);
		opt = opts[mid];
		var comp = compareFunc(opt.text,text);
		if(comp < 0) {
			low = mid + 1;
		} else if(comp > 0) {
			high = mid - 1;
	}
	else {
			return mid;
		}
	}
	return (-1 * (low + 1));
}


function transferSelectedOptions(source, target, keepAlphabeticalOrder, removeFromSource, transferAll) {
    var sourceOptions = source.options;
    var targetOptions = target.options;

    var sourceLength = sourceOptions.length;
    var targetLength = targetOptions.length;

    for(var i = 0; i < sourceOptions.length; i++) {
        var sourceOption = sourceOptions[i];
        if(!transferAll && !sourceOption.selected) {
            continue;
        }
        var insertionIndex = binarySearch(targetOptions, sourceOption.text, compareAllIgnoreCase);
        //already there
        if(insertionIndex > -1) {
            continue;
        }
        insertionIndex = (insertionIndex * -1) - 1;
        var targetOption = new Option(sourceOption.text, sourceOption.value);
        //add at end
        if(!keepAlphabeticalOrder) {
            targetOptions[targetOptions.length] = targetOption;
        } else {
            targetOptions.add(targetOption,insertionIndex);
        }
        if(removeFromSource) {
            source.remove(i);
            i--;
        }
    }
	}

function copyOption(opt) {
    return new Option(opt.text, opt.value, opt.defaultSelected, opt.selected);
}

function swapOptions(select, index1, index2) {
    var temp1 = copyOption(select.options[index1]);
    var temp2 = copyOption(select.options[index2]);
    select.options[index1] = temp2;
    select.options[index2] = temp1;
}

function moveSelectedOptionsUp(select) {
    var options = select.options;
    var length = options.length;

	for(var i = 1; i < length; i++) {
		if(options[i].selected && !options[i-1].selected) {
            swapOptions(select, i, i-1);
            options[i-1].selected = true;
        }
    }
}

function moveSelectedOptionsDown(select) {
    var options = select.options;
    var length = options.length;

	for(var i = length - 2; i >=0; i--) {
		if(options[i].selected && !options[i+1].selected) {
            swapOptions(select, i, i+1);
            options[i+1].selected = true;
        }
    }
}

function initSingleSelect(select, invalidValues) {
    if(typeof invalidValues == "string") {
        invalidValues = invalidValues.split(',');
    }
    select.prevSelectedIndex = select.selectedIndex;
    select.invalidValues = invalidValues;
}
  
function bounceBackSingleSelectIfInvalid(select) {
    if(!select.prevSelectedIndex || select.selectedIndex < 0) {
        return false;
    }
    var selectedValue = select.options[select.selectedIndex].value;
    for(var i = 0; i < select.invalidValues.length; i++) {
        if(select.invalidValues[i] == selectedValue) {
            select.selectedIndex = select.prevSelectedIndex;
            return true;
        }
    }
    select.prevSelectedIndex = select.selectedIndex;
    return false;
}

function hide(element) {
	element.style.display = 'none';
}

function show(element) {
	element.style.display = '';
}

function trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function setText(element, text) {
	element.innerHTML = text;
}

// handles a firefox bug
function getNextSibling(startElem){
	nextElem = startElem.nextSibling;
	while(nextElem!=null && nextElem.nodeType!=1) {
		nextElem = nextElem.nextSibling;
	}
	return nextElem;
}

function toggleSection(el) {
	var target = getNextSibling(el.parentNode);
	var toggler = el.firstChild;
	if(target.style.display != 'block') {
		target.style.display = 'block';
		toggler.src = 'images/open_arrow.gif';
	}
	else {
		target.style.display = 'none';
		toggler.src = 'images/closed_arrow.gif';
	}
	return false;
}

function setFocus(elementId) {
	document.getElementById(elementId).focus();
}