

function hideLoad(){

	document.getElementById('load').style.display="none";
	//alert(document.getElementById('load').style.display);
}

function showLoad(obj){

	x = findPosX(obj);
	y = findPosY(obj) + 50;

	document.getElementById('load').style.left = x + 'px';
	document.getElementById('load').style.top = y + 'px';
	document.getElementById('load').style.display="block";
	
	document.getElementById('load').style.display="block";
}

// Show comments
function showComment(strURL,data,divId) {

 	   
		var xmlHttpReq = false;
    // Mozilla
    	if (window.XMLHttpRequest) {
        	xmlHttpReq = new XMLHttpRequest();
        	xmlHttpReq.overrideMimeType('text/xml');
    	}
    // IE
    	else if (window.ActiveXObject) {
        	xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    	xmlHttpReq.open('POST', strURL, true);
    	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlHttpReq.onreadystatechange = function() { 
        	if (xmlHttpReq.readyState == 4) {

				if(xmlHttpReq.responseText!=''){
					if(xmlHttpReq.responseText!='NO'){
						document.getElementById(divId).innerHTML = xmlHttpReq.responseText;
					} else if(xmlHttpReq.responseText=='NO')document.getElementById(divId).innerHTML='';
					hideLoad();
				}
				else {hideLoad();}
    		}
		}
    	xmlHttpReq.send(data);    	
}


// Show comments, use showComment()
// flag show is the cooment field mast be vissible or hide
function showCommentBlock(id,userId,flag){


	comFiled = "comField" + id;
	if(flag=='sh'){document.getElementById(comFiled).style.display = "block";}
	else if(flag=='0'){
		commId = "commentArea" + id;
		document.getElementById(comFiled).style.display = "none";
		document.getElementById(commId).value = '';
		}
	
	divId = "comment" + id;
	elem = document.getElementById(divId);

	if(elem.style.display == 'block'){
		elem.style.display = 'none';
	}
	else {
		obj = 'comCount' + id;
		obj = document.getElementById(obj);
		if(document.getElementById('load').style.display=="none")showLoad(obj);
		elem.style.display = 'block';
		data = "id=" + id + "&user_id=" + userId;
		divId = "listComment" + id;
		showComment("/show_comment.php",data,divId);
	}
}



//Insert the comment in DB
function submitComment(strURL,data,id) {

 	   
		var xmlHttpReq = false;
    // Mozilla
    	if (window.XMLHttpRequest) {
        	xmlHttpReq = new XMLHttpRequest();
        	xmlHttpReq.overrideMimeType('text/xml');
    	}
    // IE
    	else if (window.ActiveXObject) {
        	xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    	xmlHttpReq.open('POST', strURL, true);
    	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlHttpReq.onreadystatechange = function() { 
        	if (xmlHttpReq.readyState == 4) {

				if(xmlHttpReq.responseText!=''){
						
						divId = "comment" + id;
						document.getElementById(divId).style.display = 'none';
						showCommentBlock(id,'','0');//alert(xmlHttpReq.responseText);
						hideLoad();
						
				}

				else {hideLoad();}
    		}
		}
    	xmlHttpReq.send(data);    	
}


//Use submitComment()
function postComment(id,userId){

	obj = 'comCount' + id;
	obj = document.getElementById(obj);
	showLoad(obj);
	commId = "commentArea" + id;
	comm = document.getElementById(commId).value;
	data = "id=" + id + "&comment=" + comm + "&user_id=" + userId;	
	submitComment("/post_comment.php",data,id);
}


function delComment(comId,blockId){

	obj = 'comCount' + blockId;
	obj = document.getElementById(obj);
	showLoad(obj);
	data = "id=" + comId;	
	submitComment("/delete_comment.php",data,blockId);
}

