function getURL(uri) {
        uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
        uri.dom = uri.dir; 

	if (uri.dom.substr(0,7) == 'http:\/\/'){ 
		uri.dom = uri.dom.substr(7); 
	} 
	else if (uri.dom.substr(0,8) == 'https:\/\/'){ 
		uri.dom = uri.dom.substr(8); 
	} 

        uri.path = ''; 
	var pos = uri.dom.indexOf('\/'); 

	if (pos > -1) {
		uri.path = uri.dom.substr(pos+1); 
		uri.dom = uri.dom.substr(0,pos);
	}

        uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
        pos = uri.page.indexOf('?');

	if (pos > -1) {
		uri.page = uri.page.substring(0, pos);
	}

        pos = uri.page.indexOf('#');
	if (pos > -1) {
		uri.page = uri.page.substring(0, pos);
	}

        uri.ext = ''; 
	pos = uri.page.indexOf('.');

	if (pos > -1) {
		uri.ext = uri.page.substring(pos+1); 
		uri.page = uri.page.substr(0,pos);
	}

        uri.file = uri.page;
        if (uri.ext != '') 
		uri.file += '.' + uri.ext;
        if (uri.file == '')
		uri.page = 'index';

        uri.args = location.search.substr(1).split("?");

        return uri;
}

function langSwitch(lang) {
        var uri = new Object();
        var uri = getURL(uri);

		var prtcl;
		if (uri.dir.substr(0,7) == 'http://') 
			prtcl='http://';
		else if (uri.dir.substr(0,8) == 'https://')
			prtcl='https://';

		//var path = '/' + lang + uri.path.substring(uri.path.indexOf('\/')) + '/' + uri.file;
	
		//alert(uri.path);

		str = new String(uri.path)

        str = str.split("/")

		//document.write(str[0]+"<br/>")
        //document.write(str[1]+"<br/>")
		//alert(lang);
		//alert(str.length);
		
		var path = '';
		if (lang == 'chinese' || lang == 'zht'){

			for(i=0; i<str.length; i++) {
				if(str[i]!='en'){
			   		if (i==0){
				   		path += '/' + str[0] + '/' + lang;
			   		}
			   		else{
						path += '/' + str[i];
			   		}
				} 
			}
			path += '/' + uri.file;
			/* Original coding
			if (typeof(str[1]) == 'undefined') {		
				var path = '/' + str[0] + '/' + lang + '/' +  uri.file;
				//alert(str[1]);
			}
			else if (typeof(str[2]) == 'undefined'){
				var path = '/' + str[0] + '/' + lang + '/' + str[1] + '/' +  uri.file;
				//alert(str[1]);
			}
			else if (typeof(str[3]) == 'undefined'){
				var path = '/' + str[0] + '/' + lang + '/' + str[1] + '/' + str[2] + '/' + uri.file;
			}
			else if (typeof(str[4]) == 'undefined'){
				var path = '/' + str[0] + '/' + lang + '/' + str[1] + '/' + str[2] + '/' + str[3] + '/' + uri.file;
			}
			else if (typeof(str[5]) == 'undefined'){
				var path = '/' + str[0] + '/' + lang + '/' + str[1] + '/' + str[2] + '/' + str[3] + '/' + str[4] + '/' + uri.file;
			}*/
		}
		else if(lang == 'en'){
			for(i=0; i<str.length; i++) {
			   if (i==0){
				   path += '/' + str[0] + '/' + lang;
			   }
			   else if(i==1 && typeof(str[2]) != 'undefined'){
				   i++;
				   path += '/' + str[i];
			   }
			   else if(typeof(str[2]) != 'undefined'){
					path += '/' + str[i];
			   }
			} 
			path += '/' + uri.file;
		}
		else{
			for(i=0; i<str.length; i++) {
				if (i!=1)
					path += '/' + str[i];
					
			} 
			path += '/' + uri.file;
			/* Original coding
			if (typeof(str[1]) == 'undefined') {		
				var path = '/' + str[0] + '/' + uri.file;
				//alert(str[1]);
			}
			else if (typeof(str[2]) == 'undefined'){
				var path = '/' + str[0] + '/' +  uri.file;
				//alert(str[1]);
			}
			else if (typeof(str[3]) == 'undefined'){
				var path = '/' + str[0] + '/' + str[2] + '/' + uri.file;
			}
			else if (typeof(str[4]) == 'undefined'){
				var path = '/' + str[0] + '/' + str[2] + '/' + str[3] + '/' + uri.file;
			}
			else if (typeof(str[5]) == 'undefined'){
				var path = '/' + str[0] + '/' + str[2] + '/' + str[3] + '/' + str[4] + '/' + uri.file;
			}*/
		}
		window.location= prtcl+uri.dom+path;
}
