function showhideDiv(divid,spamcheckid,spamcheckname,oThis){
	divobj = document.getElementById(divid);
	if (divobj.style.display=="none"){
		if (oThis) {
			jQuery('input', oThis).attr('src', '/images/debat/downarrow.gif');
		}
		divobj.style.display = "inline";
		document.getElementById(spamcheckid).name=spamcheckname;
	} else {
		if (oThis) {
			jQuery('input', oThis).attr('src','/images/debat/rightarrow.gif');
		}
		divobj.style.display = "none";
	}
	
	return false;
}

function ensureContent(checkSubject) {
	var ok = true;
	if(checkSubject) {
		var subject = document.getElementById('debat_subject').value;
		if(subject) {
			var anything = subject.length > 0;
			document.getElementById("post_err_message2").style.display =
				anything ? "none" : "block";
			if(!anything) ok = false;
		}
	}
	
	var content = document.getElementById('debat_content').value;
	var anything = content.length > 0;
	document.getElementById("post_err_message").style.display = 
		anything ? "none" : "block";
	if(!anything) ok = false;
	return ok;
}

function changeIsUser(isUser){
	if (isUser){
		document.getElementById('preuser').style.display="inline";
		document.getElementById('notuser').style.display="none";
	} else {
		document.getElementById('preuser').style.display="none";
		document.getElementById('notuser').style.display="inline";
	}
}

function observeInstrument(id, author, buttonID) {
	toggleObserve("instrument", id, author, function(status) {});
}

function observeThread(id, author, buttonID) {
	toggleObserve("thread", id, author, function(status) { 
		var observeLink = document.getElementById('observelink'); 
		if(observeLink) { 
			if(status=='disabled') {
				observeLink.innerHTML = 'Overvåg'; 
			} else {
				observeLink.innerHTML = 'Stop overvågning';
			} 
		} 
	});
}

function blockInstrument(id, author, buttonID) {
	toggleBlock("instrument", id, author, function(status) {});
}

var observeHttp = null;
function toggleObserve(typekey, typeval, author, whenDone) {
	observeHttp=GetXmlHttpObject();
	if (observeHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
    }

    var url = '/services/debat/observe.php?action=toggle&' + typekey + '=' + typeval + '&author='+author;
    url=url+"&sid="+Math.random();
	observeHttp.onreadystatechange = function() { observeStateChanged(whenDone); };
	observeHttp.open("GET", url, true);
	observeHttp.send(null);
}

function observeStateChanged(whenDone) {
	if (observeHttp.readyState!=4) return;
	var xml = observeHttp.responseXML;
	if(!xml || xml==undefined) return;

	var status=xml.getElementsByTagName('status')[0].firstChild.data;
	whenDone(status);
}

var blockHttp = null;
function toggleBlock(typekey, typeval, author, whenDone) {
	blockHttp=GetXmlHttpObject();
	if (blockHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
    }

    var url = '/services/debat/block.php?action=toggle&' + typekey + '=' + typeval + '&author='+author;
	blockHttp.onreadystatechange = function() { blockStateChanged(whenDone); };
	blockHttp.open("GET", url, true);
	blockHttp.send(null);
}

function blockStateChanged(whenDone) {
	if (blockHttp.readyState!=4) return;
	var xml = blockHttp.responseXML;
	if(!xml || xml==undefined) return;

	var status=xml.getElementsByTagName('status')[0].textContent;
	whenDone(status);
}

function cycleOrder() {
	var form = document.getElementById('showThreadsForm');
	form.debat_order.value = form.next_debat_order.value;
	form.submit();
}

function cycleFilter() {
	var form = document.getElementById('showThreadsForm');
	form.debat_filter.value = form.next_debat_filter.value;
	form.submit();
}

