/// <reference path="LaunchNetClientBase.js"/>
/// <reference path="BrowserServices.js"/>
/// <reference path="ErrorServices.js"/>
/// <reference path="ClientProxyServices.js"/>
/// <reference path="LaunchNetClientContext.js"/>
/// <reference path="PlatformServices.js"/>


//the following are variables for which values are injected
//var __yourVariable;

function EmailServices()
{
  var commands = new LaunchNetServiceCommands();
  commands.Add("ShareThisSiteSubmit");

  this.OnPageLoad = OnPageLoad;
  this.OnPageClick = OnPageClick;
  this.OnValidationRequiredCheck = OnValidationRequiredCheck;
  this.OnValidateLaunchNetElements = OnValidateLaunchNetElements;

  this.ShareThisSiteSubmit = ShareThisSiteSubmit;
  
/* ***********************************************************************************
-  Page Load Event
************************************************************************************ */

  function OnPageLoad()
  {
  }


/* ***********************************************************************************
-  Page Click Event
************************************************************************************ */

  function OnPageClick(launchNetElementClicked, pageValid)
  {
    commands.HandlePageClick(this, launchNetElementClicked, pageValid);
  }                       

  function ShareThisSiteSubmit()
  {
    var yourEmail = GetLaunchNetElementById("YourEmail").GetValue();
    var friendEmail = GetLaunchNetElementById("FriendEmail").GetValue();

    //as some elements are optional, we'll provide an empty string if the apropriate element cannot be found
    var yourNameElement = GetLaunchNetElementById("YourName");
    var friendNameElement = GetLaunchNetElementById("FriendName");
    var customMessageElement = GetLaunchNetElementById("CustomMessage");
    
    var yourName = yourNameElement == null ? "" : yourNameElement.GetValue();
    var friendName = friendNameElement == null ? "" : friendNameElement.GetValue();
    var customMessage = customMessageElement == null ? "" : customMessageElement.GetValue();

    var response = __context.GetServiceClientProxy('EmailServices').ShareThisSite(yourName, yourEmail, friendName, friendEmail, customMessage);
    
    if (response.error != null)
    {
      __context.RedirectToErrorPage();
      return false;
    }
    
    return true;
  }

/* ***********************************************************************************
-  Page Validation
************************************************************************************ */

  function OnValidationRequiredCheck(launchNetElementClicked)
  {
    return commands.HandleValidationRequiredCheck(launchNetElementClicked);
  }

  function OnValidateLaunchNetElements(validatableLaunchNetElements)
  {
    //validate any special elements here; return true if validation successful
    return true;
  }
}