var fTO = 0.083;
var fDTO = 0.05;
var fDO = 0.167;
var fDDO = 0.1;
var fSO = 0.333;
var fDSO = 0.2;
var fHast = 0.7;
var fWeap = 0.05;
var fAMRec = 0.33;
var fAMDam = 0.25;
var fQuick = 0.2;
var fFoeACC = new Array(1.0, 0.9067, 0.8133, 0.7333, 0.64, 0.5466, 0.4533, 0.3333, 0.1467, 0.08, 0.0667, 0.0667, 0.0667);
var fFoeEff = new Array(1.0, 0.9, 0.8, 0.65, 0.48, 0.3, 0.15, 0.08, 0.05, 0.04, 0.03, 0.02, 0.01);


function CalcVal(ReqType){
	var EnhCalcForm = document.getElementsByName("EnhCalcForm")[0];
	var CharLvl = EnhCalcForm.CharLvl.value;
	var FoeLvl = EnhCalcForm.FoeLvl.value;
	var EnhLvl = new Array(6)
	EnhLvl[0] = EnhCalcForm.Enh1.value;
	EnhLvl[1] = EnhCalcForm.Enh2.value;
	EnhLvl[2] = EnhCalcForm.Enh3.value;
	EnhLvl[3] = EnhCalcForm.Enh4.value;
	EnhLvl[4] = EnhCalcForm.Enh5.value;
	EnhLvl[5] = EnhCalcForm.Enh6.value;
	var EnhProp = new Array(6)
	EnhProp[0] = EnhCalcForm.EnhProp1.value;
	EnhProp[1] = EnhCalcForm.EnhProp2.value;
	EnhProp[2] = EnhCalcForm.EnhProp3.value;
	EnhProp[3] = EnhCalcForm.EnhProp4.value;
	EnhProp[4] = EnhCalcForm.EnhProp5.value;
	EnhProp[5] = EnhCalcForm.EnhProp6.value;
	var EnhType = new Array(6)
	EnhType[0] = EnhTypeVal(EnhCalcForm.EnhType1.value, EnhProp[0]);
	EnhType[1] = EnhTypeVal(EnhCalcForm.EnhType2.value, EnhProp[1]);
	EnhType[2] = EnhTypeVal(EnhCalcForm.EnhType3.value, EnhProp[2]);
	EnhType[3] = EnhTypeVal(EnhCalcForm.EnhType4.value, EnhProp[3]);
	EnhType[4] = EnhTypeVal(EnhCalcForm.EnhType5.value, EnhProp[4]);
	EnhType[5] = EnhTypeVal(EnhCalcForm.EnhType6.value, EnhProp[5]);
	var EnhVal = new Array(6)
	for(var i=0; i<EnhProp.length; i++)
		EnhVal[i] = IndEnhVal(EnhType[i], CharLvl, EnhLvl[i]);
	var EnhTotVal = new Array(8);
	for(var i=0; i< EnhTotVal.length; i++)
		EnhTotVal[i] = 1.0;
	for(var i=0; i<6; i++)
		EnhTotVal[(EnhProp[i]-1)] = EnhTotVal[(EnhProp[i]-1)] + EnhVal[i];
	if(EnhCalcForm.Hasten.checked == true)
		EnhTotVal[6] += fHast;
	if(EnhCalcForm.PermaH.checked == true)
		EnhTotVal[6] += fHast; 
	if(EnhCalcForm.Weapon.checked == true)
		EnhTotVal[0] += fWeap;
	if (EnhCalcForm.AccMet.checked == true){
		EnhTotVal[1] += fAMDam;
		EnhTotVal[6] += fAMRec;
	}
	if(EnhCalcForm.Quick.checked == true)
		EnhTotVal[6] += fQuick;
	if(FoeLvl - CharLvl > 0){
		FoeLvl = FoeLvl - CharLvl;
		if(FoeLvl>12)
			FoeLvl = 12;
	}
	else
		FoeLvl = 0;
		

	if(ReqType == "Base"){
		EnhCalcForm.EnhAcc.value = (Math.round((EnhCalcForm.BaseAcc.value*EnhTotVal[0]*fFoeACC[FoeLvl])*100))/100;
		EnhCalcForm.EnhDam.value = (Math.round((EnhCalcForm.BaseDam.value*EnhTotVal[1]*fFoeEff[FoeLvl])*100))/100;
		EnhCalcForm.EnhDef.value = (Math.round((EnhCalcForm.BaseDef.value*EnhTotVal[2])*100))/100;
		EnhCalcForm.EnhDur.value = (Math.round((EnhCalcForm.BaseDur.value*EnhTotVal[3])*100))/100;
		EnhCalcForm.EnhEnd.value = (Math.round((EnhCalcForm.BaseEnd.value/EnhTotVal[4])*100))/100;
		EnhCalcForm.EnhRan.value = (Math.round((EnhCalcForm.BaseRan.value*EnhTotVal[5])*100))/100;
		if(EnhCalcForm.BaseRec.value < 206.8 || EnhCalcForm.Hasten.checked!=true)
			EnhCalcForm.EnhRec.value = (Math.round((EnhCalcForm.BaseRec.value/EnhTotVal[6])*100))/100;
		else
			EnhCalcForm.EnhRec.value = fHasten(EnhCalcForm.BaseRec.value, (EnhTotVal[6]-fHast), fHast);
		EnhCalcForm.EnhRes.value = (Math.round((EnhCalcForm.BaseRes.value*EnhTotVal[7])*100))/100;
	}
	if(ReqType == "Enhanced"){
		EnhCalcForm.BaseAcc.value = (Math.round((EnhCalcForm.EnhAcc.value/(EnhTotVal[0]*fFoeACC[FoeLvl]))*100))/100;
		EnhCalcForm.BaseDam.value = (Math.round((EnhCalcForm.EnhDam.value/(EnhTotVal[1]*fFoeEff[FoeLvl]))*100))/100;
		EnhCalcForm.BaseDef.value = (Math.round((EnhCalcForm.EnhDef.value/EnhTotVal[2])*100))/100;
		EnhCalcForm.BaseDur.value = (Math.round((EnhCalcForm.EnhDur.value/EnhTotVal[3])*100))/100;
		EnhCalcForm.BaseEnd.value = (Math.round((EnhCalcForm.EnhEnd.value*EnhTotVal[4])*100))/100;
		EnhCalcForm.BaseRan.value = (Math.round((EnhCalcForm.EnhRan.value/EnhTotVal[5])*100))/100;
		if(EnhCalcForm.EnhRec.value<120.1 || EnhCalcForm.Hasten.checked!=true)
			EnhCalcForm.BaseRec.value = (Math.round((EnhCalcForm.EnhRec.value*EnhTotVal[6])*100))/100;
		else
			EnhCalcForm.BaseRec.value = fRevHasten(EnhCalcForm.EnhRec.value, (EnhTotVal[6]-fHast), fHast);
		EnhCalcForm.BaseRes.value = (Math.round((EnhCalcForm.EnhRes.value/EnhTotVal[7])*100))/100;
	}	
}

