
if(document.getElementById("perk_box")) {
	document.getElementById("perk_box").style.left= (findX(document.getElementById("openPopup"))-130)+"px"; 
	document.getElementById("perk_box").style.top = (findY(document.getElementById("openPopup"))+18)+"px";	
}	
function findX(obj){
	var x=0,y=0;
	while (obj!=null){
		x+=obj.offsetLeft-obj.scrollLeft;
		y+=obj.offsetTop-obj.scrollTop;
		obj=obj.offsetParent;
	}
	return x;
}
function findY(obj){
	var x=0,y=0;
	while (obj!=null){
		x+=obj.offsetLeft-obj.scrollLeft;
		y+=obj.offsetTop-obj.scrollTop;
		obj=obj.offsetParent;
	}
	return y;
}
function toggleBox() { 
	//perk = document.getElementById("perk_box");
		if($("perk_box").style.display=="block") 
			$("perk_box").style.display="none";
		else	
			$("perk_box").style.display="block";	
}

function setNotExpired(threadid) {
	if(confirm("Do you want to set the thread UNEXPIRED?")) {
		location.href="http://"+window.location.hostname+"/showthread.php?threadid="+threadid+"&unexp=1";
	}
}
function toggle_starred(tid) { 
	
	if ($('starredicon_'+tid).src=='http://'+window.location.hostname+'/images/icon_starred_on.gif') {
		$('starredicon_'+tid).src = '../images/icon_starred_off.gif';
		xmlhttpPost("http://"+window.location.hostname+"/dshowthread/controller.php?todo=12&tid="+tid,"test","test");
	} else if($('starredicon_'+tid).src=='http://'+window.location.hostname+'/images/icon_starred_off.gif') {
		$('starredicon_'+tid).src = '../images/icon_starred_on.gif';
		xmlhttpPost("http://"+window.location.hostname+"/dshowthread/controller.php?todo=12&tid="+tid,"test","test");
	} else {
		$('starredicon_'+tid).src = '../images/icon_starred_off.gif';
	}
}
function menuClicked(todo, threadid, userid) {
	//alert(todo+"---"+threadid);
	if(todo==1) 
		location.href="http://del.icio.us/post?url=http://"+window.location.hostname+"/showthread.php?threadid="+threadid;
	else if(todo==2) 
		location.href="http://digg.com/submit?phase=2&url=http://"+window.location.hostname+"/showthread.php?threadid="+threadid;
	else if(todo==4) {
		if(confirm("Do you want to add this thread to newsletter?")) {
			xmlhttpPost("http://"+window.location.hostname+"/dshowthread/controller.php?todo=add&threadid="+threadid,"test","test");
			alert("The thread has been added to newsletter.");
		}
	}	
	else if(todo==3) {
		if(userid==0) {
			alert("Please login first.");
			return;
		}
			xmlhttpPost("http://"+window.location.hostname+"/dshowthread/controller.php?todo=basket&threadid="+threadid,"test","OUTBOXNUMBER");
			alert("The thread has been added to your outbox.");		
	}	
	else if(todo==5) 
		location.href="http://"+window.location.hostname+"/newsletter.php";
	else if(todo==7) 
		location.href="http://"+window.location.hostname+"/forum/postings.php?action=openclosethread&threadid="+threadid;
	else if(todo==8) 
		location.href="http://"+window.location.hostname+"/forum/postings.php?action=move&threadid="+threadid;
	else if(todo==9) 
		location.href="http://"+window.location.hostname+"/forum/postings.php?action=editthread&threadid="+threadid;
	else if(todo==10) 
		location.href="http://"+window.location.hostname+"/forum/postings.php?action=deletethread&threadid="+threadid;
	else if(todo==11) 
		location.href="http://"+window.location.hostname+"/forum/postings.php?action=merge&threadid="+threadid;
	else if(todo==12) 
		location.href="http://"+window.location.hostname+"/forum/postings.php?action=split&threadid="+threadid;
	else if(todo==13) 
		location.href="http://"+window.location.hostname+"/forum/postings.php?action=stick&threadid="+threadid;	
	else if(todo==14)	
		location.href="http://"+window.location.hostname+"/forum/member2.php?s=&action=addsubscription&threadid="+threadid;
	else if(todo==15)	
		location.href="http://"+window.location.hostname+"/linx.php?tid="+threadid+"&autooff=1";
	return;				
}
function content(threadid) {
	obj=document.getElementById("thread_content");
	//obj2=document.getElementById("hiddenmsg");
	if(obj.style.display=="block") {
		obj.style.display="none";
		//obj2.style.display="block";
		//if(document.getElementById("wiki_"+threadid))
		//	document.getElementById("wiki_"+threadid).style.display="none";
		document.getElementById("contentButton").innerHTML="<font style='color:#666;'>Contents are hidden - </font><a style='cursor:pointer;color:#003399;'>View Content</a>";
		//document.getElementById("first_post").style.backgroundColor="#efefef";
		//if(document.getElementById("perk_box"))
		//document.getElementById("perk_box").style.display="none";
	}
	else {
		obj.style.display="block";
		//obj2.style.display="none";
		//wikiRead(threadid);
		//if(document.getElementById("wiki_"+threadid)) {
		//	document.getElementById("wiki_"+threadid).style.display="block";
		//}
		document.getElementById("contentButton").innerHTML="<a style='cursor:pointer;color:#003399;'>Hide Content</a>";
		//document.getElementById("first_post").style.backgroundColor="#efefef";
		//if(document.getElementById("perk_box"))
		//document.getElementById("perk_box").style.display="block";
	}
}

