function panelController(name) {
	var ctrls = new Array();
	var specials = new Array();
	var specialNames = new Array();
	var itemClick = false;
	var myName = name;
	
	this.addItem = function(name) {
		var added = document.getElementById(name);
		if (added != null)
			ctrls[ctrls.length] = added;
	}
	
	this.addSpecial = function(name) {
		var added = document.getElementById(name);
		if (added != null)
			specials[specials.length] = added;
		else
			specialNames[specialNames.length] = name;
	}
	
	this.clearField = function() {
		if (!itemClick) {
			var size = specials.length;
			for (var i=0; i<size; ++i)
				specials[i].style.visibility = 'visible';
			var size = ctrls.length;
			for (var i=0; i<size; ++i) {
				ctrls[i].style.display = 'none';
				if (ctrls[i].parentNode != null)
					ctrls[i].parentNode.style.color = '#999999';
			}
		}
		itemClick = false;
	}
	
	this.toggleItem = function(ev, position) {
		var size = specialNames.length;
		for (var i=size-1; i>=0; --i) {
			var postInit = document.getElementById(specialNames[i]);
			if (postInit != null)
				specials[specials.length] = postInit;
			--specialNames.length;
		}
		size = ctrls.length;
		var isVisible = false;
		for (var i=0; i<size; ++i) {
			if (ctrls[i].id==position) {
				var currentStyle = '' + ctrls[i].style.display;
				if (currentStyle.length == 0) currentStyle = 'none';
				isVisible = currentStyle != 'none';
			}
		}
		size = specials.length;
		for (var i=0; i<size; ++i)
			specials[i].style.visibility = isVisible ? 'visible' : 'hidden';
		size = ctrls.length;
		for (var i=0; i<size; ++i) {
			if (ctrls[i].id==position) {
				ctrls[i].style.display = (isVisible) ? 'none' : 'block';
				if (ctrls[i].parentNode != null)
					ctrls[i].parentNode.style.color = (isVisible) ? '#999999' : '#666666';
			}
			else {
				ctrls[i].style.display = 'none';
				if (ctrls[i].parentNode != null)
					ctrls[i].parentNode.style.color = '#999999';
			}
		}
		itemClick = true;
		ev.returnValue = 0;
		return 0;
	}
	
	this.changeIndex = function (ctrlName, index) {
		var ctrl = document.getElementById(ctrlName);
		if (ctrl != null) {
			var size = ctrl.options.length;
			var currIndex = ctrl.selectedIndex;
			if (currIndex != index) {
				ctrl.selectedIndex = index;
				ctrl.onchange();
			}
		}
	}
	
	this.copyCombo = function(position, combo) {
		var posCtrl = document.getElementById(position);
		var comboCtrl = document.getElementById(combo);
		if ((posCtrl != null) && (comboCtrl != null)) {
			var content = '';
			var size = comboCtrl.options.length;
			for (var i=0; i<size; ++i) {
				var ctrlText = comboCtrl.options[i].text
					.replace(/^\s*/, '')
					.replace(/\s*$/, '');
				if (ctrlText.length == 0)
					ctrlText = 'Todos';
				if (ctrlText == 'Erro#1')
					continue;
				content += '<a href="#self" class="selectpanel_el"><div class="selectpanel_in" onclick="'
					+ myName
					+ '.changeIndex(\''
					+ combo
					+ '\', '
					+ i
					+ ');">'
					+ (comboCtrl.options[i].selected ? '&nbsp;&gt;&nbsp;' : '')
					+ ctrlText
					+ '</div></a>'
			}
			posCtrl.innerHTML = content;
		}
	}
}

var menuHandler = new panelController('menuHandler');

function changeCombo(ctrlName, index) {
	var ctrl = document.getElementById(ctrlName);
	if (ctrl != null) {
		var size = ctrl.options.length;
		var currIndex = ctrl.selectedIndex;
		if (currIndex != index) {
			ctrl.selectedIndex = index;
			ctrl.onchange();
		}
	}
}

