﻿
var w3cookies = {
	date: new Date(),
	// Cria o(s) cookie(s)
	// Forma de uso: w3cookies.create('nome_do_cookie','valor',dias_para_expirar);
	create: function(strName, strValue, intDays) {
		if ( intDays ) {
			this.date.setTime(this.date.getTime()+(intDays*24*60*60*1000));
			var expires = "; expires=" + this.date.toGMTString();
		} else {
			var expires = "";
		}
		
		var oldValue = "";
		if (w3cookies.verify(strName, strValue))
		{
		    oldValue = w3cookies.read(strName);
		    document.cookie = strName + "=" + oldValue + "," + strValue + expires + "; path=/";
		}
		else
		{
		    oldValue = w3cookies.read(strName);
		    document.cookie = strName + "=" + oldValue + expires + "; path=/";
		}
	},
	// Ler as informações de um cookie em específico
	// Forma de uso: w3cookies.read('nome_do_cookie');
	read: function(strName) {
		var strNameIgual = strName + "=";
		var arrCookies = document.cookie.split(";");
		for ( var i = 0, strCookie; strCookie = arrCookies[i]; i++ ) {
			while ( strCookie.charAt(0) == " ") {
				strCookie = strCookie.substring(1,strCookie.length);
			}
			if ( strCookie.indexOf(strNameIgual) == 0 ) {
				return strCookie.substring(strNameIgual.length,strCookie.length);
			}
		}
		return null;
	},
	// Verificar se valor ja foi inserido
	// Forma de uso: w3cookies.verify('nome_do_cookie','valor');
	verify: function(strName, strValue) {
		var strNameIgual = strName + "=";
		var arrCookies = document.cookie.split(";");
		
		for ( var i = 0, strCookie; strCookie = arrCookies[i]; i++ ) {
			while ( strCookie.charAt(0) == " ") {
				strCookie = strCookie.substring(1,strCookie.length);
			}
			if ( strCookie.indexOf(strNameIgual) == 0 ) 
			{
				var arrValores = strCookie.substring(strNameIgual.length,strCookie.length).split(",");
				for (j = 0; j < arrValores.length; j++) 
				{
			        if (arrValores[j] == strValue)
			        {
			            return false;
			        }
				}
                return true;
			}
		}
		return true;
	},
//	// Deleta um valor da string do cookie
//	// Forma de uso: w3cookies.delete_value('nome_do_cookie','valor',dias_para_expirar);
//	delete_value: function(strName, strValue, intDays) {
//		var strNameIgual = strName + "=";
//		var arrCookies = document.cookie.split(";");
//		
//		if ( intDays ) {
//			this.date.setTime(this.date.getTime()+(intDays*24*60*60*1000));
//			var expires = "; expires=" + this.date.toGMTString();
//		} else {
//			var expires = "";
//		}
//		
//		for ( var i = 0, strCookie; strCookie = arrCookies[i]; i++ ) {
//			while ( strCookie.charAt(0) == " ") {
//				strCookie = strCookie.substring(1,strCookie.length);
//			}
//			if ( strCookie.indexOf(strNameIgual) == 0 ) 
//			{
//				var arrValores = strCookie.replace("," + strValue,"");
//				
//				document.cookie = strName + "=" + arrValores + expires + "; path=/";
//			}
//		}
//	},
	// Deleta um valor da string do cookie
	// Forma de uso: w3cookies.delete_value('nome_do_cookie','valor',dias_para_expirar);
	delete_value: function(strName, strValue, intDays) {
		if ( intDays ) {
			this.date.setTime(this.date.getTime()+(intDays*24*60*60*1000));
			var expires = "; expires=" + this.date.toGMTString();
		} else {
			var expires = "";
		}
		
		var newValue = "";
	    newValue = w3cookies.read(strName);
	    newValue = newValue.replace("," + strValue,"");
	    document.cookie = strName + "=" + newValue + expires + "; path=/";
	},
	// Delete um cookie desejado
	// Forma de uso: w3cookies.erase('nome_do_cookie');
	erase: function(strName) {
		this.create(strName,"",-1);
	}
}
