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('Ошибка','Возникли проблемы при обработке предпросмотра');
            } 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 (!winFormImgUpload) { 
	winFormImgUpload=new StickyWinModal({content: $('uploadimg-form-content').get('html'), closeClassName: 'close-block'}); 
	} 
	winFormImgUpload.show();
	winFormImgUpload.pin(true);
	
}

function hideImgUploadForm() {
	winFormImgUpload.hide();
}

var winFormImgUpload;

window.addEvent('domready', function() {  	
	var form=$('window_load_img');
	if (form) {
		form.setStyle('display','block');
    	winFormImgUpload=new StickyWinModal({content: form, closeClassName: 'close-block'});    
    	winFormImgUpload.pin(true);
    	winFormImgUpload.hide();
	}
});


function ajaxUploadImg(value,sToLoad) {
	sToLoad=$(sToLoad);
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.responseJS.bStateError) {
				msgErrorBox.alert('Ошибка','Возникли проблемы при загрузке изображения, попробуйте еще разок. И на всякий случай проверьте правильность URL картинки');
			} else {				
				sToLoad.insertAtCursor(req.responseJS.sText);
				hideImgUploadForm();
			}
		}
	}
	req.open(null, DIR_WEB_ROOT+'/include/ajax/uploadImg.php', true);
	req.send( { value: value } );
}

function ajaxUploadFile(value, sToLoad)
{
    var req = new JsHttpRequest();    
    req.onreadystatechange = function() {
        if (req.readyState == 4) {         
            document.getElementById('debug').innerHTML = req.responseText;  
            closeWindowStatus();         
            if (req.responseJS.bStateError) {
                showWindowStatus(req.responseJS.sText);
            } else {   
                voidPutTag(sToLoad,req.responseJS.sText);               
            }
        }
    }    
    closeWindow('window_load_file');
    showWindowStatus('Загрузка файла...');
    req.open(null, DIR_WEB_ROOT+'/include/ajax/uploadFile.php', true);    
    req.send( { value: value } );
}

/* torrent begin */
function showTorrentUploadForm() {	
	if (Browser.Engine.trident) {
		//return true;
	}	
	if (!winFormTorrentUpload) {		
		winFormTorrentUpload=new StickyWin.Modal({content: $('window_load_torrent'), closeClassName: 'close-block', useIframeShim: false});
	}
	winFormTorrentUpload.show();
	winFormTorrentUpload.pin(true);
	return false;
}

function hideTorrentUploadForm() {
	winFormTorrentUpload.hide();
}

var winFormTorrentUpload;


function ajaxUploadTorrent(value,sToLoad) {
	sToLoad=$(sToLoad);
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.responseJS.bStateError) {
				msgErrorBox.alert(req.responseJS.sMsgTitle,req.responseJS.sMsg);				
			} else {				
				sToLoad.insertAtCursor(req.responseJS.sText);
				hideTorrentUploadForm();
			}
		}
	}
	req.open(null, DIR_WEB_ROOT+'/include/ajax/uploadTorrent.php', true);
	req.send( { value: value } );
}
/* torrent end */


/* questions.js */
function ajaxQuestionVote(idTopic,idAnswer) {	
	JsHttpRequest.query(
    	DIR_WEB_ROOT+'/include/ajax/questionVote.php',                       
        { idTopic: idTopic, idAnswer: idAnswer },
        function(result, errors) {  
        	if (!result) {
                msgErrorBox.alert('Error','Please try again later');           
        	}
            if (result.bStateError) {
            	msgErrorBox.alert(result.sMsgTitle,result.sMsg);
            } else {            	
            	msgNoticeBox.alert(result.sMsgTitle,result.sMsg);
            	if ($('topic_question_area_'+idTopic)) {
            		$('topic_question_area_'+idTopic).set('html',result.sText);
            	}  
            }                               
        },
        true
    );	
}

/*
 * PANEL.JS
 */
