var showDivId = false;
var comElement = false;

function setComClass(setdiv) {
	setComC = document.getElementById(setdiv);
	setComC.className = "comments";
}

function hideId(str) {

	try {
		if ( document.getElementById('c' + str) ) {
			cArea = document.getElementById('c' + str);
			while( cArea.hasChildNodes() ) {
				cArea.removeChild( cArea.lastChild ); 
			}
		}
	}
	catch (e)
	{ }

	maxminDiv = document.getElementById('c' + str);
	maxminDiv.className = "commentsE";
	//hideMe = document.getElementById('hide' + str);	
	//hideMe.blur();
}

function showId(str) {
	maxminDiv = document.getElementById('c' + str);
	showMe = document.getElementById('show' + str);
	//showMe.blur();
	showDivId = str;
	//getCommentSql();
	comstr = '?task=showcom&tid=' + showDivId;
	sqlQuery('wlib-js/sqlquery3.php', comstr, handleHttpGetCom, 'GET', 'commentsS');
	//sqlQuery('wlib-js/test.php', comstr, handleHttpGetCom, 'GET', 'commentsS');

	// start incredibly silly hack to force ie7 into displaying the maxminDiv
	showMe.focus();
	showMe.blur();
	setComClass(maxminDiv.id);
	// end ie7 compatibility sillyness
}

function handleHttpGetCom()
{
	// if there hasn't been any errors
	if (sqlHttp['commentsS'].readyState == NORMAL_STATE) {

		comResults = '';
		// split by the divider |
		comResults = sqlHttp['commentsS'].responseText.split('#');
		
		comlength = comResults.length - 2;
		comlength2 = comResults[0];

		hasCom = true;
		
		showComments();
	}
}

function showComments() {

	try {
		if ( document.getElementById('c' + showDivId) ) {
			cArea = document.getElementById('c' + showDivId);
			while( cArea.hasChildNodes() ) {
				cArea.removeChild( cArea.lastChild ); 
			}
		}
	}
	catch (e)
	{ }
		
	if (hasCom == true) {

		//commentBody = document.getElementById('body' + showDivId);
		bCom1 = document.getElementById('c' + showDivId);
		bCom1.className = "comments";
		
		bComS1 = document.createElement('span');
			bComS1.id = 'hide' + showDivId;
			bComS1.className = 'comLink';
			bComS1.innerHTML = '<a href="javascript:hideId(\'' + showDivId + '\');"><span>hide comments<span></a>';
		bCom1.appendChild(bComS1);
	
		bCom1br1 = document.createElement('br');
		bCom1br2 = document.createElement('br');
		bCom1.appendChild(bCom1br1);
		bCom1.appendChild(bCom1br2);
		//bCom1.innerHTML = bCom1 + '<br /><br />';
		
		if (comlength2 <= 0) {
	
			drawComPost();
	
			bCom1.appendChild(comPost);
	
		}
		else {
			for (var c = 1; c <= comlength; c = c + 5) {
				sql1 = comResults[c];
				sql2 = comResults[c+1];
				sql3 = comResults[c+2];
				sql4 = comResults[c+3];
				sql5 = comResults[c+4];
			
				bComDiv = document.createElement('div');
				bComDiv.className = 'commentBox';
				//bComDiv.id = 'comment' + sql2 + '.' + sql1;
					bComDivInfo = document.createElement('div');
					bComDivInfo.className = 'commentInfo';
					bComDivInfo.innerHTML = '<span>' + sql4 + '</span>' + sql3;
				bComDiv.appendChild(bComDivInfo);
				
					bComDivText = document.createElement('div');
					bComDivText.className = 'commentText';
					bComDivText.innerHTML = sql5;
				bComDiv.appendChild(bComDivText);
			
				//br1 = document.createElement('br');
				//bComDiv.appendChild(br1);
				bCom1.appendChild(bComDiv);
				br2 = document.createElement('br');
				bCom1.appendChild(br2);
			}

			drawComPost();
	
			bCom1.appendChild(comPost);
	
			commentElement = bCom1;
		}
		//window.resizeBy(-1,0);
		//window.resizeBy(1,0);
	}
}
