
var item;
var item2;

// Get ID functions

	function gid(item)
	{
		if(document.all)
			return document.all[item];
     	  else if (document.getElementById)
			return document.getElementById(item);
	}
	
	function id(item){
    	if(document.all)
			return document.all[item];
		  else if (document.getElementById)
			return document.getElementById(item);
	}

	function name(name)
	{
		return document.getElementsByName(name);
	}




// Switch functions

	function display(mode, item)
	{
		item = gid(item);
		if(mode == 0)
			item.style.display = (item.style.display == 'none' || item.style.display == 'display' || item.style.display == '')?'block':'none';
   		  else
			item.style.display = (mode == 1)?'block':'none';
	}
		
	function swapShow(item)
	{
		item = id(item);
		item.className = (item.className == 'view' || item.className == '')?'hide':'view';	
	}
	
	function swap_img(item, img1, img2)
	{
		if(strpos(item.src, img1, 1) == false)
		{
			item.src = img1;
		}else{
			item.src = img2;
		}
	}



// Form functions

	function setcheck(item) { gid(item).checked = true; }

	function swap_check(item)
	{
		if(item.checked === true)
		{
			item.checked = false;
		}else{
			item.checked = true;
		}
	}
	
	function submit(form)
	{
		form.submit();
	}
	
	function insert_text(myField, myValue){

		myField = document.getElementById(myField);

		if (document.selection) { //IE support
			myField.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
			myField.focus(0);
		}else if (myField.selectionStart || myField.selectionStart == '0') { //MOZILLA/NETSCAPE support
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			var scrollTop = myField.scrollTop;
			myField.value = myField.value.substring(0, startPos)
			              + myValue 
	                      + myField.value.substring(endPos, myField.value.length);
			myField.focus();
			myField.selectionStart = startPos + myValue.length;
			myField.selectionEnd = startPos + myValue.length;
			myField.scrollTop = scrollTop;
		} else {
			myField.value += myValue;
			myField.focus(0);
		}
	}
	
	
	function validForm(form, ids, email)
	{
		var arr = new Array();
		arr = ids.split (',');
		var status = true;

		for (var i=0; i<arr.length; i++)
		{
			if(document.getElementById(arr[i]).value == '')
			{
				document.getElementById(arr[i]).className = 'txtError';
				status = false;
			}else{
				document.getElementById(arr[i]).className = '';
			}
		}

		if(!validEmail(email))
		{
			document.getElementById(email).className = 'txtError';
			status = false;
		}else{
			document.getElementById(email).className = '';
		}

		return status;

	}


	function bbcinput(bID, ID, entry){

	     bID = gid(bID);

		if(bID.className=='active'){
		  bID.className = 'inactive';
		  entry = "[/"+entry+"]";
		 }else{
		  bID.className = 'active';
		  entry = "["+entry+"]";
		}

	 if(entry!=''){
	    insert_text(ID, entry);
	  }

	}


// CMS Post Search

function postSearch(pint,type)
{
	var str = id('postSearchStr').value;
	var lang = id('postSearchLang').value;
	var group = id('postSearchGroup').value;

	var link = 'ajax/posts.php?p='+ pint +'&t=' + type + '&s=' + str + '&l=' + lang + '&g=' + group;
	//alert(link);
	postBack(link, 'postRows');
}




// Random misc

function antiSpam(str)
{
    parts   = str.split ('|');
    domain  = parts[0].split ('.').reverse ().join ('.');
    name    = parts[1].split ('.').reverse ().join ('.');
    output = name+'@'+domain;

	window.location = 'mailto:'+output;
}

function new_window(url,width,height,status,resizable){

	if(url==''){ return; }

	status = (status!='')?',status':'';
	resizable = (resizable!='')?',resizable':'';

	    var options = "width=" + width + ",height=" + height + status + resizable;
	    var new_window = window.open(url,"",options);

  }



// Text manipulation

function strpos( haystack, needle, offset)
{
    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}

function validEmail(str) {

	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(id(str).value))
	{
		return false;
	}

	return true;
}

