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 getElementsByClassName(classname) {
	if (document.getElementsByTagName) {
		var els = document.getElementsByTagName("*");
		var c = new RegExp('/b^|' + classname + '|$/b');
		finale = new Array();
		var n=0;
		for (var i=0; i < els.length; i++) {
			if (els[i].className) {
				if(c.test(els[i].className)) {
					finale[n] = els[i];
					n++;
				}
			}
		}
		return finale;
	}
	return false;
}

function poprequest(url){
	var newwindow;
	newwindow=window.open(url,'name','height=500,width=300,left=40,top=40,toolbar=no,menubar=no,directories=no,location=no,scrollbars=yes,status=no,resizable=yes,fullscreen=no');

	if (window.focus) {
		newwindow.focus()
	}
}

function getXmlHttpRequestObject() {
	if (document.all) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

var xmlhttp = getXmlHttpRequestObject();

function load(id,url,post) {
	msgdiv = document.getElementById(id);
	if (msgdiv) {
		msgdiv.innerHTML = 'Loading...';
	}

	var method = post ? 'POST':'GET';
	xmlhttp.open(method, url,true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//getChatRequest();

			// deal with response
			var statustxt = '';
			if(xmlhttp.status == 200) {
				statustxt = xmlhttp.responseText;
			} else if(xmlhttp.status ==500) {
				//statustxt = 'Error ';
			} else {
				//statustxt = 'Unknown Response Code '+xmlhttp.status;
			}
			if (msgdiv) {
				msgdiv.innerHTML = statustxt;
				if (match = /<script[^>]*>(.*)<\/script>/gi.exec(statustxt)) {
					//alert(match[1]);
					eval(match[1]);
				}
			}
		}
	}
	if (post) {
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.send(post);
	} else {
		xmlhttp.send(null);
	}
}

function load_textarea(id,url,post) {
	msgdiv = document.getElementById(id);
	if (msgdiv) {
		//msgdiv.innerHTML = 'Loading...';
	}
	if (document.all) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlhttp = new XMLHttpRequest();
	}

	xmlhttps.push(xmlhttp);
	var method = post ? 'POST':'GET';
	xmlhttp.open(method, url,true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			// deal with response
			var statustxt = '';
			if(xmlhttp.status == 200) {
				statustxt = xmlhttp.responseText;
			} else if(xmlhttp.status ==500) {
				//statustxt = 'Error ';
			} else {
				//statustxt = 'Unknown Response Code '+xmlhttp.status;
			}
			if (msgdiv) {
				msgdiv.value = statustxt;
				//window.scrollTo(0,0);
				if (match = /<script[^>]*>(.*)<\/script>/gi.exec(statustxt)) {
					//alert(match[1]);
					eval(match[1]);
				}
				//alert(url);
			}
		}
	}
	if (post) {
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.send(post);
	} else {
		xmlhttp.send(null)
	}
}

function winClose() {
	if (self.opener) {
		self.opener.window.focus();
	}
	window.close();
}

function moveOn() {
	if (self.opener) {
		self.setTimeout('winClose()', 5000)  
	}
}

function loginregistersubmit(form,str) {
	if (str == 'login')	{
		form.action = '/login.php';
	} 
	else{	
		form.action = '/register.php';
	}
	form.submit()
}

function toggleAnswer(atag,divid,textid,msg1,msg2) {
	div = document.getElementById(divid);
	texta = document.getElementById(textid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'answervisible' ? 'answerhidden' : 'answervisible';
		if (curclass == 'answervisible') {
			var els = getElementsByClassName('answervisible');
			if (els) {
				for (el in els) {
					el = els[el];
					el.className = 'answerhidden';
				}
			}
			div.className = curclass;
			texta.focus();
			
			oTop = div.offsetTop;
			bSize = getBrowserSize();
			sPos = getScrollXY();
			oPos = oTop - sPos.y;
			//alert('oTop = ' + oTop);
			//alert('sPos = ' + sPos.y);
			//alert('bSize = ' + bSize.y);
			if(bSize.y - oPos < 266){
				window.scrollTo(0,oTop - 200);
			}
		}
		else {
			texta.value='';
			div.className = curclass;
		}
	}
}

