/*=========================================*\
|			  Army System 2.2				|
|-------------------------------------------|
|	   A close copy of Kings of Chaos		|
|   No code is copied from Kings of Chaos   |
|-------------------------------------------|
|		 Made by supersmashbrothers			|
|     Copyright removal is not allowed      |
|-------------------------------------------|
| 			   Support sites:				|
|		http://forums.invisionize.com		|
|	  http://supersmashbrothers.2ya.com		|
|-------------------------------------------|
| 				Kings of Chaos 				|
|		 http://www.kingsofchaos.com/ 		|
\*=========================================*/

function form_change_icon(form, field)
{
	var _icon_url = icon_url + '/' + eval("my_getbyid(form)."+field+".value;");
	document.images[eval("'"+field+"_preview'")].src = _icon_url;
}

function form_check_all(form_name,prefix,field)
{
	for(var i=0; i<my_getbyid(form_name).elements.length; i++) {
		var elemm = my_getbyid(form_name).elements[i];
		if(elemm.type == 'checkbox' && elemm.name.substring(0,prefix.length) == prefix)
		{
			elemm.checked = eval("my_getbyid(form_name)."+field+".checked;");
		}
	}
}

function acp_weapon_add_disable(form,suffix,suffix2)
{
	var section;
	var total = eval("my_getbyid(form).section"+suffix+suffix2+".length;");
	for(var i=0; i<total; i++)
	{
		var elemm = eval("my_getbyid(form).section"+suffix+suffix2+"[i];");
		if(elemm.selected != false)
		{
			section = elemm.value;
		}
	}
	if(section == 'q' || section == 'e')
	{
		eval("my_getbyid(form).repair"+suffix+".disabled = true;");
	}
	else
	{
		eval("my_getbyid(form).repair"+suffix+".disabled = false;");
	}
}

function acp_weapon_add_select_all(suffix)
{
	var total = eval("my_getbyid('races"+suffix+"[]').length;");
	var all = eval("my_getbyid('races"+suffix+"[]').options[0];");
	for(var i=0; i<total; i++)
	{
		var elemm = eval("my_getbyid('races"+suffix+"[]').options[i];");
		if(elemm.value == 'a')
		{
			continue;
		}
		if(all.selected == true || all.selected == "selected")
		{
			elemm.selected = all.selected;
		}
	}
}

function form_if_any_checked(form_name,prefix)
{
	for(var i=0; i<my_getbyid(form_name).elements.length; i++) {
		var elemm = my_getbyid(form_name).elements[i];
		if(elemm.type == 'checkbox' && elemm.name.substring(0,prefix.length) == prefix && elemm.checked)
		{
			return true;
		}
	}
	return false;
}

function acp_upgrade_add_disable(form,suffix,suffix2)
{
	var section;
	var total = eval("my_getbyid(form).section"+suffix+suffix2+".length;");
	for(var i=0; i<total; i++)
	{
		var elemm = eval("my_getbyid(form).section"+suffix+suffix2+"[i];");
		if(elemm.selected != false)
		{
			section = elemm.value;
		}
	}
	if(section == 's' || section == 'f')
	{
		eval("my_getbyid(form).name"+suffix+".disabled = false;");
		eval("my_getbyid(form).icon"+suffix+".disabled = false;");
	}
	else
	{
		eval("my_getbyid(form).name"+suffix+".disabled = true;");
		eval("my_getbyid(form).icon"+suffix+".disabled = true;");
	}
}

function delete_upgrade(theURL)
{
	if (confirm( 'Are you sure you want to delete this upgrade?' ))
	{
		window.location.href=theURL;
	}
	else
	{
		alert ( 'The upgrade was not deleted' );
	} 
}

//-----------------------------------
// This is a fix for multipage fix for different std
//-----------------------------------
function do_multi_page_jump_as( st_name , pages_id )
{
	var pages       = 1;
	var cur_st      = ipb_var_st;
	var cur_page    = 1;
	var total_posts = ipb_pages_array[ pages_id ][2];
	var per_page    = ipb_pages_array[ pages_id ][1];
	var url_bit     = ipb_pages_array[ pages_id ][0];
	var userPage    = parseInt( document.getElementById( 'st-'+pages_id ).value );
	
	//-----------------------------------
	// Fix up URL BIT
	//-----------------------------------
	
	url_bit = url_bit.replace( new RegExp( "&amp;", "g" ) , '&' );
	
	//-----------------------------------
	// Work out pages
	//-----------------------------------
	
	if ( total_posts % per_page == 0 )
	{
		pages = total_posts / per_page;
	}
	else
	{
		pages = Math.ceil( total_posts / per_page );
	}
	
	if ( cur_st > 0 )
	{
		cur_page = cur_st / per_page; cur_page = cur_page -1;
	}

	if ( userPage > 0  )
	{
		if ( userPage < 1 )     {    userPage = 1;  }
		if ( userPage > pages ) { userPage = pages; }
		if ( userPage == 1 )    {     start = 0;    }
		else { start = (userPage - 1) * per_page; }
	
		window.location = url_bit + "&"+st_name+"=" + start;
	}
}

