/**
 * Utilidades para o sistema "Onde Correr" da Webventure.com.br
 *
 * @author Felipe Ferreri Tonello <felipetonello.com>
 * @version 0.1
 */

/**
 * Classe de utilitários
 */
function utils(){
}

/**
 * Retorna o valor de um parâmetro passado por query string pela url.
 * @param string parametro procurado
 * @param string local para ser procurado. Exemplo: location.href
 * @return o valor do respectivo parâmetro. Em caso de falha, retorna null.
 */
utils.getParametro = function(sParam, sLocal){
	var oParam = new String(sParam)
	var oLocal = new String(sLocal);
	
	oLocal = oLocal.toLowerCase();
	
	if (oLocal.indexOf('?') > -1) {
		var aParam = oLocal.split('?');
		
		if (aParam[1].length > 0) {
			var allParams = aParam[1];
			var paramArray = allParams.split("&");
			
			for (i = 0; i < paramArray.length; i++){
				var origCaseFullParam = paramArray[i];
				var nameValuePairObj = new String(paramArray[i]);
				nameValuePairObj = nameValuePairObj.toLowerCase();
				var nameValuePairArr = nameValuePairObj.split('=');
				var indParamName = nameValuePairArr[0];
				var indParamValue = nameValuePairArr[1];
				
				if (oParam.toLowerCase() == indParamName) {
					return unescape(origCaseFullParam.substr(indParamName.length + 1))
				}
			}
		}
	}
	return null;
}

/**
 * método cross-browsing para retornar um elemento
 * @param string nome do id do element
 * @return o elemento
 */
utils.getElemento = function(sId){
	return (document.getElementById) ? document.getElementById(sId)
	: eval("document.all['" + sId + "']");
}

/**
 * Retorna se o browser do usuário é Internet Explorer ou não
 * @return true se for o IE. false se não for
 */
utils.isIE = function(){
	return !(navigator && navigator.userAgent.toLowerCase().indexOf("msie") == -1);
}

/**
 * Tem mesma funcao do addslashes no php
 * Escapa ',",\,\0
 * @param String texto respectivo
 * @return texto "escapado"
 */
utils.addslashes = function(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
}

/**
 * Tem mesma funcao do stripslashes no php
 * "Desescapa" \',\",\\,\0
 * @param String texto respectivo
 * @return texto "desescapado"
 */
utils.stripslashes = function(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}
