﻿function grayOut(makeVisible, options)
// makeVisible - true to gray out screen, false to ungray.
// options are optional.  This is a JSON object with the following (optional and can appear in any order) properties:
//	opacity		0-100		Default = 70, lower number = less grayout, hight = more of a blackout.
//	zindex		number		Default = 50, HTML elements with a higher zindex appear on top of the gray out.
//	bgcolour	#rrggbb		Default = #000000, standard RGB hex colour code.
// grayOut(true, {'opacity':'70', 'bgcolour':'#000000', 'zindex':'50'});
{
	options = options || {};
	var zindex = options.zindex || 50;
	var opacity = options.opacity || 70;
	var opaque = opacity / 100;
	var bgcolour = options.bgcolour || "#000000";
	var grayPane = document.getElementById("grayOutLayer");
	if (!grayPane)
	{
	    grayPane = document.createElement("div");
	    grayPane.style.position = "fixed";
	    grayPane.style.top = "0px";
	    grayPane.style.left = "0px";
	    grayPane.style.overflow = "hidden";
	    grayPane.style.width = "100%";
	    grayPane.style.height = "100%";
	    grayPane.style.display = "none";
	    grayPane.id = "grayOutLayer";
	    document.body.appendChild(grayPane);
	}
	if (makeVisible)
	{
	    grayPane.style.opacity = opaque;
	    grayPane.style.MozOpacity = opaque;
	    grayPane.style.filter = 'alpha(opacity=' + opacity + ')';
	    grayPane.style.zIndex = zindex;
	    grayPane.style.backgroundColor = bgcolour;
	    //grayPane.style.width = screen.width + "px";
	    //grayPane.style.height = screen.height + "px";
	    grayPane.style.display = "block";
	}
	else
	{
	    grayPane.style.display = "none";
	}
}


function schedule(objectID, functionCall, iteration)
// schedule("window", myFunc);
{
	if (iteration == null)
	{
		iteration = 0;
	}
	
	if (objectID == "window")
	{
		var oldonload = window.onload;
		if (typeof window.onload != "function")
		{
			window.onload = functionCall;
		}
		else
		{
			window.onload = function()
			{
				oldonload();
				functionCall();
			}
		}
	}
	else if (document.getElementById(objectID))
	{
		functionCall();
	}
	else if (iteration < 300)
	{
		setTimeout(function(){schedule(objectID, functionCall, iteration + 1)}, 10);
	}
	
	return true;
}


function hasClass(item, testClassName)
{
	var itemClassName = item.className;
	if (itemClassName == testClassName) return true;
	var itemLength = itemClassName.length;
	var testLength = testClassName.length;
	var index = -1;
	while (true)
	{
		index = itemClassName.indexOf(testClassName, index + 1);
		if (index == -1) return false;
		if (index == 0 || itemClassName.charAt(index - 1) == " ")
		{
			var endIndex = index + testLength;
			if (endIndex == itemLength || itemClassName.charAt(endIndex) == " ") return true;
		}
	}
	return false;
}


function hasSubmenu(item)
{
	for (var index = 0; index < item.childNodes.length; ++index)
	{
		var child = item.childNodes[index];
		if (child.tagName && child.tagName == "UL") return true;
	}
	return false;
}


function setVisibility(itemList, itemVisibility)
{
	for (var index = 0; index < itemList.length; ++index)
	{
		var item = itemList[index];
		item.style.visibility = itemVisibility;
	}
}


function setDropDown(dropDown, value, startIndex)
{
	var index;
	for (index = startIndex; index < dropDown.options.length; index++)
	{
		if (dropDown.options[index].value == value)
		{
			dropDown.selectedIndex = index;
			break;
		}
	}
}

function setDropDownWithSubstr(dropDown, length, value, startIndex)
{
	var index;
	for (index = startIndex; index < dropDown.options.length; index++)
	{
		if (dropDown.options[index].value.substr(0,length) == value)
		{
			dropDown.selectedIndex = index;
			break;
		}
	}
}


function openMail(name, domain)
{
    if (domain == null || domain == "") domain = "softed.com";
    window.location.href= "mail" + "to" + ":" + name + "@" + domain;
}


function setStyleDisplayOfParent(list, value)
{
	for (var index = 0; index < list.length; index++)
		list[index].parentNode.style.display = value;
}


function cloneNode(original, cloneChildren, nameIndex)
{
    var newElement;
    if (original.nodeName == "INPUT" && (original.type == "radio" || original.type == "checkbox") && nameIndex != null)
    {
        var newName = original.name.substr(0, original.name.length - 1) + nameIndex;
        try
        {
            // Works in IE but crashes for others
            newElement = document.createElement("<INPUT TYPE='" + original.type + "' NAME='" + newName + "'></INPUT>");
            newElement.id = original.id;
            if (original.type == "radio") newElement.value = original.value;
            newElement.setAttribute("cloneact", original.attributes["cloneact"]);
            newElement.setAttribute("clonevalue", original.attributes["clonevalue"]);
            newElement.setAttribute("cloneret", original.attributes["cloneret"]);
            newElement.setAttribute("cloneloop", original.attributes["cloneloop"]);
        }
        catch(e)
        {
            // Works in IE, but doesn't allow name to be changed
            newElement = original.cloneNode(cloneChildren);
            newElement.name = newName;
        }
    }
    else
    {
        newElement = original.cloneNode(cloneChildren);
    }
    if (nameIndex != null)
    {
        if (original.id != null && original.id.length > 1 && original.id.charAt(original.id.length - 1) == '0')
            newElement.id = original.id.substr(0, original.id.length - 1) + nameIndex;
        if (original.nodeName == "LABEL")
        {
            newElement.htmlFor = original.htmlFor.substr(0, original.htmlFor.length - 1) + nameIndex;
        }
    }
    return newElement;
}


