
// 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];
	}
}

// this returns all the parameters into a 2D array.
function getUrlVars(){
	var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;

}


function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}