function Show_Box() {
	obj=document.getElementById("submitreview2");
	if (obj.style.display == "none") {
		obj.style.display = "";
	} else {
		obj.style.display = "none";
	}
}

function quickTip(userid,postid) {
	xmlhttpPost("http://"+window.location.hostname+"/dshowthread/controller.php?todo=tip&userid="+userid+"&postid="+postid,"test","tips_"+postid);
}
function xmlhttpPost(strURL,formID,dispID) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	//strURL="http://"+window.location.hostname+""+strURL;
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
		//self.xmlHttpReq.overrideMimeType("text/xml");
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open("POST", strURL, true);
	self.xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			updatepage(self.xmlHttpReq.responseText,dispID);
		}
	}
	self.xmlHttpReq.send(getquerystring(formID));
}

function getquerystring(formID) {
	var qstr= "";
	obj = document.getElementById(formID);
	for(i=0;i<obj.length;i++) {
		qstr+=obj[i].name+"="+escape(obj[i].value)+"&";
	}
	return qstr;
}
function updatepage(str,dispID){
	document.getElementById(dispID).innerHTML = str;
}

// wiki

var controller="http://"+window.location.hostname+"/dshowthread/controller.php";
var currentThread=0;
function wikiWrite(threadid,login,posts) {
	currentThread = threadid;
	
	//if($('wiki_'+threadid).style.display == 'block') {
		//$('wiki_'+threadid).innerHTML = 'TRIGGERED';
		//$('wiki_'+threadid).style.display = 'none';
		//return;
	//}
	$('wiki_'+threadid).style.display = 'block';
	if(login==0) {
		//alert('Please login first.');
		$('wiki_'+threadid).innerHTML = '<BR><font style="color:#cc0000;font-size:9pt;">Please log in first to write a wiki</font>';
		return;
	}
	if(posts<100) {
		//alert('You need to have 100 posts.');
		$('wiki_'+threadid).innerHTML = '<BR><font style="color:#cc0000;font-size:9pt;">You need to have 100 posts or more to write a wiki</font>';
		return;		
	}
	$('wiki_'+threadid).innerHTML = 
			'<form name="form_'+threadid+'" id="form_'+threadid+'" onsubmit="return formAjax('+threadid+')">'+
			'<b></b><input type="hidden" name="threadid" value="'+threadid+'">'+
			'<textarea rows="15" cols="43" name="wiki_content" id="content_'+threadid+'" style="border:1px dashed #999;color:#666;font-size:8pt;padding:6px;background-color:#F6EBA3;" onclick="cleanTA('+threadid+');">'+
			'Write a concise summary to allow readers to preview the content.'+
			'</textarea><br/>'+
			'<input type="submit" value="Submit" style="border:1px solid #666;font:size:7pt;color:#666;margin-top:3px;">'+	
			'</form>';	
	$('content_'+threadid).focus();		

}
function cleanTA(threadid) {
	$('wiki_'+threadid).innerHTML = 
			'<form name="form_'+threadid+'" id="form_'+threadid+'" onsubmit="return formAjax('+threadid+')">'+
			'<b></b><input type="hidden" name="threadid" value="'+threadid+'">'+
			'<textarea rows="15" cols="43" name="wiki_content" id="content_'+threadid+'" style="border:1px dashed #999;color:#666;font-size:8pt;padding:6px;background-color:#F6EBA3;">'+
			'</textarea><br/>'+
			'<input type="submit" value="Submit" style="border:1px solid #666;font:size:7pt;color:#666;margin-top:3px;">'+	
			'</form>';	
	$('content_'+threadid).focus();		
}