var as2_main;
var as2_loaded = 0;

function xml_popup_init( tab )
{
	as2_main = document.getElementById( 'get-as2-pop' );
	as2_drag = document.getElementById( 'as2-drag' );
	
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			// Could do a little loading graphic here?
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		//----------------------------------
		// Stop IE showing select boxes over
		// floating div [ 1 ]
		//----------------------------------
		
		if ( is_ie )
		{
			 html = "<iframe id='as2-shim' src='javascript:;' class='iframshim' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; display:none;'></iframe>" + html;
		}
		
		as2_content           = document.getElementById('as2-content');
		as2_content.innerHTML = html;
		
		//----------------------------------
		// Stop IE showing select boxes over
		// floating div [ 2 ]
		//----------------------------------
		
		if ( is_ie )
		{
			as2_shim               = document.getElementById('as2-shim');
			as2_shim.style.width   = as2_content.offsetWidth;
			as2_shim.style.height  = as2_content.offsetHeight;
			as2_shim.style.zIndex  = as2_content.style.zIndex - 1;
			as2_shim.style.top     = as2_content.style.top;
			as2_shim.style.left    = as2_content.style.left;
			as2_shim.style.display = "block";
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	if ( ! tab )
	{
		xmlobj.process( ipb_var_base_url + 'autocom=army&core=armysystem2&CODE=xmlbox' );
	}
	else
	{
		xmlobj.process( ipb_var_base_url + 'autocom=army&core=armysystem2&CODE=xmlbox&tab=' + tab );
	}
  	
  	as2_main.style.position = 'absolute';
	as2_main.style.display  = 'block';
	as2_main.style.zIndex   = 99;
	
	//----------------------------------
	// Not loaded? INIT
	//----------------------------------
	
	if ( ! as2_loaded )
	{
		//----------------------------------
		// Figure width and height
		//----------------------------------
		
		var my_width  = 0;
		var my_height = 0;
		
		if ( typeof( window.innerWidth ) == 'number' )
		{
			//----------------------------------
			// Non IE
			//----------------------------------
		  
			my_width  = window.innerWidth;
			my_height = window.innerHeight;
		}
		else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
		{
			//----------------------------------
			// IE 6+
			//----------------------------------
			
			my_width  = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
		{
			//----------------------------------
			// Old IE
			//----------------------------------
			
			my_width  = document.body.clientWidth;
			my_height = document.body.clientHeight;
		}
		
		//----------------------------------
		// Get div height && width
		//----------------------------------
		
		var divheight = parseInt( as2_main.style.Height );
		var divwidth  = parseInt( as2_main.style.Width );
		
		divheight = divheight ? divheight : 400;
		divwidth  = divwidth  ? divwidth  : 400;
		
		//----------------------------------
		// Got it stored in a cookie?
		//----------------------------------
		
		var divxy = my_getcookie( 'army-as2-div' );
		var co_ords;
		
		if ( divxy && divxy != null )
		{
			co_ords = divxy.split( ',' );
		
			//----------------------------------
			// Got co-ords?
			//----------------------------------
			
			if ( co_ords.length )
			{
				var final_width  = co_ords[0];
				var final_height = co_ords[1];
				
				if ( co_ords[0] > my_width )
				{
					//----------------------------------
					// Keep it on screen
					//----------------------------------
					
					final_width = my_width - divwidth;
				}
				
				if ( co_ords[1] > my_height )
				{
					//----------------------------------
					// Keep it on screen
					//----------------------------------
					
					final_height = my_height - divheight;
				}
				
				as2_main.style.left = final_width  + 'px';
				as2_main.style.top  = final_height + 'px';
			}
		}
		else
		{
			//----------------------------------
			// Reposition DIV roughly centered
			//----------------------------------
			
			as2_main.style.left = my_width  / 2  - (divwidth / 2)  + 'px';
			as2_main.style.top  = my_height / 2 - (divheight / 2 ) + 'px';
		}
		
		Drag.cookiename = 'army-as2-div';
		Drag.init( as2_drag, as2_main );
		
		as2_loaded = 1;
	}
}

function check_member()
{
	var edit_field = document.getElementById( 'edit_member_fields' );
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			// Could do a little loading graphic here?
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		edit_field.style.display = "";
		edit_field.style.width = "100%";
		edit_field.style.height = "auto";
		edit_field.innerHTML = html;
		
		form_change_icon_member_race();
	}
	
	if ( document.memberlookup.member_name.value )
	{
		xmlobj = new ajax_request();
		xmlobj.onreadystatechange( do_request_function );
		
		xmlobj.process( ipb_var_base_url + '&section=components&act=army&code=armysystem2&ACT=member&CODE=xml_edit&member_name=' + document.memberlookup.member_name.value );
	}
	else
	{
		edit_field.style.height = 0;
		edit_field.style.width = 0;
		edit_field.innerHTML = '';
	}
  	
	return false;
}

function form_change_icon_member(field)
{
	var _icon_url = icon_url + '/' + icons[field][eval("my_getbyid('theAdminForm')."+field+".value")];
	document.images[eval("'"+field+"_preview'")].src = _icon_url;
}

function form_change_icon_member_race()
{
	if(document.images['race_icon'] == null)
	{
		return false;
	}
	var race_id = my_getbyid('theAdminForm').army2_race.value;
	if(race_id < 0) race_id = 0;
	document.images['race_icon'].src = icon_url + '/' + icons['r'][race_id]['default'];
	document.images['race_attack_icon'].src = icon_url + '/' + icons['r'][race_id]['attack'];
	document.images['race_mattack_icon'].src = icon_url + '/' + icons['r'][race_id]['mattack'];
	document.images['race_defence_icon'].src = icon_url + '/' + icons['r'][race_id]['defence'];
	document.images['race_mdefence_icon'].src = icon_url + '/' + icons['r'][race_id]['mdefence'];
	document.images['race_soldier_icon'].src = icon_url + '/' + icons['r'][race_id]['default'];
	document.images['race_merc_icon'].src = icon_url + '/' + icons['r'][race_id]['msoldier'];
	document.images['race_spy_icon'].src = icon_url + '/' + icons['r'][race_id]['spy'];
	document.images['race_sentry_icon'].src = icon_url + '/' + icons['r'][race_id]['sentry'];
	
	var weapons = new Array('a','d','q','e');
	if(race_id == 0)
	{
		my_show_div(my_getbyid('armor_0'));
		my_getbyid('theAdminForm').army2_fort.disabled = true;
		my_getbyid('theAdminForm').army2_siege.disabled = true;
		my_getbyid('theAdminForm').army2_prod.disabled = true;
		my_getbyid('theAdminForm').army2_covertlvl.disabled = true;
		my_getbyid('theAdminForm').army2_turns.disabled = true;
		my_getbyid('theAdminForm').army2_attack.disabled = true;
		my_getbyid('theAdminForm').army2_mattack.disabled = true;
		my_getbyid('theAdminForm').army2_defence.disabled = true;
		my_getbyid('theAdminForm').army2_mdefence.disabled = true;
		my_getbyid('theAdminForm').army2_soldier.disabled = true;
		my_getbyid('theAdminForm').army2_msoldier.disabled = true;
		my_getbyid('theAdminForm').army2_spies.disabled = true;
		my_getbyid('theAdminForm').army2_sentry.disabled = true;
		for(i = 0; i < weapons.length; i++)
		{
			for(j = 1; j < armory[1][weapons[i]].length; j++)
			{
				my_hide_div(my_getbyid('armor_'+weapons[i]+'_'+j));
			}
		}
	}
	else
	{
		my_getbyid('theAdminForm').army2_fort.disabled = false;
		my_getbyid('theAdminForm').army2_siege.disabled = false;
		my_getbyid('theAdminForm').army2_prod.disabled = false;
		my_getbyid('theAdminForm').army2_covertlvl.disabled = false;
		my_getbyid('theAdminForm').army2_turns.disabled = false;
		my_getbyid('theAdminForm').army2_attack.disabled = false;
		my_getbyid('theAdminForm').army2_mattack.disabled = false;
		my_getbyid('theAdminForm').army2_defence.disabled = false;
		my_getbyid('theAdminForm').army2_mdefence.disabled = false;
		my_getbyid('theAdminForm').army2_soldier.disabled = false;
		my_getbyid('theAdminForm').army2_msoldier.disabled = false;
		my_getbyid('theAdminForm').army2_spies.disabled = false;
		my_getbyid('theAdminForm').army2_sentry.disabled = false;
		var show_none = true;
		for(i = 0; i < weapons.length; i++)
		{
			for(j = 1; j < armory[race_id][weapons[i]].length; j++)
			{
				if(armory[race_id][weapons[i]][j])
				{
					show_none = false;
					my_show_div(my_getbyid('armor_'+weapons[i]+'_'+j));
				}
				else
				{
					my_hide_div(my_getbyid('armor_'+weapons[i]+'_'+j));
				}
			}
		}
		if(show_none)
		{
			my_show_div(my_getbyid('armor_0'));
		}
		else
		{
			my_hide_div(my_getbyid('armor_0'));
		}
	}
}

function init_resetter()
{
	my_getbyid('theAdminForm').race_set_to.disabled = true;
	my_getbyid('theAdminForm').race_non.disabled = true;
	my_getbyid('theAdminForm').money_advance.disabled = true;
	my_getbyid('theAdminForm').money_set_to.disabled = true;
	my_getbyid('theAdminForm').turn_set_to.disabled = true;
	my_getbyid('theAdminForm').army_set_to.disabled = true;
	my_getbyid('theAdminForm').atk_sold_set_to.disabled = true;
	my_getbyid('theAdminForm').def_sold_set_to.disabled = true;
	my_getbyid('theAdminForm').merc_set_to.disabled = true;
	my_getbyid('theAdminForm').atk_merc_set_to.disabled = true;
	my_getbyid('theAdminForm').def_merc_set_to.disabled = true;
	my_getbyid('theAdminForm').spy_set_to.disabled = true;
	my_getbyid('theAdminForm').sentry_set_to.disabled = true;
}

function money_recount_click()
{
	var check = my_getbyid('theAdminForm').money_recount.checked;
	my_getbyid('theAdminForm').money_set.checked = false;
	my_getbyid('theAdminForm').money_set.disabled = check;
	my_getbyid('theAdminForm').money_advance.disabled = !check;
}

function money_set_click()
{
	var check = my_getbyid('theAdminForm').money_set.checked;
	my_getbyid('theAdminForm').money_recount.checked = false;
	my_getbyid('theAdminForm').money_recount.disabled = check;
	my_getbyid('theAdminForm').money_set_to.disabled = !check;
}
function army_click()
{
	var check1 = my_getbyid('theAdminForm').army_recount.checked;
	var check2 = my_getbyid('theAdminForm').army_set.checked;
	if(check1 && !check2)
	{
		my_getbyid('theAdminForm').army_recount.disabled = false;
		my_getbyid('theAdminForm').army_set.disabled = true;
	}
	else if(!check1 && check2)
	{
		my_getbyid('theAdminForm').army_recount.disabled = true;
		my_getbyid('theAdminForm').army_set.disabled = false;
	}
	else
	{
		my_getbyid('theAdminForm').army_recount.disabled = false;
		my_getbyid('theAdminForm').army_set.disabled = false;
		check1 = check2 = false;
	}
	my_getbyid('theAdminForm').army_set_to.disabled = !check2;
}

function change_group()
{
	var edit_field = document.getElementById( 'edit_group_fields' );
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			// Could do a little loading graphic here?
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		edit_field.style.width = "100%";
		edit_field.style.hieght = "auto";
		edit_field.innerHTML = html;
	}
	
	if ( my_getbyid('group_id').value )
	{
		xmlobj = new ajax_request();
		xmlobj.onreadystatechange( do_request_function );
		
		xmlobj.process( ipb_var_base_url + '&section=components&act=army&code=armysystem2&ACT=group&CODE=edit&group_id=' + my_getbyid('group_id').value );
	}
	else
	{
		edit_field.style.height = 0;
		edit_field.style.width = 0;
		edit_field.innerHTML = '';
	}
  	
	return false;
}

function form_icon_dir(dir)
{
	for(var i=0; i<my_getbyid('theAdminForm').elements.length; i++)
	{
		var elemm = my_getbyid('theAdminForm').elements[i];
		if(elemm.type == 'checkbox' && elemm.name.substring(0,dir.length) == dir && elemm.name.substring(dir.length).indexOf("/") == -1)
		{
			elemm.checked = my_getbyid(dir).checked;
		}
	}
}


