//==============================
// Автор: REZER
// http://rezer.net/ 
// Специально для DLE Board
//==============================

//---------------------------------------------
//	Комментарии
//---------------------------------------------

var BoardAnserCommentsID = new Array();
var TreeComWysiwyg = false;

// Цитирорование выделенного текста
function BoardQuoteSelectedText( name ) {
	
	if ( !document.getElementById( "addcomments_form" ) ) return false;
	var input = document.getElementById( "comments" );
	var finalhtml = "";
	
	if( dle_txt != "" )
		{
			input.value += dle_txt;
		}
			else
		{ 
			input.value += "[b]" + name + "[/b]," + "\n";
		}	
}

// Создание формы ответа на комментарий
function BoardAnswerComment( id ){

	if( !BoardAnserCommentsID[ id ] )
		{
			BoardCancelAnswer();			
			document.getElementById( "tree_comments_form-" + id ).innerHTML = form_test;
			BoardAnserCommentsID[ id ] = 1;
			document.getElementById( "addcomments_form" ).innerHTML = "";
			document.getElementById( "cancel_answer" ).style['display'] = "";
			document.getElementById( "cancel_answer" ).innerHTML = "<input type=\"button\" onclick=\"BoardAnswerComment( '" + id + "' ); return false;\" class=\"bbcodes\" value=\"Отменить\" /><input type=\"hidden\" name=\"answer_id\" value=\"" + id + "\">";
		}
			else
		{
			BoardAllCancelAnswer();
		}
}

// Отмена ответа на комментарий
function BoardAllCancelAnswer(){
	
	BoardCancelAnswer();
	document.getElementById( "addcomments_form" ).innerHTML = form_test;
	document.getElementById( "cancel_answer" ).style['display'] = "none";	
}

// Убирание формы ответа на комментарий
function BoardCancelAnswer(){

	var com_search = document.getElementById( "dle-content" );
	var com_answer = com_search.getElementsByTagName( "div" );
	var count_com = com_answer.length;
	for( var i = 0; i < count_com; i++ )
		{
			if( com_answer[ i ] != null )
				{
					if( /tree_comments_form-[0-9]/i.exec( com_answer[ i ].id ) != null )
						{
							com_answer[ i ].innerHTML = "";
							var answer_id = com_answer[ i ].id.match( /[0-9]{1,50}/ );
							BoardAnserCommentsID[ answer_id[ 0 ] ] = 0;
							delete( answer_id );
						}
				}
		}
}

// Навигация по комментариям
function BoardComPage( page, board_id, link_tpl ){
	
	BoardAllCancelAnswer();
	if( document.getElementById( "status-nav-com" ) != null )
		{
			document.getElementById( "status-nav-com" ).innerHTML = "<img src=\"/templates/" + dle_skin + "/board/style/horizont-ajax.gif\" border=\"0\" alt=\"Загрузка\" align=\"absmiddle\" />";
		}
	
	$.ajax({
		url: "/engine/ajax/board/com-navigation.php",
		data: "com_page=" + page + "&board_id=" + board_id + "&link_tpl=" + link_tpl,
		success: function( data ){
			$( "#board_comments" ).html( data );
			window.location = "#comment";
		},
		dataType: "html",
		type: "POST"
	});
	
}

// Добавлеение комментария
function BoardAddComments(){

	var Url = "";
	var form = document.forms['AddBoardComment'];
    ShowLoading( "" );
	
	for( var i=0; i < form.elements.length; i++ )
		{
			var ElementForm = form.elements[i];
			switch( ElementForm.type ){
				
				case "checkbox": {
					if( ElementForm.checked == true )
						{
							Url += "&" + ElementForm.name + "=" + ElementForm.value;
						}
					break
				}
				
				default: {
					Url += "&" + ElementForm.name + "=" + ElementForm.value;
					break
				}
			}
		}

	$.ajax({
		url: dle_root + "engine/ajax/board/addcomments.php",
		data: Url,
		success: function( data ){
			
			HideLoading( "" );
			if( /script language/i.exec( data ) == null )
				{
					if( form.answer_id )
						{
							$( "#tree_comments_form-" + form.answer_id.value ).html( data );
							document.getElementById( "button_answer_to_" + form.answer_id.value ).innerHTML = "";
						}
							else
						{
							var new_id = Math.floor( Math.random() * ( 100 - 5 + 1 ) ) + 5;
							var Element = document.createElement( "div" );
							document.getElementById( "BoardCommentsList" ).appendChild( Element );
							Element.id = "new_comment_" + new_id;
							$( "#" + Element.id ).html( data );					
						}
						
					if( document.getElementById( "BoardNotComment" ) != null )
						{
							var DelElement = document.getElementById( "BoardNotComment" );
							DelElement.parentNode.removeChild( DelElement );
						}
						
					if( form.answer_id )
						{
							var ObjCom = document.getElementById( "tree_comments_form-" + form.answer_id.value );
							ObjCom.id = "tree_comments_result-" + form.answer_id.value;
							BoardAllCancelAnswer();
						}
						
					if( dle_group == 5 )
						{
							form.name.value = "";
							form.com_mail.value = "";
						}
					
					form.comments.value = "";
					
				}
					else
				{
					var new_id = Math.floor( Math.random() * ( 100 - 5 + 1 ) ) + 5;
					var Element = document.createElement( "div" );
					document.getElementById( "BoardCommentsList" ).appendChild( Element );
					Element.id = "new_comment_" + new_id;
					$( "#" + Element.id ).html( data );	
				}
			
			if( document.getElementById( "dle-captcha") != null )
				{
					BoardReCaptcha();
					form.sec_code.value = "";
				}
		},
		dataType: "html",
		type: "POST"
	});
}

