//Copyright - 2000-2007 Alfredo J G A Borba. Todos os Direitos Reservados


/* Classe manipulação de URL Query (Função Construtora)
* Utiliza recursos da versão ECMA v3 (JavaScript 1.5 - JScript 5.5) -> Versões do IE inferiores ao 5.5 não são suportadas
* @param arg_url: URL a ser manipulada (URL com ou sem caracteres de escape)
*/
function UrlQueryWbc(arg_url) {	
	this.argUrl = (arg_url)? arg_url : "";
	this.stgURL;
	this.isURL;
	this.hasQuery;
	this.queryParam;
	this.urlQuery = this.setUrl(arg_url);
}


/* Métodos de instância:
*
* @return: this.UrlQuery (e atribui valores para todas as propriedades da instância)
*/
UrlQueryWbc.prototype.setUrl = function(arg_url){	
	this.argUrl = (arg_url)? arg_url : "";
	this.stgURL = (unescape(arg_url)).replace(/\s/g, "");
	if (IsURL(this.stgURL)) {
		this.isURL = true;	
	}
	else {
		throw new Error("WbC: URL inv" + unescape("\u00E1") + "lida");
	}
	this.hasQuery = false;
	this.queryParam = null;
	this.queryParam = new Object();
	this.urlQuery = "";		
	if (HasQuery(this.stgURL)) {		
		this.hasQuery = true;	
		var stgURL_prov = "";
		var queryComplete = true;
		if ((this.stgURL).indexOf("&km=")==-1) {
			stgURL_prov = this.stgURL;
		}
		else {
			stgURL_prov = (this.stgURL).slice(0, (this.stgURL).indexOf("&km="));
			queryComplete = false;
		}		
		this.urlQuery = stgURL_prov.slice(stgURL_prov.lastIndexOf("?")+1);
		var paramArray = this.urlQuery.split("&");
		for (var i = 0; i < paramArray.length; i++) {
			var nome = "" + paramArray[i];
			nome = nome.slice(0, nome.indexOf("="));
			var valor = "" + paramArray[i];
			valor = valor.slice(valor.indexOf("=")+1);
			this.queryParam[nome] = valor;
		}		
		if (!queryComplete) {
			this.queryParam["km"] = (this.stgURL).slice((this.stgURL).indexOf("&km=")+4);			
			this.urlQuery += (this.stgURL).slice((this.stgURL).indexOf("&km="));
		}			
	}	
	return this.urlQuery;
	
	//Funções auxiliares
	function IsURL(arg) {		
		if ((arg.indexOf("http://")==-1 || arg.indexOf(".")==-1) && arg.indexOf("file://")==-1) {
			return false;
		}			
		return true;
	}
	
	function HasQuery(arg) {		
		if (arg.indexOf("?")==-1 && arg.indexOf("=")==-1) {
			return false;
		}			
		return true;
	}
}


// @return:  URL com caracteres de escape
UrlQueryWbc.prototype.getUrlEscape = function(){return escape(this.argUrl);}


/* @return: URL sem caracteres de escape (String)
*/
UrlQueryWbc.prototype.getUrlUnescape = function(){return unescape(this.argUrl);}


/* @return: URL sem caracteres de escape e sem espaços em branco (String)
*/
UrlQueryWbc.prototype.toString = function(){return this.stgURL;}


/* @return: URL Query (String)
*/
UrlQueryWbc.prototype.getUrlQuery = function(){return this.urlQuery;}
