var CasheProposalTemsTitle = new Array();
var CasheProposalTemsDescription = new Array();

function EditStatus( id ){

	document.getElementById( "loadstatus_proposal_status_" + id ).innerHTML = "<img src=\"/templates/" + dle_skin + "/images/proposal/horizont-ajax.gif\" alt=\"Загрузка\" border=\"0\" />";
	
	var new_status = document.getElementById( "proposal_status_" + id ).value;
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/edit_status.php",
		data: "new_status=" + new_status + "&id=" + id,
		success: function( data ){
			$( "#loadstatus_proposal_status_" + id ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

function SaveOfficialAnswer( id ){
	
	document.getElementById( "status_save_official_answer_" + id ).innerHTML = "<img src=\"/templates/" + dle_skin + "/images/proposal/horizont-ajax.gif\" alt=\"Загрузка\" border=\"0\" />";
	var form = document.forms[ "form_edit_official_answer_" + id ];
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/official_answer.php",
		data: "content=" + form.content.value + "&id=" + id,
		success: function( data ){
			$( "#status_save_official_answer_" + id ).html( data );
		},
		dataType: "html",
		type: "POST"
	});	
}

function ProposalEditTems( id ){
	
	if( !CasheProposalTemsDescription[ id ] )
		{
			CasheProposalTemsTitle[ id ] = document.getElementById( "proposal_title_" + id ).innerHTML;
			CasheProposalTemsDescription[ id ] = document.getElementById( "proposal_description_" + id ).innerHTML;
		}
	
	document.getElementById( "proposal_title_" + id ).innerHTML = "---";
	document.getElementById( "proposal_description_" + id ).innerHTML = "Идёт загрузка формы, подождите...";
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/tems-edit.php",
		data: "type=form&id=" + id,
		success: function( data ){
			$( "#proposal_description_" + id ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

function ProposalEditCancelTems( id ){
	
	if( CasheProposalTemsDescription[ id ] )
		{
			document.getElementById( "proposal_title_" + id ).innerHTML = CasheProposalTemsTitle[ id ];
			document.getElementById( "proposal_description_" + id ).innerHTML = CasheProposalTemsDescription[ id ];
			delete( CasheProposalTemsTitle[ id ] );
			delete( CasheProposalTemsDescription[ id ] );
		}
}

function ProposalEditSaveTems( id ){
	
	var new_title = document.getElementById( "edit-proposal-tems-title-" + id ).value;
	var new_description = document.getElementById( "edit-proposal-tems-description-" + id ).value;
	
	if( !new_title || new_title == "" ) 
		{
			alert( "Название темы обязательно к заполнению!" );
			return false;
		}
	
	document.getElementById( "status-edit-proposal-tems-" + id ).innerHTML = "<img src=\"/templates/" + dle_skin + "/images/proposal/horizont-ajax.gif\" alt=\"Загрузка\" border=\"0\" />";
	
	document.getElementById( "edit-proposal-tems-title-" + id ).disabled = true;
	document.getElementById( "edit-proposal-tems-description-" + id ).disabled = true;
	document.getElementById( "button-for-save-proposal-" + id ).disabled = true;
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/tems-edit.php",
		data: "title=" + new_title + "&description=" + new_description + "&id=" + id,
		success: function( data ){
			$( "#proposal_description_" + id ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

function ProposalDelTems( url, id, type ){
	
	if( !type ) type = "full";
	var Quest = confirm( "Вы действительно желаете удалить данныу идею и все комментарии относящиеся у ней?" );
	if( Quest && url )
		{
			if( type == "short" )
				{
					document.getElementById( "proposal_title_" + id ).innerHTML = "---";
					document.getElementById( "proposal_description_" + id ).innerHTML = "Идёт удаление идеи, подождите...";
					
					$.ajax({
						url: dle_root + "engine/ajax/proposal/del.php",
						data: "id=" + id,
						success: function( data ){
							$( "#proposal_description_" + id ).html( data );
							ForCloseId = id;
							setTimeout( 'ProposalCloseIdea()', 1000 );
						},
						dataType: "html",
						type: "POST"
					});
				}
					else
				{
					window.location = url;
				}
		}
}

function ProposalSearch( cat ){
	
	document.getElementById( "proposal_content" ).style['display'] = "none";
	document.getElementById( "proposal_search_result" ).style['display'] = "";
	document.getElementById( "proposal_search_for_load" ).innerHTML = "<div align=\"center\"><img src=\"/templates/" + dle_skin + "/images/proposal/load.gif\" alt=\"Загрузка\" border=\"0\" /></div>";
	
	var story = document.getElementById( "proposal_search" ).value;
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/search.php",
		data: "proposal_search=" + story + "&cat=" + cat,
		success: function( data ){
			$( "#proposal_search_result" ).html( data );
		},
		dataType: "html",
		type: "POST"
	});	
}

function ProposalCancelSearch(){
	document.getElementById( "proposal_search_result" ).style['display'] = "none";
	document.getElementById( "proposal_search_for_load" ).innerHTML = "<div align=\"center\"><img src=\"/templates/" + dle_skin + "/images/proposal/load.gif\" alt=\"Загрузка\" border=\"0\" /></div>";
	document.getElementById( "proposal_content" ).style['display'] = "";
}

function ProposalSelectPage( page,  status, cat ){
	
	if( page > 0 )
		{
			document.getElementById( "status-nav-proposal" ).innerHTML = "<img src=\"/templates/" + dle_skin + "/images/proposal/horizont-ajax.gif\" alt=\"Загрузка\" border=\"0\" />";
		}
			else
		{
			document.getElementById( "proposal_status_ajax" ).innerHTML = "<img src=\"/templates/" + dle_skin + "/images/proposal/mini-ajax.gif\" alt=\"Загрузка\" border=\"0\" />";
		}
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/content.php",
		data: "type=" + status + "&cat=" + cat + "&page=" + page,
		success: function( data ){
			$( "#proposal_content" ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

function ProposalRating( id, type ){
	
	ShowLoading( "" );
	$.ajax({
		url: dle_root + "engine/ajax/proposal/rating.php",
		data: "type=" + type + "&id=" + id,
		success: function( data ){
			$( "#proposal_rating_" + id ).html( data );
			HideLoading( "" );
		},
		dataType: "html",
		type: "POST"
	});
	
}

var proposal_answer_comments_id = new Array();

function ProposalAnswerComments( id ){

	if( !proposal_answer_comments_id[ id ] )
		{
			ProposalAnswerCommentsFalse();		
			document.getElementById( 'tree_comments_form-' + id ).innerHTML = form_test;
			proposal_answer_comments_id[ id ] = 1;
			document.getElementById( 'addcomments_form' ).innerHTML = "";
			document.getElementById( 'cancel_answer' ).style['display'] = "";
			document.getElementById( 'cancel_answer' ).innerHTML = "<input type=\"button\" onClick=\"ProposalAnswerComments( '" + id + "' ); return false;\" class=\"bbcodes\" value=\"Отмена\" /><input type=\"hidden\" name=\"answer_id\" value=\"" + id + "\">";
		}
			else
		{
			ProposalCancelAnswer();
		}
}

function ProposalCancelAnswer(){
	
	ProposalAnswerCommentsFalse();
	document.getElementById( 'addcomments_form' ).innerHTML = form_test;
	document.getElementById( 'cancel_answer' ).style['display'] = "none";
	
}

function ProposalAnswerCommentsFalse(){

	var com_search = document.getElementById( 'proposal_comments' );
	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}/ );
							proposal_answer_comments_id[ answer_id[ 0 ] ] = 0;
							delete( answer_id );
						}
				}
		}
}

function ProposalComPage( page, tems_id, link_tpl ){
	
	CancelAnswer();
	if( document.getElementById( "status-nav-com" ) != null )
		{
			document.getElementById( "status-nav-com" ).innerHTML = "<img src=\"/templates/" + dle_skin + "/images/proposal/mini-ajax.gif\" border=\"0\" alt=\"Загрузка\" align=\"absmiddle\" />";
		}
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/com-navigation.php",
		data: "page=" + page + "&tems=" + tems_id + "&link_tpl=" + link_tpl,
		success: function( data ){
			$( "#proposal_comments" ).html( data );
			window.location = "#comments";
		},
		dataType: "html",
		type: "POST"
	});
}

function ProposalAddComment(){
	
	var Url = "";
	var form = document.forms['AddProposalComment'];
	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
				}
			}
		}
	
	Url += "&addcomment=1";
	if( form.answer_id ) Url += "&answer_id=" + form.answer_id.value;
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/addcomments.php",
		data: Url,
		success: function( data ){
			
			HideLoading( "" );
			if( form.answer_id )
				{
					$( "#tree_comments_form-" + form.answer_id.value ).html( data );
					document.getElementById( 'button_answer_to_' + form.answer_id.value ).innerHTML = "Ответить";
					var ObjCom = document.getElementById( "tree_comments_form-" + form.answer_id.value );
					ObjCom.id = "tree_comments_result-" + form.answer_id.value;
					ProposalCancelAnswer();
				}
					else
				{
					var new_id = Math.floor( Math.random() * ( 100 - 5 + 1 ) ) + 5;
					var Element = document.createElement( "div" );
					document.getElementById( "proposal_comments" ).appendChild( Element );
					Element.id = "new_comment_" + new_id;
					$( "#" + Element.id ).html( data );
				}
			
			if( document.getElementById( "sec_code" ) != null )
				{
					document.getElementById( "sec_code" ).value = "";
					ProposalCaptchaReload();
				}
		},
		dataType: "html",
		type: "POST"
	});
}

