function FF_fbDump(s)
{
    if(window.console)
    {
        console.log(s);
    }
}

function AFW_addEvent(oElement, eventType, fn, useCapture) {
    if (oElement.addEventListener) {
        oElement.addEventListener(eventType, fn, useCapture);
        return true;
    } else if (oElement.attachEvent) {
        var evt = oElement.attachEvent('on' + eventType, fn);
        return evt;
    } else {
        oElement['on'+eventType] = fn;
    }
}

function AFW_removeEvent(oElement, eventType, fn, useCapture) {
    if (oElement.removeEventListener) {
        oElement.removeEventListener(eventType, fn, useCapture)
    } else if (oElement.detachEvent) {
        oElement.detachEvent('on' + eventType, fn);
    } else {
        oElement['on' + eventType] = null;
    }
}

function AFW_iframeResizer(id) //temporary, only used by: kinnevik, wallenstam, gunnebo, common/ams/ams_default.tpl
{
    if (document.addEventListener) { // Moz
        documentHeight = document.documentElement.offsetHeight;
    } else {
        documentHeight = document.documentElement.scrollHeight > document.body.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
    }
    documentHeight = documentHeight + 20;
    var currentIframe = null;
    if(id == null || id == '')
    {
        var parentIframes = top.document.getElementsByTagName('iframe');
        var frameFound = false;
        for(var i = 0; i < parentIframes.length; i++)
        {
            currentIframe = parentIframes[i];
            if(currentIframe.src && (location.href.indexOf(currentIframe.src) > -1 || currentIframe.src.indexOf(location.pathname) > -1))
            {
                frameFound = true;
                top.document.AFW_Iframe_Memory = currentIframe;
                break;
            }
            else
                currentIframe = null;
        }
        if(!frameFound)
            if(top.document.AFW_Iframe_Memory != undefined)
                currentIframe = top.document.AFW_Iframe_Memory;
    }
    else
    {
        if(top.document.getElementById(id))
            currentIframe = top.document.getElementById(id);
    }
    if(currentIframe)
    {
        currentIframe.style.height = documentHeight + 'px';
    }
    FF_fbDump([currentIframe, documentHeight]);
}

function AFW_bigSizeImage(img, step, stamp)
{
    if(!img || img == null || img == '')
        return;
    if(!step || step == null || step == '')
        step = 1;
    if(step == 1)
    {
        if(!AFW_JSmemory.AFW_bigSizeImage)
        {
            AFW_JSmemory.AFW_bigSizeImage =
            {
                references : []
            };
        }
        var afw =
        {
            parent : top
            ,container : document.createElement('DIV')
            ,info : document.createElement('DIV')
            ,child : document.createElement('IMG')
            ,loader : document.createElement('DIV')
            ,stamp : (new Date).getTime()
        };
        afw.container.id = 'ajax-container_' + afw.stamp;
        afw.loader.id = 'ajax-loader_' + afw.stamp;
        afw.container.className = 'ajax-image-big';
        afw.loader.className = 'ajax-loader-fullscreen';
        afw.info.appendChild(document.createTextNode('Click to close'));
        afw.child.src = '/images/' + img;
        afw.container.appendChild(afw.child);
        afw.container.appendChild(afw.info);
        AFW_setOpacity(afw.loader, 10);
        AFW_setOpacity(afw.container, 0);
        AFW_JSmemory.AFW_bigSizeImage.references.push
        (
            {
                ref : afw.stamp
                ,obj : afw
            }
        );
        afw.parent.document.body.appendChild(afw.loader);
        afw.parent.document.body.appendChild(afw.container);
        setTimeout('AFW_bigSizeImage(\'' + img + '\', 2, ' + afw.stamp + ')', 50);
    }
    else if(step == 2)
    {
        var afw = AFW_JSmemory.AFW_bigSizeImage.references.searchByRef(stamp);
        if(afw !== false)
        {
            afw = afw.obj;
            alert(afw.child.offsetWidth);
        }
    }
    else if(step == 3)
    {

    }
}

function openWindow(uri, w, h) { /* inga menyer, bara fönster */
	ticker=window.open(uri, "afw_win", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+w+",height="+h);
}

