﻿function PNGFix() {
	//return;
	var blankSrc = "Images/1pxtransparent.gif";
	var allTags = document.getElementsByTagName("*");
	if (document.documentElement && typeof document.documentElement.style.maxHeight != "undefined") {
		return "window.XmlHttpRequest does not exist in IE6 but does in IE7 and most other browsers";
	}
	for (var i = 0; i < allTags.length; i++) {
		var current = allTags[i];
		if (current.tagName && (current.tagName.toLowerCase() == "img" || ( current.tagName.toLowerCase() == "input" && current.type && current.type.toLowerCase() == "image"))) {
			if (current.filters && current.src.substr(current.src.length - 3, 3).toLowerCase() == "png") {
				var realSrc = current.src;
				var realWidth = current.offsetWidth;
				var realHeight = current.offsetHeight;
				current.src = blankSrc;

				current.style.width = realWidth;
				current.style.width = realHeight;
				current.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + realSrc + "', sizingMethod='image')";
			}
		}
		else if (current.filters && current.tagName.toLowerCase() != "body" && current.currentStyle["backgroundImage"] != "none" && current.className != "horizontalMenu") {
			// Skips body because if backgroundImage gets filtered there ALL links stop working.
			var image = current.currentStyle["backgroundImage"]
			var realSrc = image.substr(image.indexOf("url(\"") + 5, image.length - 7);
			if (realSrc.substr(realSrc.length - 3, 3).toLowerCase() == "png") {
				var method = (current.currentStyle["backgroundRepeat"] == "no-repeat") ? "crop" : "scale";
				if (method == "scale") continue;
				current.style.backgroundImage = "none";
				var oldHeight = current.offsetHeight;
				var oldWidth = current.offsetWidth;
				
				current.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + realSrc + "', sizingMethod='" + method + "')";
				current.style.width = oldWidth;
				current.style.position = "relative";
			}
		}
	}
}
addOnload(PNGFix);
function oGetNodes(stringTagName, nodeTargetNode, intLimit, stringClassName, stringIds) {
	this.sTagName = stringTagName;
	this.nTargetNode = nodeTargetNode;
	this.iLimit = intLimit;
	this.sClassName = stringClassName;
	this.sIds = stringIds;
}
function mGetNodes(new_oGetNodes) {
	// This function is made to return all (?with specified tag) elements from (?specified) an html node
	var o = new_oGetNodes;
	o.sTagName = (o.sTagName) ? o.sTagName : "*";
	o.nTargetNode = (o.nTargetNode) ? o.nTargetNode : document.body;
	o.iLimit = (o.iLimit) ? o.iLimit : 0;

	var nodeList = [];
	var tagList = o.nTargetNode.getElementsByTagName(o.sTagName);
	if (!o.sClassName && !o.sIds) { return tagList; }

	if (o.sClassName) {
		for (i = 0; i < tagList.length; i++) {
			var currentNode = tagList[i];
			var pattern = new RegExp('(^|\\\\s)' + o.sClassName + '(\\\\s|$)');
			if (currentNode && currentNode.className && currentNode.className.search(pattern) != -1) {
				nodeList[nodeList.length] = currentNode;
				if (o.iLimit && nodeList.length >= o.iLimit) { break; }
			}
		}
	}
	if (o.sIds) {
		var splitIds = o.sIds.split(",");
		for (i = 0; i < splitIds.length; i++) {
			var currentNode = document.getElementById(splitIds[i]);
			nodeList[nodeList.length] = currentElm;
			if (iLimit && nodeList.length >= iLimit) { break; }
		}
	}
	return nodeList;
}
function oTableEnhance(bAlternatingItems, bFirstLastRow, bRowClick, bRowHover) {
	this.bAlternatingItems = bAlternatingItems;
	this.bFirstLastRow = bFirstLastRow;
	this.bRowClick = bRowClick;
	this.bRowHover = bRowHover;
}
function mTableEnhance(new_oTableEnhance) {
	var o = new_oTableEnhance;
	// Tanken var att göra en function som snyggar till och förbättrar vid visning av tabulärdata.
	if (!mGetNodes) {
		alert("Error @ function mTableEnhance: function mGetNodes is not specified");
		return false;
	}
	var allTables = mGetNodes(new oGetNodes("table", 0, 0, 0, 0));
	if (o.bAlternatingItems) {
		for (var i = 0; i < allTables.length; i++) {
			var currentTable = allTables[i];
			var rowCollection = mGetNodes(new oGetNodes("tr", currentTable));
			for (var j = 0; j < rowCollection.length; j++) {
				var currentRow = rowCollection[j];
				if (o.bFirstLastRow && j == 0) { currentRow.className = "firstItem"; }
				else if (o.bFirstLastRow && j == rowCollection.length - 1) { currentRow.className = "lastItem"; }
				else { currentRow.className = (j % 2) ? "alternatingItem" : "item"; }
				if (o.bRowHover) { currentRow.onmouseover = mRowHover; }
				if (o.bRowClick) { currentRow.onmouseout = mRowClick; }
			}

		}
	}
}
function mRowHover(e) {
	if (!e) { e = window.event; }
	var nTarget;
	if (e.target) { nTarget = e.target; }
	nTarget.className = (nTarget.className) ? nTarget.className + " hover" : "hover";
}
function mRowClick(e) {

}
function showNode(node, bool) {
	if (typeof node == "string") {
		node = document.getElementById(node);
	}
	else if (typeof node == "object" && node.length) {
		for (var i in node) {
			var definedNode = node[i];
			definedNode.style.display = (bool) ? "block" : "none";
		}
		return;
	}
	node.style.display = (bool) ? "block" : "none";
}
function visibleNode(node, bool) {
    if (typeof node == "string") {
        node = document.getElementById(node);
    }
    else if (typeof node == "object" && node.length) {
        for (var i in node) {
            var definedNode = node[i];
            definedNode.style.visibility = (bool) ? "visible" : "hidden";
        }
        return;
    }
    node.style.visibility = (bool) ? "visible" : "hidden";
}

