/*
* Home Page Search Javascript
* Use in conjunction with homepage.js and provider_search.js
* Calls functions in provider_search.js
* Date: 10.16.2007 - Edited: 07.24.2008
* Author: Gerard Konars
*/

var AllFields = {};
function hideAll() {
	for(var elem in AllFields) {
		if(AllFields.hasOwnProperty(elem)) {
			AllFields[elem].className = "hide";
		}
	}
}

// PROVIDER SEARCH: show/hide form fields based on select change.  Changes form actions.
function toggleFields() {
	AllFields.county = document.getElementById('select-county');
	AllFields.doctor = document.getElementById('search-doctor');
	AllFields.plantype = document.getElementById('plan-type');
	AllFields.pHospitalBlue = document.getElementById('physicianHospitalBlue');
	AllFields.fplantype = document.getElementById('facility-plan-type');
	AllFields.fHospitalBlue = document.getElementById('facilityHospitalBlue');
	AllFields.hospital = document.getElementById('search-facility');
	AllFields.hospitalMappo = document.getElementById('search-facility-mappo');
	AllFields.hospitalBCPPO = document.getElementById('search-facility-bcppo');
	AllFields.vision = document.getElementById('search-vision');
	AllFields.hearing = document.getElementById('search-hearing');
	
	findaform = document.getElementById('search-findA');
	selectedSearch = document.Search.findA.options[document.Search.findA.selectedIndex].value;
	
	
	if(selectedSearch=="dentist")
	{
		document.getElementById('hideForPlans').style.display="none";
		document.getElementById('showForDentist').style.display="block";
		document.getElementById('showForNational').style.display="none";			
		return;
	}
	else if(selectedSearch=="national")
	{
		document.getElementById('hideForPlans').style.display="none";
		document.getElementById('showForNational').style.display="block";
		document.getElementById('showForDentist').style.display="none";		
		return;
	}	
	else
	{
		document.getElementById('hideForPlans').style.display="block";
		document.getElementById('showForDentist').style.display="none";	
		document.getElementById('showForNational').style.display="none";
	}
		
	
	if(selectedSearch=="none")
	{
		alert('You must select a plan type')
		document.getElementById('search-findA').focus();
		return;
	}

	

	
	
	switch(selectedSearch) {
		case "hospital" :
			hideAll();
			AllFields.hospital.className = "show";
			AllFields.fplantype.className = "show";
			AllFields.county.className = "show";
			AllFields.county.setAttribute("name", "fcounty");
			findaform.action = "http://www.bcbsm.com/FacilityDirectories/FacilitySearch.jsp";
			//findaform.action = "http://nsx401a7040.bcbsm.com/FacilityDirectories/FacilitySearch.jsp";
			flipHospitalBlue();
		break;
		case "doctor" :
			hideAll();
			AllFields.doctor.className = "show";
			AllFields.plantype.className = "show";
			AllFields.county.className = "show";
			AllFields.county.setAttribute("name", "provCounty");
			findaform.action = "http://www.bcbsm.com/provdir/ProviderSearchServlet";
			//findaform.action = "http://nsx401a7040.bcbsm.com/provdir/ProviderSearchServlet";
			flipHospitalBlue();
		break;
		case "vision" :
			hideAll();
			AllFields.vision.className = "show";
			AllFields.county.className = "show";	
			AllFields.county.setAttribute("name", "provCounty");
			findaform.action = "http://www.bcbsm.com/vision/ProviderSearchServlet";
			//findaform.action = "http://nsx401a7040.bcbsm.com/vision/ProviderSearchServlet";
		break;
		case "hearing" :
			hideAll();
			AllFields.hearing.className = "show";
			AllFields.county.className = "show";
			AllFields.county.setAttribute("name", "provCounty");
			findaform.action = "http://www.bcbsm.com/vision/ProviderSearchServlet";
			//findaform.action = "http://nsx401a7040.bcbsm.com/vision/ProviderSearchServlet";
		break;
		default : // default to doctor search
			hideAll();
			AllFields.doctor.className = "show";
			AllFields.county.className = "show";
			AllFields.county.setAttribute("name", "provCounty");
			findaform.action = "http://www.bcbsm.com/provdir/ProviderSearchServlet";
			//findaform.action = "http://nsx401a7040.bcbsm.com/vision/ProviderSearchServlet";
	}
		
}


