/* ------------------------------------------------
   Smack JavaScript application library
   All code Copyright Smack 2000-2007.
   Author: Eric Svensson
------------------------------------------------ */


var formValidateNoSubmit = false;

var mPosX,mPosY,mousePagePosX=0,mousePagePosY=0;

var browser_gecko=(navigator.userAgent.search("Gecko")!=-1 ? true : false);
var browser_ie=(navigator.userAgent.search("MSIE")!=-1 && navigator.userAgent.search("Opera")==-1 ? true : false);
var browser_opera=(navigator.userAgent.search("Opera")!=-1 ? true : false);

var agt = navigator.userAgent.toLowerCase();
var is_ie	   = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_opera  = (agt.indexOf("opera") != -1);
var is_mac	   = (agt.indexOf("mac") != -1);
var is_mac_ie = (is_ie && is_mac);
var is_win_ie = (is_ie && !is_mac);
var is_gecko  = (navigator.product == "Gecko");

var ua = navigator.userAgent.toLowerCase();
var isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1))? true:false;
var IEVersion = parseFloat(ua.substring(ua.lastIndexOf("msie ")+5));
var isIE7 = (isIE && (ua.indexOf("msie 7") !=-1));
var isIE5 = (isIE && (ua.indexOf("msie 5.0") !=-1));
var	isOpera = (ua.indexOf("opera") != -1)? true:false;
var OperaVersion = parseFloat(ua.substring(ua.lastIndexOf("Opera/") + 7));
var	isSafari = (ua.indexOf("webkit") != -1)? true:false;
var SafariVersion = parseFloat(ua.substring(ua.lastIndexOf("webkit/") + 7));
if(!isSafari) var isGecko = (ua.indexOf("gecko") != -1)? true:false;
else var isGecko = false;
var	isKonqueror = (ua.indexOf("konqueror") != -1)? true:false;





/* -----------------------------
       General functions
----------------------------- */

function updateMousePositionCoords(e) {
	mousePagePosX=(is_gecko ? e.layerX : event.x);
	mousePagePosY=(is_gecko ? e.layerY : event.y);
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() {
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
obj.visibility=v; }
}


function rb(obj) {obj.checked=1;}
function cb(obj) {obj.click();}


function addEvent(obj, evType, fn, useCapture){
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, useCapture);
        return true;
    }else if (obj.attachEvent) {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    }
	return false;
}

function removeEvent(obj, evType, fn, useCapture) {
	if (obj.removeEventListener) {
		obj.removeEventListener(evType, fn, useCapture);
		return true;
	}else if (obj.detachEvent) {
		obj.detachEvent("on"+evType, fn);
		return true;
	}
	return false;
}


function removeClass(el, className) {
	if (!(el && el.className)) {
		return;
	}
	var cls = el.className.split(" ");
	var ar = new Array();
	for (var i = cls.length; i > 0;) {
		if (cls[--i] != className) {
			ar[ar.length] = cls[i];
		}
	}
	el.className = ar.join(" ");
}

function addClass(el, className) {
	removeClass(el, className);
	el.className += " " + className;
}


function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}


function arraySearch(ar,val) {
	for (var i=0;i<ar.length;i++) {
		if (ar[i]==val) {
			return i;
		}
	}
	return false;
}


function SWIBV(obj,mode) {
	if (mode==0) {
		MM_showHideLayers(obj,'','hide');
	}else{
		MM_showHideLayers(obj,'','show');
	}
}


function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return [curleft,curtop];
}

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


// createElements by Stephen Sorensen (spudly.shuoink.com)
function createElements( args ) {
    var el;

    if( typeof(args) == "string" ) {

        el = document.createTextNode(args);

    } else if ( typeof(args) == "object" ) {

        el = document.createElement( args.tag );

        if ( args.attributes ) {
            for ( i in args.attributes ) {
                el.setAttribute(i, args.attributes[i]);
            }
        }
        if ( args.style ) {
            for ( i in args.style ) {
                el.style[i] = args.style[i];
            }
        }
        if ( args.children ) {
            for ( var i = 0; i < args.children.length; i++ ) {
                el.appendChild( createElements( args.children[i] ) );
            }
        }

    }
    return el;
}


function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}






/* -----------------------------
        Custom windows
----------------------------- */

