// SmartScroll methods
var msSmartScrollID;

function RecordSmartScrollInfo() {
   var ctl = event.srcElement;
   var sID = ctl.id;
   var hidX, hidY;

   if (sID != null) {
      hidX = document.getElementById(sID + '_hidScrollX');
      hidY = document.getElementById(sID + '_hidScrollY');
   }
   else {
      hidX = document.getElementById(msSmartScrollID + '_hidScrollX');
      hidY = document.getElementById(msSmartScrollID + '_hidScrollY');
   }

   if (hidX != null) hidX.value = ctl.scrollLeft.toString();
   if (hidY != null) hidY.value = ctl.scrollTop.toString();
}

function SmartScrollLoad() {
   var iss = document.getElementById(msSmartScrollID);
   if (iss == null) return;

   var bTrackHoriz = (iss.attributes['TrackHoriz'].value == 'True');
   var bTrackVert  = (iss.attributes['TrackVert'].value  == 'True');
   var bTrackFocus = (iss.attributes['TrackFocus'].value == 'True');

   var hidX = iss.firstChild;
   var hidY = hidX.nextSibling;
   var hidF = hidY.nextSibling;

   var nX = (bTrackHoriz && (hidX != null) && (hidX.value != '')) ? parseFloat(hidX.value) : 0;
   var nY = (bTrackVert  && (hidY != null) && (hidY.value != '')) ? parseFloat(hidY.value) : 0;

   window.scrollTo(nX, nY);

   // scroll registered controls
   var sCtlID = '';
   var ctl = null;
   var spn = hidF.nextSibling;
   
   while (spn != null) {
      // get scroll info, find window, scrollTo
      sCtlID = GetAttribute(spn, 'ControlID');
      ctl = document.getElementById(sCtlID);

      if (ctl != null) {
         hidX = spn.firstChild;
         hidY = hidX.nextSibling;

         nX = ((hidX != null) && (hidX.value != '')) ? parseFloat(hidX.value) : 0;
         nY = ((hidY != null) && (hidY.value != '')) ? parseFloat(hidY.value) : 0;

         ctl.scrollLeft = nX;
         ctl.scrollTop  = nY;
         ctl.attachEvent('onscroll', RecordSmartScrollInfo);
      }

      spn = spn.nextSibling;
   }

   if (bTrackFocus && (hidF != null) && (hidF.value != '')) {
      var bFocusNext = (iss.attributes['FocusNext'].value == 'True');
      var ctlF = document.getElementById(hidF.value);

      if (bFocusNext && (ctlF != null)) {
         var bCurrFocusFound = false;
         var ctl;

         for (var i = 0; i < document.forms[0].elements.length; i++) {
            ctl = document.forms[0].elements[i];

            if (bCurrFocusFound) {
               if ((ctl.type == 'text') || (ctl.type == 'password') || (ctl.type == 'file') || 
                     (ctl.type == 'checkbox') || (ctl.type == 'radio') || (ctl.type == 'textarea') || 
                     (ctl.type == 'select-one') || (ctl.type == 'select-multiple')) {
                  try {
                     ctl.focus();
                     ctlF = ctl;    // this only happens if the control can be focused
                     ctl.select();  // this only happens if the control can be selected
                     break;
                  }
                  catch (ex) {
                     // can't focus -- skip it
                  }
               }
            }
            else {
               if (ctl.id.toUpperCase() == ctlF.id.toUpperCase()) bCurrFocusFound = true;
            }
         }
      }

      if (ctlF != null) {
         try {
            ctlF.focus();

            if ((ctl.type == 'text') || (ctl.type == 'password') || (ctl.type == 'file') || 
                  (ctl.type == 'checkbox') || (ctl.type == 'radio') || (ctl.type == 'textarea') || 
                  (ctl.type == 'select-one') || (ctl.type == 'select-multiple')) {
               ctlF.select();
            }
         }
         catch (ex) {
            // ignore if fails
         }
      }
   }
   else {
      var bAutoFocus = (iss.attributes['AutoFocus'].value == 'True');

      if (bAutoFocus) {
         var ctl;
         var bHideFocus;

         for (var i = 0; i < document.forms[0].elements.length; i++) {
            ctl = document.forms[0].elements[i];

            bHideFocus = (ctl.attributes['hideFocus'] == null) ? false : (ctl.attributes['hideFocus'].value.toUpperCase() == 'TRUE');

            if (!bHideFocus && ((ctl.type == 'text') || (ctl.type == 'password') || (ctl.type == 'file') || 
                  (ctl.type == 'checkbox') || (ctl.type == 'radio') || (ctl.type == 'textarea') || 
                  (ctl.type == 'select-one') || (ctl.type == 'select-multiple'))) {
               try {
                  ctl.focus();
                  if (ctl.select) ctl.select();
                  break;
               }
               catch (ex) {
                  // can't focus -- skip it
               }
            }
         }
      }
   }

   document.body.attachEvent('onscroll', RecordSmartScrollInfo);
   setInterval('SmartScrollUnload()',50);
}

function SmartScrollUnload() {
   var hidF = document.getElementById(msSmartScrollID + '_hidFocus');

   if (hidF != null) {
      if (document.activeElement) {
         hidF.value = document.activeElement.id;
      }
      else {
         // code for non-IE browsers
      } 
   }
}

