/*
=================================================================================
 Copyright (c) 2008 Optimal Development
=================================================================================
 Данный код защищен авторскими правами
=================================================================================
 Файл: site_announcements.js
-------------------------------------------------------------------------------------------------------------------------
 Назначение: скрипты для раздела "Объявления"
=================================================================================
*/
function vcAnnouncementsSumitSearchForm()
{
	var form=document.getElementById('search_form');
	var searchUrl=location.href.replace(/^(http:\/\/(([a-z0-9-]+)(\.)?)+\/).*$/i,'$1');
	
	searchUrl+='?part=announcements&lang='+interfaceLang;
	if(form.category.value>0)
	{
		searchUrl+='&category='+form.category.value;
	};
	if(form.city.value>0)
	{
		searchUrl+='&city='+form.city.value;
	};
	window.location.href=searchUrl;
}

function vcAnnouncementsCheckSelectedCity(city)
{
	var form=document.getElementById('addnew_form');
	var inputBlock=document.getElementById('block_new_city');
	
	if(city<0)
	{
		inputBlock.style.display='block';
	}
	else if(city==0)
	{
		inputBlock.style.display='none';
		return false;
	}
	else
	{
		inputBlock.style.display='none';
	};
	return true;
}

function vcAnnouncementsCheckAddform()
{
	var form=document.getElementById('addnew_form');
	var errorFlag=false;
	
	if(form.title.value.length==0)
	{
		errorFlag=true;
	}
	else if(form.text.value.length==0)
	{
		errorFlag=true;
	}
	else if(form.category.value==0)
	{
		errorFlag=true;
	}
	else if(form.city.value==0)
	{
		errorFlag=true;
	}
	else if(form.city.value==-1&&form.new_city.value.length==0)
	{
		errorFlag=true;
	}
	else if(form.lifetime.value==''||form.lifetime.value==0)
	{
		errorFlag=true;
	};
	if(errorFlag)
	{
		alert(vcCommonFormRequireNotEmptyFields);
	};
	return !errorFlag;
}

function vcAnnouncementsCheckEditform()
{
	var form=document.getElementById('edit_form');
	var errorFlag=false;
	
	if(form.title.value.length==0)
	{
		errorFlag=true;
	}
	else if(form.text.value.length==0)
	{
		errorFlag=true;
	}
	else if(form.category.value==0)
	{
		errorFlag=true;
	}
	else if(form.city.value==0)
	{
		errorFlag=true;
	}
	else if(form.city.value==-1&&form.new_city.value.length==0)
	{
		errorFlag=true;
	};
	if(errorFlag)
	{
		alert(vcCommonFormRequireNotEmptyFields);
	};
	return !errorFlag;
}

function vcAnnouncementsShowBlockViews(announcementId,page)
{
	var ajax=new vc_ajax();
	
	ajax.onShow('');
	
	ajax.setVar('target','announcements');
	ajax.setVar('action','showviewers');
	ajax.setVar('object_id',announcementId);
	ajax.setVar('page',page);
	
	ajax.requestFile="/ajax.php";
	ajax.method='GET';
	ajax.execute=true;
	ajax.element='announcement_'+announcementId+'_views';
	ajax.sendAJAX();
}

function vcAnnouncementsDeleteAnnouncement(announcementId,mode)
{
	if(confirm(vcCommonRemoveConfirm))
	{
		var ajax=new vc_ajax();
		
		ajax.setVar('target','announcements');
		ajax.setVar('action','removeannouncement');
		ajax.setVar('object_id',announcementId);
		
		if(mode==1)
		{
			ajax.onShow('');
			
			ajax.onCompletion=function() {
				var div=document.getElementById('my_announcement_'+announcementId);
				if(this.response==1)
				{
					div.style.display='none';
				};
			}
		}
		else
		{
			ajax.onCompletion=function() {
				if(this.response==1)
				{
					var newUrl=location.href.replace(/^(http:\/\/(([a-z0-9-]+)(\.)?)+\/[a-z]{2}\/announcements).*$/i,'$1')+".html";
					alert(vcAnnouncementsAnnouncementRemovedSuccess);
					location.href=newUrl;
				}
				else
				{
					alert(vcAnnouncementsAnnouncementRemovedFailed);
				};
			};
		};
		
		ajax.requestFile="/ajax.php";
		ajax.method='POST';
		ajax.sendAJAX();
	};
}

function vcAnnouncementsEditAnnouncement(announcementId,mode)
{
	var newUrl=location.href.replace(/^(http:\/\/(([a-z0-9-]+)(\.)?)+\/).*$/i,'$1')+"?part=announcements&lang="+interfaceLang+"&edit="+announcementId;
	
	if(mode==1)
	{
		open(newUrl,'_blank');
	}
	else
	{
		location.href=newUrl;
	};
}