function nwEX(p) {
	nwEXWindow=window.open(p, "EXAM", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=490, height=620, top=50, left=30, screenX=30, screenY=50");
	nwEXWindow.focus();
}

function nw(p) {
	nwWindow=window.open(p, 'GONmp', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=580, height=610, top=50, left=30, screenX=30, screenY=50');
	nwWindow.focus();
}
function nwU(p) {
	nwWindow=window.open(p, 'GONue', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=510, height=620, top=50, left=30, screenX=30, screenY=50');
	nwWindow.focus();
}
function nw1(p) {
	nw1Window=window.open(p, 'GON', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=510, height=580, top=50, left=30, screenX=30, screenY=50');
	nw1Window.focus();
}
function nwR() {
	nwRWindow=window.open('regler.phtml', 'regler', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=600, height=540, top=20, left=20, screenX=20, screenY=20');
}
function nwLost() {
	nwLostWindow=window.open('/lostuser.phtml', 'lostuser', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=360, height=300, top=200, left=200, screenX=200, screenY=200');
	nwLostWindow.focus();
}
function nwMP(p) {
	nwWindow=window.open(p, 'GONmp', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=570, height=620, top=50, left=30, screenX=30, screenY=50');
	nwWindow.focus();
}
function nwE32k5Comments(p) {
	nwE32k5Window=window.open(p, 'E32k5Comments', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=570, height=620, top=50, left=30, screenX=30, screenY=50');
	nwE32k5Window.focus();
}
function nwSpelkanslorComments(p) {
	nwSpelkanslorWindow=window.open(p, 'SpelkanslorComments', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=570, height=620, top=50, left=30, screenX=30, screenY=50');
	nwSpelkanslorWindow.focus();
}

function openInParent(url) {
	window.opener.location=url;
	window.opener.focus();
}

function videoWin(query) {
	videoWindow=window.open('../video.phtml?'+query, "video", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=570, height=620, top=50, left=30, screenX=30, screenY=50");
	videoWindow.focus();
}

function ntrpPrev(form) {
	fPrevAction = form.getAttribute("action");
	form.setAttribute("action","ntrp_preview.phtml");
	form.setAttribute("target","preview");
	ntrpPrevWindow = window.open('ntrp_preview.phtml', "preview", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=760, height=400, top=50, left=30, screenX=30, screenY=50");
	form.submit();
  	ntrpPrevWindow.focus();
	form.setAttribute("action",fPrevAction);
	form.setAttribute("target","");
}

function nw0(url) {
	nw0Window = window.open(url);
	nw0Window.focus();
}

function nwOrgPost(p) {
	nwOrgPostWindow = window.open(p, "orgpost", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=760, height=400, top=50, left=30, screenX=30, screenY=50");
	nwOrgPostWindow.focus();
}





/* -----------------------------
        Sorting/filtering
----------------------------- */

function SelectView(obj,name,fm,offset,count,forumOffset) {
	var soString=document.selview.so.value;
	var soThisOption=soString.substr(fm,1);
	var soStringFirst=soString.substr(0,fm);
	var soStringLast=soString.substr(fm+1,soString.length);
	if (soThisOption==1) {
		document.selview.so.value=soStringFirst+'0'+soStringLast;
		obj.src='http://img.gon.se/playing.se/cbox1_'+name+'_unchecked.gif';
	}else{
		document.selview.so.value=soStringFirst+'1'+soStringLast;
		obj.src='http://img.gon.se/playing.se/cbox1_'+name+'_checked.gif';
	}
	soString=document.selview.so.value;
	var allActive=true;
	for (var i=offset;i<offset+count;i++) {
		if (soString.substr(i,1)==0) allActive=false;
	}
	var newInt=null;
	if (!allActive) {
		document.getElementById('uVselAll'+forumOffset).src=document.getElementById('uVselAll'+forumOffset).src.replace(/_checked/,'_unchecked');
		newInt=0;
	}else{
		document.getElementById('uVselAll'+forumOffset).src=document.getElementById('uVselAll'+forumOffset).src.replace(/_unchecked/,'_checked');
		newInt=1;
	}
	var forumSelStatesString=document.selview.forumSelStates.value;
	var soStringFirst=forumSelStatesString.substr(0,forumOffset);
	var soStringLast=forumSelStatesString.substr(forumOffset+1,forumSelStatesString.length);
	document.selview.forumSelStates.value=soStringFirst+newInt+soStringLast;
}

function ToggleUserViewRange(offset,count,forumOffset) {
	var soString=document.selview.so.value;
	var forumSelStatesString=document.selview.forumSelStates.value;
	var newInt=(forumSelStatesString.substr(forumOffset,1)==0 ? 1 : 0);
	if (newInt==0) {
		document.getElementById('uVselAll'+forumOffset).src=document.getElementById('uVselAll'+forumOffset).src.replace(/_checked/,'_unchecked');
	}else{
		document.getElementById('uVselAll'+forumOffset).src=document.getElementById('uVselAll'+forumOffset).src.replace(/_unchecked/,'_checked');
	}
	var newSoPart='';
	for (var i=offset;i<offset+count;i++) {
		if (newInt==0) {
			document.getElementById('uVFS'+i).src=document.getElementById('uVFS'+i).src.replace(/_checked/,'_unchecked');
		}else{
			document.getElementById('uVFS'+i).src=document.getElementById('uVFS'+i).src.replace(/_unchecked/,'_checked');
		}
		newSoPart+=newInt;
	}
	var soStringFirst=forumSelStatesString.substr(0,forumOffset);
	var soStringLast=forumSelStatesString.substr(forumOffset+1,forumSelStatesString.length);
	document.selview.forumSelStates.value=soStringFirst+newInt+soStringLast;

	soStringFirst=soString.substr(0,offset);
	soStringLast=soString.substr(offset+count,soString.length);
	document.selview.so.value=soStringFirst+newSoPart+soStringLast;
}





/* -----------------------------
        Field insertion
----------------------------- */

function insertIntoTA(obj,text) {
	if(obj.isTextEdit) {
		obj.focus();
		var sel=document.selection;
		var rng=sel.createRange();
		rng.colapse;
		if((sel.type=="Text" || sel.type=="None") && rng!=null) {
			rng.text=text;
		}
	}else{
		obj.value+=text;
	}
}


function resizeTImg( imgRand ) {
	tImg = eval( 'tImg'+imgRand );
	imgHWAspect = 1;
	if ( tImg.complete ) {
		if (tImg.width!=0 && tImg.height!=0) imgHWAspect = ( tImg.height / tImg.width );
		if ( ( tImg.width>maxWidth || (tImg.width==0 || tImg.height==0 ))) {
			document.getElementById( 'tImg'+imgRand+'_img' ).style.width=maxWidth+'px';
			document.getElementById( 'tImg'+imgRand+'_img' ).style.height=(imgHWAspect>0.0 && tImg.height!=0 ? maxWidth*imgHWAspect : maxHeight)+'px';
			clearInterval( eval( 'resizeProc'+imgRand ) );
		}else{
			document.getElementById( 'tImg'+imgRand+'_img' ).style.width=tImg.width+'px';
			document.getElementById( 'tImg'+imgRand+'_img' ).style.height=tImg.height+'px';
			clearInterval( eval( 'resizeProc'+imgRand ) );
		}
	}
}





/* ----------------------------
          User pages
---------------------------- */

function userFriendShow(e) {
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    var pagePosX=findPosX(document.getElementById('root'));
    var pagePosY=findPosY(document.getElementById('root'));
    var posX=findPosX(tg)-pagePosX-51;
    var posY=findPosY(tg)-pagePosY+18;
    document.getElementById('userBecomeFriend').style.top=posY+'px';
    document.getElementById('userBecomeFriend').style.left=posX+'px';
    removeClass(document.getElementById('userFriendIcon'),"userFriendIconDL");
    addClass(document.getElementById('userFriendIcon'),"userFriendIconHL");
    document.getElementById('userBecomeFriend').style.visibility='visible';
}
function userFriendHide(e) {
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
    // Check whether we are moving from the icon to the divs
    if ((tg.id=='userFriendIcon' || tg.id=='userFriendIconImg') && (reltg.id=='userBecomeFriend' || reltg.id=='userBecomeFriendInner')) return;
    // Check whether we left for a child of the element (i.e. an element inside our element)
	var i=0;
	while (reltg != document.getElementById('userBecomeFriend') && reltg.nodeName != 'BODY') {
        if (i>20) break; // break if we went more than 20 levels up in the DOM-tree
        reltg = reltg.parentNode;
        i++;
	}
    if (reltg == document.getElementById('userBecomeFriend')) return;

	document.getElementById('userBecomeFriend').style.visibility='hidden';
	removeClass(document.getElementById('userFriendIcon'),"userFriendIconHL");
    addClass(document.getElementById('userFriendIcon'),"userFriendIconDL");
}






/* ----------------------------
             AJAX
---------------------------- */


function createXMLHttpRequest() {
  try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
  try { return new XMLHttpRequest();                   } catch(e) {}
  alert("XMLHttpRequest not supported");
  return null;
}


function AJAXMakeRequest(url,vars,handler) {
    var httpRequest;

    if (typeof handler != "function") return false;

    var httpRequest = createXMLHttpRequest();

    if (!httpRequest) {
        alert('Fel: Kunde inte skapa ett XMLHTTP-objekt.');
        return false;
    }
    httpRequest.onreadystatechange = function() {
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                handler(httpRequest);
            }
        }
    };
    httpRequest.open('POST', url, true);
    httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    httpRequest.setRequestHeader("Content-length", vars.length);
    httpRequest.setRequestHeader("Connection", "close");
    httpRequest.send(vars);

}





/* -----------------------------
          DOM parsing
------------------------------ */

function getElementsByClassName(classname) {
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = document.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}





/* ----------------------------
         Miscellaneous
---------------------------- */

function resizeScreenImages(rootElmId,maxWidth) {
    if (!document.getElementById(rootElmId)) return;
    var elms = document.getElementById(rootElmId).getElementsByTagName('img');
    var imgObjA = new Array();
    var j=0;
    for (var i=0;i<elms.length;i++) {
        imgObj = new Image();
        imgObj.src = elms[i].getAttribute("src");
        if (imgObj.width>maxWidth) elms[i].style.width = maxWidth+'px';

        elms[i].onload = function(e) {
            imgObj = new Image();
            imgObj.src = this.getAttribute("src");
            if (imgObj.width>maxWidth) this.style.width = maxWidth+'px';
        }
    }
}





