
function fn_show_log_up_item(item_id){
	jQuery.post(WEB_DIR+"ajax.php?act=personal&code=show_log_up_item", {
		item_id: item_id
		},
		function(msg){
			if(msg == 'no_log'){
				log_faile("Chưa có lịch sử up tin cho tin này!");
				return false;
			}
			else{
				jQuery("#show_edit_up_item").css({display:"inline"});
				jQuery("#show_edit_up_item").html(msg);
				jQuery("#list_data_up_item").css({display:"none"});
			}
		}
	);
}

function refresh_log_up_item(item_id){
	jQuery.post(WEB_DIR+"ajax.php?act=personal&code=show_log_up_item", {
		item_id: item_id
		},
		function(msg){
			if(msg == 'no_log'){
				jQuery("#show_edit_up_item").html('');
				jQuery("#list_data_up_item").css({display:"block"});

				log_faile("Chưa có lịch sử up tin cho tin này!");
				return false;
			}
			else{
				jQuery("#show_edit_up_item").html(msg);
			}
		}
	);
}

function back_list_up_item(){
	jQuery("#list_data_up_item").css({display:"block"});
	jQuery("#paging").css({display:"block"});
	jQuery("#show_edit_up_item").html('');
}

function AllowInputData(myfield, e, svalue){
	var key;
	var keychar;
	var strinput;

	switch(svalue){
		case "str":
		  strinput = "0123456789,";
		  break;
		case "int":
		  strinput = "0123456789";
		  break;
		case "float":
		  strinput = "0123456789.";
		  break;
	};

	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
		(key==9) || (key==13) || (key==27) )
	   return true;

	// numbers
	else if (((strinput).indexOf(keychar) > -1))
	   return true;
	else
	   return false;
}

function changePasWord(){
	var old_password = getValueId('old_password');
	var new_password = getValueId('new_password');
	var retype_new_password = getValueId('retype_new_password');
	jQuery.post(WEB_DIR+"ajax.php?act=personal&code=change_password", {
		old_password: old_password,
		new_password: new_password,
		retype_new_password: retype_new_password
		},
		function(json){
			if(json['msg'] == 'no_login' || json['msg'] == 'unsuccess'){
				document.getElementById('content_pm').innerHTML = json['note'];
			}
			else{
				document.getElementById('changepas_suc_note').style.display = '';
				document.getElementById('changepas_suc_note').innerHTML = '<center>'+json['note']+'</center>';
				jQuery(".error").css({display:"none"});
				getValueId("old_password","assign","");
				getValueId("new_password","assign","");
				getValueId("retype_new_password","assign","");
				setTimeout(function(){document.getElementById('changepas_suc_note').style.display = 'none';}, 5000);
			}
		}
		,"json"
	);
}

function del_items(){
	document.getElementById('action').value='del_items';
	document.manage_my_transaction.submit();
}
function hide_items(){
	document.getElementById('action').value='hide_items';
	document.manage_my_transaction.submit();
}

//############################
//Up schedule
function schedule_up(item_id,status)
{
	if(status == 0 && !confirm("Bạn có chắc chắc muốn xóa không?"))
	{
		return false;
	}
	
	jQuery.post(WEB_DIR+"ajax.php?act=personal&code=schedule_up", {
		item_id: item_id,
		status: status
	},
	function(msg){
		if(msg == 'success'){
			if(status){//Bat lich up tin
				jQuery("#schedule_off_"+item_id).show();
				jQuery("#schedule_on_"+item_id).hide();
				log_success ('Tin đã được đưa vào Lịch up tin.<br>Bạn cần vào <a href="'+WEB_DIR+'personal.html?cmd=manage_up_item">Quản lý Lịch Up tin</a> để kích hoạt lịch up tin và thay đổi số liệu!',6000);
			}
			else{ //Tat lich up tin
				jQuery("#schedule_on_"+item_id).show();
				jQuery("#schedule_off_"+item_id).hide();
				jQuery("#bound_data_row_"+item_id).hide();

				log_success ('Tin đã được Xoá khỏi Lịch up tin.',6000);
			}
		}
		else if(msg == 'limit'){//limit
			log_success ('Bạn không được lên lịch quá 5 tin!',6000);
			return false;
		}
		else if(msg == 'not_login'){
			login_error();
			return false;
		}
		else if(msg == 'not_success'){
			log_faile("Thực hiện không thành công!");
		}
	});	
}

function schedule_config(item_id, reload_page){
	 jQuery.ajax({
        type: "POST",
        url: WEB_DIR+"ajax.php?act=personal&code=get_schedule_config&reload_page=" + reload_page,
        dataType: 'json',
        data: {item_id: item_id},
        success: function(data)
		{			
			if(data.msg == 'not_login')
			{
				login_error();
				return false;
			}
			else if(data.msg == 'no_perm')
			{
				log_faile('Bạn không có quyền thực hiện chức năng này.');	
				return false;
			}
			else if(data.msg == 'not_exist')
			{
				log_faile('Tin chưa được lên lịch.');	
				return false;
			}
			else if(data.msg == 'item_not_exist')
			{
				log_faile('Tin không tồn tại hoặc đã bị xóa.');	
				return false;
			}
			
			if(data.msg == '' && data.html)
			{
				jQuery.blockUI({message: data.html ,
					overlayCSS: {
						opacity: 0.9,
						background:'#000000',
						algin:'center'
					},
					css: {
						position: 'absolute',
						border: 'none',
						width:'100%',
						top:'10px',
						left:0,
						backgroundColor: 'transparent'
				}});
				
				recoverTime(data.run_times);
			}
		}
	});
	
	return false;
}

