/** This javascript is for use with the IDSOS Online Notary Search **/
// Global variables...
var rowsPerPage;
var frmQuery=null;
var frmSubmit=null;
var frmReport=null;
var newPos=5;
var isIE = false;
if(navigator.appName.indexOf("Microsoft")!=-1) isIE=true;

function getFormReferences() {
  frmQuery=$("queryForm");
  frmSubmit=$("submitForm");
  frmReport=$("reportForm");
}
function initNotarySearch() {
  /** Initialize the page when it is first loaded. **/
  getFormReferences();
  // "notarySearch.jsp" initially sets the "rows" value in the
  // hidden form "submitForm" to the value stored as session
  // attribute "RowsPerPage", or to a default value of 25.
  // Therefore, "submitForm.optRows" will always have a non-empty
  // value when the page loads.
  rowsPerPage=frmSubmit.rows.value;
  // Initialize the query form (the form the user sees)...
  with(frmQuery) {
    // Build the rows-per-page option list and select the option
    // that matches the value set in submitForm.rows...
    for(var i=0;i<=optRows.options.length; i++) {
      optRows.options[0].selected=false;
    }
    optRows.options[0].value="10";
    optRows.options[0].text="10";
    optRows.options[1].value="25";
    optRows.options[1].text="25";
    optRows.options[2].value="50";
    optRows.options[2].text="50";
    optRows.options[3].value="75";
    optRows.options[3].text="75";
    optRows.options[4].value="100";
    optRows.options[4].text="100";
    if(rowsPerPage=="10" || rowsPerPage==10) optRows.options[0].selected=true;
    else if(rowsPerPage=="25" || rowsPerPage==25) optRows.options[1].selected=true;
    else if(rowsPerPage=="50" || rowsPerPage==50) optRows.options[2].selected=true;
    else if(rowsPerPage=="75" || rowsPerPage==75) optRows.options[3].selected=true;
    else if(rowsPerPage=="100" || rowsPerPage==100) optRows.options[4].selected=true;
    // Set initial focus to the commission number field...
    fNum.focus();
  }
}
function submitNotaryQuery() {
/** Get values from the query form fields. Replace empty
 ** field values with a wildcard character for database use.
 ** Perform some data validation. If the query passes validation,
 ** set the hidden form field values to the edited user-entered
 ** values and submit the hidden form.
 */
  var fileNum="", lastName="", firstName="", middleName="";
  var cityName="", startDate="", endDate="", currStatus="", nGender="";
  var parms="";
  var WILDCARD="?"; // URL encoded value: %3F
  var DELIM="|";    // URL encoded value: %7C
  var prompt="";
  var EMPTY_QUERY=WILDCARD+DELIM+WILDCARD+DELIM+WILDCARD+DELIM+
                  WILDCARD+DELIM+WILDCARD+DELIM+WILDCARD+DELIM+
                  WILDCARD+DELIM+WILDCARD+DELIM+WILDCARD+DELIM;
  getFormReferences();
  with(frmQuery) {
    // Get values from the query form...
    fileNum=trim(setEmpty(fNum.value)).toUpperCase();
    lastName=trim(setEmpty(lName.value)).toUpperCase();
    firstName=trim(setEmpty(fName.value)).toUpperCase();
    middleName="";
    cityName=trim(setEmpty(city.value)).toUpperCase();
    currStatus=status.value;
    nGender=gender.value;
    startDate=trim(setEmpty(dateStart.value)).toUpperCase();
    endDate=trim(setEmpty(dateEnd.value)).toUpperCase();
    if(isEmpty(fileNum)) fileNum=WILDCARD;
    else if(isNaN(fileNum)) {
      focusTo=fNum;
      buildAlert("Commission Number must be numeric.");
      return;
    }
    if(isEmpty(lastName)) lastName=WILDCARD;
    if(isEmpty(firstName)) firstName=WILDCARD;
    if(isEmpty(middleName)) middleName=WILDCARD;
    if(isEmpty(cityName)) cityName=WILDCARD;
    if(isEmpty(currStatus)) currStatus=WILDCARD;
    if(isEmpty(nGender)) nGender=WILDCARD;
    if(isEmpty(startDate)) startDate=WILDCARD;
    else if(!isDate(startDate)) return;
    if(isEmpty(endDate)) endDate=WILDCARD;
    else if(!isDate(endDate)) return;
    // Build the query parameter string...
    parms=fileNum+DELIM+lastName+DELIM+firstName+DELIM+middleName+
          DELIM+cityName+DELIM+currStatus+DELIM+nGender+DELIM+
          startDate+DELIM+endDate+DELIM;
    /* CHECK FOR AMBIGUOUS CRITERIA OR CRITERIA THAT COVERS TOO BROAD A SCOPE */
    // Check for search query that is too broad-all fields are blank...
    if(parms==EMPTY_QUERY) {
      focusTo=fNum;
      prompt="PLEASE ENTER EITHER<br />~ A notary commission number ~<br />OR<br />"+
             "~ One or more notary name, city or date search values ~";
      buildAlert(prompt);
      return;
    }
    // Search must be either by commission number OR name/city/date info-not both...
    if(fileNum!=WILDCARD && (lastName!=WILDCARD || firstName!=WILDCARD ||
        middleName!=WILDCARD || cityName!=WILDCARD || startDate!=WILDCARD ||
        endDate!=WILDCARD || currStatus!=WILDCARD || nGender!=WILDCARD)) {
      focusTo=fNum;
      prompt="PLEASE ENTER EITHER<br />-A notary commission number<br />OR<br />"+
             "-One or more notary name, city or date search values<br />NOT BOTH";
      buildAlert(prompt);
      return;
    }
  }
  with(frmSubmit) {
    // Set hidden form values and submit the query...
    parms+="1"+DELIM;   // Append 10th parameter-starting row=1
    parmString.value=parms;
    submit();
  }
  window.document.body.style.cursor="wait";
  window.status="Searching...";
  return;
}
function submitAgencyQuery() {
/** Get values from the query form fields. Replace empty
 ** field values with a wildcard character. Set the hidden
 ** form field values to the edited user-entered values and
 ** submit the hidden form.
 */
  var agencyName="", agencyAddr="", agencyCity="", agencyState="";
  var parms="";
  var WILDCARD="?"; // "?" will be converted to "%" in jaguar for database use
  var DELIM="|";
  getFormReferences();
  with(frmQuery) {
    // Get values from the query form...
    agencyName=trim(setEmpty(agency.value)).toUpperCase();
    agencyAddr=trim(setEmpty(address.value)).toUpperCase();
    agencyCity=trim(setEmpty(city.value)).toUpperCase();
    agencyState=trim(setEmpty(state.value)).toUpperCase();
    // Set empty string values to the query wildcard value...
    if(isEmpty(agencyName)) agencyName=WILDCARD;
    if(isEmpty(agencyAddr)) agencyAddr=WILDCARD;
    if(isEmpty(agencyCity)) agencyCity=WILDCARD;
    if(isEmpty(agencyState)) agencyState=WILDCARD;
    // Build the query parameter string...
    parms=agencyName+DELIM+agencyAddr+DELIM+agencyCity+DELIM+agencyState+DELIM;
  }
  with(frmSubmit) {
    // Set hidden form values and submit the query...
    parms+="1"+DELIM;   // Append 5th parameter-starting row=1
    parmString.value=parms;
    submit();
  }
  window.document.body.style.cursor="wait";
  window.status="Searching...";
  return true;
}
function agencySearch() {
  getFormReferences();
  location.href="agencySearch.jsp?rows="+frmSubmit.rows.value;
}
function displayHelp(sHelpID, bDisplay) {
  var msg=""; // Default to no help message
  var elem=$(sHelpID); // Get handle on the intended div element
    switch(sHelpID.substr(sHelpID.length-1, 1)) {
    case "1": // Help about wildcards
      if(bDisplay) {
        msg="A&nbsp;wildcard&nbsp;matches&nbsp;0,&nbsp;1&nbsp;or&nbsp;more&nbsp;characters.<br />";
        msg+="Example:&nbsp;JO?&nbsp;matches&nbsp;JO,&nbsp;JOE,&nbsp;JON,&nbsp;JOHN,&nbsp;etc.";
      }
      break;
    default:
      if(notNull(elem)) {
        elem.style.display="none";
        elem.innerHTML="";
      }
      document.body.style.cursor="default";
      return;
  }
  elem.innerHTML=msg;
  if(bDisplay) {
    document.body.style.cursor="help";
    elem.style.display="block";
  } else {
    document.body.style.cursor="default";
    elem.style.display="none";
  }
}
function initializeActivity() {
/** Initialize the form to request the report for the
 ** month (and year) prior to the current month.
 */
  newPos=10;
  var d=new Date();
  var m=d.getMonth();     // Returns 0-11, not 1-12
  var y=d.getFullYear();  // yyyy
  getFormReferences();
  // Set the month and year to the previous month and year from current...
  if(m==0) {
    m=11;
    y=y-1;
  } else m=m-1;
  with(frmReport) {
    month.options[m].selected=true;
    year.options[0].value=y-4;
    year.options[0].text=y-4;
    year.options[1].value=y-3;
    year.options[1].text=y-3;
    year.options[2].value=y-2;
    year.options[2].text=y-2;
    year.options[3].value=y-1;
    year.options[3].text=y-1;
    year.options[4].value=y;
    year.options[4].text=y;
    year.options[4].selected=true;
    month.focus();
  }
}
function getReport() {
/** If the request for a report is valid, set values
 ** in the hidden submission form and submit the request.
 */
  getFormReferences();
  with(frmReport) {
    if(!requestValid()) return;
    else {
      // Build the url string and request the form, opening a new window...
      var m=month.value;
      var y=year.value;
      var reportUrl="activityReport.jsp?m="+m+"&y="+y;
      newPos+=15;
      openWindow(reportUrl, "rptWin"+m+y);
    }
  }
}
function requestValid() {
/** Verify that the report request is for a valid month and year */
  var d=new Date();
  var currentMonth=d.getMonth();    // returns 0-11, not 1-12
  var currentYear=d.getFullYear();  // yyyy
  var oldestYear=currentYear-6;   // Don't go back more than 6 years
  getFormReferences();
  with(frmReport) {
    var m=month.value-1;  // Convert standard month number to javascript equivalent value
    var y=trim(year.value);
    // Validate the year...
    if(isNaN(y) || y.length!=4) {
      focusTo=year;
      buildAlert("The year must be in 4-digit format (yyyy)");
      return false;
    } else {
      if((y==oldestYear && m<=currentMonth) || y<oldestYear || y>currentYear) {
        focusTo=year;
        buildAlert("Only reports less than six years old are available");
        return false;
      }
    }
    // Verify that we aren't looking for a future report...
    if(m>currentMonth && y>=currentYear) {
      focusTo=month;
      buildAlert("You have selected a future report.<br /><br />Only reports for past months are available.");
      return false;
    }
  }
  return true;
}
function setTitle() {
/** Set the submit button title and mouseover text **/
  getFormReferences();
  var m=frmReport.month.value;
  var y=frmReport.year.value;
  var month="";
  switch(m) {
    case "1":
      month="January";
      break;
    case "2":
      month="February";
      break;
    case "3":
      month="March";
      break;
    case "4":
      month="April";
      break;
    case "5":
      month="May";
      break;
    case "6":
      month="June";
      break;
    case "7":
      month="July";
      break;
    case "8":
      month="August";
      break;
    case "9":
      month="September";
      break;
    case "10":
      month="October";
      break;
    case "11":
      month="November";
      break;
    case "12":
      month="December";
      break;
    default:
      month="";
      break;
  }
  return "Get report for "+month+" "+y;
}
function openWindow(urlSrc, winName) {
  var rptWin=window.open(urlSrc, winName, "scrollbars,resizable,menubar,width=750,height=600,top="+newPos+",left="+newPos);
  rptWin.focus();
}
function setFocus() {
    var links;
    links=getElementsByClass("small");
    if(links.length>0) links[0].focus();
}
function getPages(pageParms) {
  if(isEmpty(pageParms)) {
    window.status='';
    return;
  }
  with(document.pageForm) {
    parmString.value=pageParms;
    submit();
  }
}
