// trabalha com cookies:
function criaCookie(name,value,days) {
//alert("Valor do cookie a criar/gravar: "+value);
if(value!='') {

	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	// pega os valores atuais do cookie
	valcookies = CarregaCookie(name);
	val = ConcatenaCookie(name,value);
	//alert("valores atuais do cookie: " +valcookies+" - Veio da concat: "+val);
	if(valcookies!==null && valcookies!==undefined) {
	val = ConcatenaCookie(name,value);
	//alert("concatena cookie!!!");
	} else {
	//alert("pega valor passado!!!");
	val = value;
	}
	//alert("Valor do cookie: "+val);
	value = val;
	//alert("Valor a escrever no cookie: "+value);
	document.cookie = name+"="+value+expires+"; path=/";
} else {
//alert("nenhum valor a gravar em cookie");
}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function CarregaCookie(nome){
var x = readCookie(nome);
if (x) {
//alert(x);
return x;
}
}

function ConcatenaCookie(nome,valor){
var x = readCookie(nome);
if (x) {
var valores=x.split("|");
tam = valores.length;
var val='';
if (x.indexOf(valor)==-1){
//alert("nao achou valor, entao adiciona");
val=x+"|"+valor;
} else {
val=x;
}

} else {
val=valor;
//alert("valor para concatenação: "+valor);
}
val = val;
return val;
}

function mostraCookie(nome){
var x = readCookie(nome);
if (x) {
//alert(x);
}
}

// verifica se o valor do checkbox está no cookie (imovel selecionado no cookie)
// para selecionar o checkbox 
function VerificaCookie(nome,valor,idcheck){
var x = readCookie(nome);
if (x) {
if (x.indexOf(valor)!==-1){
document.getElementById(idcheck).checked=true;
} else {
document.getElementById(idcheck).checked=false;
}
}
}

// na listagem de imoveis apos a pesquisa ou listagem provinda da area do cliente
// verificar se os imoveis listados já estão no cookie para então marcar o checkbox
function VerificaItens() {
	var nome='imoveis'; // nome do cookie
	var vselecao=document.getElementsByName('selecao_imovel');
	//alert(vselecao.length);
	for(var i=0;i < vselecao.length;i++) {
	campovs=vselecao[i];
	idcampox=campovs.id;
	var x = readCookie(nome);
	if (x) {
		if (x.indexOf(campovs.value)!==-1){
		document.getElementById(idcampox).checked=true;
		} else {
		document.getElementById(idcampox).checked=false;
		}
	}
	}
}


function EscreveRemoveCookie(nome,valor,idcheck){

//alert(nome+" "+valor+" "+idcheck);
var valx = 0;	
var x = readCookie(nome);
//alert("x = "+x);

if (x) {
//var valores= new Array(x);
var valores=x.split("|");
valores=x;
tam = valores.length;
//alert("valores: "+valores);
var val='';


if (document.getElementById(idcheck).checked==true || idcheck=="selec"){
	//alert("está marcado!");
	if(valores.indexOf(valor)==-1) {
	//alert("nao achou então adiciona para criar o cookie!");
	valx=valores+"|"+valor;
	document.getElementById(idcheck).checked=true;
	if(idcheck=='selec') alert('Imóvel adicionado à lista de seleção!');
	} else 
	if(idcheck=='selec') alert('Imóvel já está na lista de seleção!');
	
} else {
if (document.getElementById(idcheck).checked==false){
	//alert("nao está marcado! valores:"+valores+" valor:"+valor); 
	valx=removeItem(valores, valor);
		posV=valx.lastIndexOf("|");
		//alert("posição de | "+posV);
		if(posV>0) {
		valx=valx.slice(0,posV);
		//alert(posV+valx);
		}
	}
}

} else {

valx=valor;
//alert("valores do cookie: "+valx);
}
var valx = valx;
//alert("Valor final do cookie: "+valx);
//return valx;
//if(valx!='') {
createCookie('imoveis',valx,360);
//}
}


//remove item (string or number) from an array
function removeItem(originalArray, itemToRemove) {
	var j = 0;
	var posV = 0;
	var fimArray="";
	//alert("Array original: "+originalArray);
	originalArray=originalArray.split("|");
	while (j < originalArray.length) {
		if (originalArray[j] == itemToRemove) {
			originalArray.splice(j, 1);
			//alert("Original tornou-se: "+originalArray);
		} else { 
		fimArray=fimArray+originalArray[j]+"|";
		j++; 
		}

	}
	//alert("Final, após remoção: "+fimArray);
return fimArray;
}

