// JavaScript Document
/*
        This function returns an object with 4 properties
        Ussage:
        var urlParts = parseURL();
        urlParts.domain = the domain of the url with no trailing slash 'http://www.domain.com'
        urlParts.pathFile = Everthing after the domain in the url minuse any anchors or arguments.
        urlParts.anchor = Any anchor if any
        urlParts.mainDir = The first directory in the URL if any.
*/
function parseURL() {
        var urlParts = new Object();
        // extract the domain name
		urlParts['fullURL'] = document.URL;
        urlParts['domain'] = document.URL.substr(0, document.URL.indexOf('/',7));
        // extract the path and file name
        urlParts['pathFile'] = document.URL.substr(document.URL.indexOf('/',7));

        // Handle any '#' or '?' in the pathPlus. They have to be removed for the on-state detection to work.
        if (urlParts['pathFile'].indexOf('#') != -1) { // Strip off any anchor and store it in the object.
                var pathPlusSplit = urlParts['pathFile'].split('#');
                urlParts['pathFile'] = pathPlusSplit[0];
                urlParts['anchor'] = pathPlusSplit[1];
        } else if (urlParts['pathFile'].indexOf('?') != -1) { // Strip off any arguments and discard, argument parsing will be handled by the getArgs function
                var pathPlusSplit = urlParts['pathFile'].split('?');
                urlParts['pathFile'] = pathPlusSplit[0];
        }

        //alert(url);
        // Extract the first directory of the URL
        // This is used to activate the main button on states
        // Extract everthing up to but not including the 2nd '/'
		var pathParts = urlParts['pathFile'].split('/');
		urlParts['mainDir'] = pathParts[1];
        return urlParts;
}

/*
 * This function parses comma-separated name=value
 * argument pairs from the query string of the URL.
 * It stores the name=value pairs in
 * properties of an object and returns that object.
 * useage:
 * var args = getArgs();
 * arg1 = args.argname;
 */
function getArgs() {
    var args = new Object(  );
    var query = location.search.substring(1);     
      // Get query string
    var pairs = query.split("&");
     // Break at comma
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
          // Look for "name=value"
        if (pos == -1) continue;
          // If not found, skip
        var argname = pairs[i].substring(0,pos);
          // Extract the name
        var value = pairs[i].substring(pos+1);
          // Extract the value
        args[argname] = unescape(value);
         // Store as a property
       // In JavaScript 1.5, use decodeURIComponent(  )
       // instead of escape(  )
    }
    return args;     // Return the object
}
