/* Health Plan Search Functions */

//run this when the pages has loaded
function pageLoad() {
	checkLocationParameters();
	toggleObject("physician","physician_container",showObject,hideObject);
	toggleObject("ancillary","ancillary_container",showObject,hideObject);
	toggleObject("hospital","hospital_container",showObject,hideObject);
}
function pageLoadResults() {
	enableRowMouseOvers();
}

function checkMaxSelected(selobj,maxselected) {
	var count = 0;
	var last = -1;
	for(var i=0; i<selobj.options.length; i++) {
		if(selobj.options[i].selected) {
			count += 1;
			last = i;
		}
	}
	if(count > maxselected) {
		alert("You have selected the maximum number of items in this list.\n\nThe \"lowest\" item you have selected will be removed.\nThis may not be the \"last\" item you selected.");
		selobj.options[last].selected = false;
	}
}

function checkLocationParameters(id) {
	var zip = getObject("zipcode");
	var cty = getObject("city");
	if((id == "zipcode") && (!isNaN(zip.value)) && (zip.value !== "")) {cty.selectedIndex = 0;}
	else if((id == "city") && (cty.selectedIndex >= 2)) {zip.value = "";}
	if(((isNaN(zip.value)) || (zip.value === "")) && (cty.selectedIndex < 2)) {
		updateClassName(document.getElementById("location_container"),"","enabled");
	} else {
		updateClassName(document.getElementById("location_container"),"enabled",""); 
	}
}

//sets row mouseovers for results data
function enableRowMouseOvers() {
	var pdata = getObject("physicianresults");
	var hdata = getObject("hospitalresults");
	var adata = getObject("ancillaryresults");
	if(pdata !== null) {
		var prows = pdata.getElementsByTagName("tr");
		for(var i=0; i<prows.length; i++) {
			prows[i].onmouseover = function() {updateClassName(this,"over","");};
			prows[i].onmouseout = function() {updateClassName(this,"","over");};
			prows[i].onclick = function() {showDetailPage(this.id);};
		}
	}
	if(hdata !== null) {
		var hrows = hdata.getElementsByTagName("tr");
		for(var j=0; j<hrows.length; j++) {
			hrows[j].onmouseover = function() {updateClassName(this,"over","");};
			hrows[j].onmouseout = function() {updateClassName(this,"","over");};
			hrows[j].onclick = function() {showDetailPage(this.id);};
		}
	}
	if(adata !== null) {
		var arows = adata.getElementsByTagName("tr");
		for(var k=0; k<arows.length; k++) {
			arows[k].onmouseover = function() {updateClassName(this,"over","");};
			arows[k].onmouseout = function() {updateClassName(this,"","over");};
			arows[k].onclick = function() {showDetailPage(this.id);};
		}
	}
}

//redirects user to detail page
function showDetailPage(rowid) {
	if(rowid.length > 0) {
		var id = parseElementId(rowid);
		var type = rowid.slice(0,1);
		if(type == "p") {window.location.href = "HealthPlanDetail"+PageRegionId+".cfm?WebProviderId="+id;}
		else if(type == "h") {window.location.href = "HealthPlanDetail"+PageRegionId+".cfm?WebHospitalId="+id;}
		else if(type == "a") {window.location.href = "HealthPlanDetail"+PageRegionId+".cfm?WebAncillaryId="+id;}
	}
}

