// Base

// Verify field is empty, false focus it
function isEmpty ()
{
	
	var argv = isEmpty.arguments;

	if (argv[2]=='' || argv[2]==null)
	{
		argv[2]=0;
	}

	x = document.forms[argv[2]].elements[argv[0]];

	if (x == undefined || x.value == '0' || x.value == '')
	{
    	alert ('Por favor preencha o campo "' + argv[1] + '"');
		x.focus();
	    return false;
	}

	return true;
}

//verifies field is numeric
function isNumeric (field, name, formz)
{
	x = document.forms[formz].elements[field];
	if (x.value == "")
	{
		return true;
	}
	
	if (x.value.match (/[^\d^\.]/))
	{
		alert("Por favor preencha o campo " + name + " com um valor numérico!");
        x.focus();
		return false;
	}

	return true;
}

function isEmail ()
{

	var argv = isEmail.arguments;

	if (argv[2]=='' || argv[2]==null)
	{
		argv[2]=0;
	}

	email = document.forms[argv[2]].elements[argv[0]];
	valid = /^.*\@.*\..*$/i;

	if (email.value.search (valid) == -1)
	{
		alert("Por favor preencha correctamente o campo " + argv[1] + "!");
		email.focus();
		return false;
	}
	return true;
}

function isSelected (field, name)
{
	x = document.forms[0].elements[field];
	
	if ( x.selectedIndex == -1 )
	{
		alert ('Por favor preencha o campo "' + name + '"');
		x.focus();
		return false;
	}

	value = x.options[x.selectedIndex].value;
	
	if (value == undefined || value == '0' || value == '')
	{
		alert ('Por favor preencha o campo "' + name + '"');
		x.focus();
		return false;
	}
	
	return true;
}



function pop(pagina)
{
    window.open(pagina, 'pop', 'width=400,height=200');
}

// verify if field is a valid phone number
function isPhoneNumber (field, name)
{
  x = document.forms[0].elements[field];
	if (x.value == "") return true;
  if (x.value.match (/[^\d^\.^\(^\)^\+^\s]/)) {
		alert("Por favor preencha o campo " + name + " com um numero de telefone válido!");
		return false;
	}
	return true;
}

// verifies if a field have three numbers
function isThreeNumbers(field, name, longitud, formz)
{
	aaa = document.forms[formz].elements[field].value;
	if (aaa.length == 0)
	{
		return true;
	} 
	else if (aaa.length!=longitud)
	{
		alert("Por favor preencha o campo " + name + " com um codigo válido!");
		x.focus();
		return false;
	}
	return true;
}

//Copy data between fields
function deliveryData ()
{
	/*if(document.forms[2])
		var formz = 2;
	else*/
		var formz = 0;
	
	x = document.forms[formz];
	x.elements['delivery[name]'].value = x.elements['client[name]'].value;
	x.elements['delivery[address]'].value = x.elements['client[address]'].value;
	x.elements['delivery[city]'].value = x.elements['client[city]'].value;
	x.elements['delivery[postCode1]'].value = x.elements['client[postCode1]'].value;
	x.elements['delivery[postCode2]'].value = x.elements['client[postCode2]'].value;
	//x.elements['delivery[idCountry]'].selectedIndex = x.elements['client[idCountry]'].selectedIndex;
	//x.elements['delivery[PCLocal]'].value = x.elements['client[PCLocal]'].value;
}

