/***************************************
START : VARIABLES TO HOLD LISTBOX VALUES
****************************************/
var availableStateList;
var selectedStateList;
var hiddenState1;
var hiddenState2;

var availableCityList;
var selectedCityList;
var hiddenCity1;
var hiddenCity2;

var availableCountyList;
var selectedCountyList;
var hiddenCounty1;
var hiddenCounty2;

var availableMSAList;
var selectedMSAList;
var hiddenMSA1;
var hiddenMSA2;

var availableTitleList;
var selectedTitleList;
var hiddenTitle1;
var hiddenTitle2;

var availableSpecialtyList;
var selectedSpecialtyList;
var hiddenSpecialty1;
var hiddenSpecialty2;

/* Trait Divs */
var objGMP;
var objHSP;


var debug = 0;

/***************************************
END : VARIABLES TO HOLD LISTBOX VALUES
****************************************/




// ----------------------------------------------------------------------------
// addAttribute
//
// Description : event handler for moving items from one listbox to another
//    updates the "TO" listbox with the selected value in the "FROM" listbox
//
// Arguments: listbox1, listbox2
//
//
function addAttribute(list1, list2)
{
    //alert("1");
    var addIndex = list1.selectedIndex;
       
   if(addIndex < 0)
      return;      
    
    if(getNodeType(list1.id))
    {
    //alert("2");
       // ADDS VALUES TO THE TREE LIST
       addNode(getNodeType(list1.id), list1.options[addIndex].text, list1.options[addIndex].value);
    }
    
    //APPEND VALUE TO THE RIGH-LIST
   list2.appendChild( 
      list1.options.item(addIndex));			
   //alert("3");
   //AddHiddenValues(hfield, addIndex, list1.options[addIndex].text, list1.options[addIndex].value);
  
   selectNone(list2,list1);
   
   setSize(list2,list1);
}
//
// addAttribute
// ----------------------------------------------------------------------------



