// Purpose - PermitApplicationSearch.js provides all client-side script event handlers for 
// PermitApplicationSearch.aspx.  This version greatly simplifiees the processing logic required
// when compared with previous version.  (EYang, Sep 2009)
// 
// Notes:  With Master Pages, it is necessary to register this code block at run time so that
// it runs only for PermitApplicationSearch.aspx, and not other pages that share the same
// .Master file.  See ConfigClientScripts() method in code-behind to see how the code block
// is registered.
//
// Reference Google Groups for original article from a guy who struggled with exactly the
// same thing:
// Group:   microsoft.public.dotnet.framework.aspnet    
// Subject: "JavaScript From PageLoad()?"
// Author:  "Frank"
// Date:    Dec 7, 2006

var _isSubmitWithSearch = false; // this flag controls what updates to UI occur when user
                                 // initiates a search, or else suppresses them if any other
                                 // kind of postback occurs

function body_OnLoad(txtSearchInputClientID)
{
  // Purpose:  generic <body> tag onload code, such as putting focus on the correct UI control
  var txt = document.getElementById(txtSearchInputClientID);
  if(txt)
  {
    //alert('found the text box');
    txt.focus();
  }
}

// ALERT! - disable body_OnKeyDown (whereas we supported it previously) because...
// * Firefox - does not seem to recognize onkeydown attribute of <body> tag.  Firefox may support
// keydown somewhere else in the DOM, but we don't need it right now.
// * IE - we can avoid defining onkeydown for IE6 by setting up DefaultButton attribute of <asp:Panel>
// control in HTML Design view.
// * General Tip - we get overall better UI behavior by setting UseSubmitBehavior="false" for the 
// <asp:Button> control in addition to the DefaultButton attribute of <asp:Panel>.
function xxbody_OnKeyDown()  
{
  // Purpose:  captures all KeyDown events on the form to determine when the Enter key
  // has been pressed, as it will result in a postback action.
  //
  if (window.event.ctrlKey == false && window.event.altKey == false) 
  {
    switch (window.event.keyCode)
    {
	    case 13: // enter key
	      //alert('enter key pressed');
        _isSubmitWithSearch = true;
		    break;
		  default:
		    break;					   
    }
  }
}

function btnSearch_OnClick(btn, isSearch)
{
  // Purpose:  generic handler for initiation of search (also fires when user presses Enter key
  // after some trial-and-error in Firefox and IE - see body_OnKeyDown notes, above)
  //
  // Assumes:  OnClientClick attribute has been set up in Design view of page.
  _isSubmitWithSearch = isSearch; 
  //alert('new value of _isSubmitWithSearch = ' + _isSubmitWithSearch);                              
}

function frm_OnSubmit(lblStatusMessageTopClientID, tblSearchResultsClientID)
{
  // Purpose:  generic <form> tag onsubmit code.  Assumes that this code has been "registered"
  // by server-side code using .RegisterOnSubmitStatement() call.  It gets inserted by .NET
  // in front if any 'doPostback()' logic that the Framework generates.  Use this opportunity
  // to update any part of the UI that should change while the user awaits the server's response.
  if (_isSubmitWithSearch)
  {
    // make the UI more informative to user...
    // - set label to indicate search is in progress; notice that this changes display
    // before the request for postback of page is actually sent from browser to server;
    // thus, it is more of an illusion than anything to set display to "Searching..." from
    // here, because it has no direct connection to the the server-side code that actually
    // executes search logic
    
    var lblStatusMessageTop = document.getElementById(lblStatusMessageTopClientID);
    //alert(lblStatusMessageTop);
    
    lblStatusMessageTop.className = 'statusMessageNormal';
    //lblStatusMessageTop.innerText = "Searching...";
    lblStatusMessageTop.innerHTML = 'Searching...'; // late change - standard DOM property is .innerHtml,
                                                    // .innerText only works on IE (EYang, Sep 2009)
  
    // to prevent any confusion about consecutive searches, hide the previous search results
    // - notice that tblSearchResults is actually the .NET GridView control
    var tblSearchResults = document.getElementById(tblSearchResultsClientID);
    if(tblSearchResults)
    {
      tblSearchResults.style.display = 'none';
    }
  }
  else
  {
    // do nothing for now, but consider later adding some messaging for other postbacks that 
    // may occur such as:
    // * click on Permit App # to drilldown to PermitApplicationDetail.aspx
    // * click on GridView column header to re-sort data
    // * others? 
  }
  
  _isSubmitWithSearch = false; // reset to default value because AJAX does not fully reload the page
}