function openPopup(whereTo) {
	vWin = window.open(
		whereTo
		, '_detalhes'
		, 'top=40,left=40,width=562,height=306,menubar=no,resizable=no,scrollbars=no,toolbar=no'
	);
	if (vWin == null) {
		alert('Seu navegador est\u00E1 bloqueando popups. N\u00E3o \u00E9 poss\u00EDvel a exibi\u00E7\u00E3o dos detalhes do an\u00FAncio.');
	}
}

function buildRecommendationMail(adNumber, group) {
	var whereTo = '';
	switch (group) {
		case 1:
			whereTo = 'autos';
			break;
		case 2:
			whereTo = 'imoveis';
			break;
		case 3:
			whereTo = 'empregos';
			break;
		case 4:
			whereTo = 'tecnologia';
			break;
		case 5:
			whereTo = 'gerais';
			break;
	}
	var base = './';
	var thisLoc = '' + document.location;
	if ((/\/(autos|imoveis|empregos|tecnologia|gerais)\//i).test(thisLoc))
		base = '../';
	thisLoc = thisLoc
		.replace(/\/[^\/]*$/g, '/detalhes.aspx')
		.replace(/(autos|imoveis|empregos|tecnologia|gerais)/g, whereTo);
	var vWin = window.open(
		'about:blank'
		, '_proposta'
		, 'top=60,left=440,width=386,height=372,menubar=no,resizable=no,scrollbars=no,toolbar=no'
	);
	if (vWin == null) {
		alert('Seu navegador est\u00E1 bloqueando popups. N\u00E3o \u00E9 poss\u00EDvel a exibi\u00E7\u00E3o dos detalhes do an\u00FAncio.');
	}
	else {
		vWin.document.location = base + 'home_indique.aspx?adnum=' + adNumber + '&group=' + group;
	}
}

function showProposalPopup(adNumber, addressLength) {
	if (addressLength <= 0) {
		alert('Anunciante n\u00E3o possui e-mail para contatos cadastrado.\nVerifique o texto do an\u00FAncio');
		return;
	}
	vWin = window.open(
		'./proposta.aspx?adnum=' + adNumber
		, '_proposta'
		, 'top=60,left=440,width=562,height=202,menubar=no,resizable=no,scrollbars=no,toolbar=no'
	);
	if (vWin == null) {
		alert('Seu navegador est\u00E1 bloqueando popups. N\u00E3o \u00E9 poss\u00EDvel a exibi\u00E7\u00E3o dos detalhes do an\u00FAncio.');
	}
}

function openCompareSelector(group) {
	try {
		group = parseInt(''+group);
	}
	catch (e) {
		group = 0;
	}
	var basedir = '../';
	if ((group < 1) || (group > 5)) {
		group = 0;
		basedir = './';
	}
	vWin = window.open(
		basedir + 'home_selecionar.aspx'
		, '_proposta'
		, 'top=30,left=240,width=600,height=300,menubar=no,resizable=no,scrollbars=yes,toolbar=no'
	);
	if (vWin == null) {
		alert('Seu navegador est\u00E1 bloqueando popups. N\u00E3o \u00E9 poss\u00EDvel a exibi\u00E7\u00E3o do seletor para compara\u00E7\u00E3o.');
	}
}

function compareAdsShow(group, tail) {
	try {
		group = parseInt(''+group);
	}
	catch (e) {
		group = 0;
	}
	if ((group < 1) || (group > 5)) {
		alert('Erro de chamada para compara\u00E7\u00E3o');
		return;
	}
	var basedir = '';
	switch (group) {
		case 1:
			basedir = 'autos/comparativo.aspx?';
			break;
		case 2:
			basedir = 'imoveis/comparativo.aspx?';
			break;
		case 3:
			basedir = 'empregos/comparativo.aspx?';
			break;
		case 4:
			basedir = 'tecnologia/comparativo.aspx?';
			break;
		case 5:
			basedir = 'gerais/comparativo.aspx?';
			break;
	}
	if (window.opener != null) {
		locPath = '' + window.opener.location;
		var pathXP = (/^(.*)\/[^\/]*\.aspx(.*)?$/i);
		var basepath = '';
		if (pathXP.test(locPath)) {
			basepath = RegExp.$1;
		}
		pathXP = /^(.*)(autos|imoveis|empregos|tecnologia|gerais)/i;
		if (pathXP.test(basedir)) {
			basepath = RegExp.$1;
		}
		window.opener.document.location = basepath + basedir + tail;
	}
	window.close();
}

function updateParent(ctrl) {
	if (ctrl == null) return;
	if (window.opener != null) {
		var cBoxes = window.opener.document.getElementsByTagName('input');
		var size = cBoxes.length;
		for (var i=0; i<size; ++i) {
			if ((cBoxes[i].type == 'checkbox') && (cBoxes[i].value == ctrl.value)) {
				cBoxes[i].checked = ctrl.checked;
				break;
			}
		}
	}
}

function imgError(imgCtrl, isLogo, isMain, isDetail) {
	if (imgCtrl != null) {
		var basepath = './';
		var locP = '' + document.location;
		var pathXP = /\/(autos|imoveis|empregos|tecnologia|gerais)\//i;
		if (pathXP.test(locP))
			basepath = '../';
		imgCtrl.onerror = null;
		var padNoImg = isMain ? 'fotonaoenviada_destaque.jpg' : 'fotonaoenviada_padrao.jpg';
		var destNoImg = isMain ? 'fotonaoenviada_grande.gif' : 'fotonaoenviada_pequena.jpg';
		var noImage =
			'framework/img/'
			+ (isDetail ? destNoImg : padNoImg);
		var noLogo = 'framework/img/sem_logo.gif';
		imgCtrl.src =
			basepath
			+ (isLogo ? noLogo : noImage);
	}
}

function imgErrorBit(imgCtrl, isLogo, isMain, received) {
	if (imgCtrl != null) {
		var basepath = './';
		var locP = '' + document.location;
		var pathXP = /\/(autos|imoveis|empregos|tecnologia|gerais)\//i;
		if (pathXP.test(locP))
			basepath = '../';
		imgCtrl.onerror = null;
		var destImg = received ? 'dest_aprovacao.gif' : 'fotonaoenviada_grande.gif';
		var padImg  = received ?  'pad_aprovacao.gif' : 'btn_ft_n_enviada.gif';
		var noLogo  = 'framework/img/sem_logo.gif';
		var noImage =
			'framework/img/'
			+ (isMain ? destImg : padImg);
		imgCtrl.src =
			basepath
			+ (isLogo ? noLogo : noImage);
	}
}

function imgRemError(imgCtrl, ctName) {
	if (imgCtrl != null) {
		imgCtrl.onerror = null;
		var ctThumb = document.getElementById(ctName);
		if (ctThumb != null) {
			ctThumb.removeChild(imgCtrl);
		}
	}
}

function changeImg(imgCtrl, panelName) {
	if (imgCtrl != null) {
		var imgPath = '' + imgCtrl.src;
		var ctPanel = document.getElementById(panelName);
		if (ctPanel != null) {
			ctPanel.src = imgPath.replace(/THUMB_/i, 'DET_');
		}
	}
}

function AddFlash(w,h,id,wmode,movie,container,flashVars) {
	var isFlashVars = arguments.length==7;
	var ctCtrl = document.getElementById(container);
	if (ctCtrl != null) {
		var result =
			  '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
			+ 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" '
			+ 'width="' + w + '" height="' + h + '" id="' + id + '">\n'
			+ '<param name="id" value="' + id + '"/>\n'
			+ '<param name="wmode" value="' + wmode + '"/>\n'
			+ '<param name="quality" value="high"/>\n'
			+ '<param name="movie" value="' + movie + '"/>\n';
		if (isFlashVars){
			result += '<param name="FlashVars" value="' + flashVars + '"/>\n';
		}
		result +=
			  '<embed src="' + movie + '" quality="high" '
			+ 'pluginspage="http://www.macromedia.com/go/getflashplayer" '
			+ 'type="application/x-shockwave-flash" '
			+ 'wmode="' + wmode + '" width="' + w + '" height="' + h + '" '
		if (isFlashVars){
			result += ' FlashVars="' + flashVars + '"';
		}
		result +=
			  '></embed>'
			+ '</object>';
		ctCtrl.innerHTML = result;
	}
}

function carSearchClick(oName, wName) {
	var orig = document.getElementById(oName);
	var shaw = document.getElementById(wName);
	if ((orig != null) && (shaw != null)) {
		// var pos = shaw.selectedIndex;
		// var selVal = shaw.options(pos).value;
		// var size = orig.options.length;
		// for (var i=0; i<size; ++i) {
			// if (orig.options(i).value == selVal) {
				// orig.selectedIndex = i;
				// break;
			// }
		// }
		orig.value = shaw.value;
		//orig.selectedIndex = shaw.selectedIndex;
	}
}

function carSearchInit(sName, oName, wName) {
	carSearchChange(sName, oName, wName);
	var orig = document.getElementById(oName);
	var shaw = document.getElementById(wName);
	if ((orig != null) && (shaw != null)) {
		shaw.value = orig.value;
	}
}

function carSearchChange(sName, oName, wName) {
	var selt = document.getElementById(sName);
	var orig = document.getElementById(oName);
	var shaw = document.getElementById(wName);
	if ((selt == null) || (orig == null) || (shaw == null))
		return;
	var part = selt.value;
	var size = orig.options.length;
	shaw.options.length = 0;
	for (var i=0; i<size; ++i) {
		var optText = orig.options[i].value;
		if ((part.length == 0) || (optText.length == 0) || (optText.indexOf(part + '\u2020') == 0)) {
			var newOpt = document.createElement('OPTION');
			shaw.options.add(newOpt);
			newOpt.text = orig.options[i].text;
			//newOpt.innerText = orig.options[i].innerText;
			newOpt.value = orig.options[i].value;
		}
	}
}

function getInternetExplorerVersion() {
	var rv = -1; 
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat( RegExp.$1 );
	}	
	return rv;
}

function initTypeField (ctrlName, name1, name2) {
    ctrl = document.getElementById(ctrlName);
    enableTypeField(ctrl, name1, name2);
}

//Desabilita Modelo, Fabricante e Ano quando diferente de Automóveis XXXXXXXXXXXXXXXXXXXXXX
function enableTypeField (ctrl, name1, name2) {
    if (ctrl != null) {
        var ddl1 = document.getElementById(name1);
        var ddl2 = document.getElementById(name2);
        if (ctrl.value == 'Autom\u00F3veis') {
            if (ddl1 != null) {
                ddl1.disabled = false;
            }
            if (ddl2 != null) {
                ddl2.disabled = false;
            }
        }
        else {
            if (ddl1 != null) {
                ddl1.disabled = true;
                ddl1.selectedIndex = 0;
            }
            if (ddl2 != null) {
                ddl2.disabled = true;
                ddl2.selectedIndex = 0;
            }
        }
    }
}

function getInternetExplorerVersion(){

 {
  var rv = -1; 
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
 }
  var ver = getInternetExplorerVersion();
  document.write("<link rel=\"stylesheet\" href=\"_estilo-imoveis.css\" type=\"text/css\"></link>")
  if ( ver > -1 )
  {
    if ( ver == 7.0 ) {
	  document.write("<link rel=\"stylesheet\" href=\"_estilo-imoveisIE7.css\" type=\"text/css\"></link>")
	}
  }
}
