function CreateContactSearchPanel(show_cms, website, agency_types, divisions)
{
	var SearchPanelItems = [], jurinst = GetjurinstData();

	/**************************************************************************************
    ****************************       BUTTON HANDLER     *********************************
    **************************************************************************************/
    contactBtnHandler = function(b,e)
    {
        // Check All Checkbox if all options are selected
    	var fs = ContactSearchPanel.find("name", "state_fs");
        checkForAll(fs[0]);
        
        checkForAll(Ext.getCmp("role_type_fs"));
        checkForAll(Ext.getCmp("division_type_fs"));
        
        fs = ContactSearchPanel.find("name", "agency_type_fs");
        checkForAll(fs[0]);
        
        var form = ContactSearchPanel.getForm();
        var values = form.getValues();
        var keywords = ContactSearchPanel.find("name", "keywords");
        
        if (values.keywords == keywords[0].emptyText)
        {
            values.keywords = '';
        }
        
        /*
        if(downloadBox && downloadBox.isChecked())
        {
        	values.tofile = 1;
        }
        */
        
        var url = "/search/contacts?";
        for (var i in values)
        {
            url += i + "=" + values[i] + "&";
        }
                
        location.href = url;
    }
    
    contactResetHandler = function(b,e)
    {
        var checkboxes = ContactSearchPanel.findByType(Er.CheckBoxField);
        var text_fields = ContactSearchPanel.findByType(Er.TextField);
        var radios = ContactSearchPanel.findByType(Er.RadioButtonField);
        
        for (var i = 0; i < checkboxes.length; i++)
        {
            checkboxes[i].setValue((checkboxes[i].name != 'jurinst'));
        }
        
        for (var i = 0; i < text_fields.length; i++)
        {
            text_fields[i].setValue('');
        }

        for (var i = 0; i < radios.length; i++)
        {
            radios[i].setValue((radios[i].inputValue == 'published'));
        }
    }
    
    var keywordField = GetKeywordField();
    
    keywordField.on("keypress", function(f, e)
    {
        if (e.keyCode == Ext.EventObject.ENTER)
        {
            contactBtnHandler();
        }
    });
    
    TopSection = new Er.Panel(
    {
        layout:'column',
        bodyStyle:'margin:10px 0;',
        items:[
        {
            style: 'margin-right:10px;',
            border:false,
            defaults: {border:false,width: 100},
            items: [keywordField]
        },
        {   
            border:false,
            style: 'margin-right:5px;',
            defaults:{border:false},
            items: [GetSearchBtn('contactBtnHandler')]
        },
        {
            border:false,
            defaults:{border:false},
            items:[GetResetBtn('contactResetHandler')]
        }]
    })
    
    SearchPanelItems.push(TopSection);
      
    
    if (show_cms)
    {
       	var options  = GetPublishField();
    	options.unshift(GetjurinstField());
    	
    	var download = new Er.CheckBoxField({name:'download', boxLabel:'<b>Download as .CSV file</b>'}) 
    	options.unshift(download);
    	
    	
        var cbFlag = new Er.ComboBoxField({ emptyText:'Choose a Contact Flag', width:180, hiddenName: 'flag', 
        	 								 store:[['CF_CFO','CFO'] ]   });
 //       	                                 store:[['CF_CFO','CFO'],['CF_TEST', 'Test Flag'] ]   });
    	options.push(cbFlag);
    	
    	SearchPanelItems.push(new Er.Panel({items:options}));
    }
    
	
	SearchPanelItems.push(GetRoleTypesField());
	
	if (show_cms);
	{
        SearchPanelItems.push(GetDivisionTypesField(divisions));
	}

    //################### STATE FS #####################################################
    var Items = GetStatesField();
    
    Items.push(GetSearchBtn('contactBtnHandler'));
    
    var StateFS = new Er.Panel(
    {
        name: 'state_fs', 
        title: 'States', 
        items:Items,
        collapsible:true,
        collapsed:true,
        border:false,
        defaults:{border:false},
        animCollapse: false,
        titleCollapse:true
    }); 
    
    SearchPanelItems.push(StateFS);
    
    //################### LEVEL SECTION ################################################
    var levelItems, fs_title;
    
    if (jurinst.jurinst == 'Institutions')
    {
        fs_title = "Education Level";           
        levelItems = GetEdLevelsField();
    }
    else
    {
        fs_title = "Government Level";
        levelItems = GetGovLevelsField();
    }
    
    levelItems.push(GetSearchBtn('contactBtnHandler'));
    
    LevelSection = new Er.Panel(
    {
        id: 'level_fs', 
        title: fs_title, 
        items: levelItems, 
        collapsible:true, 
        collapsed:true,
        border:false,
        defaults:{border:false},
        animCollapse: false,
        titleCollapse:true
    });
    
    SearchPanelItems.push(LevelSection);
    
    //#################################### AGENCY TYPE FS ###################################
    var Items = GetAgencyTypesField(agency_types);
    
    Items.push(GetSearchBtn('contactBtnHandler'));
    
    var agency_type_fs = new Er.Panel(
    {
        name: 'agency_type_fs', 
        title: "Agency Types", 
        items: Items, 
        collapsible:true, 
        collapsed:true,
        border:false,
        defaults:{border:false},
        animCollapse: false,
        titleCollapse:true
    });
    
    SearchPanelItems.push(agency_type_fs);
	
    //################################### CONTACT SEARCH PANEL ##############################
    
	var ContactSearchPanel = new Er.FormPanel(
    {
//        title: 'Contacts',
        id: 'contacts',
        labelAlign: 'top',
        labelWidth:75,
        items: SearchPanelItems,
//        standardSubmit: true,
        autoHeight:true,
        method: "GET",
        border:false,
        defaults: {border: false},
        hidden:true
    });
	
	return ContactSearchPanel;
}

