function ajaxTextPreview(textId,save,divPreview) { 
	var text;    
	if (BLOG_USE_TINYMCE && tinyMCE && (ed=tinyMCE.get(textId))) {
		text = ed.getContent();
	} else {
		text = $(textId).value;	
	}	
	JsHttpRequest.query(
    	DIR_WEB_ROOT+'/include/ajax/textPreview.php',                       
        { text: text, save: save },
        function(result, errors) {  
        	if (!result) {
                msgErrorBox.alert('Error','Please try again later');           
        	}
            if (result.bStateError) {
            	msgErrorBox.alert('Error','Please try again later');
            } else {    	
            	if (!divPreview) {
            		divPreview='text_preview';
            	}            	
            	if ($(divPreview)) {
            		$(divPreview).set('html',result.sText).setStyle('display','block');
            	}
            }                               
        },
        true
    );
}


// для опроса
function addField(btn){
        tr = btn;
        while (tr.tagName != 'TR') tr = tr.parentNode;
        var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
        checkFieldForLast();
}
function checkFieldForLast(){	
        btns = document.getElementsByName('drop_answer');      
        for (i = 0; i < btns.length; i++){
        	btns[i].disabled = false;            
        }
        if (btns.length<=2) {
        	btns[0].disabled = true;
        	btns[1].disabled = true;
        }
}
function dropField(btn){	
        tr = btn;
        while (tr.tagName != 'TR') tr = tr.parentNode;
        tr.parentNode.removeChild(tr);
        checkFieldForLast();
}



function checkAllTalk(checkbox) {
	$$('.form_talks_checkbox').each(function(chk){
		if (checkbox.checked) {
			chk.checked=true;
		} else {
			chk.checked=false;
		}		
	});	
}


function showImgUploadForm() {	
	if (Browser.Engine.trident) {
		//return true;
	}	
	if (!winFormImgUpload) {		
		winFormImgUpload=new StickyWin.Modal({content: $('window_load_img'), closeClassName: 'close-block', useIframeShim: false});
	}
	winFormImgUpload.show();
	winFormImgUpload.pin(true);
	return false;
}

function hideImgUploadForm() {
	winFormImgUpload.hide();
	
	bTeaserOnly=false;
	bNoTeaser=false;
}

var winFormImgUpload;

function ajaxUploadImg(value,sToLoad) {
	if (typeof bNoTeaser == "undefined") {
		bNoTeaser = false;
	}
	 
	sToLoad=$(sToLoad);
	// бокс для хранение превьюшек
	oTeaserBox=$('topic_img_teaser');
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.responseJS.bStateError) {
				msgErrorBox.alert(req.responseJS.sMsgTitle,req.responseJS.sMsg);				
			} else {				
				if(!bTeaserOnly) sToLoad.insertAtCursor(req.responseJS.sText);
				// выводим превью в бокс
				if(!bNoTeaser && req.responseJS.sTeaserPath && req.responseJS.sTeaserPath != DIR_WEB_ROOT+'/') {
					oTeaserImg = new Element('img', { 'src': req.responseJS.sTeaserPath });
					oTeaserCheck = new Element('input', {'type':'radio', 'name':'topic_img_teaser', 'value':req.responseJS.sTeaserPath });
					oTeaserDiv = new Element('p', 
						{
							'styles': {
								'border': '1px dotted lightgray', 
								'padding':'2px', 
								'margin-bottom':'3px'
							}
						}
					);
					oTeaserBox.adopt(oTeaserDiv.adopt(oTeaserCheck,oTeaserImg));				
				}
				
				hideImgUploadForm();
			}
		}
	}
	req.open(null, DIR_WEB_ROOT+'/include/ajax/uploadImg.php', true);
	req.send( { value: value } );
}

function submitTopicForm() {
	oTeaserCheck = $('topic_img_teaser').getElement('input[name=topic_img_teaser]:checked'); 
	sTeaserPath = '';
	
	if( oTeaserCheck ){
		sTeaserPath = oTeaserCheck.getProperty('value');
	} else {
		if($('topic_img_teaser').getElements('input[name=topic_img_teaser]').length) {
			sTeaserPath = $('topic_img_teaser').getElement('input[name=topic_img_teaser]').getProperty('value');
		}
	}
	$(document.body).getElement('input[name=topic_teaser_path]').setProperty('value',sTeaserPath);
	$(document.body).getElement('input[name=topic_teaser_video]').setProperty('value',($('topic_teaser_video_block').getProperty('checked')) ? 1:0);
		
	return true;
}