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 ((navigator.userAgent).indexOf("Safari")!=-1) {
	var sa = true;
}
if (w3c==false && ie==false && ns==false && sa==false) {
	alert('Din webbläsare är för gammal eller för dålig för Favorite.nu. Ladda hem en nyare, företrädesvis Firefox.');
	top.location.href='http://www.mozilla.org';
}

function changeDisplay( elementId, setTo ) {
	if( w3c ) {
		var theElement = document.getElementById( elementId );
	} else if( ie ) {
		var theElement = document.all[ elementId ];
	} else if (ns) {
		var theElement = new Object();
	}
	if (theElement) { //check if the element exists
		if( theElement.style ) {
			theElement = theElement.style;
		}
		theElement.display = setTo;
	}
}

function changeItem(preText,iNum) {
	sItem = preText + iNum;
	if (document.getElementById(sItem).style.display == 'block'){
		hideItem(sItem);
		if (sItem.length == 4) { //closed a whole section
			deleteCookie(sItem);
		}
	} else {
		showItem(sItem);
		if (sItem.length == 4) { //opened a whole section
			setCookie(sItem, 'block');
		}
	}
}

function hideItem(sItem) {
	changeDisplay( sItem, 'none' );
}

function showItem(sItem) {
	changeDisplay( sItem, 'block' );
}

function changeLink(linkid,forceImg) {
	if (document.images['img'+linkid]) {
		var find = document.images['img'+linkid].src;
		sItem = linkid.split("expand");
		sItem = 'id' + sItem[1];
		if (find.indexOf("plus.gif") > 0 || forceImg == 'minus') {
			document.images['img'+linkid].src='img/minus.gif';
			setCookie(sItem, 'block');
		} else {
			document.images['img'+linkid].src='img/plus.gif';
			deleteCookie(sItem);
		}
		//alert(sItem);
	}
}

//hämtar höjden
function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}

//sätter höjden på iframe 
function setIframeHeight(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  iframeEl.style.height = document.documentElement.clientHeight + "px"; //sets the iframe to the height of the page
  if ( iframeEl && iframeWin ) {
    //iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
    var docHt = getDocHeight(iframeWin.document);
    // need to add to height to be sure it will all show
    if (docHt) iframeEl.style.height = docHt + "px";
  }
}

function SelectAll() {
	for (var i=0;i<document.forms[0].elements.length;i++) {
		var e=document.forms[0].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);
		}
	}
}

//do not allow editing a fields content
function lockField(falt){
	var temp="document.forms[0].";
	temp += falt;
	temp += ".blur()";
	eval (temp);
}

//open new windows
function openWin(theUrl,theName, theSettings) {
	window.open(theUrl,theName,theSettings);void(0);
}

function makeDate () {
	var tooShort = "Året måste innehålla 4 siffror.";
	var tooShortError = "false";
	if (w3c) {
		document.getElementById('Datum').value = document.getElementById('theYear').value + "-" + document.getElementById('theMonth').value + "-01";
		if (document.getElementById('theYear').value.length < 4) {
			tooShortError = "true";
		}
	}
	else if (ie) {
		document.all('Datum').value = document.all('theYear').value + "-" + document.all('theMonth').value + "-01";
		if (document.all('theYear').value.length < 4) {
			tooShortError = "true";
		}
	}
	else if (ns) {
		document.theForm.Datum.value = document.theForm.theYear.value + "-" + document.theForm.theMonth.value + "-01";
		if (document.theForm.theYear.value.length < 4) {
			tooShortError = "true";
		}
	}
	if (tooShortError == "true") {
		alert(tooShort);
	}
	else {
		document.kalenderForm.submit();
	}
}

function loadingPage(elementId,txt) { //not used
	totalWidth = document.body.clientWidth;
	halfWidth = totalWidth/2;
	loadingPos = halfWidth - 100;
	loadingPos = loadingPos.toFixed(0);
	//alert("totalWidth=" + totalWidth + " halfWidth=" + halfWidth + " loadingPos=" + loadingPos);
	document.getElementById(elementId).innerHTML = '<div style="left:'+loadingPos+'px;width:200px;height:200px;" class="logging"><div class="loggingPadding"><img src="img/loading.gif" alt="jobbar"/><br/>'+txt+'</div></div>';
}

//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;
	}
}

function changeAddLink(qs) {
	document.getElementById("inner3_2").innerHTML = '<a href="kalender.asp?cat=add&action=view&' + qs + '" target="contentFrame" onclick="javascript:this.blur();">Lägg till notering</a>';
}
