
//---------------------------------------------------------------------------
// Copyright (c) Knowledge Analysis Technologies, LLC  All Rights Reserved
//
// Proprietary - Use with KATech Written Permission Only
// Module Name:   schoolAdmin.js
// Last Revised:   $Id: schoolAdmin.js,v 1.59 2008/08/14 23:38:40 dooley Exp $

var encryptMap;

function openWindow( url, windowTitle, width, height ) {
  var newWindow =
    window.open( url, windowTitle,
                 "height=" + height + ",width=" + width + 
                 ",menubar=yes,toolbar=no,directories=no,resizable=yes,scrollbars=yes",
                 true );
  newWindow.focus();
}

// Utility function to determine if we are running a version of IE.

function isExplorer() {
  return ( navigator.appName.indexOf( "Microsoft" ) != -1 );
}



// Ensure that the user has entered a user ID and selected (or preselected)
//    a school. 
//
function validateLogin( ) {
  var form = document.forms.loginForm;

  var userID = form.login.value;
  var password = form.password.value;
  var userIDError = false;
  var passwordError = false;
  var schoolSelectionError = false;

  var returnVal = true;

  if ( !userID ) { userIDError = true; }
  if ( !password ) { passwordError = true; }

  // schoolID may be a hidden field or it may be a selection box.
  if (( typeof form.schoolID != "undefined" ) &&
      (typeof form.schoolID.selectedIndex != "undefined" ) &&
      (form.schoolID.selectedIndex < 1)) {
    schoolSelectionError = true;
  }

  if ( userIDError && passwordError ) {
    var message =
      "Please type in your user ID and password and select your " +
      "school from the list.";
    alert( message );
    returnVal = false;
  }
  else if ( userIDError ) {  
    alert( "Please type in your user ID." );
    returnVal = false;
  }
  else if ( passwordError ) {  
    alert( "Please type in your password." );
    returnVal = false;
  }
  else if ( schoolSelectionError ) {
    returnVal = false;
    alert( "Please select a school." );
  }

  return returnVal;
}


//----------------------------------------------------------------------
//-- BEGIN - Registration functions.

function verifyRegistrationSchoolSelection() {
  var form = document.registrationForm;  

  var schoolPIDIndex = form.schoolSelection.selectedIndex;
  var returnVal = true;
  if ( schoolPIDIndex == 0 ) {
    var otherSchool = form.otherSchool.value;
    if ( otherSchool.match( /^\s*$/ ) ) {
      alert( 'You must either select a school from the drop-down list, ' +
             'type in a school name, or click ' +
             '"Back to Step 1" and provide a different school zip code.' );
      returnVal = false;
    }
  }

  return returnVal;
}


function verifyAgreementAcceptance( appName ) {
  var form = document.registrationForm;  

  var checkbox = form.acceptAgreement;
  if ( !checkbox.checked ) {
    alert( 'You must accept the subscriber agreement to register your ' +
           appName + ' purchase.' );
    return false;
  }
  else {
    return true;
  }
}

//-- END - Registration functions.
//----------------------------------------------------------------------
//-- BEGIN - Utility functions.

// Ensure that the author has selected given list item.
//

function validateSelection(element, descriptor) {
  var selectionError = false;
  var returnVal = true;

  if (element.selectedIndex < 1) { selectionError = true; }

  if ( selectionError ) {
    returnVal = false;
    alert( "Please select a " + descriptor + "." );
  }

  return returnVal;
}


//-- END - Utility functions
//----------------------------------------------------------------------

//----------------------------------------------------------------------
//-- BEGIN - Subscription Activator / Account Archiver functions

function confirmSubscriptionActivation( challengeMessage ) {
  return confirm( challengeMessage );
}

//-- END - Subscription Activator / Account Archiver functions
//----------------------------------------------------------------------

//----------------------------------------------------------------------
//-- BEGIN - User Editor functions.


// confirmUserDeletion
//
function confirmUserDeletion( userType ) {
  return (confirm( "Are you sure you want to delete this " + userType +
                   " account?" ));
}


