function o_check(){
	var	str_show_error = 'Не все обязательные поля заполнены!',
		regX2 = /^[A-Za-zА-Яа-я\s_-]+$/,
		e_err = false;
	if ($.trim($("#select_city").val())=='' || $.trim($("#fio").val())=='')
		e_err = true;
	if(!regX2.test($.trim($("#fio").val()))){
		if(e_err){
			str_show_error += '\nПоле логина содержит недопустимые символы.';
		} else {
			str_show_error = 'Поле логина содержит недопустимые символы.';
		}
		e_err = true;
	}
	if(e_err){
		alert(str_show_error);
		return false;
	}
	$("#f_login_online").submit();
}

/*speed=1000;
len=40;
tid = 0;
num=0;
clockA = new Array();
timeA = new Array();
formatA = new Array();
dd = new Date();
var d,x;

var time_to_wait = 120;
 
function doDate(x)
{
  for (i=0;i<num;i++) {
	 dt = new Date();
     if (timeA[i] != 0) {
      v1 = Math.round(( dt - timeA[i] )/1000) ;
	  if (formatA[i] == 1){
		if (x==1){
			//alert(v1);
			if (v1>0 && v1<3){
				setCookie("timer_1", dt, "Mon, 01-Jan-2051 00:00:00 GMT", "/");
			}
			var showed = getCookie("showed");
			//alert(v1);
			//alert(v1+" "+showed+" "+(showed!=1));
			if (v1>time_to_wait && (showed!=1)){
				showBan();
				return false;
			}
		}
        $("#timer"+x).val(v1);
      }else if (formatA[i] ==2) {
        sec = v1%60;
		v1 = Math.floor( v1/60);
		min = v1 %60 ;
		hour = Math.floor(v1 / 60);
		if (sec < 10 ) sec = "0"+sec;
		if (min < 10 ) min = "0"+min;
		var vv1 = hour+":"+min+":"+sec;
		$("#timer"+x).text(vv1);
     }else if (formatA[i] ==3) {
        sec = v1%60;
		v1 = Math.floor( v1/60);
		min = v1 %60 ;
		v1 = Math.floor(v1 / 60);
		hour = v1 %24 ;
		day = Math.floor(v1 / 24);
		if (sec < 10 ) sec = "0"+sec;
		if (min < 10 ) min = "0"+min;
		if (hour < 10 ) hour = "0"+hour;
		$("#timer"+x).text(hour+":"+min+":"+sec);
     }else if (formatA[i] ==4 ) {
        sec = v1%60;
		v1 = Math.floor( v1/60);
		min = v1 %60 ;
		v1 = Math.floor(v1 / 60);
		hour = v1 %24 ;
		day = Math.floor(v1 / 24);
        var vv1 = day+(day==1?"day ":"days ")+hour+(hour==1?"hour ":"hours ")+min+(min==1?"min ":"mins ")+sec+(sec==1?"sec ":"secs ");
		 $("#timer"+x).text(vv1);
    }else
	  $("#timer"+x).text("");
	}else
		$("#timer"+x).text("");
    }
	
	tid=window.setTimeout("doDate("+x+")",speed);
}
 
function start(d,x,format) {
  clockA[num] = x
  if (d == "now")
    timeA[num] = new Date();
  else
    timeA[num] = new Date(d);
  if (x==1){
	c = getCookie("timer_1");
	if (c!=null){
		timeA[num] = new Date(c);
		//alert(c+" "+timeA[num]);
	}
  }
  formatA[num] = format;
  tid=window.setTimeout("doDate("+x+")",speed);
  num++;
}
 
function CountupLong(t,format,len){
    start(t,0,format);
}*/

function nl2br_js(myString){
	var regX = /\n/g;
	var replaceString = '<br>';
	return myString.replace(regX, replaceString);
	//return myString.replace("\n", "<br>");
}

//////////

var o_ac_ch = 0;
var end_chat = 0;

function o_sendMsg(){
	var datestamp = getMsgDate();
	
	var elem_reply = $("#area_reply");
	var msg = elem_reply.val();
	var msg2 = nl2br_js(msg);
	var app = '<div';
	if (o_manager==1){
		app+=' class="manager"';
	}
	elem_reply.val("");
	if($.trim(msg)!=''){
		app+='><span>'+o_name+' '+datestamp+':</span><p>'+msg2+'</p></div>';
		$("#chat_window").prepend(app);
		$.ajax({ 
				type: "POST", 
				url: "/online/?send_msg=1", 
				datatype: "html", 
				data: {'msg':msg,'channel':o_ac_ch}, 
				success: function(data){ 
					$("#ajax_res").html(data);
				}
		}); // $.ajax()
	}
}

function getMsgDate() {
	var datetime = new Date();
	return '('+ formatLeadZeros(datetime.getDate()) +'.'+ formatLeadZeros(datetime.getMonth() + 1) +' '+ formatLeadZeros(datetime.getHours()) +':'+ formatLeadZeros(datetime.getMinutes()) +')';
}
function formatLeadZeros(value) {
	return (parseInt(value, 10) < 10 ) ? ('0'+value) : (value);
}


