dojo.setObject( 'nuvek.vektr.Login', {
	first_focus : false,
  chrome : navigator.userAgent.toLowerCase().indexOf('chrome') > -1,
  safari : navigator.userAgent.toLowerCase().indexOf('safari') > -1,

	templates : {
		login : '<input class="textfield" type="text" name="username" id="username" ' +
			'value="Username" onclick="javascript:this.focus()" onfocus="nuvek.vektr.Login.changeOnFocus( this, \'Username\' )" ' +
			'onblur="nuvek.vektr.Login.changeOnBlur( this, \'Username\' )" onkeydown="' +
			'nuvek.vektr.Login.executeOnEnter( event, this, \'login\' )" style="font-style: italic;color:#636363;" />&nbsp<input ' +
			'class="textfield" type="text" name="password" id="password" value="Password" ' +
			'onfocus="nuvek.vektr.Login.changeOnFocus( this, \'Password\', true )" ' +
			'onblur="nuvek.vektr.Login.changeOnBlur( this, \'Password\', true )" onkeydown="' +
			'nuvek.vektr.Login.executeOnEnter( event, this, \'login\' )" ' +
			'style="margin-left: 5px;font-style: italic;color:#636363;" /><input id="login_btn" name="login_btn" type="image" src="/login/png/btn_login.png" ' +
			'style="margin-left: 5px; vertical-align: middle;" onclick="nuvek.vektr.Login.login()" />' +
      '<br />'	+
      '<div id="remember_div" style="padding-right: 145px; color: #ffffff; font-weight: bold;">' +
      '<input type="checkbox" name="remember_me" id="remember_me" /> Keep me logged in' +
      '</div>',
      
    error_login_android_show_download : '<table><tr>' +
      '<div class="error" style="margin-bottom: 5px"><font color="red"><b>Your Username/Password Combination is Incorrect</b></font></div></tr><tr>' +
      '<td><input type="text" name="username" id="username" value="Username" onClick="this.value=\'\';"/></td>' +
      '<td><input type="text" name="password" id="password" value="Password" onClick="this.type=\'password\'; this.value=\'\';"/></td>' +
      '<td><input type="button" id="login" value="Login" onclick="nuvek.vektr.Login.login( this, true, true );"></td>' +
      '<td><input type="button" id="download_vektr" value="Download ' + CUSTOM_APP_NAME + ' for Android" onclick="nuvek.vektr.Login.login( this, true, true );"></td>' +
      '</tr></table>',
      
    error_login_android_not_supported : '<table><tr>' +
      '<div class="error" style="margin-bottom: 5px"><font color="red"><b>Your Username/Password Combination is Incorrect</b></font></div></tr><tr>' +
      '<td><input type="text" name="username" id="username" value="Username" onClick="this.value=\'\';"/></td>' +
      '<td><input type="text" name="password" id="password" value="Password" onClick="this.type=\'password\'; this.value=\'\';"/></td>' +
      '<td><input type="button" id="login" value="Login" onclick="nuvek.vektr.Login.login( this, true, true );"></td>' +
      '<td><b>This version of Android is not <br />supported to get the Android App</b></td>',
      
    error_login_android : '<table><tr>' +
      '<div class="error" style="margin-bottom: 5px"><font color="red"><b>Your Username/Password Combination is Incorrect</b></font></div></tr><tr>' +
      '<td><input type="text" name="username" id="username" value="Username" onClick="this.value=\'\';"/></td>' +
      '<td><input type="text" name="password" id="password" value="Password" onClick="this.type=\'password\'; this.value=\'\';"/></td>' +
      '<td><input type="button" id="login" value="Login" onclick="nuvek.vektr.Login.login( this, true );"></td>',
      
	  error_login : '<div class="error" style="margin-bottom: 5px">Your Username/Password ' +
			'Combination is Incorrect</div><input type="image" src="/login/png/btn_try_again.png" ' +
			'style="margin-left: 5px; vertical-align: middle;" onclick="nuvek.vektr.' +
			'Login.changeFormHTML( \'login\' )" /><input type="image"' +
			' src="/login/png/btn_get_a_reminder.png" style="margin-left: 5px; vertical-align: middle;" ' +
			'onclick="nuvek.vektr.Login.changeFormHTML( \'email_reminder\' )" />',
			
		email_reminder : '<div style="margin-bottom: 5px">Enter your Email Address ' +
			'below and then click on submit</div><input class="textfield" type="text" ' +
			'name="email" id="email" value="Email" onfocus="nuvek.vektr.Login.changeOnFocus( ' +
			'this, \'Email\' )" onblur="nuvek.vektr.Login.changeOnBlur( this, \'Email\' )" ' +
			'style="margin-left: 5px;" onkeydown="nuvek.vektr.Login.executeOnEnter( ' +
			'event, this, \'remind\' )"/><input type="image" src="/login/png/btn_submit.png"' +
			' style="margin-left: 5px; vertical-align: middle;" onclick="nuvek.vektr.' +
			'Login.remind()"/><input type="image" ' +
			'src="/login/png/btn_try_again.png" style="margin-left: 5px; vertical-align: ' +
			'middle;" onclick="nuvek.vektr.Login.changeFormHTML( \'login\' )" />',
			
		reminder_confirm : '<div style="margin-bottom: 5px">Your password reminder ' +
			'has been sent to $email_adress</div>' +
			'<input type="image" src="/login/png/btn_try_again.png" style="margin-left: 5px; vertical-align: ' +
			'middle;" onclick="nuvek.vektr.Login.changeFormHTML( \'login\' )" />',
			
		reminder_fail : '<div class="error" style="margin-bottom: 5px">Your E-mail Address is not currently in our' +
			'database of users.</div><input type="image" ' +
			'src="/login/png/btn_try_again.png" style="margin-left: 5px; vertical-align: ' +
			'middle;" onclick="nuvek.vektr.Login.changeFormHTML( \'login\' )" /><input type="image"' +
			' src="/login/png/btn_get_a_reminder.png" style="margin-left: 5px; vertical-align: middle;" ' +
			'onclick="nuvek.vektr.Login.changeFormHTML( \'email_reminder\' )" />',
		
		user_disabled : '<div class="error" style="margin-bottom: 5px">User $username can not log in' +
			'</div><input type="image" ' +
			'src="/login/png/btn_try_again.png" style="margin-left: 5px; vertical-align: ' +
			'middle;" onclick="nuvek.vektr.Login.changeFormHTML( \'login\' )" />',
			
		loading : '<div id="message">$message</div>'
	},
	changeOnFocus : function( element, default_value, is_password ) {
		if( nuvek.vektr.Login.first_focus )
			return;
		if( element.value == default_value ) {
			element.value = '';
			element.style.fontStyle = 'normal';
			element.style.color = 'black';
			if( is_password && element.type != 'password' ) {
				var new_element = document.createElement( 'input' );
				new_element.type = 'password';
				new_element.className = 'textfield';
				new_element.style.marginLeft = element.style.marginLeft;
				new_element.style.fontStyle = element.style.fontStyle;
				new_element.style.color = element.style.color;
				new_element.name = new_element.id = element.id;
				new_element.onfocus = element.onfocus;
				new_element.onblur = element.onblur;
				new_element.onkeydown = element.onkeydown;
				element.parentNode.replaceChild( new_element, element );
				setTimeout( function() {
					new_element.focus();
				}, 100 );
			}
		}
	},
	changeOnBlur : function( element, default_value, is_password ) {
    var NVL = nuvek.vektr.Login;
		if( element.value == '' ) {
			if( is_password && !NVL.chrome ) {
        try {
          var type_text = element.type == 'text';
          var new_element = document.createElement( 'input' );
          new_element.type = 'text';
          new_element.className = 'textfield';
          new_element.style.marginLeft = element.style.marginLeft;
          new_element.name = new_element.id = element.id;
          new_element.onfocus = element.onfocus;
          new_element.onblur = element.onblur;
          element.parentNode.replaceChild( new_element, element );
          element = new_element;
          if( NVL.safari && type_text ) {
            texts = new Array();
            texts = document.getElementsByTagName( 'input' );
            for( k = 0; k < texts.length; k++ ) {
              if( texts[k].type.toLowerCase() == 'text' && texts[k].id.toLowerCase() == 'password' ) {
                texts[k].parentNode.removeChild( texts[k] );
              }
            }
          }
        } catch( e ) {}
			} else if( !NVL.chrome ) {
        NVL.changeOnBlur( dojo.byId( 'password' ), 'Password', true );
      }
			element.style.color = '#636363';
			element.style.fontStyle = 'italic';
			element.value = default_value;
		} else if( dojo.byId( 'password' ) && dojo.byId( 'password' ).value == '' && !is_password ) {
      NVL.changeOnBlur( dojo.byId( 'password' ), 'Password', true );
    }
    NVL.first_focus = false;
    NVL.checkRememberedValue();
	},
	changeOnEditUsername : function( keyCode ) {
    if( dojo.byId( 'password' ) ) {
      var password_field = dojo.byId( 'password' );
      if( keyCode > 36 && keyCode < 41 ) {
        return;
      }
      if( password_field.value != 'Password' ) {
        password_field.value = '';
        dojo.byId( 'remember_me' ).checked = false;
        setTimeout( function() {
          nuvek.vektr.Login.changeOnBlur( password_field, 'Password', true );
        }, 100 );
        setTimeout( function() {
          dojo.byId( 'username' ).focus();
        }, 100 );
      }
    }
	},
	changeFormHTML : function( key, html_args, on_finish ) {
		var NVL = nuvek.vektr.Login;
		replace_html = NVL.templates[key];
		if( html_args )
			for( var i in html_args )
				replace_html = replace_html.replace( '$' + i, html_args[i] );
    NVL.first_focus = false;
    dojo.anim( 'login_form_content', { 'opacity' : 0 }, null, null, function() {
			dojo.byId( 'login_form_content' ).innerHTML = replace_html;
			dojo.anim( 'login_form_content', { 'opacity' : 1 }, null, null, on_finish );
		} );
	},
  checkRememberedValue : function() {
    if( dojo.byId( 'password' ) ) {
      var NVL = nuvek.vektr.Login;
      var login_value = dojo.byId( 'password' );
      var empy_pass = login_value.type == 'text' || login_value.value == 'Password' || login_value.value == '';
      if( NVL.ifValueExist( dojo.byId( 'username' ).value ) && empy_pass ) {
        login_value.disabled = 'disabled';
        NVL.keepLoginAction( 3, true );
      }
    }
  },
  ifValueExist : function( account ) {
    var index = 0, file_exist = false;
    var all_files_string = document.cookie;
    if( all_files_string == '' ) {
      return false;
    }
    var files = new Array(4);
    var sub_files = new Array(2);
    files = all_files_string.split( '; ' );
    while( files[index] && !file_exist ) {
      all_files_string = files[index];
      sub_files = all_files_string.split( '=' );
      if( sub_files[0] == account ) {
        file_exist = true;
      } else {
        index++;
      }
    }
    return file_exist;
  },
  keepLoginAction : function( action, synchronous ) {
    set = action == 1 || action == 2;
    dojo.xhrPost( {
      'url' : '/profile.php',
      'sync' : synchronous,
      'content' : {
        'cmd' : 'remind',
        'keep': true,
        'action' : action,
        'account' : dojo.byId( 'username' ).value,
        'value' : set ? dojo.byId( 'password' ).value : ''
      },
      'handleAs' : 'json',
      'handle' : function( response ) {
        if( !set ) {
          var NVL = nuvek.vektr.Login;
          NVL.changeOnFocus( dojo.byId( 'password' ), 'Password', true );
          login_value = dojo.byId( 'password' );
          login_value.value = response.script;
          NVL.changeOnBlur( login_value, 'Password', true );
          dojo.byId( 'remember_me' ).checked = true;
          dojo.byId( 'password' ).disabled = '';
        }
      }
    } );
  },
	login : function( object, is_android, show_download ) {
		var username = dojo.byId( 'username' );
		var password = dojo.byId( 'password' );
		if( username.value == '' || username.value == 'Username' || password.value == '' ||
			password.type == 'text' ) {
			alert( 'Please provide the username and password' );
			return;
		}
		username = username.value;
		password = password.value;
		var NVL = nuvek.vektr.Login;
		NVL.changeFormHTML( 'loading', { 'message' : 'Logging in...' } );
		dojo.xhrPost( {
			'url' : '/profile.php',
			'content' : {
				'cmd' : 'login',
				'username' : username,
				'password' : password,
				'ajax_login' : true
			},
			'handleAs' : 'json',
			'handle' : function( response ) {
				NVL.loginResponse( response, username, object, is_android, show_download );
			}
		} );
    NVL.keepLoginAction( ( dojo.byId( 'remember_me' ).checked ? 1 : 2 ), false );
	},
  loginResponse : function( response, username, object, is_android, show_download ) {
    var NVL = nuvek.vektr.Login;
    if( response.status == 'FAILURE' ) {
      if( is_android == true ) {
        is_android_supported = dojo.byId( 'is_android_supported' );
        if( show_download ) {
          if( is_android_supported )
            NVL.changeFormHTML( 'error_login_android_show_download' );
          else if( is_android_supported == null )
            NVL.changeFormHTML( 'error_login_android_not_supported' );
        }
        else
          NVL.changeFormHTML( 'error_login_android' );
        return;
      } else {
        NVL.changeFormHTML( 'error_login' );
        return;
			}
		}
		if( response.status == 'DENIED' ) {
			NVL.changeFormHTML( 'user_disabled', { 'username' : username } );
			return;
		}
		NVL.changeFormHTML( 'loading', { 'message' : 'Redirecting...' } );
		var redirect = dojo.byId( 'redirect' ).value;
		if( is_android && object.id == 'download_vektr' )
			redirect = dojo.byId( 'redirect_to_drupal' ).value;
		if( redirect == '' || redirect == '/' )
			redirect = !response.default_page ? '/' : response.default_page;

    setTimeout( function() {
      window.location = redirect;
    }, 150 );
	},
	remind : function() {
		var NVL = nuvek.vektr.Login;
		var email = dojo.byId( 'email' ).value;
		if( email == '' || email == 'Email' ) {
			alert( 'Please provide a valid Email' );
			return;
		}
		NVL.changeFormHTML( 'loading', { 'message' : 'Loading...' } );
		dojo.xhrPost( {
			'url' : '/profile.php',
			'content' : {
				'cmd' : 'remind',
				'mail' : email,
				'email' : email,
				'is_ajax' : true
			},
			'handleAs' : 'json',
			'handle' : function( response ) {
				NVL.changeFormHTML( 'reminder_' + ( response.status == 'SUCCESS'
					? 'confirm'
					: 'fail' ), { 'email_adress' : email } );
			}
		} );
	},
	setFocus : function( element, first_focus ) {
		var NVL = nuvek.vektr.Login;
		NVL.first_focus = first_focus;
		if( dojo.isString( element ) )
			element = dojo.byId( element );
		element.focus();
	},
	executeOnEnter : function( event, element, function_name ) {
		var NVL = nuvek.vektr.Login;
		event = event || window.event;
		if( event.keyCode != 13 && NVL.first_focus ) {
			element.value = '';
			element.style.fontStyle = 'normal';
			element.style.color = 'black';
			NVL.first_focus = false;
			return;
		}
		NVL.first_focus = false;
		if( event.keyCode == 13 ) {
      NVL.checkRememberedValue();
			NVL[function_name]();
		} else if( element.id != 'password' ) {
      NVL.changeOnEditUsername( event.keyCode );
    }
	},
	preloadImages : function( image_list ) {
		if( !dojo.isArray( image_list ) )
			image_list = [image_list];
			
		for( var i = 0; image_list[i]; i++ ) {
			var loading_img = document.createElement( 'img' );
			loading_img.src = image_list[i];
		}
	}
} );

( function() {
	var NVL = nuvek.vektr.Login;
	NVL.preloadImages( ['/images/uploading.gif'] );
	dojo.addOnLoad( function() {
		NVL.setFocus( 'username', true )
	} );
} )()