// functii de validare primare pt. campuri din formular

function eObligatoriu(numecamp,mesaj){
	valcamp = document.getElementById(numecamp).value ;
	if (valcamp.length < 1){
		alert(mesaj);
		try {
			document.getElementById(numecamp).focus();
		} catch(er) {
			
		}
		return false;
	}else{
		return true;
	}
}

function eObligatoriu2(numecamp1,numecamp2,mesaj){
	valcamp1 = document.getElementById(numecamp1).value;
	valcamp2 = document.getElementById(numecamp2).value;
	if ((valcamp1.length < 1) && (valcamp2.length < 1)){
		alert(mesaj);
		try {
			document.getElementById(numecamp1).focus();
		} catch(er) {
			
		}
		return false;
	}else{
		return true;
	}
}

function eSelectat(numecamp,mesaj){
	valcamp = document.getElementById(numecamp).value ;
	if (valcamp == "-1"){
		alert(mesaj);
		try {
			document.getElementById(numecamp).focus();
		} catch(er) {
			
		}
		return false;
	}else{
		return true;
	}
}

function validareAn(numecamp,mesaj){
	valcamp = document.getElementById(numecamp).value;
	if (isNaN(valcamp)){
		alert(mesaj);
		try {
			document.getElementById(numecamp).focus();
	} catch(er) {
		
	}
		return false;
	}else{
		return true;
	}
}

function validareInterventie (numecamp,length, mesaj) {
	var rezultat = false;
	for (var counter=1; counter<=length; counter++) {
		temp = numecamp + "_" + counter;
		inputs = document.getElementById(temp).checked;
		if (inputs == true) {
			rezultat = true;
			break;
		}
	}
	if (rezultat == false){
		alert (mesaj);
	}
	return rezultat;
}

function Email(numecamp,mesaj){
		valcamp = document.getElementById(numecamp).value;
		if ((valcamp.length > 0)&&((valcamp.length < 6) || (valcamp.indexOf("@") < 1) || (valcamp.indexOf(".") < 3))){
			alert(mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;
		}else{
	return true;
	}
}

function eParola(numecamp1,numecamp2){
	ecompletat = eObligatoriu(numecamp1, "Completati parola!");
	if (!ecompletat){
		return;
	}
	ecompletat = eObligatoriu(numecamp2, "Reintroduceti parola!");
	if (!ecompletat){
		return;
	}
	valcamp1 = document.getElementById(numecamp1).value;
	valcamp2 = document.getElementById(numecamp2).value;
	if (valcamp1 != valcamp2 ){
		alert("Parola reintrodusa nu este aceeasi!");
		try {
			document.getElementById(numecamp2).focus();
		} catch(er) {
			
		}
		return false;
	}else{
		return true;
	}
}

var car_neutru = '!#$^&*()+|}{[]?><~%:;/,=`"\'';
function carNeutre(s) {   
	var i;
	var char="";
		for (i = 0; i < s.length; i++) {
			var c = s.charAt(i);
		if(i>0) lchar=s.charAt(i-1);
			if (car_neutru.indexOf(c) != -1 || (char=="." && c==".")) return false;
			}
		return true;
}

function eTelefon(numecamp,mesaj)  
{
	var tfld = trim(document.getElementById(numecamp).value);  // value of field with whitespace trimmed off
	var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
	if (!telnr.test(tfld)) {
		alert(mesaj);
		try {
			document.getElementById(numecamp).focus();
		} catch(er) {
			
		}
		return false;
	}
	var numdigits = 0;
	for (var j=0; j<tfld.length; j++)
	if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;
	if ((numdigits>0)&&(numdigits<10)||(numdigits>12)) {
		alert(mesaj);
		try {
			document.getElementById(numecamp).focus();
		} catch(er) {
			
		}
		return false;
	}
	return true;
}


function eEmail(numecamp,mesaj){
	email = document.getElementById(numecamp).value;
	mailLength = email.length;
	dot = ".";
	at = "@";
	atPos = email.indexOf(at);
	stopPos = email.lastIndexOf(dot);
	if (email != "") {
		if (mailLength < 6) {
			alert(mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;
			}
		if ((atPos <= 0) || (stopPos == -1)) {
			alert(mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;
			}
		if (stopPos < atPos) {
			alert (mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;
			}
		if (stopPos - atPos == 1) {
			alert(mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;	
			}
		if (email.indexOf(at,(atPos+1)) != -1) {
			alert(mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;	
			}
		if (email.indexOf(" ") != -1) {
			alert(mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;	
			}
		if ( mailLength - stopPos < 3) {
			alert(mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;
			}
		if (carNeutre(email) == false) {
			alert(mesaj);
			try {
				document.getElementById(numecamp).focus();
			} catch(er) {
				
			}
			return false;
			}
		else {
			return true;
		}
	} else {
		return true;
	}
}


function verifySelected(x)
{
	if (x[0].selected) return false;
	return true;
}
function verifyChecked(x)
{
	for (var i=0; i<x.length; i++) if (x[i].checked) return true;
	return false;
}





function validateForm(formular){
	if ( !eObligatoriu(formular.nume.name, "Numele si prenumele dumneavoastra este obligatoriu") ) return false;
	if ( !eObligatoriu(formular.mail.name, "Adresa de e-mail este obligatorie") ) return false;
	if ( !Email(formular.mail.name, "Adresa de e-mail este invalida") ) return false;
	var tel = formular.telefon.value;
	if (tel == null || tel == ""){
		alert("Numarul de telefon este obligatoriu");
		formular.telefon.focus();
		return false;
	} else if (tel){
		var filter2 = /^[0-9]/
		if (!filter2.test(tel)){
			alert("Numarul de telefon este invalid");
			formular.telefon.focus();
			return false;
		}
	}
	var raspuns = new Array();
	raspuns[0] = verifyChecked(formular.client);
	raspuns[1] = verifyChecked(formular.problSol);
	raspuns[2] = verifyChecked(formular.atitudine);
	raspuns[3] = verifyChecked(formular.parere_pret);
	raspuns[4] = verifyChecked(formular.comunicare);
	raspuns[5] = verifySelected(formular.nota_pol);
	raspuns[6] = verifySelected(formular.nota_com);
	raspuns[7] = verifySelected(formular.nota_pro);
	raspuns[8] = verifyChecked(formular.recomand);
	for (var i=0; i<9; i++) if (!raspuns[i]){
		alert("Nu ati raspuns la toate intrebarile");
		return false;
	}
	return true;
}