function openWindow2(uri, w, h) { /* samma som openWin fast med toolbar */
	ticker=window.open(uri, "afw_win", "toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+w+",height="+h);
}

function openWindow3(uri, w, h) { /* samma som openWin fast med toolbar och meny */
	ticker=window.open(uri, "afw_win", "toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width="+w+",height="+h);
}

/* använd denna om popup-fönstret inte ska ha scrollbars */
function openImageWindow(uri, w, h) {
	ticker=window.open(uri, "afw_win", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width="+w+",height="+h);
}

function openWin(uri, w, h) {
	return openWindow(uri, w, h);
}

function openWinFocus(uri, w, h) {
	win=window.open(uri, "afw_win", "toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+w+",height="+h);
    win.focus();
	return;
}

function windowOpen(uri, toolbar, location, directories, status, menubar, scrollbars, resizable, w, h) {
	ticker=window.open(uri, "afw_win", "toolbar="+toolbar+",location="+location+",directories="+directories+",status="+status+",menubar="+menubar+",scrollbars="+scrollbars+",resizable="+resizable+",width="+w+",height="+h);

}

function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/) != -1) {
		return true;
	} else {
		return false;
	}
}

function isReady(myForm) {
	 if (!isEmail(myForm.email.value)) {
		  alert("Please enter a valid email address.");
		  return false;
	 }
	 return true;
}

/**
* Prompta användaren med meddelandet i msg och
* fortsätt till 'uri'
*/
function alertUserLink(msg, uri) {
	alert(msg);
	world = window.open(uri, "win1");
	return false;
}

/**
* Låt användaren svara ja eller nej på frågan 'msg',
* fortsätt till 'uri' på ja, avbryt på nej
*/
function promptUserLink(msg, uri) {
	if (confirm(msg)) {
		world = window.open(uri, "win1");
	}
	return false;
}

/**
 * Post a form to popup win
 */
function postToPopup(formObj,w,h) {
	windowName = 'formTarget';
	formObj.target = windowName;
	foo = window.open('',windowName,'width='+w+',height='+h+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
}

/* Sigma function for Elux */
function popUp(url, w, h){
	if(url.indexOf("?")!=-1){
		url = url + "&pHeight=" + h + "&pWidth=" + w
	}else{
		url = url + "?pHeight=" + h + "&pWidth=" + w
	}
	aScroll = "yes";
	h = h + 15
	w = w + 20
	if( h > 600 ){
		h = 600
	}
	if( w > 800){
		w = 800
	}
	winProps = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=" + aScroll + ",resizable=false,width=" + w + ",height=" + h
	newWin = window.open(url,"popup",winProps)
}

function AFW_displayLoadingBox(str) {
    if (o = document.getElementById('AFW-loading')) {
        if (str) {
            o.innerHTML = '<div>'+str+'</div>';
        }
        o.style.display = 'block';
    }
}

function getXMLHttpRequest() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        http = new XMLHttpRequest();
    }
    return http;
}

function afwEBrev(a,b) {
    document.location = 'mailto:' + a + '@' + b;
}

Array.prototype.inArray = function(obj) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if (this[x] == obj) return true;
	}
	return false;
}

Array.prototype.search = function(obj) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if (this[x] == obj) return x;
	}
	return false;
}

Array.prototype.searchByRef = function(ref)
{
    var len = this.length;
    for(var x = 0; x < len; x++)
    {
        if(this[x].ref == ref)
            return this[x];
    }
    return false;
}

function getAbsolutePosition(e) {
  var r = { x: e.offsetLeft, y: e.offsetTop };
  if (e.offsetParent) {
    var tmp = getAbsolutePosition(e.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
}

function addEvent(obj, type, fn) {
    if (obj.attachEvent) {
        obj['e'+type+fn] = fn;
        obj[type+fn] = function() {
            obj['e'+type+fn](window.event);
        }
       obj.attachEvent('on'+type, obj[type+fn]);
    } else {
       obj.addEventListener(type, fn, false);
    }
}

function removeEvent( obj, type, fn ) {
    if (obj.detachEvent) {
        obj.detachEvent('on'+type, obj[type+fn]);
        obj[type+fn] = null;
    } else {
        obj.removeEventListener(type, fn, false);
    }
}

var AFW = {};

AFW.addGoogleAnalyticsLinkTrackers = function() {
    var a = document.getElementsByTagName('a');
    var r = new RegExp(document.location.host);
    var t;
    for (var i = 0; i < a.length; i++) {
        if (!a[i].onclick) {
            if (a[i].href.match(/^mailto:/)) {
                t = 'mailto-link';
            } else if (a[i].href.match(r)) {
                /* How to avoid "internal-link" for start page? */
                if (!(a[i].href.match(/\.php/) || a[i].href.match(/\?(p|s|t)/))) {
                    t = 'internal-link';
                }
            } else if (!a[i].href.match(r)) {
                t = 'outgoing-link';
            }
            if (t) {
                a[i].onclick = function() {
                    pageTracker._trackPageview ('/'+t+'/?'+this.href);
                }
            }
        }
    }
};

var AFW_JSmemory = {};