$(document).ready(function(){

	// Put the focus into the myapps username box
	$(".myapps-username").focus();
	
	// Make external links in the MyApps bar open in a new browser
	$("a[href^=http]").click( function() {
		if ($(this).attr("rel") == "external" ) {
			window.open(this.href);
			return false;
		}
	});

	// When the user clicks in the search box clear the text and change the class so the text is black
	$("#search-text").focus( function() {
		if ($(this).val() == "Search the intranet" ) {
			$(this).val("").addClass("search-active");
			return true;
		}
	});

	// When the user clicks the search button without entering a query clear the text and change the class so the text is black
	$("#title-search").submit( function() {
		if ($("#search-text").val() == "Search the intranet" ) {
			$("#search-text").val("").addClass("search-active").focus();
			return false;
		} // If the user submits a blank search or a search containing only spaces then highlight the searchbox and show an error. 
		else if (jQuery.trim($("#search-text").val()) === "" ){
			 $("#search-text").addClass("search-highlight").focus();
			alert("To search, please enter something in the highlighted box.");
			return false;
		}
	});
	
	// If a search has been completed 
	var q = getQueryStringValue("q");
	if (q !== "") {
		q = q.replace(/\++/, " ");
		$("#search-text").val(q).addClass("search-active");
	}
});

function getQueryStringValue( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ) {
		return ""; } 
	else {
		return results[1];
	}
}