// ----------------------------------------------------------------------------
// move
//
// Description : event handler for moving items from one listbox to another
//    updates the "TO" listbox with the selected value in the "FROM" listbox
//
// Arguments: fromlistbox, tolistbox, isfirst
//
//
function move(fbox, tbox, isFirst) 
{     
    if(debug == 1)
        alert("in Move");
    
    // new temp arrays to hold values of list boxes and lookup    
    var arrFbox = new Array();     
    var arrTbox = new Array();     
    var arrLookup = new Array();     
    var i;     

    // go through the "TO" listbox and fill the lookup and new "TO" array
    for (i = 0; i < tbox.options.length; i++) 
    {     
        
	    arrLookup[tbox.options[i].text] = tbox.options[i].value;     
	    arrTbox[i] = tbox.options[i].text;     
    }     

    var fLength = 0;     
    var tLength = arrTbox.length;     
    
    
    // go through "FROM" listbox and fill the lookup and any select options to new "TO" array
    // else add it to the new "FROM" array
    for(i = 0; i < fbox.options.length; i++) 
    {     
	    arrLookup[fbox.options[i].text] = fbox.options[i].value;     
	    if (fbox.options[i].selected && fbox.options[i].value != "") 
	    {     arrTbox[tLength] = fbox.options[i].text;     
		    tLength++;     
	    }     
	    else 
	    {     
		    arrFbox[fLength] = fbox.options[i].text;     
		    fLength++;     
	    }     
    }     

    // sort the new arrays
    arrFbox.sort();     
    arrTbox.sort();     
    fbox.length = 0;     
    tbox.length = 0;     
    var c;     

    // delete all nodes in the tree that were in the "FROM" list box
    delAllNode(getNodeType(fbox.id));
   
   // go through new "FROM" array and add nodes to tree and options to "FROM" listbox 
    for(c = 0; c < arrFbox.length; c++)
    {   
        if (isFirst == false)
            addNode(getNodeType(tbox.id), arrFbox[c], arrLookup[arrFbox[c]]);
          
	    var no = new Option();     
	    no.value = arrLookup[arrFbox[c]];     
	    no.text = arrFbox[c];     
	    fbox[c] = no;     
    }     

    
    // go through new "TO" array and add nodes to tree and options to "TO" listbox 
    for(c = 0; c < arrTbox.length; c++) 
    {    
        if(isFirst == true) 
           addNode(getNodeType(fbox.id), arrTbox[c], arrLookup[arrTbox[c]]);
        
	    var no = new Option();     
	    no.value = arrLookup[arrTbox[c]];     
	    no.text = arrTbox[c];     
	    tbox[c] = no;     
    }     

}     
//
// move
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// moveAll
//
// Description : event handler for moving all items from one listbox to another
//    updates the "TO" listbox with the all values in the "FROM" listbox
//
// Arguments: fromlistbox, tolistbox, isfirst
//
//
function moveall(fbox, tbox, isFirst) 
{     
    if(debug == 1)
        alert("in Move All");
    
    // new temp arrays to hold values of list boxes and lookup    
    var arrFbox = new Array();     
    var arrTbox = new Array();     
    var arrLookup = new Array();     
    var i;     

    // go through the "TO" listbox and fill the lookup and new "TO" array
    for (i = 0; i < tbox.options.length; i++) 
    {     
        
	    arrLookup[tbox.options[i].text] = tbox.options[i].value;     
	    arrTbox[i] = tbox.options[i].text;     
    }     

    var fLength = 0;     
    var tLength = arrTbox.length;     
    
    
    // go through "FROM" listbox and fill the lookup and any select options to new "TO" array
    // else add it to the new "FROM" array
    for(i = 0; i < fbox.options.length; i++) 
    {     
	    arrLookup[fbox.options[i].text] = fbox.options[i].value;     
	    //if (fbox.options[i].selected && fbox.options[i].value != "") 
	    //{     
	        arrTbox[tLength] = fbox.options[i].text;     
		    tLength++;     
	    //}     
	    //else 
	    //{     
		    //arrFbox[fLength] = fbox.options[i].text;     
		    //fLength++;     
	    //}     
    }     

    // sort the new arrays
    arrFbox.sort();     
    arrTbox.sort();     
    fbox.length = 0;     
    tbox.length = 0;     
    var c;     

    // delete all nodes in the tree that were in the "FROM" list box
    delAllNode(getNodeType(fbox.id));
   
   // go through new "FROM" array and add nodes to tree and options to "FROM" listbox 
    for(c = 0; c < arrFbox.length; c++)
    {   
        if (isFirst == false)
            addNode(getNodeType(tbox.id), arrFbox[c], arrLookup[arrFbox[c]]);
          
	    var no = new Option();     
	    no.value = arrLookup[arrFbox[c]];     
	    no.text = arrFbox[c];     
	    fbox[c] = no;     
    }     

    
    // go through new "TO" array and add nodes to tree and options to "TO" listbox 
    for(c = 0; c < arrTbox.length; c++) 
    {    
        if(isFirst == true) 
           addNode(getNodeType(fbox.id), arrTbox[c], arrLookup[arrTbox[c]]);
        
	    var no = new Option();     
	    no.value = arrLookup[arrTbox[c]];     
	    no.text = arrTbox[c];     
	    tbox[c] = no;     
    }     

}     
//
// move
// ----------------------------------------------------------------------------



/*
function AddHiddenValues(hiddenField, listIndex, optionText, optionValue)
{

    if(hiddenField == null)
        hiddenField = listIndex + ', ' + optionText + ', ' + optionValue;
    else
        hiddenField = hiddenField + ', ' + listIndex + ', ' + optionText + ', ' + optionValue;
}
*/