function fCheckHaste(sType){
	var EnhCalcForm = document.getElementsByName("EnhCalcForm")[0];
	switch(sType){
		case "H":
			if(EnhCalcForm.Hasten.checked == true)
				EnhCalcForm.PermaH.checked = false;
			break;
		case "PH":
			if(EnhCalcForm.PermaH.checked == true)
				EnhCalcForm.Hasten.checked = false;
	}
}

function EnhTypeVal ( iType, iProp){
	var fEnhVal = 0.0;
	if(parseInt(iProp) == 3 || parseInt(iProp) == 8){
		switch(parseInt(iType)){
			case 1:
				fEnhVal = fDTO;
				break;
			case 2:
				fEnhVal = fDDO;
				break; 
			case 3:
				fEnhVal = fDSO;
		}
	} 
	else {
		switch(parseInt(iType)){
			case 1:
				fEnhVal = fTO;
				break;
			case 2:
				fEnhVal = fDO;
				break; 
			case 3:
				fEnhVal = fSO;
		}
	}
	return fEnhVal;
		
}

function fHasten(fRecOrig, fBaseRecRate, fHasten){
	var fMaxRec = fBaseRecRate + fHasten;
	var fHasteDur = 120.0;
	var fHConsumed = fHasteDur*fMaxRec;
	if(fRecOrig > fHConsumed){
		var fHRemain = fRecOrig - fHConsumed;
		return ((Math.round((fHasteDur + (fHRemain/fBaseRecRate))*100))/100)
	}
	else
		return ((Math.round(fRecOrig/(fBaseRecRate+fHasten))*100)/100)
}

function fRevHasten(fRecOrig, fBaseRecRate, fHasten){
	var fMaxRec = fBaseRecRate + fHasten;
	var fHasteDur = 120.0;
	var fHRemain = fRecOrig - fHasteDur;
	var fHConsumed = fHasteDur*fMaxRec;
	return (Math.round((fHConsumed + fHRemain*fBaseRecRate)*100)/100);
}

function IndEnhVal( BaseVal, CharLvl, EnhLvl){
	iLvlDiff = EnhLvl - CharLvl;
	if(iLvlDiff > -4 && iLvlDiff < 0)
		return (1-(0.1*Math.abs(iLvlDiff)))*BaseVal;
	else if(iLvlDiff == 0 )
		return BaseVal;
	else if(iLvlDiff > 0 && iLvlDiff<4)
		return (1+(0.05*iLvlDiff))*BaseVal;
	else
		return 0;
}

