AEGON.dms.emailer = {

	options: {
		DOC_EMAILER_URL: '/doc_emailer.php',
		TYPE: '',
    VALIDATOR: 'dms_checkOrderForm'
	},
	setType: function(t) {
		this.options.TYPE = t;
	},
  setValidator: function(v) {
    this.options.VALIDATOR = v;
  },
	emailerSubmit: function(form) {

		if (this.options.TYPE == 'email') {
			// json submit
			f = $(form);
			$("input:not('.docmail')").each(function() { $(this).attr('disabled', 'true'); });
			$.post(AEGON.dms.emailer.options.DOC_EMAILER_URL, f.serialize(), function(data, textStatus) {
				try {
					data = $.evalJSON(data);
				} catch(err) {
					// console.log(err);
				}

				if (data['status'] == 'OK') {
					$('div.emaildoc').text(data['message']);
				} else {
					alert(data['message']);
				}
			});

			$("input:not('.docmail')").each(function() { $(this).removeAttr('disabled'); });
			$("input:checked").each(function() { $(this).attr('checked', false); });
			return false;

		} else if (this.options.TYPE == 'order'){

			if (eval(this.options.VALIDATOR +'(form)') == true) {
				$("input.docmail").each(function() { $(this).attr('disabled', 'true'); });
				return true;
			}
		}

		return false;

	}
};


$(document).ready(function() { 
	
	$('div.emaildoc input').focus(
		function() {
			var input = $(this);
			if (input.val() == 'Email address') {
				input.val('');
			}
			else {
				this.select();
			}
			input.addClass('focus');
		}
	).blur(function() {
		var input = $(this);
		if (!input.val()) {
			input.val('Email address');
			$(this).removeClass('focus');
		}

	});
	
	$.clipboardReady(function() {
			$('td.copydoc').each(function() {
				a = $('<a href="#"><img src="/assets/img/icons/copy.gif" alt="Copy the document URL to your clipboard" /></a>');
				a.click(function() {
					link = $(this).parent().parent().find('td:first-child a').attr('href');
					if (link != '') $.clipboard(link);
				});
				$(this).html(a);	
			});
			$('div.linkhelp').show();
	}, { swfpath: "/assets/swf/jquery.clipboard.swf", debug: false });
	try {
	$('td.copydoc a').show();
	$('div.emaildoc').show();
	} catch(err) {}
});

