function getCalcVal(elem) {
	if (elem.tagName == 'SELECT')
		return elem.options[elem.selectedIndex].value;
	var val = elem.value.replace(/[^\d.:]+/g, '');
	if (elem.value != val)
		elem.value = val;
	vals = val.split(':');
	val = 0;
	for (var i = 0; i < vals.length; i++) {
		if (isNaN(vals[i] = parseFloat(vals[i])))
			vals[i] = 0;
		if (i)
			val += vals[i] / Math.pow(60, i);
		else
			val = vals[i];
	}
	return val;
}
ceRates = [
	{max: 0, ce: 'CE3'},
	{max: 3, ce: 'CE10'},
	{max: 8, ce: 'CE20'},
	{max: 16, ce: 'CE30'},
	{max: 25, ce: 'CE40'},
	{max: 35, ce: 'CE60'},
	{max: 50, ce: 'More recharge time required'}
];
function calcCe() {
	var cf = document.ceCalc,
		current = getCalcVal(cf.current),
		load = getCalcVal(cf.load),
		amph = current * load / 60,
		recharge = getCalcVal(cf.recharge),
		rate = recharge == 0 ? 0 : amph / recharge,
		ce;
	cf.amph.value = amph.toFixed(2);
	cf.rate.value = rate.toFixed(2);
	for (var i = 0; i < ceRates.length; i++) {
		if (ceRates[i].max > rate)
			break;
		ce = ceRates[i].ce;
	}
	$('#ceCalcAnswer')[0].innerHTML = ce;
}
cSizes = [
	{max: 0, c: 15},
	{max: 1.7, c: 14},
	{max: 2.7, c: 13},
	{max: 3.4, c: 12},
	{max: 4.2, c: 11},
	{max: 5.3, c: 10},
	{max: 6.7, c: 9},
	{max: 8.4, c: 8},
	{max: 10.6, c: 7},
	{max: 13.3, c: 6},
	{max: 16.8, c: 5},
	{max: 21.2, c: 4},
	{max: 26.7, c: 3},
	{max: 33.7, c: 2},
	{max: 42.4, c: 1},
	{max: 43.1, c: 0},
	{max: 53.4, c: 'Use double cable'}
];
function calcCs() {
	var cf = document.csCalc,
		length = getCalcVal(cf.length),
		current = getCalcVal(cf.current),
		drop = getCalcVal(cf.drop),
		area = drop == 0 ? 0 : length * current * 0.017 / drop,
		temp = getCalcVal(cf.temp),
		area_other = area * (1 + (temp - 25) * 0.004),
		c,
		cO;
	cf.area.value = area.toFixed(2);
	cf.area_other.value = area_other.toFixed(2);
	for (var i = 0; i < cSizes.length; i++) {
		if (cSizes[i].max > area)
			break;
		c = cSizes[i].c;
	}
	for (var i = 0; i < cSizes.length; i++) {
		if (cSizes[i].max > area_other)
			break;
		cO = cSizes[i].c;
	}
	$('#csCalcAnswer')[0].innerHTML = c;
	$('#csCalcOtherAnswer')[0].innerHTML = cO;
}
function calcASc() {
	var cf = document.vdCalc,
		diameter = getCalcVal(cf.diameter),
		strands = getCalcVal(cf.strands),
		area = Math.pow(diameter/2, 2) * Math.PI * strands;
	cf.area.value = area.toFixed(2);
	calcVd();
}
function calcAS() {
	var cf = document.vdCalc;
	cf.area.value = getCalcVal(cf.size);
	calcVd();
}
function calcVd() {
	var cf = document.vdCalc,
		area = getCalcVal(cf.area),
		length = getCalcVal(cf.length),
		current = getCalcVal(cf.current),
		drop = area == 0 ? 0 : length * current * 0.017 / area,
		temp = getCalcVal(cf.temp),
		drop_other = drop * (1 + (temp - 25) * 0.004);
	cf.drop.value = drop.toFixed(2);
	cf.drop_other.value = drop_other.toFixed(2);
}

