//check browser
var w3c = false;
var ie = false;
var ns = false;
var sa = false;
if (document.getElementById) {
	var w3c = true;
}
if (document.all) {
	var ie = true;
}
if (document.layers) {
	var ns = true;
}
if (window.navigator.userAgent.indexOf('Safari') != -1) {
    var sa = true;
}
if (w3c==false && ie==false && ns==false) {
	alert('too old (or bad) browser. get a newer one at \n\n - www.mozilla.org \n - www.microsoft.com \n - www.apple.com');
	location.href='http://www.mozilla.org';
}

//show a hidden layer
function showLayer(strName){
	if (w3c) document.getElementById(strName).style.visibility = "visible";
    else if (ie) eval(strName).style.visibility = "visible";
 	else if (ns) document.layers[strName].visibility = "show";
}

//hide a shown layer
function hideLayer(strName){
	if (w3c) document.getElementById(strName).style.visibility = "hidden";
    else if (ie) eval(strName).style.visibility = "hidden";
 	else if (ns) document.layers[strName].visibility = "hide";
}

//change display for a layer
function displayLayer(strName,dispType){
	document.getElementById(strName).style.display = dispType;
}

//open a layer with an image in it
function selectImage(layerName,img){
	showLayer(layerName);
	innerHtmlCode = "<a href=javascript:hideLayer('" + layerName + "');><img src=" + img + " border='0'></a>";
	
	if (w3c){
		document.getElementById(layerName).innerHTML=innerHtmlCode;
	}
	else if (ie){
		window.eval(layerName).innerHTML=innerHtmlCode;
	}
	else if (ns){
		document.layers[layerName].document.open()
		document.layers[layerName].document.write(innerHtmlCode)
		document.layers[layerName].document.close()
	}
}

//make it impossible to write anything but numbers in a form-element
function onlyNumbers(character, warning){
	var myAsciiCode = (navigator.appName.toLowerCase().indexOf("netscape") == 0)? character.which : character.keyCode;
	
	if(myAsciiCode > 47 && myAsciiCode < 58 || myAsciiCode == 8){
		if (ns){
		return true;
	}
	}else{
		alert (warning);
		return false;
	}
}

//show a text onMouseOver that tags along with the mouse
var layer = null;
var isOver = false;
 
var xPos;
var yPos;
 
function initBubbles() {
	if (ns||w3c && ie!=true) {
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = setValue;
	}
}

function setValue(evt) {
	if (ns==true && w3c==false) {
		xPos = evt.pageX;
		yPos = evt.pageY;
		//alert("X:" + xPos + "   " + "Y:" + yPos);
			if(isOver) { 
				document.layers["nummerDIV" + layer].visibility = "show";
				document.layers["nummerDIV" + layer].top = yPos -20;
				document.layers["nummerDIV" + layer].left = xPos +15;
			} else if(!isOver) {
				if(layer != null) {
					document.layers["nummerDIV" + layer].visibility = "hide";
				}
			}
	}
	else if (w3c==true && ie!=true) {
		xPos = evt.pageX;
		yPos = evt.pageY;
		//alert("X:" + xPos + "   " + "Y:" + yPos);
		if(isOver) { 
			showLayer("nummerDIV" + layer);
			document.getElementById("nummerDIV" + layer).style.top = yPos -20;
			document.getElementById("nummerDIV" + layer).style.left = xPos +15;
		} else if(!isOver) {
			if(layer != null) {
				hideLayer("nummerDIV" + layer);
			}
		}
	}
}

function dialog(what, number) {
	if(what == "show") {
		isOver = true;
		layer = number;
	} else if(what == "hide") {
		isOver = false;
		layer = number;
	}

	if(isOver) {
		if(ns||w3c && ie!=true) {
			layer = number;
		}
		if(ie) {
			var xPos = event.x;
			var yPos = event.y;
		    showLayer("nummerDIV" + layer);
		    eval("nummerDIV" + layer).style.pixelTop = yPos -20 + document.body.scrollTop;
		    eval("nummerDIV" + layer).style.pixelLeft = xPos +15;
		}
	} else if(!isOver) {
		hideLayer("nummerDIV" + layer);
	}
}

