var last_timestamp=0;
var in_process=0;
var global_domain='';

function run(dom_ob)
{
	if(dom_ob.type=="text")
	{
		in_process=1;
		var d=new Date();
		last_timestamp=d.getTime();
	}
	else
	{
		go_ob(dom_ob);
	}
}

$(function()
{
	$('#domain').bind('keydown',function(e){ 
		if(e.which==13)
		{
			global_domain="";//reset is done
		}
	});
});

function go()
{
	$('.to_hide').hide();
	$('.to_show').show();
	
	var domain = $('#domain').attr('value');
	if(domain!="" && domain!=global_domain)
	{
		global_domain=domain;
		$.each($('.zone'),function(key,zone)
		{
			var full_name = domain + $(zone).attr('value');
			var dom_el=document.getElementById($(zone).attr('value'));
			$(dom_el).children('.res_av').html("<center><img src='images/loading.gif'></center>");
			$(dom_el).show();
			
			if($(zone).attr('checked')==true)
			{
				$(dom_el).children('.res_av').css('color','#000');
				$(dom_el).children('.res_dom').css('color','#000');
				
				$(dom_el).children('.res_dom').html( $(zone).attr('value') );
				$(dom_el).children('.res_blue').html("&nbsp;");
				$(dom_el).children('.black1').children('.amd').html("&nbsp;");
				$(dom_el).children('.black1').children('.usd').html("&nbsp;");
				
				$.post('checkdomain.php',{domain:full_name},function(data){
						
						if($('#domain').attr('value') + $(zone).attr('value')==full_name)
						{
							$(dom_el).children('.res_dom').html( $(zone).attr('value') );
							
							if(data=='1'+full_name)
							{
								$(dom_el).children('.res_av').css('color','#093');
								$(dom_el).children('.res_dom').css('color','#093');
								$(dom_el).children('.res_av').html(t_available);
								$(dom_el).children('.res_blue').html("<a class='res_blue' href='http://www.ds.am/index.php?option=com_user&view=both&from=register&domains="+full_name+"' target='_blank'>"+t_register+"</a>");
								$(dom_el).children('.black1').children('.amd').html('('+prices_amd[key]+'AMD)');
								$(dom_el).children('.black1').children('.usd').html('($'+prices_usd[key]+')');
							}
							else if(data=='2'+full_name)
							{
								$(dom_el).children('.res_av').css('color','#ff0000');
								$(dom_el).children('.res_dom').css('color','#ff0000');
								$(dom_el).children('.res_dom').html( "<a href='http://"+full_name+"' style='color:#ff0000;'  target='_blank'>"+$(zone).attr('value')+"</a>" );
								$(dom_el).children('.res_av').html(t_not_available);
								$(dom_el).children('.res_blue').html("<a class='res_blue' href='http://www.ds.am/index.php?option=com_domainshop&view=whois&domain="+full_name+"' target='_blank'>"+t_whois+"</a>");
								$(dom_el).children('.black1').children('.amd').html('('+prices_amd[key]+'AMD)');
								$(dom_el).children('.black1').children('.usd').html('($'+prices_usd[key]+')');
							}
							else if(data=='3'+full_name)
							{
								$(dom_el).children('.res_av').css('color','#90C');
								$(dom_el).children('.res_dom').css('color','#90C');
								$(dom_el).children('.res_av').html(t_on_hold);
								$(dom_el).children('.res_blue').html("<a class='res_blue' href='http://www.ds.am/index.php?option=com_user&view=both&from=grab&domain="+full_name+"' target='_blank'>"+t_grab+"</a>");
								$(dom_el).children('.black1').children('.amd').html('('+grab_amd[key]+'AMD)');
								$(dom_el).children('.black1').children('.usd').html('($'+grab_usd[key]+')');
							}
							else if(data=='4'+full_name)
							{
								$(dom_el).children('.res_av').css('color','#000000');
								$(dom_el).children('.res_dom').css('color','#000000');
								$(dom_el).children('.res_av').html(t_unable_to_check);
								$(dom_el).children('.res_blue').html("&nbsp;");
								$(dom_el).children('.black1').children('.amd').html("&nbsp;");
								$(dom_el).children('.black1').children('.usd').html("&nbsp;");
							}
						}
					});
			}
			else
			{
				$(dom_el).children('.black1').children('.amd').html('&nbsp;');
				$(dom_el).children('.black1').children('.usd').html('&nbsp;');
				$(dom_el).children('.res_blue').html('&nbsp;');
				$(dom_el).children('.res_av').html('&nbsp;');
				$(dom_el).children('.res_dom').css('color','#000');
				$(dom_el).hide();
			}
		});
	}
}


