function checkComment() {
    var pass = true;
    var msg = 'Please fill the requied fields!';
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    getLabelForId('email').style.color = '#FFFFFF'; 
    getLabelForId('author').style.color = '#FFFFFF';
    
    if(document.getElementById('author').value == '') {
        pass = false;
        getLabelForId('author').style.color = '#FF5248';
    }
    if(document.getElementById('email').value == '') {
        pass = false;
        getLabelForId('email').style.color = '#FF5248';
    }
    else if(document.getElementById('email').value.search(emailRegEx) == -1) {
        pass = false;
        getLabelForId('email').style.color = '#FF5248';
        msg = 'Please enter a valid email address!';
    }
    
    if(!pass) {
        alert(msg);
    }
    
    return pass;
}

function saveComment() {
	if(checkComment()) {
		var url = 'acc=save';
		url += '&pageId=' + document.getElementById('pageId').value;
		url += '&author=' + document.getElementById('author').value;
		url += '&email=' + document.getElementById('email').value;
		url += '&website=' + document.getElementById('url').value;
		url += '&comment=' + encodeURIComponent(document.getElementById('comment').value);
		http.open('post', 'scripts/comment_script.php');
		http.onreadystatechange = updateCommentList;
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.send(url);
		document.getElementById('commentForm').reset();
	}
}

function updateCommentList() {	
    if(http.readyState == 4) {
        if (http.status == 200) {	
            var response = http.responseText.split('|=|-|');
            document.getElementById('commentList').innerHTML += response[1];
            goExpan('comment'+ response[0]);	 
		}
	}
}

function deleteComment(commentId) {
  if(confirm("Are you sure you want to delete this comment")) {
    document.getElementById('commentList').removeChild(document.getElementById('commentDiv'+commentId));
    document.getElementById('commentList').removeChild(document.getElementById('commentDiv'+commentId+'_'));
    var url = 'acc=delete';
    url += '&commentId=' + commentId;
    http.open('post', 'scripts/comment_script.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send(url);
  }
}
