
function checkAllContacts()
{
	$$("#contacts input:checkbox").each(function(i) {
		$(i).checked = true;
	});	
}

function uncheckAllContacts()
{
	$$("#contacts input:checkbox").each(function(i) { 
		$(i).checked = false;
	});	
}

function sendMails()
{
	$('spinner').show();
	var emails = [];
	$$("#contacts input:checkbox").each(function(i) {
		if ($(i).checked) {
			emails.push($(i).value);
			$(i).parentNode.remove()
		}
	});

	var params = "";
	emails.each(function(item) {
		params += "emails[]="+item+"&";
	});

	if (emails.length > 0) {
		new Ajax.Request('/mailer/send_invitation',
		{
		    method:'post',
				parameters: params+'authenticity_token='+window._token,
		    onSuccess: function(transport){
		      var response = transport.responseText || "no response text";
					$('spinner').hide();
		      alert("\n" + response + "\n");
		    },
		    onFailure: function(){ alert('No se pudieron enviar las invitaciones'); $('spinner').hide(); }
		});
	} else {
		alert('Debes seleccionar algún amigo para agregar');
		$('spinner').hide();
	}
}

function sendMail()
{
	var emails = [];

	if ($('single_email') && $('single_email').value != "") {
		emails.push($('single_email').value);
		$('single_email').value = '';
	}
	for(i=1; i<=5; i++) {
		if ($('single_email'+i) && $('single_email'+i).value != "") {
			emails.push($('single_email'+i).value);
			$('single_email'+i).value = '';
		}		
	}
	var params = "";
	emails.each(function(item) {
		params += "emails[]="+item+"&";
	});

	if (emails.length > 0) {
		new Ajax.Request('/mailer/send_invitation',
		{
		    method:'post',
				parameters: params+'authenticity_token='+window._token,
		    onSuccess: function(transport){
		      var response = transport.responseText || "no response text";
		      alert("\n" + response + "\n");
		    },
		    onFailure: function(){ 
		    	alert('No se pudo enviar la invitación');
		    }
		});
		return false;
	} else {
		alert('Debes ingresar el email de algún amigo');
		return false;
	}
}
