/*function trim(str) {
    return str.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
}*/
function trim(str){
	if(!str || typeof str != 'string')
		return null;
	str = Loaidaucach(str);
	return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,'');
}

function Loaidaucach(ElementId){
	var strDes = ElementId;
	var strTag = strDes;

	var i=0;
	while (i<strDes.length){
		if(strTag.charAt(i)==' ' && strTag.charAt(i+1)==' ' && strTag.charAt(i+2)==' '){
			str1 = strTag.substring(0,i);
			str2 = strTag.substring(i+2,strTag.length);
			strTag = str1 + str2;
			//alert(strTag+'\n gia tri cua bien i:'+i);
			i = i;
		//	alert('gia tri xau Tag:'+strTag+'\ngia tri xau Des:'+strDes);
		}
		if(strTag.charAt(i)==' ' && strTag.charAt(i+1)==' '){
			str1 = strTag.substring(0,i);
			str2 = strTag.substring(i+1,strTag.length);
			strTag = str1 + str2;
			//alert(strTag+'\n gia tri cua bien i:'+i);
			i = i;
		//	alert('gia tri xau Tag:'+strTag+'\ngia tri xau Des:'+strDes);
		}
		else{i=i+1;}
		return strTag;
	}
}

function promt_error(msg){
	if(!msg){
		msg = 'Có lỗi xảy ra';
	}
	jQuery.prompt(
		'<span style="color:#DF0023">'+ msg +'</span>',
		{
			prefix:'cleanblue',
			opacity: 0.3,
			top: '35%',	
			overlayspeed: 'fast'								
		}
	); 

}

function AddBookmark(ad_id){
	jQuery.get('ajax_comments.php?act=bookmark&ad_id=',{ad_id:ad_id}, function(data){
		if(data == 'saved'){
			document.getElementById('Bookmark').style.display='none';
			alert('Tin đã được lưu!\nVui lòng vào menu Quản lý tin đăng -> Tin đã lưu.');

		}else if(data == 'exist'){ 
			alert('Bạn đã lưu tin này rồi!\nVui lòng vào menu Quản lý tin đăng -> Tin đã lưu.');
		}else{
			alert('Có lỗi xảy ra');
		}
	  }
	);
		
}

//tuannk add on 2008/02/19
function displayDiv(obj){
	if(obj && typeof(obj.style) !='undefined' && obj.style.display=='none')
		obj.style.display='';
}

function hideDiv(obj){
	if(obj && typeof(obj.style) !='undefined' &&  obj.style.display=='')
	obj.style.display='none';
}

function displayID(objId){
	if(document.getElementById(objId))
	displayDiv(document.getElementById(objId));
}

function hideID(objId){
	if(document.getElementById(objId))
	hideDiv(document.getElementById(objId));
}


//added by Nova
//29.07.08
function showhideDiv(objId){
	var obj = document.getElementById(objId);
	if(obj){
		if(obj.style.display=='')
		obj.style.display='none';
		else
		obj.style.display='';
	}
}

function EditAdTags(){
	tinyMCE.triggerSave();
	var adTags = document.getElementById('ad_tags_value').value;
	document.getElementById('btnEditTags').disabled = true;
	document.getElementById('loadingTagsDiv').style.display='';
	
	new Ajax.Request(base_url+'/ajax_request.php?request_type=update_tags&ad_id=' + ad_id,{
		parameters: {ad_tags: adTags} ,
		onSuccess: function(transport){
			if(trim(transport.responseText)){
				//do something when actions finished
				//document.getElementById('warnningAd').innerHTML ='';					
				document.getElementById('ad_tags_contents').innerHTML = trim(transport.responseText);
				document.getElementById('btnEditTags').disabled = false;
				document.getElementById('loadingTagsDiv').style.display = 'none';
				
				showhideDiv('ad_tags_list');showhideDiv('ad_tags_list_editable');
			}
			else{
				document.getElementById('ad_tags_contents').innerHTML = '';
				document.getElementById('btnEditTags').disabled = false;
				document.getElementById('loadingTagsDiv').style.display = 'none';
				showhideDiv('ad_tags_list');showhideDiv('ad_tags_list_editable');
			}
		}
	});
}

