_msg_eroare = 'Urmatoarele campuri trebuie completate corect :';
_msg_nume="\n - Nume";
_msg_prenume="\n - Prenume";
_msg_email="\n - Email";
_msg_telefon="\n - Telefon";
_msg_tip="\n - Un tip trebuie selectat";
_msg_nume_firma="\n - Denumire Firma";
_msg_functia="\n - Functia";
_msg_cod_fiscal="\n - Cod fiscal";
_msg_utilizator="\n - Nume utilizator";
_msg_parola="\n - Prola";
_msg_parola2="\n - Reintrodu Parola";
_msg_parole="\n - Parolele Difera";
_msg_email_firma="\n - Email Frima";
_msg_domeniu="\n - Selectati minim un domeniu";
_msg_cod_postal="\n - Cod Postal";




function valideaza_user(){
	var err = 0;
	var mess = _msg_eroare;
	var df = document.form1;  /* form name*/
	if (!trim_space(df.nume)){
		if (err == 0){
			err = 1;			
			df.nume.focus();			
		}
		mess = mess + _msg_nume;
		df.nume.className = "RedInput";
	}else df.nume.className = "text-field";

	if (!trim_space(df.prenume)){
		if (err == 0){
			err = 1;			
			df.prenume.focus();			
		}
		mess = mess + _msg_prenume;
		df.prenume.className = "RedInput";
	}else df.prenume.className = "text-field";
if(df.tip[0].checked==true){// in cazul in care nu este persona juridica
	if(!checkTelefon(df.telefon.value) & !checkTelefon_fara_plus(df.telefon.value)){
		if (err == 0){
			err = 1;			
			df.telefon.focus();			
		}	
		mess = mess + _msg_telefon;
		df.telefon.className = "RedInput";
	}else df.telefon.className = "text-field";
	
	if(!checkNumeric(df.cod_postal.value)){
		if (err == 0){
			err = 1;			
			df.cod_postal.focus();			
		}	
		mess = mess + _msg_cod_postal;
		df.cod_postal.className = "RedInput";
	}else df.cod_postal.className = "text-field";
}
 if(trim_space(df.email)){
	if(!checkEmailAdress(df.email.value)){
		if (err == 0){
			err = 1;			
			df.email.focus();			
		}	
		mess = mess + _msg_email;
		df.email.className = "RedInput";
	}else df.email.className = "text-field";
 }else df.email.className = "text-field";

 if(df.tip[0].checked==false && df.tip[1].checked==false){
	 if(err ==0) err=1;
	 mess = mess + _msg_tip;
 }
 if(df.tip[0].checked==false){// in cazul in care este persona juridica
	 if (!trim_space(df.functia)){
		if (err == 0){
			err = 1;			
			df.functia.focus();			
		}
		mess = mess + _msg_functia;
		df.functia.className = "RedInput";
	}else df.functia.className = "text-field";
	
   var checkBox=false;
   var boxes = document.getElementsByName('domeniu[]');
   for (i=0; i < boxes.length; i++) {
    	  if(boxes[i].checked==true){
			  checkBox=true;
		  }
   }
 if (!trim_space(df.nume_firma)){
		if (err == 0){
			err = 1;			
			df.nume_firma.focus();			
		}
		mess = mess + _msg_nume_firma;
		df.nume_firma.className = "RedInput";
	}else df.nume_firma.className = "text-field";
	
 if (!trim_space(df.cod_postal_firma)){
		if (err == 0){
			err = 1;			
			df.cod_postal_firma.focus();			
		}
		mess = mess + _msg_cod_postal;
		df.cod_postal_firma.className = "RedInput";
	}else df.cod_postal_firma.className = "text-field";
	
	if(!trim_space(df.cod_fiscal)){
		if (err == 0){
			err = 1;			
			df.telefon.focus();			
		}	
		mess = mess + _msg_cod_fiscal;
		df.cod_fiscal.className = "RedInput";
	}else df.cod_fiscal.className = "text-field";	
	
 if(trim_space(df.email_firma)){
	if(!checkEmailAdress(df.email_firma.value)){
		if (err == 0){
			err = 1;			
			df.email_firma.focus();			
		}	
		mess = mess + _msg_email_firma;
		df.email_firma.className = "RedInput";
	}else df.email_firma.className = "text-field";
 }else df.email_firma.className = "text-field";
 
 }else{
	 df.cod_fiscal.className = "text-field";
	 df.nume_firma.className = "text-field";
	 
 }
	
	if (!trim_space(df.utilizator)){
		if (err == 0){
			err = 1;			
			df.utilizator.focus();			
		}
		mess = mess + _msg_utilizator;
		df.utilizator.className = "RedInput";
	}else df.utilizator.className = "text-field";

		
	if (!trim_space(df.parola)){
		if (err == 0){
			err = 1;			
			df.parola.focus();			
		}
		mess = mess + _msg_parola;
		df.parola.className = "RedInput";
	}else df.parola.className = "text-field";

	if (!trim_space(df.parola2)){
		if (err == 0){
			err = 1;			
			df.parola2.focus();			
		}
		mess = mess + _msg_parola2;
		df.parola2.className = "RedInput";
	}else df.parola2.className = "text-field";
	
	if(df.parola.value!=df.parola2.value){
		if(err ==0){
			err=1;
		}
		mess=mess + _msg_parole;
		df.parola2.value="";
		df.parola2.className="RedInput";
	}else df.parola2.className="text-field";
	if(df.tip[1].checked==true){
	if(!checkBox){
		err=1;
		mess = mess + _msg_domeniu;
	}
	}
	
	if (err == 1){
		alert(mess);
		return false;
	}

 
		return true;
}

