var wordRegex  = new RegExp(/^([a-zA-Z0-9_]+)\w+$/);
var emailRegex = new RegExp("^([a-zA-Z0-9_]|\\-|\\.)+@(([a-zA-Z0-9_]|\\-)+\\.)+[a-zA-Z]{2,4}\$");
var isEmty = new RegExp(/^[\ \t\r\n]*$/);
var script = new RegExp(/<.*?>/);
var isNumber = new RegExp(/^[1-9][0-9\ ]*$/);
var isNumberNotMandatory = new RegExp(/^[0-9]{0,10}$/);

function ValidateUserFields()
{
	if (document.user.step.value != "2")
	{
		if (!document.user.username.value.match(wordRegex))
		{
			alert('Username invalid !\nFolositi doar cifre si litere !');
			return false;
		}
		if (!document.user.password.value.match(wordRegex))
		{
			alert('Parola invalida !\nFolositi doar cifre si litere !');
			return false;
		}
		if (document.user.passwordconfirm.value != document.user.password.value)
		{
			alert('Confirmare parola gresita !');
			return false;
		}
		if (!document.user.email.value.match(emailRegex))
		{
			alert('Email invalid !');
			return false;
		}
		if (document.user.email.value != document.user.email2.value)
		{
			alert('Confirmare email gresita !');
			return false;
		}
	}
		
	if (document.user.step.value != "1")
	{
		if (document.user.name.value.match(isEmty) || document.user.name.value.match(script))
		{
			alert('Nume invalid !\nCaractere nepermise: <..>');
			return false;
		}
		if (document.user.address.value.match(isEmty) || document.user.address.value.match(script))
		{
			alert('Adresa invalida !\nCaractere nepermise: <..>');
			return false;
		}
		if (document.user.day.selectedIndex == 0 || document.user.month.selectedIndex == 0 || document.user.year.selectedIndex == 0)
		{
			alert('Data de nastere gresita');
			return false;
		}
		if (document.user.telefon.value.match(isEmty) || document.user.telefon.value.match(script))
		{
			alert('Telefon invalid !\nCaractere nepermise: <..>');
			return false;
		}
		if (document.user.city.selectedIndex == 0)
		{
			alert('Alege un oras !');
			return false;
		}
		if (document.user.security.value.match(isEmty))
		{
			alert('Cod de securitate invalid !');
			return false;
		}
	}
	
	return true;
}

function ValidateExperientaCVFields()
{
	if (document.cv.post.value.match(isEmty) || document.cv.post.value.match(script))
	{
		alert('Post detinut invalid !\nCaractere nepermise: <..>');
		return false;
	}
	if (document.cv.firma.value.match(isEmty) || document.cv.firma.value.match(script))
	{
		alert('Firma invalida !\nCaractere nepermise: <..>');
		return false;
	}
	if (document.cv.nrAngajati.selectedIndex == 0)
	{
		alert('Alege numarul de angajati !');
		return false;
	}
	if (document.cv.domain.selectedIndex == 0)
	{
		alert('Alege un domeniu !');
		return false;
	}
	if (document.cv.delaMonth.selectedIndex == 0 || document.cv.delaYear.selectedIndex == 0)
	{
		alert('Perioada \"de la\" invalida !');
		return false;
	}
	if (!document.cv.prezent.checked)
	{
		if (document.cv.panaMonth.selectedIndex == 0 || document.cv.panaYear.selectedIndex == 0)
		{
			alert('Perioada \"pana la\" invalida !');
			return false;
		}
	}
	if (document.cv.descriere.value.match(script))
	{
		alert('Descrierea companiei invalida !\nCaractere nepermise: <..>');
		return false;
	}
	if (document.cv.realizari.value.match(script))
	{
		alert('Responsabilitati, realizari invalide !\nCaractere nepermise: <..>');
		return false;
	}
	if (document.cv.altele.value.match(script))
	{
		alert('Referinte, observatii invalide !\nCaractere nepermise: <..>');
		return false;
	}
	return true;
}

function ValidateCVLangSkills()
{
	if (document.cv_lang.language.selectedIndex == 0)
	{
		alert('Selecteaza limba !');
		return false;
	}
	if (document.cv_lang.level1.selectedIndex == 0)
	{
		alert('Selecteaza nivel vorbit !');
		return false;
	}
	
	return true;
}


