LoginUtility = {};

LoginUtility.DEFAULT_VALUES = {};

LoginUtility.isContainDefVal = function (elId){
	var e = $(elId);
	var txtValue = e.value;
	var defValue = LoginUtility.DEFAULT_VALUES[e.id];
	if (txtValue.search(defValue) > 0 || txtValue == ''){
		e.value = defValue;
	}
	return e.value;
	
};

LoginUtility.isEmpty = function (elId){
	var e = $(elId);
	var txtValue = e.value;
	var defValue = LoginUtility.DEFAULT_VALUES[e.id];	
	if (txtValue == defValue ){
		e.value = '';
	}
	return e.value;	
};

LoginUtility.setFormToDefaultValue = function(defUsrname, defPwd, fgPwd){
	
	LoginUtility.DEFAULT_VALUES['username'] = defUsrname;
	LoginUtility.DEFAULT_VALUES['password'] = defPwd;
	LoginUtility.DEFAULT_VALUES['forgotpwd'] = fgPwd;
	
	if ($('username') && ($('username').value == null || $('username').value == '')){ 
		$('username').value = LoginUtility.DEFAULT_VALUES['username'];
	}
	if ($('password') && ($('password').value == null || $('password').value == '')){ 
		$('password').value = LoginUtility.DEFAULT_VALUES['password'];
	}
	if ($('forgotpwd') && ($('forgotpwd').value == null || $('forgotpwd').value == '')){ 
		$('forgotpwd').value = LoginUtility.DEFAULT_VALUES['forgotpwd'];
	}
};

LoginUtility.submitLoginForm = function (formId){	
	if ('goLogin' == formId){
		var usrname = LoginUtility.isEmpty('username');
		var pwd = LoginUtility.isEmpty('password_password');
		LoginUtility.isEmpty('password');
		if (usrname != null &&  usrname!= ''&& pwd != null && pwd != '') {
			$(formId).submit();
		} else {
			// Mostro un messaggio di compilazione dei campi
			$('err_req_usr_pwd').show();
		}
	} else if ('forgotForm' == formId){		
		var email = LoginUtility.isEmpty('forgotpwd');
		if ( email != null && email != '' ){
			$(formId).submit();
		} else {
			$('err_req_email').show();
		}
	}
	return false;
};

/**
 * 
 */
LoginUtility.showHideElement = function(elIdShow, elIdHide){
	var divShow = $(elIdShow);
	divShow.show();
	
	var linkShow = $(elIdHide+'_link');	
	
	if (linkShow) {
		linkShow.show();
	}
	
	var divHide = $(elIdHide);
	divHide.hide();
	
	var linkHide = $(elIdShow+'_link');
	
	if (linkHide) {
		linkHide.hide();
	}
	
	if ($('error')){
		$('error').hide();
	}
};

LoginUtility.goToLink = function(where, lang){
	if ('register' == where){
		parent.window.location.href = '/'+lang+'/userRegistration/register.do';
	}
};

/**
 * Update the header login div with info of just logged user,
 * welcomeContent should be headerWelcome.jsp
 */
LoginUtility.updateHeaderWithLoggedUserContent = function(welcomeContent){
	$('userAction').update(welcomeContent);
};

/**
 * 
 */
LoginUtility.switchTextToPassword = function(q){	
    if (q){
         $('password').hide();
         $('password_password').show().focus();
         // document.getElementById('fieldPasswordHead').focus();
	} else {		
		 $('password_password').hide();
         $('password').show().value = LoginUtility.DEFAULT_VALUES['password'];	      
	}
};

LoginUtility.onBlurPassword = function(obj){
	if ($(obj).value.blank()){ 
		LoginUtility.switchTextToPassword(false); 		
	}
};
