﻿var havar1 = 1;
var havar2 = 1;
var havar3 = 1;
var havar4 = 1;
var havar5 = 1;

var ArrayVarsUser = new Array();
var ContadorVarsUser = 0;

var txt_label_telef1 = '';
var txt_label_telef2 = '';

var js_usa_confirm = 0;

function e_br(indice) {
	if(indice == 30) {
		document.getElementById('linha_ddd').style.display='inline';
		document.getElementById('txt_ddd').innerHTML=txt_label_telef2;
		document.getElementById('telef').style.width='109px';
	}
	else {
		document.getElementById('linha_ddd').style.display='none';
		document.getElementById('txt_ddd').innerHTML=txt_label_telef1;
		document.getElementById('telef').style.width='149px';
	}
}

function isValidEmail(str) {
	var at="@";
	var dot=".";
	var lat = str.indexOf(at);
	var lstr = (str.length) - 1;
	var ldot = str.indexOf(dot);
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) { return false }
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) { return false }
	if (str.indexOf(at,(lat+1))!=-1) { return false }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) { return false }
	if (str.indexOf(dot,(lat+2))==-1) { return false }
	if (str.indexOf(" ")!=-1) { return false }
	return true					
}
function isNumeric(sText){
   var ValidChars = "0123456789 +";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
function isDDD(sText){
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	var iaux = 0;
	var jaux = 1;
	var total = 0;
 
	if(sText.length > 3) {
		IsNumber = false;
	}
	else {
		for (i = 0; i < sText.length && IsNumber == true; i++) {
			Char = sText.charAt(i);
			if (ValidChars.indexOf(Char) == -1) {
				IsNumber = false;
			}
		}
		
		if(IsNumber) {
			for (i = (sText.length - 1); i >= 0; i--) {
				Char = sText.charAt(i);
				iaux = parseInt(Char);
				total += iaux*jaux;
				jaux = jaux*10;
			}
			if(total<10 || total>999) {
				IsNumber = false;
			}
		}
	}
	
	return IsNumber;
}
function submeter(f) {
	var res = true;
	var alerta = "";
	var auxdddval;
	var iaux;
	
	if (submitted) {
		alerta = "Os dados já foram submetidos. Aguarde por favor.";
		res = false;
	}
	else {
		if(havar1 == 1) {
			if (f.nome.value=="") {
				res = false;
				alerta = "Tem de inserir o Primeiro Nome.\n";
			}
		}
		if(havar2 == 1) {
			if (f.apelido.value=="") {
				res = false;
				alerta = alerta + "Tem de inserir o Último Nome.\n";
			}
		}
		if (f.email.value=="") {
			res = false;
			alerta = alerta + "Tem de inserir o endereço de Email.\n";
		}
		else {
			if (!isValidEmail(f.email.value)) {
				res = false;
				alerta = alerta + "Tem de inserir um endereço de Email válido.\n";
			}	
		}
		if(havar3 == 1) {
			if (f.pais.value==0) {
				res = false;
				alerta = alerta + "Tem de escolher um país.\n";
			}
		}
		if(havar4 == 1) {
			if(f.telef.value=="") {
				res = false;
				alerta = alerta + "Tem de inserir o nº de telefone.\n";
			}
			else {
				if (!isNumeric(f.telef.value)) {
					res = false;
					alerta = alerta + "Tem de inserir um nº de telefone válido.\n(utilize apenas os caracteres '+0123456789' ou o espaço)\n";
				}
				else {
					if(f.pais.options[f.pais.selectedIndex].value == 30) {
						if(typeof f.ddd != 'undefined') {
							if(f.ddd.value=="") {
								res = false;
								alerta = alerta + "Tem de inserir o código DDD.\n";
							}
							else {
								auxdddval = f.ddd.value;
								if(!isDDD(auxdddval)) {
									res = false;
									alerta = alerta + "Tem de inserir um código DDD válido.\n(valor numérico entre 10 e 999)\n";
								}
							}
						}
					}
				}
			}
		}
		if(havar5 == 1) {
			if(f.operadora.value=="") {
				res = false;
				alerta = alerta + "Tem de inserir a operadora.\n";
			}
		}
		for(iaux = 0; iaux < ContadorVarsUser; iaux++) {
			if(document.getElementById(ArrayVarsUser[iaux][0]).value == "") {
				res = false;
				alerta = alerta + "Tem de inserir o campo " + ArrayVarsUser[iaux][1] + ".\n";
			}
		}
		if(js_usa_confirm == 1) {
			if(document.getElementById("confirmado").checked == false) {
				res = false;
				alerta = alerta + "\nTem de seleccionar a caixa de confirmação para poder prosseguir.\n";
			}
		}
	}
	if(res) {
		if(havar4 == 1) {
			if(typeof f.ddd != 'undefined') {
				if(f.ddd.value != "" && f.pais.options[f.pais.selectedIndex].value == 30) {
					f.telef.value = "(" + f.ddd.value + ") " + f.telef.value;
				}
			}
		}
		submitted = 1;
		f.submit();
	}
	else {
		alert(alerta);
	}
}