function countUpChar(str){
	str = trim(str);
	
	var strNotAllow = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÃẠÂẤẦẪẬĂẮẰẴẶÉÈẼẸÊẾỀỄỆÍÌĨỊÓÒÕỌÔỐỒỖỘƠỚỜỠỢÚÙŨỤƯỨỪỮỰÝỲỸỴ";
	var aLen = str.length;
	var intCount = 0;
	for(i=0;i<aLen;i++) 
	{
		strC = str.charAt(i);
		if (strNotAllow.indexOf(strC)!=-1)
			intCount++;
	}
	if(intCount>Math.floor(aLen/3)) {
		alert("Bạn nhập tiêu đề quá nhiều chữ HOA cho phép!");
		return false;
	}
	return true;
}

function EditAdTitle(){
	var adTitle=document.getElementById('ad_title_value');
	
	if(trim(adTitle.value)==''){
		document.getElementById('warnningAd').innerHTML ='<div class="red" style="padding-left: 30px; text-align: left;"><ul><li>Bạn chưa nhập vào tiêu đề tin!</li></ul></div>';
		adTitle.focus();
		return false;
	}
	
	if (!countUpChar(trim( adTitle.value ))) 
	{
		adTitle.focus();
		return false;
	}
		
	document.getElementById('btnEditTitle').disabled=true;
	document.getElementById('loadingTitleDiv').style.display='';
	
	new Ajax.Request(wgScriptPath + 'act=editAdTitle&ad_id=' + ad_id + '&catid=' + ad_catid,
	{
		parameters: $('editAdTitleForm').serialize(true),
		onSuccess: function(transport){
			if(trim(transport.responseText) == 'ok')
			{
				//do something when actions finished
				document.getElementById('warnningAd').innerHTML ='';	
				
				document.getElementById('ad_title').innerHTML = adTitle.value;
				document.getElementById('btnEditTitle').disabled=false;
				document.getElementById('loadingTitleDiv').style.display='none';
				
				hideID('ad_title_edit');
				displayID('ad_title');
			}
			else if(trim(transport.responseText) == 'validate')
			{
				document.getElementById('warnningAd').innerHTML ='<div class="red" style="padding-left: 30px; text-align: left;"><ul><li>Tin chứa nội dung nhạy cảm, tin sẽ được kiểm duyệt lại sau!</li></ul></div>';
				document.getElementById('ad_title').innerHTML = adTitle.value;
				document.getElementById('btnEditTitle').disabled=false;
				document.getElementById('loadingTitleDiv').style.display='none';
				
				hideID('ad_title_edit');
				displayID('ad_title');
			}
		}
	});
}

function cancelEditTitle()
{
	displayID('ad_title'); 
	hideID('ad_title_edit'); 
	document.getElementById('ad_title_value').value=document.getElementById('ad_title').innerHTML;
	document.getElementById('btnEditTitle').disabled=false;
	document.getElementById('loadingTitleDiv').style.display='none';
}

