  // Functiti aplicabile modulului Resurse Umane

function TestNr(obj){//alert($(obj).attr('required'));
	if($(obj).attr('requir')=='is_Nu')return true;
	var max=$(obj).attr('predefined');
	if(!max)var maxx='';
	else var maxx='{'+eval(max)+'}';

	var val=$(obj).val();
	var nrt1='/([0-9])'+maxx+'/';
	var nrt=eval(nrt1);

	if(!$(obj).parent().children('.check').hasClass('check'))$('<span class="ui-state-default ui-corner-all check"></span>').insertAfter($(obj));
	if(nrt.test(val) && val.length<11 && val.length > 0){
		$(obj).parent().children('.check').html('<span class="ui-icon ui-icon-check"></span>');
		return true;
		}
	else{
		$(obj).parent().children('.check').html('<span class="ui-icon ui-icon-close"></span>');
		return false;
		}
	}

function TestEmail(obj){
	if($(obj).attr('valid')=='true')return true;
	if($(obj).attr('requir')=='is_Nu')return true;
	var val=$(obj).val();
	if(!$(obj).parent().children('.check').hasClass('check'))$('<span class="ui-state-default ui-corner-all check"></span>').insertAfter($(obj));
	if(emailfilter.test(val) && val.length > 1){
		var tabel=$(obj).attr('tabel');
		$.post('/ajax_smart.php?altmodul=module/php/HR',{verificmail:val,tabel:tabel},function(ret){
		  if(ret=='true'){
			$.alert({text: 'Aceasta adresa de e-mail exista deja<br>email <i>'+val+'</i> incorect'});
			$(obj).val('');
			$(obj).focus();
			$(obj).attr('valid','false');
			return false;
		  }
		  else {$(obj).attr('valid','true');$(obj).parent().children('.check').html('<span class="ui-icon ui-icon-check"></span>');return true;}
		});
		}
	else{
		$(obj).parent().children('.check').html('<span class="ui-icon ui-icon-close"></span>');
		return false;
		}
}

function TestText(obj){
	if($(obj).attr('requir')=='is_Nu')return true;
	var val=$(obj).val();
	var txt=/[0-9]/;
	if(txt.test(val) && val.length > 1){
		return false;
		}
	else if(val.length > 1){
		return true;
		}
}

function filtruFocus(obj){
	if($(obj).attr('requir')=='is_Nu')return true;
	var back=Testfield(obj);
	if(!back){
	var txt=$(obj).parent().parent().children('*[class*="field"]').html();
	$.alert({text:txt.replace('*','')+' incorect',onclose:function(){return $(obj).focus();}});
	}
	else return true;
}

function TestPass(obj){
	var val=$(obj).val();
	var val2=$(obj).parent().parent().parent().find("input[name='password']").val();

	if(!$(obj).parent().children('.check').hasClass('check'))$('<span class="ui-state-default ui-corner-all check"></span>').insertAfter($(obj));
	if(val==val2 && val.length > 5){
		$(obj).parent().children('.check').html('<span class="ui-icon ui-icon-check"></span>');
		return true;
		}
	else{
		$(obj).parent().children('.check').html('<span class="ui-icon ui-icon-close"></span>');
		return false;
		}
}

function TestSelect(obj){
	if($(obj).attr('requir')=='is_Nu')return true;
	var val=$(obj).val();
	if(val.length > 0)return true; else return false;
}

function TestData(obj){
	if($(obj).attr('requir')=='is_Nu')return true;
	var val=$(obj).val();
	if(val.length > 1)return true; else return false;
}

function TestLocatie(obj){
	if($(obj).attr('requir')=='is_Nu')return true;
	var val=$(obj).val();
	if(val.length > 1)return true; else return false;
}

function TestHTML(obj){
	var id=$(obj).attr('id');
	if($(obj).parent().find('span').is('span'))var date=CKEDITOR.instances[id].getData();
	else var date=$(obj).val();
	date=trim(date.replace(/<[^>]+>/ig,""));
	if(date.length>0)return true;
	else return false;
}

