$(document).ready(function() {
	var theFullUrl = location.href;
	/* Start quick logic to test whether we're in the local staging environment or the live one. */
	var enviroTest = theFullUrl.indexOf(".com/");
		if (enviroTest != -1) {
			var theScrap = theFullUrl.indexOf(".com/") + 14;
		} else {
			var theScrap = theFullUrl.indexOf(".org/") + 14;
		}
	/* Start logic to test whether we're inside a chapter section or not. The variable 'prevChapTest' is used to check for the presence of a query. */
	var prevChapTest = theFullUrl.indexOf("?");
	if (prevChapTest != -1) {
		//We're on a search results page, meaning we're NOT within a Chapter, but our search might have come from one.
		//So we need to be certain that we're only testing against the URL up to the '?' for the presence of the word 'chapters'.
		var testableHref = theFullUrl.substr(0,prevChapTest);
		var chapterTest = testableHref.indexOf("chapters");
	} else {
		//We're NOT on a search results page, so we can safely test against the entire URL for the presence of the word 'chapters'.
		var chapterTest = theFullUrl.indexOf("chapters");
	}
	if (chapterTest != -1) {
		//alert("You are within a chapter.");
		var theUsefulPart = theFullUrl.substr(theScrap,3);
		$('#chapterValue').val('www.nationalmssociety.org/chapters/' + theUsefulPart + '');
		$('#advancedSearch').css('display','block');
		
	} else {
		//alert("You are not within a chapter.");
		$('#sitesearch').css('margin-bottom','5px');
		$('#advancedSearch').remove();
		$('#chapterValue').remove();
	}
	/* Start functions for the global radio buttons. */
	$("#thisChapter").change(function() {
		//alert("You clicked chapter.");
		$('#chapterValue').val('www.nationalmssociety.org/chapters/' + theUsefulPart + '');
	});
	$("#wholesite").change(function() {
		//alert("You clicked whole site.");
		$('#chapterValue').val('');
	});
	
	/* Start functions for the advanced search page radio buttons. */
	/* Can probably be deleted. Just left for safety's sake. */
	$("#thisChapter2").change(function() {
		//alert("You clicked chapter.");
		$('#chapterDrop').removeAttr('disabled');
	});
	$("#wholesite2").change(function() {
		//alert("You clicked whole site.");
		$('#chapterDrop').attr('disabled','disabled');
		$('#chapterDrop').val('');
	});
});

