/*
	Name of library:		functions_URL.js
	Version:				0.2
	Created on:				2005/07/06
	Created by:				Jth
	Last modified on:		2005/08*25
	Last modified by:		Jth
*/

/*	URL_getvars function v0.1
	From a given URL, it returns an array with all the URL variables found, if any, or an empty array if there are no GET variables in the URL.
	It the URL is not given, it takes the variables from the URL of the current window.
	It returns:
		If no GET variables are found = an empty array.
		If there are GET variables = an array where every element goes like = ARRAY [GET VARIABLE NAME] = GET VARIABLE VALUE.
	Ex.:
		a = URL_getvars ();
		if (a["ads"])
		{
			alert ("a[\"ads\"] = " + a["ads"]);
		}
		if (a["otro"])
		{
			alert ("a[\"otro\"] = " + a["otro"]);
		}
*/
	function URL_getvars (theURL)
	{
		var to_return = new Array ();
		var qm_pos;
		var theURL_vars;
		var theURL_vars_array = new Array ();
		var i;
		var tmp_vars;

		if (!theURL)
		{
			theURL = window.location.href;
		}
		qm_pos = theURL.indexOf ("?");
		theURL_len = theURL.length;
		if (qm_pos === -1 || qm_pos === (theURL_len - 1)) return to_return;
		theURL_vars = theURL.substring (qm_pos + 1, theURL_len);
		theURL_vars_array = theURL_vars.split ("&");

		for (i=0; i<theURL_vars_array.length; i++)
		{
			tmp_vars = theURL_vars_array[i].split ("=");
			to_return[tmp_vars[0]] = tmp_vars[1] ? tmp_vars[1] : "";
		}

		return to_return;
	}
/*	end of URL_getvars	*/

/*	URL_parse function v0.1
	If parses and returns the following components of a URL in an array:
		URL
		protocol
		query_string
		domain_name
		path
		folder
		filename
	Ex.:
		the_URL = URL_parse ();
		alert ("URL = *" + the_URL["URL"] + "*");
		alert ("protocol = *" + the_URL["protocol"] + "*");
		alert ("query_string = *" + the_URL["query_string"] + "*");
		alert ("domain_name = *" + the_URL["domain_name"] + "*");
		alert ("path = *" + the_URL["path"] + "*");
		alert ("filename = *" + the_URL["filename"] + "*");
		alert ("folder = *" + the_URL["folder"] + "*");
*/
	function URL_parse (theURL)
	{
		var to_return = new Array ();
		var tmp_array = new Array ();
		var tmp_string = "";
	
		if (!theURL)
		{
			theURL = window.location.href;
		}
	
		tmp_string = theURL;
		to_return["URL"] = theURL;
		to_return["protocol"] = "";
		to_return["query_string"] = "";
		to_return["domain_name"] = "";
		to_return["path"] = "";
		to_return["folder"] = "";
		to_return["filename"] = "";
	
		if (tmp_string.indexOf ("://") !== -1)
		{
			tmp_array = tmp_string.split ("://");
			to_return["protocol"] = tmp_array[0];
			tmp_string = tmp_array[1];
		}
	
		if (tmp_string.indexOf ("?") !== -1)
		{
			tmp_array = tmp_string.split ("?");
			to_return["query_string"] = tmp_array[1];
			tmp_string = tmp_array[0];
		}
	
		to_return["path"] = "/";
		if (tmp_string.indexOf ("/") !== -1)
		{
			tmp_array = tmp_string.split ("/");
			to_return["domain_name"] = tmp_array[0];
			to_return["path"] = tmp_string.substring ((to_return["domain_name"].length));
		}
	
		tmp_array = to_return["path"].split ("/");
		to_return["filename"] = tmp_array[tmp_array.length - 1];
		to_return["folder"] = to_return["path"].substring (0, to_return["path"].length - to_return["filename"].length);
	
		return to_return;
	}
/*	end of URL_parse	*/

/*	URL_addvars function v0.1
	From a given URL, it returns the same URL with the new GET variables added.
	If the URL is not given, it takes the variables from the URL of the current window.
	If the params are not given, it returns the URL ready for the new vars to be added (ending with a ? or a &)
	Ex.:
		theURL = "http://www.myhosting.com";
		theParams = "name=theName&lastname=theLastName";
		newURL = URL_addvars (theURL, theParams);

		Result:
		the value of the variable newURL would be = "http://www.myhosting.com?name=theName&lastname=theLastName"
*/
	function URL_addvars (theURL, theParams)
	{
		var to_return = "";
	
		if (!theURL) { theURL = window.location.href; }
		if (!theParams) { theParams = ""; }
	
		to_return = theURL;
		if (theURL.indexOf ("?") == -1)
		{
			to_return += "?" + theParams;
		}
		else
		{
			if (theURL.indexOf ("?") == (theURL.length - 1) || (theURL.indexOf ("&") == (theURL.length - 1)))
			{
				to_return += theParams;
			}
			else
			{
				to_return += "&" + theParams;
			}
		}
		return to_return;
	}
/*	end of URL_addvars	*/
