/** submitting stuff **/
function lightCandle(){
	var typeId = 2;
	var submitterName = document.getElementById("candle_name").value;
	var submitterEmail = document.getElementById("candle_email").value;
	var submitterMessage = document.getElementById("candle_message").value;
	validateAndSaveCandle(null, memorialId, typeId, submitterName, submitterEmail, submitterMessage);
}

// I dropped this in instead of messing with lightCandle or validateAndSaveCandle because I don't know what they do.
function validateCandleForm()
{
	var candleMessage = window.document.getElementById("candle_message");

	if(candleMessage.value.length > 128)
	{
		alert("Candle Message contains more than 128 characters.\nIf you would like to enter more text, you may add a Condolence instead.");
		candleMessage.focus();
		return false;
	}
	return true;
}


function validateAndSaveCandle(userId, memorialId, typeId, submitterName, submitterEmail, submitterMessage){
	document.getElementById("candle_email_error").innerHTML="*Email: ";
	document.getElementById("candle_name_error").innerHTML="*Name: ";
	document.getElementById("candle_message_error").innerHTML="Candle Message: <span style='font-size:9px; font-weight:100;'> (max characters: 128)</span>";

	var callBack = function(data){
		var returnValue = true;
		
		if(!data){
			document.getElementById("candle_email_error").innerHTML="*Email: invalid email address";
			returnValue = false;
		}
		
		if(isEmpty(submitterName)){
			document.getElementById("candle_name_error").innerHTML="*Name: please enter a name";
			returnValue = false;
		}
		
		if(returnValue)
			saveCondolence(null, memorialId, typeId, submitterName, submitterEmail, submitterMessage);
	}
	AjaxValidator.validateEmail(submitterEmail, callBack);
}

function validateCondolence(condolence_message){
	if(isEmpty(condolence_message)){
		document.getElementById("condolence_error").innerHTML="Condolence: please enter message <span style='font-size:9px; font-weight:100;'> (max characters: 2000)</span>";
		return false;
	}
	return true;
}


function sendCondolence(){
	var typeId = 1;
	var submitterMessage = document.getElementById("condolence").value;
	var submitterName = null;
	var submitterEmail = null;
	if(userId != 'anonymous' && validateCondolence(submitterMessage))
		saveCondolence(userId, memorialId, typeId, submitterName, submitterEmail, submitterMessage);
}

function saveCondolence(_userId, _memorialId, _typeId, _submitterName, _email, _message){
	
	var usr={
		email: _userId
	};
	
	var mem={
		id: _memorialId
	};
	
	var condType = {
		id: _typeId
	};
	
	var condolence={
  		submitterName: _submitterName,
  		email: _email,
  		message: _message,
  		user: usr,
  		memorial: mem,
  		type: condType
  	};

  	var callback_function;
  	if(_typeId == 1){
  		MemorialService.saveCondolence(condolence, condolenceCallback);
  	}
  	else{
  		MemorialService.saveCondolence(condolence, lightCandleCallback);
  	}
}