﻿var SortColumnOrder;        // used in page/sorting navigation
var CurrentPage;            // used in page/sorting navigation
var MinCharsToBeginSearch;  // used in search prep

/****************** SEARCH UTILITY FUNCTIONS *********************/
////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   swapAccordionPane
//
//  Parameters: --
//
//  Returns:    --
//
//  Purpose:    The function resets the searches
////////////////////////////////////////////////////////////////////////////////////////////////
function swapAccordionPane() {
    if ( SearchInProgress ) {
        SearchWasCancelled = true;
    }
    resetSearch('SmartSearchInput', 'ResultId' );
    resetSearch('AdvancedSearchInput', 'ResultsAdvSearch');
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   resetSearch
//
//  Parameters: inputContainerID - the id of the container holding the inputs (string/text)
//              resultContainerID - the id of the results container (string/text)
//
//  Returns:    --
//
//  Purpose:    The function clears the inputs and hides the results
////////////////////////////////////////////////////////////////////////////////////////////////
function resetSearch( inputContainerID, resultContainerID ) {
    hideContainer( resultContainerID );
    clearInputs( inputContainerID );
    hideIndicators( inputContainerID );
    CurrentPage = 1;
    var ApplicationName = getApplicationFromURL();
    if ( ApplicationName == 'births' ) {
        SortColumnOrder = 'LastName, FirstMiddleName ASC';
    } else if ( ApplicationName == 'deaths' ) {
        SortColumnOrder = 'LastName, FirstMiddleName ASC';
    } else if ( ApplicationName == 'marriages' ) {
        SortColumnOrder = 'GroomLastName, GroomFirstMiddleName ASC';
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   hideContainer
//
//  Parameters: containerID
//
//  Returns:    --
//
//  Purpose:    The function sets the display property to none for containerID
////////////////////////////////////////////////////////////////////////////////////////////////
function hideContainer( containerID ) {
    var ContainerControl = document.getElementById( containerID );
    if ( ContainerControl != null ) {
        ContainerControl.style.display = 'none';
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   hideIndicators
//
//  Parameters: containerID
//
//  Returns:    --
//
//  Purpose:    The function sets the display property to none for image indicators in the containerID
////////////////////////////////////////////////////////////////////////////////////////////////
function hideIndicators( containerID ) {
    var ContainerControl = document.getElementById( containerID );
    if ( ContainerControl != null ) {
        var ImageIndicators = new Array();
        ImageIndicators = ContainerControl.getElementsByTagName( 'img' );
        for ( var ImageIndex = 0; ImageIndex < ImageIndicators.length; ImageIndex++ ) {
            if ( ImageIndicators[ImageIndex].id.indexOf( 'Loading' ) > -1 ) {
                ImageIndicators[ImageIndex].style.display = 'none';
            }
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   clearInputs
//
//  Parameters: parentElementID - the ID of the element with the controls to clear (string/text)
//
//  Returns:    --
//
//  Purpose:    The function clears all of the textbox values and then resets drop down selected
//              index to 0, unless it is the parish and parish was set in the query string
////////////////////////////////////////////////////////////////////////////////////////////////
function clearInputs( parentElementID ) {
    var ParentContainer = document.getElementById( parentElementID );
    if ( ParentContainer != null ) {
        var InputControls = new Array();
        var SelectControls = new Array();
        InputControls = ParentContainer.getElementsByTagName( 'input' );
        for ( var InputIndex = 0; InputIndex < InputControls.length; InputIndex++ ) {
            if ( InputControls[InputIndex].type == 'text' ) {
                InputControls[InputIndex].value = '';
            }
        }
        SelectControls = ParentContainer.getElementsByTagName( 'select' );
        for ( var SelectIndex = 0; SelectIndex < SelectControls.length; SelectIndex++ ) {
            if ( SelectControls[SelectIndex].id.indexOf( 'Parish' ) > -1 & getQueryStringValue( 'pid' ) != null  ) {
                // DON'T CLEAR IF PARISH WAS SET IN QUERY STRING
            } else {
                SelectControls[SelectIndex].selectedIndex = 0;
            }
        }
    }
}   
/****************** SEARCH UTILITY FUNCTIONS - END **************/

/****************** NAVIGATION FUNCTIONS ************************/
////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   navToOrderRecord
//
//  Parameters: rid - the ID of the selected record (numeric/int)
//              rtype - the Vital Record type (string/text - m/marriage, b/birth, d/death)
//              inNewWindow - open OrderRecord in new window (boolean - true/false)
//
//  Returns:    --
//
//  Purpose:    The function navigates the user to the OrderRecord.aspx page
////////////////////////////////////////////////////////////////////////////////////////////////
function navToOrderRecord( rid, rtype, inNewWindow ) {
    var OrderWindowURL = '/OrderRecord.aspx?vrid=' + rid.toString();
    if ( rtype == 'marriage' | rtype == 'MARRIAGE' | rtype == 'm' | rtype == 'M' ) {
        OrderWindowURL = OrderWindowURL + '&vrtype=m';
    } else if ( rtype == 'birth' | rtype == 'BIRTH' | rtype == 'b' | rtype == 'B' ) {
        OrderWindowURL = OrderWindowURL + '&vrtype=b';
    } else if ( rtype == 'death' | rtype == 'DEATH' | rtype == 'd' | rtype == 'D' ) {
        OrderWindowURL = OrderWindowURL + '&vrtype=d';
    } else {
        // INCORRECT TYPE
        return;
    }
    
    if ( window.location.search.substring(1).length > 0 ) {
        OrderWindowURL = OrderWindowURL + '&' + window.location.search.substring(1);
    }
    
    if ( inNewWindow ) {
        window.open( OrderWindowURL, 'orderVitalRecord' );
    } else {
        window.location = OrderWindowURL;
    }    
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   navToEditRecord
//
//  Parameters: rid - the ID of the selected record (numeric/int)
//              fromAdvancedSearch - if record to edit was found in advanced search (boolean - true/false)
//              inNewWindow - open EditRecord in new window (boolean - true/false)
//
//  Returns:    --
//
//  Purpose:    The function navigates the user to the EditRecord.aspx page
////////////////////////////////////////////////////////////////////////////////////////////////
function navToEditRecord( rid, fromAdvancedSearch, inNewWindow ) {
    var ReturnPaneIndex = 1;
    if ( fromAdvancedSearch ) {
        ReturnPaneIndex++;
    }
    var EditRecordURL = 'DataEntry.aspx?apid=' + ReturnPaneIndex.toString() + '&vrid=' + rid.toString();
  
    var CleanQueryString = removeQueryStringValue( 'apid' );
    if ( CleanQueryString.length > 0 ) {
        EditRecordURL = EditRecordURL + '&' + CleanQueryString;
    }
    
    if ( inNewWindow ) {
        window.open( EditRecordURL, 'editVitalRecord' );
    } else {
        window.location = EditRecordURL;
    }                                   
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   navToHandSearch
//
//  Parameters: rtype - the Vital Record type (string/text - m/marriage, b/birth, d/death)
//              inNewWindow - open EditRecord in new window (boolean - true/false)
//
//  Returns:    --
//
//  Purpose:    The function navigates the user to the EditRecord.aspx page
////////////////////////////////////////////////////////////////////////////////////////////////
function navToHandSearch( rtype, inNewWindow ) {
    var HandSearchURL = '/HandSearch.aspx';
    if ( rtype == 'marriage' | rtype == 'MARRIAGE' | rtype == 'm' | rtype == 'M' ) {
        HandSearchURL = HandSearchURL + '?vrtype=m';
    } else if ( rtype == 'birth' | rtype == 'BIRTH' | rtype == 'b' | rtype == 'B' ) {
        HandSearchURL = HandSearchURL + '?vrtype=b';
    } else if ( rtype == 'death' | rtype == 'DEATH' | rtype == 'd' | rtype == 'D' ) {
        HandSearchURL = HandSearchURL + '?vrtype=d';
    } else {
        // INCORRECT TYPE
        return;
    } 
        
    if ( window.location.search.substring(1).length > 0 ) {
        HandSearchURL = HandSearchURL + '&' + window.location.search.substring(1);
    }
    if ( inNewWindow) {
        window.open(HandSearchURL, 'handSearchWindow');
    } else {
        window.location = HandSearchURL;
    }
}
/****************** NAVIGATION FUNCTIONS - END ******************/

/****************** PAGING/SORTING FUNCTIONS ********************/
////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   goToNextPage
//
//  Parameters: fromAdvancedSearch - if record to edit was found in advanced search (boolean - true/false) 
//
//  Returns:    --
//
//  Purpose:    The function moves the search results to the next page
////////////////////////////////////////////////////////////////////////////////////////////////
function goToNextPage( fromAdvancedSearch ) {
    if ( CurrentPage != null ) {
        CurrentPage = CurrentPage + 1;
        if ( fromAdvancedSearch ) {
            doAdvancedSearch();
        } else {
            doBasicSearch();
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   goToPreviousPage
//
//  Parameters: fromAdvancedSearch - if record to edit was found in advanced search (boolean - true/false) 
//
//  Returns:    --
//
//  Purpose:    The function moves the search results to the previous page
////////////////////////////////////////////////////////////////////////////////////////////////
function goToPreviousPage( fromAdvancedSearch ) {
    if ( CurrentPage != null ) {
        if ( CurrentPage <= 1 ) {
            // NO PREVIOUS PAGE
        } else {
            CurrentPage = CurrentPage - 1;
            if ( fromAdvancedSearch ) {
                doAdvancedSearch();
            } else {
                doBasicSearch();
            }        
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   sortByColumnName
//
//  Parameters: columnName - name of the column to sort by (string/text)
//              fromAdvancedSearch - if record to edit was found in advanced search (boolean - true/false) 
//
//  Returns:    --
//
//  Purpose:    The function moves the search results to the previous page, also going back to page 1
////////////////////////////////////////////////////////////////////////////////////////////////
function sortByColumn( columnName, fromAdvancedSearch ) {
    if ( SortColumnOrder ) {
        if ( SortColumnOrder.indexOf( columnName ) > -1 ) {
            if ( SortColumnOrder.indexOf( 'ASC' ) > -1 ) {
                SortColumnOrder = columnName + ' DESC';
            } else {
                SortColumnOrder = columnName + ' ASC';
            }
        } else {
            SortColumnOrder = columnName + ' ASC';
        }
    } else {
        SortColumnOrder = columnName + ' ASC';
    }
    
    CurrentPage = 1;
    
    if ( fromAdvancedSearch ) {
        doAdvancedSearch();
    } else {
        doBasicSearch();
    }
}
/****************** PAGING/SORTING FUNCTIONS -END ****************/

/****************** SEARCH FUNCTIONS *****************************/ 
////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   prepBasicSearch
//
//  Parameters: senderID - the id of the control causing the search to begin
//
//  Returns:    --
//
//  Purpose:    The function checks criteria to be met in order to perform the basic search
////////////////////////////////////////////////////////////////////////////////////////////////
function prepBasicSearch( senderId ) {
    var SenderControl = document.getElementById( senderId );
    if ( SenderControl != null ) {
        if ( MinCharsToBeginSearch != null ) {
            if ( SenderControl.value.length >= MinCharsToBeginSearch ) {
                var BasicSearchMessageControl = document.getElementById( 'basicSearchMessage' );
                if ( BasicSearchMessageControl != null ) {
                    BasicSearchMessageControl.style.display = 'none';
                }
                CurrentPage = 1;
                doBasicSearch();
            } else {
                var BasicSearchMessageControl = document.getElementById( 'basicSearchMessage' );
                if ( BasicSearchMessageControl != null ) {
                    BasicSearchMessageControl.innerHTML = 'Please enter at least <b>' + MinCharsToBeginSearch.toString() + '</b> characters for results.  <BR>If you are searching for a last name of two letters, <BR>please use the Advanced Search.';
                    BasicSearchMessageControl.style.display = '';
                }
                // HIDE RESULTS
                var ResultContainer = document.getElementById( 'ResultId' );
                if ( ResultContainer != null ) {
                    ResultContainer.style.display = 'none';
                }
            }       
        } else {
            CurrentPage = 1;
            doBasicSearch();
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   prepAdvancedSearch
//
//  Parameters: senderID - the id of the control causing the search to begin
//
//  Returns:    --
//
//  Purpose:    The function checks criteria to be met in order to perform the advanced search
////////////////////////////////////////////////////////////////////////////////////////////////
function prepAdvancedSearch( senderId ) {
    var SenderControl = document.getElementById( senderId );
    if ( SenderControl != null ) {
        if ( senderId.indexOf( 'Year' ) > -1 ) {
            if ( SenderControl.value.length == 4 ) {
                CurrentPage = 1;
                doAdvancedSearch();
            }
        } else {
            CurrentPage = 1;
            doAdvancedSearch();
        }
    }
}
/****************** SEARCH FUNCTIONS - END ***********************/

/****************** QUERY STRING FUNCTIONS ***********************/
////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   getQueryStringValue
//
//  Parameters: keyToGet - the query string key to retrieve (string/text)
//
//  Returns:    value of the appropriate key, null if key does not exist   
//
//  Purpose:    The function retrieves the value for the key from the query string
////////////////////////////////////////////////////////////////////////////////////////////////
function getQueryStringValue( key ) {
    var CurrentQueryString = window.location.search.substring(1);
    var QueryStringKeys = new Array();
    QueryStringKeys = CurrentQueryString.split("&");
    for (var KeyIndex = 0; KeyIndex < QueryStringKeys.length; KeyIndex++) {
        var KeyValuePair = new Array();
        KeyValuePair = QueryStringKeys[KeyIndex].split("=");
        if ( KeyValuePair[0] == key ) {
            return KeyValuePair[1];
        }
    }
    return null;
} 

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   removeQueryStringValue
//
//  Parameters: keyToRemove - the query string key to remove (string/text)
//
//  Returns:    updated query string after key is removed   
//
//  Purpose:    The function removes the appropriate key/value pair from the query string
////////////////////////////////////////////////////////////////////////////////////////////////
function removeQueryStringValue( keyToRemove ) {
    var CurrentQueryString = window.location.search.substring(1);
    if ( CurrentQueryString.length > 0 ) {
        var UpdatedQueryString;
        var KeyToRemoveIndex = CurrentQueryString.indexOf( keyToRemove + '=' );
        if ( KeyToRemoveIndex != 0 ) {
            KeyToRemoveIndex = CurrentQueryString.indexOf( '&' + keyToRemove + '=' );
        }
        if ( KeyToRemoveIndex > -1 ) {
            UpdatedQueryString = CurrentQueryString.substring( 0, KeyToRemoveIndex);
            var AmpersandIndex = CurrentQueryString.substring( KeyToRemoveIndex + 1 ).indexOf( '&' );
            if ( AmpersandIndex > -1 ) {
                if ( KeyToRemoveIndex > 0 ) {
                    UpdatedQueryString = UpdatedQueryString + CurrentQueryString.substring(KeyToRemoveIndex + AmpersandIndex + 1);
                } else {
                    UpdatedQueryString = UpdatedQueryString + CurrentQueryString.substring(KeyToRemoveIndex + 1 + AmpersandIndex + 1);
                }
            }
            return UpdatedQueryString;
        }
    } 
    return CurrentQueryString;
}
/****************** QUERY STRING FUNCTIONS ***********************/ 

/****************** GENERAL FUNCTIONS ****************************/
////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   trimString
//
//  Parameters: inString - the string to trim (string/text)
//
//  Returns:    the trimmed string   
//
//  Purpose:    The function trims leading and trailing spaces from a string
////////////////////////////////////////////////////////////////////////////////////////////////
function trimString( inString ) {
    var TrimmedString;
    TrimmedString = inString.replace( /^\s+/g, "" );// strip leading
    TrimmedString = inString.replace( /\s+$/g, "" );// strip trailing
    return TrimmedString;
}  

////////////////////////////////////////////////////////////////////////////////////////////////
//  Function:   getApplicationFromURL
//
//  Parameters: --
//
//  Returns:    the application name   
//
//  Purpose:    The function parses the url for the application name
////////////////////////////////////////////////////////////////////////////////////////////////
function getApplicationFromURL() {
    var ApplicationName;
    if ( window.location.href.toUpperCase().indexOf( 'BIRTH' ) > -1 ) {
        ApplicationName = 'births';
    } else if ( window.location.href.toUpperCase().indexOf( 'DEATH' ) > -1 ) {
        ApplicationName = 'deaths';
    } else if ( window.location.href.toUpperCase().indexOf( 'MARRIAGE' ) > -1 ) {
        ApplicationName = 'marriages';
    } else {
        ApplicationName = '';
    }
    return ApplicationName;
}

//var timerID = 0;
//var tStart  = null;

//function UpdateTimer() {
//   alert('timer done');
//   if(timerID) {
//      clearTimeout(timerID);
//      clockID  = 0;
//   }

//   if(!tStart)
//     tStart   = new Date();

//   var   tDate = new Date();
//   var   tDiff = tDate.getTime() - tStart.getTime();

//   tDate.setTime(tDiff);
//  
//   timerID = setTimeout("UpdateTimer()", 1000);
//}

//function Start() {
//   tStart   = new Date();

//   timerID  = setTimeout("UpdateTimer()", 1000);
//}

//function Stop() {
//   if(timerID) {
//      clearTimeout(timerID);
//      timerID  = 0;
//   }

//   tStart = null;
//}

//function Reset() {
//   tStart = null;
//}
/****************** GENERAL FUNCTIONS - END **********************/