﻿/// <reference path="~/Scripts/External/jquery-1.4.2.js"/>

// first, declare the two namespaces if they do not already exist
if (Adlib == null || typeof (Adlib) != "object") { var Adlib = new Object(); }
if (Adlib.View == null || typeof (Adlib.View) != "object") { Adlib.View = new Object(); }


Adlib.View.ContactForm = function () {

  return {

    OpenPopup: function ($subsLink) {
      $j.blockUI({
        message: $j('#divContactForm'),
        css: { width: $j('#divContactForm').width() + 20, top: '10px' }
      }
              );


    } //OpenEventSubscriptionPopup
    ,

    SendForn: function () {

      $j.ajax({
        url: Adlib.Base.Data.GetBaseUrl() + "/contact-form",
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        data: ({
          Title: $j("#divContactForm *[name='Title']").val(),
          Initials: $j("#divContactForm *[name='Initials']").val(),
          Name: $j("#divContactForm *[name='Name']").val(),
          Company: $j("#divContactForm *[name='Company']").val(),
          Street: $j("#divContactForm *[name='Street']").val(),
          PostCode: $j("#divContactForm *[name='PostCode']").val(),
          Place: $j("#divContactForm *[name='Place']").val(),
          Country: $j("#divContactForm *[name='Country'] :selected").text(),
          CountryCode: $j("#divContactForm *[name='Country']").val(),
          Telephone: $j("#divContactForm *[name='Telephone']").val(),
          Email: $j("#divContactForm *[name='Email']").val(),
          OrganizationType: $j("#divContactForm *[name='OrganizationType']").val(),
          NumConcurrentAdlibUsers: $j("#divContactForm *[name='NumConcurrentAdlibUsers']").val(),
          CurrentSoftwareSolution: $j("#divContactForm *[name='CurrentSoftwareSolution']").val(),
          Library: ($j("#divContactForm *[name='Library']").attr("checked") != ""),
          Museum: ($j("#divContactForm *[name='Museum']").attr("checked") != ""),
          Archive: ($j("#divContactForm *[name='Archive']").attr("checked") != ""),
          InternetProducts: ($j("#divContactForm *[name='InternetProducts']").attr("checked") != ""),
          DemoCD: ($j("#divContactForm *[name='DemoCD']").attr("checked") != ""),
          ReasonsInterest: $j("#divContactForm *[name='ReasonsInterest']").val(),
          Remarks: $j("#divContactForm *[name='Remarks']").val()
        }),
        dataType: "json",
        success: function (data) {

          if (data.Success == true) {
            //success behavior
            $j("#divContactForm .message").html(data.Message).show();
            $j("#divContactForm #btnClose").show();
            $j("#divContactForm #btnSubmit").hide();
            $j('#divContactForm #fieldList').hide();
          }
          else {
            //error behavior
            $j("#divContactForm .message").html(data.Message).show();
          }
        },
        error: function (e) {
          alert("error:" + e);
        }

      }
          );

    } //SendEventSubscription

    ,

    Init: function () {
      $j(document).ready(function () {

        //jquery live events handlers
        $j("a.open-contact-form").live("click", function () {
          Adlib.View.ContactForm.OpenPopup($j(this));
        });

        $j("#divContactForm #btnSubmit").live("click", function () {
          Adlib.View.ContactForm.SendForn();
        });

        $j("#divContactForm #btnClosePopup, #divContactForm #btnClose").live("click", function () {
          $j("#divContactForm").hide();
          $j.unblockUI();

          //clean fields
          $j("#divContactForm input, #divContactForm textarea").val("").attr("checked", "");
          $j("#divContactForm select").val("0");

          //clean message
          $j("#divContactForm .message").html("").hide();

          //show fields
          $j('#divContactForm #fieldList').show();

          //show buttons
          $j("#divContactForm #btnSubmit").show();
        });

      });
    }

  };
} ();