function GetRoleTypesField()
{
	var qString = GetQueryString();
	var RoleTypeItems = [];
	
	var allChecked = (!qString || isChecked(qString, "allStates", "1"));
	
	RoleTypeItems.push(
        new Er.CheckBoxField(
        {
            id: 'allRoleTypes', 
            inputValue: 1, 
            boxLabel: 'All', 
            checked: allChecked, 
            listeners:{check: allCheckboxHandler}
        })
    )
    
    RoleTypeItems.push(
        new Er.CheckBoxField(
        {
            name: 'role_types', 
            inputValue: "HEAD", 
            boxLabel: "Chief",
            checked: (allChecked || isChecked(qString, "role_types", "HEAD")), 
            listeners: 
            {
                check: singleAllCheckboxHandler
            }
        })
    )
    
    RoleTypeItems.push(
        new Er.CheckBoxField(
        {
            name: 'role_types', 
            inputValue: "DEPUTY", 
            boxLabel: "Deputy",
            checked: (allChecked || isChecked(qString, "role_types", "DEPUTY")), 
            listeners: 
            {
                check: singleAllCheckboxHandler
            }
        })
    )
    
    RoleTypeItems.push(
        new Er.CheckBoxField(
        {
            name: 'role_types', 
            inputValue: "SPECIALIST", 
            boxLabel: "Specialist",
            checked: (allChecked ||isChecked(qString, "role_types", "SPECIALIST")), 
            listeners: 
            {
                check: singleAllCheckboxHandler
            }
        })
    )
    
    RoleTypeItems.push(GetSearchBtn('contactBtnHandler'));
    
    var role_type_fs = new Er.Panel(
    {
    	id: 'role_type_fs',
        title: 'Role Types', 
        items: RoleTypeItems,
        collapsible: true,
        collapsed:true,
        border:false,
        defaults:{border:false},
        animCollapse: false,
        titleCollapse:true
    }); 
    
    return role_type_fs;
}

function GetDivisionTypesField(divisions)
{
	var qString = GetQueryString();
	
	var DivisionItems = [];
	var allChecked = (!qString || isChecked(qString, "allStates", "1"));
	
	DivisionItems.push(
	    new Er.CheckBoxField(
        {
            id: 'allDivisionTypes', 
            inputValue: 1, 
            boxLabel: 'All', 
            checked: allChecked, 
            listeners:{check: allCheckboxHandler}
        })
	)
	
	for (var i = 0; i < divisions.length; i++)
	{
	    DivisionItems.push(
	        new Er.CheckBoxField(
            {
                name: 'division_types', 
                inputValue: divisions[i].pk, 
                boxLabel: divisions[i].title,
                checked: (allChecked || isChecked(qString, "division_types", divisions[i].pk)), 
                listeners: 
                {
                    check: singleAllCheckboxHandler
                }
            })
        )	
	}
	
	DivisionItems.push(GetSearchBtn('contactBtnHandler'));
		
	var division_type_fs = new Er.Panel(
    {
        id: 'division_type_fs',
        title: 'Division Types', 
        items: DivisionItems,
        collapsible: true,
        collapsed:true,
        border:false,
        defaults:{border:false},
        animCollapse: false,
        titleCollapse:true
    }); 
    
    return divisions.length ? division_type_fs : false;
}
