function up_json_decode(a)	{
	eval( 'var jsonObj = ' + a + ';');
	return jsonObj;
}
 
function sticky_price_calculater()	{
	var select = $('#sticky_time_1');
	var input = $('#sticky_time_2');
	var days = 0;
	if (0 == select.val())	{
		input.show();
		days = input.val();
	}
	else{
		input.hide();
		days = select.val();
	}
	$('#sticky_price_calculater').html(days * up_sticky_cost);
}

function up_price_calculater()	{
	var times = $('#autoup_times').val();
	$('#up_auto_price_calculater').html(times * up_autoup_cost);
}

function up_sticky(threadid, pending){
	var time = $("#sticky_time_1").val();
	if (time == 0)  time = $("#sticky_time_2").val();
	
	$('#up_sticky_progress').show();
	
	ajax_request = new vB_AJAX_Handler(true);
	ajax_request.onreadystatechange(function ()	{
			if (ajax_request.handler.readyState == 4 && ajax_request.handler.status == 200)	{	
				var response = up_json_decode(ajax_request.handler.responseText);
				if(response.error ==0)	{
					alert(response.message);
					$('#sticky_content').html(response.html);
				}
				else if(response.error == 1)	{
					alert(response.message);
				}
				else if(response.error == -1)	{
					if (confirm(response.message))	{
						up_sticky(threadid, 1);
					}
				}
				$('#up_sticky_progress').hide();
			}
		}			
	);
	ajax_request.send('ajax.php?do=setsticky&ran=' + Math.random(),'do=setsticky&t=' + threadid + '&time='+time +'&pending='+pending);
	
	return false;
}

function up_buyauto(threadid){
	var times = $('#autoup_times').val();	
	$('#up_autoup_progress').show();
	
	ajax_request = new vB_AJAX_Handler(true);
	ajax_request.onreadystatechange(function ()	{
			if (ajax_request.handler.readyState == 4 && ajax_request.handler.status == 200)	{	
				var response = up_json_decode(ajax_request.handler.responseText);
				if(response.error ==0)	{
					alert(response.message);
					$('#autoup_content').html(response.html);
				}
				else if(response.error == 1)	{
					alert(response.message);
				}
				$('#up_autoup_progress').hide();
			}
		}			
	);
	ajax_request.send('ajax.php?do=buyauto&ran=' + Math.random(),'do=buyauto&t=' + threadid + '&times='+times);
	
	return false;
}

function del_sticky(threadid){	
	ajax_request = new vB_AJAX_Handler(true);
	ajax_request.onreadystatechange(function ()	{
			if (ajax_request.handler.readyState == 4 && ajax_request.handler.status == 200)	{	
				var response = up_json_decode(ajax_request.handler.responseText);
				if(response.error ==0)	{
					$('#sticky_content').html(response.html);
				}
				
			}
		}
	);
	
	ajax_request.send('misc.php?do=delsticky&ran=' + Math.random(),'do=delsticky&ajax=1&t=' + threadid);
	
	return false;
}

function up_auto(){	
	
	ajax_request = new vB_AJAX_Handler(true);
	ajax_request.onreadystatechange(function ()	{
			if (ajax_request.handler.readyState == 4 && ajax_request.handler.status == 200)	{
				//alert(ajax_request.handler.responseText);
				var response = up_json_decode(ajax_request.handler.responseText);
				if(response.error != 1)	{
					if (response.error == -1)	{
						alert(response.msg);
					}
					$('#autoup_active').html(response.active ? '<font color="#FF0000" id="autoup_active">Đã kích hoạt</font>' : '<font color="#FF0000">Tạm dừng</font> hoặc <font color="#FF0000">Chưa kích hoạt</font>');
					$.fancybox.close();
				}
				else	{
					alert(response.msg);
				}
			}
		}			
	);
	ajax_request.send('ajax.php?do=autoup&ran=' + Math.random(),$("#schedule_form").serialize());
	
	return false;
}