function ValidateSkillsCVFields()
{
	if (document.cv.level.selectedIndex == 0)
	{
		alert('Nivel invalid !');
		return false;
	}
	if (document.cv.experience.selectedIndex == 0)
	{
		alert('Experienta invalida !');
		return false;
	}
	if (document.cv.domain.selectedIndex == 0)
	{
		alert('Domeniu invalid !');
		return false;
	}
	if (document.cv.skill.value.match(isEmty) || document.cv.skill.value.match(script))
	{
		alert('Aptitudine invalida !');
		return false;
	}
	
	return true;
}

function ValidateStudiiCVFields()
{
	if (document.cv.education_level.selectedIndex == 0)
	{
		alert('Nivel de educatie invalid !');
		return false;
	}
	if (document.cv.study_domain.selectedIndex == 0)
	{
		alert('Domeniu de studiu invalid !');
		return false;
	}
	if (document.cv.school.value.match(isEmty) || document.cv.school.value.match(script))
	{
		alert('Nume scoala invalid !');
		return false;
	}
	if (document.cv.delaMonth.selectedIndex == 0 || document.cv.delaYear.selectedIndex == 0)
	{
		alert('Perioada \"de la\" invalida !');
		return false;
	}
	if (!document.cv.prezent.checked)
	{
		if (document.cv.panaMonth.selectedIndex == 0 || document.cv.panaYear.selectedIndex == 0)
		{
			alert('Perioada \"pana la\" invalida !');
			return false;
		}
	}
	if (document.cv.descriere.value.match(script))
	{
		alert('Subiecte acoperite invalid !\nCaractere nepermise: <..>');
		return false;
	}
	return true;
}

function ValidateObiectiveCV()
{
	if (document.cv.altele.value.match(script))
	{
		alert('Altele invalid !\nCaractere nepermise: <..>');
		return false;
	}
	if (document.cv.hobby.value.match(script))
	{
		alert('Hobby-uri invalide !\nCaractere nepermise: <..>');
		return false;
	}
	if (document.cv.obiectiv.value.match(isEmty) || document.cv.obiectiv.value.match(script))
	{
		alert('Obiectiv profesional invalid !\nCaractere nepermise: <..>');
		return false;
	}
	if (document.cv.domain.selectedIndex == 0)
	{
		alert('Selecteaza domeniu !');
		return false;
	}
	return true;
}

function ValidateCompanyFields()
{
	if (document.companie.step.value != "1")
	{
		if (!document.companie.codcui.value.match(isNumber) && !document.companie.codcui.value.match(isEmty))
		{
			alert('CUI invalid !');
			return false;
		}
		if (document.companie.name.value.match(isEmty))
		{
			alert('Nume firma invalid !');
			return false;
		}
		if (document.companie.address.value.match(isEmty) || document.companie.address.value.match(script))
		{
			alert('Adresa invalida !\nCaractere nepermise: <..>');
			return false;
		}
		if (!document.companie.turnover.value.match(isNumber) && !document.companie.turnover.value.match(isEmty))
		{
			alert('Cifra de afaceri invalida !');
			return false;
		}
		if (document.companie.description.value.match(isEmty) || document.companie.description.value.match(script))
		{
			alert('Descriere invalida !\nCaractere nepermise: <..>');
			return false;
		}
		if (!document.companie.employees.value.match(isNumber) && !document.companie.employees.value.match(isEmty))
		{
			alert('Numar mediu de salariati este invalid !');
			return false;
		}
		if (document.companie.telefon.value.match(isEmty) || document.companie.telefon.value.match(script))
		{
			alert('Telefon invalid !\nCaractere nepermise: <..>');
			return false;
		}
		if (document.companie.city.selectedIndex == 0)
		{
			alert('Alege un oras !');
			return false;
		}
		if (document.companie.security.value.match(isEmty))
		{
			alert('Cod de securitate invalid !');
			return false;
		}
	}
	
	if (document.companie.step.value != "2")
	{
		if (!document.companie.username.value.match(wordRegex))
		{
			alert('Username invalid !\nFolositi doar cifre si litere !');
			return false;
		}
		if (!document.companie.password.value.match(wordRegex))
		{
			alert('Parola invalida !\nFolositi doar cifre si litere !');
			return false;
		}
		if (document.companie.passwordconfirm.value != document.companie.password.value)
		{
			alert('Confirmare parola gresita !');
			return false;
		}
		if (!document.companie.email.value.match(emailRegex))
		{
			alert('E-mail invalid !');
			return false;
		}
		if (document.companie.email.value != document.companie.email2.value)
		{
			alert('Confirmare email gresita !');
			return false;
		}
		if (document.companie.contactname.value.match(isEmty) || document.companie.contactname.value.match(script))
		{
			alert('Nume contact invalid !\nCaractere nepermise: <..>');
			return false;
		}
		if (document.companie.contacttel.value.match(script))
		{
			alert('Telefon contact invalid !\nCaractere nepermise: <..>');
			return false;
		}
		if (!document.companie.contactemail.value.match(emailRegex) && !document.companie.contactemail.value.match(isEmty))
		{
			alert('E-mail contact invalid !');
			return false;
		}
	}
	return true;
}

