
// any JavaScript functions that manipulate URLs should be placed here

// pass in the protocol and domain and it will swap it into the _current_ location.
// you can use this to swap the current protocol and domain with a secure version.
function swapProtocolDomain(protocolDomain) {

	// no need to swap if they aren't different
	var matchingIndex = window.location.href.indexOf(protocolDomain);
	if (matchingIndex > -1) {
		return window.location.href;
	}
	
	// we'll search after the 7th char assuming "http://"
	var slashIndex = window.location.href.indexOf('/', 7);
	
	if(window.location.href.indexOf('https') > -1){
		slashIndex = window.location.href.indexOf('/', 8);
	}
	
	if (slashIndex > -1) {
		var swapOut = window.location.href.substr(0, slashIndex);
		var keep = window.location.href.substr(slashIndex);
		return protocolDomain + keep;
	} else {
		return protocolDomain;
	}
}

function gotoSwapProtocolDomain(protocolDomain){
	var resultURL = swapProtocolDomain(protocolDomain);
	
	if (window.location.href != resultURL) {
		document.location=resultURL;
	}
}

// url can be something like /front/blah.jsp?x=2
// parameter can be test=true (it must contain the value too).
// and don't preface with ? or &.
function addParameter(url, parameter) {
	if (url.indexOf('?') > -1) {
		return url + "&" + parameter;
	} else {
		return url + "?" + parameter;
	}
}

// gets a URL parameter value given the parameter name
function getURLParameter(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];
	}
}

// this takes in the href
function getURLParam(href, name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(href);
	
	if (results == null) {
		return "";
	} else {
		return results[1];
	}
}
