//General Javascript functions for Nordic Apple Key

//Anvder AJAX-bibliotek Ace, se www.lishen.name
//global variables
var ajaxEngine=null; 
var hasres=new Array(0,0,0);
var waitDB=false;

function initAjaxEngine(){
    if(ajaxEngine==null){
		ajaxEngine = new Ace.Engine();
	//aceEngine = new Ace.Engine("debug"); //Use this for debug
	//return new Ace.Engine();
    }
	//return ajaxEngine;
}
function startWait(){
	//waitDB=true;
	//var ww = document.getElementById('waitwindow');
	//ww.style.visibility="visible";
}
function releaseWait(){
	//waitDB=false;
	//var ww = document.getElementById('waitwindow');
	//ww.style.visibility="hidden";
}

function setLanguage(lang){
    var ls = location.search;
    if(ls ==""){
	ls="?chglang="+lang;
    }else{
	if(ls.indexOf("chglang=")>-1){
	    ls=ls.substr(1);
	    par = ls.split("&");
	    ls="?";
	    for(var i=0;i<par.length;i++){
		if(par[i].indexOf("chglang=")>-1){
		    ls+="chglang="+lang;    
		}else{
		    ls+=par[i];
		}
		if(i<par.length-1){
		    ls+="&";
		}
	    }
	}else{
	    ls+="&chglang="+lang; 
	}

    }
    location.search=ls;
    //location.href=location.href+addtourl;
}

//Show large image
function showLargeImg(url){
	var td = document.getElementById("fullsizeimg");
	var div = document.getElementById("fullsize");
	var img = document.createElement("img");
	img.src = url;
	img.onclick=closeFullsize;
	while(td.hasChildNodes()){
		td.removeChild(td.lastChild);
	}
	td.appendChild(img);
	div.style.visibility="visible";
}
function closeFullsize(){
	var div = document.getElementById("fullsize");
	div.style.visibility="hidden";	
}

//This styles are set hardcoded here and not by setting attribute class
//because that requires browser specific code (IE:class, N:className)
function setPicklistActive(elem){
    elem.style.color="#ffffff";
    elem.style.backgroundColor="#aaaaaa";
    //elem.style.textDecoration="none";
}
function setPicklistInactive(elem){
    elem.style.color="#ffffff";
    elem.style.backgroundColor="#005070";
}

//Handle result display
function showResult(list){
	var i;
	for(i=0;i<3;i++){
		if(i==list && hasres[list]>0){
			document.getElementById("resdiv"+i).style.visibility="visible";
		}else{
			document.getElementById("resdiv"+i).style.visibility="hidden";
		}
	}
}

function hideResult(){
	var div;
	for(i=0;i<3;i++){	
		div = document.getElementById("resdiv"+i);
		div.style.visibility="hidden";
	}
}

function printResult(instr){
	
	//alert("printRes in:"+instr);
	var div;
	var html;
	var sort;
	var i,j,ix,end,start,cls,dh,lh,mh;
	
	var res = instr.split("::");
	hasres=res.slice(0,3);
	
	//if(res[0]>0){hasres_0=true;}else{hasres_0=false;}
	//if(res[1]>0){hasres_1=true;}else{hasres_1=false;}
	//if(res[2]>0){hasres_2=true;}else{hasres_2=false;}
	//Print the numbers, position 0-2
	for(i=0;i<3;i++){
/*
		if(res[i] > 0){
			document.getElementById("restxt"+i).setAttribute("onmouseover","showResult("+i+")");
			document.getElementById("resnum"+i).setAttribute("onmouseover","showResult("+i+")");
		}else{
			document.getElementById("restxt"+i).removeAttribute("onmouseover");
			document.getElementById("resnum"+i).removeAttribute("onmouseover");		
		}
*/
		document.getElementById("resnum"+i).innerHTML=res[i];
	}	
	//Print the sortlists
	start=3;
	end=3;
	lh=18; //line height
	mh=440; //max height
	for(i=0;i<3;i++){
		dh=30;
		div = document.getElementById("resdiv"+i);
		end += parseInt(res[i]);
		html="<table cellspacing='0' cellpadding='0' id='restab'>";
		//alert("start:"+start+" end:"+end+" res.length:"+res.length); 
		for(j=start;j<end;j++){
			if(j%2!=0){
				cls	= "res"+i;				
			}else{
				cls	= "resblank";		
			}
			sort = res[j].split(";;");
			//html+="<tr><td class='"+cls+"' width='80%' onclick='showSort("+sort[0]+")'>"+sort[1]+"</td><td onclick='showSort("+sort[0]+")' width='20%' class='"+cls+"'>"+sort[2]+"&nbsp;%</td></tr>";
        	html+="<tr><td class='"+cls+"' width='80%' onclick='showSort("+sort[0]+")'>"+sort[1]+"</td><td onclick='showSort("+sort[0]+")' width='20%' class='"+cls+"'>"+sort[2]+"</td></tr>";
			dh+=lh;
		}
		start += parseInt(res[i]);
		html+="</table>";
		div.innerHTML = html;
		if(dh > mh){
			div.style.height=mh+"px";
		}else{
			div.style.height=dh+"px";
		}
	}
}

function showSort(id){
	var form = document.showsortform;
	form.id.value=id;
	form.submit();
}

