var urlHost = "http://beta.bling182.fr/scripts/";

function postMessage(form) {
	var msg = form.__comment_message.value;
	msg = msg.replace('&', 'ampersand');

	var xmlHttp = getXmlHttp();
	
	toggleDisplay('book_comment_submit');
	showLoad('book_comment_loading');
	
	var url = urlHost + "comment_post.php";
	var params = "add=add&msg=" + msg;
	xmlHttp.open("POST", url, true);

	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange = function() { //Call a function when the state changes.
		if (xmlHttp.readyState < 4) {
			showLoad('book_comment_loading');
		}
		if(xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				hideLoad('book_comment_loading');
				form.__comment_message.value = '';
				toggleDisplay('book_comment_submit');
			} else {
				alert("Unknown error saving your message");
				hideLoad('book_comment_loading');
				toggleDisplay('book_comment_submit');
			}
		}
	}
	xmlHttp.send(params);
}

function getMessages() {
	var xmlHttp = getXmlHttp();
	
	var url = urlHost + "/comment_post.php";
	var params = "disp=display";
	xmlHttp.open("POST", url, true);

	showLoad('comment_display_loading');

        //Send the proper header information along with the request
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");

        xmlHttp.onreadystatechange = function() { //Call a function when the state changes.
                if (xmlHttp.readyState < 4) {
			showLoad('comment_display_loading');
                }              
               if(xmlHttp.readyState == 4) {
                        if (xmlHttp.status == 200) {
				hideLoad('comment_display_loading');
                                displayComments(xmlHttp.responseXML);
                        } else {
                                alert("An unknown error occured");
                        }
                }
        }
        
        xmlHttp.send(params);

}

function displayComments(response) {
	var responseRoot = response.documentElement;
	var origDiv = document.getElementById('book_comments');

	var newDiv = document.createDocumentFragment();
	var bookCommentsDiv = document.createElement('div');
	bookCommentsDiv.setAttribute('id', 'book_comments');

	var pArray = responseRoot.getElementsByTagName('p');
	for (i = 0 ; i < pArray.length ; i++) {
		var pElem = document.createElement('p');
		pElem.appendChild(document.createTextNode(pArray[i].firstChild.nodeValue));
		bookCommentsDiv.appendChild(pElem);
	}

	newDiv.appendChild(bookCommentsDiv);

	origDiv.parentNode.replaceChild(newDiv, origDiv);
}


function showLoad(id) {
	document.getElementById(id).style.display= 'block';
}


function hideLoad(id) {
	document.getElementById(id).style.display = 'none';
}


function toggleDisplay (id)
{       
        if (document.getElementById(id).style.display == 'none')
                document.getElementById(id).style.display = 'block';
        else    
                document.getElementById(id).style.display = 'none';
}

