// 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 corretamente 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[3])
    
	if(document.forms[2])
		var formz = 2;
	else
		var formz = 3;
    if(document.forms[3].name == "formulario")
        var formz = 3;
        
	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() 
{

    //alert(document.forms[3].name);
	if(document.forms[2])
		var formz = 2;
	else
		var formz = 3;
    if(document.forms[3].name == "formulario")
        var formz = 3;

	//alert(document.forms[formz].email);
         
         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) && 
		        isEmpty('client[contributorNumber]', 'Nr.Contribuinte', 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[phone]','Telefone',formz ) && isNumeric('client[phone]','Telefone',formz ))&&
                (isEmpty('client[cellularPhone]','Telemovel',formz ) && isNumeric('client[cellularPhone]','Telemovel',formz ))&& 
		        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))
		    { 
                //alert('ok');
			    document.forms[formz].action = 'step2.php'; 
                return true; 
		    }
	    }
	    return false;
     
} 


function check_form_elements2() 
{
	if(document.forms[2])
		var formz = 2;
	else
		var formz = 1;
	//	alert(document.forms[2]);

	if (document.forms[formz].email.value) 
	{ 
		if (isEmpty('password', 'Password', formz)) 
		{
			document.forms[formz].action = 'step_c.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()
{
	if(document.forms[1])
		var formz = 1;
	else
		var formz = 2;

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

	return false;
}

function xek_cc_formular()
{
	if(document.forms[1])
		var formz = 1;
	else
		var formz = 2;

	if( isEmpty('f_credit[nome]', 'Nome', formz) &&
		isEmpty('f_credit[birth_date]', 'Data Nasc.', formz) &&
		isEmpty('f_credit[naturalidade]', 'Naturalidade', formz) &&
		isEmpty('f_credit[contributorNumber]', 'N.Contribuinte', formz) &&
		isEmpty('f_credit[contributorNumber]', 'N.Contribuinte', formz) &&
		isEmpty('f_credit[bi]', 'B.I.', formz) &&
		isEmpty('f_credit[aut_residencia]', 'Aut.Residência', formz) &&
		isEmpty('f_credit[nacionalidade]', 'Nacionalidade', formz) &&
		isEmpty('f_credit[estado_civil]', 'Estado Civil', formz) &&
		isEmpty('f_credit[filhos]', 'Nº Filhos', formz) &&
		isEmpty('f_credit[morada]', 'Morada', formz) &&
		isEmpty('f_credit[localidade]', 'Localidade', formz) &&
		isEmpty('f_credit[cod_postal]', 'Cod. Postal', formz) &&
		isEmpty('f_credit[habitacao]', 'Habitação', formz) &&
		isEmpty('f_credit[desde]', 'Desde', formz) &&
		isEmpty('f_credit[tel_dom]', 'Tel.Dom.', formz) &&
		isEmpty('f_credit[profissao]', 'Profissão', formz) &&
		isEmpty('f_credit[empresa]', 'Empresa', formz) &&
		isEmpty('f_credit[desde2]', 'Desde', formz) &&
		isEmpty('f_credit[morada2]', 'Morada', formz) &&
		isEmpty('f_credit[localidade2]', 'Localidade', formz) &&
		isEmpty('f_credit[cod_postal2]', 'Cod. Postal', formz) &&
		isEmpty('f_credit[tel_emp]', 'TelEmp.', formz) &&
		isEmpty('f_credit[telemovel]', 'Telemovel', formz) &&
		isEmpty('f_credit[tipo_contrato]', 'Tipo Contrato', formz) &&
		isEmpty('f_credit[rendimentos_titular]', 'Rendimentos Titular', formz) &&
		isEmpty('f_credit[outros_rendimentos_titular]', 'Outros Rend.', formz) &&
		isEmpty('f_credit[titular_comprovavel]', 'Comprováveis ', formz) &&
		isEmpty('f_credit[renda_casa]', 'Renda/Prest. Casa', formz) &&
		isEmpty('f_credit[automovel]', 'Automóvel', formz) &&
		isEmpty('f_credit[pensao]', 'Pensão', formz) &&
		isEmpty('f_credit[outros_encargos]', 'Outros Encargos', formz) &&
		isEmpty('f_credit[nib]', 'NIB', formz) &&
		isEmpty('f_credit[banco]', 'Banco', formz) &&
		isEmpty('f_credit[balcao]', 'Balcão', formz) &&
		isEmpty('f_credit[ano_abertura_conta]', 'Ano Abertura Conta', formz) &&
		isEmpty('f_credit[bem_financiar]', 'Bem a financiar', formz) &&
		isEmpty('f_credit[tipo_credito]', 'Tipo de Crédito', formz) &&
		isEmpty('f_credit[preco_venda]', 'Preço de Venda', formz) &&
		isEmpty('f_credit[valor_entrada]', 'Valor de entrada', formz) &&
		isEmpty('f_credit[montante_credito]', 'Montante Crédito', formz) &&
		isEmpty('f_credit[duracao]', 'Duração', formz) &&
		isEmpty('f_credit[mensalidade]', 'Mensalidade', formz) &&
		isEmpty('f_credit[tabela]', 'Tabela', formz) &&
		isEmpty('f_credit[seguro]', 'Seguro', formz) &&
		isEmpty('f_credit[plafond_pretendido]', 'Plafond Pretendido', formz) &&
		isEmpty('f_credit[degressividade]', 'Degressividade', formz) &&
		isEmpty('f_credit[degressividade]', 'Degressividade', formz) 
	)
	{
		return true;
	}

	return false;
}
function cobrancaMinimo( value ){
    //alert(document.getElementById('typePayment').value);
    if(document.getElementById('typePayment').value == 2 && value<100){ alert("Encomendas à cobrança válidas a partir de 100 euros.");  return false;}
    else return true;
}
