// JavaScript Document


function urlVars()
{
	var urlParams = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	var attributes = new Array();
	var values = new Array();
	
	if (window.location.href.indexOf('?') != -1)
	{
		for(var i = 0; i < urlParams.length; i++)
		{
			var hash = urlParams[i].split('=');
			attributes.push(hash[0]);
			values.push(hash[1]);
		}
	}
		
	this.hasStuff = function()
	{
		if (attributes.length != 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	this.checkForAttribute = function(theAttribute)
	{
		for (i=0; i<attributes.length; i++)
		{
			if (attributes[i] == theAttribute)
			{
				return true;
				break;
			}
		}
		
		return false;
	}
	
	this.howManyPairs = function()
	{
		return attributes.length;
	}
	
	this.listAllPairs = function()
	{	
		var tempString = new String();
		for (i=0; i<attributes.length; i++)
		{
			tempString += attributes[i] + '=' + values[i];
			if (i < attributes.length - 1)
			{
				tempString += '&';
			}
		}
		return tempString;
	}
	
	this.getValueOf = function(theAttribute)
	{
		for (i=0; i<attributes.length; i++)
		{
			if (attributes[i] == theAttribute)
			{
				return values[i];
				break;
			}
		}
	}
	
	this.getValueByIndex = function(theIndex)
	{
		return values[theIndex];
	}
	
	this.getAttributeByIndex = function(theIndex)
	{
		return attributes[theIndex];
	}
	
	this.setValue = function(theAttribute, newValue)
	{
		var valueHasBeenSet = false;
		for (var i=0; i<attributes.length; i++)
		{
			if (attributes[i] == theAttribute)
			{
				values[i] = newValue;
				valueHasBeenSet = true;
			}
		}
		if (!valueHasBeenSet)
		{
			
			attributes.push(theAttribute);
			values.push(newValue);
		}
	}
	
	this.removePair = function(theAttribute)
	{
		//alert('removing: ' + theAttribute);
		for (i=0; i<attributes.length; i++)
		{
			if (attributes[i] == theAttribute)
			{
				attributes.splice(i, 1);
				values.splice(i, 1);
			}
		}
	}
	
	this.removePairByIndex = function(theIndex)
	{
		attributes.splice(theIndex, 1);
		values.splice(theIndex, 1);
	}
	
}


//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function reloadPageWithURLParams(attribute, value)
{
	//alert('reloadPageWithURLParams(' + attribute +', '+ value +')');
	//alert('reloadPageWithURLParams()');
	URLvarList = new urlVars();
	//alert ('before: ' + URLvarList.listAllPairs());
	URLvarList.setValue(attribute, value);
	//alert ('after: ' + URLvarList.listAllPairs());
	
	// append new URL params onto targetURL
	targetURL += ('?' + URLvarList.listAllPairs());
	
	// replace each space with a plus
	targetURL = spaceToPlus(targetURL);
	
	window.location.href = targetURL;
}//reloadPageWithURLParams()


//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function reloadPageWithURLParamsX(attribute, value, theURLvars, theModule, theTargetURL)	// theModule & theTargetURL are optional
{
	//alert('reloadPageWithURLParams(' + attribute +', '+ value +', '+ theURLvars +')');
	//alert('reloadPageWithURLParams()');
	
	var noParams = false; // set to true to pass no URL params and avoid adding the '?'
	
	if (attribute == "Nothing")
	{
		noParams = true;
	}
	else
	{
		theURLvars.setValue(attribute, value);
	}
	
		
	var thePage = new String;	// gets added to the end of targetURL when needed
	
	if (theTargetURL)
	{
		if (targetURL)
		{
			targetURL = theTargetURL;
		}
		
		switch (theModule)
		{
			case 'SeasonStats' :
			{
				thePage = 'Statistics/Season-Stats/Statistics.aspx';
				break;
			}
			case 'DriverAverages' :
			{
				thePage = 'Statistics/Driver-Averages/Averages.aspx';
				break;
			}
			case 'DriverList' :
			{
				thePage = 'Drivers.aspx';
				break;
			}
			case 'RaceByRace' :
			{
				//thePage = 'Statistics/Race-By-Race.aspx';
				thePage='';
				break;
			}
			case 'Schedule' :
			{
				thePage = 'Schedule.aspx';
				break;
			}
			case 'Standings' :
			{
				thePage = 'Standings.aspx';
				break;
			}
			default :
			{
				alert('a menu in the ' + theModule + ' module needs work.\nIf this message persists, tell Mark to add a case for it in \'urlVars.js\'.');
			}
		}
		targetURL += thePage;
	}
	
	//alert ('URLvars = ' + theURLvars.listAllPairs());
	
	if (!noParams)
	{
		// append new URL params onto targetURL
		targetURL += ('?' + theURLvars.listAllPairs());
	}
	
	//alert('targetURL = ' + targetURL);
	
	// replace each space with a plus
	targetURL = spaceToPlus(targetURL);

	window.location.href = targetURL;
}//reloadPageWithURLParams()

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
