1// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var gaia = gaia || {};
6gaia.chromeOSLogin = {};
7
8gaia.chromeOSLogin.parent_page_url_ =
9'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik/main.html';
10
11gaia.chromeOSLogin.attemptLogin = function(email, password, attemptToken) {
12  var msg = {
13    'method': 'attemptLogin',
14    'email': email,
15    'password': password,
16    'attemptToken': attemptToken
17  };
18  window.parent.postMessage(msg, gaia.chromeOSLogin.parent_page_url_);
19};
20
21gaia.chromeOSLogin.clearOldAttempts = function() {
22  var msg = {
23    'method': 'clearOldAttempts'
24  };
25  window.parent.postMessage(msg, gaia.chromeOSLogin.parent_page_url_);
26};
27
28gaia.chromeOSLogin.onAttemptedLogin = function(emailFormElement,
29                                               passwordFormElement,
30                                               continueUrlElement) {
31    var email = emailFormElement.value;
32    var passwd = passwordFormElement.value;
33    var attemptToken = new Date().getTime();
34
35    gaia.chromeOSLogin.attemptLogin(email, passwd, attemptToken);
36
37    if (continueUrlElement) {
38      var prevAttemptIndex = continueUrlElement.value.indexOf('?attemptToken');
39      if (prevAttemptIndex != -1) {
40        continueUrlElement.value =
41            continueUrlElement.value.substr(0, prevAttemptIndex);
42      }
43      continueUrlElement.value += '?attemptToken=' + attemptToken;
44    }
45}
46