﻿String.prototype.trim = function ()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
};

function ParseInt(str)
{
	var n = parseInt(str, 10);
	if (n == str) return n;
	else return NaN;
}

function IsTrueForAll(lst, func)
{
	var len = lst.length;
	for (var i = 0; i < len; ++i)
	{
		var elem = lst[i];
		if (!func(elem)) return false;
	}
	return true;
}

function DoForAll(lst, func)
{
	var len = lst.length;
	for (var i = 0; i < len; ++i)
	{
		var elem = lst[i];
		func(elem);
	}
}

function HasAttribute(elem, attr)
{
	if (elem.attributes == null) return false;
	if (elem.attributes[attr] == null) return false;
	return true;
}

function GetAttributeValue(elem, attr)
{
	return (HasAttribute(elem, attr) ? elem.attributes[attr].value : null);
}

function SetAttributeValue(elem, attr, value)
{
	elem.setAttribute(attr, value);
}

function FindRecursive(base, func, stoptag)
{
	if (stoptag == null) stoptag = "";
	var lst = new Array();
	FindRecursive_Internal(lst, base, func, stoptag.toLowerCase());
	return lst;
}

function FindRecursive_Internal(lst, base, func, stoptag)
{
	var len = base.childNodes.length;
	for (var i = 0; i < len; ++i)
	{
		var elem = base.childNodes[i];
		if (func(elem)) lst.push(elem);
		if (elem.tagName && elem.tagName.toLowerCase() == stoptag) continue;
		FindRecursive_Internal(lst, elem, func, stoptag);
	}
}

function GetTextContent(elem)
{
	return (elem.textContent ? elem.textContent : elem.innerText);
}

function SetTextContent(elem, text)
{
	if (elem.textContent)
	{
		elem.textContent = text;
	}
	else
	{
		elem.innerText = text;
	}
}

function GetNextSibling(elem, tag, forward)
{
	tag = tag.toLowerCase();
	while ((elem = (forward ? elem.nextSibling : elem.previousSibling)) != null)
	{
		if (elem.tagName && elem.tagName.toLowerCase() == tag) return elem;
	}
	return null;
}

//
// Denne metoden lager gjennomsiktige DIV-elementer over alle disabla knapper på sida.
// Dette gjør det mulig å hente ut musposisjonen skikkelig, noe som ikke lar seg gjøre over disabla knapper.
// Kopierer eventene OnMouseOut og OnMouseMove for at ToolTips som er satt på knappen skal fungere.
//
function CreateDisabledButtonOverlays()
{
	var lst = FindRecursive(document.body, function (e) { return (e.tagName && e.tagName.toLowerCase() == "input" && e.disabled == true); });
	for (var i = 0; i < lst.length; ++i)
	{
		var elem = lst[i];
		if (elem.type != "button" && elem.type != "submit" && elem.type != "reset") continue;

		var div = document.createElement("div");
		div.id = elem.id + "_overlay";
		div.style.width = elem.offsetWidth + "px";
		div.style.height = elem.offsetHeight + "px";
		div.className = "DisabledButtonOverlay";

		div.onmouseout = elem.onmouseout;
		div.onmousemove = elem.onmousemove;

		elem.parentNode.insertBefore(div, elem);
	}
}

function keepImageSizeInBounds(imageId, maxBounds)
{
    /// <summary>
    /// Resizes an image if the width or height is greater than specified max bounds.
    /// Hides the image while it is loading, and displays it again after it is loaded and resized.
    /// </summary>
    /// <param name="imageId">ClientID of the Image control as string</param>
    /// <param name="maxBounds">Max size in pixels of the longest image side (width or height)</param>

    var originalImg = document.getElementById(imageId);
    originalImg.style["display"] = "none"; // do not display image while it is loading

    var workImg = new Image();
    workImg.onload = function ()
    {
    	var ratio;
    	originalImg.style["display"] = ""; // display image again

    	if (workImg.width >= maxBounds) // if width is longer than height
    	{
    		maxBounds = maxBounds > workImg.width ? workImg.width : maxBounds;
    		ratio = maxBounds / workImg.width;
    		originalImg.width = maxBounds;
    		originalImg.height = workImg.height * ratio;
    	}
    	else // if height is longer than width
    	{
    		maxBounds = maxBounds > workImg.height ? workImg.height : maxBounds;
    		ratio = maxBounds / workImg.height;
    		originalImg.width = workImg.width * ratio;
    		originalImg.height = maxBounds;
    	}
    };

    // start loading image
    workImg.src = originalImg.src;
}