function ProposalDelCom( id ){
	
	var Quest = confirm( "Вы действительно желаете удалить данный комментарий?" );
	if( Quest )
		{
			document.getElementById( "proposal_com_" + id ).innerHTML = "Происходит удаление, дождитесь ответа...";
			
			$.ajax({
				url: dle_root + "engine/ajax/proposal/com-del.php",
				data: "id=" + id,
				success: function( data ){
					$( "#proposal_com_" + id ).html( data );
				},
				dataType: "html",
				type: "POST"
			});
		}
	
}

var CasheCom = new Array();
function ProposalEditCom( id ){
	
	if( !CasheCom[ id ] ) CasheCom[ id ] = document.getElementById( "proposal_com_" + id ).innerHTML;
	document.getElementById( "proposal_com_" + id ).innerHTML = "Идёт загрузка, пожалуйста подождите...";	
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/com-edit.php",
		data: "id=" + id + "&type=form",
		success: function( data ){
			$( "#proposal_com_" + id ).html( data );
		},
		dataType: "html",
		type: "POST"
	});			
}

function ProposalEditCancelCom( id ){
	
	if( CasheCom[ id ] )
		{
			document.getElementById( "proposal_com_" + id ).innerHTML = CasheCom[ id ];
			delete( CasheCom[ id ] );
		}
	
}