// VALIDATOR
// CHECK REQUIRED: look for 'required' class name in SELECTs throw alert if option has no value.
function checkRequired() {
	var returnval;
	var facilityType = document.Search.ftype.value;
	var healthPlanType = document.Search.healthPlanType.options[document.Search.healthPlanType.selectedIndex].value;
	var visionPlanType = document.Search.visionPlanType.options[document.Search.visionPlanType.selectedIndex].value;
	var hearingPlanType = document.Search.hearingPlanType.options[document.Search.hearingPlanType.selectedIndex].value;
/*
	alert('in checkRequired function ' + '\n' + selectedSearch + '\n' + facilityType  + '\n' + healthPlanType  + '\n' + visionPlanType + '\n' + hearingPlanType);
 */
	switch(selectedSearch) {
		case 'hospital' :
					if (facilityType === "") {
						alert('Please select a facility type');
						returnval = false;
						return returnval;
					} else {
						returnval = true;
						return returnval;
					}
		break;
		case 'doctor' :
					if (healthPlanType === "") {
						alert('Please choose a plan');
						returnval = false;
						return returnval;
					} else {
						returnval = true;
						return returnval;
					}
		break;
		case 'vision' :
		 			if (visionPlanType === "") {
						alert('Please select a vision provider type');
						returnval = false;
						return returnval;
					} else {
						returnval = true;
						return returnval;
					}
		break;
		case 'hearing' :
					if (hearingPlanType === "") {
						alert('Please select a hearing specialist type');
						returnval = false;
						return returnval;
					} else {
						returnval = true;
						return returnval;
					}
		break;
		default :
				//	alert('default returnval = false');
					returnval = false;
					return returnval;
	}
}

// HOSPITAL BLUE OPTION
// Check to see if checkHospitalBlue should run on return to page from search results called by toggleFields function.
function flipHospitalBlue() {
	var findType = document.getElementById('findA');
	var findTypeSelected = findType.options[findType.selectedIndex].value;
	var docPlan = document.getElementById('field-plan');
	var docPlanSelected = docPlan.options[docPlan.selectedIndex].value;
	var provPlan = document.getElementById('facility-field-plan');
	var provPlanSelected = provPlan.options[provPlan.selectedIndex].value;
	
//	alert(findTypeSelected + "\n" + docPlanSelected + "\n" + provPlanSelected);
	
	if(findType && findTypeSelected === 'doctor' && docPlanSelected === 'MHMP') {
		var o = "MHMP";
		var n = "healthPlanType";
		checkHospitalBlue(o, n);
	} else if (findType && findTypeSelected === 'hospital' && provPlanSelected === 'MHMP') {
		var o =	"MHMP";
		var n = "fplantype";
		checkHospitalBlue(o, n);
	} else if (findType && findTypeSelected === 'hospital' && provPlanSelected === 'MAPPO'){
		var o =	"MAPPO";
		var n = "fplantype";
		checkHospitalBlue(o, n);
	}
}


// Display secondary dropdown menu if Hospital Blue is selected.
function checkHospitalBlue(o, n) {
	AllFields.pHospitalBlue.className = "hide";
	AllFields.fHospitalBlue.className = "hide";
	AllFields.hospital.className = "hide";
	AllFields.hospitalMappo.className = "hide";
	AllFields.hospitalBCPPO.className = "hide";
	if(o === 'MHMP' && n === 'healthPlanType') { // doctor
		AllFields.pHospitalBlue.className = "show";
	} else if ( o === 'MHMP' && n === 'fplantype') { // facility
		AllFields.fHospitalBlue.className = "show";
	} else if ( o == 'MAPPO' && n === 'fplantype') {
		AllFields.hospitalMappo.className = "show";
	}
	else if( o == 'PPOT3' && n === 'fplantype') {
		AllFields.hospitalBCPPO.className = "show";		
	}
	else if (n === 'fplantype') {
		AllFields.hospital.className = "show";
	}
}

/*	
// HASCLASS: function to determine if object has a particular class.
function hasClass(target, theClass) {
	var pattern = new RegExp("(^| )" + theClass + "( |$)");
	if (pattern.test(target.className)) {
		return true;
		alert("hasClass = true");
	};
	return false;
	alert("hasClass = false");
};
*/	

// SWITCH SPECIALTY OPTIONS - this mimics the "verifyPlansAndSelections(theForm)" function from provider_search.js
// it calls functions from provider_search.js.

function switchSpecialties(hPlanType) {
	var theForm = document.getElementById('search-findA');

	if(selectedSearch == 'doctor' && hPlanType) {
		if(hPlanType == '99' || hPlanType == 'MEDA' || hPlanType == '15' || hPlanType == 'TRIM' || hPlanType == 'ECCA') {
			chooseSpecialtyForBCN(findaform);
		}
		else if(hPlanType == 'TRD') {
			chooseSpecialtyForTraditional(findaform);
		}
		else {
			chooseSpecialtyForNonBCN(findaform);
		}
	}
	else {
		chooseSpecialtyForNonBCN(findaform);
	}
}