function Setlocation(obj){
	$('body').append('<div id="location" title="Select location"><h3>Selectati localitatea</h3><div id="get_judete">Judete: <select onchange="getLoc(\'get_localitati\',this.value)"></select></div><div id="get_localitati">Localitati: <select><option>select</option></select></div><div id="nfloc"><button style="width:100%;margin-top:30px">Nu am gasit locatia</button></div></div>');
	$("#nfloc button").button();
	$("#nfloc button").click(function(){searchloc($(this),obj);});
	getLoc('get_judete',0);
	$("#get_judete").children('select').change(function(){
//		alert($("div:contains('jud')").closest("div[class*='refist']").find('input[type=text]').val());
//		alert($("div:contains('Jud')").get(0).tagName);
//		alert($("div:contains('jud')").get(0).tagName);
		$("div:contains('jud')").closest("div[class*='refist']").find('input[type=text]').val($(this).children('option:selected').html());
		$("div:contains('Jud')").closest("div[class*='refist']").find('input[type=text]').val($(this).children('option:selected').html());
//		$(obj).val($(this).children('option:selected').html());
	});
	$("#get_localitati").children('select').change(function(){
		$(obj).val($(this).val());
		$("#location").dialog('destroy');
		$("#location").remove();
		});
	$("#location").dialog({
		modal:true,
		draggable:false,
		width:350,
		height:340,
		close:function(){
				$("#location").dialog('destroy');
				$("#location").remove();
			},
		buttons:{
			  OK :function(){
				$("#location").dialog('destroy');
				$("#location").remove();}
			}
		});
}

function getLoc(obj,parinte){//alert(obj);
	$.get('/ajax.php?getloc=true&parinte='+parinte,function(date){$('#'+obj).children('select').html(date);});
}

function searchloc(obj,origin){
  $(obj).parent().prepend('<input type="text" style="width:100%;" class="ui-state-highlight" value="Tastati numele localitatii"/><div id="continer_loc" style="display:none;position:absolute;max-height:80px;overflow:auto; width:89%;padding:2%; z-index:1005; border:1px solid #ddd; border-top:none;" class="ui-corner-bottom ui-state-highlight"></div>');
  var nobj=$(obj).parent().children('input');
  $(nobj).blur(function(){
    if($(this).val()=='')$(this).val('Tastati numele localitatii');
  });
  $(nobj).focus(function(){
    if($(this).val()=='Tastati numele localitatii')$(this).val('');
  });
  $(obj).remove();

  $(nobj).keyup(function(){
	  if($(nobj).val().length>2){
	  $.post('/ajax.php',{searchloc:$(nobj).val()},function(data){
	    $("#continer_loc").html(data);
	    $("#continer_loc").show();
	    $('#continer_loc').find('a').each(function(){
			$(this).css({"display":"block","text-decoration":"none"});
			$(this).click(function(){
				$(origin).val($(this).attr('title'));
					$("#location").dialog('destroy');
					$("#location").remove();
				});
			});
	    $('#continer_loc a').hover(
			function(){$(this).css({"background":"#333","color":"#fff"});},
			function(){$(this).css({"background":"transparent","color":"inherit"});});
	  });
	  }
	  else if($(nobj).val().length<3){$("#continer_loc").html('');$("#continer_loc").hide();}
  });
}

function TestDocu(obj){
	if($(obj).attr('requir')=='is_Nu')return true;
	var val=$(obj).val();
	var da=$(obj).attr('predefined');
	if(da=='da'){if(val.length > 1)return true; else return false;}
	else return true;
}

function Testfield(obj){ //alert($(obj).attr('required'));
	if($(obj).attr('requir')=='is_Nu')return true;
	if($(obj).attr('ftype')=='nr')return TestNr(obj);
	if($(obj).attr('ftype')=='email')return TestEmail(obj);
	if($(obj).attr('ftype')=='html')return TestHTML(obj);
	if($(obj).attr('ftype')=='text')return TestText(obj);
	if($(obj).attr('ftype')=='password')return TestPass(obj);
	if($(obj).attr('ftype')=='select')return TestSelect(obj);
	if($(obj).attr('ftype')=='location')return TestLocatie(obj);
	if($(obj).attr('ftype')=='data')return TestData(obj);
	if($(obj).attr('ftype')=='foto')return TestDocu(obj);
	if($(obj).attr('ftype')=='document')return TestDocu(obj);
	if($(obj).closest('#domeniibox').is(':visible')){
		if($(obj).find('input[type="checkbox"]:checked').length>0) return true;
		if($(obj).find('input[type="checkbox"]:checked').length==0) return false;
	}
	else if($('select[name="drept"]').val()=='angajator')return true;
}

function colorize(obj){
	$(obj).find('input').each(function(){$(this).parent().css({"background":"#fff"});});
	$(obj).find('input:checked').each(function(){//alert($(this).val());
		$(this).parent().css({"background-color":"#bbdbe8"});
		});
	}


function StergFile(id,obj,tabel,bolean){
//  alert($(obj).attr('href'));
  if(!bolean){
  $.confirm({
    title:'Sterg?',
    text:'Doriti sa stergeti acest fisier?<img src="'+$(obj).attr('src')+'" style="max-width:50px; max-height:100px; float:right;">',
    ontrue:function(){StergFile(id,obj,tabel,true);}
    });
  return false;
  }
  var parinte=$(obj).parent();
  
  if($(obj).get(0).tagName=='IMG')var src=$(obj).attr('src');
  else if($(obj).get(0).tagName=='A')var src=$(obj).attr('href');
//  alert(src);
  
  $.post('/ajax_smart.php?altmodul=module/php/HR',{stergfile:src,id:id,tabel:tabel},function(){
	$(obj).remove();
	$(parinte).html('<input type="file" name="'+$(obj).attr('name')+'"/>');
  });
}

