// Javascript File

// Get Query String parameters
var queryStr = location.search;
var params = new Array();
var names = new Array();

if (queryStr != "") getParams();

// Sets the browser window status bar
function setStatus(text)
{
	window.status = text
	return true
}

function loadImage(smallImage, dir) {
	document.sizeForm.Image.value = smallImage;
	document.sizeForm.dir.value = dir;
	document.sizeForm.submit();
}

function loadImage2(smallImage) {
	document.sizeForm.Image.value = smallImage;
	document.sizeForm.submit();
}


// Populate parameter arrays
function getParams()
{
	// remove leading ? from querystring and split it into chunks at the & delimiter
	var queryArray = queryStr.substr(1).split("&");

	// process each chunk
	for (i=0; i<queryArray.length; i++)
	{
		var paramName = ""
		var paramValue = ""

		// find the divider between the parameter name & value
		var mid = queryArray[i].indexOf("=");

		// if the = is not found, assume that the content of the chunk is
		// a value not a name
		paramName = unencode ( queryArray[i].substr(0,mid) );		
		paramValue = unencode ( queryArray[i].substr(mid+1) );
		
		// fill the parameter value & name arrays
		params[paramName]= paramValue;
		params[i] = paramValue;
		names[i] = paramName;
	}
}

// unencode a string
function unencode(str)
{
	var retStr = "";
	var tempStr = str;
	
	// replace each occurrence of '+' with a space
	var pos = str.indexOf("+");
	while (pos != -1)
	{
		// add string up to first '+' then replace '+' with a space
		retStr += tempStr.substring ( 0 , pos );
		retStr += " ";
		// get remainder of string and check for '+' in that
		tempStr = tempStr.substr ( pos + 1);
		pos = tempStr.indexOf ("+");
	}
	// none/no more found, so add remainder of string
	retStr += tempStr;
		
	// unencode remainder of characters
	retStr = unescape (retStr);

	return retStr;
}

// TEST:  get a parameter given its name - does not use parameter arrays
function param(name)
{
	var value="";

	var paramName = name + "=";
	var startParamName = queryStr.indexOf(paramName);

	// look for parameter name in query string
	var rgEx = new RegExp ( [ "[?&]" + paramName ] );
	var resultArray = rgEx.exec (queryStr);

	// parameter name found?
	if ( resultArray )
	{
		// get beginning of param name & its length
		var start=resultArray.index;
		var len=resultArray[0].length;

		// get param value
		var valueStr=queryStr.substr ( start + len );	// query string with the parameter name removed
		var end = valueStr.indexOf("&");				// end of parameter value
		if (end == -1) end = valueStr.length;			
		value = unescape ( valueStr.slice (0, end) );	// parameter value unencoded
	}

	return value;
}
