// JavaScript Document
function populateHospitalList(moName, strSpecialty, strState, hosp, isMO)
{
	//var y = document.createElement('option');
	var x = hosp;
	var i, w, s;

	// remove any options in the hospitalList if any
	removeAllOptions(hosp);

	// call function
	s = splitArray(moName, arrHospital, strSpecialty.value, isMO)

	// need to weed out the duplicates
	s.sort();
	w = removeDuplicates(s);

	for (i=0; i<=w.length-1; i++)
	{
		tempState = w[i].substring(0,3); 
		tempSpec = w[i];

		if ( tempSpec != w[i-1] ) {
			if ( strState.value == tempState ) // display a particular state
			{
				addOption(hosp,w[i],i+1);
			}
			else if (strState.value == "All" ) // display all the states in the drop down list
			{
				addOption(hosp,w[i],i+1);
			}
		}
	}
}

function populateSpecialty(objMO, rmo, scmo, both, objSpec, objState, objHosp)
{
	objState.options[0].selected = true; // move selectedIndex to 0 or ""
	if ( objHosp.length > 1 ) { objHosp.options[0].selected = true; }
	removeAllOptions(objSpec);
	
	if ( objMO == "Both" )
	{
		for ( i=0; i < both.length; i++ )
		{
			addOption(objSpec,both[i],both[i]);
		}
	}
	else if ( objMO == "RMO" )
	{
		for ( j=0; j < rmo.length; j++ )
		{
			addOption(objSpec,rmo[j],rmo[j]);
		}
	}
	else if ( objMO == "SCMO" )
	{
		for ( k=0; k < scmo.length; k++ )
		{
			addOption(objSpec,scmo[k],scmo[k]);
		}
	}
}

function splitArray(mo, hosp, spec, isMO)
{
	// temp array
	var arrTemp = new Array()
	var g, j, x;
	j = 0;

	if ( mo != "Both" )
	{
		for (g=0; g<=hosp.length-1; g++)
		{
			x = hosp[g].split("|"); // grab one of the values and split on |
			if ( spec != "All" )
			{
				if ( x[1] == mo && isMO == true )
				{
					if ( x[0] == spec )
					{
						arrTemp[j] = x[2];
						j++;
					}
				}
				else if ( x[1] == "N/A" && isMO == false )
				{
					if ( x[0] == spec )
					{
						arrTemp[j] = x[2];
						j++;
					}
				}
			}
			else if ( spec == "All" )
			{
				if ( x[1] == mo )
				{
					arrTemp[j] = x[2];
					j++;
				}
			}
			
		}
	}
	else if ( mo == "Both" )
	{
		for (g=0; g<=hosp.length-1; g++)
		{
			x = hosp[g].split("|"); // grab one of the values and split on |
			if ( x[1] != "N/A" )
			{
				if ( spec != "All" )
				{
					if ( x[0] == spec )
					{
						arrTemp[j] = x[2];
						j++;
					}
				}
				else if ( spec == "All" )
				{
					arrTemp[j] = x[2];
					j++;
				}
			}
		}
	}
	
	return arrTemp;
}

function removeDuplicates(arrTemp)
{
	var p = new Array();
	var u = ""; // no uniques found yet
	var j = 0;
	
	for ( i=0; i<arrTemp.length; i++ )
	{
		if ( arrTemp[i] != u )
		{
			p[j] = arrTemp[i];
			u = p[j];
			j++; //alert(u);
		}
	}

	return p;
}

function addOption(object,text,value)
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	object.options.add(optn);
}

function removeAllOptions(selectbox)
{
	var i; //alert(selectbox.length);
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		selectbox.remove(i);
	}
}

function gotoLink(mo, spec, hospital)
{
	var g, x;

	for (g=0; g<=arrHospital.length-1; g++)
	{
		x = arrHospital[g].split("|"); // grab one of the values and split on |
		if ( x[1] == mo.value && x[1] != "N/A" )
		{
			if ( x[0] == spec.value )
			{
				if ( x[2] == hospital.options[hospital.selectedIndex].text )
				{
					// go to url link
					window.location="http://www.opps4docs.com.au/Hospital-Profiles-by-State/hospital-profile.aspx?ID=" + x[3];
					break;
				}		
			}
		}
		else if ( x[1] == "N/A" )
		{
			if ( x[0] == spec.value )
			{
				if ( x[2] == hospital.options[hospital.selectedIndex].text )
				{
					// go to url link
					window.location="http://www.opps4docs.com.au/Hospital-Profiles-by-State/hospital-profile.aspx?ID=" + x[3];
					break;
				}		
			}

		}
	}	
}