// confirmUserDeactivation
//
function confirmUserDeactivation( userType ) {
  if ( userType == 'student' ) {
    return (confirm( "Deactivating the student account will unenroll the student from all classes and remove the account from the teacher viewable roster.  Please click \"OK\" to confirm." ));
  }
  else if ( userType == 'teacher' ) {
    alert( "Deactivation of teacher accounts is not yet available.");
    return 0;
  }
  else {
    return 0;
  }
}


// User Editor Form method.
//   The user has chosen a different class from the list.
//   Set the selected class ID and submit the form.
//
function userEditClassChange() {
  var form = document.userEditForm;

  var classIDIndex = form.classIDSelection.selectedIndex;
  var classID = form.classIDSelection.options[ classIDIndex ].value;
  if ( form.classID.value != classID ) {
    form.classID.value = classID;
    form.userID.value = -1;
    form.userIDSelection.selectedIndex = 0;
    form.submit();
    return true;
  }
  else {
    return false;
  }
}


// User Editor Form method.
//   The author has chosen a different user (student or teacher) from the list.
//   Set the selected user ID and submit the form.
//
function userEditUserChange() {
  var form = document.userEditForm;

  var userIDIndex = form.userIDSelection.selectedIndex;
  var userID = form.userIDSelection.options[ userIDIndex ].value;
  if ( form.userID.value != userID ) {
    form.userID.value = userID;
    form.submit();
    return true;
  }
  else {
    return false;
  }
}


function verifyDiscardForStudentEdit() {

  return true;
  //return confirm( "Switching to the Student Editor.  Any unsaved student " +
  //"values will be discarded." );
}


function verifyDiscardForClassEdit2() {

  return true;
  //return confirm( "Switching to the Class Editor.  Any unsaved student " +
  //"values will be discarded." );
}


function verifyDiscardForStudentBatchCreate() {

  return true;
  //return confirm( "Switching to the Student Creation Tool.  Any unsaved " +
  //"student values will be discarded." );
}


// prepareForUserSubmission( action );
//   Validate the form values.
//   Set the userClasses hidden parameter to the union of all the values
//   in the userClassSelection list.  Otherwise only the highlighted ones
//   are submitted.

function prepareForUserSubmission( action ) {
  var userForm = document.forms.userEditForm;

  var returnVal = true;
  if ( action == 'delete' ) {
    if ( !confirmUserDeletion( userForm.userType.value ))
      returnVal = false;
  }
  else if ( action == 'deactivate' ) {
    if ( !confirmUserDeactivation( userForm.userType.value ))      
      returnVal = false;
  }
  else { 
    returnVal = validateUserForm( userForm );
  }

  return returnVal;
}


function validateUserForm( userForm ) {

  var userID    = userForm.login.value;
  var password  = userForm.password.value;
  var passwordV = userForm.passwordVerification.value;
  var userType  = userForm.userType.value;

  var name;
  if ( userType == 'student' ) {
    name = userForm.firstName.value;
    if ( name == '' ) {
      name = userForm.lastName.value;
    }
  }
  else {
    name = userForm.name.value;
  }

  var userIDError        = false;
  var nameError          = false;
  var passwordError      = false;
  var passwordMatchError = false;
  var returnVal          = true;

  if ( !userID ) { userIDError = true; }
  if ( !name ) { nameError = true; }
  if ( !password ) { passwordError = true; }

  if ( password != passwordV ) { passwordMatchError = 1; }

  if ( userIDError && passwordError && nameError ) {
    alert( "Please provide a login ID, name and password." );
    returnVal = false;
  }
  else if ( userIDError && passwordError ) {
    alert( "Please provide a login ID and password." );
    returnVal = false;
  }
  else if ( userIDError ) {  
    alert( "Please provide a login ID." );
    returnVal = false;
  }
  else if ( passwordError ) {  
    alert( "Please provide a password." );
    returnVal = false;
  }
  else if ( passwordMatchError ) {
    alert( "Password and password verification do not match.\n" );
    returnVal = false;
  }
  else if ( nameError ) {
    alert( "Please provide a name." );
    returnVal = false;
  }

  return returnVal;
} // validateUserForm

//-- END - User Editor functions.
//----------------------------------------------------------------------

//----------------------------------------------------------------------
//-- BEGIN - Class Editor functions.

