/*
	Variavel para armazenamento do texto de busca
*/

var campoBusca = '';

//BEGIN - SRN#4138 - André Andrade 12/11/2007
//funcao adicionada para esconder todas as respostas
function hideAllRespostas() {
	try {
		var allDivs = document.getElementsByTagName("div");
		for(var i=0; i<allDivs.length; i++) {
			if(allDivs[i].className == "resposta" && allDivs[i].id.charAt(0) == "p") {
				allDivs[i].style.display = 'none';
				document.getElementById('ico' + allDivs[i].id.substring(1)).className = 'pmais';
			}
		}
	} catch(e) {}
}
//END - SRN#4138 - André Andrade 12/11/2007

/*
	Shortcut utilizado pelo xsl para reduzir o output e termos todas as funcionalidades do windows.open
*/
function opnUrP(url, target, flags, height, width, left, top) {
	var flagsPop =
	"channelmode=" + flags.charAt(0) +
	",menubar=" + flags.charAt(1) +
	",resizable=" + flags.charAt(2) +
	",fullscreen=" + flags.charAt(3) +
	",scrollbars=" + flags.charAt(4) +
	",status=" + flags.charAt(5) +
	",titlebar=" + flags.charAt(6) +
	",location=" + flags.charAt(7) +
	",toolbar=" + flags.charAt(8);

	if (height > 0)		{
		flagsPop += ",height=" + height;
	}
	if (width > 0) {
		flagsPop += ",width=" + width;
	}
	if (left > 0) {
		flagsPop += ",left=" + left;
	}
	if (top > 0) {
		flagsPop += ",top=" + top;
	}
	open(url,target,flagsPop);
}

function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

/*
	Altera o estado conforme comandado pelo rodape do portal.
*/
function openUrlRodape() {
	if (currentArea == "") {
		alert("Selecione um DDD");
	}	else {
		if (campoBusca != '') {
			preencheFormReplaceLocSubmit(currentArea, currentEstado);
		} else {
			var dest = destChannelArea + '&wfe_pweb_area=' + currentArea + '&wfe_pweb_estado=' + currentEstado + '&wfe_set_cookie=1';
			window.location = dest;
		}
	}
	if (getCookie("pagina"))
    	setCookie("pagina", "1", "", "/");

}

/*
	"Classe" de uma area do portal web
*/
function AreaPw(id, area, estado) {
	this.id = id;
	this.area = area;
	this.estado = estado;

	return this;
}

/*
	Configura o novo estado
*/
function setNewState(obj) {
	currentEstado = obj.value;
	currentArea = "";
	setArea();
}

/*
	Configura a nova area
*/
function setNewArea(obj) {
	currentArea = obj.value;
}

/*
	Ativa o estado corrente no rodape para que a combo reflita o estado selecionado.
*/
function setState() {

	var est = document.getElementById('wfe_pweb_estado_rodape');

	if (est == null)
		return;

	if (est.options == null)
		return;

	var opt = est.options;
	var cont = 0;

	for (cont = 0; cont < opt.length; cont++) {
		if (trim(opt[cont].value) == trim(currentEstado)) {
			opt[cont].selected = true;
			return;
		}
	}
}

/*
	Ao alterar o estado a combo de DDD's precisa estar correta, esta funcao tem esta finalidade.
*/
function setArea() {

	if (area == null)
		return "area didnt found";

	if (area.length == 0)
		return "area.length equals 0";

	var selArea = document.getElementById('wfe_pweb_area_rodape');

	if (selArea == null)
		return "setArea equals null";

	var opt = selArea.options;
	var cont = 0;

	opt.length = 0;

	opt[opt.length] = new Option("","");
	for (cont = 0; cont < area.length; cont++) {
		if (trim(area[cont].estado) == trim(currentEstado)) {
			var nopt = new Option(area[cont].area,area[cont].id);
			opt[opt.length] = nopt;
		}
	}

	for (cont = 0; cont < opt.length; cont++) {
		if (trim(opt[cont].value) == trim(currentArea)) {
			opt[cont].selected = true;
			return ""+opt[cont].value+" selected";
		}
	}

	return "returning from the end";
}