var lsPanelClass = new Class({	
	initialize: function(){
		
	},
	
	putText: function(obj,text) {
		obj=$(obj);
		obj.insertAtCursor(text);
	}, 
	
	putTag: function(obj,tag) {
		this.putText(obj,'<'+tag+'/>');
	},
	
	putTextAround: function(obj,textStart,textEnd) {
		obj=$(obj);
		obj.insertAroundCursor({
			before: textStart,
			defaultMiddle: '',
			after: textEnd
		});
	},
	
	putTagAround: function(obj,tagStart,tagEnd) {
		if (!tagEnd) {
			tagEnd=tagStart;
		}
		this.putTextAround(obj,'<'+tagStart+'>','</'+tagEnd+'>');
	},
	
	putTagUrl: function(obj,sPromt) {
		obj=$(obj);
		if (url=prompt(sPromt,'http://')) {
			var sel=obj.getSelectedText();
        	this.putText(obj,'<a href="'+url+'">'+sel+'</a>');
        }
	}
});

var lsPanel;

window.addEvent('domready', function() {  	
    lsPanel = new lsPanelClass();   
});


/*
 *LOGIN.JS
 */
function showLoginForm() {	
	if (!winFormLogin) {
		winFormLogin=new StickyWinModal({content: $('login-form-content').get('html'), closeClassName: 'close-block'});
	}
	winFormLogin.show();
	winFormLogin.pin(true);	
}

function hideLoginForm() {
	winFormLogin.hide();
}

var winFormLogin=false;


/*
 *BLOG.JS
 */
function ajaxJoinLeaveBlog(obj,idBlog) {   
	obj=$(obj);
	JsHttpRequest.query(
    	DIR_WEB_ROOT+'/include/ajax/joinLeaveBlog.php',                       
        { idBlog: idBlog },
        function(result, errors) {  
        	if (!result) {
                msgErrorBox.alert('Error','Please try again later');           
        	}
            if (result.bStateError) {
            	msgErrorBox.alert(result.sMsgTitle,result.sMsg);
            } else {            	
            	msgNoticeBox.alert(result.sMsgTitle,result.sMsg);
            	if (obj)  {     
            		obj.getParent().removeClass('active');       		
            		if (result.bState) {            			
            			obj.getParent().addClass('active');
            		}
            		divCount=$('blog_user_count_'+idBlog);
            		if (divCount) {
            			divCount.set('text',result.iCountUser);
            		}
            	}
            }                               
        },
        true
    );
}


function ajaxBlogInfo(idBlog) { 	
	JsHttpRequest.query(
    	DIR_WEB_ROOT+'/include/ajax/blogInfo.php',                       
        { idBlog: idBlog },
        function(result, errors) {  
        	if (!result) {
                msgErrorBox.alert('Error','Please try again later');           
        	}
            if (result.bStateError) {
            	
            } else {            	
            	if ($('block_blog_info')) {
            		$('block_blog_info').set('html',result.sText);            		
            	}
            }                               
        },
        true
    );
}


/*
 * FRIEND.JS
 */
function ajaxToggleUserFrend(obj,idUser) {   
	obj=$(obj);
	JsHttpRequest.query(
    	DIR_WEB_ROOT+'/include/ajax/userFriend.php',                       
        { idUser: idUser },
        function(result, errors) {  
        	if (!result) {
                msgErrorBox.alert('Error','Please try again later');           
        	}
            if (result.bStateError) {
            	msgErrorBox.alert(result.sMsgTitle,result.sMsg);
            } else {            	
            	msgNoticeBox.alert(result.sMsgTitle,result.sMsg);
            	if (obj)  {
            		obj.set('text',result.sToggleText);
            		if (result.bState) {
            			obj.getParent('li').removeClass('add');
            			obj.getParent('li').addClass('del');
            		} else {
            			obj.getParent('li').removeClass('del');
            			obj.getParent('li').addClass('add');
            		}
            	}
            }                               
        },
        true
    );
}