var emailValidator=false;
var emailIdentity=false;

var passValidator=false;
var passIdentity=false;

var userValidator=false;

var emlState = true;

function validateEmail(field)
{
	str = field.value;
	if (trim(str) == "")
		return true;

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	var refusePhrase = " Adresa de email e incorecta. Adresa trebuie sa contina semnul '@'\n  si un nume de domeniu: .com, .ro, .it, .es, .de, .fr, .co.uk, etc... ";

	if (str.indexOf(at)==-1)
	{
		alert(refusePhrase);
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		alert(refusePhrase);
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert(refusePhrase);
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
		alert(refusePhrase);
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert(refusePhrase);
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1){
		alert(refusePhrase);
		return false;
	}

	if (str.indexOf(" ")!=-1){
		alert(refusePhrase);
		return false;
	}

	return true;
}


function validateUsername(field)
{
	var vv = field.value;
	var strength = 0;

	if (trim(vv)=="")
		return true;

	if (vv.length < 4)
	{
		alert('Numele de utilizator trebuie sa aiba minim 4 caractere');
		return false;
	}

	re = /[0-9a-zA-Z_]/;
	if (!re.test(vv))
	{
		alert('Numele de utilizator poate contine doar litere, cifre sau "_"');
		return false;
	}

	return true;
}

function validatePassword(field)
{
//compute password strength
	var strength = 0;
	var vv = field.value;

	if (trim(vv) == "")
		return false;

	if (vv.length < 6)
	{
		document.getElementById("p_strength").style.color = "#FFFFFF";
		document.getElementById("p_strength").style.backgroundColor = "#FF0000";
		document.getElementById("p_strength").innerHTML ='Nivel: Slab'; 
		return 0;
	}

	if (vv.length >= 6)
		strength++;

	if (vv.length >= 8)
		strength++;

	re = /[?_-]/;
	if (re.test(vv))
		strength++;	

	re = /[a-z]/; 
	if (re.test(vv))
		strength++;	
	
	re = /[0-9]/; 
	if (re.test(vv))
		strength++;	

	
	re = /[A-Z]/; 
	if (re.test(vv))
		strength++;

	if (strength > 5)
		strength = 5;

	objPS = document.getElementById("p_strength").style;
	objPV = document.getElementById("p_strength");
	switch (strength)
	{
		case 2: objPS.backgroundColor = "#FF0000"; objPS.color = "white"; objPV.innerHTML ='Nivel: Slab'; break;
		case 3: objPS.backgroundColor = "#FFFF00"; objPS.color = "black"; objPV.innerHTML ='Nivel: Mediu'; break;
		case 4: objPS.backgroundColor = "#108DD9"; objPS.color = "black"; objPV.innerHTML ='Nivel: Acceptabil'; break;
		case 5: objPS.backgroundColor = "#A4DF09"; objPS.color = "black"; objPV.innerHTML ='Nivel: Excelent'; break;
	}

	return (strength >= 3) ? true : false;
}


function passMessages(index)
{
	switch (index)
	{
		case 0: alert("Parola trebuie sa aiba cel putin 6 litere sau cifre iar Nivelul de Securitate sa fie minim 'Mediu'"); break;
		default: return;
	}
}

function checkClone(obj1, obj2, type)
{
	var str1 = obj1.value;
	var str2 = obj2.value;

	if (str1 != str2)
	{
		if (type == 2)
			alert("Va rugam reconfirmati emailul. `Email` si `Confirmare email` trebuie sa fie identice")
		else 
			alert("Cele doua parole nu sunt identice. Va rugam reverificati-le")

		return false;
	}
	return true;
}


function generalValidator()
{
	if (!emailValidator || !passValidator || !userValidator || document.getElementById("captcha").value.length!=9)
	{
		if (document.getElementById("captcha").value.length != 9)
			alert("Codul de verificare trebuie sa aiba EXACT 9 litere sau cifre.\n           Va rugam reintroduceti codul de verificare");
		else
			alert("Formularul nu este complet. Va rugam reverificati datele introduse");
		return false;
	}
return true;
}

function chk(event)
{
	if (event.keyCode==13) 
	{
		if (trim($("ex_username").value) == '' || trim($("ex_password").value) == '')
			return;

		document.getElementById('connect_form').submit();
	}
	else return;
}


function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) 
{
	return stringToTrim.replace(/\s+$/,"");
}