function CumparCredit(id,functie){
  $.confirm({
    width:350,
    height:200,
    title:'Credit insuficient',
    text:'<h5>Pentru aceasta actiune ai nevoie de mai multe puncte</h5><p>Puncte disponibile: <b>'+$('body').find('.creditmonitor').html()+'</b><br/>Doresti sa mergi la pagina de achizitie puncte valorice?</p>',
    text_true:"Cumpar puncte",
    text_false:"Nu, continui navigarea",
    ontrue:function(){cumpara();}
  });
}

function cumpara(){
	return document.location.href="/ro/HR:ebona/myaccount/plata.html";
	$.get('/ajax_smart.php?altmodul=mobilpay/sms',function(date){
		$('body').append('<div id="cumparare" title="Pagina de achizitie">'+date+'</div>');
		$("#cumparare button").button().css({"width":"300px"});
		$('#cumparare').dialog({
			modal:true,
			width:600,
			height:500,
			draggable:false,
			resizable:false,
			close:function(){
				$('#cumparare').dialog('destroy');
				$('#cumparare').remove();
			}
			});
		
		});
}

function PublicAction(id){// alert(id);
var status=$(id).attr('status');
(eval(status))?status=0:status=1;

$.post('/ajax.php',{updatecell:status,tabel:$(id).attr('tabel'),id:$(id).attr('mysqlid'),field:$(id).attr('field')},function(){
    $(id).attr('status',status);
    if(status==1){
      $(id).removeClass('inactiv');
      $(id).addClass('activ');
      $(id).button({label:'activ'});
    }
    else{
      $(id).removeClass('activ');
      $(id).addClass('inactiv');
      $(id).button({label:'inactiv'});
    }
});
}

function adaugAngajat(id){
  $.post('/ajax_smart.php?altmodul=module/php/HR',{tabel:$(id).attr('tabel'),addangajat:$(id).attr('mysqlid')},function(){
    if(trim(date)=='login')return logInWindow();
    else {$(id).attr('disabled','disabled');
	  $(id).button({label:'adaugat'});
	  }
  });
}

function ModificCredit(actiune,id,functie,acord){
  
  if(actiune=='achizitie'){
    if(!id)return CumparCredit();
    else return CumparCredit(id,functie);
  }
  else {
    $.post('/ajax_smart.php?altmodul=module/php/HR',{verificactiune:actiune},function(cost){
    if(trim(cost)=='login'){logInWindow();return false;}
    if(trim(cost)=='cumpara')return ModificCredit('achizitie','Credit insuficient',cost);
    if($(id).attr('expirat')=='1')return $.alert({height:160,text:"<p>Acest anunt a expirat.<br>Nu puteti publica un anunt expirat</p>"});
		var credit=cost;
		if(parseInt(credit) < 0)return ModificCredit('achizitie','Credit insuficient',data);
		if(parseInt(credit) ==0 || $(id).attr('status')=='1'){functie(id); return true;}
		else if(eval(credit) >= 0 && !acord)return SetAcord({functie:ModificCredit,actiune:actiune,id:id,fnc:functie,credit:credit});
		else{
		    $.post('/ajax_smart.php?altmodul=module/php/HR',{vandcredit:actiune,id:$(id).attr('mysqlid'),tabel:$(id).attr('tabel'),field:$(id).attr('field')},function(data){
			$('.creditmonitor').html(data);
			functie(id);
			return true;
		    });
		}
    });
  }
  
}

function SetAcord(obj){
  $('body').append('<div id="acord" title="Avertizare consum credit"><h4>Pentru aceasta actiune veti consuma <u>'+obj.credit+'</u> puncte valorice</h4><p style="margin-bottom:0px;">Sunteti de acord sa continuati?</p></div>');
  $("#acord").dialog({
    modal:true,
    draggable:false,
    resizable:false,
    width:350,
    height:190,
    close:function(){$("#acord").dialog('destroy');$("#acord").remove();},
    buttons:{	"Nu, ma opresc aici":function(){$("#acord").dialog('destroy');$("#acord").remove();},
		"Da, merg mai departe":function(){$("#acord").dialog('destroy');$("#acord").remove(); obj.functie(obj.actiune,obj.id,obj.fnc,true);}}
  });
}

