/****************************************************************************************************************
Fisier care contine functiile js folosite in mod universal 
****************************************************************************************************************/
// Special jquery blur.
// Credits: http://stackoverflow.com/questions/1199293/simulating-focus-and-blur-in-jquery-live-method
(function(){var special=jQuery.event.special,uid1='D'+(+new Date()),uid2='D'+(+new Date()+1);jQuery.event.special.focus={setup:function(){var _self=this,handler=function(e){e=jQuery.event.fix(e);e.type='focus';if(_self===document){jQuery.event.handle.call(_self,e);}};jQuery(this).data(uid1,handler);if(_self===document){if(_self.addEventListener){_self.addEventListener('focus',handler,true);}else{_self.attachEvent('onfocusin',handler);}}else{return false;}},teardown:function(){var handler=jQuery(this).data(uid1);if(this===document){if(this.removeEventListener){this.removeEventListener('focus',handler,true);}else{this.detachEvent('onfocusin',handler);}}}};jQuery.event.special.blur={setup:function(){var _self=this,handler=function(e){e=jQuery.event.fix(e);e.type='blur';if(_self===document){jQuery.event.handle.call(_self,e);}};jQuery(this).data(uid2,handler);if(_self===document){if(_self.addEventListener){_self.addEventListener('blur',handler,true);}else{_self.attachEvent('onfocusout',handler);}}else{return false;}},teardown:function(){var handler=jQuery(this).data(uid2);if(this===document){if(this.removeEventListener){this.removeEventListener('blur',handler,true);}else{this.detachEvent('onfocusout',handler);}}}};})();


function check_email(string) {
	var emailReg = /(^[a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$)/i;
  if (emailReg.test(string)) return true;
  else return false;
}

// proper login
function doLogin() {
	var checkEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = $('#loginEmail').val();
	var pass = $('#loginPass').val();
	var error = false;

	if (!checkEmail.test(email) || email == 'Adresa de email') {
		$('#loginEmail').addClass('wrong');
		error = true;
	}
	if (pass == '' || pass.length < 4) {
		$('#loginPassText').addClass('wrong');
		error = true;
	}

	if (!error) {
		$('#login_box').slideUp('fast', function() {
			$('#login_box').load(base+'formulare/doLogin.php', {doLogin: 'true', loginEmail: email, loginPass: pass});
		});
	}
}

// logout
function doLogout() {
	$('#login_box').slideUp('fast', function() {
		$('#login_box').load(base+'formulare/doLogin.php', {doLogout: 'true'});
	});
}


//functie pentru schimbarea nr de elem pe pag.
function schimbaNrPePag(val) {	
	$('#nrPePag').attr('value', val);
	$('#formSelectie').submit();
}

//functie pentru schimbarea paginii
function mutaPagina(pag) {
	$('#pagina').attr('value', pag);
	$('#formSelectie').submit();
}

$('document').ready(function() {
	$('#data_arhiva').datepicker({
		changeMonth: true,
		changeYear: true
	}); 
	// check user
	$('#login_box').load(base+'formulare/doLogin.php');
	
	// fancy login
	$('#loginEmail, #loginPass').live('focus', function() {
		if ($(this).val() == 'Adresa de email' )
			$(this).val('');
		$(this).removeClass('wrong');
		if ($(this).val() == 'Parola' ) {
			$(this).val('');
		}
	});
	$('#loginEmail').live('blur', function() {
		
		var emailText = 'Adresa de email';
		
		if ($(this).val() == '')
			$(this).val(emailText);
	});
	$('#loginPass').live('keypress', function(e) {
		if (e.keyCode == 13) {
			doLogin();
		}
	});
	$('#loginPassText').live('focus', function() {
		$(this).hide();
		$('#loginPass').show().focus();
	});
	$('#loginPass').live('blur', function() {
		if ($(this).val() == '') {
			$(this).hide();
			$('#loginPassText').show();
		}
	});
	
	  
	  $('#termen').focus(function() {
		if ($(this).attr('value') == 'Termen' ){
		  $(this).attr('value', '');
		}
	  });
	  $('#termen').blur(function() {
		if ($(this).attr('value') == '') {
			$(this).attr('value', 'Termen');
		}
	  });
});
