﻿/*
DISCLAIMER functionality
    
This functionality consists in displaying a modal confirmation box before clicking a link, button or submitting a form
For this functionality to be avaible you need to have a DIV with ID=disclaimerBox (this div will contain the disclaimer text) 
To display and alternate container page decorate your element with data-license-id="ID", where ID is the id of page that contains the disclaimer

Disclaimer for links and buttons
-> links need to be decorated with data-disclaimer='true'
Disclaimer for forms without validation:
-> needs to be decorated with data-disclaimer='true'
Disclaimer for forms with validation:
-> In the validation function you need to add the following code
        
showDisclaimer(FORM);    
return false;
        
where FORM is the current form.
This will instruct the code to display the validation dialog box
        
*/
(function ($) {
  var dlgDisclaimer;

  $().ready(function () {
    dlgDisclaimer = $("#disclaimerBox");
    dlgDisclaimer.dialog({
      resizable: true,
      bgiframe: true,
      autoOpen: false,
      height: 600,
      width: 800,
      modal: true
    });

    if (dlgDisclaimer.length > 0) {
      findDisclaimerElements();
    }
  });

  function createButtons() {
    dlgDisclaimer.dialog('option', 'buttons', {
      'I Agree': function () {
        dlgDisclaimer.dialog('close');
        location.href = dlgDisclaimer.attr('data-download');
      },
      'I Disagree': function () { $(this).dialog('close'); }
    });
  }

  function findDisclaimerElements() {
    $("[a|input|form][data-license-id]").each(function () {
      switch (this.tagName) {
        case 'A':
          initDisclaimerElement($(this));
          break;
        case 'INPUT':
          if ($(this).attr('type') === 'button') {
            initDisclaimerElement($(this));
          }
          break;
        case 'FORM':
          $(this).submit(function () { return false; });
          initDisclaimerElement($(this));
          break;
      }
    });
  }

  function initDisclaimerElement(elem) {
    var licenseId = parseInt(elem.attr("data-license-id"));
    if (licenseId != null && licenseId > 0) {
      elem.click(function () { showDisclaimerAjax($(this)); });
    }
  }

  function showDisclaimerAjax(element) {
    var objLoader =
      $("<img src='/images/ajax-loader.gif' style />")
        .css("margin-left", (dlgDisclaimer.dialog('option', 'width') / 2 - 60) + "px")
        .css("margin-top", (dlgDisclaimer.dialog('option', 'height') / 2 - 60) + "px");

    dlgDisclaimer
      .html(objLoader)
      .attr('data-download', element.attr('data-download'))
      .dialog('option', 'title', 'Loading disclaimer...')
      .dialog('open');

    $.ajax({
      type: "GET",
      url: "/Disclaimer/Details/" + element.attr("data-license-id"),
      cache: false,
      success: loadDisclaimer,
      error: showDisclaimerResponseFailed
    });
  }

  function loadDisclaimer(data, success) {
    dlgDisclaimer.html(data);
    var title = dlgDisclaimer.find("div:first").attr('title');
    dlgDisclaimer.dialog('option', 'title', title);
    createButtons();
  }

  function showDisclaimerResponseFailed(data) {
    dlgDisclaimer
      .html(data.responseText)
      .dialog('option', 'title', "Error loading disclaimer");
  }

})(window.jQuery);
