// $
function $(id) {
    return document.getElementById(id);
}

// checkForm(cform)
function checkForm(cform) {

    if ($(ClientID.name).value.length == 0)
    {
        alert("Du gl\u00F6mde fylla i ditt namn.");
        $(ClientID.name).focus();
        return false;    
    }
    if ($(ClientID.email).value.length == 0)
    {
        alert("Du gl\u00F6mde ange e-postadress.");
        $(ClientID.email).focus();
        return false;    
    }
    if ($(ClientID.msg).value.length == 0)
    {
        alert("Du gl\u00F6mde ditt meddelande.");
        $(ClientID.msg).focus();
        return false;    
    }                     
}


// addEvent() v1.0
function addEvent(obj, evt, fnc, useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
}

// getElementsByClassName() v1.0
document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
	return retVal;
}

/*	
	mmAddEventHandler() v1.0
	Author: Mammon Media (c) 2006
	Description: Add events to elements with specific class names
	takes an array of items and adds fn to all
	Usage: mmAddEventHandler("classname", "event", function)
	run in onload event
	Depends on: getElementsByClassName()
*/
function mmAddEventHandler(cls, ev, fn) {

	var elm = document.getElementsByClassName(cls);
	
	if(elm.length > 0) {
		for(var i = 0; i < elm.length; i++) {
			if(elm[i].addEventListener) {
				elm[i].addEventListener(ev, fn, false);
			} else if(elm[i].attachEvent) {
				elm[i].attachEvent("on"+ev, fn);
			}
		}
	}
}

mmMenu = {
	
	subColor: "#000",

	Activate: function() {
		var filename = document.location.href;
		filename = filename.substring(filename.lastIndexOf("/")+1, filename.length).toLowerCase();
		
		if (!filename) filename = "default.aspx";
		
	    var id = filename.substring(0, filename.lastIndexOf(".")).toLowerCase();
	    id = id.substring(0, (id.indexOf("_") > 0) ? id.indexOf("_") : id.length);
	
	    var item = document.getElementById(id);
	
	    if(item)
		    item.firstChild.className += " active";

	},

	ActivateSub: function() {
		
		// Get container
		var container = document.getElementById("subnav");

		// check if container element exists
		if (container) {			
			
			// Get all LI elements in container
			var linklist = container.getElementsByTagName("li");
			
			// Get filename and remove everything before last "/" + set everything to lowercase
			var filename = document.location.href;
			filename = filename.substring(filename.lastIndexOf("/")+1, filename.length).toLowerCase();

			for (var i = 0; i<linklist.length; i++) {
				var linkobj = linklist[i].firstChild;
				
				
				if(linkobj.href) {
					var linkname = linkobj.href.substring(linkobj.href.lastIndexOf("/")+1, linkobj.href.length).toLowerCase();
				
					// Set color to subColor if link href = current page name	
					if(linkname == filename) linkobj.style.color = mmMenu.subColor;
				}
			}
		}
	}
}

function makeActiveTab(e) {
	
	var elm = (e.target) ? e.target : window.event.srcElement;
	
	var tabs = document.getElementById("newscontainer").getElementsByTagName("li");
	
	for (i=0; i<tabs.length; i++) {
		
		if(tabs[i].className.indexOf("active") > -1) {
			tabs[i].className = "inactive";			
			document.getElementById(tabs[i].id + "content").style.display = "none";
		
		}
		
	}
	
	elm.parentNode.className = "active";
	document.getElementById(elm.parentNode.id + "content").style.display = "block";
	
	return false;
	
}

// image pop-up
function imgPopup(url, title)
{
    // Script Source: CodeLifter.com
    // Copyright 2003

    // Set the horizontal and vertical position for the popup
    PositionX = 100;
    PositionY = 100;

    // Set these value approximately 20 pixels greater than the
    // size of the largest image to be used (needed for Netscape)
    defaultWidth  = 500;
    defaultHeight = 500;

    // Set autoclose true to have the window close automatically
    // Set autoclose false to allow multiple popup windows

    var AutoClose = true;

    // ================================
    if (parseInt(navigator.appVersion.charAt(0))>=4){
    var isNN=(navigator.appName=="Netscape")?1:0;
    var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
    var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
    var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
    function popImage(imageURL,imageTitle)
    {
        if (isNN){imgWin=window.open('about:blank','',optNN);}
        if (isIE){imgWin=window.open('about:blank','',optIE);}
        with (imgWin.document){
        writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
        writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
        writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
        writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(300,300);');
        writeln('width=300-(document.body.clientWidth-document.images[0].width);');
        writeln('height=300-(document.body.clientHeight-document.images[0].height);');
        writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
        writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
        writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
        if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
        else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
        writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
        close();		
    }}
    
    
    popImage(url, title);

}

function pageInit() {

	// Remove background flicker in IE
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
		
}


// Attach events
addEvent(window, "load", pageInit);
addEvent(window, "load", mmMenu.Activate);

addEvent(window, "load", mmMenu.ActivateSub);