//	
//
/**
* phpBB3 2010
*
* Thank 2 Unhide MOD By Threetrees (dreitrees@yahoo.com)
*
* Feb 2010
**/
//
//
var xmlhttp;
var obj;
var post;
var orgLink;
var link;
var thankedUser;
var	postThankNum;
var thankerName;
var postId;
var postIdNum;
var postBody;

var generating;
var thnxlist;
var clicker;
var thisObj;

var postUpdate = 0;
var viewList = 0;


function loadXMLPage(url)
	{
		xmlhttp=null;
		if (window.XMLHttpRequest)
		{
		xmlhttp=new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlhttp!=null)
		{
		xmlhttp.onreadystatechange=state_update;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
		}
		else
		{
		alert("Not supported by your Browser!");
		}
	}

function state_update()
	{
		if (xmlhttp.readyState==4)
		{// 4 = "loaded"
			if (xmlhttp.status==200)
			{// 200 = OK
				if (postUpdate == 1)
				{
	
					if ((document.getElementById('th'+postIdNum) && document.getElementById('ck'+postIdNum)) != null)
					{
						document.getElementById('th'+postIdNum).style.display = 'none';
						document.getElementById('lstxt'+postIdNum).style.display = 'none';
						document.getElementById('lsload'+postIdNum).style.display = 'block';						
						document.getElementById('ck'+postIdNum).innerHTML = 'Show users';
					}
	
					postBody.style.opacity = 1; 
					postBody.style.filter = 'alpha(opacity=100)';
					post.style.height = 'auto';					
					post.innerHTML = xmlhttp.responseText;
					document.getElementById('postload'+postIdNum).style.display = 'none';
					post.style.display = 'block';
					postUpdate = 0;
				}
				
				else if (viewList == 1)
				{					
					document.getElementById('lstxt'+postId).innerHTML = xmlhttp.responseText;
					document.getElementById('lsload'+postId).style.display = 'none';
					document.getElementById('lstxt'+postId).style.display = 'block';
					thisObj.innerHTML = 'Hide users';
					document.getElementById(generating).style.display = 'none'; 
					thisObj.style.display = "inline"; 
					viewList = 0;
				}		
	
			else if (xmlhttp.responseText == 'User Thanked!')
			{
			orgLink = orgLink.replace('thanks.php', 'viewtopic.php');
			orgLink = orgLink.replace(/mode=[a-z]+/, 'mode=jparse');
			
			postUpdate = 1;
			loadXMLPage(orgLink);
	
			obj.parentNode.className = "unthank-icon";
			obj.title = "Unthank";
	
				if(document.getElementById('tls'+postIdNum)!= null)
				{

					postThankNum = document.getElementById('tls'+postIdNum).innerHTML.match(/\[[0-9]+\]/);
					postThankNum = postThankNum.toString(postThankNum).match(/[0-9]+/);
			
					thank = document.getElementsByTagName('dd');
				
				for (i=0; i< thank.length; i++)
				{
					if (thank[i].className.match('thanked_'+thankedUser))
					{	
					
					thankedNum = thank[i].innerHTML.match(/[0-9]+ time/);					
					thankedNum = thankedNum.toString(thankedNum).match(/[0-9]+/);
					thankedPostsNum = thank[i].innerHTML.match(/[0-9]+ post/);					
					thankedPostsNum = thankedPostsNum.toString(thankedPostsNum).match(/[0-9]+/);
					
					
					if(thankedNum == 0)
						{
							thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ [a-z]+ in [0-9]+ [a-z]+/, '1 time in 1 post');													
						}
						else
						{
							thankedNum++;
							thankedPostsNum++;
							
							if(postThankNum == 0)
								{								
									thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ [a-z]+ in [0-9]+ [a-z]+/,thankedNum+' times in '+thankedPostsNum+' posts');																
								}
								else
								{
									thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ time/,thankedNum+' time');								
								}						
						}													
					
					}
				
					if (thank[i].className.match('thanks_'+thankerName))
				
					{
						thanksNum = thank[i].innerHTML.match(/[0-9]+/);
						thanksNum++;
						thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+/,thanksNum);				
					}
				}
			
				postThankNum++;
			
				document.getElementById('tls'+postIdNum).innerHTML = document.getElementById('tls'+postIdNum).innerHTML.replace(/\[[0-9]+\]/,'['+postThankNum+']');
				
				if (document.getElementById('tls'+postIdNum).style.display == 'none')
					{
						document.getElementById('tls'+postIdNum).style.display = 'block';
					}
				}
				
		}
		else if (xmlhttp.responseText == 'User Unthanked!')
		{
			
			orgLink = orgLink.replace('thanks.php', 'viewtopic.php');
			orgLink = orgLink.replace(/mode=[a-z]+/, 'mode=jparse');
			
			postUpdate = 1;
			loadXMLPage(orgLink);
			
			obj.parentNode.className = "thanks-icon";
			obj.title = "Thanks";
	
			if(document.getElementById('tls'+postIdNum)!= null)
			{
				postThankNum = document.getElementById('tls'+postIdNum).innerHTML.match(/\[[0-9]+\]/);
				postThankNum = postThankNum.toString(postThankNum).match(/[0-9]+/);
				
				thank = document.getElementsByTagName('dd');
			
				for (i=0; i< thank.length; i++)
				{
					if (thank[i].className.match('thanked_'+thankedUser))
					{						
						
						thankedNum = thank[i].innerHTML.match(/[0-9]+ time/);											
						thankedNum = thankedNum.toString(thankedNum).match(/[0-9]+/);
						thankedPostsNum = thank[i].innerHTML.match(/[0-9]+ post/);					
						thankedPostsNum = thankedPostsNum.toString(thankedPostsNum).match(/[0-9]+/);
					
						if (thankedNum - 1 > 1)
						{
							thankedNum--;
							thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ time/,thankedNum+' time');
						}
						else if (thankedNum - 1 == 1)
						{
							thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ times/,'1 time');
						}
						else
						{
							thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ time/,'0 times');
						}
						
						if(postThankNum - 1 == 0)
							{
								
								if(thankedPostsNum - 1 > 1) 
									{
										thankedPostsNum--;
										thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ post/,thankedPostsNum+' post');
									}
								else if(thankedPostsNum - 1 == 1)
									{
										thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ posts/,'1 post');									
									}
								else
									{
										thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+ post/,'0 posts');
									}							
							}
						
					
					}
				
					if (thank[i].className.match('thanks_'+thankerName))
				
					{
						thanksNum = thank[i].innerHTML.match(/[0-9]+/);
						if (thanksNum - 1 >= 1) 
						{thanksNum--;}
						else 
						{thanksNum = 0;}
						thank[i].innerHTML = thank[i].innerHTML.replace(/[0-9]+/,thanksNum);				
					}
				}
					
			if (postThankNum - 1 >= 1)
			{
				postThankNum--;
				document.getElementById('tls'+postIdNum).innerHTML = document.getElementById('tls'+postIdNum).innerHTML.replace(/\[[0-9]+\]/,'['+postThankNum+']');				
			}
			else
			{										
				document.getElementById('tls'+postIdNum).innerHTML = document.getElementById('tls'+postIdNum).innerHTML.replace(/\[[0-9]+\]/,'[0]')
				document.getElementById('tls'+postIdNum).style.display = 'none';					
			}
				
			}
		
			}
		
			else
			{
				alert(xmlhttp.responseText);
				postBody.style.opacity = 1; 
				postBody.style.filter = 'alpha(opacity=100)';	
	
				post.style.height = 'auto';
				document.getElementById('postload'+postIdNum).style.display = 'none';
				post.style.display = 'block';
			}
    }
  else
    {
	
		alert("Cannot Connect to Server!");
	
	
	if (document.getElementById(thnxlist) != null)
		{
			document.getElementById(thnxlist).style.display = 'none';
		}
	
	if (postBody != null)
		{
			postBody.style.opacity = 1; 
			postBody.style.filter = 'alpha(opacity=100)';	
			post.style.height = 'auto';						
			document.getElementById('postload'+postIdNum).style.display = 'none';
			post.style.display = 'block';
		}	
    }
  }
}


