function lang( id )
{
	return agent.call('ajax.php', 'lang', '', id);
}

function plano_info( plano_id )
{
	var obj = document.getElementById('plano_info');
	obj.innerHTML = lang(73);
	obj.style.display = 'block';

	var str = agent.call('ajax.php', 'plano_info', '', plano_id);

	if( str == false )
	{
		obj.style.display = 'none';
		naujas_vhost( false );
	}
	else
	{
		obj.innerHTML = str;
		naujas_vhost( plano_id );
	}

	naujas_galiojimas();
}

function plano_info_2012( plano_id )
{
	var obj = document.getElementById('plano_info');
	obj.innerHTML = lang(73);
	obj.style.display = 'block';

	var str = agent.call('ajax.php', 'plano_info', '', plano_id);

	if( str == false )
	{
		obj.style.display = 'none';
		//naujas_vhost( false );
	}
	else
	{
		obj.innerHTML = str;
		//naujas_vhost( plano_id );
	}

	//naujas_galiojimas();
}

function vhost_plan( vhost )
{
	var obj = document.getElementById('vhost_plan');
	obj.innerHTML = lang(73);
	obj.style.display = 'block';

	var str = agent.call('ajax.php', 'vhost_info', '', vhost);

	if( str == false )
	{
		obj.style.display = 'none';
	}
	else
	{
		obj.innerHTML = str;
	}
	
	try
	{
		naujas_galiojimas();
		changeUsername();
	}
	catch (e)
	{
	}
}

var plano_tsk = 0;

function vhost_continue( vhost )
{
	var obj = document.getElementById('vhost_continue');

	if( vhost == 0 )
	{
		obj.style.display = 'none';

		var obj = document.getElementById('months_continue');
		if( obj )
		{
			obj.style.display = 'none';
		}
	}
	else
	{
		obj.innerHTML = lang(73);
		obj.style.display = 'block';

		agent.call('ajax.php', 'vhost_continue', 'callback_vhost_continue', vhost);
	}
}

function callback_vhost_continue( str )
{
	var obj = document.getElementById('vhost_continue');
	if( str == false )
	{
		obj.style.display = 'none';
	}
	else
	{
		plano_tsk = str['planPoints'];
		obj.innerHTML = str['output'];
		months_continue();
	}
}

var user_tsk = false;

function months_continue()
{
	var obj = document.getElementById('months_continue');
	if( obj )
	{
		if( user_tsk == false )
		{
			user_tsk = agent.call('ajax.php', 'user_points', '');
		}

		var value = document.monform.month.value;

		if( value > 0 && plano_tsk > 0)
		{
			obj.style.display = 'block';
			
			var tasku = value*plano_tsk;
			if( tasku <= user_tsk )
			{
				obj.innerHTML = agent.call('ajax.php', 'months_continue', '', tasku);
				submit_disable( false );
			}
			else
			{
				obj.innerHTML = lang(181);
				submit_disable( true );
			}
		}
		else
		{
			obj.style.display = 'none';
		}
	}

	var objVhost = document.getElementById('vhost_continue');
	if( objVhost )
	{
		if( objVhost.style.display == 'none' )
		{
			obj.style.display = 'none';
		}
	}
}

function naujas_galiojimas()
{
	var ngt = document.getElementById('naujasGaliojimasTR');
	if( ngt )
	{
		var vhost = document.monform.vhost.value;
		var plan = document.monform.planas.value;

		if( vhost != '0' && plan != '0' )
		{
			ngt.style.visibility = 'visible';
		
			var ng = document.getElementById('naujasGaliojimas');
			ng.innerHTML = lang(73);

			var str = agent.call('ajax.php', 'vhost_naujas_galiojimas', '', vhost, plan );
			if( str == false )
			{
				ng.innerHTML = lang(175);
				submit_disable( true );
			}
			else
			{
				ng.innerHTML = str;
				submit_disable( false );
			}
		}
		else
		{
			ngt.style.visibility = 'hidden';
		}
	}
}

function svetaines_veiksmai( vhost, act )
{
	if( act != '' )
	{	
		var str = agent.call('ajax.php', 'svetaines_veiksmai', '', vhost, act);

		if( act == 'delete' )
		{
			var a = confirm(lang(176));

			if( a == true )
			{
				location.href = str;
			}
		}
		else
		{
			location.href = str;
		}
	}
}

