// JavaScript Document
/*-----------------------------------------------*\
| Este arquivo implementa funcionalidades gerais, |
| como tratamento de strings etc...   
\*-----------------------------------------------*/

/*### APRIMORAMENTOS DA CLASSE Sring ###*/ //->>

String.prototype.trim = function(){
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

String.prototype.lpad = function(pSize, pCharPad){
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
} //String.lpad

String.prototype.getIndexOf = function(str){
	for(var i = 0; i < this.length; i++){
		if(this.charAt(i) == str)
			return i;
	}
	
	return -1;
}

String.prototype.trChar = function(trueColection, newCollection){
	var auxIndex = -1;
	var auxResult = "";
	for(var i = 0; i < this.length; i++){
		auxIndex = trueColection.getIndexOf(this.charAt(i));
		if( auxIndex >= 0 ){
			auxResult += newCollection.charAt(auxIndex);	
		} else {
			auxResult += this.charAt(i);
		}
	}
	return auxResult;	
}

/*### APRIMORAMENTOS DA CLASSE Sring ###*/ //<<-


/*Controlador de coockies*/
var S_COOKIE = {
	date: new Date(),
	
	set: function(name, content, days) {
		var expires = "";
		if(days) {
			this.date.setTime(this.date.getTime()+(days*24*60*60*1000));
			expires = this.date.toGMTString() + "; ";
		}
		document.cookie = name + "=" + content + "; " + expires + "path=/";
		return true;
	},
	get: function( name ) {
		var nameE = name + "=";
		var cookies = document.cookie.split(";");
		for(var i = 0, Cookie; Cookie = cookies[i]; i++) {
			while(Cookie.charAt(0) == " ") {
				Cookie = Cookie.substring(1,Cookie.length);
			}
			if(Cookie.indexOf(nameE) == 0) {
				return Cookie.substring(nameE.length,Cookie.length);
			}
		}
		return false;
	},
	unset: function( name ) {
		this.set(name, "", -1);
		return true;
	}
	
}

/*  Adiciona uma Classe de estilo a um elemento
	@parâmetros
		cn:string - Nome da classe a ser adicionanda ao elemento
		el:object - Elemento que sofrerá a mudança
*/
function addClassProperty(cn , el){
	var elClass = el.className;
	
	if(elClass.lastIndexOf(cn) < 0){
		elClass += " " + cn;
	}
	
	el.className = elClass;
}

/*  Remove uma Classe de estilo a um elemento
	@parâmetros
		cn:string - Nome da classe a ser removida do elemento
		el:object - Elemento que sofrerá a mudança
*/
function removeClassProperty(cn , el){
	var elClass = el.className;
	var auxClass = "";
	
	auxClass = voxStrReplace(cn , '' , elClass);
	auxClass = voxStrReplace('  ' , ' ' , auxClass);
	
	el.className = auxClass.trim();
}


function voxStrReplace(subStr, newStr, str){
	var auxResult = '';
	for(i=0; i<str.length; i++){
		if(str.substr(i, subStr.length) == subStr){
			auxResult += newStr;
			i += subStr.length-1;
		} else {
			auxResult += str.charAt(i);
		}
	}
	return auxResult;
}