// JavaScript Document
var ultimofml = null;

function o(id)
{
	return document.getElementById(id);
}
function ValidaEmail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }else{return false;}
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){return true;}else{return false;}
    }else{
        return false;
        }
}

function isArray(obj) 
{
	if (obj.constructor.toString().indexOf("Array") == -1)
		return false;
	else
		return true;
}
function inArray(string,obj)
{
	if(isArray(obj))
	{
		for(var i=0 ; i < obj.length ; i++)
		{
			if(!string.localeCompare(obj[i]))
			{
				return true;
			}
		}
	}
	return false;
}
function alert(texto)
{
	
	texto = unescape(texto);
	texto = texto.replace(/:\n\n/g,"||");
	texto = texto.replace(/\n/g,"<br>");
	
	texto = texto.split('||');
	
	if(texto[1])
	{
		titulo = texto[0];
		o('titulo_aviso').innerHTML=titulo;
		texto = texto[1];
	}
	
	o('texto_aviso').innerHTML=texto;
	
	o('aviso_fundo').style.display='block';	
	o('aviso').style.display='block';
}
function abreLoading()
{
	AjaxLoading=true;
	o('fundo_loading').style.display='block';	
	o('loading').style.display='block';	
}
function fecharAviso()
{
	o('aviso_fundo').style.display='none';	
	o('aviso').style.display='none';	
}
function fecharLoading()
{
	AjaxLoading=false;
	o('fundo_loading').style.display='none';	
	o('loading').style.display='none';	
}

function enviaForm(form,tabela)
{
	ultimofml = form;
	
	var i = 0;
	var j = 'Campos obrigatórios:\n\n';
	var campos = new Array();
	
	
	for(var k=0;k<form.length;k++)
	{
		var campo = form[k];
		
		if(campo.getAttribute('obr')=='s')
		{
			if(campo.getAttribute('validador'))
			{
					funcao = campo.getAttribute('validador');
					
					var jsonData = eval('(' + funcao + ')');
					
					if(!jsonData(campo.value)){
						j += ++i + ')'+campo.getAttribute('label')+' \n';
					}
			}else{
				if (campo.value.length == 0) {
					j += ++i + ')'+campo.getAttribute('label')+' \n';
				}
			}
			
			
		}
		if(campo.getAttribute('ajax')=='s')
			campos[campos.length]  = campo.getAttribute('name')+'='+URLEncode(campo.value);
	}
	
	queryString = campos.join('&');
	//alert(queryString);
	
	
	
	if (i > 0) {
		alert(j);
		return false;
	} else {
		getAjax('ajax/enviaForm.php?tabela='+tabela+"&"+queryString,'respostaFormPadrao','');
		return false;
	}
	
}
function limpaFormPadrao()
{
		if(ultimofml)
		{
			ultimofml.reset();
		}
}
function respostaFormPadrao(dados)
{
	dados = unescape(dados);
	
	dados = dados.split('|*|');
	alert(dados[1]);
	
	if(dados[0]=='ok')
	{
		limpaFormPadrao();
	}
}
function verRadioCheck(obj)
{
	verificador=false;
	
	for(var i=0; i<obj.length;i++)
	{
		if(obj[i].checked)
		verificador=true;
	}
	
	return verificador;
}
function enviaFormCompra(form)
{
	var i = 0;
	var j = 'Campos obrigatórios:\n\n';
	var campos = new Array();
	
	if(form['nome'].value.length==0)
	{
		j += ++i + ')Nome \n';
	}
	if(!ValidaEmail(form['email']))
	{
		j += ++i + ')Email \n';
	}
	if(form['telefone'].value.length==0 && form['celular'].value.length==0)
	{
		j += ++i + ')Infome o Telefone ou Celular \n';
	}
	if(!verRadioCheck(form['hospedagem']))
	{
		j += ++i + ')Hospedagem \n';
	}
	if(!verRadioCheck(form['transporte']))
	{
		j += ++i + ')Transporte \n';
	}
	if(!verRadioCheck(form['almoco']))
	{
		j += ++i + ')Pacote Almoço \n';
	}
	if(form['qtd_px'].value.length==0)
	{
		j += ++i + ')Quantidade de pessoas \n';
	}
	if(!verRadioCheck(form['pagamento[]']))
	{
		j += ++i + ')Preferência por pagamento \n';
	}
	
	if (i > 0) {
		alert(j);
		return false;
	} else {
		return true;
	}
	
}