/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
 var parent = '';
 var compName = '';
 var spCatIndex = 0;
function GetXmlHttpRequest()
{
   var xmlreq = false;
   if (window.XMLHttpRequest)
   {
      // Create XMLHttpRequest object in non - Microsoft browsers
      xmlreq = new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      // Create XMLHttpRequest via MS ActiveX
      try
      {
         // Try to create XMLHttpRequest in later versions
         // of Internet Explorer
         xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e1)
      {
         // Failed to create required ActiveXObject
         try
         {
            // Try version supported by older versions
            // of Internet Explorer
            xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e2)
         {
            // Unable to create an XMLHttpRequest with ActiveX
         }
      }
   }

   return xmlreq;
}

function listTOC(parent1, compName1 )
{
	parent = parent1;
	compName = compName1;
	var selectedValue = '';
	if("TYPEOFCARE" == compName){
		selectedValue = parent.TYPEOFCARE.options[parent.TYPEOFCARE.selectedIndex].value;
	}else if("SPECIALTY_CATEGORY" == compName){
		selectedValue = parent.SPECIALTY_CATEGORY.options[parent.SPECIALTY_CATEGORY.selectedIndex].value;
	}else if("SPECIALTY" == compName){
		selectedValue = parent.SPECIALTY.options[parent.SPECIALTY.selectedIndex].value;
	}
	var requestUrl = "search";
	
	XMLHttp = GetXmlHttpRequest()
	if (XMLHttp == null)
	{
		  alert ("Browser does not support HTTP Request");
		  return;
	}
	var param = "selectedTOC="+selectedValue+"&selectedComp="+compName;
   	XMLHttp.onreadystatechange = handleResponse;
   	XMLHttp.open("POST", requestUrl,  true);
   	XMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   	XMLHttp.send(param);          
}
function handleResponse()
{

    if(XMLHttp.readyState == 4)
    {
        if(XMLHttp.status == 200)
        {
            var tocXML = XMLHttp.responseXML;
            var toc = tocXML.documentElement;
      
			if("TYPEOFCARE" == compName){
		    	removeAllOptions(parent.SPECIALTY_CATEGORY);
		    	removeAllOptions(parent.SPECIALTY);
		    	addOption(parent.SPECIALTY_CATEGORY,"All","");
		    	addOption(parent.SPECIALTY,"All","");
		    }else if("SPECIALTY_CATEGORY" == compName){
		    	//removeAllOptions(parent.TYPEOFCARE);
		    	removeAllOptions(parent.SPECIALTY);
		    	//addOption(parent.TYPEOFCARE,"All","");
		    	addOption(parent.SPECIALTY,"All","");
		    }else if("SPECIALTY" == compName){
		    	//removeAllOptions(parent.TYPEOFCARE);
		    	//removeAllOptions(parent.SPECIALTY_CATEGORY);
		    	//addOption(parent.TYPEOFCARE,"All","");
		    	//addOption(parent.SPECIALTY_CATEGORY,"All","");
		    }
        	//document.providerSearchForm.SPECIALTY.disabled=false;
		    for (loop = 0; loop < toc.childNodes.length; loop++) {
		   			var tocSpec = toc.childNodes[loop];
           	var name = tocSpec.getElementsByTagName("Code")[0];
           	var desc = tocSpec.getElementsByTagName("Description")[0];
				if(tocSpec.tagName == "TypeOfCare"){
					if("SPECIALTY_CATEGORY" == compName ||"SPECIALTY" == compName ){
						getElementIndex(parent.TYPEOFCARE,name.childNodes[0].nodeValue);
						parent.TYPEOFCARE.options[spCatIndex].selected = true;
					}
					//addOption(parent.TYPEOFCARE,desc.childNodes[0].nodeValue,name.childNodes[0].nodeValue);
				}else if(tocSpec.tagName == "SpecialtyCategory"){
					if("TYPEOFCARE" == compName){
						addOption(parent.SPECIALTY_CATEGORY,desc.childNodes[0].nodeValue,name.childNodes[0].nodeValue);
					}
					if("SPECIALTY" == compName){
						getElementIndex(parent.SPECIALTY_CATEGORY,name.childNodes[0].nodeValue);
						parent.SPECIALTY_CATEGORY.options[spCatIndex].selected = true;
					}
				}else if(tocSpec.tagName == "Specialty"){
					addOption(parent.SPECIALTY,desc.childNodes[0].nodeValue,name.childNodes[0].nodeValue);
				}
			}
        }
        else
        {
            alert("There was a problem retrieving data from the server." );
        }
    }
}

function sendPhilReq(url)
{
	XMLHttpPhil = GetXmlHttpRequest();
	if (XMLHttpPhil == null)
	{
		  //alert ("Browser does not support HTTP Request");
		  return;
	}
	if (XMLHttpPhil) {
	    XMLHttpPhil.onreadystatechange = showPhil;
	    try{
		    XMLHttpPhil.open("GET", url, true);
		    XMLHttpPhil.send(url);
		}catch(err){
		}
	}
	       
}

function showPhil()
{
  if (XMLHttpPhil.readyState == 4 ){
	if(XMLHttpPhil.status==200){	     
	    document.getElementById("phil_div").innerHTML = XMLHttpPhil.responseText;
	    showHideSpan('phil_on',true); 
	}
  }else{
  	showHideSpan('phil_on',false);
  }
}


function clearList(list)
{
	var i=0;
	var o = list.options;
	for (i = o.length; i >=0; --i)
	o[i] = null;
}

function addElement(list, text_in, value_in)
{
	var o = list.options;
	var nIdx;
	if (o.length < 0) 
	nIdx = 0;
	else
	nIdx = o.length;
	o[nIdx] = new Option(text_in, value_in);
}
function getElementIndex(list, key)
{
	var o = list.options;
	if(o && o.length > 0){
		for (i=0;i < o.length; i++){
			if(key == o[i].value){
				spCatIndex = i;
				break;
			}
		}
	}
}

function showAgeText(comp,itemID)
{
	
    if(comp.checked){
    	document.getElementById(itemID).style.visibility = 'visible';
    }else{
    	document.providerSearchForm.PAT_AGE.value = '';
    	document.getElementById(itemID).style.visibility = 'hidden';
    }
    
}
function limitSelection(obj){
	var selCnt = 0;
	for(i=0; i < obj.length; ++i){
		if(obj[i].selected && obj[i].value != ''){
			selCnt += 1 ;
			if(selCnt > 3){
				obj[i].selected = false;
			}
		}
	}
}
function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
	selectbox.remove(i);
	}
}

function addOption(selectbox,text,value )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}
function removeOption(selectbox,value )
{
  for( i=selectbox.length-1; i>=0; i--) {	
    if(selectbox.options[i].value == value) {
		selectbox.options.remove(i);    
    }
  }
}