var remoteLogin, trim, validateEmail, validateEmpty, validatePassword, validateUsername;
var $j = jQuery.noConflict();

validateEmpty = function(fld) {
  var error;
  error = "";
  if (fld.length === 0) {
    error = "The required field has not been filled in.<br />";
  }
  return error;
};
validateUsername = function(fld) {
  var error, illegalChars;
  error = "";
  illegalChars = /\W/;
  if (fld === "") {
    error = "You didn't enter a username.<br />";
  } else if ((fld.length < 3) || (fld.length > 30)) {var remoteLogin, trim, validateEmail, validateEmpty, validatePassword, validateUsername;
validateEmpty = function(fld) {
  var error;
  error = "";
  if (fld.length === 0) {
    error = "The required field has not been filled in.<br />";
  }
  return error;
};
validateUsername = function(fld) {
  var error, illegalChars;
  error = "";
  illegalChars = /\W/;
  if (fld === "") {
    error = "You didn't enter a username.<br />";
  } else if ((fld.length < 3) || (fld.length > 30)) {
    error = "The username has a wrong length.<br />";
  } else if (illegalChars.test(fld)) {
    error = "The username contains illegal characters.<br />";
  }
  return error;
};
validatePassword = function(fld) {
  var error, illegalChars;
  error = "";
  illegalChars = /[\W]/;
  if (fld === "") {
    error = "You didn't enter a password.<br />";
  } else if ((fld.length < 3) || (fld.length > 30)) {
    error = "The password has a wrong length. <br />";
  } else if (illegalChars.test(fld)) {
    error = "The password contains illegal characters.\n";
  } else if (!((fld.search(/(a-z)+/)) && (fld.search(/(0-9)+/)))) {
    error = "The password must contain at least one numeral.<br />";
  }
  return error;
};
trim = function(s) {
  return s.replace(/^\s+|\s+$/, "");
};
validateEmail = function(fld) {
  var emailFilter, error, illegalChars, tfld;
  error = "";
  tfld = trim(fld);
  emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
  illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
  if (fld === "") {
    error = "You didn't enter an email address.<br />";
  } else if (!emailFilter.test(tfld)) {
    error = "Please enter a valid email address.<br />";
  } else if (fld.match(illegalChars)) {
    error = "The email address contains illegal characters.<br />";
  }
  return error;
};
remoteLogin = function(sessionId) {
  var url;
  url = "https://www.vertexdigitalsports.com/api/user/remoteLogin?sessionId=" + sessionId;
  return document.location.href = url;
};
$j(document).ready(function() {
  var $dialog1, $dialog2, $dialog3, forgotFormContent, loginFormContent, msgSentContent;
  loginFormContent = "<div class='ui-dialog-error' id='login-error' style='display:none;'></div><div class='ui-dialog-form'><p><label for='txt-username' class='ui-dialog-label'>Username</label><br /><input type='text' name='username' id='txt-username' class='ui-dialog-text'></p><p><label class='ui-dialog-label'>Password</label><il><a href='#' id='passwordForgotten' class='ui-dialog-link'> ( Forgot password ? )</a></il><br /><input type='password' name='password' id='txt-password' class='ui-dialog-password'></p><p><input id='loginForm' type='submit' class='ui-dialog-submit' value='Login'></p></div>";
  forgotFormContent = "<div class='ui-dialog-message' id='forgot-message'  style='display:none;'></div><div class='ui-dialog-error' id='forgot-error'  style='display:none;'></div><div class='ui-dialog-form'><p><label for='txt-email' class='ui-dialog-label'>Email</label><br /><input type='text' name='email' id='txt-email' class='ui-dialog-text'></p><p><input id='forgotForm' type='submit' class='ui-dialog-submit' value='Submit'></p></div>";
  msgSentContent = "<div class='ui-dialog-message' id='forgot-message'  style='display:none;'></div>";
  $dialog1 = $j("<div></div>").html(loginFormContent).dialog({
    autoOpen: false,
    resizable: false,
    title: "eAcademy Login"
  });
  $dialog2 = $j("<div></div>").html(forgotFormContent).dialog({
    autoOpen: false,
    resizable: false,
    title: "Forgot Password"
  });
  $dialog3 = $j("<div></div>").html(msgSentContent).dialog({
    autoOpen: false,
    resizable: false,
    title: "Forgot Password"
  });
  $j(".eacademy-login").click(function() {
    $dialog1.dialog("open");
    return false;
  });
  $j("input[type='submit']#loginForm}").live("click", function(e) {
    var data, error, password, username;
    username = $j("input[type='text']#txt-username").val();
    password = $j("input[type='password']#txt-password").val();
    error = validateUsername(username) + validatePassword(password);
    if (error.length > 0) {
      return $j("div#login-error").html(error).show();
    } else {
      data = {
        authentication: [
          {
            type: 'credentials',
            id: username,
            secret: password
          }
        ],
        expiry: 600,
        language: 'en'
      };
      data = JSON.stringify(data);
      return $j.ajax({
        cache: false,
        crossDomain: true,
        processData: false,
        contentType: "application/json",
        dataType: "jsonp",
        data: data,
        jsonp: "callback",
        url: "https://www.vertexdigitalsports.com/api/secure/session/create",
        success: function(json) {
          if (json.error === void 0) {
            return document.location.href = "http://www.vertexdigitalsports.com/api/user/remoteLogin?sessionId=" + json.sessionId;
          } else {
            $j('.ui-dialog-error').html('authentication failed');
            return $j('.ui-dialog-error').show();
          }
        },
        error: function(xhr, err, e) {
          $j('.ui-dialog-error').text = "System error 1. Please get in touch with your administrator";
          return $j('.ui-dialog-error').show();
        }
      });
    }
  });
  $j('#passwordForgotten').click(function() {
    $dialog1.dialog("close");
    $dialog2.dialog("open");
    return false;
  });
  return $j("input[type='submit']#forgotForm}").live("click", function(e) {
    var data, email, error;
    email = $j("input[type='text']#txt-email").val();
    error = validateEmail(email);
    if (error.length > 0) {
      return $j("div#forgot-error").html(error).show();
    } else {
      data = {
        authentication: [
          {
            type: 'public',
            id: 'ignored'
          }
        ],
        emailAddr: email
      };
      data = JSON.stringify(data);
      return $j.ajax({
        cache: false,
        crossDomain: true,
        processData: false,
        contentType: "application/json",
        dataType: "jsonp",
        data: data,
        jsonp: "callback",
        url: "https://www.vertexdigitalsports.com/api/secure/user/emailCredentials",
        success: function(json) {
          if (json.error === void 0) {
            $dialog2.dialog("close");
            $dialog3.dialog("open");
            $j('.ui-dialog-message').html('Thank you for your request. <br />Your account details will be sent to you shortly.');
            return $j('.ui-dialog-message').show();
          } else {
            $j('.ui-dialog-error').html('Invalid Email Address');
            return $j('.ui-dialog-error').show();
          }
        },
        error: function(xhr, err, e) {
          $j('.ui-dialog-error').text = "System error 1. Please get in touch with your administrator";
          return $j('.ui-dialog-error').show();
        }
      });
    }
  });
});
    error = "The username has a wrong length.<br />";
  } else if (illegalChars.test(fld)) {
    error = "The username contains illegal characters.<br />";
  }
  return error;
};
validatePassword = function(fld) {
  var error, illegalChars;
  error = "";
  illegalChars = /[\W]/;
  if (fld === "") {
    error = "You didn't enter a password.<br />";
  } else if ((fld.length < 3) || (fld.length > 30)) {
    error = "The password has a wrong length. <br />";
  } else if (illegalChars.test(fld)) {
    error = "The password contains illegal characters.\n";
  } else if (!((fld.search(/(a-z)+/)) && (fld.search(/(0-9)+/)))) {
    error = "The password must contain at least one numeral.<br />";
  }
  return error;
};
trim = function(s) {
  return s.replace(/^\s+|\s+$/, "");
};
validateEmail = function(fld) {
  var emailFilter, error, illegalChars, tfld;
  error = "";
  tfld = trim(fld);
  emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
  illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
  if (fld === "") {
    error = "You didn't enter an email address.<br />";
  } else if (!emailFilter.test(tfld)) {
    error = "Please enter a valid email address.<br />";
  } else if (fld.match(illegalChars)) {
    error = "The email address contains illegal characters.<br />";
  }
  return error;
};
remoteLogin = function(sessionId) {
  var url;
  url = "https://www.vertexdigitalsports.com/api/user/remoteLogin?sessionId=" + sessionId;
  return document.location.href = url;
};


