// NOTE: duplicate functions in /asp/argUtilsServer.js
//
// Return argument string from href
// e.g. args= getArgs(location.href);
function getArgs(theHref)
{
	var args= "";
	var tmp= "";
	var strHref = String(theHref);

	if((tmp= strHref.indexOf("?")) == -1)
		args = "";
	else
		args = strHref.substring(tmp + 1);

	return args;
}

// targeted value returned from a passed url 
// with name=value pairs, returns null string if none found
function getArgValue(url, passedArg)
{
    var value = "";
    var arg = passedArg + "=";
    var argStart = url.indexOf(arg);
    if (argStart != -1) 
    {
        var remainder = url.substring(argStart + arg.length, url.length);
        var ampIndex = remainder.indexOf("&");
        if (ampIndex != -1)
            value = remainder.substring(0, ampIndex);  
        else
            value = remainder;
    }
        
    return value; 
}

// removeArgValue removes a targeted name=value pair given 
// a passed url and name then returns new url
// covers each of 4 possible cases - only: one pair, last: last pair, 
//    middle: any middle of 3 or more, first: first of 2 or more
// for only and last, the prior char is removed (? or &)
// for middle and first, the following "&" is removed
// Note: .replace() not compatible with 3.0 browsers

function removeArgValue(url, arg)
{
	if(url == "" || arg == "") return "";

    var pair;
	var newUrl= url;
    var remainder = "";
    var tempArg = arg + "=";
    var argStart = url.indexOf(tempArg);
    if (argStart != -1) 
    {
        remainder = url.substring(argStart, url.length);
        var ampIndex = remainder.indexOf("&");
        if (ampIndex != -1)
            pair = remainder.substring(0, ampIndex + 1); // case "middle" and "first"
              
        else
            pair = url.substring(argStart - 1, url.length); // case "only" and "last"
            
        newUrl = url.replace(pair, ""); 
    }
        
    return newUrl; 
}

// updateArgValue updates a target name=value pair 
// or appends it to the arg list if not found
// can be called with full href or just arg list including quest mark leader

function updateArgValue(argList, arg, value)
{
    var newArg = removeArgValue(argList, arg);

    if(newArg.indexOf("?") == -1)
        newArg = newArg + "?" + arg + "=" + value; // case only pair 
    else
        newArg = newArg + "&" + arg + "=" + value; // case append pair to end

    return newArg; 
}

// For certain situations we need a clean
// set of top level args (rand etc) 
function getGenericArgs()
{
	var args= getArgs(top.location.href);
	args= removeArgValue(args, "lid");		// linked user taxiid
	args= removeArgValue(args, "ls");		// linked user search
	args= removeArgValue(args, "taxiid");
	args= removeArgValue(args, "search");	
	args= removeArgValue(args, "related");	// video related images
	args= removeArgValue(args, "vdisplay");	// video display
	args= removeArgValue(args, "idisplay");	// image display
	args= removeArgValue(args, "imageId");	
	args= removeArgValue(args, "more");		// image "more" 
	args= removeArgValue(args, "uc");		// User Code
	args= removeArgValue(args, "un");		// User Name
	args= removeArgValue(args, "area");	
	
	//FOO: RW New
	args= removeArgValue(args, "base");

	return args;
}

