﻿/// <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 mailTalk = { // MailTalk 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"
                }, 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.removeContact = function (options) {
                options = jQuery.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) {
                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."); }
                    }
                });
            }
        }
    }
};

