function emailPackage(link){
	$.ajax( {url: link, type: 'get', cache:false, dataType:'html', complete: function(xhr){ if($('#popup').is(":hidden")) { $('#popup').html(xhr.responseText); $('#popup').modal({close: false, onShow: callback.show}); } } } );
}

/* callback method option for sending emails using ajax */
var callback = {

	errorMsg: null,

	show: function(dialog){
		$('#submit').click( function(e) { e.preventDefault(); if(callback.validate()){ $.ajax({url: 'emailPackage.php', data: $('#popup form').serialize(), type:'post', cache:false, dataType:'html', complete: function(xhr) { $('#msg').html(xhr.responseText); }}); } else{ $('#msg').html('Errors:<br/>'+callback.errorMsg)} });
	},

	validate: function() {
		callback.errorMsg = '';

		var to = $('#to').val();
		if(!to){
			callback.errorMsg += '** Recipient email address is required **<br/>';
		}else if(!callback.validateEmail(to)){
			callback.errorMsg += '** The recipient email address is not a valid email address **<br/>';
		}

		var from = $('#from').val();
		if(!from){
			callback.errorMsg += '** Sender email address is required **<br/>';
		}else if(!callback.validateEmail(from)){
			callback.errorMsg += '** The sender email address is not a valid email address **<br/>';
		}

		if(callback.errorMsg.length > 0){
			return false;
		}else{
			return true;
		}
	},

	validateEmail: function (email) {
		var at = email.lastIndexOf("@");

		// Make sure the at (@) sybmol exists and  
		// it is not the first or last character
		if (at < 1 || (at + 1) === email.length)
			return false;

		// Make sure there aren't multiple periods together
		if (/(\.{2,})/.test(email))
			return false;

		// Break up the local and domain portions
		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		// Check lengths
		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
			return false;

		// Make sure local and domain don't start with or end with a period
		if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
			return false;

		// Check for quoted-string addresses
		// Since almost anything is allowed in a quoted-string address,
		// we're just going to let them go through
		if (!/^"(.+)"$/.test(local)) {
			// It's a dot-string address...check for valid characters
			if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
				return false;
		}

		// Make sure domain contains only valid characters and at least one period
		if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
			return false;	

		return true;
	}
}