/*
  Data para cookie
*/
function getDate() {
	var today = new Date();
	var zero_date = new Date(0,0,0);
	today.setTime(today.getTime() - zero_date.getTime());
	var cookie_expire_date = new Date(today.getTime() + (8 * 7 * 86400000));
	return cookie_expire_date;
}

/*
	Obtem parametro do cookie
*/
function getCookie(name) {
   var start = document.cookie.indexOf(name + "=");
   var len = start+name.length + 1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));

}

/*
	Define parametro no cookie
*/
function setCookie(name,value,expires, path) {
	var cookieString = name + "=" +escape(value) +
	   ( (expires) ? ";expires=" + expires.toGMTString() : "") +
		   ( (path) ? ";path=" + path : "");
	document.cookie = cookieString;

}



/*
	Faz a validacao do formulario de busca
*/
function validaBusca(form){

	if (form.qt.value == "")
	{
		alert("Por favor preencha o campo Busca.");

		form.qt.focus();
		return (false);
	}

	if(form.qt.value.length < 3){
		alert("Por favor preencha o campo Busca com pelo menos 3 caracteres.");
		form.qt.focus();
		return (false);
	}

	/* May 11, 2009 - joao.tonsic – INICIO */
	form.qt.value = form.qt.value.replace(/<[^>]+>/g, "");
	form.qt.value = form.qt.value.replace(/(;)/g, "");
	form.qt.value = form.qt.value.replace(/(\/)/g, "");
	form.qt.value = form.qt.value.replace(/(\\)/g, "");
	form.qt.value = form.qt.value.replace(/(\\)/g, "");
	form.qt.value = form.qt.value.replace(/(\')/g, "");
	form.qt.value = form.qt.value.replace(/(\")/g, "");
	form.qt.value = form.qt.value.replace(/(\()/g, "");
	form.qt.value = form.qt.value.replace(/(\))/g, "");
	form.qt.value = form.qt.value.replace(/(\|)/g, "");
	form.qt.value = form.qt.value.replace(/(\<)/g, "");
	form.qt.value = form.qt.value.replace(/(>)/g, "");
	form.qt.value = form.qt.value.replace(/(`)/g, "");
	form.qt.value = form.qt.value.replace(/(\[)/g, "");
	form.qt.value = form.qt.value.replace(/(\])/g, "");
	form.qt.value = form.qt.value.replace(/(\{)/g, "");
	form.qt.value = form.qt.value.replace(/(\})/g, "");
	form.qt.value = form.qt.value.replace(/(:)/g, "");
	form.qt.value = form.qt.value.replace(/(#)/g, "");
	form.qt.value = form.qt.value.replace(/(&)/g, "");
	form.qt.value = form.qt.value.replace(/(=)/g, "");
	form.qt.value = form.qt.value.replace(/(%)/g, "");
	form.qt.value = form.qt.value.replace(/(\$)/g, "");
	/* May 11, 2009 - joao.tonsic – FIM */

	if (getCookie("pagina"))
    setCookie("pagina", "1", "", "/");

}

/*
	Funcao para tratar os parametros da url, pega uma query string e cria os inputs no formulario indicado.
*/
function setGetParametersToForm(url, idForm, replace) {

	if (idForm == null){
		return false
	}

	var frm = document.getElementById(idForm);

	if (frm == null){
		return false
	}

	var inputs = frm.getElementsByTagName("INPUT");

	if (url == null){
		return false
	}

	if (replace == null) {
		replace = false;
	}

	url = url + "";
	var qss = url.indexOf('?');

	if (qss == -1) {
		return true;
	}

	var qs = url.substr(qss+1);
	var args = qs.split("&");

	for (var cont = 0; cont < args.length ; cont++ ){
		var att = args[cont].split('=');
		var name = att[0];
		var value = att[1];

		var temNoForm = false;
		var formInputElement = null;

		for (var conti = 0; conti < inputs.length && temNoForm == false; conti++ ) {
			if (inputs[conti].name == name) {
				temNoForm = true;
				formInputElement = inputs[conti];
			}
		}

		if (!temNoForm)	{
			var newElm = document.createElement("input");
			newElm.name = name;
			newElm.value = value;
			newElm.type = "text";
			frm.appendChild(newElm);
		} else if (replace) {
			formInputElement.value = value;
		}

	}
	return true;
}

/*
	Criar uma nova action de acordo com o campo da busca, area e cidade
*/
function preencheFormBuscaCampo(campo, area, cidade) {

	var frm = document.getElementById('form1');
	var urlFull = frm.action;
	var valorArea = urlFull;
	var valorEstado = urlFull;

	valorArea = "wfe_pweb_area=" + valorArea.substring(valorArea.indexOf("wfe_pweb_area=")+14, valorArea.indexOf("wfe_pweb_area=")+16);
	valorEstado = "wfe_pweb_estado=" + valorEstado.substring(valorEstado.indexOf("wfe_pweb_estado=")+16, valorEstado.indexOf("wfe_pweb_estado=")+18);

	urlFull =  urlFull.replace(valorArea, "wfe_pweb_area=" + area);
	urlFull =  urlFull.replace(valorEstado, "wfe_pweb_estado=" + cidade);

//	if (urlFull.indexOf("http://") < 0) {
//		frm.action = "http://" + window.location.href.split('/')[2] + urlFull;
//	} else {
		frm.action = urlFull;
//	}

	frm.qt.value = campo;

}

/*
	Criar uma nova action de acordo com a url
*/
function preencheFormAction() {

	var frm = document.getElementById('form1');
	var urlAction = frm.action;
	var valorAreaUrl = document.location.href;
	var valorEstadoUrl = document.location.href;
	var valorAreaAction = urlAction;
	var valorEstadoAction = urlAction;

	// Pegar parametros da url
	valorAreaUrl = "wfe_pweb_area=" + valorAreaUrl.substring(valorAreaUrl.indexOf("wfe_pweb_area=")+14, valorAreaUrl.indexOf("wfe_pweb_area=")+16);
	valorEstadoUrl = "wfe_pweb_estado=" + valorEstadoUrl.substring(valorEstadoUrl.indexOf("wfe_pweb_estado=")+16, valorEstadoUrl.indexOf("wfe_pweb_estado=")+18);

	// Pegar parametros da action
	valorAreaAction = "wfe_pweb_area=" + valorAreaAction.substring(valorAreaAction.indexOf("wfe_pweb_area=")+14, valorAreaAction.indexOf("wfe_pweb_area=")+16);
	valorEstadoAction = "wfe_pweb_estado=" + valorEstadoAction.substring(valorEstadoAction.indexOf("wfe_pweb_estado=")+16, valorEstadoAction.indexOf("wfe_pweb_estado=")+18);

	urlAction = urlAction.replace(valorAreaAction, valorAreaUrl);
	urlAction = urlAction.replace(valorEstadoAction, valorEstadoUrl);

//	if (urlAction.indexOf("http://") < 0) {
//		frm.action = "http://" + window.location.href.split('/')[2] + urlAction;
//	} else {
		frm.action = urlAction;
//	}
}

/*
	Criar uma nova action de acordo com a area e cidade mantendo a alguns parametros da action original
*/
function preencheFormReplaceLocSubmit(area, cidade) {

	var frm = document.getElementById('form1');
	var urlAction = frm.action;
	var valorAreaAction = urlAction;
	var valorEstadoAction = urlAction;

	// Pegar parametros da action
	valorAreaAction = "wfe_pweb_area=" + valorAreaAction.substring(valorAreaAction.indexOf("wfe_pweb_area=")+14, valorAreaAction.indexOf("wfe_pweb_area=")+16);
	valorEstadoAction = "&wfe_pweb_estado=" + valorEstadoAction.substring(valorEstadoAction.indexOf("&wfe_pweb_estado=")+17, valorEstadoAction.indexOf("&wfe_pweb_estado=")+19);

	urlAction = urlAction.replace(valorAreaAction, "wfe_pweb_area=" + trim(area) + "&wfe_pweb_estado=" + trim(cidade) + "&wfe_set_cookie=1");
	urlAction = urlAction.replace(valorEstadoAction, "");

//	if (urlAction.indexOf("http://") < 0) {
//		frm.action = "http://" + window.location.href.split('/')[2] + trim(unescape(urlAction));
//	} else {
		frm.action = trim(unescape(urlAction));
//	}

	frm.submit();
}

//Function to trim the space in the left side of the string
function ltrim ( s ){
  return s.replace( /^\s*/, "" );
}

//Function to trim the space in the right side of the string
function rtrim ( s ){
   return s.replace( /\s*$/, "" );
}

//*Function to trim the space in the  string
function trim(s) {
   var temp = s;
   return temp.replace(/^\s+/,'').replace(/\s+$/,'');
}

function trimPlus(s) {
   var temp = s;
   return temp.replace(/^\++/,'').replace(/\++$/,'');
}

function getHomeChannel(isVoce, isSuaEmpresa, isSobreATim) {
	var newKey = null;

	if (isVoce) {
		newKey = "menuitem.661ec82b34ee3a93b5508210703016a0/?vgnextoid=b2ed14653e2fa010VgnVCM1000000603610aRCRD";
	} else if (isSuaEmpresa) {
		newKey = "menuitem.8d59a20c68654a93b5508210703016a0/?vgnextoid=70fd14653e2fa010VgnVCM1000000603610aRCRD";
	} else if (isSobreATim) {
		newKey = "menuitem.bcd0f5e79ab784f8714f1010703016a0/?vgnextoid=cb4b21f93fbda010VgnVCM1000000603610aRCRD";
	}

	return newKey;
}

// funcion to set the market depending on the last navigation.

function setHomeCookie(isVoce, isSuaEmpresa, isSobreATim) {

	if (isVoce == false && isSuaEmpresa == false && isSobreATim == false) {
		return;
	}

	var cookies = document.cookie.split(";");
	var pwebCookie = null;

	for (var cont=0 ;cont < cookies.length ; cont++) {
		if (cookies[cont].indexOf("wfe_pweb_estado=") == 0) {
			pwebCookie = cookies[cont].slice(16);
			break;
		}
	}

	if (pwebCookie != null) {
		var pwebCookieContents = pwebCookie.split("|");
		var newCookieArray = new Array();
		var newCookie;
		var data = new Date();

		data.setYear(data.getFullYear()+2);

		for (var cont=0;cont < pwebCookieContents.length ; cont++ )	{
			if (pwebCookieContents[cont].indexOf("menuitem") != 0) {
				if(pwebCookieContents[cont].indexOf("undefined") != 0) {
					newCookieArray[newCookieArray.length] = pwebCookieContents[cont];
				}
			}
		}

		var newKey = getHomeChannel(isVoce, isSuaEmpresa, isSobreATim);

		if (newKey == null) {
			return;
		}

		newCookieArray[newCookieArray.length] = newKey;

		newCookie = "wfe_pweb_estado="+newCookieArray[0];

		for (var cont=1 ; cont < newCookieArray.length ; cont++ ) {
			newCookie += "|" + newCookieArray[cont];
		}

		document.cookie = newCookie + ";expires=" + data.toGMTString() + ";path=/portal/site/PortalWeb";
	}

}

function executeHomeCookie() {
	setHomeCookie(isVoce, isSuaEmpresa, isSobreATim);
}

function execAftLoad(func) {

  try
  {
  	if (func == null || (typeof func) != "function" )
  		return false;

  	if (window.onload == null) {
  		window.onload = func;
  		return true;
  	}

  	var tmp = window.onload;
  	eval(" window.onload = function () {  var fnct = " + tmp + "; fnct();  var fnct = " + func + "; fnct(); };");
  }
  catch(err)
  {
    txt="Error on this page.\n";
  }

}

//execAftLoad(setArea);
//execAftLoad(setState);
//execAftLoad(executeHomeCookie);

