function ToString(str) {
	if(str == null) return "";
	else return str.toString();
}

function getRefById(id) {
	return document.getElementById ? document.getElementById(id) : document.all ? document.all[id] : null;
}

function lib_query(str) {
	this.keys = new Array();
	this.values = new Array();
	this.valueOf = {};

	this.string = str;

	this.queries = str.split("&");
	for(var i=0; i<this.queries.length; i++) {
		var pair = this.queries[i].split("=");
		var k = pair[0];
		var v = pair[1];
		this.keys[i] = k;
		this.values[i] = v;
		this.valueOf[k] = v;
	}
}

function lib_url() {
	l = window.location.toString();
	this.full = l;

	////////////// anchor //////////////////
	re = new RegExp("#.*");
	this.anchor = ToString(l.match(re));
	if(this.anchor != "")
		l = l.replace(this.anchor, "");
	//l = stringReplace(l, this.anchor, "");
	this.anchor = this.anchor.replace("#", "");

	////////////// query //////////////////
	this.queryString = ToString(l.match(/\?.*$/));
	if(this.queryString != "")
		l = l.replace(this.queryString, "");
	this.queryString = this.queryString.replace("?", "");

	this.query = new lib_query(this.queryString);
	
	////////////// protocol //////////////////
	re = new RegExp("^.*//");
	this.protocol = ToString(l.match(re));
	if(this.protocol != "")
		l = l.replace(this.protocol, "");
	this.protocol = this.protocol.replace("://", "");

	re = new RegExp("/.*");
	this.path = ToString(l.match(re));
	if(this.path != "")
		l = l.replace(this.path, "");
	
	re = new RegExp(":\d+");
	this.port = ToString(l.match(re));
	if(this.port != "")
		l = l.replace(this.port, "");
	this.port = this.port.replace(":", "");

	this.domain = l;

	// path components
	re = new RegExp("[^/]*$");
	this.file = ToString(this.path.match(re));

	re = new RegExp("[^.]*$");
	this.extension = ToString(this.file.match(re));

	this.filename = this.file.replace( "." + this.extension, "");

	if(this.file != "") {
		this.directory = this.path.replace(this.file, "");
		this.directory = this.directory.substring(0, this.directory.length-1);
	} else
		this.directory = this.path;
	
	this.parentDirectory = this.directory.replace(/.*\//,"");

}

function switchLang(lang){
	var l = url.full;
	//var l = window.location.toString();
	l = l.replace(/\/(eng|cht|chs)\//, "/" + lang + "/");
	window.location.href  = l;
	return;
}
var url = new lib_url();
var section = url.parentDirectory;