function openCloseTread(treeid,offset,threadid,post_id,parent,coupleartid,level,shownpost){
	
	var form = document.showThreadsForm;
	form.threadid.value = "null";
	form.post_id.value = "null";
	form.coupleartid.value = "null";
	if(treeid!=null) form.treeid.value = treeid;
	if(offset!=null) form.offset.value = offset;
	if(threadid!=null) form.threadid.value = threadid;
	if(post_id!=null) form.post_id.value = post_id;
	if(parent!=null) form.post_id.value = parent;
	if(coupleartid!=null) {
		form.coupleartid.value = coupleartid;
		form.art_id.value = coupleartid;
	}
	if(level!=null) form.level.value = level;
	if(shownpost!=null) form.shownpost.value = shownpost;
	
	form.submit();
}
function NewopenCloseTread(treeid,offset,threadid,post_id,parent,coupleartid,level,shownpost){
	
	var form = document.showThreadsForm;
	form.threadid.value = "null";
	form.post_id.value = "null";
	form.coupleartid.value = "null";
	if(treeid!=null) form.treeid.value = treeid;
	if(offset!=null) form.offset.value = offset;
	if(threadid!=null) form.threadid.value = threadid;
	if(post_id!=null) form.post_id.value = post_id;
	if(parent!=null) form.parent.value = parent;
	if(coupleartid!=null) {
		form.coupleartid.value = coupleartid;
		form.art_id.value = coupleartid;
	}
	if(level!=null) form.level.value = level;
	if(shownpost!=null) form.shownpost.value = shownpost;
	
	form.submit();
}
function openCloseTreadNav(treeid,offset,threadid,post_id,parent,coupleartid,level,shownpost){
	var form = document.showThreadsForm;
	
	form.threadid.value = "null";
	form.post_id.value = "null";
	form.coupleartid.value = "null";
	form.showall.value = "";
	
	if(treeid!=null) form.treeid.value = treeid;
	if(offset!=null) form.offset.value = offset;
	if(threadid!=null) form.threadid.value = threadid;
	if(post_id!=null) form.post_id.value = post_id;
	if(parent!=null) form.parent.value = post_id;
	if(coupleartid!=null) {
		form.coupleartid.value = coupleartid;
		form.art_id.value = coupleartid;
	}
	if(level!=null) form.level.value = level;
	if(shownpost!=null) form.shownpost.value = shownpost;
	form.submit();
}
function openCloseTreadNavL(treeid,offset,threadid,post_id,parent,coupleartid,level,shownpost){
	var form = document.showThreadsForm;
	
	form.threadid.value = "null";
	form.post_id.value = "null";
	form.coupleartid.value = "null";
	//form.showall.value = "";
	
	if(treeid!=null) form.treeid.value = treeid;
	if(offset!=null) form.offset.value = offset;
	if(threadid!=null) form.threadid.value = threadid;
	if(post_id!=null) form.post_id.value = post_id;
	if(parent!=null) form.parent.value = post_id;
	if(coupleartid!=null) {
		form.coupleartid.value = coupleartid;
		form.art_id.value = coupleartid;
	}
	if(level!=null) form.level.value = level;
	if(shownpost!=null) form.shownpost.value = shownpost;
	form.submit();
}

function openCloseTreadWithShown(treeid,offset,threadid,post_id,parent,coupleartid,level,shownpost){
	var form = document.showThreadsForm10;
	form.threadid.value = "null";
	form.post_id.value = "null";
	form.coupleartid.value = "null";

	if(treeid!=null) form.treeid.value = treeid;
	if(offset!=null) form.offset.value = offset;
	if(threadid!=null) { 
		form.threadid.value = threadid;
		form.shownthread.value = threadid;
	}
	if(post_id!=null) form.post_id.value = post_id;
	if(parent!=null) form.post_id.value = parent;
	if(coupleartid!=null) {
		form.coupleartid.value = coupleartid;
		form.art_id.value = coupleartid;
	}
	if(level!=null) form.level.value = level;
	if(shownpost!=null) form.shownpost.value = shownpost;
	form.submit();
}

function openCloseTreadLatest(treeid,offset,threadid,post_id,parent,coupleartid,level,shownpost){
	var form = document.showThreadsFormLatest;
	form.threadid.value = "null";
	form.post_id.value = "null";
	form.coupleartid.value = "null";

	if(treeid!=null) form.treeid.value = treeid;
	if(offset!=null) form.offset.value = offset;
	if(threadid!=null) { 
		form.threadid.value = threadid;
		form.shownthread.value = threadid;
	}
	if(post_id!=null) form.post_id.value = post_id;
	if(parent!=null) form.post_id.value = parent;
	if(coupleartid!=null) {
		form.coupleartid.value = coupleartid;
		form.art_id.value = coupleartid;
	}
	if(level!=null) form.level.value = level;
	if(shownpost!=null) form.shownpost.value = shownpost;
	
	form.submit();
}

