﻿/// <reference path="JSON.js" />
/// <reference path="jquery-1.5.2.min.js" />
/*
 * jQuery Email Validation 1.0
 *
 * Copyright (c) 2010 Delegate A/S
 *
 * Depends:
 *   jQuery 1.5.2 (probably works for previous versions too but we always recommend latest build of jQuery)
 *
 * History:
 *   0.1   / 2010-29-04 / abo / creation of plugin
 */
 var mailMonitor = { // MailMonitor namespace
	sdk: { // SDK namespace
		services: function(options) { // collection of service functions
			var settings = $jQuery.extend({
				userID: '00000000-0000-0000-0000-000000000000', // the identifier of the user
				url: '/WebServices/ContactValidationService.svc' // the base address of the service
			}, options); // userID, url

			var self = this;

			this.validateEmail = function(options) { // validates an e-mail address
				options = $jQuery.extend({
					// userID: guid,
					// level: string - values: syntax, mxrecords, smtp, mailbox (default),
					// email: string
				}, settings, {
					url: settings.url + "/ValidateEmail",
					level: 'mailbox',
				}, options);

				self.invoke(options, {
					userID: options.userID,
					level: options.level,
					email: options.email
				});
			},

			this.getContactDatabases = function(options) { // returns a list of contact databases
				options = $jQuery.extend({
					// userID: guid,
					// id: guid
				}, settings, {
					url: settings.url + "/GetContactDatabases"
				}, options);

				self.invoke(options, {
					userID: options.userID,
					id: options.id
				});
			},

			this.createContact = function(options) {
				options = $jQuery.extend({
					// userID: guid,
					// level: string - values: syntax, mxrecords, smtp, mailbox (default)
					// email: string,
					// givenName: string,
					// fields: [],
					// databases: []
				}, settings, {
					url: settings.url + "/CreateContact",
					level: 'mailbox',
				}, options);

				self.invoke(options, {
					userID: options.userID,
					level: options.level,
					email: options.email,
					givenName: options.givenName,
					fields: JSON.stringify(options.fields),
					databases: JSON.stringify(options.databases)
				});
			},

			this.invoke = function(options, data) {
				$jQuery.ajax({
					url: options.url,
					data: data,
					dataType: "jsonp",
					success: function(json) {
						if ($jQuery.isFunction(options.success)) {
							options.success(json);
						} else { alert("success: " + json); }
					},
					error: function() {
						if ($jQuery.isFunction(options.error)) {
							options.error();
						} else { alert("An error occured while invoking the remote service."); }
					}
				});
			}
		}
	}
 };

 //(function($jQuery) {
	$jQuery.fn.validateEmail = function(options) {
		var settings = $jQuery.extend({
			url: '/WebServices/ContactValidationService.svc/ValidateEmail',
			level: '', // syntax, mxrecords, smtp, mailbox
			success: function (json) {
				if (json) {
					if (json.result) {
						alert(json.result);
					}
				}
			},
			error: function () {
				alert("error");
			}
		}, options);

		var value = $jQuery(this).val();

		if (value) {
			$jQuery.ajax({
				url: settings.url,
				data: {
					email: value,
					level: settings.level
				},
				dataType: "jsonp",
				success: function(json) {
					if ($jQuery.isFunction(settings.success)) {
						settings.success(json);
					}
				},
				error: function() {
					if ($jQuery.isFunction(settings.error)) {
						settings.error(json);
					}
				}
			});
		}

		return this;
	}
//}(jQuery));

