/*----------------------------------------------------------------------------------------------
*  File Name:	 ProviderSearch.js
*
*  Description:  This file contains functions to control the provider search page.
*
*
*|                    | Mod Log    |                |
*| Author             | Date       | Authorization  |  Description
*|--------------------|------------|----------------|--------------------------------------------
*| Brad Coon          | 11/03/04   |                | Initial Creation
*-------------------------------------------------------------------------------------------------*/

/*--------------------------------Global Variables----------------------------------
*
* Define all arrays and variables
*---------------------------------------------------------------------------------------------------*/
// Store all 'other' provider type codes
var provCodeArray =  new Array();

// Number of pages in results
if (resultNum > 100) {
	resultNum = 100;
	var pages = 10;
}
else {
	var pages = Math.ceil(parseFloat(resultNum/10));
}



/*--------------------------------------------------------------------------------
*  Function:     showSpecialty
*
*  Description:  Show the specialty dropdown that is related to the selected provider type
*						
*  Notes:
*
*  Return Value: Boolean indicating that a specialty is being shown
*
*  Input Parms: Provider Code number
*
*|                    | Mod Log    |                 |
*| Author             | Date       | Authorization   | Description
*|------------------- |------------|-----------------|-------------------------------
*| Brad Coon          | 11/03/04   |			     | Initial Development
*-------------------------------------------------------------------------------*/
function showSpecialty(provCode) {
	// Hide all specialty dropdowns
	for (var i = 0; i < provCodeArray.length; i++) {
		eval("document.getElementById('specialty_" + provCodeArray[i] + "').style.display = 'none'");
	}

	// Show the specialty dropdown for the selected provider types
	eval("document.getElementById('specialty_" + provCode + "').style.display = 'block'");
	
	// Deselect any selected specialties that might have been chosen
	var specLen = eval("document.Provider_Search.spec_dropdown_" + provCode + ".options.length");
	for (var i = 0; i < specLen; i++) {
		eval("document.Provider_Search.spec_dropdown_" + provCode + ".options[" + i + "].selected = false");
	}
	
	// Select the default 'All Specialties'
	eval("document.Provider_Search.spec_dropdown_" + provCode + ".options[0].selected = true");
			
	return true;
}


/*--------------------------------------------------------------------------------
*  Function:     buildNav
*
*  Description:  Build the naviagation that will display the pages of results to select
*						
*  Notes:
*
*  Return Value: Boolean indicating successful build of navigation
*
*  Input Parms: None
*
*|                    | Mod Log    |                 |
*| Author             | Date       | Authorization   | Description
*|------------------- |------------|-----------------|-------------------------------
*| Brad Coon          | 11/03/04   |			     | Initial Development
*-------------------------------------------------------------------------------*/
function buildNav() {
	// Determine what results page is being displayed
	var selectedPage = parseFloat(document.Provider_Search.selectedPage.value);

	// String to hold navigation structure
	var navStr = "";
	
	// Does a results page before this page exist
	if (selectedPage > 1) {
		navStr += "<a href='javascript:void(0);' style='text-decoration:none' onClick='resultsNav(" + (selectedPage - 1) + ");'><font face='arial' size='2'>[Prev 10]</font></a> ";
	}
	
	// Build a reference to each results page
	for(var i = 1; i < pages + 1; i++) {
		if (i == selectedPage) {
			navStr += "<b>" + i + "</b> ";
		}
		else {
			navStr += "<a href='javascript:void(0);' style='text-decoration:none' onClick='resultsNav(" + i + ");'><font face='arial' size='2'>" + i + "</font></a> ";
		}
	}

	// Does a results page after this page exist
	if (pages > selectedPage) {
		navStr += "<a href='javascript:void(0);' style='text-decoration:none' onClick='resultsNav(" + (selectedPage + 1) + ");'><font face='arial' size='2'>[Next 10]</font></a>";
	}
		
	// Load the navbar above and below the results
	document.getElementById('navTop').innerHTML = navStr;
	document.getElementById('navBottom').innerHTML = navStr;
	
	return true;
}
		

/*--------------------------------------------------------------------------------
*  Function:     resultsNav
*
*  Description:  Display the results that coorespond to the selected page number
*						
*  Notes:
*
*  Return Value: Boolean indicating that a set of results is being displayed
*
*  Input Parms: page number
*
*|                    | Mod Log    |                 |
*| Author             | Date       | Authorization   | Description
*|------------------- |------------|-----------------|-------------------------------
*| Brad Coon          | 11/03/04   |			     | Initial Development
*-------------------------------------------------------------------------------*/
function resultsNav(page) {
	var start;
	var end;
	
	document.Provider_Search.selectedPage.value = page;
			
	// Hide each result div element
	for (var i = 1; i < resultNum + 1; i++) {
		eval("document.getElementById('ResultRow_" + i + "').style.display = 'none'");
	}

	// Determine what page of results should be displayed
	document.Provider_Search.resultStart.value = (page*10) - 9;
								
	// Will the next page contain 10 results or only the remaining number of results
	if (resultNum > page*10) {
		document.Provider_Search.resultEnd.value = page*10;
	}
	else {
		document.Provider_Search.resultEnd.value = resultNum;
	}
	
	// Store the selected page number
	document.Provider_Search.selectedPage.value = page;
				
	// First result to display on this page
	var start = document.Provider_Search.resultStart.value;
	
	// Last result to display on this page
	var end = parseInt(document.Provider_Search.resultEnd.value);
	
	// Show the results for the selected page	
	for (var i = start; i < end + 1; i++) {
		eval("document.getElementById('ResultRow_" + i + "').style.display = 'block'");
	}
	
	// Rebuild the navbar 
	buildNav();
	
	return true;
}