function openDebate(treeid,inst_id){
	var form = document.showThreadsForm10;
	form.threadid.value = "null";
	form.post_id.value = "null";
	form.coupleartid.value = "null";

	if(treeid!=null) form.treeid.value = treeid;
	if(inst_id!=null) form.inst_id.value = inst_id;
	form.submit();
}

function openDebateD10(treeid,inst_id){
	var form = document.showThreadsForm10;
	form.threadid.value = "null";
	form.post_id.value = "null";
	form.coupleartid.value = "null";
	form.showArticleData.value = 1;
	if(treeid!=null) form.treeid.value = treeid;
	if(inst_id!=null) form.inst_id.value = inst_id;
	form.submit();
}

// Autocomplete
var urlbase = "/services/debat/autocomplete.php?limit=10&";
var xmlHttp;
var xmlDoc;
var key="";
var posInDiv=-1;
var stockids=new Array();
var stocknames=new Array();
var lastMarkedStock=0;

function colorChange(highlight,pos){
	posInDiv=pos; 
	stockid=stockids[pos];
	if (lastMarkedStock && lastMarkedStock!=stockid){
		document.getElementById(lastMarkedStock).style.backgroundColor="white";
	}
	if (highlight){
		document.getElementById(stockid).style.backgroundColor="lightblue";
		document.getElementById(stockid).cursor="pointer";
		lastMarkedStock=stockid;
	}	else {
		document.getElementById(stockid).style.backgroundColor="white";
		lastMarkedStock=0;
	}
}

function handleNavKeys(keyevt){
	if (keyevt==40 && posInDiv<stockids.length-1){
		posInDiv++;
	} else if(keyevt==38 && posInDiv>=0 && posInDiv){
		posInDiv--;
	}
	colorChange(true,posInDiv);
}

function chooseThis(stockname, stockid){
	document.submitPost.searchinput.value=stockname;
	document.submitPost.instid.value=stockid;
	document.getElementById('stock').innerHTML = "";		
}

function autocomp(str,keyevt){
	if (keyevt>=37 && keyevt<=40){
		handleNavKeys(keyevt);
		return false;
	} else if (keyevt==13 && posInDiv>=0){
		chooseThis(stocknames[posInDiv],stockids[posInDiv]);
		return false;
	} else {
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
  	
		url=urlbase+"search="+str;
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function stateChanged(){ 
	posInDiv=-1;
	stockids=new Array();
	stocknames=new Array();
	lastMarkedStock=0;
	if (xmlHttp.readyState==4){
		xmlDoc=xmlHttp.responseXML;
		if(!xmlDoc || xmlDoc==undefined) return new Array();
		var elements=xmlDoc.getElementsByTagName('result');
		if(!elements || !elements.length || elements.length==undefined) return new Array();
		if (elements.length){
			var stocklist='<div style="background-color:#FFFFFF; padding:1px; line-height:14px; border: 1px solid #000000;">';
			for (i=0; i<elements.length; i++){
				stockname = elements[i].getAttribute('name');
				stockid = elements[i].getAttribute('id');
				stockids[stockids.length]=stockid;
				stocknames[stocknames.length]=stockname;
				pos = stockname.toLowerCase().indexOf(key.toLowerCase());
				if (pos>=0){
					stocklist+='<div id="'+stockid+'" onMouseOver="colorChange(true,'+i+');" onMouseOut="colorChange(false,'+i+');" onClick="chooseThis(\''+stockname+'\',\''+stockid+'\');">'+
											stockname.substr(0,pos)+
											"<b>"+stockname.substr(pos,key.length)+"</b>"+
											stockname.substr(key.length+pos,stockname.length-(key.length+pos))+
											'</div>'; 
				} else {
					stocklist+='<div id="'+stockid+'" onMouseOver="colorChange(true,'+i+');" onMouseOut="colorChange(false,'+i+');" onClick="chooseThis(stockname,stockid);">'+
											stockname+
											'</div>'; 
				}
			}
			stocklist+='</div>';
			document.getElementById('stock').innerHTML = stocklist;
		}
	} else {
		document.getElementById('stock').innerHTML = "";		
	}
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try {
  	// Firefox, Opera 8.0+, Safari
  	xmlHttp=new XMLHttpRequest();
  } catch (e){
  	// Internet Explorer
  	try {
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  	} catch (e) {
    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	}
	return xmlHttp;
}