/*function EditAdContent()
{//	btnEditComtent,loadingContentDiv
	tinyMCE.triggerSave();
	if(trim(document.getElementById('ad_content_value').value)=='')
	{
		document.getElementById('warnningAd').innerHTML ='<div class="red" style="padding-left: 30px; text-align: left;"><ul><li>Bạn chưa nhập vào nội dung tin!</li></ul></div>';
		return false;
	}
	
	document.getElementById('btnEditContent').disabled=true;
	document.getElementById('loadingContentDiv').style.display='';
	
	new Ajax.Request(wgScriptPath + 'act=editAdContent&ad_id=' + ad_id + '&catid=' + ad_catid,
	{
		parameters: $('editAdContentForm').serialize(true),
		onSuccess: function(transport){
			if(trim(transport.responseText) == 'ok')
			{
				//do something when actions finished
				document.getElementById('warnningAd').innerHTML ='';
				
				document.getElementById('ad_content').innerHTML = document.getElementById('ad_content_value').value;
				document.getElementById('btnEditContent').disabled=false;
				document.getElementById('loadingContentDiv').style.display='none';
	
				hideID('ad_content_edit');
				displayID('ad_content');
			}
			else if(trim(transport.responseText) == 'validate')
			{
				document.getElementById('warnningAd').innerHTML ='<div class="red" style="padding-left: 30px; text-align: left;"><ul><li>Tin chứa nội dung nhạy cảm, tin sẽ được kiểm duyệt lại sau!</li></ul></div>';												
				
				document.getElementById('ad_content').innerHTML = document.getElementById('ad_content_value').value;
				document.getElementById('btnEditContent').disabled=false;
				document.getElementById('loadingContentDiv').style.display='none';
	
				hideID('ad_content_edit');
				displayID('ad_content');
			}
		}
	});
}

function cancelEditContent()
{
	displayID('ad_content'); 
	hideID('ad_content_edit'); 
	document.getElementById('ad_content_value').value=document.getElementById('ad_content').innerHTML;
	document.getElementById('btnEditContent').disabled=false;
	document.getElementById('loadingContentDiv').style.display='none';
}*/

//tuannk add on 2008/02/19

