// preciso de função pra verificar se ajax funciona....
// se não funcionar -> tentar enviar pedido por via normal
// - agrupar os varios elementos do endereço (host//file//dir//
// - verificar se ajax funciona
// - se sim obtem pela classe xajax
// - se não faz um pedido normal
var supportAjax=true;

function loadAjaxPage(page,target)
{
	loadingIcon(true,target);
	return xajax_mainContent(page,target);	
}

function loadAjaxForm(page,target,formValues)
{
	loadingIcon(true,target);
	return xajax_mainContent(page,target,formValues);	
}

function loadingIcon(flag,target){
	var txt = "<center style='margin-top:20px;'><img src='imgs/loading.gif' width='16' height='16' /></center>";
	var x = document.getElementById(target);
	if(flag==true)
		x.innerHTML = txt;
	else
		x.innerHTML = '';
}

function Engine() {
	//apanhar todos os elementos "a"
    var eList,i, aux;
	Engine2();
	//Lytebox.updateLyteboxItems();
    eList =  document.getElementsByTagName("a");
    for(i=0; i < eList.length; i++) {		
		if(eList[i].href.indexOf('#') >= 0){
		/**
		 *    util quando quiser forçar um pedido normal (rel="no-ajax")
		 */
		}else if (eList[i].rel.indexOf('no-ajax') >= 0){   
		}else{
			eList[i].onclick = function (e) {
				if (!supportAjax) return true; // ajax nao suportado sai
				if ( this.rel) {
					if (this.rel.indexOf('lytebox[galleryset]') >= 0 || this.rel.indexOf('lytebox') >= 0) myLytebox.start(this); return false;      
				}	
				// query ajax
				clearActive(this);
				this.className = "active";
				loadAjaxPage(this.href,this.getAttribute('tgt'));
				//xajax_mainContent(this.href);
				return false; 
			};
		}
	}
}

function Engine2() {
	//apanhar todos os elementos "input"
	//a ideia é servir todos os elementos input do tipo "submit"
    var eList,i, aux;
    eList =  document.getElementsByTagName("input");
    for(i=0; i < eList.length; i++) {
		if(eList[i].type=="submit"){
			eList[i].onclick = function (e) {
				if (!supportAjax) return true; // ajax nao suportado sai
				loadAjaxForm(this.getAttribute('url'),this.getAttribute('tgt'),xajax.getFormValues(this.getAttribute('form')))
				//xajax_mainContent(this.href);
				return false; 
			};
		}
	}
    eList2 =  document.getElementById("select_gallery");
	if(eList2){
	eList2.onchange = function (e) {
		if (!supportAjax) return true; // ajax nao suportado sai
		loadAjaxForm(this.getAttribute('url'),this.getAttribute('tgt'),xajax.getFormValues(this.getAttribute('form')))
		return false; 
	};
	}
}

function clearActive(tgt) {
    var eList,i, aux;
    eList =  document.getElementsByTagName("a");
    for(i=0; i < eList.length; i++) {		
		if(eList[i].className.indexOf('active') >= 0 && eList[i].getAttribute('tgt') == tgt.getAttribute('tgt') && eList[i].href!=tgt.href)
			eList[i].className = "";
	}
}

function clearEventContent(){
    var eList,i, aux;
    eList =  document.getElementsByTagName("div");
	for(i=0; i < eList.length; i++) {
		if(eList[i].className=="events_content")
			eList[i].innerHTML="";
	}
}

function toogle(field){
	if(xajax.$(field).style.display=="none")
		xajax.$(field).style.display="block";
	else
		xajax.$(field).style.display="none";
}

function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
			{
				//innerHTML = "ERRO";
				return false;
			}
		else
			{return true}
	}
}
// validacao dos campos - email
function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
			{return false}
		else {return true}
	}
}

// controlador de submissao
function validate_form(thisform)
{
	with (thisform)
	{
		var out = "";
		var val = true;
		// campos requeridos
		if (validate_required(f_name)==false)
		{
			val = false;
			out = out + "Nome tem de ser preenchido!\n";
		}
		if (validate_required(f_message)==false)
		{
			val = false;
			out = out + "Mensagem tem de ser preenchida!\n";
		}
		if (validate_required(f_email)==false)
		{
			val = false;
			out = out + "Email tem de ser preenchido!\n";
		}
		else
		{	// campos correctos
			if (validate_email(f_email)==false)
			{
				val = false;
				out = out + "O endereco de e-mail nao e valido!\n";
			}
		}
		// se for false tem erros, imprime os erros no ecra
		if (val == false)
		{
			alert(out);
		}
	}
	return val;
}


function submitForm()
{
	var insere = document.getElementById('contacts_form');
	var val = validate_form(insere);
	if (val)
	{
		return true;
		//insere.submit();
	}else
	{
		return false;
	}
}