function thankUser(thankLink,thankedName,userName,postId,thisPassedObj) 
	{	

				obj = thisPassedObj;				
				link = orgLink = thankLink;
				thankedUser = thankedName;				
				thankerName = userName;
				postIdNum = postId;
				
				postBody = obj.parentNode.parentNode.parentNode;				
				postBody.style.opacity = 0.5; 
				postBody.style.filter = 'alpha(opacity=50)';
								
				post = document.getElementById('post'+postIdNum);
				
				if(obj.parentNode.className == "thanks-icon")
				{					

					if(confirm('Thank the user for this post?'))
					{
						
						link = link.replace('mode=unthank', 'mode=thanks');
						
						link =link+'&accmod=js';

						postBody.style.opacity = 1; 
						postBody.style.filter = 'alpha(opacity=100)';
						document.getElementById('postload'+postId).style.height = post.offsetHeight+'px';				
						post.style.display = 'none'; 
						document.getElementById('postload'+postId).style.display = 'block';
						loadXMLPage(link);						
					}
					else
					{
						postBody.style.opacity = 1; 
						postBody.style.filter = 'alpha(opacity=100)';
					}
				}
				
				else if(obj.parentNode.className == "unthank-icon")
				{		
					
					
					while(true)
					{
					
					reason = prompt('Reason to unthank this post?\r\n(Note: Post author will be notified!)','');
					if(reason == '')
					{alert('You Must Provide an Appropriate Reason!');}
					else if(reason != null && reason.length > 96)
					{alert('Reason is too long!');}
					else 
						{
						postBody.style.opacity = 1; 
						postBody.style.filter = 'alpha(opacity=100)';
						break;
						}
					}
					
					if(reason != '' && reason != null)
					{
						
						link = link.replace('mode=thanks', 'mode=unthank');
						
						link =link+'&accmod=js'+'&unthanked='+thankedUser+'&reason='+escape(reason);
						
						
						postBody.style.opacity = 1; 
						postBody.style.filter = 'alpha(opacity=100)';
						post.style.height = post.offsetHeight+'px';						
						document.getElementById('postload'+postId).style.height = post.offsetHeight+'px';				
						post.style.display = 'none'; 
						document.getElementById('postload'+postId).style.display = 'block';
						loadXMLPage(link);						
						
					}
					
				}
				
				else 
				return false;

	}

function showHide(passedObj,gn,th,listLink) 
{
			thisObj = passedObj;
			thnxlist = th;
			generating = gn;

	if (thisObj.innerHTML == 'Show users' && document.getElementById(thnxlist).style.display == "none")
		{ 
		
			
			thisObj.style.display = "none"; 
			document.getElementById(generating).style.display = "inline";			
			document.getElementById(thnxlist).style.display = "block";
			postId = generating.substr(2);
			request = listLink+'&mode=list&accmod=js';
			viewList = 1;
			loadXMLPage(request);

		}
	else
		{ 
			thisObj.innerHTML = 'Show users'; 
			document.getElementById(thnxlist).style.display = "none";
			document.getElementById('lstxt'+postId).style.display = 'none';
			document.getElementById('lsload'+postId).style.display = 'block';
			
			
		}				
}
