﻿///////////////////////////////////////////////////////////////////////////////
//
//  wcoSecurityManager.js
//
// © 2007-2010 Wco iEnterprise Solutions Pty Ltd. ALL RIGHTS RESERVED.
// This file is licensed as part of the DataPortal 3.0 Web Presence Solution, for details look here: http://www.wco.com.au
//
///////////////////////////////////////////////////////////////////////////////

HtmlSecurityManagerObject = function (parentObject) {
  this.parent = parentObject;
  this.isOpen = false;
  this.session = null;
  this.roles = null;

  var html = '<div id="securityContainer">';
  html += '<table id="signinLinkBkg">';
  html += '<tr>';
  html += '<td align="center" valign="top" nowrap="true" style="width:100%;">';
  
  html += '<p id="signinLink"><a hidefocus="true" href="javascript:void(0);">SIGN IN</a></p>';
  
  html += '</td>';
  html += '</tr>';
  html += '</table>';
  html += '</div>';

  $('#securityElement').append(html);

  // activate the click event to display the popup signin dialog
  $('#signinLink').click(function (e) {
	  if (!e) var e = window.event;
	  e.cancelBubble = true;
	  if (e.stopPropagation) e.stopPropagation();
      
    pageManager.securityManager.displaySigninDlg();
  });
}

HtmlSecurityManagerObject.prototype = {
  init: function (websiteData) {

    var html = '<div id="signinContainer">';
    html += '<table border="0" style="width:100%; padding:10px 10px 0 10px; margin:0;">';
    html += '<tr>';
    html += '<td align="right" valign="middle" nowrap="true" style="width:30%;">';
    html += '<p class="signinLabel">Email Address:</p>';
    html += '</td>';
    html += '<td align="left" valign="middle" nowrap="true" style="width:70%;">';
    html += '<input id="signinEmailAddress" class="signinInput" type="text" value="" >';
    html += '</td>';
    html += '</tr>';
    html += '<tr>';
    html += '<td align="right" valign="middle" nowrap="true" style="width:30%;">';
    html += '<p class="signinLabel">Password:</p>';
    html += '</td>';
    html += '<td align="left" valign="middle" nowrap="true" style="width:70%;">';
    html += '<input id="signinPassword" class="signinInput" type="password" value="" >';
    html += '</td>';
    html += '</tr>';
    html += '<tr>';
    html += '<td align="right" valign="middle" nowrap="true" colspan="2" style="width:100%;">';

    html += '<table border="0" style="padding:0; margin:0;">';
    html += '<tr>';
    html += '<td align="right" valign="middle" nowrap="true">';
    html += '<button id="signinBtnCancel" type="button" value="Cancel">Cancel</button>';
    html += '</td>';
    html += '<td align="right" valign="middle" nowrap="true">';
    html += '<button id="signinBtnOk" type="submit" value="OK">OK</button>';
    html += '</td>';
    html += '</tr>';
    html += '</table>';
    
    html += '</td>';
    html += '</tr>';
    html += '</table>';
    html += '</div>';

    $('#securityElement').append(html);

    // activate the click event for the Cancel button
    $('#signinBtnCancel').click(function (e) {
	    if (!e) var e = window.event;
	    e.cancelBubble = true;
	    if (e.stopPropagation) e.stopPropagation();
      
      pageManager.securityManager.displaySigninDlg();
    });

    // activate the click event for the OK button
    $('#signinBtnOk').click(function (e) {
	    if (!e) var e = window.event;
	    e.cancelBubble = true;
	    if (e.stopPropagation) e.stopPropagation();
      
      pageManager.securityManager.executeSignin();
    });
  },

  displaySigninDlg: function () {
    // clear the signin input controls
    $('#signinEmailAddress')[0].value = '';
    $('#signinPassword')[0].value = '';

    $('#signinContainer').toggle("slow", createDelegate(this, this.initSigninDlg));

  },

  initSigninDlg: function () {
    // set the focus to the first input control
    $('#signinEmailAddress').focus();
  },

  executeSignin: function () {
    var userEmail = $('#signinEmailAddress')[0].value;
    var userPassword = $('#signinPassword')[0].value;
    var persistenceLevel = 0;

    //alert('Do signin!\nEmail Address: ' + userEmail + '\nPassword: ' + userPassword);
    //dataportal.fetchCurrentObject = function (userEmail,
    //                                          userPassword,
    //                                          persistenceLevel) {
    dataportal.setCallback(createDelegate(this, this.processSignin));
    dataportal.signIn(userEmail, userPassword, persistenceLevel);

  },

  processSignin: function () {
    //alert('here');
    //var currentObject = arguments[0];
    //var childCollection = arguments[1];
    this.session = arguments[2];
    this.roles = this.session.UserRoles;
//    for (var r = 0; r < this.roles.length; r++) {
//      alert(this.roles[r]);
//    }
    
    $('#signinLink a').html('SIGN OUT')
    $('#signinContainer').toggle("slow");
  },

  isInRole: function (roleName) {
    var isRoleFound = false;

//    if (dataportal.session.UserIsAuthenticated) {
//      for (var r = 0; r < this.roles.length; r++) {
//        if (this.roles[r] == roleName) {
//          isRoleFound = true;
//          return isRoleFound;
//        }
//      }
//    }
//    else {
//      var roles = dataportal.session.UserRoles;
//      for (var r = 0; r < roles.length; r++) {
//        if (roles[r] == roleName) {
//          isRoleFound = true;
//          return isRoleFound;
//        }
//      }
//    }
    
    var roles = dataportal.session.UserRoles;
    for (var r = 0; r < roles.length; r++) {
      if (roles[r] == roleName) {
        isRoleFound = true;
        return isRoleFound;
      }
    }

    return isRoleFound;
  }

}


