function HeaderCombosGo(oSelect)
{
	try
	{
		var iIndex 	= oSelect.selectedIndex;
		var oOption = oSelect.options[iIndex];
		var value = oOption.value;
		document.location = value;
	} catch(e){
		//do nothing
	}
}

function sendMail(url)
{
	newWindow = window.open(url,'','scrollbars=no,menubar=no,height=265,width=500,resizable=no,toolbar=no,location=no,status=no');return false;
	return false;
}

function sendMailGetUrl()
{
	var height = parseInt(document.getElementById('clear').offsetTop) + parseInt(document.getElementById('clear').clientHeight) + 70;
	window.resizeTo(500, height);
	document.Form1.url.value = opener.document.URL;
	return true;
}

function onEnterGoTo(url)
{
	if((event.which && event.which == 13) || 
    (event.keyCode && event.keyCode == 13))
	{
		searchSubmit(url);
		return false;
	}
	return true;
}

function searchSubmit(url)
{
	var element = document.getElementById('pesq');
	var searchKeyWord = encodeURI(element.value);
	if(searchKeyWord.length > 0){
		searchKeyWord = searchKeyWord.replace(/&/, '%26');
		var redirectUrl = url + searchKeyWord;
		document.location = redirectUrl;	
	}
	return false;
}


/* AJAX */

var concelhosHTML = '<select id="concelho" name="concelho">';
var gruposHTML = '<select id="grupo" name="grupo" onchange="javascript:loadClasses(this.value)"><option value="">Segundo Nível ...</option>';
var classesHTML = '<select id="classe" name="classe"><option value="">Terceiro Nível ...</option>';

function getHTTPObject() {
	if (typeof XMLHttpRequest != 'undefined') {
		return new XMLHttpRequest();
	}
	try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
	return false;
}


function loadConcelhos(distrito)
{
	var http = getHTTPObject();
	if(typeof http == typeof false)
	{
		document.getElementById('submit').value = 0;
		document.forms[1].submit();
		return;
	}
	document.body.style.cursor = "wait";
	var url = '/Ajax/getEmpresasConcelhosOptions.aspx?distrito=' + distrito;
	http.open('GET', url, true);
	http.onreadystatechange = function()
	{
		if (http.readyState == 4 && http.status == 200)
		{
			var text = http.responseText;
			text = text.replace('<html id="template">', '');
			text = text.replace('</html>', '');
			var html = concelhosHTML;
			html += text;
			html += '</select>';
			document.getElementById('concelhosDiv').innerHTML = html;
			document.body.style.cursor = "auto";
		}
	}
	http.send(null);
}

function loadGrupos(divisao)
{
	document.getElementById('classesDiv').innerHTML = classesHTML + '</select>';

	var http = getHTTPObject();
	if(typeof http == typeof false)
	{
		document.getElementById('submit').value = 0;
		document.forms[1].submit();
		return;
	}
	document.body.style.cursor = "wait";
	var url = '/Ajax/getEmpresasCAEsOptions.aspx?divisao=' + divisao;
	http.open('GET', url, true);
	http.onreadystatechange = function()
	{
		if (http.readyState == 4 && http.status == 200)
		{
			var text = http.responseText;
			text = text.replace('<html id="template">', '');
			text = text.replace('</html>', '');
			var html = gruposHTML;
			html += text;
			html += '</select>';
			document.getElementById('gruposDiv').innerHTML = html;
			document.body.style.cursor = "auto";
		}
	}
	http.send(null);
}

function loadClasses(grupo)
{
	var http = getHTTPObject();
	if(typeof http == typeof false)
	{
		document.getElementById('submit').value = 0;
		document.forms[1].submit();
		return;
	}
	document.body.style.cursor = "wait";
	var url = '/Ajax/getEmpresasCAEsOptions.aspx?grupo=' + grupo;
	http.open('GET', url, true);
	http.onreadystatechange = function()
	{
		if (http.readyState == 4 && http.status == 200)
		{
			var text = http.responseText;
			text = text.replace('<html id="template">', '');
			text = text.replace('</html>', '');
			var html = classesHTML;
			html += text;
			html += '</select>';
			document.getElementById('classesDiv').innerHTML = html;
			document.body.style.cursor = "auto";
		}
	}
	http.send(null);
}

function clearAndSubmit(elem)
{
	document.getElementById(elem).selectedIndex = 0;
	document.forms[1].submit();
}

