var xmlHttp;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function SaveRatingAjax( idCat, articleId, ratingValue)
{
	var submitURL = ''
	+ '/rating.asp?'
	+ 'idCat=' + idCat                     // Id = id de l'article
	+ '&articleId=' + articleId
	+ '&value=' + ratingValue
	+ '&fake=' + Math.random();
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleRatingStateChange;
	xmlHttp.open("GET", submitURL, true);
	xmlHttp.send(null);
}

function handleRatingStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
					var tbl = xmlHttp.responseText.split( '-');
					if( '0' == tbl[0]) {
					  document.getElementById('vote-ko').style.visibility='hidden';
	          document.getElementById('vote-ok').style.visibility='visible';
					}
					else {
					  document.getElementById('vote-ok').style.visibility='hidden';
          	document.getElementById('vote-ko').style.visibility='visible';
					}
	    }
			else {
				alert("Error in AJAX");
			}
	}
}

function SaveAddCommentAjax( idCat, articleId, comment)
{
	var submitURL = ''
	+ '/add-comment.asp?'
	+ 'idCat=' + idCat                     // Id = id de l'article
	+ '&articleId=' + articleId
	+ '&value=' + escape(comment);

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleAddCommentStateChange;
	xmlHttp.open("GET", submitURL, true);
	xmlHttp.send(null);
}

function handleAddCommentStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            document.getElementById('comment-fill').innerHTML = '<div align="center">Votre commentaire a bien été pris en compte.<br /><br /></div><div align="center" style="text-align:center; width:100%; margin-bottom:5px;"><a href="javascript:void(0)" onclick="ToogleAddComment(\'add-comment\')"><img src="/img/button-fermer.gif"></a></div>';
	    }
			else {
				alert("Error in AJAX");
			}
	}
}

function LoadCommentAjax( idCat, articleId)
{
	var submitURL = ''
	+ '/comment.asp?'
	+ 'idCat=' + idCat                     // Id = id de l'article
	+ '&articleId=' + articleId
	+ '&fake=' + Math.random();

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleLoadCommentStateChange;
	xmlHttp.open("GET", submitURL, true);
	xmlHttp.send(null);
}

function handleLoadCommentStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
					var str = xmlHttp.responseText;
          var id;
					var content;
					
					id = str.substring( 0, str.indexOf( ' '));
					content = str.substring( str.indexOf( '-') + 1, str.length);
          document.getElementById('fillcomment'+id).innerHTML = content;
          document.getElementById('comment' + id).style.display='block';
	    }
			else {
				alert("Error in AJAX");
			}
	}
}

