// Verificator formular
function formContactValidation(form) {
   nume = form.nume.value;
   telefon = form.telefon.value;
   email = form.email.value;
   firma = form.firma.value;
   cui = form.cui.value;
   
   if(nume.length == 0) {
      alert("Nu ati completat numele si prenumele...");
      return false;
   }
   if(telefon.length == 0) {
      alert("Nu ati completat telefonul...");
      return false;
   }
   if(telefon.length != 10) {
      alert("Nu ati completat corect telefonul (10 cifre, fara spatii)...");
      return false;
   }
   if(!(checkPhone(telefon))) {
      alert("Numarul de telefon trebuie sa contina numai cifre...");
      return false;
   }
   if(email.length == 0) {
      alert("Nu ati completat adresa de email...");
      return false;
   }
   if(!(checkEmail(email))) {
      alert("Nu ati completat corect adresa de email...");
      return false;
   }
   if(firma.length == 0) {
      alert("Nu ati completat numele companiei...");
      return false;
   }
   if(cui.length == 0) {
      alert("Nu ati completat codul fiscal...");
      return false;
   }
   if(cui.length > 10) {
      alert("Nu ati completat corect codul fiscal (fara spatii, ex. RO12345678)...");
      return false;
   }

   email = email.replace(/ /g,"");
   email = email.toLowerCase();
   form.email.value = email;
   return true;
}

function checkPhone(phoneNo) {
	// check for bad characters in the username
	var ch;
	for (i = 0; i < phoneNo.length; i++) {
		ch = (phoneNo.substring(i, i + 1)).toLowerCase();
		if (!((ch >= "0") && (ch <= "9"))) {
			return false;
		}
	}
	return true;
}

function checkEmail(emailAddr) {
	// this function checks for a well-formed e-mail address
	// in the format:
	// user@domain.com
	
	emailAddr = emailAddr.toLowerCase();
	
	var i;
	
	// check for @
	i = emailAddr.indexOf("@");
	if (i == -1) {
		return false;
	}
	
	// separate the user name and domain
	var username = emailAddr.substring(0, i);
	var domain = emailAddr.substring(i + 1, emailAddr.length)

	// look for spaces at the beginning of the username
	i = 0;
	while ((username.substring(i, i + 1) == " ") && (i < username.length)) {
		i++;
	}
	// remove any found
	if (i > 0) {
		username = username.substring(i, username.length);
	}

	// look for spaces at the end of the domain
	i = domain.length - 1;
	while ((domain.substring(i, i + 1) == " ") && (i >= 0)) {
		i--;
	}
	// remove any found
	if (i < (domain.length - 1)) {
		domain = domain.substring(0, i + 1);
	}

	// make sure neither the username nor domain is blank
	if ((username == "") || (domain == "")) {
		return false;
	}
	
	// check for bad characters in the username
	var ch;
	for (i = 0; i < username.length; i++) {
		ch = (username.substring(i, i + 1)).toLowerCase();
		if (!(((ch >= "a") && (ch <= "z")) || 
			((ch >= "0") && (ch <= "9")) ||
			(ch == "_") || (ch == "-") || (ch == "."))) {
				return false;
		}
	}
	
	// check for bad characters in the domain
	for (i = 0; i < domain.length; i++) {
		ch = (domain.substring(i, i + 1)).toLowerCase();
		if (!(((ch >= "a") && (ch <= "z")) || 
			((ch >= "0") && (ch <= "9")) ||
			(ch == "_") || (ch == "-") || (ch == "."))) {
				return false;
		}
	}

	var aSuffix = new Array("com","edu","org","gov","mil","ca","net","us","aero","arpa","biz","coop","info","int","museum","co.uk","co.au","ro","com.ro","org.ro","tm.ro","nt.ro","nom.ro","info.ro","rec.ro","arts.ro","firm.ro","store.ro","www.ro"
							,"ac","ad","ae","af","ag","ai","al","am","an","ap","aq","ar","as","at","au","az","ba","bb","be","bf","bg","bh","bi","bm","bn","bo","br","bt","by","bz","ca","cc","cd","cf","cg","ch","ck","cl","cm","cn","co","cr","cu"
							,"cx","cy","cz","de","dj","dk","do","dz","ec","ee","eg","es","fi","fj","fk","fm","fr","fo","gb","ge","gf","gg","gh","gi","gl","gm","gn","gr","gs","gt","gu","hk","hm","hn","hr","hu","id","ie","il","im","in","io","ir"
							,"is","it","je","jo","jp","ke","kg","kh","kr","kw","ky","kz","lb","lc","li","lk","lr","lt","lu","lv","ly","mc","md","mg","mh","mk","mm","mn","mo","mp","mq","mr","ms","mt","mu","mx","my","mw","na","nc","nf","ni","nl"
							,"no","np","nu","nz","om","pa","pe","pg","ph","pk","qa","re","ru","rw","sa","sb","se","sg","sh","si","sk","sm","sn","so","st","su","sv","sz","tc","td","tf","th","tj","tm","tn","to","tp","tr","tt","tv","tw","tz","ua"
							,"ug","uk","um","us","uy","uz","ve","vg","vi","vu","wf","ws","yt","yu","za","zm");
	var bFoundSuffix = false;
	i = 0;
	while (i < aSuffix.length) {
		if (("." + aSuffix[i]) == domain.substring(domain.length - aSuffix[i].length - 1, domain.length)) {
			return true;
		}
		i++;
	}
	// we would have exited if we'd found a good suffix, so return false
	return false;
}

