_authform = new Object();

_authform.ajax = false;
_authform.backward;
	
_authform.open = function(backward, type, help_title, help_title2)
{
	if (type == undefined) type = 'login'; //signup
	if (type == 'login')
		this.setLogin();
	else
		this.setSignup();
	$('#_auth_form .error').html("");

	if( help_title && help_title.length )
		$('#_auth_form .help_title_id').html(help_title+'<br /><br />');
	else
		$('#_auth_form .help_title_id').html('');

	if( help_title2 && help_title2.length )
		$('#_auth_form #help_title2').html(help_title2);
	else
		$('#_auth_form #help_title2').html('');

	
	toneBody(true);
	
	if( $('#_auth_form').attr('cloned') != "1" )
	{
		var my_form = $('<div cloned="1" id="_auth_form" class="regauth_popup">'+$('#_auth_form').html()+'</div>');
		$('#_auth_form').remove();
		$(my_form).appendTo('body');
	}
	
	$('#_auth_form').show();
	
	$('a').attr('tabindex', '-1');
	$('input').attr('tabindex', '-1');
	$('#_auth_form input[name=login]').attr('tabindex', '1');
	$('#_auth_form input[name=pass]').attr('tabindex', '2');
	$('#_auth_form #rememberme').attr('tabindex', '3');
	$('#_auth_form input:submit').attr('tabindex', '6');
	$('input[name=pass1]').attr('tabindex', 2);
	$('input[name=pass2]').attr('tabindex', 3);
	$('input#terms').attr('tabindex', 4);
	$('input#receive').attr('tabindex', 5);
	
	
	
	$('#_auth_form input[name=login]').focus();
	
	this.backward = (typeof(backward) == 'undefined' || backward=='')?document.location.href:backward;
};

_authform.close = function()
{
	$('a').attr('tabindex', '1');
	$('input').attr('tabindex', '1');	
	if ($('.inv_reg')) {
		$('.inv_reg').remove();
	}
	toneBody(false);
	$('#_auth_form').hide();
};

_authform.setLogin = function()
{
	if (this.ajax) return;
	$('#_auth_reg').hide();
	$('#_auth_login').show();
	$('#_auth_form ul.tabs').find('li:eq(0)').addClass('active');
	$('#_auth_form ul.tabs').find('li:eq(1)').removeClass('active');
};

_authform.setSignup = function()
{
	if (this.ajax) return;
	$('#_auth_reg').show();
	$('#_auth_login').hide();
	$('#_auth_form ul.tabs').find('li:eq(0)').removeClass('active');
	$('#_auth_form ul.tabs').find('li:eq(1)').addClass('active');
};

_authform.setAjax = function(set)
{
	if (set == undefined) set = false;
	if (set)
	{
		$('#_auth_form p[name=loading]').html('<img src="/images/loading.gif" width=50/>');
		$('#_auth_form input').attr('disabled', true);
		this.ajax = true;
	}
	else
	{
		$('#_auth_form p[name=loading]').html('&nbsp;');
		$('#_auth_form input').attr('disabled', '');
		this.ajax = false;
	}
};

_authform.tryLogin = function()
{
	this.setAjax(true);
	var data = new Object();
	data['login'] = $('#_auth_login input[name=login]').val();
	data['pass'] = $('#_auth_login input[name=pass]').val();
	data['ajax'] = 'login';
	$.post('/user/authajax/', data, function(result){
		if (result == '1'){
			//document.location = '/';
			document.location = (_authform.backward)?(_authform.backward):('/');
			//location.href = '/';//_authform.backward;
		}
		else
			$('#_auth_form .error').html("The E-mail address/password combination you've entered is invalid.");
		_authform.setAjax(false);
	});
};

_authform.tryReg = function()
{	
	if( !$('#_auth_reg #terms').attr('checked') )
	{
		alert('Please read and agree to the terms of use!');
		return;
	}
	
	
	// Invite Friend
	var back_link = this.backward.toString();
	
	if( back_link.match(/^[0-9]+/) )
	  back_link = location.href + back_link;
	
	
	var full_linker = window.location.href.split('?');
	var inv_link=false;
	if( full_linker[1] )
	{
		var par_linker = full_linker[1].toString().split('&');
		var arr;
		for( i in par_linker )
		{
			if(par_linker[i])
			{
				arr = new Array();
				arr = par_linker[i].toString().split('=');
				if( arr[0] == 'invite_link' )
					break;
			}
		}
		//alert(arr[0]);
		if( arr[0] == 'invite_link' )
			inv_link = arr[1];
	}
	
	// Invite friends
	var inv_input = '';
	if ($('#invite_hash_input')) {
		inv_input = '<input type="hidden" name="invite_hash" value="'+$('#invite_hash_input').val()+'" />';
		back_link='/';
    }
	
	$('<form id="popup_reg_form_hidden" action="/user/registration/?backward='+back_link+'" method="post">'+
	  ((inv_link)?('<input type="hidden" name="invite_link" value="'+inv_link+'" />'):(''))+
	  '<input type="hidden" name="type" value="registration" />'+
	  '<input type="hidden" name="login" value="'+$('#_auth_reg input[name=login]').val()+'" />'+
	  '<input type="hidden" name="pass" value="'+$('#_auth_reg input[name=pass1]').val()+'" />'+
	  '<input type="hidden" name="cpass" value="'+$('#_auth_reg input[name=pass2]').val()+'" />'+
	  '<input type="hidden" name="terms" value="'+(($('#_auth_reg #terms').attr('checked')===true)?(1):(0))+'">'+
	  '<input type="hidden" name="receive" value="'+(($('#_auth_reg #receive').attr('checked')===true)?(1):(0))+'">'+
	  inv_input+
	  '</form>').appendTo('body');
	$('form#popup_reg_form_hidden').submit();
}

_authform.forgetOpen = function()
{
	toneBody(true);
	loading(true);
	$('#_auth_form').hide();
	$('#_auth_reg').hide();	
	
    $.get('/user/forget/show', function(data){
		loading(false);
		$('<div id="_auth_form_forget">'+data+'</div>')
		.appendTo('body');
    });
}

_authform.forgetClose = function()
{
	$('#_auth_form_forget').remove();
	toneBody(false);
}

_authform.forgetSend = function()
{
        if( $('#preloader_forgot_password').css('visibility') != 'hidden' )
          return;

	$('#preloader_forgot_password').css('visibility', 'visible');

	$.post('/user/forget/forget', { "login": $('#FORGET_INPUT_ROW').val(), "type": "forget" }, function(data){
	    $('#_auth_form_forget').html(data);
	});
}

function open_authform( type, backward, help_title ) {_authform.open(type, backward, help_title);}
function open_forget( type, backward ) {_authform.forgetOpen();}

function close_only_auth_form()
{
	$('#_auth_form').hide();
}
