/* ==========================
 * ===    Gundam Storm    ===
 * ===    Revision A01    ===
 * ==========================
 *
 *      Author:  Jace Walker
 * (C) 2009 The Nanashi Foundation
 * http://www.nanashifoundation.org
 *  j.walker@nanashifoundation.org
 */

/* ====================================
 * === JavaScript Functions Library ===
 * ====================================
 */

//Generic Functions
function addCommas(num)
{
	num+='';
	x=num.split('.');
	x1=x[0];
	x2=x.length>1?'.'+x[1]:'';
	var rgx=/(\d+)(\d{3})/;
	while(rgx.test(x1))
	{
		x1=x1.replace(rgx, '$1'+','+'$2');
	}
	return x1+x2;
}

//Sign-Up Functions
function raceInfo()
{
	if(document.getElementById('signup_form').race.options[0].selected)
	{
		document.getElementById('race_info').innerHTML="+10% defense bonus";
	}
	else if(document.getElementById('signup_form').race.options[1].selected)
	{
		document.getElementById('race_info').innerHTML="+10% attack bonus";
	}
	else
	{
		document.getElementById('race_info').innerHTML="+5% attack and defense bonus";
	}
}

//Train Soldiers Functions
function train_pilot(credits, soldiers)
{
	document.train_form.trainpilot_1.value=(document.train_form.trainpilot.value*500)+" Credits";
	train_total(credits, soldiers);
}

function train_spy(credits, soldiers)
{
	document.train_form.trainspy_1.value=(document.train_form.trainspy.value*1000)+" Credits";
	train_total(credits, soldiers);
}

function train_aspy(credits, soldiers)
{
	document.train_form.trainaspy_1.value=(document.train_form.trainaspy.value*1000)+" Credits";
	train_total(credits, soldiers);
}

function train_total(credits, soldiers)
{
	var tp=(document.train_form.trainpilot.value*1);
	var ts=(document.train_form.trainspy.value*1);
	var ta=(document.train_form.trainaspy.value*1);
	var tno=(tp+ts+ta);
	
	var tp1=(tp*500);
	var ts2=(ts*1000);
	var ta3=(ta*1000);
	var ttotal=(tp1+ts2+ta3);
	
	if (ttotal>credits)
	{
		document.train_form.train_total.value="Not enough credits";
	}
	else if(tno>soldiers)
	{ 
		document.train_form.train_total.value="Not enough soldiers";
	}
	else
	{
		document.train_form.train_total.value=ttotal+" Credits";
	}
}

//Buy Suits Functions
function buy_suit_1(credits, pilots)
{
	document.train_form2.buysuit_1.value=(document.train_form2.buysuit1.value*1000)+" Credits";
	buy_suit_total(credits, pilots);
}

function buy_suit_2(credits, pilots)
{
	document.train_form2.buysuit_2.value=(document.train_form2.buysuit2.value*2000)+" Credits";
	buy_suit_total(credits, pilots);
}

function buy_suit_3(credits, pilots)
{
	document.train_form2.buysuit_3.value=(document.train_form2.buysuit3.value*4000)+" Credits";
	buy_suit_total(credits, pilots);
}

function buy_suit_4(credits, pilots)
{
	document.train_form2.buysuit_4.value=(document.train_form2.buysuit4.value*7000)+" Credits";
	buy_suit_total(credits, pilots);
}

function buy_suit_5(credits, pilots)
{
	document.train_form2.buysuit_5.value=(document.train_form2.buysuit5.value*10000)+" Credits";
	buy_suit_total(credits, pilots);
}

function buy_suit_total(credits, pilots)
{
	var buysuit1a=(document.train_form2.buysuit1.value*1);
	var buysuit2a=(document.train_form2.buysuit2.value*1);
	var buysuit3a=(document.train_form2.buysuit3.value*1);
	var buysuit4a=(document.train_form2.buysuit4.value*1);
	var buysuit5a=(document.train_form2.buysuit5.value*1);
	var buysuittotalno=(buysuit1a+buysuit2a+buysuit3a+buysuit4a+buysuit5a);
	
	var buysuit1=(buysuit1a*1000);
	var buysuit2=(buysuit2a*2000);
	var buysuit3=(buysuit3a*4000);
	var buysuit4=(buysuit4a*7000);
	var buysuit5=(buysuit5a*10000);
	var buysuittotal=(buysuit1+buysuit2+buysuit3+buysuit4+buysuit5);
	
	if(buysuittotal>credits)
	{
		document.train_form2.buysuit_total.value="Not enough credits";
	}
	else if(buysuittotalno>pilots)
	{ 
		document.train_form2.buysuit_total.value="Not enough pilots";
	}
	else
	{
		document.train_form2.buysuit_total.value=buysuittotal+" Credits";
	}
}

