var callstat='';
var calldie=0;

function die_call_phone(die) {
	calldie=die;
	get('call_form0').style.display='';
	get('call_form1').style.display='none';
}

function submit_call_phone(ret_div,id) {
   die=calldie;
   JsHttpRequest.query(
      '/popupcall_/action_call/', // backend
      {
		'id' : id,
		'to': document.getElementById("form_"+ret_div).to.value, 
		'time': document.getElementById("form_"+ret_div).time.value, 
		'from': document.getElementById("form_"+ret_div).from.value,
		'die': calldie
      },
      function(r, errors) {
	  	 get('call_form0').style.display='none';
		 get('call_form1').style.display='none';
		 if(r["errors"]) {
		 	get('call_form0').style.display='';
		 } else {
		 	get('call_form1').style.display='';
		 }
         obj_mes=get(ret_div);
		 if(r["errors"]) {
		 	obj_mes.innerHTML="<b class='red big'>"+r["errors"]+"</b>";
			die_call_phone(1);
			return false;
		 }
		 if(callstat!=r["result"]["status"]) {
			callstat=r["result"]["status"];
			obj_mes.innerHTML= "<div class=small style='padding:5px 0'>"+ get_call_message(r) + "</div>";
		 }
		 if(r["result"]["closed"]=="NO") {
			setTimeout(function(){submit_call_phone(ret_div,r["result"]["id"])},1000);
			return false;
		 } else {
		 	get("form_"+ret_div).reset();
		 	die_call_phone(1);
		 	return false;
		 }
      },
      true
   );
}

function get_call_message(r) {
	if(r["result"]["closed_time"])	rtime=r["result"]["closed_time"];
	else if(r["result"]["answered_time"]) rtime=r["result"]["answered_time"];
	else if(r["result"]["callback_time"]) rtime=r["result"]["callback_time"];
	else if(r["result"]["posted"])	rtime=r["result"]["posted"];
	abon=get('abon'); 	abon_call=get('abon_call');
	oper=get('oper');	oper_call=get('oper_call');
	mess='';
	if(r["result"]["status"]=='DIALING') {
		abon_call.src='/images/call/abon_call2.gif';
		mess='<div style="padding: 7px 0" class="green big2"><b>Идёт соединение<br><span class="gray2">с абонентом</span></b></div>';
	} else if(r["result"]["status"]=='CALLBACK_ANSWERED') {
		abon_call.src='/images/call/abon_call1.gif';
		abon.src='/images/call/abon1.gif';
		mess='<div style="padding: 7px 0" class="green big2"><b>Установлено соединение<br><span class="gray2">с абонентом</span></b></div>';
	} else if(r["result"]["status"]=='QUEUED') {
		oper_call.src='/images/call/oper_call2.gif';
		mess='<div style="padding: 7px 0"><div class="small">соединение с абонентом установлено</div><b><span class="green big2">Идёт соединение</span><br><span class="gray2 big2">с оператором</span></b></div>';
	} else if(r["result"]["status"]=='ANSWERED') {
		oper_call.src='/images/call/oper_call1.gif';
		oper.src='/images/call/oper1.gif';
		mess='<div style="padding: 13px 0" class="green"><b class="big2">Связь установлена</b><div class="big"><b>На связи: '+r["result"]["answered_name"]+'</b></div></div>';
	} else if(r["result"]["closed"]=='YES') {
		abon.src='/images/call/abon0.gif';
		abon_call.src='/images/call/abon_call0.gif';
		oper.src='/images/call/oper0.gif';
		oper_call.src='/images/call/oper_call0.gif';
		if(r["result"]["status"]=='NORMAL_CLEARING') {
			mess='<div style="padding: 13px 0" class="green big2"><b>Вызов завершён</b></div>';
		} else if(r["result"]["status"]=='CALL_TIME') {
			mess='<div style="padding: 13px 0" class="green big2"><b>'+r["result"]["status_text"]+'</b></div>';
		} else {
			mess='<div style="padding: 13px 0" class="red big2"><b>'+r["result"]["status_text"]+'</b></div>';
		}
	}
	return mess;
}




function val_registr() {
	f=get('registr-form');
	er=get('errormes');
	er.innerHTML='';
	if (f.fio.value=='') {
		er.innerHTML='Введите Ф.И.О.';
		return false;
	}
	if (f.phone.value=='' || f.phone.value=='+7') {
		er.innerHTML='Введите контактный телефон';
		return false;
	}
	if (f.email.value=='') {
		er.innerHTML='Введите Email';
		return false;
	}
	//return true; //true
	// временный блок логина
	//er.innerHTML='Проверьте правильность ввода логина или пароля';
	return true;
}

function val_login() {
	er=get('errormes');
	er.innerHTML='';
	if (get('login_f_login').value=='') {
		er.innerHTML='Введите логин!';
		return false;
	}
	if (get('login_f_pass').value=='') {
		er.innerHTML='Введите пароль!';
		return false;
	}
	return true; //true
	// временный блок логина
	er.innerHTML='Проверьте правильность ввода логина или пароля';
	return false;
}
  