function changeUsername()
{
	var ut = document.getElementById('usernameTR');
	if( ut )
	{
		var vhost = document.monform.vhost.value;
		if( vhost != '0' )
		{
			ut.style.visibility = 'visible';
		
			var u = document.getElementById('username');
			u.innerHTML = lang(73);

			var str = agent.call('ajax.php', 'vhost_username', '', vhost);
			if( str == false )
			{
				ut.style.visibility = 'hidden';
			}
			else
			{
				u.innerHTML = str;
			}
		}
		else
		{
			ut.style.visibility = 'hidden';
		}
	}
}

function vhost_activate( id, domain, username )
{
	var obj = document.getElementById("vhostActivate["+id+"]");
	var objLink = document.getElementById("vhostActivationLink["+id+"]");
	
	obj.innerHTML = lang(73);
	objLink.style.display = 'none';

	agent.call( 'ajax.php', 'vhost_activate', 'callback_vhost_activate', id, domain, username );
}

function callback_vhost_activate( str )
{
	var obj = document.getElementById("vhostActivate["+str['id']+"]");
	var objLink = document.getElementById("vhostActivationLink["+str['id']+"]");

	if( str['status'] == false )
	{
		if( str['error'] == true ){
			objLink.style.display = 'block';
			obj.innerHTML = str['alert'];
		}else{
			objLink.style.display = 'block';
			obj.innerHTML = lang(184);
		}
		
	}
	else
	{
		alert(str['alert']);
		history.go();
	}
}

function shorten_vhost( vhost )
{
	submit_disable( true );
	var str = agent.call('ajax.php', 'svetaines_veiksmai', '', vhost, 'shorten');
	location.href = str;
}

function shorten_dates( expires )
{
	var obj = document.monform.vhost;
	
	if( obj )
	{
		if( expires > 0 )
		{
			agent.call( 'ajax.php', 'shorten_dates', 'callback_shorten_dates', obj.value, expires );
		}
		else
		{
			 document.getElementById('shorten_dates').style.display = 'none';
		}
	}
}

function callback_shorten_dates( str )
{
	var obj = document.getElementById('shorten_dates');
	obj.style.display = 'block';
	obj.innerHTML = str;
}

function naujas_vhost( plano_id )
{
	var obj = document.getElementById('naujas_vhost');

	if( obj )
	{
		if( plano_id == false )
		{
			obj.style.display = 'none';
		}
		else
		{
			obj.innerHTML = lang(73);
			obj.style.display = 'block';
	
			var str = agent.call( 'ajax.php', 'new_plan', '', plano_id );
			if( str == false )
			{
				submit_disable( true );
				obj.innerHTML = lang(190);
			}
			else
			{
				submit_disable( false );
				obj.innerHTML = str;
			}

		}
	}
}

function submit_disable( d )
{
	try
	{
		var submit = document.monform.submit;

		if( submit )
		{
			submit.disabled = d;
		}		
	}
	catch (e)
	{
	}
}

function extra_veiksmai( id, act )
{
	if( act != '' )
	{
		var str = agent.call('ajax.php', 'extra_veiksmai', '', id, act);

		if( act == 'delete' )
		{
			var a = confirm(lang(194));

			if( a == true )
			{
				location.href = str;
			}
		}
		else
		{
			location.href = str;
		}
	}
}

function vhost_extra( extraID )
{
	var obj = document.getElementById('vhost_extra');
	if( extraID > 0 )
	{
		var str = agent.call('ajax.php', 'vhost_extra', '', extraID);

		obj.style.display = 'block';
		obj.innerHTML = str;
	}
	else
	{
		obj.style.display = 'none';
	}

	extra_continue();
}

function extra_continue()
{
	var obj = document.getElementById('months_continue');
	if( obj )
	{
		var value = document.monform.month.value;
		var extraID = document.monform.id.value;
		
		if( extraID == 0 )
		{
			obj.style.display = 'none';
		}
		else
		{
			agent.call('ajax.php', 'extra_continue', 'callback_extra_continue', extraID, value);
		}
	}
}

function callback_extra_continue( str )
{
	var obj = document.getElementById('months_continue');
	obj.style.display = 'block';
	
	if( str == 'hide' )
	{
		obj.style.display = 'none';
	}
	else if( str == false )
	{
		obj.innerHTML = lang(221);
		submit_disable( true );
	}
	else
	{
		obj.innerHTML = str;
		submit_disable( false );	
	}
}