$j(document).ready(function() {
  var $dialog1, $dialog2, $dialog3, forgotFormContent, loginFormContent, msgSentContent;
  loginFormContent = "<div class='ui-dialog-error' id='login-error' style='display:none;'></div><div class='ui-dialog-form'><p><label for='txt-username' class='ui-dialog-label'>Username</label><br /><input type='text' name='username' id='txt-username' class='ui-dialog-text'></p><p><label class='ui-dialog-label'>Password</label><il><a href='#' id='passwordForgotten' class='ui-dialog-link'>(Forgot password?)</a></il><br /><input type='password' name='password' id='txt-password' class='ui-dialog-password'></p><p><input id='loginForm' type='submit' class='ui-dialog-submit' value='Login'></p></div>";
  forgotFormContent = "<div class='ui-dialog-error' id='forgot-error'  style='display:none;'></div><div class='ui-dialog-form'><p><label for='txt-email' class='ui-dialog-label'>Email</label><br /><input type='text' name='email' id='txt-email' class='ui-dialog-text'></p><p><input id='forgotForm' type='submit' class='ui-dialog-submit' value='Submit'></p></div>";
  msgSentContent = "<div class='ui-dialog-message' id='forgot-message'  style='display:none;'></div>";
  $dialog1 = $j("<div></div>").html(loginFormContent).dialog({
    autoOpen: false,
    resizable: false,
    title: "eAcademy Login"
  });
  $dialog2 = $j("<div></div>").html(forgotFormContent).dialog({
    autoOpen: false,
    resizable: false,
    title: "Forgot Password"
  });
  $dialog3 = $j("<div></div>").html(msgSentContent).dialog({
    autoOpen: false,
    resizable: false,
    title: "Forgot Password"
  });
  $j(".eacademy-login").click(function() {
    $dialog1.dialog("open");
    return false;
  });
  $j("input[type='submit']#loginForm}").live("click", function(e) {
    var data, error, password, username;
    username = $j("input[type='text']#txt-username").val();
    password = $j("input[type='password']#txt-password").val();
    error = validateUsername(username) + validatePassword(password);
    if (error.length > 0) {
      return $j("#login-error").html(error).show();
    } else {
      data = {
        authentication: [
          {
            type: 'credentials',
            id: username,
            secret: password
          }
        ],
        expiry: 600,
        language: 'en'
      };
      data = JSON.stringify(data);
      return $j.ajax({
        cache: false,
        crossDomain: true,
        processData: false,
        contentType: "application/json",
        dataType: "jsonp",
        data: data,
        jsonp: "callback",
        url: "https://www.vertexdigitalsports.com/api/secure/session/create",
        success: function(json) {
          if (json.error === void 0) {
            return document.location.href = "http://www.vertexdigitalsports.com/api/user/remoteLogin?sessionId=" + json.sessionId;
          } else {
            $j('#login-error').html('authentication failed');
            return $j('#login-error').show();
          }
        },
        error: function(xhr, err, e) {
          $j('#login-error').text = "System error 1. Please get in touch with your administrator";
          return $j('#login-error').show();
        }
      });
    }
  });
  $j('#passwordForgotten').click(function() {
    $dialog1.dialog("close");
    $dialog2.dialog("open");
    return false;
  });
  return $j("input[type='submit']#forgotForm}").live("click", function(e) {
    var data, email, error;
    email = $j("input[type='text']#txt-email").val();
    error = validateEmail(email);
    if (error.length > 0) {
      return $j("#forgot-error").html(error).show();
    } else {
      data = {
        authentication: [
          {
            type: 'public',
            id: 'ignored'
          }
        ],
        emailAddr: email
      };
      data = JSON.stringify(data);
      return $j.ajax({
        cache: false,
        crossDomain: true,
        processData: false,
        contentType: "application/json",
        dataType: "jsonp",
        data: data,
        jsonp: "callback",
        url: "https://www.vertexdigitalsports.com/api/secure/user/emailCredentials",
        success: function(json) {
          if (json.error === void 0) {
            $dialog2.dialog("close");
            $dialog3.dialog("open");
            $j('#forgot-message').html('Thank you for your request. <br />Your account details will be sent to you shortly.');
            return $j('#forgot-message').show();
          } else {
            $j('#forgot-error').html('Invalid Email Address');
            return $j('#forgot-error').show();
          }
        },
        error: function(xhr, err, e) {
          $j('#forgot-error').text = "System error 1. Please get in touch with your administrator";
          return $j('#forgot-error').show();
        }
      });
    }
  });
});