//Buy Equipment Functions
function buy_wep_1(credits)
{
	document.buy_form.buywep_1.value=(document.buy_form.buywep1.value*100)+" Credits";
	buy_wep_total(credits);
}

function buy_wep_2(credits)
{
	document.buy_form.buywep_2.value=(document.buy_form.buywep2.value*200)+" Credits";
	buy_wep_total(credits);
}

function buy_wep_3(credits)
{
	document.buy_form.buywep_3.value=(document.buy_form.buywep3.value*400)+" Credits";
	buy_wep_total(credits);
}

function buy_wep_4(credits)
{
	document.buy_form.buywep_4.value=(document.buy_form.buywep4.value*700)+" Credits";
	buy_wep_total(credits);
}

function buy_wep_5(credits)
{
	document.buy_form.buywep_5.value=(document.buy_form.buywep5.value*1000)+" Credits";
	buy_wep_total(credits);
}

function buy_wep_total(credits)
{
	var buywep1a=(document.buy_form.buywep1.value*1);
	var buywep2a=(document.buy_form.buywep2.value*1);
	var buywep3a=(document.buy_form.buywep3.value*1);
	var buywep4a=(document.buy_form.buywep4.value*1);
	var buywep5a=(document.buy_form.buywep5.value*1);
	var buyweptotalno=(buywep1a+buywep2a+buywep3a+buywep4a+buywep5a);
	
	var buywep1=(buywep1a*100);
	var buywep2=(buywep2a*200);
	var buywep3=(buywep3a*400);
	var buywep4=(buywep4a*700);
	var buywep5=(buywep5a*1000);
	var buyweptotal=(buywep1+buywep2+buywep3+buywep4+buywep5);
	
	if(buyweptotal>credits)
	{
		document.buy_form.buywep_total.value="Not enough credits";
	}
	else
	{
		document.buy_form.buywep_total.value=buyweptotal+" Credits";
	}
}

//Sell Turns Functions
function sell_turns(atkturns)
{
	var numatks=(document.sell_form.numatk.value*1);
	var total=(document.sell_form.numatk.value*500);
	
	if(numatks>atkturns)
	{
		document.sell_form.sub_sell.value="Not enough attack turns";
	}
	else
	{
		document.sell_form.sub_sell.value="Sell ("+addCommas(total)+" C)";
	}
}

//Barracks Functions
function store_sold_f(soldiers)
{
	if(document.store_form.storesold.value>soldiers)
	{
		document.store_form.store_sold.value="Not enough soldiers";
	}
}

function store_suit_1(suit1, suitText) 
{
	if(document.store_form.storesuit1.value>suit1)
	{
		document.store_form.storesuit_1.value="Not enough "+suitText+"s";
	}
}

function store_suit_2(suit2, suitText)
{
	if(document.store_form.storesuit2.value>suit2)
	{
		document.store_form.storesuit_2.value="Not enough "+suitText+"s";
	}
}

function store_suit_3(suit3, suitText)
{
	if(document.store_form.storesuit3.value>suit3)
	{
		document.store_form.storesuit_3.value="Not enough "+suitText+"s";
	}
}

function store_suit_4(suit4, suitText)
{
	if(document.store_form.storesuit4.value>suit4)
	{
		document.store_form.storesuit_4.value="Not enough "+suitText+"s";
	}
}

function store_suit_5(suit5, suitText)
{
	if(document.store_form.storesuit5.value>suit5)
	{
		document.store_form.storesuit_5.value="Not enough "+suitText+"s";
	}
}
function store_wep_1(suit1_wep, wepText)
{
	if(document.store_form.storesuit1_wep.value>suit1_wep)
	{
		document.store_form.storewep_1.value="Not enough "+wepText+"s";
	}
}

function store_wep_2(suit2_wep, wepText)
{
	if(document.store_form.storesuit2_wep.value>suit2_wep)
	{
		document.store_form.storewep_2.value="Not enough "+wepText+"s";
	}
}

function store_wep_3(suit3_wep, wepText)
{
	if(document.store_form.storesuit3_wep.value>suit3_wep)
	{
		document.store_form.storewep_3.value="Not enough "+wepText+"s";
	}
}

