// emailSuccess
function emailSuccess() {
	displayTheEmailMsg('Thanks, we hope they enjoy it!<br/>' +
						'Your Email has been sent to:<br/>' + 
						$F('inviteTo'));
}
function emailHelpSuccess() {
	displayTheEmailMsg('Thanks, your help request has been sent to our administrator.');
}
// emailFailure
function emailFailure() {
	displayTheEmailMsg('Sorry, there were errors attempting to send your email to:<br/>' +
						$F('inviteTo'));
}

// Function display the message in the email div
function displayTheEmailMsg(msg) {
	$('emailInviteContainer').style.display = 'none';
	$('emailThanksContainer').style.display = 'block';
	$('emailThanksContainer').innerHTML= msg;
}

// Send an invite email
function sendInviteEmail() {
	if (validateInviteEmail()) {
		var toList = $F('inviteTo');
		var url = contextRoot + '/emailInvite.do';
		var pars = {
			email: toList,
			message: $F('inviteMsg')
		};
		
    	doJsonAjaxRequest(url, pars, emailSuccess, emailFailure);
    
	}
	return false;
}
function validateInviteEmail() {
	var rlist = $F('inviteTo');
    var recpts = $A(rlist.split(','));
    var errors = false;
    var s;
	recpts.each(function(recipient) {
		if (!validEmail(recipient)) {
			s = 'Please enter valid emails!<br/>';
			errors = true;
		}
	});
	if (errors) {
		$('prErrors').innerHTML = s;
		$('prErrors').style.display="block";
	} else {
		$('prErrors').innerHTML ='';
		$('prErrors').style.display="none";
	}
	return !errors;
}
function cancelInviteEmail() {
	initEmailForm();
	return false;
}
function initEmailForm() {
	$('prErrors').innerHTML ='';
	$('prErrors').style.display="none";
	$('inviteTo').value='';
	$('inviteMsg').value='';
	return false;
}
function sendContactEmail() {

	if (validateContactEmail()) {
		var fromEmail = $F('yourEmailTxt');
		var fromName = $F('yourNameTxt');
		var url = contextRoot + '/emailContact.do';
		var pars = {
				from: fromEmail,
				fromName: fromName,
				message: $F('contactMsg')
			};
	
    	doJsonAjaxRequest(url, pars, emailSuccess, emailFailure);
	}
	return false;
}
function sendHelpEmail() {

	if (validateContactEmail()) {
		var fromEmail = $F('yourEmailTxt');
		var fromName = $F('yourNameTxt');
		var url = contextRoot + '/emailContact.do';
		var pars = {
				from: fromEmail,
				fromName: fromName,
				message: $F('contactMsg')
			};
	
    	doJsonAjaxRequest(url, pars, emailHelpSuccess, emailFailure);
	}
	return false;
}
function validateContactEmail() {
	var fromEmail = $F('yourEmailTxt');
	var fromName = $F('yourNameTxt');
	var msg = $F('contactMsg');
	
    var errors = false;
    var s = "";
	if (!validEmail(fromEmail)) {
		s = s +  'Please enter a valid email Address.<br />';
			errors = true;
	}
	if (fromName == "") {
		s = s + 'Please enter your name.<br />';
		errors = true;
	}
	if (msg == "") {
		s = s + 'Please enter a message.<br />';
		errors = true;
	}
	
	if (errors == true) {
		$('helpErrors').innerHTML = s;
		$('helpErrors').style.display="block";
	} else {
		$('helpErrors').innerHTML ='';
		$('helpErrors').style.display="none";
	}
	return !errors;
}
function cancelContactEmail() {
	initContactForm();
	return false;
}
function initContactForm() {
	$('yourEmailTxt').value='';
	$('yourNameTxt').value='';
	$('contactMsg').value='';
	$('helpErrors').innerHTML ='';
	$('helpErrors').style.display="none";
	return false;
}