function checkEmailAdress(who){
//var email=/^[A-Za-z0-9][\w-.]+[A-Za-z0-9]@[A-Za-z0-9]([\w-.]+[A-Za-z0-9]\.)+([A-Za-z]){2,4}$/i; // xx@xx.xx
var email=/^[A-Za-z0-9][\w-.]*[A-Za-z0-9]*@[A-Za-z0-9]*([\w-.]*[A-Za-z0-9]\.)+([A-Za-z]){2,4}$/i; // x@x.xx
return(email.test(who));
}

function checkTelefon(who){
	var phone=/^\+[0-9]/;
	return (phone.test(who));
}

function checkTelefon_fara_plus(who){
	var phone=/^[0-9]/;
	return (phone.test(who));
}

function checkNumeric(who){
	if(who==0){
		return false;
	}
	var phone=/^[0-9]/;
	return (phone.test(who));
}
function validare_data(obiect){
c_datastart = check_name(obiect.value,"start");
var err=0;
    if(!c_datastart){
		if (err == 0){
			err = 1;			
			obiect.value="";
			obiect.focus();			
		}
		mess="Data invalida";
		obiect.className = "RedInput";
	 }else obiect.className = "textarea";
	 if(err==1){
	 	alert(mess);
		return false;
	 }
	return true;
	
}
 
function exist_date(data_f,c_data_f,msg){
 ystart = data_f.getFullYear();
 mstart = String(data_f.getMonth()+1);
 if(mstart.length < 2){
  mstart = "0"+mstart;
 }
 dstart = String(data_f.getDate());
 if(dstart.length < 2){
  dstart = "0"+dstart;
 }
 check_datastart = ystart+"/"+mstart+"/"+dstart;
 if(check_datastart == c_data_f){
  return true;
 }else{
  alert(msg+" date is invalid!");
  return false;
 }
}
 
function check_name(nume_camera,msg){
 x = nume_camera;
 while (x.substring(0,1) == ' ') x = x.substring(1);
 while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
 nume_camera = x
 if(nume_camera == '' || nume_camera == 0 || nume_camera.length != 10 || nume_camera == 'a'){
  return false;
 }else{
  //verificari ptr data
  if(nume_camera.substr(4,1) != "-" || nume_camera.substr(7,1) != "-"){
   return false;
  }
  if(Number(nume_camera.substr(0,4)) <= 1970 || Number(nume_camera.substr(0,4)) > 2100){
   return false;
  }
  if(Number(nume_camera.substr(5,2))-1 < 0 || Number(nume_camera.substr(5,2))-1 > 11){
   return false;
  }
  if(Number(nume_camera.substr(8,2)) < 1 || Number(nume_camera.substr(8,2)) > 31){
   return false;
  }
  return nume_camera;
 }
}
// end - validare data





function keyhandler(e){ 
	if(document.getElementById && !document.all){ 
			Key = e.which;
	}else{  
		Key = window.event.keyCode;
	} 
	
	if(Key == 13){ 
		if (validare_produse()){
			df.submit();
			return false;
		}else{
				return false;
		}
	}	
}

function trim_space(nume_obj){
	nume_obj.value=nume_obj.value.replace(/^\s*|\s*$/g,'');
	if(nume_obj.value==''){
		return false;
	}else{
		return true;
	}
}