function store_wep_4(suit4_wep, wepText)
{
	if(document.store_form.storesuit4_wep.value>suit4_wep)
	{
		document.store_form.storewep_4.value="Not enough "+wepText+"s";
	}
}

function store_wep_5(suit5_wep, wepText)
{
	if(document.store_form.storesuit5_wep.value>suit5_wep)
	{
		document.store_form.storewep_5.value="Not enough "+wepText+"s";
	}
}

function with_sold_f(stored_soldiers)
{
	if(document.with_form.withsold.value>stored_soldiers)
	{
		document.with_form.with_sold.value="Not enough soldiers";
	}
}

function with_suit_1(stored_suit1, suitText)
{
	if(document.with_form.withsuit1.value>stored_suit1)
	{
		document.with_form.withsuit_1.value="Not enough "+suitText+"s";
	}
}

function with_suit_2(stored_suit2, suitText)
{
	if(document.with_form.withsuit2.value>stored_suit2)
	{
		document.with_form.withsuit_2.value="Not enough "+suitText+"s";
	}
}

function with_suit_3(stored_suit3, suitText)
{
	if(document.with_form.withsuit3.value>stored_suit3)
	{
		document.with_form.withsuit_3.value="Not enough "+suitText+"s";
	}
}

function with_suit_4(stored_suit4, suitText)
{
	if(document.with_form.withsuit4.value>stored_suit4)
	{
		document.with_form.withsuit_4.value="Not enough "+suitText+"s";
	}
}

function with_suit_5(stored_suit5, suitText)
{
	if(document.with_form.withsuit5.value>stored_suit5)
	{
		document.with_form.withsuit_5.value="Not enough "+suitText+"s";
	}
}
function with_wep_1(stored_suit1_wep, wepText)
{
	if(document.with_form.withsuit1_wep.value>stored_suit1_wep)
	{
		document.with_form.withwep_1.value="Not enough "+wepText+"s";
	}
}

function with_wep_2(stored_suit2_wep, wepText)
{
	if(document.with_form.withsuit2_wep.value>stored_suit2_wep)
	{
		document.with_form.withwep_2.value="Not enough "+wepText+"s";
	}
}

function with_wep_3(stored_suit3_wep, wepText)
{
	if(document.with_form.withsuit3_wep.value>stored_suit3_wep)
	{
		document.with_form.withwep_3.value="Not enough "+wepText+"s";
	}
}

function with_wep_4(stored_suit4_wep, wepText)
{
	if(document.with_form.withsuit4_wep.value>stored_suit4_wep)
	{
		document.with_form.withwep_4.value="Not enough "+wepText+"s";
	}
}

function with_wep_5(stored_suit5_wep, wepText)
{
	if(document.with_form.withsuit5_wep.value>stored_suit5_wep)
	{
		document.with_form.withwep_5.value="Not enough "+wepText+"s";
	}
}

//Account Preferences Functions
function showName(username, formURL)
{
	document.getElementById('name_change').innerHTML="<form action='"+formURL+"' method='post'>Username: <input type='text' name='change_name' value='"+username+"' maxlength='30'> <input type='submit' value='Change'> <input type='button' value='Close' onClick='removeName()'></form>";
}

function removeName()
{
	document.getElementById('name_change').innerHTML="";
}

function showPass(formURL)
{
	document.getElementById('pass_change').innerHTML="<form action='"+formURL+"' method='post'><table><tr><td>Current Password:</td><td><input type='password' name='current_pass'></td></tr><tr><td>New Password:</td><td><input type='password' name='new_pass1' maxlength='30'></td></tr><tr><td>Confirm New Password:</td><td><input type='password' name='new_pass2' maxlength='30'></td></tr><tr><td align='center' colSpan='2'><input type='submit' value='Change'> <input type='button' value='Close' onClick='removePass()'></td></tr></table></form>"; 
}
function removePass()
{
	document.getElementById('pass_change').innerHTML="";
}

function showLoc(location, formURL)
{
	document.getElementById('loc_change').innerHTML="<form action='"+formURL+"' method='post'>Location:<input type='text' name='change_loc' value='"+location+"' maxlength='30'> <input type='submit' value='Change'> <input type='hidden' name='locsubmit' value='true'><input type='button' value='Close' onClick='removeLoc()'></form>";
}

function removeLoc()
{
	document.getElementById('loc_change').innerHTML="";
}