// Set focus to the given form field, or to the first
// visible and enabled form field on the page.
function formFocus(objectID) {
    if (objectID != '') {
        element = document.getElementById(objectID);
        if (element && !element.disabled && !element.readonly && element.style.display != 'none' && element.style.visibility != 'hidden') {
            element.focus();
        }
        else {
            formFocus('');
        }
    }
    else if (document.forms[0]) {
        for (i = 0; i < document.forms[0].elements.length; i++) {
            element = document.forms[0].elements[i];
            if (element.type == 'text' || element.type == 'textarea' || element.type == 'password') {
                if (!element.disabled && !element.readonly && element.style.display != 'none' && element.style.visibility != 'hidden') {
                    element.focus();
                }
            }
        }
    }
}

// Strip the leading and trailing spaces from a string.
function Trim(text) {
    return text.replace(/^\s*|\s*$/g, '');
}

// Clear a text box, unless the value is not the default value.
function clearTextBox(textBox, text) {
	if (textBox.value == text) {
		textBox.value = '';
		if (textBox.className == "password") {
			var newBox = textBox.cloneNode(true);
			newBox.type = "password";
			textBox.parentNode.replaceChild(newBox, textBox);
			window.setTimeout(function(){newBox.focus()},10);
		}
	}
}

// Fill a text box with a default value, unless the value is changed.
function fillTextBox(textBox, text) {
	if (textBox.value == '') {
		textBox.value = text;
	}
}

// Show help popup window.
function showHelp(templateName, pageType) {
    window.open('Help/ShowHelp.aspx?TemplateName=' + templateName + '&PageType=' + pageType, 'ComitWebUIHelp', 'toolbar=yes, directories=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=400, height=500, top=' + (screen.height - 500) / 2 + ', left=' + (screen.width - 400) / 2).focus();
}