function delAttribute(list1, list2){

    if(debug == 1)
        alert("in delAttribute " + list1 + ", " + list2);
               
    var selIndex = list2.selectedIndex;
   
    if(selIndex < 0)
        return;
    /*
    if((list1.id.indexOf("ListBoxStates") != -1))
        delNode("State", list2.options[selIndex].text, list2.options[selIndex].value);
    else if((list1.id.indexOf("ListBoxCities") != -1))
        delNode("City", list2.options[selIndex].text, list2.options[selIndex].value);  
    else if((list1.id.indexOf("ListBoxCounties") != -1))
        delNode("County", list2.options[selIndex].text, list2.options[selIndex].value); 
    else if((list1.id.indexOf("ListBoxMSAs") != -1))
        delNode("MSA", list2.options[selIndex].text, list2.options[selIndex].value);  
    else if((list1.id.indexOf("ListBoxTitles") != -1))  
        delNode("Title", list2.options[selIndex].text, list2.options[selIndex].value);
    else if((list1.id.indexOf("ListBoxSpecialties") != -1))
        delNode("Specialty", list2.options[selIndex].text, list2.options[selIndex].value);
    */    
      
    if(getNodeType(list1.id))
    {
        delNode(getNodeType(list1.id), list2.options[selIndex].text, list2.options[selIndex].value);
    }    
    list1.appendChild(
        list2.options.item(selIndex))
      
    selectNone(list1,list2);
    setSize(list1,list2);
}

function delAll(list1, list2)
{
    var len = list2.length;
    
    for(i=0; i<len; i++)
    { 
        try
        {
            if(list2.length > 0)
            {
                if(list2.options[0])
                {
                    /*
                    if((list1.id.indexOf("ListBoxStates") != -1))
                        delNode("State", list2.options[0].text, list2.options[0].value);
                    else if((list1.id.indexOf("ListBoxCities") != -1))
                        delNode("City", list2.options[0].text, list2.options[0].value);
                    else if((list1.id.indexOf("ListBoxCounties") != -1))
                        delNode("County", list2.options[0].text, list2.options[0].value); 
                    else if((list1.id.indexOf("ListBoxMSAs") != -1))
                        delNode("MSA", list2.options[0].text, list2.options[0].value); 
                    else if((list1.id.indexOf("ListBoxTitles") != -1))  
                        delNode("Title", list2.options[0].text, list2.options[0].value);
                    else if((list1.id.indexOf("ListBoxSpecialties") != -1))
                        delNode("Specialty", list2.options[0].text, list2.options[0].value);               
                    */
                    
                    if(getNodeType(list1.id))
                    {
                        delNode(getNodeType(list1.id), list2.options[0].text, list2.options[0].value); 
                    
                    }
                }
                
                list1.appendChild(list2.item(0));
            }
        }
        catch(err)
        {
            alert(err.description);
        }
    }
    selectNone(list2,list1);
    setSize(list2,list1);
}

function addAll(list1, list2)
{
    var len = list1.length;
    
    for(i=0; i<len; i++)
    { 
       //alert(i);
       
       try
       {
            if(list1.length > 0)
            {
                
                
                if(list1.options[0])
                {
                    /*
                    if((list1.id.indexOf("ListBoxStates") != -1))
                        addNode("State", list1.options[0].text, list1.options[0].value);
                    else if((list1.id.indexOf("ListBoxCities") != -1))
                        addNode("City", list1.options[0].text, list1.options[0].value); 
                     
                    else if((list1.id.indexOf("ListBoxTitles") != -1))  
                        addNode("Title", list1.options[0].text, list1.options[0].value);
                    else if((list1.id.indexOf("ListBoxSpecialties") != -1))
                        addNode("Specialty", list1.options[0].text, list1.options[0].value);
                    */
                    
                    if(getNodeType(list1.id))
                    {
                        addNode(getNodeType(list1.id), list1.options[0].text, list1.options[0].value);
                    }
                 }
                 
                 list2.appendChild(list1.item(0));
            }
       }
       catch(err)
       {
            alert(err.description);
       }
    }
    
    //alert("done");
    selectNone(list1, list2);
    setSize(list1, list2);
}
function setSize(list1, list2){
    list1.size = getSize(list1);
    list2.size = getSize(list2);
}