up_form_check_event = false;

function addStatusForm(status)
{
	jQuery('#status').val(status);
	
	var form = K('up_calendar');
	var strTime = [];
	
	var inputs = form.getElementsByTagName("input");
	
	for (var i = 0 ; i < inputs.length;  ++ i) 
	{
		if (inputs[i].className == 'interval') {
			strTime.push(inputs[i].value);
		}
	}//strTime = strTime.sort(function(a, b) {return a - b});
	
	html_time = '<input type="hidden" value="'+(strTime.join("|"))+'" id="string_up_time" name="string_up_time" />';
	
	jQuery('#input_check_time').html(html_time);
	
		
	up_auto();
	
	
	// if(!up_form_check_event)
	// {
		// up_form_check_event = true;
		
		// jQuery('#schedule_form').submit(function(){
			// jQuery(this).ajaxSubmit({
				// beforeSubmit:showRequestSchedule,
				// success:showResponseSchedule,
				// url:'ajax.php?do=autoup',
				// dataType:  'json'
			// });
			// return false;
		// });
	// }
	return false;
}

function showRequestSchedule(formData, jqForm, options){
	return true;
}

function showResponseSchedule(data, statusText){
	if (statusText == 'success') {
		if(data.msg=='invalid'){
			alert('Không cập nhật được lịch up tin!');
		}
		if(data.msg=='not_exist'){
			log_faile('Lịch up không tồn tại!');
		}
		else if(data.msg=='choose_wday'){
			alert('Bạn phải chọn ngày up trong tuần!');
		}
		else if(data.msg=='run_hour_error'){
			alert(data.alert);
		}
		else if(data.msg=='run_hour'){
			alert('Bạn phải chọn ít nhất 1 lần up / ngày!');
		}
		else if(data.msg=='up_zero'){
			alert('Bạn chưa nhập tổng số lần up cho lịch!');
		}
		else if(data.msg=='up_over'){
			alert('Số lượt up trong tài khoản của bạn không đủ để lên lịch up tin!');
		}
		else if(data.msg=='success'){
			jQuery("#schedule_off_"+data.item_id).show();
			jQuery("#schedule_on_"+data.item_id).hide();
			
			up_form_check_event = false;
			
			log_success('Cập nhật lịch up cho thành công!');
			
			if(data.reload_page == '1')
			{
				window.location.reload();
			}
		}
	}
	else{
		alert('Không cập nhật được lịch up tin!');
	}
}

function close_up()
{
	up_form_check_event = false;
	
	jQuery.unblockUI();		
	return false;
}

var up_gen1 = 0;
var up_gen2 = 0;
var up_gen3 = 0;
var up_gen4 = 0;	
	
function up_gen()//Tạo lượt up ngẫu nhiên theo các khoảng
{
	up_gen1 = parseInt(jQuery("#up_gen1").val());
	up_gen2 = parseInt(jQuery("#up_gen2").val());
	up_gen3 = parseInt(jQuery("#up_gen3").val());
	up_gen4 = parseInt(jQuery("#up_gen4").val());			
	
	var up_total = up_gen1 + up_gen2 + up_gen3 + up_gen4;
	
	if(up_total <= 0){
		alert("Bạn chưa nhập số lượt up cho từng khoảng thời gian trong ngày!");
		return false;
	}
	else if(up_total > 60){
		alert("Tổng số lần up trong một ngày không được quá 60 lần!");
		return false;
	}
	
	if(up_gen1 > 24){
		alert("Số lần up trong khoảng thời gian từ 0h00 đến 5h59 không được lớn hơn 24!");
		return false;
	}
	if(up_gen2 > 24){
		alert("Số lần up trong khoảng thời gian từ 6h00 đến 11h59 không được lớn hơn 24!");
		return false;
	}
	if(up_gen3 > 24){
		alert("Số lần up trong khoảng thời gian từ 12h00 đến 17h59 không được lớn hơn 24!");
		return false;
	}
	if(up_gen4 > 24){
		alert("Số lần up trong khoảng thời gian từ 18h00 đến 23h59 không được lớn hơn 24!");
		return false;
	}
	
	var	str_up_time = '';
	
	for(var i=1; i<=4; i++)
	{
		if(window['up_gen'+i] > 0)
		{
			start_time  = 3600 * 6 * (i-1);
			
			up_scale 	= Math.ceil(3600*6/window['up_gen'+i]);
			amplitude	= Math.ceil(up_scale/2) - 450
			
			for(var j=1; j<= window['up_gen'+i]; j++)
			{
				str_up_time += (str_up_time !='' ? '|' : '') + (get_time_day(start_time, (Math.ceil(up_scale/2) + (j-1)*up_scale + Math.ceil( amplitude * Math.random() ) ) ));
			}
		}
	}
	
	jQuery("#up_calendar").html('<div id="tooltip"><div style="float: right;" class="icon-remove" onmousedown="removeTime(this, event)" title="Xóa"></div><div id="result"></div><div class="clear"></div></div><div class="clear"></div>');
	$("#up_gen1").val(0);
	$("#up_gen2").val(0);
	$("#up_gen3").val(0);
	$("#up_gen4").val(0);	
	recoverTime(str_up_time);
	return false;
}

function get_time_day(time_start, time_pos)
{
	var h = Math.floor((time_start + time_pos)/3600);
	var m = Math.floor((time_start + time_pos)/60)%60;
	return (h<10?('0'+h):h) + ':' + (m<10?('0'+m):m);
}
//End Up schedule
