//	cadastro
var cidades;
function cidades(campo, valor) {
	if(window.XMLHttpRequest) {
		cidades	= new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		cidades = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "/includes/cidades_ajax.php?campo="+campo+"&valor="+valor;
	cidades.open("Get", url, true);
	cidades.onreadystatechange = function() {

	if(cidades.readyState == 1) {
			document.getElementById('campo_' + campo + '').innerHTML = '<font color="gray"><img src="/imagens/site/carregando.gif">Carregando cidades...</font>';
		}
		if(cidades.readyState == 4 && cidades.status == 200) {
		var resposta = cidades.responseText;
		document.getElementById('campo_'+ campo +'').innerHTML = resposta;
		}
	}
	cidades.send(null);
}



/*    requisicoes em geral       */

   var geral;
   function ajax(campo, valor1, valor2, valor3, valor4, pagina) {
   	if(window.XMLHttpRequest) {
		geral	= new XMLHttpRequest();
	}
       else if(window.ActiveXObject) {
		geral = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "/includes/ajax.php?campo="+campo+"&valor1="+valor1+"&valor2="+valor2+"&valor3="+valor3+"&valor4="+valor4+"&pagina="+pagina;
	geral.open("Get", url, true);
	geral.onreadystatechange = function() {
		if(geral.readyState == 1) {
			document.getElementById('campo_' + campo + '').innerHTML = '<font color="gray"><img border="0" src="/scripts/graphics/loader.white.gif">&nbsp;Aguarde...</font>';
		}
		if(geral.readyState == 4 && geral.status == 200) {
			var resposta = geral.responseText;
			document.getElementById('campo_'+ campo +'').innerHTML = resposta;
		}
	}
	geral.send(null);
      }


/**
* Função que contém chamadas a funções
* que serão feitas no evento onload do window
*/
function doOnLoad(){
	//Adiciona função de validação aos forms
	adicionarValidacaoForms();
}

/**
* Função que adiciona ao evento onsubmit de todos os formulários
* do documento, a função de validação de formulário "validaForm()"
*
*/
function adicionarValidacaoForms(){
	var forms = document.forms;
	for ( var i = 0; i < forms.length; i++ ){
		forms[i].onsubmit = validaForm;
	}
}


/**
* Função que valida os campos obrigatórios num formulário.
* Para que um campos seja obrigatório, o valor do seu atributo
* 'title' deve começar com um asterísco (*)
*
* @return boolean
*/
var validaForm = function(){
	var frm = this;
	for (var i = 0; i < frm.elements.length; i++){
		if ( (frm.elements[i].title.substr(0,1)) == "*" ){
			if (frm.elements[i].value == ""){
				alert("Atenção! um campo '"+frm.elements[i].title.substr(1,(frm.elements[i].title.length))+"'precisa ser informado.");
				frm.elements[i].style.backgroundColor = "#D7FFD7";
				frm.elements[i].focus();
				return false;
				break;
			}else{
				frm.elements[i].style.backgroundColor = "#ffffff";
			}
		}
	}
	return true;
}

// Atribui ao evento onload da janela a função doOnLoad.
// OBS: Desta forma estamos trabalhando com javascript não-obstrutivo.
window.onload = doOnLoad;





// PARA REFLEXAO DE IMAGENS

var searchterm;
var searchpage=1;
var a;
var reflecting;
var lasturl;
var loaded = false;
var nativeWidth;

function reflectImage(url) {
	image = new Image();
	loaded = false;
	image.src = url;
	lasturl = url;

	$('imageresult').innerHTML = '';

	image.onload = function() {
		if (!loaded) {
			height = 80/this.height;
			if (height > 0.5) {
				height = 0.5;
			}

			nativeWidth = this.width;
			nativeHeight = this.height;

			$('popacity').value = 33;
			$('pheight').value = Math.round(height*100);
			$('pwidth').value = this.width;

			$('imagehtml').innerHTML = '<strong>Add to webpage</strong><br />Install <a href="http://cow.neondragon.net/stuff/reflection/">reflection.js</a> and then you can add this reflection to your webpage using the following code:<br /><span class="code">&lt;img src="'+image.src+'" class="reflect rheight'+$('pheight').value+' ropacity33" width="'+this.width+'" /&gt;</span><br /><br /><strong><a href="http://cow.neondragon.net/stuff/reflection/reflectomatic/?url='+encodeURIComponent(image.src)+'">Link to this</a></strong>';

			Reflection.add(this, { height: height, opacity: 1/3 });
			a.display(3);
			loaded = true;
		}
	}

	$('reflectproperties').style.display = 'block';
	$('imageresult').appendChild(image);
	$('imagehtml').style.paddingBottom = '30px';

	if ((document.all || window.opera) && image.complete) {
		image.onload();
	}

	return false;
}

function adjustReflection() {
	$('imageresult').innerHTML='';
	loaded=false;

	image = new Image();
	image.src = lasturl;

	image.onload = function() {
		if (!loaded) {
			if (this.height > 80) {
				height = 80/this.height;
			} else {
				height = 1;
			}

			if (!$('pwidth').value) {
				$('pwidth').value = nativeWidth;
			}

			opacity = $('popacity').value/100;
			height = $('pheight').value/100;
			image.width = $('pwidth').value;
			image.height = $('pwidth').value/nativeWidth * nativeHeight;

			$('imagehtml').innerHTML = '<strong>Add to webpage</strong><br />Install <a href="http://cow.neondragon.net/stuff/reflection/">reflection.js</a> and then you can add this reflection to your webpage using the following code:<br /><span class="code">&lt;img src="'+image.src+'" class="reflect rheight'+$('pheight').value+' ropacity'+$('popacity').value+'" width="'+this.width+'" /&gt;</span><br /><br /><strong><a href="http://cow.neondragon.net/stuff/reflection/reflectomatic/?url='+encodeURIComponent(image.src)+'&amp;height='+encodeURIComponent($('pheight').value)+'&amp;opacity='+encodeURIComponent($('popacity').value)+'&amp;width='+encodeURIComponent($('pwidth').value)+'">Link to this</a></strong>';

			Reflection.add(this, { height: height, opacity: opacity });
			a.display(0);
			a.display(3);
			loaded = true;
		}
	}

	$('reflectproperties').style.display = 'block';
	$('imageresult').appendChild(image);
	$('imagehtml').style.paddingBottom = '30px';

	if ((document.all || window.opera) && image.complete) {
		image.onload();
	}
}

function reflectFromURL(url) {
	if (url.substring(0,4) != "http") {
		url = "http://"+url;
	}

	reflectImage(url);
}

function getYahooImages(jsonData) {
	imageresults = '<p>Image search results '+((searchpage-1)*10+1)+' to '+((searchpage)*10+1)+' for <strong>'+searchterm.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')+'</strong> - <a href="javascript:searchImages(\''+searchterm.replace(/\'/g,'\\\'')+'\', '+(searchpage+1)+')">Next &raquo;</a></p><table><tr>';
	for (i=0;i<10;i++) {
		j = new Image();
		j.src=jsonData.ResultSet.Result[i].Thumbnail.Url;

		if (i==5) {
			imageresults += '</tr><tr>';
		}

		imageresults += '<td><a href="#" onclick="javascript:reflectImage(\''+jsonData.ResultSet.Result[i].ClickUrl+'\');return false;"><img src="'+jsonData.ResultSet.Result[i].Thumbnail.Url+'" alt="'+escape(jsonData.ResultSet.Result[i].Title)+'" onload="Reflection.add(this, {height: 1/3, opacity: 1/3})" /></a></td>';
	}
	imageresults += '</tr></table><p>Click on the image to load it into reflect-o-matic.</p>';

	document.getElementById('searchresults').innerHTML = imageresults;
	document.getElementById('searchresults').style.visibility = 'visible';
}

function searchImages(term,page) {
	$('searchresults').style.height = '500px';

	a.previous=0;
	a.display(1);

	document.getElementById('imagesearch').value = term;

	if (page) {
		start = 10*(page-1)+1;
		searchpage = page;
	} else {
		start = 1;
		searchpage=1;
	}

	// The web service call
	var req  = 'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=JwTGwmPV34F1OcZJEFEBrMNqkeWfrGdg7ReMlrBu8CCXBYvMOW_dj.DiK3XLyEWGFA--&query='+encodeURIComponent(term)+'&start='+start+'&output=json&callback=getYahooImages';
	searchterm = term;

	// Create a new request object
	bObj = new JSONscriptRequest(req);
	// Build the dynamic script tag
	bObj.buildScriptTag();
	// Add the script tag to the page
	bObj.addScriptTag();
}