/*--------------------------------------------------------------------------------
*  Function:     isValidName
*
*  Description:  Validates and will only one of the name fields to be filled out.
*				 
*						
*  Notes:
*
*  Return Value: Boolean indicating that fields were valid
*
*  Input Parms: Reference to the field's object
*				Name of the field
*
*|                    | Mod Log    |                 |
*| Author             | Date       | Authorization   | Description
*|------------------- |------------|-----------------|-------------------------------
*| Brad Coon          | 11/03/04   |			     | Initial Development
*-------------------------------------------------------------------------------*/
function isValidName(obj, objName) {
	if (obj.value.length > 0) {
		// Do not allow last or first name fields to be entered when an institution name exists
		if ((objName == "First Name") || (objName == "Last Name")) {
			if (document.Provider_Search.ProvInstitution.value.length > 0) {
				alert(objName + " is not allowed when an Institution Name has been entered.");
				document.Provider_Search.ProvLastName.value = "";
				document.Provider_Search.ProvFirstName.value = "";
				document.Provider_Search.ProvLastName.focus();
				return false;
			}
		}
		// Do not allow an institution name to be entered when a last or first name exist
		else if (objName == "Institution Name") {
			if ((document.Provider_Search.ProvLastName.value.length > 0) || (document.Provider_Search.ProvFirstName.value.length > 0)) {
				alert(objName + " is not allowed when a Last Name or First Name has been entered.");
				document.Provider_Search.ProvInstitution.value = "";
				document.Provider_Search.ProvInstitution.focus();
				return false;
			}
		}
	
		// Verify that field is valid
		if (!isAlphaNumericSpecialPeriod(obj, objName)) {
			return false;
		}
	}
	
		return true;
}

	
/*--------------------------------------------------------------------------------
*  Function:     isValidAddress
*
*  Description:  Validates and will only allow one of the address fields to be filled out.
*				 
*						
*  Notes:
*
*  Return Value: Boolean indicating that fields were valid
*
*  Input Parms: Reference to the field's object
*				Name of the field
*
*|                    | Mod Log    |                 |
*| Author             | Date       | Authorization   | Description
*|------------------- |------------|-----------------|-------------------------------
*| Brad Coon          | 11/03/04   |			     | Initial Development
*-------------------------------------------------------------------------------*/
function isValidAddress(obj, objName) {
	if (obj.value.length > 0) {
		switch (objName) {
			// If city is entered, do not allow a county or zip code
			case "City":
				if ((document.Provider_Search.County.value != "00") || (document.Provider_Search.ZipCode.value.length > 0)) {
					alert("Only 1 address type can be entered per search");
					obj.value = "";
					obj.focus();
					return false;
				}
			
				// Verify that field is valid
				if (!isAlphaNumericSpecialPeriod(obj, objName)) {
					return false;
				}	
				break;
			
			// If state (other than Indiana) is selected, do not allow a county or zip code
			case "State":
				if (obj.value != "IN") {
					if ((document.Provider_Search.County.value != "00") || (document.Provider_Search.ZipCode.value.length > 0)) {
						alert("Only 1 address type can be entered per search");
						obj.value = "IN";
						return false;
					}
				}
				break;
		
			// If a county is selected, do not allow a city/state (other than Indiana), or zip code
			case "County":
				if ((document.Provider_Search.County.value != "00") && ((document.Provider_Search.City.value.length > 0) || (document.Provider_Search.State.value != "IN") || (document.Provider_Search.ZipCode.value.length > 0))) {
					alert("Only 1 address type can be entered per search");
					obj.value = "00";
					return false;
				}
				break;
		
			// If Zip code is entered, do not allow a city/state (other than Indiana), or county
			case "Zip Code":
				if ((document.Provider_Search.City.value.length > 0) || (document.Provider_Search.State.value != "IN") || (document.Provider_Search.County.value != "00")) {
					alert("Only 1 address type can be entered per search");
					obj.value = "";
					obj.focus();
					return false;
				}
			
				// Verify that field is valid
				if (obj.value.length < 5) {
					alert("Zip Code must have a length of 5.");
					obj.value = "";
					obj.focus();
					return false;
				}
				else {
					if (!isNumeric(obj, objName)) {
						return false;
					}
				}
				break;
		}
	}
	
	return true;
}


/*--------------------------------------------------------------------------------
*  Function:     submitSearch
*
*  Description:  Revalidates all fields before search is submitted
*				 
*						
*  Notes:
*
*  Return Value: none
*
*  Input Parms: Reference to form object
*
*|                    | Mod Log    |                 |
*| Author             | Date       | Authorization   | Description
*|------------------- |------------|-----------------|-------------------------------
*| Brad Coon          | 11/03/04   |			     | Initial Development
*-------------------------------------------------------------------------------*/
function submitSearch(form) {
	// Valid First name (do not allow Institution name)
	if (!isValidName(form.ProvLastName, 'Last Name')) {
		return false;
	}
	
	// Valid Last name (do not allow Institution name)
	if (!isValidName(form.ProvFirstName, 'First Name')) {
		return false;
	}
	
	// Valid Institution name (do not allow first/last name)
	if (!isValidName(form.ProvInstitution, 'Institution Name')) {
		return false;
	}

	// Valid Zip code ( do not allow city/state or county
	if (!isValidAddress(form.ZipCode, 'Zip Code')) {
		return false;
	}
	
	// County other than default (blank) (do not allow city/state or zip code
	if (!isValidAddress(form.County, 'County')) {
		return false;
	}
	
	// Valid City (do not allow county or zip code)
	if (!isValidAddress(form.City, 'City')) {
		return false;
	}

	// State other than Indiana (do not allow county or zip code)
	if (!isValidAddress(form.State, 'State')) {
		return false;
	}

	// Submit the form
	form.submit();
}