function $(obj)
{
	return document.getElementById(obj);
}

function ValidaDigitos(objeto)
{
	var	valorAuxiliar =	'';
	var	valor;
	var	digitosValidos;

	digitosValidos = '0123456789';

	valor =	objeto.value;

	for	(i = 0; i < valor.length; i++)
	{ 
		if (digitosValidos.indexOf(valor.charAt(i)) >= 0)
			valorAuxiliar += valor.charAt(i);
	}
		
	objeto.value = valorAuxiliar;						
}

function RedimensionaCorpo()
{
	princ = $("divPrincipal");
	
	tam = parseInt(princ.offsetHeight);
	tam = tam - 170;
	
	$("divCorpo").style.height = tam;
}

function TiraFoco()
{
	document.body.focus();
}

function AdicionaCampoOutros(obj, id)
{
	if (obj.value == "0")
	{		
		txt = document.createElement("input");
		txt.setAttribute("type", "text");
		txt.setAttribute("id", "txtOutra" + id);
		txt.setAttribute("name", "txtOutra" + id);
		txt.setAttribute("style", "width: 100px; margin-right: 30px;");
		
		$("divCategoria").appendChild(txt);
	}
	else
	{
		if (obj.value == "")
		{
			if (obj.id != "drpCategoria1")
				$("divCategoria").removeChild(obj);
		}
	
		if ($("txtOutra" + id))
			$("divCategoria").removeChild($("txtOutra" + id));
	}
}

function AdicionaTelefone()
{
	div = $("divTelefone");
	txts = div.getElementsByTagName("input");
	
	id = 0;
	campo = "";
	
	for (i = 0; i < txts.length; i++)
	{
		if (txts[i].getAttribute("type") != "button")
		{
			if (txts[i].value != "")
				id++;
			else
			{
				campo = txts[i].id;
				break;
			}
		}
	}
	
	if (campo != "")
	{
		alert("Entre com o telefone antes de adicionar outro.");
		$(campo).focus();
		return;
	}
	else
	{
		label = document.createElement("label");
		txtL = document.createTextNode("DDD-Telefone: ");
		label.appendChild(txtL);
		
		txt = document.createElement("input");
		txt.setAttribute("type", "text");
		txt.setAttribute("id", "txtTelefone" + id);
		txt.setAttribute("name", "txtTelefone" + id);
		txt.setAttribute("size", "11");
		txt.setAttribute("maxlength", "10");
		txt.setAttribute("onKeyUp", "ValidaDigitos(this)");
		
		br = document.createElement("br");
		
		div.appendChild(label);
		div.appendChild(txt);
		div.appendChild(br);
	}
}

function TestaCienciaTermo()
{
	if (! $("chkTermos").checked)
	{
		alert("Você deve aceitar os termos para cadastrar um anúncio");
		$("chkTermos").focus();
	}
	else
		$("form1").submit();
}

function AbreTermos()
{
	window.open("termos.htm", "termos", "width=350, heigth=450, statusbar=no, scrollbars=yes");
}

function AbrePDF(c)
{
	window.open("Actions/exportaPDF.php?c=" + c, "pdf", "location=no,toolbar=no,menubar=no,resizable=yes");
	void(0);
}

// Funções AJAX
var chamada = new cpaint();
	
chamada.set_transfer_mode('post');
chamada.set_response_type('text');

function Ajax_AdicionaCombo()
{
	drps = $("divCategoria").getElementsByTagName("select");
	
	id = "";
	campo = "";
	
	for (i = 0; i < drps.length; i++)
	{
		if (drps[i].id != "drpEstado")
		{
			if (drps[i].value != "")
				id += drps[i].value + ",";
			else
			{
				campo = drps[i].id;
				break;
			}
		}
	}
	
	i++;i++;
	
	id += "0";
	
	txts = $("divCategoria").getElementsByTagName("input");
	
	campo1 = "";
	
	for (j = 0; j < txts.length; j++)
	{
		if (txts[j].getAttribute("type") != "button")
			if (txts[j].value == "")
			{
				campo1 = txts[j].id;
				break;
			}
	}
	
	if (campo != "")
	{
		alert("Selecione uma categoria antes de adicionar outra.");
		$(campo).focus();
		return;
	}
	else if (campo1 != "")
	{
		alert("Entre com a nova categoria antes de adicionar outra.");
		$(campo1).focus();
		return;
	}
	
	chamada.call("Actions/adicionaCombo.php", "AdicionaCombo", Ajax_AdicionaComboAction, id, i);
}

function Ajax_AdicionaComboAction(retorno)
{
	arr = retorno.split(",");
	
	itm = arr[0].split("||");
	
	drp = document.createElement("select");
	drp.setAttribute("id", "drpCategoria" + itm[0]);
	drp.setAttribute("name", "drpCategoria" + itm[0]);
	//drp.setAttribute("onChange", "TiraFoco();AdicionaCampoOutros(this, " + itm[0] + ");");
	
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		drp.attachEvent("onchange", TiraFoco);
		drp.attachEvent("onchange", function() {AdicionaCampoOutros(this, itm[0]);});
	}
	else
		drp.setAttribute("onChange", "TiraFoco();AdicionaCampoOutros(this, " + itm[0] + ");");
	
	opt = document.createElement("option");
	opt.setAttribute("value", "");
	
	txt = document.createTextNode(itm[1]);
	opt.appendChild(txt);
	drp.appendChild(opt);
	
	for (i = 1; i < arr.length; i++)
	{
		itm = arr[i].split("||");
		
		opt = document.createElement("option");
		opt.setAttribute("value", itm[0]);
		
		txt = document.createTextNode(itm[1]);
		opt.appendChild(txt);
		drp.appendChild(opt);
	}
	
	$("divCategoria").appendChild(drp);
}

function Ajax_CadastraAnuncio()
{
	drps = $("divCategoria").getElementsByTagName("select");
	
	categ = "";
	
	for (i = 0; i < drps.length; i++)
	{
		if (drps[i].value != "")
		{
			if (drps[i].value != "0")
				categ += drps[i].value + ";";
		}
	}
}

function Ajax_CadastraAnuncioAction()
{
}