function check_form_elements() 
{
	/*if(document.forms[2])
		var formz = 2;
	else*/
		var formz = 0;
	//	alert(document.forms[2]);

	if (document.forms[formz].email.value) 
	{ 
		if (isEmpty('password', 'Password', formz)) 
		{
			document.forms[formz].action = 'step1.php'; return true; 
		} 
		return false; 
	} 
	else 
	{ 
		if (isEmpty('client[name]', 'Nome', formz) && 
		isNumeric('client[contributorNumber]', 'Nr.Contribuinte', formz) && 
		isEmpty('client[address]', 'Morada', formz) && 
		isEmpty('client[city]', 'Localidade', formz) && 
		isEmpty('client[postCode1]', 'Código Postal', formz) && 
		isNumeric('client[postCode1]', 'Código Postal', formz) && 
		isThreeNumbers('client[postCode1]', 'Código Postal (4 dígitos)', 4, formz) && 
		isNumeric('client[postCode2]', 'Código Postal', formz) && 
		isThreeNumbers('client[postCode2]', 'Código Postal 2 (3 dígitos)', 3, formz) /*&& 
		isSelected('client[idCountry]', 'País')*/ && 
		isEmpty('client[email]', 'Email', formz) && 
		isEmail('client[email]', 'Email', formz) &&
		isEmpty('delivery[name]', 'Nome', formz) &&
		isEmpty('delivery[address]', 'Morada', formz) &&
		isEmpty('delivery[city]', 'Localidade', formz) &&
		isEmpty('delivery[postCode1]', 'Código Postal', formz) &&
		isNumeric('delivery[postCode1]', 'Código Postal', formz) &&
		isThreeNumbers('delivery[postCode1]', 'Código Postal (4 dígitos)', 4, formz) &&
		isEmpty('delivery[postCode2]', 'Código Postal', formz) &&
		isNumeric('delivery[postCode2]', 'Código Postal', formz) &&
		isThreeNumbers('delivery[postCode2]', 'Código Postal (3 dígitos)', 3, formz)
		)
		{ 
			return true; 
		}
	}
	return false; 
} 

function xek_contact_form(form_num)
{
	/*if(form_num == 0)
		var formz = 0;
	else if(document.forms[3])
		var formz = 2;
	else
		var formz = 1;*/

	var formz = form_num;

	if( isEmpty('nome', 'Nome', formz) &&
		isEmpty('email', 'Email', formz) && 
		isEmail('email', 'Email', formz) &&
		isEmpty('mensagem', 'Mensagem', formz)
	)
	{
		return true;
	}

	return false;
}

function xek_appointment_form(form_num)
{
	/*if(form_num == 0)
		var formz = 0;
	else if(document.forms[3])
		var formz = 2;
	else
		var formz = 1;*/

	var formz = form_num;

	if( isEmpty('nome', 'Nome', formz) &&
		isEmpty('email', 'Email', formz) && 
		isEmail('email', 'Email', formz) &&
		isEmpty('data', 'Data da Visita', formz) &&
		isEmpty('nr_alunos', 'Nº Alunos', formz) &&
		isEmpty('nr_acompanhantes', 'Nº Acompanhantes', formz) &&
		isEmpty('assunto', 'Assunto', formz) &&
		isEmpty('mensagem', 'Mensagem', formz)
	)
	{
		return true;
	}

	return false;
}

function xek_contact_form2()
{
	if( isEmpty('nome_from', 'Nome', 0) &&
		isEmpty('email_from', 'Email', 0) && 
		isEmail('email_from', 'Email', 0) &&
		isEmpty('nome_to', 'Nome do amigo', 0) &&
		isEmpty('email_to', 'Email do amigo', 0) && 
		isEmail('email_to', 'Email do amigo', 0)

	)
	{
		return true;
	}

	return false;
}

function xek_register()
{
	if( isEmpty('nome', 'Nome', 0) &&
		isEmpty('email', 'Email', 0) && 
		isEmail('email', 'Email', 0)
	)
	{
		return true;
	}

	return false;
}

function xek_recover()
{
	if( isEmpty('email', 'Email', 0) && 
		isEmail('email', 'Email', 0) &&
		isEmpty('pass1', 'Password', 0) &&
		isEmpty('pass2', 'Password', 0)
	)
	{
		return true;
	}

	return false;
}

function xek_comment()
{
	if( isEmpty('title', 'Titulo', 0) &&
		isEmpty('comment', 'Comentário', 0)
	)
	{
		return true;
	}

	return false;
}

function xek_newsletter()
{
	if( isEmpty('email', 'Email', 1) &&
		isEmail('email', 'Email', 1)
	)
	{
		return true;
	}

	return false;
}