// Show image popup window.
function showImage(imageID) {
    window.open('Files/ShowImage.aspx?ImageID=' + imageID, 'ComitWebUIImage', 'scrollbars=no, resizable=no, status=no, toolbars=no, width=200, height=200').focus();
}

// Sets the position of a control.
function setPosition(control, absoluteTop, absoluteLeft) {
    if (control) {
        if (control.offsetParent) {
            control.style.top = absoluteTop - findPosY(control.offsetParent);
            control.style.left = absoluteLeft - findPosX(control.offsetParent);
        }
        else {
            control.style.top = absoluteTop;
            control.style.left = absoluteLeft;
        }
        if (control.style.top < 0) {
            control.style.top = 0;
        }
        if (control.style.left < 0) {
            control.style.left = 0;
        }
    }
}

function findPosX(object) {
    var curleft = 0;
    if (object.offsetParent) {
        while (object.offsetParent) {
            curleft += object.offsetLeft;
            object = object.offsetParent;
        }
    }
    else if (object.x) {
        curleft += object.x;
    }
    return curleft;
}

function findPosY(object) {
    var curtop = 0;
    if (object.offsetParent) {
        while (object.offsetParent) {
            curtop += object.offsetTop;
            object = object.offsetParent;
        }
    }
    else if (object.y) {
        curtop += object.y;
    }
    return curtop;
}
function getMousePos(e) {
    // Returnerar ett objekt {y,x}
    var posY = 0;
    var posX = 0;
    if (!e) { e = window.event; }
    if (e.pageY) {
        posY = e.pageY;
        posX = e.pageX;
    }
    else if (e.clientY) {
        posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    }
    var retObject = { "y": posY, "x": posX };
    return retObject;
}
function extScreen() {
    // Returnerar viewport och scroll
    var width = 0;
    var height = 0;
    if (window.innerHeight) {
        height = window.innerHeight;
        width = window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        height = document.documentElement.clientHeight;
        width = document.documentElement.clientHeight;
    }
    else if (document.body.clientWidth) {
        height = document.body.clientHeight;
        width = document.body.clientHeight;
    }
    else { alert("Browser does not support extScreen()"); }
    var scrollTop = (document.body.scrollTop > 0) ? document.body.scrollTop : (document.body.parentNode && document.body.parentNode.scrollTop > 0) ? document.body.parentNode.scrollTop : 0;
    var scrollLeft = (document.body.scrollLeft > 0) ? document.body.scrollLeft : (document.body.parentNode && document.body.parentNode.scrollLeft > 0) ? document.body.parentNode.scrollLeft : 0;
    var obj = { "height": height, "width": width, "scrollTop": scrollTop, "scrollLeft": scrollLeft };
    return obj;
}
function getElmByClass(stringClassName, nodeTarget, stringTargetTag, boolReturnOnlyOne) {
    // Returnerar en array med alla element med klassnamn, alternativt bara första som stöts på.
    nodeTarget = (nodeTarget) ? nodeTarget : document.body;
    stringTargetTag = (stringTargetTag) ? stringTargetTag : '*';
    if (!document.getElementsByTagName) { return false; }
    var arrClassNodes = [];
    var arrTagNodes = document.getElementsByTagName(stringTargetTag);
    for (var i in arrTagNodes) {
        var tagNode = arrTagNodes[i];
        if (tagNode && tagNode.className && tagNode.className == stringClassName) { arrClassNodes[arrClassNodes.length] = tagNode; }
    }
    if (boolReturnOnlyOne) { return arrClassNodes[0]; }
    return arrClassNodes;
}
function addOnload(func) {
    var old = window.onload;
    if (typeof window.onload != "function") window.onload = func;
    else {
        window.onload = function() {
            if (old) { old(); }
            func();
        }
    }
}