// var all pages need to change to set the link properly
// this is the hard-coded path to the file you want to link from.
// make SURE this is in reference to the server name, and, like
// the path below, do NOT include the host name, just the path
//var linkPageURL = "/One1/Two/Three/toLinkto.html";
//var linkPageURL = window.location.pathname;

// this is the path to the page that will call the fcn.  you can get
// this path from document.location.pathname i believe.  it's whataver
// i had you see if it started with a '/' char or not.  in any event,
// this is the page url PATH ONLY.. don't include the hostname
//var pagePath    = "/One/Two/toLinkfrom.html";
// path we are building
//var relPath     = "";
// Valerie:  ga'head and remove if ya want.  this is for me for debug priting purposes...
var DEBUG = false;

// makeLinks : given the above two paths, it will build a relative path from
//              pagePath to linkPageURL and give you and alert() box as to the
//              outcome ( you will want to delete that... )
function makeLinks(linkPageURL, pagePath)
{
	var relPath     = ""
	// index into the path strings.  start @ first index ( 0 )
    var i = 0;
    // current directory name we are building
    var curDirName = "";
    // how many '/' we have found in case we don't find a match
    var slashCount = 0;
    // the last slash index that they have in common
    var lastSlashIndex = -1;

	// make sure the args aren't null or.  if they are, then return with "" as the path
	if( linkPageURL == null || pagePath == null )
		return "";

    // now.. loop thru each path name. when we hit a '/' character, then we know
    // we are at a new directory level.... so save the directory name we built up
    // when we find that dir names don't match btwn any two '/' chars, then the
    // previous dir name we saved is the one they have in common.  if NO dir name
    // is found in common, then just have as many '../' as we found '/'.  BAM!

    // so.. look @ the next char and compare
    while( true )
    {
        // make sure we haven't gotten past the sting path lengths. if we have then
        // we lost :(
        if( i >= linkPageURL.length || i >= pagePath.length )
        {
            // we are at then end of our strings... we be done looping
            break;
        }

        // see if we have a match
        if( linkPageURL.charAt( i ) == pagePath.charAt( i ) )
        {
            // they match!
            // see if it is a slash '/' character
            if( linkPageURL.charAt( i ) == '/' )
            {
                // increment the number of slashes found
                slashCount++;
                // set the index of this slash
                lastSlashIndex = i;
            }
            // increment to look at the next char in the string
            i++;
        }
        else
        {
            // hmm.. no match.  done looking
            break;
        }

    }  // of while( !done )

    // debug stuff...
    if( DEBUG == true )
    {
        msg =  "i: " + i + "\n";
        msg += "lastSlashIndex: " + lastSlashIndex + "\n";
        msg += "slashCount: " + slashCount + "\n";
        alert( msg );
    }

    // now... we found out at what last slash '/' index they DID have in common.
    // in other words... we know the last corresponding dirname they have, and the
    // preceeding '/' character.

    // so... get the rest of the path to the file to link TO by just grabbing ALL
    // remaining path info after the last corresponding '/'
    remainingLinkToPath = linkPageURL.substring( lastSlashIndex + 1 ); //, lastSlashIndex.length - 1 );

    // so...  find out how many MORE '/' chars there are AFTER that... and for every
    // one found, add a '../' to go up a dir level
    // start searching for the rest of the '/' AFTER the last slash index
    lastSlashIndex++;
    // ok, now for every '/' found at the lastSlashIndex and beyond, append another
    // '../' string to the relative linking string
    while( ( lastSlashIndex = pagePath.indexOf( '/', lastSlashIndex ) ) != -1 )
    {
        // found another slash...
        relPath += '../';
        // find the next slash
        lastSlashIndex++;
    }

    // append the rest of the path to the directory
    relPath += remainingLinkToPath;

    if( DEBUG == true )
    {
        alert( "Relative Path:\n" + relPath );
    }

    //alert( "Relative path to link to from:\n" + pagePath + "\nTo:\n" + linkPageURL + "\nis:\n" + relPath );

	return relPath;

}  // end of makeLinks( )