function toggleHighLight(divid) {
	div = document.getElementById(divid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'highlightvisible' ? 'highlighthidden' : 'highlightvisible';
		if (curclass == 'highlightvisible') {
			div.className = curclass;
		}
		else {
			div.className = curclass;
		}
	}
}

function toggleSmallHighLight(divid) {
	div = document.getElementById(divid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'shighlightvisible' ? 'shighlighthidden' : 'shighlightvisible';
		if (curclass == 'shighlightvisible') {
			div.className = curclass;
		}
		else {
			div.className = curclass;
		}
	}
}

function toggleDebateEditor(divid,textid) {
	div = document.getElementById(divid);
	texta = document.getElementById(textid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'editdebatevisible' ? 'editdebatehidden' : 'editdebatevisible';
		if (curclass == 'editdebatevisible') {
			div.className = curclass;
			texta.focus();
		}
		else {
			div.className = curclass;
		}
	}
}

function toggleResponseEditor(divid,textid) {
	div = document.getElementById(divid);
	texta = document.getElementById(textid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'editresponsevisible' ? 'editresponsehidden' : 'editresponsevisible';
		if (curclass == 'editresponsevisible') {
			div.className = curclass;
			texta.focus();
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleCommentEditor(divid,textid) {
	div = document.getElementById(divid);
	texta = document.getElementById(textid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'editcommentvisible' ? 'editcommenthidden' : 'editcommentvisible';
		if (curclass == 'editcommentvisible') {
			div.className = curclass;
			texta.focus();
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleSearchIntLink(divid,query,ignore,listid,contentid) {
	var xmlhttp = getXmlHttpRequestObject();
	var div = document.getElementById(divid);

	if (div) {
		curclass = div.className;
		curclass = curclass == 'searchlinkvisible' ? 'searchlinkhidden' : 'searchlinkvisible';
		if (curclass == 'searchlinkvisible') {
			div.className = curclass;
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
				xmlhttp.open('GET','/res/listsearchqs.php?query=' + query + '&ignore=' + ignore,true);
				
				var related = document.getElementById(listid);
				related.innerHTML = '<span style=\"color:#ffffff\">Loading...</span>';

				xmlhttp.onreadystatechange = function(){
					if (xmlhttp.readyState == 4) {
						if (xmlhttp.status==200){
							related.innerHTML = '';
							var str = xmlhttp.responseText.split("&iexcl;");
							//alert(str);
							for(i=0; i < str.length - 1; i++) {
								var substr = str[i].split("&plusmn;");
								var relatedqs = '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin-bottom: 10px;\">';
								relatedqs += '<tr><td width=\"130\" style="color:#ffffff">';
								relatedqs += '<input type=\"radio\" id=\"radioq'+contentid+'\" name=\"radioq'+contentid+'\" value=\"'+ substr[0]+'\">&nbsp;'+ substr[0]+'</td>';
								relatedqs += '<td><textarea id=\"text'+substr[0]+'\" name=\"text'+substr[0]+'\" style=\"width:220px;height:35px;\">'+substr[1]+'</textarea></td>';
								relatedqs += '</tr>';
								relatedqs += '</table>';
								related.innerHTML += relatedqs;
							}
							//alert(related.innerHTML);
						}
						else {
							alert("Problem retrieving XML data:" + xmlhttp.statusText)
						}
					}
				}
				xmlhttp.send(null);
			}
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleIntLink(divid) {
	div = document.getElementById(divid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'intlinkvisible' ? 'intlinkhidden' : 'intlinkvisible';
		if (curclass == 'intlinkvisible') {
			div.className = curclass;
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleExtLink(divid,textid) {
	var div = document.getElementById(divid);
	var texta = document.getElementById(textid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'extlinkvisible' ? 'extlinkhidden' : 'extlinkvisible';
		if (curclass == 'extlinkvisible') {
			bSize = getBrowserSize();
			leftwidth = (bSize.x - 771)/2;
			setStyleByClass('div','extlinkvisible', 'left', leftwidth + 400);

			div.className = curclass;
			texta.focus();
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleExtLinkForArticle(divid) {
	var div = document.getElementById(divid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'articleextlinkvisible' ? 'extlinkhidden' : 'articleextlinkvisible';
		if (curclass == 'articleextlinkvisible') {
			div.className = curclass;
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleVideoLink(divid) {
	var div = document.getElementById(divid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'articleextlinkvisible' ? 'extlinkhidden' : 'articleextlinkvisible';
		if (curclass == 'articleextlinkvisible') {
			div.className = curclass;
		} 
		else {
			div.className = curclass;
		}
	}
}

function showOverlay(){
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);

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

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}

function hideOverlay(){
	var objOverlay = document.getElementById('overlay');
	objOverlay.style.display = 'none';
}

function toggleAvatarUploader(uploaderdivid,iframedivid,newuploader) {
	var udiv = document.getElementById(uploaderdivid);
	var idiv = document.getElementById(iframedivid);

	if (udiv) {
		var uploaderBgheight = 140;		
		var uploaderBgwidth = 400;

		ucurclass = udiv.className;
		ucurclass = ucurclass == 'avataruploadervisible' ? 'avataruploaderhidden' : 'avataruploadervisible';

		if (ucurclass == 'avataruploadervisible') {
			var objOverlay = document.getElementById('overlay');			
			if(!objOverlay || newuploader)
				showOverlay();

			udiv.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - uploaderBgheight) / 2) + 'px');
			udiv.style.left = (((arrayPageSize[0] - 20 - uploaderBgwidth) / 2) + 'px');
			udiv.style.display = 'block';

			udiv.className = ucurclass;
		}
		else{
			hideOverlay();
			udiv.className = ucurclass;
		}

		if (idiv) {
			var iframeheight = 620;		
			var iframewidth = 800;

			icurclass = idiv.className;
			icurclass = icurclass == 'loadervisible' ? 'loaderhidden' : 'loadervisible';

			if (icurclass == 'loadervisible') {
				showOverlay();
				idiv.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 10 - iframeheight) / 2) + 'px');
				idiv.style.left = (((arrayPageSize[0] - 10 - iframewidth) / 2) + 'px');

				idiv.style.display = 'block';
				idiv.className = icurclass;
			}
			else{
				hideOverlay();
				idiv.className = icurclass;
				if(udiv){
					hideOverlay();
					ucurclass = 'avataruploadervisible';
					udiv.className = ucurclass;
				}
			}
		}
	}
}

function toggleArticleImageUploader(uploaderdivid,iframedivid,newuploader) {
	var udiv = document.getElementById(uploaderdivid);
	var idiv = document.getElementById(iframedivid);

	if (udiv) {
		var uploaderBgheight = 140;		
		var uploaderBgwidth = 400;

		ucurclass = udiv.className;
		ucurclass = ucurclass == 'aimageuploadervisible' ? 'aimageuploaderhidden' : 'aimageuploadervisible';

		if (ucurclass == 'aimageuploadervisible') {
			var objOverlay = document.getElementById('overlay');			
			if(!objOverlay || newuploader)
				showOverlay();

			udiv.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - uploaderBgheight) / 2) + 'px');
			udiv.style.left = (((arrayPageSize[0] - 20 - uploaderBgwidth) / 2) + 'px');
			udiv.style.display = 'block';

			udiv.className = ucurclass;
		}
		else{
			hideOverlay();
			udiv.className = ucurclass;
		}

		if (idiv) {
			var iframeheight = 620;		
			var iframewidth = 800;

			icurclass = idiv.className;
			icurclass = icurclass == 'loadervisible' ? 'loaderhidden' : 'loadervisible';

			if (icurclass == 'loadervisible') {
				showOverlay();
				idiv.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 10 - iframeheight) / 2) + 'px');
				idiv.style.left = (((arrayPageSize[0] - 10 - iframewidth) / 2) + 'px');

				idiv.style.display = 'block';
				idiv.className = icurclass;
			}
			else{
				hideOverlay();
				idiv.className = icurclass;
				if(udiv){
					hideOverlay();
					ucurclass = 'aimageuploadervisible';
					udiv.className = ucurclass;
				}
			}
		}
	}
}

function toggleFileUploader(divid) {
	div = document.getElementById(divid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'imageuploadervisible' ? 'imageuploaderhidden' : 'imageuploadervisible';
		if (curclass == 'imageuploadervisible') {
			div.className = curclass;
		}
		else {
			div.className = curclass;
		}
	}
}

function toggleSearch(divid) {
	div = document.getElementById(divid);
	if (div) {
		curclass = div.className;
		curclass = curclass == 'intlinkvisible' ? 'intlinkhidden' : 'intlinkvisible';
		if (curclass == 'intlinkvisible') {
			div.className = curclass;
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleSearchQuestions(divid,query,ignore,listid,contentid) {
	var xmlhttp = getXmlHttpRequestObject();
	var div = document.getElementById(divid);

	if (div) {
		curclass = div.className;
		curclass = curclass == 'searchlinkvisible' ? 'searchlinkhidden' : 'searchlinkvisible';
		if (curclass == 'searchlinkvisible') {
			div.className = curclass;
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
				xmlhttp.open('GET','/res/listsearchquestions.php?query=' + query + '&ignore=' + ignore,true);
				
				var related = document.getElementById(listid);
				related.innerHTML = '<span style=\"color:#ffffff\">Loading...</span>';

				xmlhttp.onreadystatechange = function(){
					if (xmlhttp.readyState == 4) {
						if (xmlhttp.status==200){
							related.innerHTML = xmlhttp.responseText;
						}
						else {
							alert("Problem retrieving XML data:" + xmlhttp.statusText)
						}
					}
				}
				xmlhttp.send(null);
			}
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleSearchDebates(divid,type,query,listid) {
	var xmlhttp = getXmlHttpRequestObject();
	var div = document.getElementById(divid);

	if (div) {
		curclass = div.className;
		curclass = curclass == 'searchlinkvisible' ? 'searchlinkhidden' : 'searchlinkvisible';
		if (curclass == 'searchlinkvisible') {
			div.className = curclass;
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
				xmlhttp.open('GET','/res/listsearchdebates.php?type=' + type + '&query=' + query,true);
				
				var listd = document.getElementById(listid);
				listd.innerHTML = '<span style=\"color:#ffffff\">Loading...</span>';

				xmlhttp.onreadystatechange = function(){
					if (xmlhttp.readyState == 4) {
						if (xmlhttp.status==200){
							listd.innerHTML = xmlhttp.responseText;
						}
						else {
							alert("Problem retrieving XML data:" + xmlhttp.statusText)
						}
					}
				}
				xmlhttp.send(null);
			}
		} 
		else {
			div.className = curclass;
		}
	}
}

function toggleMySummary(tdid,divid) {
	td = document.getElementById(tdid);
	div = document.getElementById(divid);
	if (td && div) {
		tcurclass = td.className;
		dcurclass = div.className;
		tcurclass = tcurclass == 'profile_summary_down' ? 'profile_summary_up' : 'profile_summary_down';
		dcurclass = dcurclass == 'mysummaryhidden' ? 'mysummaryvisible' : 'mysummaryhidden';
		td.className = tcurclass;
		div.className = dcurclass;
	}
}


function toggleMyAchievement(tdid,divid) {
	td = document.getElementById(tdid);
	div = document.getElementById(divid);
	if (td && div) {
		tcurclass = td.className;
		dcurclass = div.className;
		tcurclass = tcurclass == 'profile_achievements_down' ? 'profile_achievements_up' : 'profile_achievements_down';
		dcurclass = dcurclass == 'myachievehidden' ? 'myachievevisible' : 'myachievehidden';
		td.className = tcurclass;
		div.className = dcurclass;
	}
}

function toggleMyFavourites(tdid,divid) {
	td = document.getElementById(tdid);
	div = document.getElementById(divid);
	if (td && div) {
		tcurclass = td.className;
		dcurclass = div.className;
		tcurclass = tcurclass == 'profile_favourites_down' ? 'profile_favourites_up' : 'profile_favourites_down';
		dcurclass = dcurclass == 'myfavhidden' ? 'myfavvisible' : 'myfavhidden';
		td.className = tcurclass;
		div.className = dcurclass;
	}
}

function toggleMyComments(username,tdid,divid,page) {
	td = document.getElementById(tdid);
	div = document.getElementById(divid);
	if (td && div) {
		tcurclass = td.className;
		dcurclass = div.className;
		tcurclass = tcurclass == 'profile_comments_down' ? 'profile_comments_up' : 'profile_comments_down';
		dcurclass = dcurclass == 'mycommentvisible' ? 'mycommenthidden' : 'mycommentvisible';
		if(dcurclass == 'mycommentvisible'){
			td.className = tcurclass;
			div.className = dcurclass;
			toggleAjaxMyComments(username,'ajax_mycomment',page);
		}
		else{
			td.className = tcurclass;
			div.className = dcurclass;
		}
	}
}

function toggleAjaxMyComments(username,listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listmycomments.php?username=' + username + '&page=' + page,true);
		
		var listd = document.getElementById(listid);
		//listd.innerHTML = 'Loading...';

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleMyDebates(username,tdid,divid,page) {
	td = document.getElementById(tdid);
	div = document.getElementById(divid);
	if (td && div) {
		tcurclass = td.className;
		dcurclass = div.className;
		tcurclass = tcurclass == 'profile_debates_down' ? 'profile_debates_up' : 'profile_debates_down';
		dcurclass = dcurclass == 'mydebatevisible' ? 'mydebatehidden' : 'mydebatevisible';
		if(dcurclass == 'mydebatevisible'){
			td.className = tcurclass;
			div.className = dcurclass;
			toggleAjaxMyDebates(username,'ajax_mydebate',page);
		}
		else{
			td.className = tcurclass;
			div.className = dcurclass;
		}
	}
}

function toggleAjaxMyDebates(username,listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listmydebates.php?username=' + username + '&page=' + page,true);
		
		var listd = document.getElementById(listid);
		//listd.innerHTML = 'Loading...';

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleMyResponses(username,tdid,divid,page) {
	td = document.getElementById(tdid);
	div = document.getElementById(divid);
	if (td && div) {
		tcurclass = td.className;
		dcurclass = div.className;
		tcurclass = tcurclass == 'profile_responses_down' ? 'profile_responses_up' : 'profile_responses_down';
		dcurclass = dcurclass == 'myresponsevisible' ? 'myresponsehidden' : 'myresponsevisible';
		if(dcurclass == 'myresponsevisible'){
			td.className = tcurclass;
			div.className = dcurclass;
			toggleAjaxMyResponses(username,'ajax_myresponse',page);
		}
		else{
			td.className = tcurclass;
			div.className = dcurclass;
		}
	}
}

function toggleAjaxMyResponses(username,listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listmyresponses.php?username=' + username + '&page=' + page,true);
		
		var listd = document.getElementById(listid);
		//listd.innerHTML = 'Loading...';

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleAjaxAllGroups(listid,display_by,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listallgroups.php?display_by=' + display_by + '&page=' + page,true);
		
		var listd = document.getElementById(listid);

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleAjaxNewDebates(listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listnewdebates.php?page=' + page,true);
		
		var listd = document.getElementById(listid);

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleAjaxFeaturedDebates(listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listfeatureddebates.php?page=' + page,true);
		
		var listd = document.getElementById(listid);

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleAjaxTopDebates(listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listtopdebates.php?page=' + page,true);
		
		var listd = document.getElementById(listid);

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleAjaxRandomDebates(listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listrandomdebates.php?page=' + page,true);
		
		var listd = document.getElementById(listid);

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleAjaxAllDebates(listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listalldebates.php?page=' + page,true);
		
		var listd = document.getElementById(listid);

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleAjaxAllArticles(listid,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listallarticles.php?page=' + page,true);
		
		var listd = document.getElementById(listid);

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleAjaxAllComments(listid,atitle,page) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listarticlecomments.php?title=' + atitle + '&page=' + page,true);
		
		var listd = document.getElementById(listid);

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					listd.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function makeItMyFavourite(div_id,type,resource,action) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listmyfavdebates.php?resource_type=' + type + '&resource_id=' + resource + '&action=' + action,true);
		
		var div_id = document.getElementById(div_id);
		//listd.innerHTML = 'Loading...';

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					//alert(xmlhttp.responseText);
					div_id.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function makeItRelatedDebate(div_id,articleid,debateid,action) {
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
		xmlhttp.open('GET','/res/listrelateddebates.php?resource_id=' + articleid + '&debate_id=' + debateid + '&action=' + action,true);
		
		var div_id = document.getElementById(div_id);
		//listd.innerHTML = 'Loading...';

		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status==200){
					//alert(xmlhttp.responseText);
					div_id.innerHTML = xmlhttp.responseText;
				}
				else {
					alert("Problem retrieving XML data:" + xmlhttp.statusText)
				}
			}
		}
		xmlhttp.send(null);
	}
}

function toggleLoginAndForgot(div1_id,div2_id) {
	div1 = document.getElementById(div1_id);
	div2 = document.getElementById(div2_id);
	if (div1 && div2) {
		curclass1 = div1.className;
		curclass2 = div2.className;
		curclass1 = curclass1 == 'loginformvisible' ? 'loginformhidden' : 'loginformvisible';
		curclass2 = curclass2 == 'forgotformhidden' ? 'forgotformvisible' : 'forgotformhidden';
		if (curclass1 == 'loginformvisible') {
			div1.className = curclass1;
		}
		else {
			div1.className = curclass1;
		}
		if (curclass2 == 'forgotformhidden') {
			div2.className = curclass2;
		}
		else {
			div2.className = curclass2;
		}
	}
}

function getBrowserSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
  } 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {x:myWidth, y:myHeight};
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } 
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return {x:scrOfX, y:scrOfY};
}

function setStyleByClass(t,c,p,v){
	var ie = (document.all) ? true : false;
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}

function addEngine(name,ext,cat){
	if ((typeof window.sidebar == "object") && (typeof	window.sidebar.addSearchEngine == "function"))	{
		window.sidebar.addSearchEngine(
			"http://www.forandagainst.com/inc/"+name+".src",
			"http://www.forandagainst.com/inc/"+name+"."+ext,
			name,cat);
	}
	else{
		errorMsg(name,ext,cat);
	}
}

function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	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;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function setText(textid,linknameid) {
	var ie = (document.all) ? true : false;
	var texta=document.getElementById(textid);
	var linkname=document.getElementById(linknameid);

	if (!texta) return false;
	str=new String(texta.value);

	if(ie){
		if(document.selection){ 
			var range = document.selection.createRange(); 
			var stored_range = range.duplicate(); 
			stored_range.moveToElementText( texta ); 
			stored_range.setEndPoint( 'EndToEnd', range ); 
			texta.selectionStart = stored_range.text.length - range.text.length; 
			texta.selectionEnd = texta.selectionStart + range.text.length; 
		}
		else{
			return;
		}
	}
	else{
		if (!(str && texta.selectionEnd)) {
			return;
		}
	}

	str = str.substring(texta.selectionStart,texta.selectionEnd);
	linkname.value = str;

	return true;
}
