﻿/// <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
 *   0.2   / 2011-24-11 / abo / encapsulation and guarding against mutation
 */

;(function( $, window, document, undefined) {
	window.mailTalk = { // MailTalk namespace
		sdk: { // SDK namespace
			services: function (options) { // collection of service functions
				var settings = $.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 = $.extend({
						// userID: guid,
						// level: string - values: syntax, mxrecords, smtp, mailbox (default),
						// email: string
					}, settings, {
						url: settings.url + "/ValidateEmail"
					}, options);

					self.invoke(options, {
						userID: options.userID,
						level: options.level,
						email: options.email
					});
				},

				this.getContactDatabases = function (options) { // returns a list of contact databases
					options = $.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 = $.extend({
						// userID: guid,
						// level: string - values: syntax, mxrecords, smtp, mailbox (default)
						// email: string,
						// givenName: string,
						// fields: [],
						// databases: []
					}, settings, {
						url: settings.url + "/CreateContact"
					}, 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.removeContact = function (options) {
					options = $.extend({
						// userID: guid,
						// email: string,
						// databases: []
					}, settings, {
						url: settings.url + "/RemoveContact"
					}, options);

					self.invoke(options, {
						userId: options.userID,
						email: options.email,
						databases: JSON.stringify(options.databases)
					});
				},

				this.invoke = function (options, data) {
					$.ajax({
						url: options.url,
						cache: false,
						data: data,
						dataType: "jsonp",
						success: function (json) {
							if ($.isFunction(options.success)) {
								options.success(json);
							} else { alert("success: " + json); }
						},
						error: function () {
							if ($.isFunction(options.error)) {
								options.error();
							} else { alert("An error occured while invoking the remote service."); }
						}
					});
				}
			}
		}
	};
})(jQuery, window, document);