function o_readChannel(id){
	o_ac_ch = id;
	$.ajax({ 
			type: "POST", 
			url: "/online/", 
			datatype: "html", 
			data: {'read_channel':1, 'id':id}, 
			success: function(data){ 
				$("#ajax_res").html(data);
				$('.sel_client').removeClass("sel_client");
				$("#p_"+id).addClass("sel_client");
			}
	}); 
}

function o_updateChat(){
	if (end_chat==1){
		return false;
	}
	$.ajax({ 
			type: "POST", 
			url: "/online/", 
			datatype: "html", 
			data: {'update_channel':1, 'id':o_ac_ch}, 
			success: function(data){ 
				//alert(data);
				$("#ajax_res").html(data);
			}
	}); 
	setTimeout('o_updateChat();', 4000);
}

function o_setUnread(id, val, name, city, pos, utype, diff, phone, email){
	//utype = 3;
	if($('#channel_'+id).length){
		if (val==0){
			$("#cnt_"+id).html("");
		}else{
			$("#cnt_"+id).html("<strong>("+val+")</strong>");
		}
		if (utype==1){
			$("#color_"+id).css("background-color", "green");
		}else{
			if  (utype==2){
				$("#color_"+id).css("background-color", "red");
			}else{
				$("#color_"+id).css("background-color", "gray");
			}
		}
		$("#time_"+id).html(diff);
	}else{
		var app = '<li id="channel_'+id+'">';
		if (utype==1){
			app+='<div style="background-color: green; width: 5px; height: 30px; float: left;" id="color_'+id+'">&nbsp;</div>';
		}else{
			if(utype==2){
				app+='<div style="background-color: red; width: 5px; height: 30px; float: left;" id="color_'+id+'">&nbsp;</div>';
			}else{
				app+='<div style="background-color: gray; width: 5px; height: 30px; float: left;" id="color_'+id+'">&nbsp;</div>';
			}
		}
		app+='<p id="p_'+id+'" onclick="o_readChannel('+id+');"';
		//alert(o_ac_ch+' '+id);
		if (o_ac_ch==0 || o_ac_ch==id){
			app+=' class="sel_client"';
		}
		app+='>';
		app+=name+' <a style="color: gray; margin-left: 5px; margin-right: 5px;" href="#" onclick="online_bl(\''+id+'\'); return false;">BLOCK</a>';
		app+='<img src="/img/icoOnlineUser1.gif" class="icoOnlineUserInActive"/> <img src="/img/icoOnlineUser2.gif" class="icoOnlineUserActive"/><span id="cnt_'+id+'">';
		if (val>0){
			app+='<strong>('+val+')</strong>';
		}
		app+='</span>&nbsp;<span id="time_'+id+'">'+diff+'</span><div class="onlineHoverInfoBox "><span class="onlineHoverRegion">'+city;
		app+='</span><span class="onlineHoverCompany">'+pos+'</span>';
        app+='<span class="onlineHoverCompany">'+phone+'</span>';
        app+='<span class="onlineHoverCompany">'+email+'</span></div></p>';
		//alert(app);
		app+='<div class="cleaner"></div>';
		$("#ch_list").prepend(app);
		online_hover_init();
		if (o_ac_ch==0){
			o_readChannel(id);
		}
	}
}

function online_bl(id){
     $.ajax({
	 type: "POST",
	 url: "/online/",
	 datatype: "html",
	 data: {'black_list':1, 'id':id},
	 success: function(data){
		    $("#channel_"+id).remove();
		   // alert(data);
		    //$("#ajax_res").html(data);
	         }
	 });
																							     
}


function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function showBan(){
	//setCookie("showed", 1);
	//alert('1');
	if(window.location.pathname != '/online2/' && !document.referrer){
		setTimeout(function(){
			$("#online_banner").show();
		}, 10000);
	}
}

function init_type(type){
	 $.ajax({
		 type: "POST",
		 url: "/online/",
		 datatype: "html",
		 data: {'init_type': type},
		 success: function(data){
			//	alert(data);  
	      }
	 });

}

function o_example(w){
	var val = $(w).val();
//	alert(val);
	$("#area_reply").val(val);
}

var o_load = 0;

function o_end(){
	 end_chat = 1;
	 o_load = 1;
	 $.ajax({
		 type: "POST",
		 url: "/online/?die=1",
		 datatype: "html",
		 data: {'die':1},
		 success: function(data){
				$("#f_reply").hide();
				$("#chat_window").html($("#poll").html());
		}
	 });
}

function o_load_window(){
	 o_load = 0;
	 $.ajax({
		 type: "POST",
		 url: "/online/",
		 datatype: "html",
		 data: {'load_window':1},
		 success: function(data){
			//alert(data);
			// $("#main_window").html('1');
			 //$("#main_window").html(data);
			 //init_select();
			 //window.close();
	      }
	 });
}


function o_poll(){
	var str = $("#poll_form").serialize()+'&poll=1';
	//alert(str);
	$.ajax({
		 type: "POST",
		 url: "/online/",
		 datatype: "html",
		 data: str,
		 success: function(data){
				$("#chat_window").html("<h1>Спасибо, ваше мнение отправлено!</h1>");
				setTimeout(function(){
					window.close();
				}, 3000);
	      }
	 });
}


//////

//////
