// JavaScript Document
var perks_add_table = [0,8,7,7,6,6,5,5,5,4,4,4,4,4,3,3,3,3,3,3,3,2,2,2,2,2];
var pers_free_stats;
var stats_pers = [0,0,0,0,0,0,0];
var stats_vesh = [0,0,0,0,0,0,0];
var stats_addd = [0,0,0,0,0,0,0];
var vk = '';
var perks_fight_free = 0;
var perks_second_free = 0;
var perks_magic_free = 0;
var perks_fight_is = [0,0,0,0,0,0,0,0];
var perks_fight_ad = [0,0,0,0,0,0,0,0];
var perks_second_is = [0,0,0,0,0];
var perks_second_ad = [0,0,0,0,0];
var perks_resist_is = [0,0,0];
var perks_resist_ad = [0,0,0];
var perks_magic_is = [0,0,0,0,0];
var perks_magic_ad = [0,0,0,0,0];

function check_slot (slot,info) {
	if (slot=='weapon1') {
		if (info[2]=='') { s = '<img src="/images/slots/pob3.gif" width=62 height=91 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=91 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='weapon2') {
		if (info[2]=='') { s = '<img src="/images/slots/pob11.gif" width=62 height=91 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=91 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='ring1' || slot=='ring2') {
		if (info[2]=='') { s = '<img src="/images/slots/pob12.gif" width=31 height=31 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=31 height=31 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='amulet') {
		if (info[2]=='') { s = '<img src="/images/slots/pob2.gif" width=62 height=35 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=35 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='helmet') {
		if (info[2]=='') { s = '<img src="/images/slots/pob1.gif" width=62 height=65 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=65 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='body') {
		if (info[2]=='') { s = '<img src="/images/slots/pob14.gif" width=62 height=90 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=90 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='belt') {
		if (info[2]=='') { s = '<img src="/images/slots/pob4.gif" width=62 height=30 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=30 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='arms') {
		if (info[2]=='') { s = '<img src="/images/slots/pob9.gif" width=62 height=40 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=40 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='gloves') {
		if (info[2]=='') { s = '<img src="/images/slots/pob10.gif" width=62 height=40 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=40 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	else if (slot=='foots') {
		if (info[2]=='') { s = '<img src="/images/slots/pob8.gif" width=62 height=60 border="0" />' }
		else { s = '<img src="/images/weapons/'+info[3]+'.png" width=62 height=60 border=0 style="cursor:pointer" onclick="dress_off('+info[1]+');">' }
	}
	return s;
}

function character (info) {
	out =   '<table cellpadding="0" cellspacing="0" border="0"><tr><td>' +
            '<table border="0" cellpadding="0" cellspacing="0">' +
            '<tr><td><img src="/images/slots/slot1.gif" border="0" /></td></tr>' +
            '<tr><td>'+check_slot('arms',info[8])+'</td></tr>' +
            '<tr><td>'+check_slot('gloves',info[9])+'</td></tr>' +
            '<tr><td>'+check_slot('weapon1',info[0])+'</td></tr>' +
            '<tr><td>'+check_slot('body',info[6])+'</td></tr>' +
            '<tr><td><img src="/images/slots/pob15.gif" border="0" /><img src="/images/slots/pob15.gif" border="0" /></td></tr>' +
            '</table></td><td valign="bottom" align=center>' +
            '<table border="0" cellpadding="0" cellspacing="0" width=115><tr height="280"><td valign="middle" align=center style="padding:2px;">' +
            '<img src="/images/obrazy/human_mage_male.png" border="0" />' +
            '</td></tr><tr height="31"><td align="center">'+check_slot('ring1',info[2])+''+check_slot('ring2',info[3])+'</td></tr>' +
            '</table></td><td>' +
            '<table border="0" cellpadding="0" cellspacing="0">' +
            '<tr><td>'+check_slot('helmet',info[5])+'</td></tr>' +
            '<tr><td>'+check_slot('amulet',info[4])+'</td></tr>' +
            '<tr><td>'+check_slot('weapon2',info[1])+'</td></tr>' +
            '<tr><td>'+check_slot('belt',info[7])+'</td></tr>' +
            '<tr><td>'+check_slot('foots',info[10])+'</td></tr>' +
            '<tr><td><img src="/images/slots/pob15.gif" border="0" /><img src="/images/slots/pob15.gif" border="0" /></td></tr>' +
            '</table></td></tr></table>';
	d('character').innerHTML = out;
}

function pers_info(info) {
	pers_free_stats = parseInt(info[26]);
	pers = [pers[0],parseInt(info[0])+parseInt(info[1]),parseInt(info[2])+parseInt(info[3]),parseInt(info[4])+parseInt(info[5]),parseInt(info[6])+parseInt(info[7]),parseInt(info[8])+parseInt(info[9]),parseInt(info[10])+parseInt(info[11])];
	out = '<table border="0" cellspacing="0" cellpadding="0" width=100%>'+
	'<tr><td>Деньги:</td><td align=right>'+info[28]+' LM</td></tr>' +
	'<tr><td colspan="2"><hr color="#333333" width="100%" size="1"></td></tr>' +
	'<tr><td colspan=2><table border=0 cellpadding=0 cellspacing=0 width=100%>' +
    '<tr><td>'+TH[0]+'</td><td align=right id="stat_1">'+(parseInt(info[0])+parseInt(info[1]))+' ('+info[0]+'+'+info[1]+')</td>'+check_avail_stats(1)+'</tr>' +
    '<tr><td>'+TH[1]+'</td><td align=right id="stat_2">'+(parseInt(info[2])+parseInt(info[3]))+' ('+info[2]+'+'+info[3]+')</td>'+check_avail_stats(2)+'</tr>' +
    '<tr><td>'+TH[2]+'</td><td align=right id="stat_3">'+(parseInt(info[4])+parseInt(info[5]))+' ('+info[4]+'+'+info[5]+')</td>'+check_avail_stats(3)+'</tr>' +
    '<tr><td>'+TH[3]+'</td><td align=right id="stat_4">'+(parseInt(info[6])+parseInt(info[7]))+' ('+info[6]+'+'+info[7]+')</td>'+check_avail_stats(4)+'</tr>' +
    '<tr><td>'+TH[4]+'</td><td align=right id="stat_5">'+(parseInt(info[8])+parseInt(info[9]))+' ('+info[8]+'+'+info[9]+')</td>'+check_avail_stats(5)+'</tr>' +
    '<tr><td>'+TH[5]+'</td><td align=right id="stat_6">'+(parseInt(info[10])+parseInt(info[11]))+' ('+info[10]+'+'+info[11]+')</td>'+check_avail_stats(6)+'</tr>';
	if (parseInt(info[26])>0) { out = out + '<tr><td colspan=3 align=center id="free_stats"><b>Свободно:</b> '+info[26]+'</td></tr><tr><td colspan=3 align=center><b><a href="javascript:void(0);" onclick="save_stats();">Сохранить</a></td></tr>'}
    out = out + '</td></tr></table></td></tr><tr><td colspan="2"><hr color="#333333" width="100%" size="1"></td></tr>' +
    '<tr><td>'+TH[6]+'</td><td align=right>'+info[18]+'</td></tr>' +
    '<tr><td>'+TH[7]+'</td><td align=right>'+info[19]+'</td></tr>' +
    '<tr><td>'+TH[8]+'</td><td align=right>'+info[12]+'%</td></tr>' +
    '<tr><td>'+TH[9]+'</td><td align=right>'+info[13]+'%</td></tr>' +
    '<tr><td>'+TH[10]+'</td><td align=right>'+info[14]+'%</td></tr>' +
    '<tr><td>'+TH[11]+'</td><td align=right>'+info[15]+'%</td></tr>' +
    '<tr><td>'+TH[12]+'</td><td align=right>'+info[16]+'%</td></tr>' +
    '<tr><td>'+TH[13]+'</td><td align=right>'+info[17]+'%</td></tr>' +
    '<tr><td colspan="2"><hr color="#333333" width="100%" size="1"></td></tr>' +
    '<tr><td>Боевой</td><td align=right>'+info[20]+'</td></tr>' +
    '<tr><td>Магический</td><td align=right>'+info[21]+'</td></tr>' +
    '<tr><td>Всего</td><td align=right>'+(parseInt(info[20])+parseInt(info[21]))+'</td></tr>' +
    '<tr><td>До уровня</td><td align=right>'+info[29]+'</td></tr>' +
    '<tr><td colspan="2"><hr color="#333333" width="100%" size="1"></td></tr>' +
    '<tr><td>Побед PVP</td><td align=right>'+info[22]+'</td></tr>' +
    '<tr><td>Поражений PVP</td><td align=right>'+info[23]+'</td></tr>' +
    '<tr><td>Побед PVN</td><td align=right>'+info[24]+'</td></tr>' +
    '<tr><td>Поражений PVN</td><td align=right>'+info[25]+'</td></tr>' +
    '</table>';
	stats_pers = [0,info[0],info[2],info[4],info[6],info[8],info[10]];
	stats_vesh = [0,info[1],info[3],info[5],info[7],info[9],info[11]];
	d('pers_info').innerHTML = out;
}

function check_avail_stats (info) {
	if (pers_free_stats>0) {
		out = '<td style="padding-left:5px;" width=30>[<a href="javascript:void(0)" onclick="add_stat('+info+');">+</a> <a href="javascript:void(0)" onclick="remove_stat('+info+');">-</a>]</td>';
	}
	else{
		out = '';
	}
	return out;
}

function add_stat(info) {
	if (pers_free_stats>0) {
		pers_free_stats = pers_free_stats - 1;
		stats_addd[info] = stats_addd[info] + 1;
		d('stat_'+info).innerHTML = (parseInt(stats_pers[info])+parseInt(stats_vesh[info])+stats_addd[info])+' ('+(parseInt(stats_pers[info]) + parseInt(stats_addd[info]))+'+'+stats_vesh[info]+')';
	}
	d('free_stats').innerHTML = '<b>Свободно:</b> '+pers_free_stats;
}

function remove_stat(info) {
	if (stats_addd[info]>0) {
		stats_addd[info] = stats_addd[info] - 1;
		pers_free_stats = pers_free_stats + 1;
		d('stat_'+info).innerHTML = (parseInt(stats_pers[info])+parseInt(stats_vesh[info])+stats_addd[info])+' ('+(parseInt(stats_pers[info]) + parseInt(stats_addd[info]))+'+'+stats_vesh[info]+')';
	}
	d('free_stats').innerHTML = '<b>Свободно:</b> '+pers_free_stats;
}

function save_stats() {
	info = stats_addd[1]+'|'+stats_addd[2]+'|'+stats_addd[3]+'|'+stats_addd[4]+'|'+stats_addd[5]+'|'+stats_addd[6];
	query = 'info='+info+'&vcode='+vk;
	ajax_query ('save_stats',query,'pers_reload');
	stats_addd = [0,0,0,0,0,0,0];
}

function pers_reload (info) {
	info = info.split('{@}');
	info1 = info[0].split('|');
	info2 = info[1].split('|');
	pers_top([info1[0],info1[1],info1[2],info1[3],info1[4],info1[5],info1[6],info1[7],info1[8]],[],[info1[8]]);
	pers_info(info2);
}

function show_abilities() {
	ajax_query("show_abilities",'vcode='+vk,'render_abilities');
}

function render_abilities(info) {
	perks_fight_ad = [0,0,0,0,0,0,0,0];
	perks_second_ad = [0,0,0,0,0];
	perks_resist_ad = [0,0,0];
	info = info.split('@');
	f_info = info[0].split('|');
	s_info = info[1].split('|');
	r_info = info[2].split('|');
	m_info = info[3].split('|');
	p_info = info[4].split('|');
	free_info = info[5].split('|');
	perks_fight_is = f_info;
	perks_second_is = s_info;
	perks_resist_is = r_info;
	perks_magic_is = m_info;
	
	fperks = f_info;
	
	f_abils = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-size:11px;"><tr><th colspan=2>Боевые умения</th></tr>';
	for (a=14,b=22;a<b;a++) {
		f_abils += '<tr><td>'+TH[a]+'</td><td width="25" align=right id="f_perk_'+(a-14)+'">['+f_info[a-14]+'/100]</td>'+check_perk_add(free_info[0],(a-14),'f')+'</tr>';
	}
	f_abils = f_abils + '</table>';
	
	s_abils = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-size:11px;"><tr><th colspan=2>Второстепенные умения</th></tr>';
	for (a=30,b=35;a<b;a++) {
		s_abils += '<tr><td>'+TH[a]+'</td><td width="25" align=right id="s_perk_'+(a-30)+'">['+s_info[a-30]+'/100]</td>'+check_perk_add(free_info[1],(a-30),'s')+'</tr>';
	}
	s_abils = s_abils + '</table>';
	
	r_abils = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-size:11px;"><tr><th colspan=2>Сопротивления</th></tr>';
	for (a=22,b=25;a<b;a++) {
		r_abils += '<tr><td>'+TH[a]+'</td><td width="25" align=right id="r_perk_'+(a-22)+'">['+r_info[a-22]+'/100]</td>'+check_perk_add(free_info[0],(a-22),'r')+'</tr>';
	}
	r_abils = r_abils + '</table>';
	
	m_abils = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-size:11px;"><tr><th colspan=2>Магические умения</th></tr>';
	for (a=25,b=30;a<b;a++) {
		m_abils += '<tr><td>'+TH[a]+'</td><td width="25" align=right id="m_perk_'+(a-25)+'">['+m_info[a-25]+'/100]</td>'+check_perk_add(free_info[2],(a-25),'m')+'</tr>';
	}
	m_abils = m_abils + '</table>';
	
	p_abils = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-size:11px;"><tr><th colspan=2>Мирные умения</th></tr>';
	for (a=35,b=39;a<b;a++) {
		p_abils += '<tr><td>'+TH[a]+'</td><td width="25" align=right>['+p_info[a-35]+']</td></tr>';
	}
	p_abils = p_abils + '</table>';

	add = '';
	saveum = 0;
	perks_fight_free = free_info[0]; 
	perks_second_free = free_info[1];
	perks_magic_free = free_info[2];
	if (free_info[0]>0) { add = add + '<tr><td colspan=2 id="free_fight_perks">Свободные очки сопротивлений и боевых умений: <b>'+free_info[0]+'</b></td></tr>'; saveum = 1;}
	if (free_info[1]>0) { add = add + '<tr><td colspan=2 id="free_second_perks">Свободные очки второстепенных умений: <b>'+free_info[1]+'</b></td></tr>'; saveum = 1;}
	if (free_info[2]>0) { add = add + '<tr><td colspan=2 id="free_magic_perks">Свободные очки магических умений: <b>'+free_info[2]+'</b></td></tr>'; saveum = 1;}
	if (saveum==1) { add = add + '<tr><td colspan=2 align=center><b><a href="javascript:void();" onclick="save_perks();">Сохранить</a></b></td></tr>' }
	if (add!='') { add = '<tr><td colspan=2>&nbsp;</td></tr>' + add}
	out = '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="font-size:11px;"><tr><td valign=top style="padding-right:10px;">' + f_abils + s_abils + '</td><td valign=top style="padding-left:10px;">' + r_abils + m_abils + p_abils + '</td></tr>' + add + '</table>';
	d('pers_content').innerHTML = out;
	
	
}

function check_perk_add(info,num,type) {
	if (info>0) {
		out = '<td width="25" align=right><a href="javascript:void(0)" onclick="add_perk(\''+type+'\','+num+');">+</a> <a href="javascript:void(0)" onclick="remove_perk(\''+type+'\','+num+');">-</a></td>'
	}
	else { out = '' }
	return out;
}

function add_perk (type,num) {
	current_perks_is = [];
	current_perks_ad = [];
	if (type=='f') { current_perks_is = perks_fight_is; current_perks_ad = perks_fight_ad; free_perks = perks_fight_free;}
	else if (type=='s') { current_perks_is = perks_second_is; current_perks_ad = perks_second_ad; free_perks = perks_second_free; }
	else if (type=='r') { current_perks_is = perks_resist_is; current_perks_ad = perks_resist_ad; free_perks = perks_fight_free; }
	else if (type=='m') { current_perks_is = perks_magic_is; current_perks_ad = perks_magic_ad; free_perks = perks_magic_free; }
	
	if (parseInt(current_perks_is[num])+parseInt(current_perks_ad[num])<100 && free_perks>0) {
		free_perks = free_perks - 1;
		full_perk = (parseInt(current_perks_is[num])+parseInt(current_perks_ad[num]));
		if (full_perk<20) { add = 4 }
		else if (full_perk>=20 && full_perk<80) { add = 3 }
		else {add = 2 }
		current_perks_ad[num] = current_perks_ad[num] + add;
		d(type+"_perk_"+num).innerHTML = '['+(parseInt(current_perks_is[num])+parseInt(current_perks_ad[num]))+'/100]';
	}
	
	if (type=='f') { perks_fight_is = current_perks_is; perks_fight_ad = current_perks_ad; perks_fight_free = free_perks; d("free_fight_perks").innerHTML = 'Свободные очки сопротивлений и боевых умений: <b>'+free_perks+'</b>';}
	else if (type=='s') { perks_second_is = current_perks_is; perks_second_ad = current_perks_ad; perks_second_free = free_perks; d("free_second_perks").innerHTML = 'Свободные очки второстепенных умений: <b>'+free_perks+'</b>';}
	else if (type=='r') { perks_resist_is = current_perks_is; perks_resist_ad = current_perks_ad; perks_fight_free = free_perks; d("free_fight_perks").innerHTML = 'Свободные очки сопротивлений и боевых умений: <b>'+free_perks+'</b>';}
	else if (type=='m') { perks_magic_is = current_perks_is; perks_magic_ad = current_perks_ad; perks_magic_free = free_perks; d("free_magic_perks").innerHTML = 'Свободные очки магических умений: <b>'+free_perks+'</b>';}
	
}

function remove_perk (type,num) {
	current_perks_is = [];
	current_perks_ad = [];
	if (type=='f') { current_perks_is = perks_fight_is; current_perks_ad = perks_fight_ad; free_perks = perks_fight_free; }
	else if (type=='s') { current_perks_is = perks_second_is; current_perks_ad = perks_second_ad; free_perks = perks_second_free; }
	else if (type=='r') { current_perks_is = perks_resist_is; current_perks_ad = perks_resist_ad; free_perks = perks_fight_free; }
	else if (type=='m') { current_perks_is = perks_magic_is; current_perks_ad = perks_magic_ad; free_perks = perks_magic_free; }
	
	if (parseInt(current_perks_is[num])+parseInt(current_perks_ad[num])>0 && parseInt(current_perks_ad[num])>0) {
		free_perks = free_perks + 1;
		full_perk = (parseInt(current_perks_is[num])+parseInt(current_perks_ad[num]));
		if (full_perk<=20) { remove = 4 }
		else if (full_perk>20 && full_perk<=80) { remove = 3 }
		else {remove = 2 }
		current_perks_ad[num] = current_perks_ad[num] - remove;
		d(type+"_perk_"+num).innerHTML = '['+(parseInt(current_perks_is[num])+parseInt(current_perks_ad[num]))+'/100]';
		d("free_fight_perks").innerHTML = 'Свободные очки сопротивлений и боевых умений: <b>'+perks_fight_free+'</b>';
	}
	
	if (type=='f') { perks_fight_is = current_perks_is; perks_fight_ad = current_perks_ad; perks_fight_free = free_perks; d("free_fight_perks").innerHTML = 'Свободные очки сопротивлений и боевых умений: <b>'+free_perks+'</b>';}
	else if (type=='s') { perks_second_is = current_perks_is; perks_second_ad = current_perks_ad; perks_second_free = free_perks; d("free_second_perks").innerHTML = 'Свободные очки второстепенных умений: <b>'+free_perks+'</b>';}
	else if (type=='r') { perks_resist_is = current_perks_is; perks_resist_ad = current_perks_ad; perks_fight_free = free_perks; d("free_fight_perks").innerHTML = 'Свободные очки сопротивлений и боевых умений: <b>'+free_perks+'</b>';}
	else if (type=='m') { perks_magic_is = current_perks_is; perks_magic_ad = current_perks_ad; perks_magic_free = free_perks; d("free_magic_perks").innerHTML = 'Свободные очки магических умений: <b>'+free_perks+'</b>';}
}

function save_perks () {
	info = perks_fight_ad[0]+'|'+perks_fight_ad[1]+'|'+perks_fight_ad[2]+'|'+perks_fight_ad[3]+'|'+perks_fight_ad[4]+'|'+perks_fight_ad[5]+'|'+perks_fight_ad[6]+'|'+perks_fight_ad[7]+'|'+perks_second_ad[0]+'|'+perks_second_ad[1]+'|'+perks_second_ad[2]+'|'+perks_second_ad[3]+'|'+perks_second_ad[4]+'|'+perks_resist_ad[0]+'|'+perks_resist_ad[1]+'|'+perks_resist_ad[2]+'|'+perks_magic_ad[0]+'|'+perks_magic_ad[1]+'|'+perks_magic_ad[2]+'|'+perks_magic_ad[3]+'|'+perks_magic_ad[4];
	query = 'info='+info+'&vcode='+vk;
	ajax_query ('save_perks',query,'rerend_abilities');
	perks_fight_ad = [0,0,0,0,0,0,0,0];
	perks_second_ad = [0,0,0,0,0];
	perks_resist_ad = [0,0,0];
	perks_magic_ad = [0,0,0,0,0];
}

function rerend_abilities (info) {
	show_abilities();
}

function show_aboutme() {
	ajax_query("show_aboutme",'vcode='+vk,'render_aboutme');
}

function render_aboutme(info) {
	info = info.split('{@}');
	about = info[0].split('|');
	vk = info[1];
	out = '<font style="font-size:11px;">Имя: <b>'+about[0]+'</b><br>';
	out = out + 'Страна: <b>'+about[1]+'</b><br>';
	out = out + 'Город: <b>'+about[2]+'</b><br>';
	out = out + 'E-mail: <b>'+about[3]+'</b><br>';
	out = out + 'Дата рождения: <b>'+about[4].replace(':','.')+'</b></font>';
	d('pers_content').innerHTML = out;
}