document.onkeydown = KeyCheck;
function KeyCheck(e) {
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	if(KeyID==74){
		pressed_keys = KeyID;
	} else if(KeyID==79){
		pressed_keys = pressed_keys+""+KeyID;
	} else if(KeyID == 78){
		pressed_keys = pressed_keys+""+KeyID;
	} else if(KeyID == 75){
		pressed_keys = pressed_keys+""+KeyID;
	}
	if ((KeyID !=74) && (KeyID !=79) && (KeyID !=78) && (KeyID !=75)) {
		pressed_keys = "";
	}
	aux = new String(pressed_keys);
	if ((aux.indexOf('74797875') != -1)) {
		document.getElementById('jonkRulez').innerHTML = "<img src='img/head.gif' border='0' style='position:absolute;top:100px;left:100px;'/>";
		pressed_keys = "";
	}
}

//change a links href and linktext
function changeLink(href,linktext,linkid) {
	if (w3c) {
		if (href != "") {
			document.getElementById(linkid).href=href;
		}
		if (linktext != "") {
			document.getElementById(linkid).innerHTML=linktext;
		}
	}
	else if (ie) {
		if (href != "") {
			document.all[linkid].href=href;
		}
		if (linktext != "") {
			document.all[linkid].innerText=linktext;
		}
	}
	else if (ns) {
		window.status="function not supported in this browser.";
	}
}

//change the bgColor of anything
function changeBgColor(HexColor,linkid) {
	if (w3c) {
		document.getElementById(linkid).style.background=HexColor;
	}
}

//link anything
function linkIt(url,frameName) {
	if (frameName=="") {
		location.href = url;
	} else if (frameName=="new" || frameName=="_blank") {
		window.open(url)
	} else {
		temp = frameName + ".location.href = '" + url + "'";
		eval (temp);
	}
}

//open a new browser window
function openWin(theUrl,theName, theSettings) {
	window.open(theUrl,theName,theSettings); void(0);
}

//lock a form-element so that it can't be edited
function lockField(falt,formName){
	var temp = "document." + formName + "." + falt + ".blur()";
	eval (temp);
}

//invert checkbox-selection
function InvertAll(formName) {
	for (var i=0;i<document.forms[formName].elements.length;i++) {
		var e=document.forms[formName].elements[i];
		e.checked=!e.checked;
	}
}

//self-submitting select
function selectBrowse(frameName,formName,selectName) {
	var temp = "document." + formName + "." + selectName + ".options[document." + formName + "." + selectName + ".options.selectedIndex].value";
	if (eval (temp) != "do_nothing") {
		if (frameName=="") {
			location.href = eval (temp);
		} else if (frameName=="new") {
			window.open(eval (temp))
		} else {
			temp = frameName + ".location.href = " + temp;
			eval (temp);
		}
	}
}

//a running clock
var flasher = false
function updateTime() {
	var now = new Date()
	var theHour = now.getHours()
	var theMin = now.getMinutes()
	var theSec = now.getSeconds()
	var theTime = "" + ((theHour < 10) ? "0" : "") + theHour
		theTime += ((flasher) ? " " : ":")
	theTime += ((theMin < 10) ? "0" : "") + theMin
		theTime += ((flasher) ? " " : ":")
	theTime += ((theSec < 10) ? "0" : "") + theSec
	flasher = !flasher

	var theTime;

	if (w3c) {
		document.getElementById("showTime").innerHTML=theTime
		}
	else if (ns) {
		document.showTime.document.open()
		document.showTime.document.write("<pre>" + theTime + "</pre>")
		document.showTime.document.close()
		}
	else {
		window.showTime.innerText=theTime;
		}
	timerID = setTimeout("updateTime()",1000)
}

//swap image
function swapImage(imgName,imgSrc) {
	document.images[imgName].src=imgSrc;
}

//a layer that stays on the same y position in the window
function stayXY(layerName,yPos,xPos) {
	if (w3c) {
		document.getElementById(layerName).style.top = yPos + document.body.scrollTop;
		document.getElementById(layerName).style.left = xPos;
	}
	else if (ns) {
		document.layers[layerName].top = yPos;
		document.layers[layerName].left = xPos;
	}
}

//a function to select a random number from 0 to what is passed in
function randomNumber(no) {
	var number;
	number = (Math.floor(Math.random() * no));
	return number;
}

//submit a form bu pressing enter in ns as it works in w3c and ie
function submitEnter(character,formName) {
	if (ns && character.which == 13) {
		document.forms[formName].submit();
	}
}

//a function to clear an imput-field when focused, unless it's value differs from what is set as "value"
function clearField(theInput) {
	if (theInput.defaultValue==theInput.value) {
		theInput.value = "";
	}
}