function go_ob(dom_ob)
{
		$('.to_hide').hide();
		$('.to_show').show();
		
		var domain = $('#domain').attr('value');
		if(domain!="")
		{
			
			$.each($('.zone'),function(key,zone)
			{
				if($(zone).attr('value')==$(dom_ob).attr('value'))
				{
					var full_name = domain + $(zone).attr('value');
					var dom_el=document.getElementById($(zone).attr('value'));
					$(dom_el).children('.res_av').html("<center><img src='images/loading.gif'></center>");
					$(dom_el).show();

					$(dom_el).children('.res_av').css('color','#000');
					$(dom_el).children('.res_dom').css('color','#000');
					$(dom_el).children('.res_dom').html($(zone).attr('value'));
					
					if($(zone).attr('checked')==true)
					{
						$.post('checkdomain.php',{domain:full_name},function(data){
								
								$(dom_el).children('.res_dom').html( $(zone).attr('value') );
								
								if(data=="1"+full_name)
								{
									$(dom_el).children('.res_av').css('color','#093');
									$(dom_el).children('.res_dom').css('color','#093');
									$(dom_el).children('.res_av').html(t_available);
									$(dom_el).children('.res_blue').html("<a class='res_blue' href='http://www.ds.am/index.php?option=com_user&view=both&from=register&domains="+full_name+"' target='_blank'>"+t_register+"</a>");
									$(dom_el).children('.black1').children('.amd').html('('+prices_amd[key]+'AMD)');
									$(dom_el).children('.black1').children('.usd').html('($'+prices_usd[key]+')');
								}
								else if(data=="2"+full_name)
								{
									$(dom_el).children('.res_av').css('color','#ff0000');
									$(dom_el).children('.res_dom').css('color','#ff0000');
									$(dom_el).children('.res_dom').html( "<a href='http://"+full_name+"' style='color:#ff0000;text-decoration:underline;'  target='_blank'>"+$(zone).attr('value')+"</a>" );
									$(dom_el).children('.res_av').html(t_not_available);
									$(dom_el).children('.res_blue').html("<a class='res_blue' href='http://www.ds.am/index.php?option=com_domainshop&view=whois&domain="+full_name+"' target='_blank'>"+t_whois+"</a>");
									$(dom_el).children('.black1').children('.amd').html('('+prices_amd[key]+'AMD)');
									$(dom_el).children('.black1').children('.usd').html('($'+prices_usd[key]+')');
								}
								else if(data=="3"+full_name)
								{
									$(dom_el).children('.res_av').css('color','#90C');
									$(dom_el).children('.res_dom').css('color','#90C');
									$(dom_el).children('.res_av').html(t_on_hold);
									$(dom_el).children('.res_blue').html("<a class='res_blue' href='http://www.ds.am/index.php?option=com_user&view=both&from=grab&domain="+full_name+"' target='_blank'>"+t_grab+"</a>");
									$(dom_el).children('.black1').children('.amd').html('('+grab_amd[key]+'AMD)');
									$(dom_el).children('.black1').children('.usd').html('($'+grab_usd[key]+')');
								}
								else if(data=="4"+full_name)
								{
									$(dom_el).children('.res_av').css('color','#000000');
									$(dom_el).children('.res_dom').css('color','#000000');
									$(dom_el).children('.res_av').html(t_unable_to_check);
									$(dom_el).children('.res_blue').html("&nbsp;");
									$(dom_el).children('.black1').children('.amd').html("&nbsp;");
									$(dom_el).children('.black1').children('.usd').html("&nbsp;");
								}
							});
					}
					else
					{
						$(dom_el).children('.black1').children('.amd').html('&nbsp;');
						$(dom_el).children('.black1').children('.usd').html('&nbsp;');
						$(dom_el).children('.res_blue').html('&nbsp;');
						$(dom_el).children('.res_av').html('&nbsp;');
						$(dom_el).children('.res_dom').css('color','#000');
						$(dom_el).hide();
					}
				}
			});
		}
}


function timer()
{
	var d=new Date();
	if( in_process && last_timestamp && (d.getTime()-last_timestamp)>1000 )
	{
		in_process=0;
		go();
	}
	setTimeout("timer()",300);
}

timer();

