//extend prototype trim functions
//trim
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//left trim
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
//right trim
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

//toggle(close or open comment window)
function toggleCommentWindow(id) {   
    /*
	//get all containers list
	allContainersList = $$('.animal_detail_option_container');
	//hide all containers
	for(i=0;i<allContainersList.length;i++){
		allContainersList[i].style.display = 'none';
	}
	*/
	//set current container
	container = $(id);	
	//set container style
	if (container.style.display == 'none') {
		// If the container was hidden, display it
		container.style.display = 'block';		
	} else {
		// Hide it, and clear its' textarea
		closeCommentContainer(id);
	}
}


function sendInappropriateContent(textareaId, animalId, contentType, errorMessage, id, buttonId) {
	textareaValue = $(textareaId).value;
	textareaValue = textareaValue.trim();
	if (textareaValue.length < 1){
	   alert (errorMessage);
	   return false;
	}	
	request = 'ajax=1&command=inappropriateContent&contentId=' + animalId + '&contentType=' + contentType + '&userComment=' + encodeURIComponent(textareaValue) + '&containerId=' + id + '&buttonId=' + buttonId;
	ajaxRequest('/submit.php', request, 'inappropiateContentResponseHandler', false);
}

function inappropiateContentResponseHandler(xmlDoc) {
	var statusObj = xmlDoc.getElementsByTagName('statusObj'); 
		statusContent = statusObj[0].firstChild.nodeValue;
		
	var containerObj = xmlDoc.getElementsByTagName('containerObj');
		containerId  = containerObj[0].firstChild.nodeValue;
		
	var buttonObj = xmlDoc.getElementsByTagName('buttonObj');
		buttonId  = buttonObj[0].firstChild.nodeValue;
	
	if (statusContent == 'succes') {
		// close text area and change button
		$(buttonId).className = "content_reported";  
		toggleCommentWindow(containerId);	
		closeDialogBox(containerId);	
	} else {
		// show error message
		alert (statusContent);
	}
}


//add diary entry comment
function addDiaryEntryComment(textareaId, entryId, containerId, containerInfoId, textareaContainerId){
	textareaValue = $(textareaId).value;
	textareaValue = textareaValue.trim();
	if (textareaValue.length < 1) return false;	 	  
	var url = '/submit.php'; 	
	var pars = 'ajax=3&command=addDiaryEntryComment&entryId=' + entryId + '&userComment=' + encodeURIComponent(textareaValue);   
	var myAjax = new Ajax.Updater(containerId, url, {method: 'post', parameters: pars, onComplete:function(transport){toggleCommentWindow(textareaContainerId);$(containerInfoId).style.display = 'block';}, insertion: 'top'});
}


function sendReply(textareaId, commentId, containerId, containerInfoId, textareaContainerId){	
	textareaValue = $(textareaId).value;	
	textareaValue = textareaValue.trim();
	if (textareaValue.length < 1) return false;	 
	var url = '/submit.php'; 	
	var pars = 'ajax=3&command=replyComment&commentId=' + commentId + '&userComment=' + encodeURIComponent(textareaValue);
	var myAjax = new Ajax.Updater(containerId, url, {method: 'post', parameters: pars, onComplete:function(transport){toggleCommentWindow(textareaContainerId);$(containerInfoId).style.display = 'block';$(textareaId).value='';}, insertion: 'top'});
}

function addAnimalComment(textareaId, animalId, containerId, containerInfoId, textareaContainerId){	
	textareaValue = $(textareaId).value;	
	textareaValue = textareaValue.trim();	
	if (textareaValue.length < 1) return false;	 	  
	var url = '/submit.php'; 	
	var pars = 'ajax=3&command=addAnimalComment&animalId=' + animalId + '&userComment=' + encodeURIComponent(textareaValue);   	
	var myAjax = new Ajax.Updater(containerId, url, {method: 'post', parameters: pars, onComplete:function(transport){toggleCommentWindow(textareaContainerId);$(containerInfoId).style.display = 'block';$(textareaId).value='';}, insertion: 'top'});	
}

function closeCommentContainer(id) {
	//hide comment container
	container = document.getElementById(id);	
	container.style.display = 'none';
}