function logInWindow(redirecturl){
  $('body').append('<div id="loginDialog" title="Autentificare"><h4>Va rugam sa va autentificati</h4><span class="error"></span><div class="logdiv"><span>email</span><input type="text"/></div><div class="logdiv"><span>parola</span><input type="password"/></div><p>daca nu aveti cont va rugam sa urmati pasii de <a href="javascript:void(0)" onclick="$(\'div.search>button\').click()" title="Inregistrare">inregistre</a></p></div>');
  $('#loginDialog').dialog({
    modal:true,
    draggable:false,
    width:300,
    height:250,
    close:function(){$("#loginDialog").dialog('destroy');$("#loginDialog").remove();},
    buttons:{"renunta":function(){$("#loginDialog").dialog('destroy');$("#loginDialog").remove();},"autentificare":function(){LogAuth(redirecturl);}}
  });
}

function LogAuth(redirecturl){
  var nume=$('#loginDialog').find("input[type='text']").val();
  var pass=$('#loginDialog').find("input[type='password']").val();
  var tabel='ebona';
  $.post('/ajax_smart.php?altmodul=module/php/HR&tab_modul='+tabel,{login_HR:true,user_HR:nume,password_HR:pass,ajax:true},function(data){
    //alert(redirecturl);
    if(trim(data)=='logat'&& redirecturl)document.location.href=redirecturl;
    else if(trim(data)=='logat' && !redirecturl)document.location.reload();
    else $('#loginDialog').find('span.error').html(data);
  });
}

function CautaJob(locatie,catalog){
  document.location.href=catalog+"/"+locatie.replace(/\ /,'+')+'.html';
}

function CautaCandidat(domeniu,locatie,catalog){
  var domeniu=$(domeniu).val();
  var locatie=$(locatie).val();
  
  if(domeniu=='' || locatie=='' || locatie=='select' || !catalog)return false;
  else document.location.href=catalog+'/'+domeniu.replace(/\ /,'+')+'/'+locatie.replace(/\ /,'+')+'.html';
  
}

function postAnunt(id){
  $.post('/ajax_smart.php?altmodul=module/php/HR',{vandcredit:'vizualizareHR',id:$(id).attr('mysqlid'),tabel:$(id).attr('tabel'),field:$(id).attr('field')},function(data){
      if(data=='login')logInWindow($(id).attr('adresa'));
      else document.location.href=$(id).attr('adresa');
  });
}

//function aplicaAnunt(obj,type){ //alert(type);
//	ModificCredit(type,obj,aplicaAnunt2);
//}

function aplicaAnunt(obj){
  
	$.post('/ajax_smart.php?altmodul=module/php/HR',{manageUsers:$(obj).attr('ind'),id:$(obj).attr('mysqlid'),tabel:$(obj).attr('tabel'),sind:$(obj).attr('sind'),sid:$(obj).attr('selfid'),stabel:$(obj).attr('selftabel')},function(date){
		if(trim(date)=='login')return logInWindow();
		$(obj).button({disabled:true});
	});
}

function CumparCV(obj,ind){
		if(!ind){
			ModificCredit($(obj).attr('actiune'),$(obj).attr('mysqlid'),function(){ CumparCV(obj,true); });
			return false;
		}
	//alert(23);
	ChangeStatus(obj);
}

function ChangeStatus(obj){
	var parinte=$(obj).closest('.rowList').parent();
	var idin=$(obj).attr('ind');
	var sidin=$(obj).attr('sind');
	$.post('/ajax_smart.php?altmodul=module/php/HR',{ChanceStatus:$(obj).attr('ind'),sind:$(obj).attr('sind'),sid:$(obj).attr('mysqlid'),stabel:$(obj).attr('tabel')},function(){
		$('#'+idin).find('span').html(parseInt($('#'+idin).find('span').html())+1);
		$('#'+sidin).find('span').html(parseInt($('#'+sidin).find('span').html())-1);
		if($(parinte).children('.rowList').length>1)$(obj).closest('.rowList').remove();
		else if($(parinte).children('.rowList').length==1){
		    $(obj).closest('.rowList').remove();
		    $(parinte).html('<h4>Nu aveti nimic in aceasta sectiune</h4>');
		}
	});
}

function DelUser(obj){
	var parinte=$(obj).closest('.rowList').parent();
	$.post('/ajax_smart.php?altmodul=module/php/HR',{RemUserCV:$(obj).attr('ind'),sid:$(obj).attr('mysqlid'),stabel:$(obj).attr('tabel')},function(){
		if($(parinte).children('.rowList').length>1)$(obj).closest('.rowList').remove();
		else if($(parinte).children('.rowList').length==1){
		    $(obj).closest('.rowList').remove();
		    $(parinte).html('<h4>Nu aveti nimic in aceasta sectiune</h4>');
		}
	});
}