function selectNone(list1,list2){
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
}


function getSize(list){
    /* Mozilla ignores whitespace, 
       IE doesn't - count the elements 
       in the list */
    var len = list.childNodes.length;
    var nsLen = 0;
    //nodeType returns 1 for elements
    for(i=0; i<len; i++){
        if(list.childNodes.item(i).nodeType==1)
            nsLen++;
    }
    if(nsLen<2)
        return 2;
    else
        return nsLen;
}

function addNode(listType, filterText, filterValue)
{
    if(debug == 1)
        alert("in addNode " + listType + ", " + filterText + ", " + filterValue);     
               
    var node = igtree_getNodeById(getNodeId(listType));
    
    var newnode;
   
   if (node)
	{
	    try
	    {
	        filterText = filterText.substring(0, 23);
		    newnode = node.addChild(filterText);
		    newnode.setTag(filterValue);
		    node.setExpanded = true;
		    
		    if(listType == "State")
		    {
		        var geonode = igtree_getNodeById(getNodeId("Geography"));
		        
		        //if(geonode)
		        //    geonode.setExpanded = true;
		    }
		    
		    
		}
		catch(err)
		{
		    alert(err.description);
		}
		
		
	}

}

function getNodeId(listType)
{
    if(debug == 1)
        alert("in getNodeId " + listType);

    var nodeid;
    
    if(listType == "Geography")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1"; 
    else if(listType == "State")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1_1";
    else if(listType == "City")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1_2";
    else if(listType == "County")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1_3";
    else if(listType == "MSA")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1_4";
    else if(listType == "Zip")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1_5";
    else if(listType == "Radius")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1_6";
    else if(listType == "ZCode")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1_6_1";
    else if(listType == "Miles")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_1_6_2";
           
    else if(listType == "SiteType")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_2";
    
    else if(listType == "Title")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_3";
        
    else if(listType == "Specialty")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_4";
        
    else if(listType == "Traits")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_5";
    else if(listType == "Decile")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_5_1";
    else if(listType == "DrCount")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_5_2";
    else if(listType == "PatVol")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_5_3";
    else if(listType == "DO")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_5_4";
    else if(listType == "BedCount")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_5_5";
    else if(listType == "HospType")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_5_6";
    
    else if(listType == "Affiliations")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_6";
    else if(listType == "MedGroup")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_6_1";
    else if(listType == "HospSystem")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_6_2";
    else if(listType == "HealthSystem")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_6_3";
    else if(listType == "HospAffil")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_6_4";
        
    else if(listType == "Selects")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_7";
    else if(listType == "Fax")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_7_1"; 
    else if(listType == "Address")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_7_2";   
    else if(listType == "1perSite")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_7_3";
    else if(listType == "1perContact")
        nodeid = "ctl00FilterCriteriaUltraWebTree1_7_4"; 
    return nodeid;
}

function getNodeType(listName)
{
    if(debug == 1)
        alert("in getNodeType " + listName);
        
    var listType;
    
    if((listName.indexOf("ListBoxStates") != -1) || (listName.indexOf("ListBoxStateValues") != -1))
        listType = "State";
    else if((listName.indexOf("ListBoxCities") != -1) || (listName.indexOf("ListBoxCityValues") != -1))
        listType = "City";
    else if((listName.indexOf("ListBoxCounties") != -1) || (listName.indexOf("ListBoxCountyValues") != -1))
        listType = "County";
    else if((listName.indexOf("ListBoxMSAs") != -1) || (listName.indexOf("ListBoxMSAValues") != -1))
        listType = "MSA"; 
    else if((listName.indexOf("ListBoxTitles") != -1) || (listName.indexOf("ListBoxTitleValues") != -1))
        listType = "Title";
    else if((listName.indexOf("ListBoxSpecialties") != -1) || (listName.indexOf("ListBoxSpecialtyValues") != -1))
        listType = "Specialty";
    else if((listName.indexOf("WebTextEditMedGroup") != -1))
        listType = "MedGroup";
    else if((listName.indexOf("WebTextEditHospSystem") != -1))
        listType = "HospSystem";
    else if((listName.indexOf("WebTextEditHealthSystem") != -1))
        listType = "HealthSystem";
    else if((listName.indexOf("WebTextEditHospAffiliation") != -1))
        listType = "HospAffil";
    else if((listName.indexOf("WebTextEditOBPDecile") != -1))
        listType = "Decile";
    else if((listName.indexOf("WebTextEditOBPDrCount") != -1))
        listType = "DrCount";
    else if((listName.indexOf("WebTextEditOBPPatientVolume") != -1))
        listType = "PatVol";
    else if((listName.indexOf("txtBedCount") != -1))
        listType = "BedCount";
    else if((listName.indexOf("DDLHospType") != -1))
        listType = "HospType";
           
    return listType;
}