// Удаление комментариев
function BoardDelCom( id, board_id ){
	
	var Quest = confirm( "Вы действительно желаете удалить данный комментарий?" );
	if( Quest )
		{
			document.getElementById( "comm-id-" + id ).innerHTML = "Происходит удаление, дождитесь ответа...";	
			
			$.ajax({
				url: dle_root + "engine/ajax/board/com-del.php",
				data: "id=" + id + "&board_id=" + board_id,
				success: function( data ){
					$( "#comm-id-" + id ).html( data );
				},
				dataType: "html",
				type: "POST"
			});	
		}	
}

// Редактирование комментариев
var BoardCasheComEdit = new Array();
function BoardEditCom( id ){
	
	if( !BoardCasheComEdit[ id ] ) BoardCasheComEdit[ id ] = document.getElementById( "comm-id-" + id ).innerHTML;
	document.getElementById( "comm-id-" + id ).innerHTML = "Загрузка, дождитесь ответа...";	
	
	$.ajax({
		url: dle_root + "engine/ajax/board/com-edit.php",
		data: "id=" + id,
		success: function( data ){
			$( "#comm-id-" + id ).html( data );
			fombj = document.forms['AddBoardComment'];
		},
		dataType: "html",
		type: "POST"
	});
}

// Сохранение редактирования комментария
function BoardSaveEditCom( id, photo_id ){
	
	var Comment = document.getElementById( "dleeditcomments" + id ).value;
		
	document.getElementById( "comm-id-" + id ).innerHTML = "Сохранение, дождитесь ответа...";
	
	$.ajax({
		url: dle_root + "engine/ajax/board/com-edit.php",
		data: "id=" + id + "&comments=" + Comment + "&save=1",
		success: function( data ){
			$( "#comm-id-" + id ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

// Отмена редактирования комментария
function CanceBoardEditCom( id ){
	document.getElementById( "comm-id-" + id ).innerHTML = BoardCasheComEdit[ id ];
	BoardCasheComEdit[ id ] = "";
}

//---------------------------------------------
//	Вывод доп. полей при выборе категории
//---------------------------------------------

function CategorySelect(){
	BoardTypeSetCategory();
	BoardFieldsSetCategory();
}

function BoardTypeSetCategory(){
		
	var Select = "";
	var SelectCategory = document.getElementById( "BoardSelectCategory" ).value;
	var SelectType = document.getElementById( "BoardTypeSelect" ).value;
	for( var i = 0; i < BoardTypeCat[ SelectCategory ].length; i++ )
		{
			var id = BoardTypeCat[ SelectCategory ][ i ];
			var name = BoardTypeName[ id ];
			var selected = SelectType == id ? " selected=\"selected\"" : "";
			Select += "<option value=\"" + id + "\"" + selected + ">" + name + "</option>";
		}
		
	Select = "<select name=\"board_type\" id=\"BoardTypeSelect\">" + Select + "</select>";
	document.getElementById( "boardType" ).innerHTML = Select;
}

function BoardFieldsSetCategory(){
	
	var SelectCategory = document.getElementById( "BoardSelectCategory" ).value;
	var field_search = document.getElementById( "BoardFields" );
	var field_id = field_search.getElementsByTagName( "tr" );
	var count_field = field_id.length;
	for( var i = 0; i < count_field; i++ )
		{
			if( field_id[ i ] != null )
				{
					if( /BoardField_[0-9]/i.exec( field_id[ i ].id ) != null )
						{
							field_id[ i ].style['display'] = "none";
							var this_field_id = field_id[ i ].id.match( /[0-9]{1,50}/ );
							
							if( BoardFields[ SelectCategory ] != null )
								{
									for( var c = 0; c < BoardFields[ SelectCategory ].length; c++ )
										{
											if( this_field_id == BoardFields[ SelectCategory ][ c ] )
												{
													field_id[ i ].style['display'] = "";
												}
										}
								}
							
							if( field_id[ i ].style['display'] != "" && BoardFields['all'] != null )
								{
									for( var c = 0; c < BoardFields['all'].length; c++ )
										{
											if( this_field_id == BoardFields['all'][ c ] )
												{
													field_id[ i ].style['display'] = "";
												}
										}
								}
						}
				}
		}
}

//---------------------------------------------
//	Другие функции
//---------------------------------------------

// Выбор страны
function SelectCountry( Prefix, CityName, CityID ){
		
	CityID = !CityID ? "" : CityID;
	Prefix = !Prefix ? "" : Prefix;
	CityName = !CityName ? "" : CityName;
	var CountryID = document.getElementById( "SelectListCountry" + Prefix ).value;
	document.getElementById( "SelectCity" + Prefix ).disabled = true;
	document.getElementById( "FormSelectCityStatus" + Prefix ).innerHTML = "<img src=\"/engine/inc/board/style/images/arrow-ajax.gif\" alt=\"Загрузка...\" border=\"0\" align=\"absmiddle\" />";
	
	$.ajax({
		url: "/engine/ajax/board/set.country.php",
		data: "country_id=" + CountryID + "&prefix=" + Prefix + "&CityID=" + CityID + "&CityName=" + CityName,
		success: function( data ){
			$( "#FormSelectCity" + Prefix ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

// Обновление капчи
function BoardReCaptcha( id ){
	if( !id ) var id = "dle-captcha";
	var rndval = new Date().getTime(); 
	if( document.getElementById( id ) != null ) document.getElementById( id ).innerHTML = "<img src=\"/engine/modules/antibot.php?rndval=" + rndval + "\" border=\"0\" width=\"120\" height=\"50\" onclick=\"BoardReCaptcha( '" + id + "' ); return false;\" style=\"cursor: pointer;\" />";
}

// Добавление и удаление объявления из блокнота
function BoardNotepad( id, type ){
	
	document.getElementById( "BoardPostNotepad_" + id ).innerHTML = "<img src=\"/engine/inc/board/style/images/status.gif\" alt=\"Загрузка...\" border=\"0\" align=\"absmiddle\" />";
	
	$.ajax({
		url: "/engine/ajax/board/notepad.php",
		data: "id=" + id + "&type=" + type,
		success: function( data ){
			$( "#BoardPostNotepad_" + id ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

var OkSelectCity = false;
function SelectRegion( CityID ){
	
	if( OkSelectCity === false )
		{
			SelectCountry( "", "Выберите", CityID );
			OkSelectCity = true;
		}
		
	ShowOrHide( "SelectCountryForm" );
}

function BoardformSubmit( nameForm, file, IdResult, img, imgId ){		
		
	if( !IdResult ) IdResult = "";
	if( !img ) img = "";
	if( !imgId ) ShowLoading( "" );
	if( !file ) return false;
		
	var Url = "";
	var formSend = document.forms[ nameForm ];
		
	if( imgId ) document.getElementById( imgId ).innerHTML = "<img src=\"/engine/inc/board/style/images/" + img + "\" border=\"0\" alt=\"Загрузка\" />";
		
	for( var i = 0; i < formSend.elements.length; i++ )
		{
			var ElementForm = formSend.elements[i];
			switch( ElementForm.type )
				{
					case "checkbox": {
						if( ElementForm.checked == true )
							{
								Url += "&" + ElementForm.name + "=" + ElementForm.value;
							}
						break
					}
						
					case "select-multiple": {
						for( var imultiple = 0; imultiple < ElementForm.options.length; imultiple++ )
							{
								if( ElementForm.options[ imultiple ].selected )
									{
										Url += "&" + ElementForm.name + "=" + ElementForm.options[imultiple].value;
									}
							}
						break
					}
					
					case "radio": {
						var radioObj = formSend[ ElementForm.name ];
						for( var imultiple = 0; imultiple < radioObj.length; imultiple++ )
							{
								if ( radioObj[ imultiple ].checked == true )
									{
										Url += "&" + ElementForm.name + "=" + radioObj[ imultiple ].value;
										radio = true;
									}
							}
						break
					}
						
					case "hidden": {
						Url += "&" + ElementForm.name + "=" + ElementForm.value;
						break
					}
						
					case "text": {
						Url += "&" + ElementForm.name + "=" + ElementForm.value;
						break
					}
						
					default: {
						Url += "&" + ElementForm.name + "=" + ElementForm.value;
						break
					}
				}
		}
		
	$.ajax({
		url: "/engine/ajax/board/" + file,
		data: Url,
		success: function( data ){
			if( !imgId ) HideLoading( "" );
			if( IdResult ) $( "#" + IdResult ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

var BoardModal = [];
function BoardSendEmail( id ){
	
	if( BoardModal[ "SendEmail-" + id ] == 1 )
		{
			$( "#BoardSendEmail_" + id ).dialog( ( $( "#BoardSendEmail_" + id ).dialog( "isOpen" ) ? "close" : "open" ) );
			return true;
		}
			else
		{
			$( "#BoardSendEmail_" + id ).dialog({
				autoOpen: false,
				hide: "fade",
				title: "Отправить письмо",
				show: "slide",
				width: 450,
			});
			
			$( "#BoardSendEmail_" + id ).html( "<div align=\"center\"><br /><br /><img src=\"/engine/inc/board/style/images/preloader.gif\" alt=\"Загрузка\" /></div>" );
			
			$.ajax({
				url: "/engine/ajax/board/send.email.php",
				data: "id=" + id,
				success: function( data ){
					$( "#BoardSendEmail_" + id ).html( data );
				},
				dataType: "html",
				type: "POST"
			});
			
			BoardModal[ "SendEmail-" + id ] = 1;
			return BoardSendEmail( id );
		}
	
	return false;
}


function BoardSendEmailFriend( id ){
	
	if( BoardModal[ "SendEmailFriend-" + id ] == 1 )
		{
			$( "#BoardSendEmailFriend_" + id ).dialog( ( $( "#BoardSendEmailFriend_" + id ).dialog( "isOpen" ) ? "close" : "open" ) );
			return true;
		}
			else
		{
			$( "#BoardSendEmailFriend_" + id ).dialog({
				autoOpen: false,
				hide: "fade",
				title: "Отправить письмо другу",
				show: "slide",
				width: 450,
			});
			
			$( "#BoardSendEmailFriend_" + id ).html( "<div align=\"center\"><br /><br /><img src=\"/engine/inc/board/style/images/preloader.gif\" alt=\"Загрузка\" /></div>" );
			
			$.ajax({
				url: "/engine/ajax/board/send.email.friend.php",
				data: "id=" + id,
				success: function( data ){
					$( "#BoardSendEmailFriend_" + id ).html( data );
				},
				dataType: "html",
				type: "POST"
			});
			
			BoardModal[ "SendEmailFriend-" + id ] = 1;
			return BoardSendEmailFriend( id );
		}
	
	return false;
}

function BoardVip( id ){
	
	if( BoardModal[ "Vip-" + id ] == 1 )
		{
			$( "#BoardSetVip_" + id ).dialog( ( $( "#BoardSetVip_" + id ).dialog( "isOpen" ) ? "close" : "open" ) );
			return true;
		}
			else
		{
			var new_id = Math.floor( Math.random() * ( 100 - 5 + 1 ) ) + 5;
			var Element = document.createElement( "div" );
			document.body.appendChild( Element );
			Element.id = "BoardSetVip_" + id;
			
			$( "#BoardSetVip_" + id ).dialog({
				autoOpen: false,
				hide: "fade",
				title: "Поднять объявление",
				show: "slide",
				width: 450,
			});
			
			$( "#BoardSetVip_" + id ).html( "<div align=\"center\"><br /><br /><img src=\"/engine/inc/board/style/images/preloader.gif\" alt=\"Загрузка\" /></div>" );
			
			$.ajax({
				url: "/engine/ajax/board/vip.php",
				data: "id=" + id,
				success: function( data ){
					$( "#BoardSetVip_" + id ).html( data );
				},
				dataType: "html",
				type: "POST"
			});
			
			BoardModal[ "Vip-" + id ] = 1;
			return BoardVip( id );
		}
	
	return false;
}

function BoardColor( id ){
	
	if( BoardModal[ "Color-" + id ] == 1 )
		{
			$( "#BoardSetColor_" + id ).dialog( ( $( "#BoardSetColor_" + id ).dialog( "isOpen" ) ? "close" : "open" ) );
			return true;
		}
			else
		{
			var new_id = Math.floor( Math.random() * ( 100 - 5 + 1 ) ) + 5;
			var Element = document.createElement( "div" );
			document.body.appendChild( Element );
			Element.id = "BoardSetColor_" + id;
			
			$( "#BoardSetColor_" + id ).dialog({
				autoOpen: false,
				hide: "fade",
				title: "Выделить объявление",
				show: "slide",
				width: 450,
			});
			
			$( "#BoardSetColor_" + id ).html( "<div align=\"center\"><br /><br /><img src=\"/engine/inc/board/style/images/preloader.gif\" alt=\"Загрузка\" /></div>" );
			
			$.ajax({
				url: "/engine/ajax/board/color.php",
				data: "id=" + id,
				success: function( data ){
					$( "#BoardSetColor_" + id ).html( data );
				},
				dataType: "html",
				type: "POST"
			});
			
			BoardModal[ "Color-" + id ] = 1;
			return BoardColor( id );
		}
	
	return false;
}