function checkEmail(email, mode)
{
	if(mode == 1)
	{
		email = id(email);
	}
	
	if(mode == 2)
	{
		email = name(email);
	}
	
	if(email.value == '')
	{
		return false;
	}
	
	var check = document.getElementById(email.name + "_check");
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value))
	{
		check.innerHTML = '<span style="color:red">E-post ogiltig.</span>';
		return false;
	}
	
	check.innerHTML = '<span style="color:green">E-post godkänd.</span>';
	return true;
	
}


// Navigation build

var hoverDebug = Array();
sfHover = function() {
	try
	{

		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" hover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" hover\\b"), "");
			}
		}
	}
	catch(err)
	{
		hoverDebug[0] = err;
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);








// Enviromental functions, fade & color behavior


var yScrollInterval;
var __nonMSDOMBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);


function GetYposition() {
    if (__nonMSDOMBrowser) {
        return window.pageYOffset;
    }
    else {
        if (document.documentElement && document.documentElement.scrollTop) {
            return document.documentElement.scrollTop;
        }
        else if (document.body) {
            return document.body.scrollTop;
        }
    }
    return 0;
}

function GetEnv() {

  var contentWidth = 0, contentHeight = 0, windowWidth = 0, windowHeight = 0, positionX = 0, positionY = 0;

	if (window.innerHeight && window.scrollMaxY) {	
		contentWidth = document.body.scrollWidth;
		contentHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		contentWidth = document.body.scrollWidth;
		contentHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		contentWidth = document.body.offsetWidth;
		contentHeight = document.body.offsetHeight;
	}


	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}


	if (__nonMSDOMBrowser) {
		positionY = window.pageYOffset;
		positionX = window.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
            positionY = document.documentElement.scrollTop;
            positionX = document.documentElement.scrollLeft;
	} else if (document.body) {
            positionX = document.body.scrollTop;
		positionY = document.body.scrollLeft;
	}

	//alert('wh:'+windowHeight+',ch:'+contentHeight+',px:'+positionY);

	var data = new Array();

	data['windowWidth'] = windowWidth;
	data['windowHeight'] = windowHeight;
	data['contentWidth'] = contentWidth;
	data['contentHeight'] = contentHeight;
	data['positionX'] = positionX;
	data['positionY'] = positionY;

	return data; }

function GetElementYposition(item) {
	item = gid(item);
	var pos = 0;
		pos = item.offsetTop;

	return pos;
}


function getElementHeight(id) {
  if (NS4) return eval(id).clip.height
  else return eval(id).clientHeight;
}

var previous_pos;

function yScroll(dest) {

	var ypos = GetYposition();

	if(ypos<dest){
	var stepsize = parseInt((ypos-dest)/14);
	alert(dest+' > '+ypos+' "'+stepsize);
   	   var pos = parseInt(ypos+stepsize)+14;

	   if(pos>=dest || previous_pos==pos){
	      clearInterval(yScrollInterval);
	    }
	  }else{
	var stepsize = parseInt((ypos-dest)/14);
   	   var pos = parseInt(ypos-stepsize)-14;
	   if(pos<=dest || previous_pos==pos){
	      clearInterval(yScrollInterval);
	    }
	 }
   previous_pos = pos;
   window.scrollTo(0, pos);
}

function yPos(dest){
	clearInterval(yScrollInterval);
	yScrollInterval = setInterval('yScroll('+dest+')',10);

  }

function yPosElement(item){
	var dest = gid(item).offsetTop;
	clearInterval(yScrollInterval);
	yScrollInterval = setInterval('yScroll('+dest+')',10);
  }


var nlbFade_hextable = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ];
var nlbFade_elemTable = new Array( );
var nlbFade_t = new Array( );