function formAjax(threadid) {
	currentThread = threadid;
	var myAjax = new Ajax.Request( 
									controller+"?todo=7", 
									{ 
										method: 'post', 
										parameters: Form.serialize('form_'+threadid), 
										onComplete: WikiDisplay 
									}
								);
	return false;								
}	

function WikiDisplay(originalRequest) {
	if($('wiki_image_'+currentThread)) {
		if($('wiki_image_'+currentThread).src=="http://"+window.location.hostname+"/ddeals/icon_wwoff.gif") 
			$('wiki_image_'+currentThread).src="http://"+window.location.hostname+"/ddeals/icon_wwnew.gif";	
		else if($('wiki_image_'+currentThread).src=="http://"+window.location.hostname+"/ddeals/icon_wwnew.gif") 
			$('wiki_image_'+currentThread).src="http://"+window.location.hostname+"/ddeals/icon_wwedit.gif";
		else if($('wiki_image_'+currentThread).src=="http://"+window.location.hostname+"/ddeals/icon_ww.gif")
			$('wiki_image_'+currentThread).src="http://"+window.location.hostname+"/ddeals/icon_wwedit.gif";				
		$('wiki_image_'+currentThread).onclick = ""+wikiRead2 ;
	}
	$('wiki_'+currentThread).innerHTML = ""+originalRequest.responseText;
}
function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}

function WikiContent(originalRequest) {
	$('wiki_'+currentThread).innerHTML = stripslashes(originalRequest.responseText);
	$('wiki_'+currentThread).style.display="block";
}

function wikiRead2() {

	
	if($('wiki_'+currentThread).style.display=="block") {
		$('wiki_'+currentThread).style.display="none";
		//$('wiki_'+currentThread).innerHTML = '';
		return;
	}
	var myAjax = new Ajax.Request( 
									controller+"?todo=8", 
									{ 
										method: 'get', 
										parameters: "tid="+currentThread, 
										onComplete: WikiContent 
									}
								);	
}

function wikiRead(threadid) {

	currentThread = threadid;
	
	if($('wiki_'+threadid).style.display=="block") {
		$('wiki_'+threadid).style.display="none";
		//$('wiki_'+currentThread).innerHTML = 'WIKIREAD TRIGGERED';
		return;
	}
	var myAjax = new Ajax.Request( 
									controller+"?todo=8", 
									{ 
										method: 'get', 
										parameters: "tid="+threadid, 
										onComplete: WikiContent 
									}
								);	
}
function wikiEdit(threadid) {

	currentThread = threadid;
	$('wiki_'+currentThread).style.display="block";
	var myAjax = new Ajax.Request( 
									controller+"?todo=9", 
									{ 
										method: 'get', 
										parameters: "tid="+currentThread, 
										onComplete: WikiContent 
									}
								);		
	
	
}