function getCookieIndex(name) {
  var dc = document.cookie;
  var prefix = name + '=';
  var begin = dc.indexOf('; ' + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
 var end = document.cookie.indexOf(';', begin);
 if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
 
function setCookieIndex(name, value, path, domain, secure) {
 var expires = new Date();
 //para expirar daqui a 1 ano
 var futuro = expires.getTime()+(365*24*60*60*1000);
 expires.setTime(futuro);
 valor = value;
 var curCookie = name + '=' + escape(valor) +
	 ((expires) ? '; expires=' + expires.toGMTString() : '') +
	 ((path) ? '; path=' + path : '') +
 ((domain) ? '; domain=' + domain : '') +
 ((secure) ? '; secure' : ''); 
	 document.cookie = curCookie;
}
 
function setaCookieVal(val,tipo){
	valor = getCookieIndex('myIndex');
	valE = ''; valM = '';
	if(valor && tiraespacos(valor)!='' && tiraespacos(valor)!=';'){
		arrValor = valor.split(';');
		for(i=0;i<arrValor.length;i++){
			escolha = arrValor[i].substring(0,arrValor[i].length);
			if(arrValor[i].substring(0,1)=='E')	valE = escolha;
			if(arrValor[i].substring(0,1)=='M')	valM = escolha;
		}
	}
	if((tiraespacos(valE)=='' || tiraespacos(valE)=='E') && tipo=='est') valE = 'E'+val;
	if((tiraespacos(valM)=='' || tiraespacos(valM)=='M') && tipo=='mun') valM = 'M'+val;
 
	valor  = valE+';'+valM;
 
	setCookieIndex('myIndex',valor,'/');
}
 
function tiraespacos(tmpStr) 
{
  var tmpStr, atChar;
  if (tmpStr.length > 0) atChar = tmpStr.charAt(0);
  while (isSpace(atChar)) 
  {
		tmpStr = tmpStr.substring(1, tmpStr.length);
	atChar = tmpStr.charAt(0);
  }
  if (tmpStr.length > 0) 
		  atChar = tmpStr.charAt(tmpStr.length-1);
  while (isSpace(atChar)) 
  {
	tmpStr = tmpStr.substring(0,(tmpStr.length-1));
	atChar = tmpStr.charAt(tmpStr.length-1);
  }
  return tmpStr;
}
 
function isSpace(inChar) 
{
  return (inChar == ' ' || inChar == '\t' || inChar == '\n');
}
 
 
function set_estado_openpage(home,optcase,tipo) {
	base = self.opener;
	   try{
			caminho = base.parent.centro_setorial.document;
	   }catch(e){
	   	   try{
				caminho = base.parent.parent.centro_setorial.document;
		   }catch(e2){
			   try{
				   caminho = base.top.document;
			   }catch(e3){
					caminho = base;
			   }
		   }
	   }
	setaCookieVal(optcase,tipo);
	home = 'estadual';
	location.href = "/main_online_frame.php?home=" + home + "&optcase=" + optcase + "&secao=6&page=inc/process_agenda_list_estadual_munic.php?uf=" + optcase;
}