function cloneElement(original, cloneAct, target, nameIndex, insertBeforeControl)
{
    var returnElement = null;
	var newElement = cloneNode(original, cloneAct == "all", nameIndex);
	if (insertBeforeControl == null)
		target.appendChild(newElement);
	else target.insertBefore(newElement, insertBeforeControl);
	if (cloneAct == "drill")
	{
		var newReturnElement = cloneTree(original, newElement, nameIndex, null);
		if (newReturnElement != null) returnElement = newReturnElement;
	}
	var cloneValue = original.attributes == null ? null : original.attributes["clonevalue"];
	if (cloneValue != null)
	{
		if (original.nodeName == "INPUT" && (original.type == "checkbox" || original.type == "radio"))
		{
			newElement.checked = cloneValue.nodeValue == "true";
		}
		else newElement.value = cloneValue.nodeValue;
	}
	else // cloneValue == null
	{
		if (original.nodeName == "SELECT")
		{
			newElement.selectedIndex = original.selectedIndex;
		}
	}
	var cloneRet = original.attributes == null ? null : original.attributes["cloneret"];
	if (cloneRet != null && cloneRet.nodeValue == "yes") returnElement = newElement;
	return returnElement;
}

function cloneTree(parent, target, nameIndex, insertBeforeControl)
{
	var returnChild = null;
	var index;
	for (index = 0; index < parent.childNodes.length; index++)
	{
		var child = parent.childNodes[index];
		var cloneAct = child.attributes == null ? null : child.attributes["cloneact"];
		if (cloneAct != null && cloneAct.nodeValue != "none")
		{
			var newReturnChild = cloneElement(child, cloneAct.nodeValue, target, nameIndex, insertBeforeControl);
			if (newReturnChild != null) returnChild = newReturnChild;
		}
		var cloneLoop = child.attributes == null ? null : child.attributes["cloneloop"];
		if (cloneLoop != null && cloneLoop.nodeValue == "stop") break;
	}
	return returnChild;
}


function setText(placeholder, value)
{
	placeholder.removeChild(placeholder.childNodes[0]);
	var followingNode = placeholder.childNodes[0];
	if (followingNode == null)
	{
		placeholder.appendChild(document.createTextNode(value));
	}
	else
	{
		placeholder.insertBefore(document.createTextNode(value), followingNode);
	}
}


function emptyDropDown(dropDown, startIndex)
{
	while (dropDown.options.length > startIndex)
	{
		dropDown.remove(startIndex);
	}
}


function getSelectedText(select)
{
	var selectedIndex = select.selectedIndex;
	if (selectedIndex > 0) return select.childNodes[select.selectedIndex].text;
	else return "";
}


function cancelSelectMouseWheel()
{
    var allSelects = document.getElementsByTagName('SELECT');
    for (var index = 0; index < allSelects.length; ++index)
    {
        var sel = allSelects(index);
        sel.onmousewheel = function() { event.cancelBubble = true; return false; };
    }
}


function getMenuHTML(items, ulClass, liClass, itemSeparator)
// items - a hierarchical array of arrays containing menu items.
//    Each inner array defines a menu item in the following format:
//        item[0] = menu text
//        item[1] = link URL
//        item[2] = item specific class
//        item[3] = item ID
//        item[4] = ulClass
//        item[5] = liClass
//        item[6] = separator
//        item[7] = array of child menu items in the same format as items
// ulClass - the class to apply to the <ul> tag.
// liClass - the class to apply to the <li> tags.
// itemSeparator - the HTML to place between successive list items.
{
    var menuHTML = "<ul";
    if (ulClass != null) menuHTML += " class='" + ulClass + "'";
    menuHTML += ">";
    for (var iItem = 0; iItem < items.length; ++iItem)
    {
        var thisItem = items[iItem];
        if (iItem != 0 && itemSeparator != null) menuHTML += itemSeparator;
        menuHTML += "<li";
        var setLIClass = (liClass == null) ? "" : liClass;
        if (thisItem.length >= 3 && thisItem[2] != null) setLIClass += thisItem[2];
        if (setLIClass != "") menuHTML += " class='" + setLIClass + "'";
        if (thisItem.length >= 4 && thisItem[3] != null) menuHTML += " id='" + thisItem[3] + "'";
        menuHTML += "><a href='" + thisItem[1] + "'>" + thisItem[0] + "</a>";
        if (thisItem.length == 8 && thisItem[7] != null) menuHTML += getMenuHTML(thisItem[7], thisItem[4], thisItem[5], thisItem[6]);
        menuHTML += "</li>";
    }
    menuHTML += "</ul>";
    return menuHTML;
}

