var Partial = {};
Partial.Login = function (){

    me = this;    
    //input names
    var CREDENTIAL_PASSWORD = '#credentialPassword';
    var CREDENTIAL_USER = '#credentialUser';
    var SEARCH_STRING = '#search-string'; 
    
    //input fields
    var user = null;
    var password = null;
    var s_string = null; 
    
    
    //***************************INITIALIZE VARIABLES***************************
    //**************************************************************************    
    
    me.main = function( ){        
        user = $j( CREDENTIAL_USER );
        password = $j( CREDENTIAL_PASSWORD );
		s_string = $j( SEARCH_STRING );
        fillDefaultText();		
        
		//imgs on cache
		var img1 = new Image();
        img1.src = '../images/buttons/en/btn_login2_on.png';
		var img2 = new Image();
        img2.src = '../images/buttons/en/btn_register_now_on.png';
    }
        
    var fillDefaultText = function( ){
        fusion.ui.fillDefaultText( user, translated_text['enter-user-name'] );
        fusion.ui.fillDefaultText( password, translated_text['password'] );
        fusion.ui.fillDefaultText( s_string, translated_text['search'] );
    } 


    //*************************REGISTRATION FUNCTIONS***************************
    //**************************************************************************    
    /**
    *Function that controls the submit form
    */
    me.onLoginSubmit = function( ){
        
        var args = validateLoginOnSubmit( );
        
        if( args.doSubmit ){
            document.submitLoginForm.submit();
        }
        else{
            var ErrorString = translated_text['invalid-email-and-password'];
            
            popup('<p>'+translated_text['invalid-login']+'<p/><p>'+ErrorString+'<a href="/index/resetpass"> '+translated_text['forgot-password']+'</a><p/>');
			
        }
    }

   /**
    *Function that validates the Registration form fields
    *Returns result:True or Fals, errorMessages: the nessages of the errors
    */
    var validateLoginOnSubmit = function( ){
        // validate login
        
        var errorMessages = 0; //variable for error messages 
        var validate = [ ];  //validations variable
        
        //**is email empty?**
        if ( validate[0] = General.isEmpty( user ) ) {
            errorMessages += 1;
        }
        else {
            //**validate email sintax**
            if ( !(validate[1] = General.validateEmail( user.val() ) ) ) { 
                errorMessages += 1;
            }
        }
        
        //**is password empty?**
		var pass = password.val();
        if ( pass=='' || pass=='*+!fus!0npr0ject!+*') {
            if (errorMessages[0]) 
                errorMessages += 1;
            else 
                errorMessages += 1;
        }
        result = !!(!validate[0] && validate[1] && !validate[2]);
        return {
            'doSubmit': result,
            'errorMessages': errorMessages
        };
    }
    
   /**
    *This function pops up field to write the new password
    */
    me.goPassword = function ()
    {
        var rpForm = 
        '<div id="retrieve-password">' +
            '<label> Email: <input id="rp-email" style="width:255px" name="rp-email" value=""></label>' +
            '<a class="button-purple" href="javascript:Partial.Login.resetPassword();" style="float:right;">' +
                '<div>' +
                   '<div>Reset Password</div>' +
                '</div>' +
            '</a>' +
            '<div class="clear-both"></div>' +
        '</div>';
        popup(rpForm);
        
    }
   /**
    *This function reset the password
    */
    me.resetPassword = function ()
    {
        var email = $j('#rp-email').val();
        goAjax( '/ajax/handle/ajax-action/reset-password', {'email' : email}, afterResetPswd);
    }
    
  /**
    *This function is called after the password is changed
    */
    var afterResetPswd = function (jsonedResponse)
    {
        var response = eval( '(' + jsonedResponse + ')' );

        var dialogTitle = response['status']? 'Success!' : 'Whoops!';
        var msg = $j('#rp-response-tpl').html();
        var msg2 = msg.replace(/{MSG}/g, response['msg']) ;
        popup(msg2);

    }
}
$j(document).ready(function(){
    Partial.Login = new Partial.Login();
    Partial.Login.main();
});