function ElementColorFade( elementId, startBgColor, endBgColor, fadeTime )
{
	var timeBetweenSteps = Math.round( Math.max( fadeTime / 300, 30 ) );
	var nlbFade_elemTableId = nlbFade_elemTable.indexOf( elementId );
	if( nlbFade_elemTableId > -1 )
	{
		for( var i = 0; i < nlbFade_t[nlbFade_elemTableId].length; i++ )
			clearTimeout( nlbFade_t[nlbFade_elemTableId][i] );
	}
	else
	{
		nlbFade_elemTable.push( elementId );
		nlbFade_elemTableId = nlbFade_elemTable.indexOf( elementId );
	}
	var startBgColorRGB = hexToRGB( startBgColor );
	var endBgColorRGB = hexToRGB( endBgColor );
	var diffRGB = new Array( );
	for( var i = 0; i < 3; i++ )
		diffRGB[i] = endBgColorRGB[i] - startBgColorRGB[i];
	var steps = Math.ceil( fadeTime / timeBetweenSteps );
	var nlbFade_s = new Array( );
	for( var i = 1; i <= steps; i++ )
	{
		var changes = new Array( );
		for( var j = 0; j < diffRGB.length; j++ )
			changes[j] = startBgColorRGB[j] + Math.round( ( diffRGB[j] / steps ) * i );
		if( i == steps )
			nlbFade_s[i - 1] = setTimeout( 'document.getElementById("'+elementId+'").style.backgroundColor = "'+endBgColor+'";', timeBetweenSteps*(i-1) );
		else
			nlbFade_s[i - 1] = setTimeout( 'document.getElementById("'+elementId+'").style.backgroundColor = "'+RGBToHex( changes )+'";', timeBetweenSteps*(i-1) );
	}
	nlbFade_t[nlbFade_elemTableId] = nlbFade_s;
}
function hexToRGB( hexVal )
{
	hexVal = hexVal.toUpperCase( );
	if( hexVal.substring( 0, 1 ) == '#' )
		hexVal = hexVal.substring( 1 );
	var hexArray = new Array( );
	var rgbArray = new Array( );
	hexArray[0] = hexVal.substring( 0, 2 );
	hexArray[1] = hexVal.substring( 2, 4 );
	hexArray[2] = hexVal.substring( 4, 6 );
	for( var k = 0; k < hexArray.length; k++ )
	{
		var num = hexArray[k];
		var res = 0;
		var j = 0;
		for( var i = num.length - 1; i >= 0; i-- )
			res += parseInt( nlbFade_hextable.indexOf( num.charAt( i ) ) ) * Math.pow( 16, j++ );
		rgbArray[k] = res;
	}
	return rgbArray;
}
function RGBToHex( rgbArray )
{
	var retval = new Array( );
	for( var j = 0; j < rgbArray.length; j++ )
	{
		var result = new Array( );
		var val = rgbArray[j];
		var i = 0;
		while( val > 16 )
		{
			result[i++] = val%16;
			val = Math.floor( val/16 );
		}
		result[i++] = val%16;
		var out = '';
		for( var k = result.length - 1; k >= 0; k-- )
			out += nlbFade_hextable[result[k]];
		retval[j] = padLeft( out, '0', 2 );
	}
	out = '#';
	for( var i = 0; i < retval.length; i++ )
		out += retval[i];
	return out;
}
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function( val, fromIndex ) {
		if( typeof( fromIndex ) != 'number' ) fromIndex = 0;
		for( var index = fromIndex, len = this.length; index < len; index++ )
			if( this[index] == val ) return index;
		return -1;
	}
}

function padLeft( string, character, paddedWidth )
{
	if( string.length >= paddedWidth )
		return string;
	else
	{
		while( string.length < paddedWidth )
			string = character + string;
	}
	return string;
}



function fadeout(id, opacStart, millisec)
{
    var speed = Math.round(millisec / 100);
    var timer = 0;
    if(opacStart > 0) {
        for(i = opacStart; i >= 0; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }

}

	function changeOpac(opacity, id)
	{
    	var object = document.getElementById(id).style;
    	object.opacity = (opacity / 100);
    	object.MozOpacity = (opacity / 100);
    	object.KhtmlOpacity = (opacity / 100);
    	object.filter = "alpha(opacity=" + opacity + ")";

		if(opacity < 2)
		{
			document.getElementById(id).style.display = 'none';
		}
	}













function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}



// Content Management System Overlay

function hideCms(){

	var objOverlay = document.getElementById('overlayer');

	objOverlay.style.display = 'none';

	}

function showCms(){

	var objOverlay = document.getElementById('overlayer');

	objOverlay.style.display = 'block';

	}