function ValidatePostFields()
{
	if (document.theForm.title.value.match(isEmty) || document.theForm.title.value.match(script))
	{
		alert('Denumire invalida !\nCaractere nepermise: <..>');
		return false;
	}
	if (document.theForm.description.value.match(isEmty))
	{
		alert('Completeaza descrierea !');
		return false;
	}
	if (document.theForm.requirements.value.match(isEmty))
	{
		alert('Completeaza cerintele !');
		return false;
	}
	if (document.theForm.expire.selectedIndex == 0)
	{
		alert('Selecteaza durata de expirare');
		return false;
	}
	if (document.theForm.posttype.selectedIndex == 0)
	{
		alert('Alege un tip !');
		return false;
	}
	if (!document.theForm.pay.value.match(isNumberNotMandatory))
	{
		alert('Salariu invalid !\nIntroduceti doar numere !');
		return false;
	}
	if ($("#domains option:selected").length == 0 || $("#domains option:selected").length > 5)
	{
		alert('Alege maxim 5 domenii !');
		return false;
	}
	if ($("#cities option:selected").length == 0 || $("#cities option:selected").length > 3)
	{
		alert('Alege maxim 3 orase !');
		return false;
	}
	
	return true;
}

function ValidateEmail()
{
	if (!document.post.email.value.match(emailRegex))
	{
		alert('E-mail invalid !');
		return false;
	}
	return true;
}


/*
*	Alerte validation
*/

function addTag(myInput, tags)
{
	var exitBool = false;
	var tag = myInput.val();
	
	tags.find("option").each(
		 function() {
			if ($(this).val() == tag) {
				exitBool = true;
				return;
			}
	});

	if (exitBool) return;
	if (tag == null || tag == "") return;
	
	var tagsArray = tag.split(",");
	for ( i=0; i<tagsArray.length; i++)
	{
		if (tagsArray[i].length > 0)
		{
			var option = $("<option></option>").text(tagsArray[i]).attr("selected", true);
			tags.append(option).change();
		}
	}
	myInput.val('');
}
function removeTag(container) {
	var m1 = document.theForm.elements[container];
	
	m1len = m1.length;
	for ( i = (m1len -1); i>=0; i--)
	{
		if (m1.options[i].selected == true ) {
			m1.options[i] = null;
		}
	}
}
function one2two(elso, masodik) {
	var m1 = document.theForm.elements[elso];
	var m2 = document.theForm.elements[masodik];
	m1len = m1.length;
	for ( i=0; i<m1len ; i++){
		if (m1.options[i].selected == true ) {
			m2len = m2.length;
			m2.options[m2len]= new Option(m1.options[i].text, m1.options[i].value);
		}
	}

	for ( i = (m1len -1); i>=0; i--){
		if (m1.options[i].selected == true ) {
			m1.options[i] = null;
		}
	}
}

function two2one(elso,masodik) {
	var m1 = document.theForm.elements[elso];
	var m2 = document.theForm.elements[masodik];

	m2len = m2.length;
	for ( i=0; i<m2len ; i++){
		if (m2.options[i].selected == true)                {
			m1len = m1.length;
			m1.options[m1len]= new Option(m2.options[i].text, m2.options[i].value);
		}
	}
	for ( i=(m2len-1); i>=0; i--) {
		if (m2.options[i].selected == true ) {
			m2.options[i] = null;
		}
	}
}

function viewCV(postid, userid)
{
	$.ajax({
	   type: "GET",
	   url: "/jobs/anunt/aplicants/"+postid+"/"+userid
	 });
}



var fields = new Array('domain_ready','city_ready');

function leaving()
{
   var i=0,contor=0
   ok=true;

   for (contor=0; contor<fields.length; contor++)
	{
		m2=document.theForm.elements[fields[contor]]
		for (i=0; i<m2.length; i++) {
		m2.options[i].selected = true;}
	}
   return(ok);
}