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;
	var ajax = new dle_ajax();
	ajax.setVar( "new_status", new_status );
	ajax.setVar( "id", id );
	ajax.execute = true;
	ajax.requestFile = dle_root + "engine/ajax/proposal/edit_status.php";
	ajax.method = "POST";
	ajax.element = "loadstatus_proposal_status_" + id;
	ajax.sendAJAX();

}

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 ];
	
	var ajax = new dle_ajax();
	ajax.setVar( "content", ajax.encodeVAR( form.content.value ) );
	ajax.setVar( "id", id );
	ajax.requestFile = dle_root + "engine/ajax/proposal/official_answer.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.element = "status_save_official_answer_" + id;
	ajax.sendAJAX();
	
}

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 = "Идёт загрузка формы, подождите...";
	
	var ajax = new dle_ajax();
	ajax.setVar( "id", id );
	ajax.setVar( "type", "form" );
	ajax.requestFile = dle_root + "engine/ajax/proposal/tems-edit.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.element = "proposal_description_" + id;
	ajax.sendAJAX();
}

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;
	
	var ajax = new dle_ajax();
	ajax.setVar( "title", ajax.encodeVAR( new_title ) );
	ajax.setVar( "description", ajax.encodeVAR( new_description ) );
	ajax.setVar( "id", id );
	ajax.requestFile = dle_root + "engine/ajax/proposal/tems-edit.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.element = "proposal_description_" + id;
	ajax.sendAJAX();
}

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 = "Идёт удаление идеи, подождите...";
					var ajax = new dle_ajax();
					ajax.setVar( "id", id );
					ajax.requestFile = dle_root + "engine/ajax/proposal/del.php";
					ajax.method = 'POST';
					ajax.execute = true;
					ajax.element = "proposal_description_" + id;
					
					ajax.onCompletion = function(){
						ForCloseId = id;
						setTimeout( 'ProposalCloseIdea()', 1000 );
					}
					
					ajax.sendAJAX();
				}
					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;
	var ajax = new dle_ajax();
	ajax.setVar( "proposal_search", ajax.encodeVAR( story ) );
	ajax.setVar( "cat", cat );
	ajax.requestFile = dle_root + "engine/ajax/proposal/search.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.element = "proposal_search_result";
	ajax.sendAJAX();
	
}

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\" />";
		}
	
	var ajax = new dle_ajax();
	ajax.setVar( "type", status );
	ajax.setVar( "cat", cat );
	ajax.setVar( "page", page );
	ajax.requestFile = dle_root + "engine/ajax/proposal/content.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.element = "proposal_content";
	ajax.sendAJAX();
}

function ProposalRating( id, type ){
	
	var ajax = new dle_ajax();
	ajax.onShow( "" );
	ajax.setVar( "type", type );
	ajax.setVar( "id", id );
	ajax.requestFile = dle_root + "engine/ajax/proposal/rating.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.element = "proposal_rating_" + id;
	ajax.sendAJAX();
	
}

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\" />";
		}
	
	var ajax = new dle_ajax();
	ajax.setVar( "page", page );
	ajax.setVar( "tems", tems_id );
	ajax.setVar( "link_tpl", link_tpl );
	ajax.requestFile = "/engine/ajax/proposal/com-navigation.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.onCompletion = function(){
		window.location = "#comments";
	}
	ajax.element = "proposal_comments";
	ajax.sendAJAX();
	
}

function ProposalAddComment(){
	var form = document.forms['AddProposalComment'];
    var ajax = new dle_ajax();
		
	ajax.onShow( "" );
	
	for( var i=0; i < form.elements.length; i++ )
		{
			var ElementForm = form.elements[i];
			switch( ElementForm.type ){
				
				case "checkbox": {
					if( ElementForm.checked == true )
						{
							ajax.setVar( ElementForm.name, ajax.encodeVAR( ElementForm.value ) );
						}
					break
				}
				
				default: {
					ajax.setVar( ElementForm.name, ajax.encodeVAR( ElementForm.value ) );
					break
				}
			}
		}
		
	ajax.setVar( "addcomment", 1 );
	
	ajax.requestFile = dle_root + "engine/ajax/proposal/addcomments.php";
	ajax.method = 'POST';
	ajax.execute = true;
	if( form.answer_id )
		{
			ajax.setVar( "answer_id", form.answer_id.value );
			ajax.element = "tree_comments_form-" + form.answer_id.value;
			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( "proposal_comments" ).appendChild( Element );
			Element.id = "new_comment_" + new_id;
			ajax.element = Element.id;
		}
		
	ajax.onCompletion = function(){
		
		if( form.answer_id )
			{
				var ObjCom = document.getElementById( "tree_comments_form-" + form.answer_id.value );
				ObjCom.id = "tree_comments_result-" + form.answer_id.value;
				ProposalCancelAnswer();
			}
			
		if( document.getElementById( "sec_code" ) != null )
			{
				document.getElementById( "sec_code" ).value = "";
				ProposalCaptchaReload();
			}
	}
	
	ajax.sendAJAX();
}

function ProposalDelCom( id ){
	
	var Quest = confirm( "Вы действительно желаете удалить данный комментарий?" );
	if( Quest )
		{
			document.getElementById( "proposal_com_" + id ).innerHTML = "Происходит удаление, дождитесь ответа...";	
			var ajax = new dle_ajax();
			ajax.setVar( "id", id );
			ajax.requestFile = "/engine/ajax/proposal/com-del.php";
			ajax.method = 'POST';
			ajax.execute = true;
			ajax.element = "proposal_com_" + id;
			ajax.sendAJAX();
		}
	
}

var CasheCom = new Array();
function ProposalEditCom( id ){
	
	if( !CasheCom[ id ] ) CasheCom[ id ] = document.getElementById( "proposal_com_" + id ).innerHTML;
	document.getElementById( "proposal_com_" + id ).innerHTML = "Идёт загрузка, пожалуйста подождите...";	
	var ajax = new dle_ajax();
	ajax.setVar( "id", id );
	ajax.setVar( "type", "form" );
	ajax.requestFile = "/engine/ajax/proposal/com-edit.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.element = "proposal_com_" + id;
	ajax.sendAJAX();
			
}

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\" />";
	
	var ajax = new dle_ajax();
	ajax.setVar( "text", ajax.encodeVAR( text ) );
	ajax.setVar( "id", id );
	ajax.requestFile = dle_root + "engine/ajax/proposal/com-edit.php";
	ajax.method = 'POST';
	ajax.execute = true;
	ajax.element = "proposal_com_" + id;
	ajax.sendAJAX();
}

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\" />";
		}
		
	var ajax = new dle_ajax();
	ajax.setVar( "id", id );
	ajax.requestFile = dle_root + "engine/ajax/proposal/moder.php";
	ajax.method = 'POST';
	ajax.execute = true;
	if( document.getElementById( "proposal_moder_load_" + id ) != null )
		{
			ajax.element = "proposal_moder_load_" + id;
			ajax.onCompletion = function(){
				ForCloseId = id;
				setTimeout( 'ProposalCloseIdea()', 1000 );
			}
		}
			else
		{
			ajax.element = "proposal_mini_idea_" + id;
		}
	ajax.sendAJAX();
}

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(); 
		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\" />";
	
	}
