﻿/******************************************************
Contains functions used in conjunction with the 
signup control, as defined in signup.ascx.

Note: also used on the authenticated Sign Up page,
as defined in signup.aspx.

Requires validators.js.
*******************************************************/
if ( typeof Validators == 'undefined' ||
     parseFloat( Validators.Version.split(".")[0] + "." +
                 Validators.Version.split(".")[1]) < 1.0 )
{
   throw ("Requires the Validators.js >= 1.0.0");
}

var SignUp =
{
   Version: "1.0.0"
}

function hasUserInput(id)
{
   value = document.getElementById(id).value;

   if (value.length > 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

function checkEmailValue(value, ID, showInlineErrors)
{
   if (!isValidEmailString(value))
   {
      displayErrorBoxEmail(showInlineErrors);
      showError(ID);
      return false;
   }
   else
   {
      hideErrorBoxEmail(showInlineErrors);
      showOK(ID);
      return true;
   }
}

function checkStringValue(value, ID, showInlineErrors)
{
   if (!isValidNameString(value))
   {
      displayErrorBoxDisplayName(showInlineErrors);
      showError(ID);
      return false;
   }
   else
   {
      hideErrorBoxDisplayName(showInlineErrors);
      showOK(ID);
      return true;
   }
}

function checkLicenseValue(key, ID, showInlineErrors)
{
   if (!isValidLicenseKey(key))
   {
      displayErrorBoxLicenseKey(showInlineErrors);
      showError(ID);
      return false;
   }
   else
   {
      hideErrorBoxLicenseKey(showInlineErrors);
      showOK(ID);
      return true;
   }
}

function checkGoogleAnalyticsKey(value, Id, showInlineErrors)
{
    if (!isValidGoogleAnalyticsKey(value)) {
        displayErrorBoxGoogleAnalyticsKey(showInlineErrors);
        showError(Id);
        return false;
    }
    else {
        hideErrorBoxGoogleAnalyticsKey(showInlineErrors);
        showOK(Id);
        return true;
    }
}

function checkPasswordReentry(id1, id2, showInlineErrors)
{
   var doPasswordsMatch = (document.getElementById(id1).value == document.getElementById(id2).value);

   if (doPasswordsMatch)
   {
      var returnVal = true;

      if (!isValidPasswordString(document.getElementById(id1).value))
      {
         displayErrorBoxPassword(showInlineErrors);
         showError(id1);
         returnVal = false;
      }
      else
      {
         showOK(id1);
      }

      if (!isValidPasswordString(document.getElementById(id2).value))
      {
         displayErrorBoxPassword(showInlineErrors);
         showError(id2);
         returnVal = false;
      }
      else
      {
         showOK(id2);
      }

      if (returnVal)
      {
         hideErrorBoxPassword(showInlineErrors);
      }
      return returnVal;
   }
   else
   {
      displayErrorBoxPassword(showInlineErrors);
      showError(id1);
      showError(id2);
      return false;
   }
}

function hide(ID)
{
   showOK(ID);
   document.getElementById(ID + "OK").style.display = 'none';
}

function showError(ID)
{
   $("#" + ID + "Invalid").css( "display", "inline" );
   $("#" + ID + "OK").css( "display", "none" );
   $("#" + ID).addClass("invalidEntry");
}

function showOK(ID)
{
   $("#" + ID + "Invalid").css("display", "none");
   $("#" + ID + "OK").css("display", "inline");
   $("#" + ID).removeClass("invalidEntry");
}

function showClear(ID)
{
   document.getElementById(ID + "Invalid").style.display = 'none';
   document.getElementById(ID + "OK").style.display = 'none';
   $("#" + ID).removeClass("invalidEntry");
}

function showErrors() {
   var shouldClearAllMessages = !emailErrorShowing && !displayNameErrorShowing && !passwordErrorShowing && !licenseKeyErrorShowing && !googleAnalyticsKeyErrorShowing;
   if (shouldClearAllMessages) {
      clearMessages();
      return;
   }

   if (emailErrorShowing) {
      displaySignupError("email");
   }
   else if (passwordErrorShowing) {
      displaySignupError("password");
   }
   else if (displayNameErrorShowing) {
      displaySignupError("displayName");
   }
   else if (licenseKeyErrorShowing) {
      displaySignupError("key");
   }
   else if (googleAnalyticsKeyErrorShowing) {
      displaySignupError("googleAnalyticsKey");
   }
}

var emailErrorShowing = false;
function displayErrorBoxEmail(showInlineError)
{
   emailErrorShowing = true;
   
   if (showInlineError && showInlineError == "False")
   {
      showErrors();
   }
}

function hideErrorBoxEmail(showInlineError)
{
   emailErrorShowing = false;
   if (showInlineError && showInlineError == "False")
   {
      showErrors();
   }
}

var displayNameErrorShowing = false;
function displayErrorBoxDisplayName(showInlineError)
{
   displayNameErrorShowing = true;
   if (showInlineError && showInlineError == "False")
   {
      showErrors();
   }
}

function hideErrorBoxDisplayName(showInlineError)
{
   displayNameErrorShowing = false;
   if (showInlineError && showInlineError == "False")
   {
      showErrors();
   }
}

var passwordErrorShowing = false;
function displayErrorBoxPassword(showInlineError)
{
   passwordErrorShowing = true;
   if (showInlineError && showInlineError == "False")
   {
      showErrors();
   }
}

function hideErrorBoxPassword(showInlineError)
{
   passwordErrorShowing = false;
   if (showInlineError && showInlineError == "False")
   {
      showErrors();
   }
}

var licenseKeyErrorShowing = false;
function displayErrorBoxLicenseKey(showInlineError)
{
   licenseKeyErrorShowing = true;
   if (showInlineError && showInlineError == "False")
   {
      showErrors();
   }
}

function hideErrorBoxLicenseKey(showInlineError)
{
   licenseKeyErrorShowing = false;
   if (showInlineError && showInlineError == "False")
   {
      showErrors();
   }
}

var googleAnalyticsKeyErrorShowing = false;
function displayErrorBoxGoogleAnalyticsKey(showInlineError)
{
    googleAnalyticsKeyErrorShowing = true;
    if (showInlineError && showInlineError == "False") {
        showErrors();
    }
}

function hideErrorBoxGoogleAnalyticsKey(showInlineError)
{
    googleAnalyticsKeyErrorShowing = false;
    if (showInlineError && showInlineError == "False") {
        showErrors();
    }
}
