/****************************************************************************************
Script to take the variables from a location bar and turn them into JavaScript variables
                      Written by Mark Wilton-Jones, 13/9/2001
*****************************************************************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

This is version 2 of the script, also allowing 'imageName.x=234&imageName.y=765' as you
would get with <input type="image" ...>

To use this script, simply add the lines
<script type="text/javascript" language="javascript1.2">
<!--
var useArray = false; //Or false
//-->
</script>
<script src="js-bin/get_vars.js" type="text/javascript" language="javascript1.2"></script>
into your page before any scripts that need the variables

The line 'var useArray = true;' tells the script whether or not you want it to put the
variables into an array as well as create them as variables.

eg 1.
if the URL ends with '?vara=help_me&varb=now_please&myImg.x=2&myImg.y=4' and
useArray = true, the script will create the following (note, all are string variables):
vara = 'help_me';
varb = 'now_please';
myImg.x = '2';
myImg.y = '4';
getVars['0'] = 'help_me';
getVars['1'] = 'now_please';
getVars['2'] = '2';
getVars['3'] = '4';
getVars['vara'] = 'help_me';
getVars['varb'] = 'now_please';
getVars['myImg.x'] = '2';
getVars['myImg.y'] = '4';

eg 2.
if the URL ends with '?vara=help_me&varb=now_please&myImg.x=2&myImg.y=4' and
useArray = false, the script will create the following (note, all are string variables):
vara = 'help_me';
varb = 'now_please';
myImg.x = '2';
myImg.y = '4';

_______________________________________________________________________________________*/

function locclean( locvarcl, loctrue ) {
	//convert stuff like:
	//%21%22%A3%24%25%5E%26*%28%29-_%3D%2B%7E%23%7B%7D%5B%5D%3A@%3B%27%3C%3E%3F%2C
	//into normal characters
	//I need to do these few myself because I need a different response to unescape
	var locvarar = new Array("+","%27","%5C","%0D","%0A"), varlocar = new Array(" ","\\'","\\\\","","\\n"), x;
	if( loctrue ) { locvarar.length = 1; } //If I'm not using eval(), I only need to convert the +
	for( x = 0; x < locvarar.length ; x++ ) {
		while( locvarcl.indexOf( locvarar[x] ) + 1 ) {
			locvarcl = locvarcl.replace( locvarar[x], varlocar[x] );
		}
	}
	return unescape( locvarcl );
}
//alert('here')
var locvartemp = self.location.href.replace(/#.*/,'') + '&', getVars = new Array(), locvarx;
//eg locvartemp = 'http://www.blah.com/test.html?var1=carp&var2=carp2&myvar=sugar&iNm.x=2&iNm.y=4&'
//I need that last & in there so the script knows where the end of the last variable is
if( locvartemp.indexOf( "?" ) + 1 ) {
	//there are variables, what are they?
	locvartemp = locvartemp.substr( locvartemp.indexOf( "?" ) + 1 );
	//eg locvartemp = 'var1=carp&var2=carp2&myvar=sugar&iNm.x=2&iNm.y=4&'
	for( locvarx = 0; locvartemp != ''; locvarx++ ) {
		//keep looking until you reach the end of the string
		if( locvartemp.indexOf( "." ) + 1 && locvartemp.indexOf( "." ) < locvartemp.indexOf( "=" ) ) {
			//eg iNm.x=2   Check if the object (iNm in this case) exists yet. If not create it
			if( !eval( "window." + locvartemp.substring( 0, locvartemp.indexOf( "." ) ) ) ) {
				eval( "var " + locvartemp.substring( 0, locvartemp.indexOf( "." ) ) + " = new Object()" );
				//eg var iNm = new Object()
			}
			var locOvar = "";
		} else {
			var locOvar = "var ";
		}
		eval( locOvar + locclean( locvartemp.substring( 0, locvartemp.indexOf( "&" ) ).replace( "\=", "='" ) + "'" ) );
		//eg var var1='carp' or iNm.x='5'
		useArray = false;
		if( useArray ) {
			getVars[locclean( locvartemp.substring( 0, locvartemp.indexOf( "=" ) ) )] = locclean( locvartemp.substring( locvartemp.indexOf( "=" ) + 1, locvartemp.indexOf( "&" ) ), true );
			//eg var getVars[var1]=carp (quotes are not needed as strings are in variable form)
			getVars[locvarx] = locclean( locvartemp.substring( locvartemp.indexOf( "=" ) + 1, locvartemp.indexOf( "&" ) ), true );
			//eg var getVars[0]=carp
		}
		locvartemp = locvartemp.substr( locvartemp.indexOf( "&" ) + 1 );
		//eg locvartemp = 'var2=carp2&myvar=sugar&iNm.x=2&iNm.y=4&'
	}
}
locvartemp = null; locvarx = null; //clear up