function addContact()
{
	var mainTable = $('#ui-main-table');
	var ok = true;
	for (i=4; i<9; i++)
	{
		if (ok)
		{
			if (mainTable.tBodies[0].rows[i].className == "hidden")
			{
				mainTable.tBodies[0].rows[i].className = ""; ok = false;
			}
		}
		else break;
	}
}
$(document).ready(function() {
	$('a[name=toggle-message-add]').click(function(e) {
		e.preventDefault();
		if ($('#invite-message').is(':hidden')) {
			$('#invite-message').slideDown('slow');
			$('#opt-mess-link').removeClass().addClass('ui-icon-arrow-collapse');
			$('#opt-mess-link-import').removeClass().addClass('ui-icon-arrow-collapse-import');

		} else {
			$('#opt-mess-link').removeClass().addClass('ui-icon-arrow-normal');
			$('#opt-mess-link-import').removeClass().addClass('ui-icon-arrow-normal-import');
			$('#invite-message').slideUp('slow');
		}
	});
	$('textarea[name=message]').click(function(e) {
		if ($(this).val() == "As vrea sa mi te alaturi pe BestJobs. Imi construiesc o retea de contacte, pentru a putea solicita recomandari profesionale, pe care le voi afisa in CV-ul meu. Astfel, imi voi creste sansele de a gasi un job mai bun.")
			$(this).val('');
	});
});
function setFrom(service)
{
	var email = "Email";
	var pass = "Parola";
	switch (service)
	{
		case '1': // yahoo
			$('#ui-arrow-register').css('margin-left', '155px');
			$('#emailFrom').hide();
			$('#passwordLabel').hide();
			$('#password').hide();
			$('.dialog_contacts').show();
			$('.gmail_contacts').hide();
			break;

		case '2': // gmail
			$('#ui-arrow-register').css('margin-left', '260px');
			$('#emailFrom').show();
			$('#passwordLabel').show().text(pass);
			$('#password').show();
			$('.dialog_contacts').hide();
			$('.gmail_contacts').show();
			break;
	}
	$('#email').val('');
	$('#password').val('');
}
function submitAddContactsForm()
{
	ok = true;
	var defaultText = "Introdu adresele de email";
	var emails = document.addContactsForm.emails.value;
	if (emails.length < 5 || emails == defaultText)
	{
		ok = false;
		alert("Atentie! Adresele de email introduse nu sunt valide");
	}
	if (ok)
		document.addContactsForm.submit();
	else
		return false;
}
function submitImportForm()
{
	var ok = true;
	if (ok==true)
	{
		if ($('#email').val().length < 2)
		{
			alert("Nu ai introdus numele/email-ul!");
			ok=false;
		}
	}
	if (ok==true)
	{
		if ($('#password').val().length < 2)
		{
			alert("Nu ai completat parola!");
			ok=false;
		}
	}
	if (ok == true)
		document.importForm.submit();
	else
		return false;
}
function toggleContacts()
{
	var state = $('input#selectall').is(':checked') ? true : false;
	if ($('div#withoutAccounts').is(':hidden')) {
		var inputClass = '.imp-check';
	} else {
		var inputClass = '.imp-check-no-acc';
	}
	$('input' + inputClass).each(
		function () {
			$(this).attr('checked', state);
		}
	);
}
function processContact(type, id)
{
	if (id == null) return false;
	if (type != 'accept' && type != 'refuse') return false;

	var obj = document.getElementById("contact_" + parseInt(id));
	document.getElementById("contactid").value = obj.value;

	var act = document.acceptContacts.action;
	document.acceptContacts.action = act + "/" + type + "contacts";
	document.acceptContacts.submit();
}
function processAllContacts(type, perpage)
{
	if (type != 'accept' && type != 'refuse') return false;

	document.getElementById("all").value = 1;
	var act = document.acceptContacts.action;
	document.acceptContacts.action = act + "/" + type + "contacts";
	document.acceptContacts.submit();
}
function deleteContacts()
{
	var ok = confirm("Esti sigur(a) ca vrei sa stergi contactele selectate? \nStergerea contactelor conduce si la stergerea invitatiilor trimise acestora, din istoric `Invitatii trimise`.");
	if (ok == true)
	{
		document.myContacts.submit();
	}
}
function bje_vote(obj) {
	var vote_url=obj.getAttribute("act_url");
	var chk=0;
	for(var i=1;i<8;i++) {
		var rd=document.getElementById("bje_rd_"+i);
		if(rd.checked==true) chk=i;
	}
	if(chk!=0) Request.sendGET(vote_url+"/vot/"+chk,bje_showthanks,true);
	return;
}
function bje_showthanks(response) {
	var bje_vote=document.getElementById("bje_vote");
	var bje_showthanks=document.getElementById("bje_showthanks");

	bje_vote.style.display="none";
	bje_showthanks.style.display="block";
}
var defValue = "Introdu adresele de email";
function clearMessage(obj)
{
	if (obj.value == defValue) obj.value = '';
}
function updateMessage(obj)
{
	if (obj.value == '') obj.value = defValue;
}
function validSearchUsers() {
	var ok=true;
	var fn=document.getElementById("s_firstname").value.trim().replace(" ","_");
	var ln=document.getElementById("s_lastname").value.trim().replace(" ","_");
	if(!fn || !ln || ln=="" || fn=="") {
		alert("Nu ai completat corect cele doua campuri.");
		return false;
	}
	else {
		document.location.href="http://www.bestjobs.md/contacts/search/firstname/"+fn+"/lastname/"+ln;
	}
}
function fireSearch() {
	if(!e) {
		var e=window.event;
	}
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	keyCode=parseInt(keyCode);

	if(keyCode==13) {
		validSearchUsers()
	}
	else {
		return false;
	}
}
function skipAddImported()
{
	$('div#withAccounts').hide();
	$('div#withoutAccounts').show();
	$('input#selectall').attr('checked', true);
	$('div#notice').hide();
	$('div#notice-no-acc').show();
	//reset message
	$('#invite-message').hide();
	$('#opt-mess-link-import').removeClass().addClass('ui-icon-arrow-collapse-import');
	$('textarea[name="message"]').val("As vrea sa mi te alaturi pe BestJobs. Imi construiesc o retea de contacte, pentru a putea solicita recomandari profesionale, pe care le voi afisa in CV-ul meu. Astfel, imi voi creste sansele de a gasi un job mai bun.");
	$('<a href="' + 'http://www.bestjobs.md/contacts/add' + '">' + $('a#skip').text() + '</a>').insertAfter($('a#skip'));
	$('a#skip').remove();
}
function addImported()
{
	var wa = $('div#withoutAccounts').is(':hidden') ? 1 : 0;
	var inputClass = wa == 1 ? '.imp-check' : '.imp-check-no-acc';
	var actionUrl = 'http://www.bestjobs.md/contacts/_addimported/wa/' + wa;
	var service = $('input[name="service"]').val();
	if (service.length > 1)
	{
		actionUrl += '/service/' + service;
	}
	if ($('textarea[name="message"]').val().length > 1)
	{
		var message = $('textarea[name="message"]').val();
		actionUrl += '/message/' + message.replace(/\//g, '');
	}
	var arrEmails = new Array();
	$('input' + inputClass).each(
		function () {
			if ($(this).is(':checked')) arrEmails.push($(this).val());
		}
	);
	if (arrEmails.length > 0) actionUrl += '/emails/' + arrEmails.join(',');
	Request.sendPOST(actionUrl, '', addImportedCB);
}
function addImportedCB(response)
{
	var resp = response.responseText;
	if (resp == 1) // with accounts, go to step 2
		skipAddImported();
	else	
		window.location.href = 'http://www.bestjobs.md/contacts/add';
	return;
}