function delNode(listType, filterText, filterValue)
{
    if(debug == 1)
        alert("inside delNode " + listType + ", " + filterText + ", " + filterValue);
                       
    var node = igtree_getNodeById(getNodeId(listType));
    
    var newnode;
   
    if (node)
	{
	    var node_arr= node.getChildNodes();
	    
	    //alert(node_arr.length);        

	    for ( var i = 0; i < node_arr.length; i++ )
        {
            //alert(node_arr[i].getTag());
            
            //alert(filterValue);
            
            if(filterValue == node_arr[i].getTag())
                node_arr[i].remove();
        }    
	}
}

function delAllNode(listType)
{
    if(debug == 1)
        alert("inside delAllNode " + listType);           
        
    var node = igtree_getNodeById(getNodeId(listType));
    
    var newnode;
   
    if (node)
	{
	    var node_arr= node.getChildNodes();
	    
	    //alert(node_arr.length);        

	    for ( var i = 0; i < node_arr.length; i++ )
        {
            
            node_arr[i].remove();
        }    
	}
}

function persistOptionsList(listBox1, listBox2, hidden1, hidden2)
{
 //var listBox1 = document.getElementById("<%= ListBoxCounties.ClientID %>");
 //var listBox2 = document.getElementById("<%= ListBoxCountyValues.ClientID %>");

 //alert("here");
 var optionsList = '';
 for (var i=0; i<listBox1.options.length; i++)
 {
  var optionText = listBox1.options[i].text;
  var optionValue = listBox1.options[i].value;
  
  if ( optionsList.length > 0 )
   optionsList += ';';
  
  optionsList += optionText + ':' + optionValue;
 }
 //document.getElementById('<%= hiddenCountyValues1.ClientID %>').value = optionsList;
 hidden1.value = optionsList;
 
 optionsList = '';
 for (var i=0; i<listBox2.options.length; i++)
 {
  var optionText = listBox2.options[i].text;
  var optionValue = listBox2.options[i].value;
  
  if ( optionsList.length > 0 )
   optionsList += ';';
  
  optionsList += optionText + ':' + optionValue;
 }
 //document.getElementById('<%= hiddenCountyValues2.ClientID %>').value = optionsList;
 hidden2.value = optionsList;
}

function RegisterAndPersist()
{
   
    registerStateControls();    
    persistOptionsList(availableStateList, selectedStateList, hiddenState1, hiddenState2);
    
    registerCityControls();     
    persistOptionsList(availableCityList, selectedCityList, hiddenCity1, hiddenCity2);
    
    registerMSAControls();    
    persistOptionsList(availableMSAList, selectedMSAList, hiddenMSA1, hiddenMSA2);
    
    registerCountyControls();      
    persistOptionsList(availableCountyList, selectedCountyList, hiddenCounty1, hiddenCounty2);
    
    registerTitleControls();  
    persistOptionsList(availableTitleList, selectedTitleList, hiddenTitle1, hiddenTitle2);
    
    registerSpecialtyControls();   
    persistOptionsList(availableSpecialtyList, selectedSpecialtyList, hiddenSpecialty1, hiddenSpecialty2);
}

