/// <reference path="LaunchNetClientContext.js"/>
/// <reference path="LaunchNetClientBase.js"/>
/// <reference path="BrowserServices.js"/>
/// <reference path="ErrorServices.js"/>
/// <reference path="ClientProxyServices.js"/>

//the following are variables for which values are injected
var __appId;
var __defaultErrorPage;
var __sessionPk;
var __userId;
var __numPagesVisited;
var __homeUrl;
var __thisUrl;
var __brand;
var __thisEnv = 'Unknown';

function PlatformServices()
{
  this.OnPageLoad = OnPageLoad;
  this.OnPageClick = OnPageClick;
  this.OnValidationRequiredCheck = OnValidationRequiredCheck;
  this.OnValidateLaunchNetElements = OnValidateLaunchNetElements;
  this.PlatformServicesValidator = PlatformServicesValidator;

  function PlatformServicesValidator()
  {
    //setup handled validation types
    var _handledValidationTypes = new Array();
    var _count = 0;
    _handledValidationTypes[_count++] = "Required";
    _handledValidationTypes[_count++] = "RequiredList";
    _handledValidationTypes[_count++] = "Date_NotRequired";
    _handledValidationTypes[_count++] = "Date";
    _handledValidationTypes[_count++] = "Email_NotRequired";
    _handledValidationTypes[_count++] = "Email";
    _handledValidationTypes[_count++] = "ConfirmEmail";
    _handledValidationTypes[_count++] = "Numeric";
    _handledValidationTypes[_count++] = "Alpha";
    _handledValidationTypes[_count++] = "AlphaNumeric";
    _handledValidationTypes[_count++] = "NoDigits";
    _handledValidationTypes[_count++] = "NoSpaces";
    _handledValidationTypes[_count++] = "NoCrLf";
    
    //call base class constructor with handled validation types
    ValidatorBase.call(this, _handledValidationTypes);
    
    //expose validation methods
    this.ValidateDate = ValidateDate;
    this.ValidateDate_NotRequired = ValidateDate_NotRequired;
    this.ValidateEmail = ValidateEmail;
    this.ValidateEmail_NotRequired = ValidateEmail_NotRequired;
    this.ValidateConfirmEmail = ValidateConfirmEmail;
    this.ValidateRequired = ValidateRequired;
    this.ValidateRequiredList = ValidateRequiredList;
    this.ValidateNumeric = ValidateNumeric;
    this.ValidateAlpha = ValidateAlpha;
    this.ValidateAlphaNumeric = ValidateAlphaNumeric;
    this.ValidateNoDigits = ValidateNoDigits;
    this.ValidateNoSpaces = ValidateNoSpaces;
    this.ValidateNoCrLf = ValidateNoCrLf;
    
    function ValidateRequired(launchNetElement)
    {
      var value = launchNetElement.GetValue();
      var validationResult = new ValidationResult(launchNetElement);
      if (!this.CommonValidator.IsNotNull(value))
      {
        SetValidationResultSuffix(validationResult, "required");
      }
      validationResult.UpdateDisplay();
      return validationResult.GetValid();
    }
    
    function ValidateRequiredList(launchNetElement)
    {
      var validationResult = new ValidationResult(launchNetElement);
      this.CommonValidator.IsSelectionMade(launchNetElement.Element, validationResult);
      validationResult.UpdateDisplay();
      return validationResult.GetValid();
    }

    function ValidateDate(launchNetElement)
    {
      return this.StandardValidation(launchNetElement, "IsValidDate");
    }
    
    function ValidateDate_NotRequired(launchNetElement)
    {
      return this.StandardValidation(launchNetElement, "IsValidDate_NotRequired");
    }
    
    function ValidateEmail(launchNetElement)
    {
      return this.StandardValidation
        (
        launchNetElement
        ,
          [
          "IsValidEmailAddress"
          , "IsMaxLength 50"
          ]
        );
    }
    
    
    function ValidateEmail_NotRequired(launchNetElement)
    {
      return this.StandardValidation
        (
        launchNetElement
        ,
          [
          "IsValidEmailAddress_NotRequired"
          , "IsMaxLength 50"
          ]
        );
    }

    function ValidateConfirmEmail(launchNetElement)
    {
      
      var originalEmailLaunchNetElement;
      
      var originalEmailLaunchNetElements = GetLaunchNetElementsByValidationType("Email");
      if (originalEmailLaunchNetElements.length < 1)
      {
        originalEmailLaunchNetElement = GetLaunchNetElementsByValidationType("Email_NotRequired")[0];
      }
      else
      {
        originalEmailLaunchNetElement = originalEmailLaunchNetElements[0];
      }
      
      var originalEmailValue = originalEmailLaunchNetElement.GetValue();
      var confirmEmailValue = launchNetElement.GetValue();
      
      var validationResult = new ValidationResult(launchNetElement);
      if (originalEmailValue != confirmEmailValue)
      {
        SetValidationResultSuffix(validationResult, "emails do not match");
      }
      validationResult.UpdateDisplay();
      return validationResult.GetValid();
    }
    
    function ValidateNumeric(launchNetElement)
    {
      return this.StandardValidation(launchNetElement, "IsNumeric");
    }

    function ValidateAlpha(launchNetElement)
    {
      return this.StandardValidation(launchNetElement, "IsAlpha");
    }

    function ValidateAlphaNumeric(launchNetElement)
    {
      return this.StandardValidation(launchNetElement, "IsAlphaNumeric");
    }

    function ValidateNoDigits(launchNetElement)
    {
      return this.StandardValidation(launchNetElement, "HasNoDigits");
    }

    function ValidateNoSpaces(launchNetElement)
    {
      return this.StandardValidation(launchNetElement, "HasNoSpaces");
    }

    function ValidateNoCrLf(launchNetElement)
    {
      return this.StandardValidation(launchNetElement, "HasNoCrLf");
    }
  }
  
  inherit(PlatformServicesValidator, ValidatorBase);

  
/* ***********************************************************************************
-  Page Load Event
************************************************************************************ */

  function OnPageLoad()
  {
  }


/* ***********************************************************************************
-  Page Click Event
************************************************************************************ */

  function OnPageClick(launchNetElementClicked, pageValid)
  {
  }                       

/* ***********************************************************************************
-  Page Validation
************************************************************************************ */

  function OnValidationRequiredCheck(launchNetElementClicked)
  {
    //return true if the launchNetElementClicked indicates a need to perform validation
    //note that launchNetElementClicked is of type LaunchNetElement (see LaunchNetClientBase.js)
    return false;
  }

  function OnValidateLaunchNetElements(validatableLaunchNetElements)
  {
    var launchNetElementsValid = true;
    var validator = new PlatformServicesValidator();
    
    //validate any special elements here
    for (var i = 0; i < validatableLaunchNetElements.length; i++)
    {
      if (!validator.Validate(validatableLaunchNetElements[i]))
      {
        launchNetElementsValid = false;
      }
    }

    return launchNetElementsValid;
  }
}