//-----------------------------------------------------------
// login_validation.js
// 
// Version 20120213
//
// This code was originally embedded in asp code and was externalized
// for ease of understanding and manipulation.  It didn't have to be
// but it sure is easier to work with here.
//
// 20120213:dms:changed "logout.asp" to "endSession.asp"
// 20100909:dms:fixed a bug I was seeing in ChangePwd by checking for undefined form prior to looking at form fields
// 20101124:dms:added script evaluation param to ajax calls
//              added url parameter to pass to ChangePwd on cancel
// 20101127:dms:fixed syntax error - search:"Your password will expire in" - concatenating with & rather than +
//-----------------------------------------------------------
function Form1_Validator(event) {
   
   // Purpose:  Validate user responses on client side
   //
   // Arguments: event:  The event that was triggered to cause the validation
   //
   // Returns:   true or false based on the results of the cursory validation and that performed by legacy submitPushed

   var frm = Event.element(event);                         // get the form object that the event was triggered on

   Event.stop(event);                                      // stop bubble-up event processing since we'll be handling it from here

   if (frm.Username.value == '') {
      alert('Please enter a value for the "Username" field.');
      frm.Username.focus();
      return (false);
   }
   
   if (frm.Password.value == '') {
      alert('Please enter a value for the "Password" field.');
      frm.Password.focus();
      return (false);
   }
   return true;
}
//-----------------------------------------------------------
function ChangePwd(myForm, sMode, iDaysLeft, sUrl) {
   // Purpose:  Handle expiring password; Invoked on load
   //
   // Arguments: sMode:      indicates the prompt message: TIMEOUT, INVALID, DISABLED, EXPIRE, or WELCOME.
   //            iDaysLeft:  indicates the number of days left before password expires
   //
   // Returns:   true or false based on the user response to popup dialog
   var myForm = $(myForm)

   if (sMode == "EXPIRE") {
      var chg;

      if (iDaysLeft > 0) {
         chg = confirm('Your password will expire in ' + iDaysLeft + ' days.\nDo you want to change it now?');
         if (chg) new Ajax.Updater({success: 'body_content', failure: 'error' }, 'password.asp?ID=LOGIN', {evalScripts: "true"}); 
         else new Ajax.Updater({success: 'body_content', failure: 'error' }, 'retailer_menu.asp', {evalScripts: "true"});
      } else {
         chg = confirm('Your password has expired.\nYou must change it before you can access the application.');
         if (chg) new Ajax.Updater({success: 'body_content', failure: 'error' }, 'password.asp?ID=EXPIRED', {evalScripts: "true"});
         else {
            new Ajax.Updater({ success: 'body_content', failure: 'error' }, 'endSession.asp');
            location.replace(sUrl);
         }
      }

      if (iDaysLeft > 0) {
      } else {
         return (chg);
      }
   }
   if (myForm != undefined) {
      if (myForm.Username.value == '') {myForm.Username.focus();myForm.Username.className='highlightActiveField'; }
      else {myForm.Password.focus();myForm.Password.className='highlightActiveField'; }
   }
   return true;
}	