// Class Editor Form method.
//   The user has chosen a different class from the list.
//   Set the selected class ID and submit the form.
//
function classEditClassChange() {
  var form = document.classEditForm;

  var classIDIndex = form.classIDSelection.selectedIndex;
  var classID = form.classIDSelection.options[ classIDIndex ].value;
  if ( form.classID.value != classID ) {
    form.classID.value = classID;
    // form target may have been set to unit editor window.  reset it.
    form.target = '';
    form.submit();
    return true;
  }
  else {
    return false;
  }
} // classEditClassChange

// Class Editor Form method.
//   The user has chosen a different class from the list.
//   Set the selected class ID and submit the form.
//
function classEditClassChangeForSearchBox(divID, formName) {
  var form = document.getElementsByName(formName).item(0);
  var currentClassIDSelection = 'classIDSelection_'+divID;
  var classIDSelectionElement = document.getElementsByName(currentClassIDSelection).item(0);
  var classIDIndex = classIDSelectionElement.selectedIndex;
  var classID = classIDSelectionElement.options[ classIDIndex ].value;
  var selectedClassFromSearchElement = document.getElementsByName('selectedClassFromSearch').item(0);
  if ( selectedClassFromSearchElement.value != classID ) {
    selectedClassFromSearchElement.setAttribute('value', classID);
    // form target may have been set to unit editor window.  reset it.
    return true;
  }
  else {
    return false;
  }
} //classEditClassChangeForSearchBox

// moveSelectionItems
//    Move the selected items to/from one a source list to a
//    destination list.  This is used by multiple authoring tools.
//
function moveSelectionItems( sourceList, destinationList ) {
    
  var destIndex = destinationList.options.length;

  // Copy and delete from the bottom of the list up.  This allows the 
  // shrinking of the source list to not have ill effects on the loop index.
  //
  var newOption;
  var oldOption;
  for (var i = (sourceList.options.length - 1) ; i >= 0; i--) {
    //confirm ("i is " + i);
    //confirm ("option is " + sourceList.options[i] );
    if ( sourceList.options[i].selected ) {
      oldOption = sourceList.options[i];
      //confirm ("old option is " + oldOption );
      newOption = new Option(oldOption.text, oldOption.value, false, true);
      //confirm ("new option is " + newOption );
      destinationList.options[ destIndex ] = newOption;
      destIndex ++;
      //confirm( "copied " + sourceList.options[i].text +
      //"to " + destinationList.name );
      //confirm ("setting option to null");
      sourceList.options[i] = null;
      //confirm ("finished setting option to null");
    }
  }
  return false;   // so that the form isn't submitted.
} // moveSelectionItems

//-- END - Class Editor functions.
//----------------------------------------------------------------------

//----------------------------------------------------------------------
//-- BEGIN - Student Archiver Methods.

// Student Archiver method.
//   The user has chosen a different class from the list.
//   Set the selected student ID and submit the form.
//
function studentArchiverClassChange() {
  var form = document.studentArchiverForm;

  var classIDIndex = form.classIDSelection.selectedIndex;
  var classID = form.classIDSelection.options[ classIDIndex ].value;
  if ( form.classID.value != classID ) {
    form.classID.value = classID;
    form.submit();
    return true;
  }
  else {
    return false;
  }
}

// Student Archiver method.
//   The user has chosen a different student from the list.
//   Update the submit button
//
function studentArchiverStudentChange() {
  var form = document.studentArchiverForm;

  var button = form.archiveStudents;
  var studentIDIndex = form.studentIDSelection.selectedIndex;
  var studentID = form.studentIDSelection.options[ studentIDIndex ].value;
  var classIDIndex = form.classIDSelection.selectedIndex;
  var classID = form.classIDSelection.options[ classIDIndex ].value;

  if ( studentID == -2 ) {
    if ( classID == -2 )
      button.value = 'Archive Students in All Classes';
    else
      button.value = 'Archive All Students Class';
  }
  else {
    button.value = 'Archive Student';
  }
}


// Ask the user to confirm their account archiving/deletion
// decision.

function studentArchiverConfirmation( message ) {

  return confirm( message );
}

//-- END - Student Archiver Methods.
//----------------------------------------------------------------------
