/****************************************************/
/********* Отправка сообщения эксперту **************/
/****************************************************/
function PostInChat(){
	var fields = new Array('fio', 'text', 'email');
	var req = new Array(
		new Array('fio', 'ФИО'),
		new Array('text', 'Текст вопроса')
	);
	var str = '';
	for (var i in fields) {
		var obj = document.getElementById('expert_form_form').elements[fields[i]];
		if (req[i] != null && obj.value=='') {
			alert("Заполните поле '" + req[i]['1'] + "'!");
			obj.focus();
			return false;
		}
		str += '&' + fields[i] + '=' + encodeURIComponent(obj.value);
	}
    
	var xsl_url = "/xslt/message.xsl";

	// Отправка GET
	// var xml_url = "/press/conference?act=liveMessage" + str + "&vid=" + (this.translation_id ? this.translation_id : this.nearest_id);
	// Ajax(xml_url, xsl_url, 'expert_message');

	// Отправка POST
	var xml_url = "/";
	AjaxPOST(xml_url, xsl_url, 'expert_message', "act=liveMessage" + str + "&vid=" + (this.translation_id ? this.translation_id : this.nearest_id) + "&dirs=/press/handleConference");

	document.getElementById('expert_form_form').style.display = 'none';
	document.getElementById('addQuestionLink').style.display = 'block';
	document.getElementById('question_text').value = '';
	return false;
}

/****************************************************/
/** Отображение сообщения об отправке (для Opera) ***/
/****************************************************/
function _Opera_setExpertMessage(){
	var obj_array = new Array();
	XMLtoArray(req.responseXML.documentElement, obj_array);
	document.getElementById('expert_message').innerHTML = obj_array['message'];
}

/****************************************************/
/********* Обновление экспертов на странице *********/
/****************************************************/
function UpdateExpert(obj) {
	if (!obj) return;	
	var experts_div = document.getElementById('experts');
	experts_div.innerHTML = '';
	document.getElementById('addQuestionLink').style.display = (obj['item']['hide_link'] == '1' || document.expert_form_form.style.display == 'block' ) ? 'none' : 'block';
	for (i in obj) {		
		for (k in obj[i]['questions']) {
		    str = "";
			var quest = obj[i]['questions'][k];
			//str = '<li>';			
			str += '<div id="q' + quest['id'] + '">';
			str += '<div class="whiteBlock comments"><div><div><div>';
			str += '<a>' + quest['fio'] + '</a>';
			if (quest['time_question']) {
                str += '<p class="date">' + getMyDate(quest['time_question']) + '</p>';
			}
			str += '<div class="hr"></div>';
			str += quest['question'];
			str += '<div class="clear"></div></div></div></div><div class="clear"></div></div><div class="clear"></div></div>';
			experts_div.innerHTML = experts_div.innerHTML + str;
			if (quest['answers']) {
				str2 = '';
				str2 += '<div class="answer" style="margin-top: -15px">';
				for (m in quest['answers']) {
					var item = quest['answers'][m];
					if (item['answer'] != '') {
						str2 += '<div class="whiteBlock comments" style="margin-bottom: 5px"><div><div><div>';
						str2 += '<a>' + item['expert_name'] + '</a>';
            			if (item['time_answer']) {
                            str2 += '<p class="date">' + getMyDate(item['time_answer']) + '</p>';
            			}
            			str2 += '<div class="hr"></div>';
						str2 += item['answer'];
						str2 += '<div class="clear"></div></div></div></div><div class="clear"></div></div>';
					}
				}
				str2 += '</div>';
				var q = document.getElementById('q' + quest['id']);
				if (q != null) {
					q.innerHTML += str2;
				}
			}
			
		}
	}
	if (experts_div.innerHTML != '') experts_div.innerHTML = '<h3>Вопросы-ответы</h3>' + experts_div.innerHTML;
	document.getElementById('translationQuestions').style.display = obj[i]['questions'] || document.expert_form_form.style.display == 'block' ? 'block' : 'none';
	this.refresh_time['expert'] = new Array();
	this.refresh_time['expert']['time'] = obj['item']['time'];

	if (document.getElementById('addQuestionLink').style.display == 'block') {
	    var href = location.href;
	    if (href.search('#form') != -1) {
	        document.expert_form_form.style.display = 'block';
	        document.getElementById('questionsFormSep').style.display = 'block';
	        document.getElementById('addQuestionLink').style.display = 'none';
	    }
	}
}

function getMyDate(time){
    var d = new Date(parseInt(time)*1000);
    var day = d.getDate(); if (day < 10) day = '0' + day;
    var mon = d.getMonth() + 1; if (mon < 10) mon = '0' + mon;
    var hour = d.getHours(); if (hour < 10) hour = '0' + hour;
    var min = d.getMinutes(); if (min < 10) min = '0' + min;
    var sec = d.getSeconds(); if (sec < 10) sec = '0' + sec;
    return day + '.' + mon + '.' + d.getFullYear() + ' ' + hour + ':' + min + ':' + sec;
}

function ShowUserInfo(id){
	var table = document.getElementById('peopleInfo' + id);
	if (table != null) {
		if (isIE4) {
			table.style.display = table.style.display == 'none' ? 'block' : 'none';
		} else {
			table.style.display = table.style.display == 'none' ? 'table' : 'none';
		}
		
	}
	return false;
} 
