// JavaScript Document
function ValidateEmail(strMail) 
{       

	//var strMail = ctrl.value
	var regMail =  /^\w+([-.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

	if (regMail.test(strMail))
	{
		return true;
	}
	else
	{
		return false;
	}

}

function submeterNewsletter () {
 
 	/* validar se os campos estão preenchidos */
	msgerro = "";
 	email   = $('#news_email').val();
	
	if (email == '') { 
		msgerro+= 'Tem de introduzir o seu email.\r\n';
	}

	retvalemail = ValidateEmail(email);
	if((!retvalemail)&&(email != '')) {
		msgerro+= '\r\nTem de introduzir um email válido';
	}
	/*alert(msgerro.length);*/
	if(msgerro.length > 0){

		alert( html_entity_decode('Foram detectados erros de valida&ccedil;&atilde;o:\r\n\r\n'+msgerro) );
		return;
	}
	else
	{
		$('#cb_ajaxwait').hide(); 	
	}
 	/*validar se os campos estão preenchidos */	
	$('#cb_ajaxwait').show();
	
	//var params = $("#form_contact").serialize();
	var url  = '/includes/ajax_newsletter.inc.php';
	var pars = 'newsletter_action=subscribe&idnews=1&nome=&email='+email;
	
	$.ajax({ 
		type: 'POST',
		url: url, 
		data: pars,
		/*contentType: "charset=utf-8",*/
		/*context: document.body, */
		success: function(data){				
			alert(data);
			//$('#submit_contact').removeAttr('onclick');
		}
	});
 
 return false;
}

function submeterContacto (form, actionCase) {
 
 	/* validar se os campos estão preenchidos */
	msgerro = "";
	
	if (form == 'encomenda') {

 	nome     = $('#nome').val();
 	email    = $('#email').val();
	telf     = $('#telefone').val();
	morada   = $('#morada').val();
 	produto  = $('#produto').val();
	qtd      = $('#quantidade').val();		
	
	//alert("nome:"+nome+"\r\n Email:"+email+"\r\n Obs:")	
	if (nome == '') { 
		msgerro = 'Tem de introduzir o seu nome.\r\n';
	}
	if (email == '') { 
		msgerro+= 'Tem de introduzir o seu email.\r\n';
	}
	if (telf == '') { 
		msgerro+= 'Tem de introduzir o seu telefone.\r\n';
	}	
	if (morada == '') { 
		msgerro+= 'Tem de introduzir o sua morada.\r\n';
	}		
	if (produto == '') { 
		msgerro+= 'Tem de introduzir o produto / variante que deseja encomendar.\r\n';
	}
	if (qtd == '') { 
		msgerro+= 'Tem de introduzir a quantidade.\r\n';
	}		
		
		
	} else {
		
 	nome  = $('#nome').val();
 	email = $('#email').val();
 	obs   = $('#descricao').val();	
	
	//alert("nome:"+nome+"\r\n Email:"+email+"\r\n Obs:")	
	if (nome == '') { 
		msgerro = 'Tem de introduzir o seu nome.\r\n';
	}
	if (email == '') { 
		msgerro+= 'Tem de introduzir o seu email.\r\n';
	}
	if (obs == '') { 
		msgerro+= 'Tem de introduzir o seu motivo de contacto.\r\n';
	}
	
	}
		
	retvalemail = ValidateEmail(email);
	if((!retvalemail)&&(email != '')) {
		msgerro+= '\r\nTem de introduzir um email válido';
	}
	/*alert(msgerro.length);*/
	if(msgerro.length > 0){
		//$('#cb_ajaxwait').html('Foram detectados diversos erros da valida&ccedil;&atilde;o:<br /><br />'+msgerro); 
		//$('#cb_ajaxwait').show(); 
		alert( html_entity_decode('Foram detectados diversos erros de valida&ccedil;&atilde;o:\r\n\r\n'+msgerro) );
		return;
	}
	else
	{
		$('#cb_ajaxwait').hide(); 	
	}
 	/*validar se os campos estão preenchidos */	
	
	$('#cb_form').hide();
	$('#cb_ajaxwait').show();
	
	var params = $("#form_contact").serialize();
	var url  = '/includes/ajax_sendform.inc.php';
	var action = (actionCase)? actionCase : 'send';
	var pars = 'action='+action+'&'+params;
	
	$.ajax({ 
		type: 'POST',
		url: url, 
		data: pars,
		/*contentType: "charset=utf-8",*/
		/*context: document.body, */
		success: function(data){				
			$('#cb_ajaxwait').html(data);
			$('#submit_contact').removeAttr('onclick');
		}
	});
 
 return false;
}

function submeterRecrutamento (form, actionCase) {
 
 	/* validar se os campos estão preenchidos */
	msgerro = "";
 	nome  = $('#nome').val();
 	email = $('#email').val();
	telf  = $('#telf').val();
 	obs   = $('#apresentacao').val();
	cv    = $('#cv_url').val();
	
	//alert("nome:"+nome+"\r\n Email:"+email+"\r\n Obs:")	
	if (nome == '') { 
		msgerro = 'Tem de introduzir o seu nome.\r\n';
	}
	if (email == '') { 
		msgerro+= 'Tem de introduzir um endere&ccedil;o de Email.\r\n';
	}
	if (telf == '') { 
		msgerro+= 'Tem de introduzir o seu Telefone.\r\n';
	}
	if (cv == null) { 
		msgerro+= 'Tem de anexar o seu CV.\r\n';
	}
	if (obs == '') { 
		msgerro+= 'Tem de introduzir a sua breve apresenta&ccedil;&atilde;o.\r\n';
	}
		
	retvalemail = ValidateEmail(email);
	if((!retvalemail)&&(email != '')) {
		msgerro+= '\r\nTem de introduzir um email valido';
	}
	/*alert(msgerro.length);*/
	if(msgerro.length > 0){
		//$('#cb_ajaxwait').html('Foram detectados diversos erros da valida&ccedil;&atilde;o:<br /><br />'+msgerro); 
		//$('#cb_ajaxwait').show(); 
		alert( html_entity_decode('Foram detectados diversos erros de valida&ccedil;&atilde;o:\r\n\r\n'+msgerro) );
		return
	}
	else
	{
		$('#cb_ajaxwait').hide(); 	
	}
 	/*validar se os campos estão preenchidos */	
 
    $('#cb_form').hide();
	$('#cb_ajaxwait').show();
	
	var params = $("#form_contact").serialize();
	var url  = '/includes/ajax_sendform.inc.php';
	var action = (actionCase)? actionCase : 'send';
	var pars = 'action='+action+'&'+params;
	/*
	var formArr = $("#form_contact").serializeArray();
	jQuery.each(formArr , function(i, field) {
	  formArr[i].value = $.trim(field.value);
	});
	*/
	
	$.ajax({ 
		type: 'POST',
		url: url, 
		data: pars,
		/*contentType: "charset=utf-8",*/
		/*context: document.body, */
		success: function(data){				
			$('#cb_ajaxwait').html(data);
			$('#submit_contact').removeAttr('onclick');
		}
	});
 
 return false;
}

function html_entity_decode(str){
/*Firefox (and IE if the string contains no elements surrounded by angle brackets )*/
try{
var ta=document.createElement("textarea");
ta.innerHTML=str;
return ta.value;
}catch(e){};
/*Internet Explorer*/
try{
var d=document.createElement("div");
d.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
if(typeof d.innerText!="undefined")return d.innerText;/*Sadly this strips tags as well*/
}catch(e){}
}
