//  Implementing URI-specific functions
//
//	See RFC 2386
//
// This is or was   http://www.w3.org/2005/10/ajaw/rdf/uri.js
// 2005 W3C open source licence
//
//
//  Take a URI given in relative or absolute form and a base
//  URI, and return an absolute URI
//
//  See also http://www.w3.org/2000/10/swap/uripath.py
//
function URIjoin(given, base) {
//    if (typeof fyi != 'undefined') fyi("   URI given="+given+" base="+base)
	var baseHash = base.indexOf('#')
	if (baseHash > 0) base = base.slice(0, baseHash)
	if (given.length==0) return base // before chopping its filename off
	if (given.indexOf('#')==0) return base + given
	var colon = given.indexOf(':')
	if (colon >= 0) return given	// Absolute URI form overrides base URI
	var baseColon = base.indexOf(':')
	if (baseColon<0) {alert("Invalid base URL "+ base); return given}
	var baseScheme = base.slice(0,baseColon+1)  // eg http:
	if (given.indexOf("//") == 0)     // Starts with //
		return baseScheme + given;
	if (base.indexOf('//', baseColon)==baseColon+1) {  // Any hostpart?
	    var baseSingle = base.indexOf("/", baseColon+3)
	    if (baseSingle < 0) {
		if (base.length-baseColon-3 > 0) {
		    return base + "/" + given
		} else {
		    return baseScheme + given
		}
	    }
	} else {
	    var baseSingle = base.indexOf("/", baseColon+1)
	    if (baseSingle < 0) {
		if (base.length-baseColon-1 > 0) {
		    return base + "/" + given
		} else {
		    return baseScheme + given
		}
	    }
	}

	if (given.indexOf('/') == 0)	// starts with / but not //
	return base.slice(0, baseSingle) + given

	var path = base.slice(baseSingle)
	var lastSlash = path.lastIndexOf("/")
	if (lastSlash <0) return baseScheme + given
	if ((lastSlash >=0) && (lastSlash < (path.length-1)))
		path = path.slice(0, lastSlash+1) // Chop trailing filename from base

	path = path + given
	while (path.match(/[^\/]*\/\.\.\//)) // must apply to result of prev
		path = path.replace( /[^\/]*\/\.\.\//, '') // ECMAscript spec 7.8.5
	path = path.replace( /\.\//g, '') // spec vague on escaping
	return base.slice(0, baseSingle) + path
}

/** returns URI without the frag **/
function uri_docpart(uri) {
    var i = uri.indexOf("#")
    if (i < 0) return uri
    return uri.slice(0,i)
} 

/** return the protocol of a uri **/
function uri_protocol(uri) {
    return uri.slice(0, uri.indexOf(':'))
} //protocol


/** returns true if has a protocol & domain **/
/* No -- this is general URI code.  Not all URIs have protocol and domain.
** Eg file:, mailto: etc

function uri_valid(uri) {
	return (uri.match(/(.+?):\/\/(.+?)/));  
} //valid


// returns the filename of a uri @fixme
function uri_filename(uri) {
	if (uri_valid(uri))
		return uri.slice(uri.lastIndexOf('/')+1);
} //filename

*/


//ends
