var registerConn=CONNEXION('div_register');

flash = '';

function validate_register(f) {
	unhighlightErrors(f, sErrorFields)
	sErrorTexts = '';
	sErrorFields = '';

	if(f.email.value.length == 0) { 
		addError('Introduceti EMAIL-ul', 'email') 
	}
	
	if((f.email.value.length > 0) && !isEMailAddr(f.email)) { 
		addError('Introduceti corect EMAIL-ul', 'email') 
	}
	
	if (f.parola) {
		if(f.parola.value.length == 0) { addError('Introduceti PAROLA', 'parola') }
		if((f.parola.value.length > 0) && (f.parola.value.length < 5)) { addError('Introduceti o PAROLA de cel putin 6 caractere', 'parola') }
	}
	
	
	
	
	for (var i=0; i < f.tpy.length; i++) {
	   if (f.tpy[i].checked)  {
		  var valTpy = f.tpy[i].value;
		  }
	}
	
	if (valTpy=="fizica") {
		
		if(f.nume.value.length == 0) { 
			addError('Introduceti numele', 'nume');
		}
		if(f.prenume.value.length == 0) { 
			addError('Introduceti prenumele', 'prenume') ;
		}
		if(f.adresa.value.length == 0) { 
			addError('Introduceti adresa', 'adresa') ;
		}
		if(f.localitate.value.length == 0) { 
			addError('Introduceti localitatea', 'localitate') ;
		}
		if(f.judet.value.length == 0) { 
			addError('Introduceti judetul', 'judet') ;
		}
		if(f.telefon.value.length == 0) { 
			addError('Introduceti telefonul', 'telefon') ;
		}
		if(f.serie_bi.value.length == 0) { 
			addError('Introduceti serie_bi', 'serie_bi') ;
		}
		if(f.numar_bi.value.length == 0) { 
			addError('Introduceti numar_bi', 'numar_bi') ;
		}
		
		
		if((f.cnp.value.length == 0)||(f.cnp.value.length > 0 && f.cnp.value.length < 13) || (f.cnp.value.length > 0 && !Number(f.cnp.value))) { addError('Introduceti corect CNP-ul', 'cnp') }

		
		
	} else {
		if(f.firma.value.length == 0) { 
			addError('Introduceti firma', 'firma');
		}
		if(f.sediu.value.length == 0) { 
			addError('Introduceti sediul', 'sediu') ;
		}
		if(f.cod_fiscal.value.length == 0) { 
			addError('Introduceti codul fiscal', 'cod_fiscal') ;
		}
		if(f.nr_regcom.value.length == 0) { 
			addError('Introduceti Numar de inregistrare', 'nr_regcom') ;
		}
		if(f.banca.value.length == 0) { 
			addError('Introduceti banca', 'banca') ;
		}
		if(f.contP.value == "") { 
			addError('Introduceti cont-ul', 'contP') ;
		}
	}
	
	if(f.verificationCode.value.length == 0) { 
			addError('Introduceti codul de verificare', 'verificationCode') ;
		}
	

	if(sErrorTexts.length > 0) {
		highlightErrors(f, sErrorFields)
		alert('Au aparut urmatoarele erori:\n\n' + sErrorTexts)
		return false
	}
	else {
		return true
	}
}

function validate_login(f) {
	unhighlightErrors(f, sErrorFields)
	sErrorTexts = '';
	sErrorFields = '';

	if(f.email.value.length == 0) { addError('Introduceti EMAIL-ul', 'email') }
	if((f.email.value.length > 0) && !isEMailAddr(f.email)) { addError('Introduceti corect EMAIL-ul', 'email') }
	
	if(f.parola.value.length == 0) { addError('Introduceti PAROLA', 'parola') }

	if(sErrorTexts.length > 0) {
		highlightErrors(f, sErrorFields)
		alert('Au aparut urmatoarele erori:\n\n' + sErrorTexts)
		return false
	}
	else {
		return true
	}
}

function validate_pass(f) {
	unhighlightErrors(f, sErrorFields)
	sErrorTexts = '';
	sErrorFields = '';

	if(f.old_password.value.length == 0) { addError('Introduceti PAROLA VECHE', 'old_password') }
	if(f.new_password.value.length == 0) { addError('Introduceti PAROLA NOUA', 'new_password') }
	if(f.new_password.value.length > 0 && f.retyped_password.value.length == 0) { addError('Reintroduceti PAROLA NOUA', 'retyped_password') }
	if(f.new_password.value.length > 0 && f.retyped_password.value.length > 0 && f.new_password.value!=f.retyped_password.value) { addError('Reintroduceti corect PAROLA NOUA', 'retyped_password') }	
	if((f.new_password.value.length > 0) && (f.new_password.value.length < 5)) { addError('Introduceti o PAROLA de cel putin 6 caractere', 'new_password') }

	if(sErrorTexts.length > 0) {
		highlightErrors(f, sErrorFields)
		alert('Au aparut urmatoarele erori:\n\n' + sErrorTexts)
		return false
	}
	else {
		return true
	}
}

function userPass(f){
	if (validate_pass(f)) {
	registerConn.openForm(document.register,'userData.php','post','changePass','Se trimit datele...');
	}
}

function userData(f){
	document.getElementById('div_register').style.display="none";
	document.getElementById('register_message').style.display="block";
	registerConn.openForm(document.register,'userData.php','post','register_message','Se salveaza datele...');
}