function filterProfiles(fullUrl) { 
	$j.get(fullUrl,function(html){ 
		if (html.indexOf("<li>") > -1)
		{
			$j('#XML').html(html); 
		}
		else
		{
			$j('#XML').html('<p class="profiles_noresults"><strong>There are currently no profiles which match your selection. You can widen the search results by choosing just a location or role.</strong></p>'); 
		}
	}); 
} 


function pageload(hash) {
	// hash doesn't contain the first # character.
	var baseUrl = "/careers_herbertsmith_com/templates/XmlProvider.aspx?dataType=Profiles";
	var queryType = GetQueryType();
	var cmsprofilespath = GetCmsProfilesPath();
	if(hash) {
		var role = hash.split(';')[0].split(':')[1];
		var location = hash.split(';')[1].split(':')[1];
		var queryString = '&';
		if (role == 'All' && (queryType == "RoleCategory" || queryType == "Role"))
		{
			role = GetRoleCategory();
		}
		queryString += 'role=' + role + '&location=' + location;
		queryString += '&queryType=both';
		
		
		queryString +='&cmsprofilespath=' +  cmsprofilespath ;
		
		baseUrl += queryString;
		filterProfiles(baseUrl);
		
	} else {
		if (queryType == "RoleCategory")
		{
			var category = GetRoleCategory();
			baseUrl += "&querytype=rolecategory&rolecategory=" + category + '&cmsprofilespath=' + cmsprofilespath;	
		}
		else if (queryType == "Role")
		{
			var role = GetRoleCategory();
			baseUrl += "&querytype=role&role=" + role + '&cmsprofilespath=' +  cmsprofilespath ;
		}
		else if (queryType == "Location")
		{
			var location = GetLocation();
			baseUrl += "&mainfiltertype=location&querytype=location&location=" + location + '&cmsprofilespath=' +  cmsprofilespath ;
		}				
		else
		{
			baseUrl += "&queryType=" + queryType + '&cmsprofilespath=' +  cmsprofilespath ;
		}		
		filterProfiles(baseUrl);
	}
}

$j(document).ready(function(){
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$j.historyInit(pageload);
	$j("select#Role").change(function() {
		var role = $j("option:selected", this).text();		
		var location = $j("option:selected", $j('select#Location')).text();
		$j.historyLoad('role:' + role + ';location:' + location);		
	});
			
	$j("select#Location").change(function() {
		var location = $j("option:selected", this).text();
		var role = $j("option:selected", $j('select#Role')).text();		
		$j.historyLoad('role:' + role + ';location:' + location);		
	});
});