function post_complant(){		
	
	jQuery.blockUI({message: '<div style="width:400px;border:none; background-color:#fff; padding:0;" align="left"><div style=" height:27px; background-color:#427bd6" align="left"><div style="line-height:26px;color: #fff; padding-left:10px;float:left;width:350px">Lý do than phiền</div><div class="close_blockui" onmouseover="this.className=\'close_blockui_hover\'" onmouseout="this.className=\'close_blockui\'" style="float:right"><img src="'+base_url+'/style/images/spacer.gif" width="15" height="14" id="close_box" title="Close..." style="cursor:pointer" /></div></div><div style=" background:url(style/images/bg_log_faile.gif) repeat-x bottom;"><div style="font-weight:normal; margin-left:10px;" id="bound_data"><br /><label><input type="checkbox" name="val_0" id="val_0" value="Tin sai chuyên mục"/><span style="margin-left:3px;cursor:pointer">Tin sai chuyên mục</span></label><br /><label><input type="checkbox" name="val_1" id="val_1" value="Không có địa chỉ, số tel của người bán"/><span style="margin-left:3px;cursor:pointer">Không có địa chỉ, số tel của người bán</span></label><br /><label><input type="checkbox" name="val_2" id="val_2" value="Không có thông tin về sản phẩm bán"/><span style="margin-left:3px;cursor:pointer">Không có thông tin về sản phẩm bán</span></label><br /><label><input type="checkbox" name="val_3" id="val_3" value="Tiêu đề tin không dấu/ có ký tự lạ"/><span style="margin-left:3px;cursor:pointer">Tiêu đề tin không dấu/ có ký tự lạ</span></label><br /><label><input type="checkbox" name="val_4" id="val_4" value="Spam/ Làm mới tin/ Đăng tin quá quy định"/><span style="margin-left:3px;cursor:pointer">Spam/ Làm mới tin/ Đăng tin quá quy định</span></label><br /><label><input type="checkbox" name="val_5" id="val_5" value="Lập nhiều nick bán hàng"/><span style="margin-left:3px;cursor:pointer">Lập nhiều nick bán hàng</span></label><br />  <br /><b>Lý do khác:</b></div><textarea name="other_complaint" id="other_complaint" rows="2" class="textAreaBlockUI" style="margin-left:10px"></textarea><div style=" margin-top:-5px; padding-bottom:5px;" align="center"> <input value="Gửi lý do"  id="send_reason" type="button" class="button_blockui" /></div></div></div>',css: { border:'none', padding:0}});		
	setTimeout(function(){jQuery("#other_complaint").focus();}, 500);
	jQuery('#close_reason').click(function () { 
		jQuery.unblockUI();
	});
	
	jQuery('#close_box').click(function () {
		jQuery.unblockUI();
	});
	
	jQuery('#send_reason').click(function () {										   	
		
		
			var reason_send = '';
			
			if(getValueId('val_0','checked')){
				reason_send += getValueId('val_0');
				if(getValueId('val_1','checked') || getValueId('val_2','checked') || getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if(getValueId('val_1','checked')){
				reason_send += getValueId('val_1');
				if(getValueId('val_2','checked') || getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if(getValueId('val_2','checked')){
				reason_send += getValueId('val_2');
				if (getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_3','checked')){
				reason_send += getValueId('val_3');
				if (getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_4','checked')){
				reason_send += getValueId('val_4');
				if (getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_5','checked')){
				reason_send += getValueId('val_5');
				if (getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('other_complaint') != ""){
				reason_send += getValueId('other_complaint');
			}
			
			if(reason_send!=""){			
				jQuery.ajax({
				type: "POST",
				url: path + "ajax_request.php?request_type=item&act=complaint",
				data: "ad_id="+ad_id+"&message="+reason_send+"&ad_userid="+ad_userid+"&ad_catid="+ad_catid,
				success: function(msg) {		
						if(msg == 'not_login'){
							 alert("Bạn phải đăng nhập mới thực hiện được chức năng này.");
							 return false;
						}					
						else if(msg == 'success'){
							document.getElementById('show_add_complaint').style.display='none';
							jQuery.unblockUI();
							return ;
						}
						else{						
							alert("Không thực hiện được than phiền.");
							 return false;							
						}						
					}
				});
			}
			else{
				alert("Lý do than phiền không được để trống");
				return false;			
			}
	}); 
	jQuery('#overlay').click(function () { 
	  jQuery.unblockUI();
	});		
}

function show_pm(){
	jQuery("#bound_box_message").toggle();	
}

function valid_item(type){
	
	if(type=='invalid'){
		
		var reason_send = '';
		
		jQuery.blockUI({message: '<div style="width:400px;border:none; background-color:#fff; padding:0;" align="left"><div style=" height:26px; background-color:#00A8EB" align="left"><div style="line-height:26px;color: #fff; padding-left:10px;float:left;width:350px">Lý do kiểm duyệt</div><div class="close_blockui" onmouseover="this.className=\'close_blockui_hover\'" onmouseout="this.className=\'close_blockui\'" style="float:right"><img src="'+base_url+'/style/images/spacer.gif" width="15" height="14" id="close_box" title="Close..." style="cursor:pointer" /></div></div><div style=" background:url(style/images/bg_log_faile.gif) repeat-x bottom;"><div style="font-weight:normal; margin-left:10px;" id="bound_data"><br /><label><input type="checkbox" name="val_0" id="val_0" value="Tin sai chuyên mục"/><span style="margin-left:3px;cursor:pointer">Tin sai chuyên mục</span></label><br /><label><input type="checkbox" name="val_1" id="val_1" value="Không có địa chỉ, số tel của người bán"/><span style="margin-left:3px;cursor:pointer">Không có địa chỉ, số tel của người bán</span></label><br /><label><input type="checkbox" name="val_2" id="val_2" value="Không có thông tin về sản phẩm bán"/><span style="margin-left:3px;cursor:pointer">Không có thông tin về sản phẩm bán</span></label><br /><label><input type="checkbox" name="val_3" id="val_3" value="Tiêu đề tin không dấu/ có ký tự lạ"/><span style="margin-left:3px;cursor:pointer">Tiêu đề tin không dấu/ có ký tự lạ</span></label><br /><label><input type="checkbox" name="val_4" id="val_4" value="Spam/ Làm mới tin/ Đăng tin quá quy định"/><span style="margin-left:3px;cursor:pointer">Spam/ Làm mới tin/ Đăng tin quá quy định</span></label><br /><label><input type="checkbox" name="val_5" id="val_5" value="Lập nhiều nick bán hàng"/><span style="margin-left:3px;cursor:pointer">Lập nhiều nick bán hàng</span></label><br />  <br /><b>Lý do khác:</b></div><textarea name="other_complaint" id="other_complaint" rows="2" class="textAreaBlockUI" style="margin-left:10px"></textarea><div style=" margin-top:-5px; padding-bottom:5px;" align="center"><input value="Gửi lý do"  id="send_reason" type="button" class="button_blockui" /></div></div></div>',css: { border:'none', padding:0}});
		
	setTimeout(function(){jQuery("#other_complaint").focus();}, 500);		
	
	jQuery('#close_reason').click(function () { 
		jQuery.unblockUI();
	});
	
	jQuery('#close_box').click(function () {
		jQuery.unblockUI();
	});
			
	jQuery('#send_reason').click(function () {										   								
			
			if(getValueId('val_0','checked')){
				reason_send += getValueId('val_0');
				if(getValueId('val_1','checked') || getValueId('val_2','checked') || getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if(getValueId('val_1','checked')){
				reason_send += getValueId('val_1');
				if(getValueId('val_2','checked') || getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if(getValueId('val_2','checked')){
				reason_send += getValueId('val_2');
				if (getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_3','checked')){
				reason_send += getValueId('val_3');
				if (getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_4','checked')){
				reason_send += getValueId('val_4');
				if (getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_5','checked')){
				reason_send += getValueId('val_5');
				if (getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('other_complaint') != ""){
				reason_send += getValueId('other_complaint');
			}
									
			if(reason_send!=""){			
								
				jQuery.ajax({
				type: "POST",
				url: path + "ajax_request.php?request_type=item&act=valid_item",		
				data: "ad_id="+ad_id+"&type="+type+"&message="+reason_send,
				success: function(msg) {		
						if(msg == 'not_login'){
							 alert("Bạn phải đăng nhập mới thực hiện được chức năng này.");
							 return false;
						}
						else if(msg=='not_perm'){
							alert("Bạn không có quyền thực hiện chức năng này");
							 return false;
						}
						else if(msg == 'success'){
							if(type=='valid'){
								document.getElementById('valid_item').innerHTML = '<a id="valid_item" href="javascript:void(0)" class="DelAd" onclick="valid_item(\'invalid\')">Kiểm duyệt</a>';
								document.getElementById('warnningAd').innerHTML='';
								jQuery.unblockUI();
							}
							else{
								document.getElementById('valid_item').innerHTML = '<a id="valid_item" href="javascript:void(0)" class="DelAd" onclick="valid_item(\'valid\')">Duyệt tin</a>';						
								document.getElementById('warnningAd').innerHTML='<div class="red" style="padding-left: 30px; text-align: left;"><ul><li>Tin đang chờ duyệt</li></ul></div>';
								jQuery.unblockUI();
							}
							return ;
						}
						else{						
							alert("Không thực hiện được kiểm duyệt.");
							 return false;							
						}						
					}
				});									
		
	}
	else{
		alert("Lý do than phiền không được để trống");
		return false;			
	}
			
			
	}); 			
	
	jQuery('#overlay').click(function () { 
	  jQuery.unblockUI();
	});	
	
	
	}	//end neu la kiem duyet
	else if(type=='valid'){
		jQuery.ajax({
		type: "POST",
		url: path + "ajax_request.php?request_type=item&act=valid_item",		
		data: "ad_id="+ad_id+"&type="+type,
		success: function(msg) {		
				if(msg == 'not_login'){
					 alert("Bạn phải đăng nhập mới thực hiện được chức năng này.");
					 return false;
				}
				else if(msg=='not_perm'){
					alert("Bạn không có quyền thực hiện chức năng này");
					 return false;
				}
				else if(msg == 'success'){
					if(type=='valid'){
						document.getElementById('valid_item').innerHTML = '<a id="valid_item" href="javascript:void(0)" class="DelAd" onclick="valid_item(\'invalid\')">Kiểm duyệt</a>';
						document.getElementById('warnningAd').innerHTML='';						
					}
					else{
						document.getElementById('valid_item').innerHTML = '<a id="valid_item" href="javascript:void(0)" class="DelAd" onclick="valid_item(\'valid\')">Duyệt tin</a>';						
						document.getElementById('warnningAd').innerHTML='<div class="red" style="padding-left: 30px; text-align: left;"><ul><li>Tin đang chờ duyệt</li></ul></div>';						
					}
					return ;
				}
				else{						
					alert("Không thực hiện được kiểm duyệt.");
					 return false;							
				}						
			}
		});	
	}
}

function up_ad(ad_id,cut_down){
	if((cut_down==1 && up_remain <=0) || (cut_down==0 && (up_day + up_remain) <=0)){
		if(cut_down)
			promt_error("Tài khoản đã hết lượt up!");
		else
			promt_error("Giao dịch đã hết lượt up!");
			
		return false;
	}
	jQuery.post('usercp.php?act=ajax&code=up_item',{ad_id:ad_id,cut_down:cut_down}, function(data){
		if(data == 'renewed'){
			if(cut_down==0 && up_day){
				up_day = 0;
				promt_error("Bạn đã hết lượt up trên ngày! Bạn còn "+up_remain+" lượt up trong tài khoản!");
			}
			else{
				up_remain--;
				promt_error('Tin đã được làm mới, bạn còn  '+up_remain+' lượt up trong tài khoản!'); 
			}
			
			if(up_remain){
				jQuery('#up_msg').html('- Còn '+up_remain+' lượt up trong tài khoản!');
			}
			else{
				jQuery('#up_msg').html('- Bạn đã hết lượt up!');
				promt_error('Soạn tin <b>RBU '+ad_userid+'</b> gửi <b>8701</b><br />để nhận thêm '+acc_up_number+' lượt up!<br />('+ad_userid+' là ID của bạn)'); 
			}
		}
		else if(data == 'un_success'){
			promt_error('Up tin không thành công!'); 
		}
		else if(data =='not_login'){
			promt_error('Bạn phải đăng nhập mới sử dụng được chức năng này!'); 
		}
		else if(data == 'up_timeout'){ 
			promt_error('Bạn không được up tin quá 1 phút/lần!'); 
		}
		else if(data == 'up_timeout_free'){
			if(confirm("Mỗi lượt up tin miễn phí phải cách nhau 3 phút!\n\nTiếp tục up tin (Sử dụng lượt up trong tài khoản của  bạn)?")){
				up_ad(ad_id, 1);//Up tin và trừ vào lượt up trong tài khoản
			} 
		}
		else if(data == 'error'){ 
			promt_error("Up không thành công!");
		}
		else{
			promt_error("Up không thành công!");
		}
	});
	
	return false;
}

function confirm_del(ad_id){
	jQuery.blockUI({message: '<div style="width:400px;border:none; background-color:#fff; padding:0;" align="left"><div style=" height:26px; background-color:#00A8EB" align="left"><div style="line-height:26px;color: #fff; padding-left:10px;float:left;width:350px">Lý do xoá</div><div class="close_blockui" onmouseover="this.className=\'close_blockui_hover\'" onmouseout="this.className=\'close_blockui\'" style="float:right"><img src="'+base_url+'/style/images/spacer.gif" width="15" height="14" id="close_box" title="Close..." style="cursor:pointer" /></div></div><div style=" background:url(style/images/bg_log_faile.gif) repeat-x bottom;"><div style="font-weight:normal; margin-left:10px;" id="bound_data"><br /><label><input type="checkbox" name="val_0" id="val_0" value="Tin sai chuyên mục"/><span style="margin-left:3px;cursor:pointer">Tin sai chuyên mục</span></label><br /><label><input type="checkbox" name="val_1" id="val_1" value="Không có địa chỉ, số tel của người bán"/><span style="margin-left:3px;cursor:pointer">Không có địa chỉ, số tel của người bán</span></label><br /><label><input type="checkbox" name="val_2" id="val_2" value="Không có thông tin về sản phẩm bán"/><span style="margin-left:3px;cursor:pointer">Không có thông tin về sản phẩm bán</span></label><br /><label><input type="checkbox" name="val_3" id="val_3" value="Tiêu đề tin không dấu/ có ký tự lạ"/><span style="margin-left:3px;cursor:pointer">Tiêu đề tin không dấu/ có ký tự lạ</span></label><br /><label><input type="checkbox" name="val_4" id="val_4" value="Spam/ Làm mới tin/ Đăng tin quá quy định"/><span style="margin-left:3px;cursor:pointer">Spam/ Làm mới tin/ Đăng tin quá quy định</span></label><br /><label><input type="checkbox" name="val_5" id="val_5" value="Lập nhiều nick bán hàng"/><span style="margin-left:3px;cursor:pointer">Lập nhiều nick bán hàng</span></label><br />  <br /><b>Lý do khác:</b></div><textarea name="other_complaint" id="other_complaint" rows="2" class="textAreaBlockUI" style="margin-left:10px"></textarea><div style=" margin-top:-5px; padding-bottom:5px;" align="center"><input value="Gửi lý do"  id="send_reason" type="button" class="button_blockui" /></div></div></div>',css: { border:'none', padding:0}});		
	setTimeout(function(){jQuery("#other_complaint").focus();}, 500);
	jQuery('#close_reason').click(function () { 
		jQuery.unblockUI();
	});
	
	jQuery('#close_box').click(function () {
		jQuery.unblockUI();
	});
	
	jQuery('#send_reason').click(function () {										   	
				
			var reason_send = '';
			
			if(getValueId('val_0','checked')){
				reason_send += getValueId('val_0');
				if(getValueId('val_1','checked') || getValueId('val_2','checked') || getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if(getValueId('val_1','checked')){
				reason_send += getValueId('val_1');
				if(getValueId('val_2','checked') || getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if(getValueId('val_2','checked')){
				reason_send += getValueId('val_2');
				if (getValueId('val_3','checked') || getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_3','checked')){
				reason_send += getValueId('val_3');
				if (getValueId('val_4','checked') || getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_4','checked')){
				reason_send += getValueId('val_4');
				if (getValueId('val_5','checked') || getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('val_5','checked')){
				reason_send += getValueId('val_5');
				if (getValueId('content_rs_mini') != ""){
					reason_send +='; ';
				}
			}
			
			if (getValueId('other_complaint') != ""){
				reason_send += getValueId('other_complaint');
			}
			
			if(reason_send!=""){			
				jQuery.ajax({
				type: "POST",
				url: path + "ajax_request.php?request_type=admin&act=del_item",
				data: "ad_id="+ad_id+"&reason_del="+reason_send,
				success: function(msg) {		
						if(msg == 'not_perm'){
							 alert("Bạn không có quyền thực hiện chức năng này.");
							 return false;
						}					
						else if(msg == 'unsuccess'){
							alert("Không thực hiện được than phiền.");
							 return false;							
						}
						else{						
							//location.reload();
							window.location = msg;
							return ;
						}						
					}
				});
			}
			else{
				alert("Lý do xóa không được để trống");
				return false;			
			}
	}); 
	jQuery('#overlay').click(function () { 
	  jQuery.unblockUI();
	});			
}

var page_log = 1;
var up_log_show = 0;

function show_log_up_ad(item_id){	
	if(up_log_show==0){
		jQuery.post("ajax_request.php?request_type=item&act=show_log_up_ad", {
			item_id: item_id
			},			
			function(msg){
				if(msg == 'no_log'){
					alert("Chưa có lịch sử up tin cho tin này!");
					return false;
				}
				else{
					page_log 	= 1;
					up_log_show = 1;
					jQuery("#show_log").html(msg).css({display:"inline"});
				}
			}
		);
	}
	else if(up_log_show==1){
		up_log_show = 2;
		jQuery("#show_log").hide();
	}
	else{
		up_log_show = 1;
		jQuery("#show_log").show();
	}
}

function show_log_up_more(item_id){
	page_log++;
	
	jQuery.post("ajax_request.php?request_type=item&act=show_log_up_ad", {
		item_id: item_id,page: page_log
		},			
		function(msg){
			if(msg == 'no_log'){
				alert("Chưa có thêm lịch sử up tin cho tin này!");
				page_log--;
				jQuery("#btn_log_more").hide();
				return false;
			}
			else{
				jQuery("#show_log_more").append(msg).css({display:"inline"});
			}
		}
	);
}

var load_jcache_edit_des = false;
function fn_click_edit_desc_click(ad_id){	
	jQuery.jCache.maxSize = 10;	
	document.getElementById('loadingEditContentDiv').style.display = '';
	var cache_key = "ajax_request.php?request_type=item&act=loadAdContent&ad_id="+ad_id;
	if (jQuery.jCache.hasItem(cache_key) && !load_jcache_edit_des){		
		tinyMCE.activeEditor.setContent(jQuery.jCache.getItem(cache_key));
	}
	else{				
		jQuery.get(cache_key, function(data){								   
			tinyMCE.activeEditor.setContent(data);
			load_jcache_edit_des = false;	
			jQuery.jCache.setItem(cache_key, data);		
		});		
	}	
	
	setTimeout(function(){
		document.getElementById('loadingEditContentDiv').style.display = 'none';
		document.getElementById('lbl_content_edit').style.display = 'none';
	}, 500);	
	document.getElementById('NewsContent').style.display = 'none';
	document.getElementById('NewsContent_edit').style.display = '';		
}

function fn_click_reset_desc(){
	document.getElementById('lbl_content_edit').style.display = '';
	document.getElementById('NewsContent').style.display = '';
	document.getElementById('NewsContent_edit').style.display = 'none';
	document.getElementById('loadingContentDiv').style.display = 'none';
	document.getElementById('loadingContentDiv').style.display = 'none';
	return;
}

function fn_click_submit_desc(ad_id){
	tinyMCE.triggerSave();
	var desc = tinyMCE.get('ad_content_value').getContent();
	
	if(desc == ''){	
		fn_click_reset_desc();
	}
	document.getElementById('loadingContentDiv').style.display = '';	
	jQuery.ajax({
		type: "POST",
		url: "ajax_request.php?request_type=item&act=editAdContent&ad_id="+ad_id,
		data: {ad_content_value: desc},
		success: function(msg){
			fn_click_reset_desc();
			if(msg=="empty"){
				document.getElementById('warnningAd').innerHTML = '<div class="red" style="padding-left: 30px; text-align: left;">  <ul> <li>Bạn cần nhập Nội dung tin rao!</li></ul></div>';
			}
			else if(msg == 'validate'){
				document.getElementById('warnningAd').innerHTML = '<div class="red" style="padding-left: 30px; text-align: left;">  <ul> <li>Tin đang chờ duyệt!</li></ul></div>';
				document.getElementById('NewsContent').innerHTML = desc;
			}
			else{ // is Okie
				document.getElementById('warnningAd').innerHTML = '';
				document.getElementById('NewsContent').innerHTML = desc;
			}
		}
	});
	fn_click_reset_desc();
	load_jcache_edit_des = true;
}



var page_log_sms = 1;
var log_sms = 0;

function show_log_sms(item_id){	
	if(log_sms==0){
		jQuery.post("ajax_request.php?request_type=item&act=show_log_sms", {
			item_id: item_id
			},			
			function(msg){
				if(msg == 'no_log'){
					alert("Chưa có SMS cho tin này!");
					return false;
				}
				else{
					page_log_sms 	= 1;
					log_sms = 1;
					jQuery("#show_log_sms").html(msg).css({display:"inline"});
				}
			}
		);
	}	
	else if(log_sms==1){
		log_sms = 2;
		jQuery("#show_log_sms").hide();
	}
	else{
		log_sms = 1;
		jQuery("#show_log_sms").show();
	}
			 
}

function show_log_sms_more(item_id){
	page_log_sms++;
	
	jQuery.post("ajax_request.php?request_type=item&act=show_log_sms", {
		item_id: item_id,page: page_log_sms
		},			
		function(msg){
			if(msg == 'no_log'){
				alert("Chưa có thêm SMS cho tin này!");
				page_log_sms--;
				jQuery("#btn_log_more").hide();
				return false;
			}
			else{
				jQuery("#show_log_more").append(msg).css({display:"inline"});
			}
		}
	);
}