onReady(function() {
	if (gebi('sort_type')) doUISort();
	if (selRoomId != '') selectRoom(selRoomId);
});





var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime()+(expDays*24*60*60*1000));

function chek_nick(){
   if(document.form_reg){
   if( (document.form_reg.Login) && (document.form_reg.Login.value == "") ) {window.alert("Укажите email");return false;}
   if( (document.form_reg.Password) && (document.form_reg.Password.value == "") ) {window.alert("Укажите пароль");return false;}
   if( (document.form_reg.nick) && (document.form_reg.nick.value == "") ) {window.alert("Укажите имя в чате");return false;}
   }
   return true;
}

function goToLeader(room, leader) {
	if(!chek_nick()) return false; 
	var exp=new Date();
	exp.setTime(exp.getTime()+5*60);
	SetCookie('request_priv_to', leader, exp);
	setChannel(nickname, room);
}

function enterRoom(event,obj) {
	event = event ? event : window.event;
	var trg = event.srcElement || event.target;
	var id = trg.value;

	document.form_reg.room.value = id;
	if (valid()) document.form_reg.submit();
}

/*<!-- IFN OT Room Section -->*/
/* form for sc() */
function setChannel(nick, room) {
	return sc(room);
}

function sc(id) {
	var f = document.form_reg ? document.form_reg : document.form_for_sc;
	if (f.nick.value=="") {f.nick.value = GetCookie('nick')}

	if (chat_vars.clhost != '') {
		var server = id;
		var colon = server.indexOf(':');
		if (colon != -1) {
			id = server.substr(colon+1);
			server = server.substr(0,colon);
			if (chat_vars.AU) f.action = 'http://' + server + '.' + chat_vars.clhost + '/';
			else f.page.value = 'http://' + server + '.' + chat_vars.clhost + '/';
		}
	}
	if (document.form_reg) {
		f.roomleft.value = id;
		saveCookie();
	} else {
		f.room.value = id;
	}
	f.submit();
}
/*<!-- /IFN OT Room Section -->*/