//a function to load flashmovies. it also prevents ie from adding the ugly border
function CreateFlash(FlashVars,movie,width,height) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '">');
    document.write('<param name="movie" value="'+ movie +'" />');
    document.write('<param name="quality" value="high" />');
	document.write('<param name="menu" value="false" />');
	document.write('<param name="FlashVars" value="'+ FlashVars +'">');
	document.write('<param name="wmode" value="transparent">');
    document.write('<embed src="'+ movie +'" wmode="transparent" quality="high" menu="false" flashvars="'+ FlashVars +'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" />');
    document.write('</object>');
}

//a function to load objects. it also prevents ie from adding the ugly border
function CreateObject(Id,Class,Name,Namevalue,CLASSID,Scrollbars,ShowDetails,ShowBorders) {
	document.write('<object ID="'+Id+'" CLASS="'+Class+'" ');
	document.write('CLASSID="'+CLASSID+'"');
	document.write('>');
	document.write('<param name="Scrollbars" value="'+Scrollbars+'">');
	document.write('<param name="'+Name+'" value="'+Namevalue+'">');
	document.write('<param name="ShowDetails" value="'+ShowDetails+'"> ');
	document.write('<param name="ShowBorders" value="'+ShowBorders+'">');
	document.write('</object>');
}

//a function that stops the user from typing too much in a form element.
function countChars(formElement,noOfCars,printTarget,maxChars,text1,text2,text3) {
	//document.getElementById(printTarget).style.display = "inline";
	if (text1 == '') {
		text1 = "Meddelandet får ej överstiga ";
	}
	if (text2 == '') {
		text2 = " tecken.";
	}
	if (text3 == '') {
		text3 = " tecken kvar";
	}
	if (noOfCars > maxChars) {
		alert(text1 + maxChars + text2);
		document.getElementById(formElement).value = document.getElementById(formElement).value.substr(0, maxChars);
		document.getElementById(printTarget).innerHTML = 0 + text3;
	} else {
		document.getElementById(printTarget).innerHTML = maxChars - noOfCars + text3;
	}
			
}

//a function to set your cursor
function setCursor(id,c) {
	document.getElementById(id).style.cursor=c;
}

//change the css-class of an element
function changeClass(id,toClass) {
	document.getElementById(id).className=toClass;
}

//a function that returns querystings
function getQs(getArr,getValue) {
	thisUrl = "" + window.location;
	if (getValue == '') {
		getValue = false;
	} else {
		getValue = true;
	}
	if (thisUrl.indexOf('?') != -1) {
		thisQS = thisUrl.substr(thisUrl.indexOf('?')+1, thisUrl.length);
		thisQS = thisQS.split("&");
		if (getArr == -1) {
			return thisQS;
		} else {
			if (getValue == true) {
				thisQS = thisQS[getArr].split("=");
				return thisQS[1];
			} else {
				return thisQS[getArr];
			}
		}
	}
}

//a function that resizes a window
function resizExact(width,height) {
	//get someting to work with
	window.resizeTo(width,height);
	//set the height
	thePageHeight = document.body.clientHeight; //get the height of the window
	winBarHeight = height - thePageHeight; //get the height of the top and bottom bars of the window
	exactPageHeight = winBarHeight + height; //set the height of the window to the height and bars
	//set the width
	thePageWidth = document.body.clientWidth; //get the width of the window
	winBordersWidth = width - thePageWidth; //get the width of the left and right borders of the window
	exactPageWidth = winBordersWidth + width; //set the width of the window to the width and borders
	//resize to exact values
	window.resizeTo(exactPageWidth,exactPageHeight);
}

//a function that sets the height of an iframe
function setIframeHeight(changeValue,iframeId) {
	newHeight = eval('document.'+htmlType()+'.clientHeight') + changeValue; //calculate the new height
	newHeight = newHeight + '';	//make the new height a string and not an integer
	if (newHeight.indexOf("px") == -1) { //check if the value contains 'px'
		newHeight = newHeight + 'px'; //if it doesn't, add it
	}
	document.getElementById(iframeId).style.height = newHeight; //set the height of the iframe
}

//if document is html it returns body else (xhtml) it returns documentElement
function htmlType() {
	if (document.documentElement.clientHeight == 0) {
		return 'body'; //document is html
	} else {
		return 'documentElement'; //document is xhtml
	}
}