//validates search form
function validateSearchForm(form) {
	var message = "";
	var errors = "";
	var warnings = "";
	if(((isNaN(form.zipcode.value)) || (form.zipcode.value === "")) && (form.city.value === "")) {
		warnings += "You did not specify a location from which to base your search.\n";
		if(form.physician.checked) {
			if((form.physician_specialty.selectedIndex == -1) && (form.physician_name.value === "")) {
				warnings += "- All Physician records will be searched.\n";
			}
		}
		if(form.ancillary.checked) {
			if((form.ancillary_type.selectedIndex == -1) && (form.ancillary_name.value === "")) {
				warnings += "- All Ancillary Services records will be searched.\n";
			}
		}
		if(form.hospital.checked) {
			if(form.physician_name.value === "") {
				warnings += "- All Hospital records will be searched.\n";
			}
		}
		
	}
	if((!form.physician.checked) && (!form.ancillary.checked) && (!form.hospital.checked)) {
		errors += "Please select at least one Provider Type to show.\n";
	}
	if(errors.length) {
		message = "Before you submit your search, complete the items shown below.\n\n";
		alert(message + errors);
		return false;
	}
	if(warnings.length) {
		message = "\n\nYour results may take some time to compile.\n\nAre you sure you wish to proceed to the search results.";
		return confirm(warnings + message);
	}
	return true;
}

//hide page object
function hideObject(obj) {
	var obj_to_hide = getObject(obj);
	if(obj_to_hide !== null) {
		obj_to_hide.style.display = "none";
	}
	updateClassName(obj_to_hide.parentNode.parentNode,"","enabled");
}
//show page object
function showObject(obj) {
	var obj_to_show = getObject(obj);
	if(obj_to_show !== null) {
		obj_to_show.style.display = "block";
	}
	updateClassName(obj_to_show.parentNode.parentNode,"enabled","");
}

//examines "checked" property of object and runs appropriate function thereafter
function toggleObject(toggler,togglee,onTrue,onFalse) {
	var obj_to_check = getObject(toggler);
	var objs_to_toggle = togglee.split(",");
	var obj_to_toggle = null;
	for(var i=0; i<objs_to_toggle.length; i++) {
		obj_to_toggle = getObject(objs_to_toggle[i]);
		if(obj_to_toggle !== null) {
			if(obj_to_check.checked) {onTrue(obj_to_toggle);}
			else {onFalse(obj_to_toggle);}
		}
	}
}

/*
BASE FUNCTIONS
Can be re-used in any application.
*/

//returns document object, checks for string
function getObject(obj) {
	//alert("getObject invoked "+obj);
	if(typeof(obj) == "string") {return document.getElementById(obj);}
	return obj;
}

//add onload event to page
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//updates the class attribute of an object
function updateClassName(obj,newclass,classtoreplace) {
	var modifiedclass = obj.className;
	var modifiedclass2 = "";
	if (classtoreplace !== "") {
		var classEStoreplace = classtoreplace.split(",");
		for(var i=0; i<classEStoreplace.length; i++) {
			if(modifiedclass.indexOf(classEStoreplace[i]) != -1) {
				modifiedclass = modifiedclass.replace(classEStoreplace[i],"");
			}
		}
	}
	if (newclass !== "") {
		var newclassES = newclass.split(",");
		for(var k=0; k<newclassES.length; k++) {
			if (modifiedclass.indexOf(newclassES[k]) == -1) {
				if (modifiedclass.length > 0) {
					modifiedclass = modifiedclass + " " + newclassES[k];
				} else {
					modifiedclass = newclassES[k];
				}
			}
		}
	}
	var spacedclass = modifiedclass.split(" ");
	for(var j=0; j<spacedclass.length; j++) {
		if(spacedclass[j].length > 0) {
			modifiedclass2 += spacedclass[j] + " ";
		}
	}
	obj.setAttribute("class", modifiedclass2);
	obj.setAttribute("className", modifiedclass2);
	//alert("final: |"+modifiedclass2+"|");
}

//function gets the element id if appended to attribute id separated by "__"
//	example: <img id="product__22" src="..." ... />
//	=> 22 would be returned from the id attribute from the img tag above passed to this function
function parseElementId(id) {
	var finalid = null;
	var pos = id.lastIndexOf("__");
	if (pos != -1 && id.length > pos+1) {
		finalid =  id.substr(pos+2);
	}
	finalid = parseInt(finalid,10);
	if (isNaN(finalid)) {
		return null;
	} else {
		return finalid;
	}
}