function showCont(emailPublic, aim, msn, yim, formURL)
{
	var html="<form action='"+formURL+"' method='post'><table><tr><td colspan='2'>Email Address Public?<input type='checkbox' name='email_p' value='Y'";
	
	if(emailPublic=='Y')
	{
		html+=" checked";
	}
	
	html+="></td></tr><tr><td>AIM:</td><td><input type='text' name='change_aim' value='"+aim+"' maxlength='50'></td></tr><tr><td>MSN:</td><td><input type='text' name='change_msn' value='"+msn+"' maxlength='60'></td></tr><tr><td>Yahoo:</td><td><input type='text' name='change_yahoo' value='"+yim+"' maxlength='50'></td></tr><tr><td align='center' colspan='2'><input type='hidden' name='contsubmit' value='true'><input type='submit' value='Change'> <input type='button' value='Close' onClick='removeCont()'></td></tr></table></form>";
	
	document.getElementById('cont_change').innerHTML=html;
}

function removeCont()
{
	document.getElementById('cont_change').innerHTML="";
}

function showFsig(fsig, formURL)
{
	document.getElementById('fsig').innerHTML="<form action='"+formURL+"' method='post'><table><tr><td>Change your forum sig to:</td></tr><tr><td><textarea name='fsig' rows='10' cols='40'>"+fsig+"</textarea></td></tr><tr><td align='center'><input type='hidden' name='fsigsubmit' value='true'><input type='submit' value='Change'> <input type='button' value='Close' onClick='removeFsig()'></td></tr></table></form>";
}

function removeFsig()
{
	document.getElementById('fsig').innerHTML="";
}

function showPlans()
{
	document.getElementById('view_plans').innerHTML="<form action='"+formURL+"' method='post'>Location:<input type='text' name='change_loc' value='"+location+"' maxlength='30'> <input type='submit' value='Change'> <input type='hidden' name='locsubmit' value='true'><input type='button' value='Close' onClick='removeLoc()'></form>";
}

//Clan Functions
function loan()
{
	if(document.loan_form.type_loan.options[0].selected)
	{
		document.loan_form.lend_sub.value = "Lend";
	}
	else if(document.loan_form.type_loan.options[1].selected)
	{
		document.loan_form.lend_sub.value = "Give";
	}
}

function color_change()
{
	document.getElementById('color_test').innerHTML="<font color="+document.opt.pcolor.value+">color</font>";
}

function getAmt(credits, soldiers, pilots, suit1, suit2, suit3, suit4, suit5, suit1_wep, suit2_wep, suit3_wep, suit4_wep, suit5_wep)
{
	if(document.getElementById("clanform").what_cur.options[0].selected)
	{
		document.getElementById("currentAmt").value=credits;
	}
	if(document.getElementById("clanform").what_cur.options[1].selected)
	{
		document.getElementById("currentAmt").value=soldiers;
	}
	if(document.getElementById("clanform").what_cur.options[2].selected)
	{
		document.getElementById("currentAmt").value=pilots;
	}
	if(document.getElementById("clanform").what_cur.options[3].selected)
	{
		document.getElementById("currentAmt").value=suit1;
	}
	if(document.getElementById("clanform").what_cur.options[4].selected)
	{
		document.getElementById("currentAmt").value=suit2;
	}
	if(document.getElementById("clanform").what_cur.options[5].selected)
	{
		document.getElementById("currentAmt").value=suit3;
	}
	if(document.getElementById("clanform").what_cur.options[6].selected)
	{
		document.getElementById("currentAmt").value=suit4;
	}
	if(document.getElementById("clanform").what_cur.options[7].selected)
	{
		document.getElementById("currentAmt").value=suit5;
	}
	if(document.getElementById("clanform").what_cur.options[8].selected)
	{
		document.getElementById("currentAmt").value=suit1_wep;
	}
	if(document.getElementById("clanform").what_cur.options[9].selected)
	{
		document.getElementById("currentAmt").value=suit2_wep;
	}
	if(document.getElementById("clanform").what_cur.options[10].selected)
	{
		document.getElementById("currentAmt").value=suit3_wep;
	}
	if(document.getElementById("clanform").what_cur.options[11].selected)
	{
		document.getElementById("currentAmt").value=suit4_wep;
	}
	if(document.getElementById("clanform").what_cur.options[12].selected)
	{
		document.getElementById("currentAmt").value=suit5_wep;
	}
}

function ChangeColor(id, color)
{
	document.getElementById(id).style.color=color;
}

function forumCodes(bbcode)
{
	document.new_topic.body.value += bbcode;
}