function fMouseOver(HelpVal){
	var HelpWin = document.getElementsByName("HelpWin")[0];
	var StartFormat = "<font face='Arial, Helvetica, sans-serif' color='#FFFF00' size='1'>";
	var EndFormat = "</font>";
	switch(HelpVal){
		case "Out":
			HelpWin.innerHTML=StartFormat+EndFormat;
			break;
		case "CharLvl":
			HelpWin.innerHTML=StartFormat+"Input your characters Combat Level."+EndFormat;
			break;
		case "EnhLvl":
			HelpWin.innerHTML=StartFormat+"Input enhancement slot level."+EndFormat;
			break;
		case "EnhType":
			HelpWin.innerHTML=StartFormat+"Use the drop down to select the type of enhancer.<br><br>Non-Defense:<br>Training = 0.083"+
			                              "<br>Dual Origin = 0.167<br>Single Origin = 0.333<br><br>Defense:<br>Training = 0.05"+
			                              "<br>Dual Origin = 0.1<br>Single Origin = 0.2"+EndFormat;
			break;
		case "PowProp":
			HelpWin.innerHTML=StartFormat+"Use the drop down to select the property of power you are enhancing."+
										  "<br><br>Be sure to decide if you want to calculate recharge rates with or without hasten."
										  +EndFormat;
			break;
		case "BaseVal":
			HelpWin.innerHTML=StartFormat+"Input base value of the property you are enhancing if you want to determine the effect of enhancers on it."+EndFormat;
			break;
		case "EnhVal":
			HelpWin.innerHTML=StartFormat+"Input the enhanced value of the property if you want to determine the base value of the property."+EndFormat;
			break;
		case "Enhance":
			HelpWin.innerHTML=StartFormat+"Click to determine the the effect of the enhancements on the base value of the power!"+EndFormat;
			break;
		case "Base":
			HelpWin.innerHTML=StartFormat+"Click to determine the base value of a powers property without the enhancements."+EndFormat;
			break;
		case "Hasten":
			HelpWin.innerHTML=StartFormat+"Check to calculate effect of Hasten on recharge rates."+EndFormat;
			break;
		case "Weapon":
			HelpWin.innerHTML=StartFormat+"Check to add in a +5% bonus given to weapons.  Be sure to check this only if your attack recieves the bonus!"+EndFormat;
			break;
		case "Quick":
			HelpWin.innerHTML=StartFormat+"Check this box if you have the Super Reflex power Quick to include the 20% bonux it gives you to your recharge calculations"+EndFormat;
			break;
		case "AccMet":
			HelpWin.innerHTML=StartFormat+"Check to calculate the effects of Accelerate Metabolism on your damage (+25% bonus) and Recharge rate (+33% bonus)"+
										  "<br><br><b>Note:</b> There is a known issue where AM is considered to"+
										  " have an unlimted duration in the calculation.  Please take this into account for long durations."+EndFormat;
			break;
		case "FoeLvl":
			HelpWin.innerHTML=StartFormat+"Enter the level of Foe you are fighting or leave 0 for foes your level or below."+
			                              "  Damage and Accuracy will be adjusted based on the foes level."+EndFormat;
			break;
		case "UpAll":
			HelpWin.innerHTML=StartFormat+"Set this field to set the same value for all enhancements."+EndFormat;
	}
}

function fUpAll(sArg){
	var EnhCalcForm = document.getElementsByName("EnhCalcForm")[0];
	if(sArg == "Lvl"){
		EnhCalcForm.Enh1.value = EnhCalcForm.UpAllLvl.value;
		EnhCalcForm.Enh2.value = EnhCalcForm.UpAllLvl.value;
		EnhCalcForm.Enh3.value = EnhCalcForm.UpAllLvl.value;
		EnhCalcForm.Enh4.value = EnhCalcForm.UpAllLvl.value;
		EnhCalcForm.Enh5.value = EnhCalcForm.UpAllLvl.value;
		EnhCalcForm.Enh6.value = EnhCalcForm.UpAllLvl.value;
	}else if (sArg == "Type"){
		EnhCalcForm.EnhType1.value = EnhCalcForm.UpAllType.value;
		EnhCalcForm.EnhType2.value = EnhCalcForm.UpAllType.value;
		EnhCalcForm.EnhType3.value = EnhCalcForm.UpAllType.value;
		EnhCalcForm.EnhType4.value = EnhCalcForm.UpAllType.value;
		EnhCalcForm.EnhType5.value = EnhCalcForm.UpAllType.value;
		EnhCalcForm.EnhType6.value = EnhCalcForm.UpAllType.value;
	}else if (sArg == "Prop"){
		EnhCalcForm.EnhProp1.value = EnhCalcForm.UpAllProp.value;
		EnhCalcForm.EnhProp2.value = EnhCalcForm.UpAllProp.value;
		EnhCalcForm.EnhProp3.value = EnhCalcForm.UpAllProp.value;
		EnhCalcForm.EnhProp4.value = EnhCalcForm.UpAllProp.value;
		EnhCalcForm.EnhProp5.value = EnhCalcForm.UpAllProp.value;
		EnhCalcForm.EnhProp6.value = EnhCalcForm.UpAllProp.value;
	}

}