function ProposalEditSaveCom( id ){
	
	var text = document.getElementById( "text-proposal-com-" + id ).value;
	var btn_save = document.getElementById( "button-for-save-com-proposal-" + id );
	var btn_cancel = document.getElementById( "button-for-cancel-com-proposal-" + id );
	
	if( text == "" )
		{
			alert( "Введите комментарий!" );
			return false;
		}
	
	document.getElementById( "text-proposal-com-" + id ).disabled = true;
	btn_save.disabled = true;
	
	document.getElementById( "status-edit-proposal-com-" + id ).innerHTML = "<img src=\"/templates/" + dle_skin + "/images/proposal/horizont-ajax.gif\" alt=\"Загрузка\" border=\"0\" />";
	
	$.ajax({
		url: dle_root + "engine/ajax/proposal/com-edit.php",
		data: "id=" + id + "&text=" + text,
		success: function( data ){
			$( "#proposal_com_" + id ).html( data );
		},
		dataType: "html",
		type: "POST"
	});
}

var ForCloseId = 0;
function ProposalModer( id ){
	
	if( document.getElementById( "proposal_moder_load_" + id ) != null )
		{
			document.getElementById( "proposal_moder_load_" + id ).innerHTML = "<img src=\"/templates/" + dle_skin + "/images/proposal/horizont-ajax.gif\" alt=\"Загрузка\" border=\"0\" />";
		}
		
	$.ajax({
		url: dle_root + "engine/ajax/proposal/moder.php",
		data: "id=" + id,
		success: function( data ){
			
			if( document.getElementById( "proposal_moder_load_" + id ) != null )
				{
					$( "#proposal_moder_load_" + id ).html( data );
					ForCloseId = id;
					setTimeout( 'ProposalCloseIdea()', 1000 );
				}
					else
				{
					$( "#proposal_mini_idea_" + id ).html( data );
				}
		},
		dataType: "html",
		type: "POST"
	});
}

function ProposalCloseIdea(){
	
	if( ForCloseId > 0 )
		{
			var Element = document.getElementById( "proposal_mini_idea_" + ForCloseId );
			Element.parentNode.removeChild( Element );
			ForCloseId = 0;
		}
}

function ProposalMenuedit( id, unite_url, spam_url, del_url, duplicate_url, not_duplicate_url, approve, type ){
	
	if( !type ) type = "full";
	var menu = new Array();
		
	menu[0] = "<a href=\"javascript:void(0);\" onclick=\"ProposalEditTems( '" + id + "' ); return false;\">Редактировать идею</a>";
	if( approve == 1 )
		{
			menu[1] = "<a href=\"" + unite_url +"\">Объеденить идеи</a>";
		}
			else
		{
			menu[1] = "<a href=\"javascript:void(0);\" onclick=\"ProposalModer( '" + id + "' ); return false;\">Опубликовать</a>";
		}
	
	if( not_duplicate_url == "" )
		{
			menu[2] = "<a href=\"" + duplicate_url +"\">Это дубликат</a>";
		}
			else
		{
			menu[2] = "<a href=\"" + not_duplicate_url +"\">Это не дубликат</a>";	
		}
		
	menu[3] = "<a href=\"" + spam_url + "\">Это спам</a>";
	menu[4] = "<a href=\"javascript:ProposalDelTems('" + del_url + "', '" + id + "', '" + type + "');\">Удалить</a>";
			
	return menu;
	
}

function ProposalCaptchaReload(){

		var rndval = new Date().getTime(); 
		if( document.getElementById( "dle-captcha" ) != null ) document.getElementById( "dle-captcha" ).innerHTML = "<img src=\"/engine/modules/antibot.php?rndval=" + rndval + "\" alt=\"Включите эту картинку для отображения кода безопасности\" border=\"0\" onclick=\"ProposalCaptchaReload(); return false;\" style=\"cursor: pointer;\" width=\"120\" height=\"50\" />";
	
}