function extra_plan( name, n )
{
	submit_disable( true );

	var str = agent.call('ajax.php', 'extra_plan', '', name, n);
	location.href = str;
}

function extra_activate( id )
{
	var str = agent.call('ajax.php', 'extra_activate', '', id);

	if( str == false )
	{
		alert(lang(206));
	}
	else
	{
		alert(str);
		history.go();
	}
}

function extra_shorten( id )
{
	submit_disable( true );

	var str = agent.call('ajax.php', 'extra_shorten', '', id);
	location.href = str;
}

function extra_shorten_dates( expires )
{
	var obj = document.monform.id;

	if( obj )
	{
		agent.call( 'ajax.php', 'extra_shorten_dates', 'callback_shorten_dates', obj.value, expires );
	}
}

function callback_extra_shorten_dates( str )
{
	var obj = document.getElementById('shorten_dates');
	obj.style.display = 'block';
	obj.innerHTML = str;
}

function new_extra( planCost )
{
	try
	{
		var quanity = document.monform.quanity;

		if( quanity )
		{
			quanity = quanity.value;
			quanity = parseInt(quanity);
			
			if( isNaN(quanity) || quanity <= 0 )
			{
				quanity = 1;
			}
		}
		else
		{
			quanity = 1;
		}		
	}
	catch (e)
	{
		quanity = 1;
	}

	var obj = document.getElementById( 'new_extra' );
	obj.style.display = 'block';

	str = agent.call( 'ajax.php', 'new_extra', '', quanity*planCost );

	if( str == false )
	{
		obj.innerHTML = lang(219);
		submit_disable( true );
	}
	else
	{
		obj.innerHTML = str;
	}
}

function homeplan( value, orderHref, orderText, compareHref, compareText )
{
	if( value != 0 )
	{		
		var obj = document.getElementById('infobox');
		
		obj.innerHTML = lang(73);
		obj.innerHTML = agent.call( 'ajax.php', 'homeplan', '', value );

		document.getElementById('orderLink').innerHTML = '<a href="' + orderHref + '&plan=' +value+ '">' + orderText + '</a>';
		document.getElementById('orderLink').style.display = 'block';

		document.getElementById('compareLink').innerHTML = '<a href="' + compareHref + '">' + compareText + '</a>';
		document.getElementById('compareLink').style.display = 'block';
	}
}

function autocontinueVhost( domain )
{
	var waitScreen = document.getElementById('autoWaitScreen');
	waitScreen.style.display = 'block';

	var status = agent.call( 'ajax.php', 'autocontinueVhost', '', domain );
	
	var objStatus = document.getElementById('autoStatus');
	var objLink = document.getElementById('autoLink');

	if( status == true )
	{
		alert( 'Automatinis pratęsimas svetainei ' + domain + ' buvo įjungtas!' );
		objLink.innerHTML = 'išjungti';
		objStatus.innerHTML = 'įjungtas';
	}
	else
	{
		alert( 'Automatinis pratęsimas svetainei ' + domain + ' buvo išjungtas!' );
		objLink.innerHTML = 'įjungti';
		objStatus.innerHTML = 'išjungtas';
	}

	waitScreen.style.display = 'none';
}

function autocontinueExtra( extraID )
{
	var waitScreen = document.getElementById('autoWaitScreen');
	waitScreen.style.display = 'block';

	var status = agent.call( 'ajax.php', 'autocontinueExtra', '', extraID );
	
	var objStatus = document.getElementById('autoStatus');
	var objLink = document.getElementById('autoLink');

	if( status == true )
	{
		alert( 'Automatinis pratęsimas papildomai paslaugai buvo įjungtas!' );
		objLink.innerHTML = 'išjungti';
		objStatus.innerHTML = 'įjungtas';
	}
	else
	{
		alert( 'Automatinis pratęsimas papildomai paslaugai buvo išjungtas!' );
		objLink.innerHTML = 'įjungti';
		objStatus.innerHTML = 'išjungtas';
	}

	waitScreen.style.display = 'none';
}

function vhosts_update( order )
{
	$.ajax({
		url: location.href,
		type: 'post',
		data: 'update=true&order='+order,
		complete: function( r )
		{
			$('#sort_actions li').removeClass( "b" );
			$('#sort_actions_'+order).addClass( "b" );
			$('#vhost_plans').fadeOut('slow', function(){
				$('#vhost_plans').html(r.responseText).fadeIn('slow');
			});
		}
	});
}