function gotoMOLink(mo, spec, hospital)
{
	var g, x;

	for (g=0; g<=arrHospital.length-1; g++)
	{
		x = arrHospital[g].split("|"); // grab one of the values and split on |

		if ( mo.value == "RMO" )
		{
			if ( "All" == spec.value )
			{
				if ( x[2] == hospital.options[hospital.selectedIndex].text )
				{
					// go to url link
					window.location="http://www.opps4docs.com.au/Hospital-Profiles-by-State/hospital-profile.aspx?ID=" + x[3];
					break;
				}		
			}
			else if ( spec.value != "All" )
			{
				if ( x[2] == hospital.options[hospital.selectedIndex].text )
				{
					// go to url link
					window.location="http://www.opps4docs.com.au/Hospital-Profiles-by-State/hospital-profile.aspx?ID=" + x[3];
					break;
				}
			}
		}
		else if ( mo.value == "SCMO" )
		{
			if ( "All" == spec.value )
			{
				if ( x[2] == hospital.options[hospital.selectedIndex].text )
				{
					// go to url link
					window.location="http://www.opps4docs.com.au/Hospital-Profiles-by-State/hospital-profile.aspx?ID=" + x[3];
					break;
				}		
			}
			else if ( spec.value != "All" )
			{
				if ( x[2] == hospital.options[hospital.selectedIndex].text )
				{
					// go to url link
					window.location="http://www.opps4docs.com.au/Hospital-Profiles-by-State/hospital-profile.aspx?ID=" + x[3];
					break;
				}
			}
		}
		else if ( mo.value == "Both" )
		{
			if ( "All" == spec.value )
			{
				if ( x[2] == hospital.options[hospital.selectedIndex].text )
				{
					// go to url link
					window.location="http://www.opps4docs.com.au/Hospital-Profiles-by-State/hospital-profile.aspx?ID=" + x[3];
					break;
				}		
			}
			else if ( spec.value != "All" )
			{
				if ( x[2] == hospital.options[hospital.selectedIndex].text )
				{
					// go to url link
					window.location="http://www.opps4docs.com.au/Hospital-Profiles-by-State/hospital-profile.aspx?ID=" + x[3];
					break;
				}
			}
		}
	}	
}

function resetValue(objState, objHosp)
{
	if ( objState != "undefined" || objState != null || objState != "" )
	{
		objState.options[0].selected = true;
		if ( objHosp.length > 1 ) { removeAllOptions(objHosp) } // remove or set value to 0
		return true;
	}
	return false;
}

function populateTheList(objMO, objSpec, objState, objHosp)
{
	// temp array
	var arrTemp = new Array()
	var g, j, x;
	j = 0;
	
	removeAllOptions(objHosp);

	if ( objMO.value != "Both" )
	{
		for (g=0; g<=arrHospital.length-1; g++)
		{ 
			x = arrHospital[g].split("|"); // grab one of the values and split on |
	
			if ( objSpec.value != "All" )
			{
				if ( x[1] == objMO.value )
				{
					if ( x[0] == objSpec.value )
					{
						arrTemp[j] = x[2];
						j++;
					}
				}
			}
			else if ( objSpec.value == "All" )
			{
				if ( x[1] == objMO.value )
				{
					arrTemp[j] = x[2];
					j++;
				}
			}
		}
	}
	else
	{
		for (g=0; g<=arrHospital.length-1; g++)
		{
			x = arrHospital[g].split("|"); // grab one of the values and split on |
	
			if ( x[1] != "N/A" )
			{
				if ( objSpec.value != "All" )
				{
					if ( x[0] == objSpec.value )
					{
						arrTemp[j] = x[2];
						j++;
					}
				}
				else if ( objSpec.value == "All" )
				{
					arrTemp[j] = x[2];
					j++;
				}
			}
		}
	}

	// need to weed out the duplicates
	arrTemp.sort();
	w = removeDuplicates(arrTemp);

	for (i=0; i<=w.length-1; i++)
	{
		tempState = w[i].substring(0,3); 
		tempSpec = w[i];

			if ( objState.value == tempState ) // display a particular state
			{
				addOption(objHosp,w[i],i+1);
			}
			else if (objState.value == "All" ) // display all the states in the drop down list
			{
				addOption(objHosp,w[i],i+1);
			}
	}
}