function SetCookie(name,value) {  
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : "/";
	var domain = (argc > 4) ? argv[4] : (chat_vars.clhost != '' ? chat_vars.clhost : null);
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escapeNew (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}



function getCookieVal(offset) {  
 var endstr=document.cookie.indexOf (";",offset);  
 if(endstr==-1) endstr=document.cookie.length;  
 return unescapeNew(document.cookie.substring(offset,endstr));
}

/*function GetCookie(name) {  
 var arg=name+"=";  
 var alen=arg.length;  
 var clen=document.cookie.length;  
 var i=0;  
 while(i<clen) {
  var j = i + alen;
  if(document.cookie.substring(i,j)==arg) return getCookieVal(j);    
  i=document.cookie.indexOf(" ",i)+1;    
  if(i==0) break;   
 }  
 return null;
}*/

function load() {
	var f = document.form_reg;
	var url=new String(document.location);
	if (!chat_vars.AU) {
		if(GetCookie('Login')!=null) { 
			f.Login.value = GetCookie('Login'); 
		}
		if(GetCookie('Domain')!=null) {
			var c=GetCookie('Domain');
			for(var i=0; i<f.Domain.length; i++) {
				if(f.Domain.options[i].value == c) {
					f.Domain.selectedIndex = i; 
					break;
				}
			}
		}
	}

	if (url.indexOf("?id=") != -1) {
		var i = url.indexOf("?id=")+4;
		var id = parseInt(url.substr(i));
		var loop=0;
		for(loop=0; loop < f.room.length; loop++) {
			if(f.room.options[loop].value == "-") { break; }
		}
		if((id+loop) <= f.room.length) { SetCookie('room',f.room.options[id+loop].value,exp); }
	}
	if(GetCookie('nick')!=null) { 
		f.nick.value = GetCookie('nick'); 
	}
	if(GetCookie('nickcolor')!=null) {
		var c=GetCookie('nickcolor');
		setColorForChat(c, 'chat_nick');
	}
	if(GetCookie('room')!=null) {
		var c=GetCookie('room');
		var loop=0;
		for(loop=0; loop < f.room.length; loop++) {
			if (chat_vars.clhost != '')
			{
				var idstr = f.room.options[loop].value;
				var colon = idstr.indexOf(':');
				if (colon != -1) idstr = idstr.substr(colon+1);
				if (idstr == c) {
					f.room.selectedIndex=loop;
					break;
				}
			} else {
				if(f.room.options[loop].value == c) {
					f.room.selectedIndex=loop;
					break;
				}
			}
		}
	} else if(document.forms.form_reg.room) {
		document.forms.form_reg.room.value = '1:1';
	}

	if(GetCookie('silent')!=null) {
		var c=GetCookie('silent');
		f.silent_ch.checked=(c == '1') ? true : false; 
	}
	if(GetCookie('signore')!=null) {
		var c=GetCookie('signore');
		f.signore_ch.checked=(c == '1') ? true : false; 
	}

	if (!chat_vars.AU) {
		if(GetCookie('Login')!=null) document.form_reg.Password.focus();
		else document.form_reg.Login.focus();
	} else document.form_reg.nick.focus();
}

function form2form() {
 var f = document.form_reg;	
 var f2 = document.leftup;
 if (f2.channel.value == '') {alert("Введите название комнаты.\nНазвание комнаты должно содержать от 1 до 32 символов."); f2.channel.focus(); return false;}
 f2.nickcolor.value=f.nickcolor.value;
 f2.signore.value=f.signore_ch.checked?1:0;
 f2.silent.value=f.silent_ch.checked?1:0;
 f2.nick.value=f.nick.value;
 return true;
}

function valid_random_room() {
   if (document.form_reg && document.form_reg.nick) {
	   var nick = document.form_reg.nick.value;
	   if (nick == "") {
	      alert("Укажите имя в чате");
		  return false;
	   } else {
	      SetCookie('nick',nick);
		  document.location = 'http://' + chat_vars.perlhost + '/cgi-bin/invite?ticket=' + ticket + '&r=1';
		  return true;
	   }
   }
}

function saveCookie() {
 var f = document.form_reg;

 SetCookie('nick',f.nick.value,exp);
 SetCookie('nickcolor',f.nickcolor.value,exp);

 if (f.roomleft.value!="") {
    SetCookie('room',f.roomleft.value, exp);
 } else
 if (f.room.selectedIndex > -1) {
    SetCookie('room',f.room.options[f.room.selectedIndex].value, exp);
 }
 SetCookie('silent',f.silent_ch.checked ? '1' : '0',exp);
 SetCookie('signore',f.signore_ch.checked ? '1' : '0',exp);    
}

function valid() {
	var f = document.form_reg;
	if (!chat_vars.AU) {
		if (f.Login.value == "") {
			window.alert("Не заполнено поле email");
			return false;
		}
		if (f.Password.value == "") {
			window.alert("Не заполнено поле пароль");
			return false;
		}
		SetCookie('Login',f.Login.value,exp);     
		SetCookie('Domain',f.Domain.options[f.Domain.selectedIndex].value,exp);    
	}
	if(f.nick.value == "" ) {
		window.alert("Укажите имя в чате");
		return false;
	}
	if (f.room.selectedIndex == -1) {
		window.alert("Не выбрана комната");
		return false;
	}
	f.silent.value = f.silent_ch.checked ? 1 : 0;
	f.signore.value = f.signore_ch.checked ? 1 : 0;
	if (chat_vars.clhost != '') {
		var server = f.room.options[f.room.selectedIndex].value;
		var colon = server.indexOf(':');
		if (colon != -1) {
			f.room.options[f.room.selectedIndex].value = server.substr(colon+1);
			server = server.substr(0,colon);
			if (chat_vars.AU) f.action = 'http://'+server+'.' + chat_vars.clhost + '/';
			else f.page.value = 'http://'+server+'.' + chat_vars.clhost + '/';
		}
	}

	if(f.room.options[f.room.selectedIndex].value==0){
		if (chat_vars.AU) {
			if (window.confirm('Вход в приватную комнату '+f.room.options[f.room.selectedIndex].text.substr(0,f.room.options[f.room.selectedIndex].text.length-4)+' возможен только по приглашению от её создателя или смотрителя.\nПопросить пригласить Вас в комнату?')) {
				var fr = document.form_privat;
				fr.channel.value = f.room.options[f.room.selectedIndex].text.substr(0,f.room.options[f.room.selectedIndex].text.length-4);
				fr.submit();
				alert('Приглашение отправлено.');
				return false;
			}
			else {return false;}
		} else {
				window.alert('Вам закрыт вход в приватную комнату '+f.room.options[f.room.selectedIndex].text.substr(0,f.room.options[f.room.selectedIndex].text.length-4) );return false;
		}
	}
	saveCookie();
	return true;
}

function loadRooms(obj) {
	gebi('room_list').innerHTML = "";
	sortRooms(obj);
	doUISort();
}

function sortRooms(rooms) {
	room_list = gebi('room_list');
	for (var i=0; i<rooms.length; i++) {
		var room = document.createElement('option');
		room.value = rooms[i].id;
		room.innerHTML = rooms[i].name+' ('+rooms[i].user_cnt+')';
		if (rooms[i].room_type == 'persistent') {
			room.className= 'persistent';
		} else if (rooms[i].room_type == 'usual') {
			room.className= 'usual';
		} else if (rooms[i].room_type == 'private') {
			room.className= 'private';
		} else if (rooms[i].room_type == 'vtrans') {
			//continue;
			if (rooms[i].bcast == 1) {
				room.className= 'bcast';
			} else {
				room.className= 'vtrans';
			}
		} else {
			room.className= 'default';
		}
		room_list.appendChild(room);
	}
}

var sort_type = 'activity';
if (GetCookie('rsm') == 'quantity') 
    sort_type = 'quantity';

function changeSort() {
   sort_type = (sort_type == 'quantity') ? 'activity' : 'quantity'
   SetCookie('rsm',sort_type);
}

function doUISort() {
col = gebi('sort_type').getElementsByTagName('div');
for (var i=0; i<col.length; i++) {
   if (col[i].id == sort_type) {
      col[i].className = 'act';
      col[i].onclick = "";
   } else {
      col[i].className = 'noact';
      col[i].onclick = function() {
	     changeSort();
		 ajax_call('gw_chat_room_list',loadRooms);
      }
   }
}
}

function selectRoom(selroomid) {
	selroomid = 'room_' + selroomid;
	if (gebi(selroomid)) gebi(selroomid).selected = true;
}


