﻿// JavaScript Document
var listEmails = new Array("adm.ua.pt","cc.isel.ipl.pt","cc.isel.ipl.pt","cc.isel.ipl.pt","civil.ist.utl.pt","dei.isep.ipp.pt","dei.uc.pt","dei.uep.ipp.pt","dei.vc.pt","det.ua.pt","di.estv.ipv.pt","di.fc.ul.pt","di.fct.unl.pt","di.ubi.pt","di.uminho.pt","dsi.uminho.pt","eseb.ipbeja.pt","esgs.pt","est.ipcb.pt","est.ips.pt","estg.iplei.pt","estg.ipleiria.pt", "estg.ipvc.pt","estgad.pt","estig.ipbeja.pt","fe.up.pt","forino.pt","gsi.inesc-id.pt","ieeta.pt","iesec.pt","ipa.univ.pt", "ipb.pt", "ipca.pt", "ipg.pt", "ipp.pt", "ipt.pt", "iscte.pt", "isec.pt", "isegi.unl.pt", "isel.ipl.pt", "isel.pt", "isep.ipp.pt", "islasantarem.pt", "mail.isec.pt","mail.uportu.pt","mail.vng.isla.pt","mat.ua.pt","uni.pt","notes.uac.pt","post.estgp.pt","tagus.ist.utl.pt", "terra.estgp.pt","ua.pt","ual.pt","ualg.pt", "uevora.pt", "ufp.pt", "ulusofona.pt","uma.pt","uninova.pt","univ-ab.pt","uportu.pt","upt.pt","utad.pt","uc.pt","hotmail.com");
function validMail( mail ){
    var mail_ = mail.split('@');
    for( i = 0; i < listEmails.length; i++){
        if(mail_[1] == listEmails[i])
            return true;
    }
    //alert(listEmails);
    return false;
}
function isNIF(valor)
{
	//x = obj;
	//valor = obj.value;

	if(valor.length == 9)
	{
		var chars = new Array();
		chars = valor.split('');
		c = chars[0];

		if(c == '1' || c == '2' || c == '5' || c == '6' || c == '9')  //digitos iniciais válidos
		{
			checkDigit = (c - '0') * 9;

			for(i = 2 ; i <= 8 ; i++)
				checkDigit += (chars[i - 1] - '0') * (10 - i);

			checkDigit = 11 - (checkDigit % 11);
		
			if(checkDigit >= 10)
				checkDigit = 0;
			
			if(checkDigit == (chars[8] - '0'))
				return true;
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
	return false;
}

function isNIB (number) {
	if (checkibancore("pt50"+number) != "1") {
		return false;
	}
	else {
		return true;
	}
}

function isEmail (string) {
	var reg = new	RegExp("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3}$");
	if (reg.test(string)) {
		return true;	
	}
	else {
		return false;	
	}
}

function isEmpty (string) {
	if (string == "") {return false;}
	else {return true;}
}
function ifNameSurname( string ){

    if(string.split(' ').length > 1)
        return true;
    else
        return false;
}
function isNumber (number) {
	if (number.match(/[^\d^\.]/)) {return false;}
	else {return true;}
}
function hasLength (string, lmin, lmax) {
	if (lmin == lmax) {
		if (string.length == lmin) {
			return true;	
		}
		else {
			return false;	
		}
	}
	if (string.length <= lmin || string.length >= lmax) {
		return false;
	}
	else {
		return true;
	}
}
function isGreaterThan (value, v_compare) {
	if (value > v_compare) {return true;}
	else {return false;}
}
function isSmallerThan (value, v_compare) {
	if (value < v_compare) {return true;}
	else {return false;}
}
function startsWith (value1, value2) {
	var tam = value2.length;
	//alert('tam='+tam);
	//alert('valor='+value1.substring(0,tam));
	if (value1.substring(0,tam) != value2) {
		return false;
	}
	else {
		return true;
	}
}
function dropDown(target){
    var contents = document.getElementsById(target);
    alert('ok');
    return false;
}
function radioCheck(target) {
	//alert(target);
	var contents = document.getElementsByName(target);
	//alert(document.getElementsByName(target));
	//alert(contents.length);
	for (var i=0; i<contents.length; i++) {
		//alert(contents[i].id+" - "+contents[i].checked);
		if(contents[i].checked) {
			return true;
		}
	}
	return false;
}
function compareEmptyWith (id1, id2) {
	if (isEmpty(getValue(id1)) || isEmpty(getValue(id2))) {
		//alert("true");
		return true;	
	}
	else {
		//alert("false");
		return false;
	}
}
function comparePhones (id1, id2) {
	if (isEmpty(getValue(id1)) || isEmpty(getValue(id2))) {
		if (isNumber(getValue(id1)) || isNumber(getValue(id2))) {
			if (hasLength(getValue(id1), 9, 9) || hasLength(getValue(id2), 9, 9)) {
				return true;	
			}
			else {return false;}
		}
		else {return false;}
	}
	else {
		return false;
	}	
}
function getValue (id) {
	return document.getElementById(id).value;	
}

function isChecked (id) {
	return document.getElementById(id).checked;
}

function focusElem (id) {
	if(document.getElementById(id)) {
		document.getElementById(id).focus()
	}
}

function generalReturn (field, msgStr) {
	alert('Por favor "'+msgStr+'"');
	focusElem (field);
	return false;
}
/**/
/**/
function startW( s ){ 
	var el = document.getElementById(s);
	
	if(el.value == "") return false;
	if(!isNumber(el.value))return false;
	if(el.value.length != 9) return false;
	if(startsWith(el.value, '2')) return true;
	else if(startsWith(el.value, '91')) return true;
	else if(startsWith(el.value, '92')) return true;
	else if(startsWith(el.value, '93')) return true;
	else if(startsWith(el.value, '96')) return true;
	else return false;
	alert(el.value);

}
function formVer(){
	
	var name = ifNameSurname(document.getElementById('name').value);
	var email=isEmail(document.getElementById('email').value);
	var telefone=startW('phone');
	var instituicao =isEmpty(document.getElementById('instituicao').value);
	var n_aluno = isEmpty(document.getElementById('n_aluno').value);
	//var cod_promo = isEmpty(document.getElementById('cod_promo').value);
	
	//alert(isEmpty(document.getElementById('n_aluno').value));
	//alert(name + ' ' +email+' '+telefone+' '+instituicao+' '+n_aluno);
	if(!name){
		alert('Insere o primeiro e último nome');
		return false;
	}
	else if(!email ){
		alert('Por favor utiliza o endereço de e-mail válido');
		return false;
	}
	else if(!isEmail( document.getElementById('email').value ) ){
		alert('Por favor utiliza o endereço de e-mail válido.' );
		return false;
	}
	else if(!telefone){
		alert('Introduza um numero de Telefone válido');
		return false;
	}
	else if(!instituicao){
		alert('Introduza a instituição');
		return false;
	}
	else if(!n_aluno){
	    
	    alert('Insira nº de aluno');
	    return false;
	}
	/*else if(!cod_promo){
	    
	    alert('Insira código promocional');
	    return false;
	}*/
	else return true;
}
