// JavaScript Document

var cable_numbers = Array(221203, 221004, 221204, 221407, 221618, 221604, 221812, 221805, 221404, 221612);
var url = "";

function initSetup() {
	var f = document.calc_form;
	f.num_cables.disabled = true;
	calculateComponents();
}

// ------------------------------------------------------------------------
// Toggle conduits
function toggleQuarter() {
	var f = document.calc_form;
	if (f.chk_quarter.checked) {
		f.con_quarter.disabled = false;
		f.chk_half.checked = false;
		f.chk_two.checked = false;
		f.con_half.disabled = true;
		f.con_two.disabled = true;
	} else {
		f.con_quarter.disabled = true;
	}
	calculateComponents();
}

function toggleHalf() {
	var f = document.calc_form;
	if (f.chk_half.checked) {
		f.con_half.disabled = false;
		f.chk_quarter.checked = false;
		f.chk_two.checked = false;
		f.con_quarter.disabled = true;
		f.con_two.disabled = true;
	} else {
		f.con_half.disabled = true;
	}
	calculateComponents();
}

function toggleTwo() {
	var f = document.calc_form;
	if (f.chk_two.checked) {
		f.con_two.disabled = false;
		f.chk_quarter.checked = false;
		f.chk_half.checked = false;
		f.con_quarter.disabled = true;
		f.con_half.disabled = true;
	} else {
		f.con_two.disabled = true;
	}
	calculateComponents();
}

// ------------------------------------------------------------------------
// Perform main calculations
function calculateComponents() {
	var f = document.calc_form;
	totalLength();
}

function cableCheck(cbox) {
	var f = document.calc_form;
	var num = 0;
	if (cbox.checked) {
		var num = f.num_cables.value++;
	} else {
		f.num_cables.value--;
	}
	totalLength();
}

function totalLength() {
	var f = document.calc_form;
	var length = f.install_length.value;
	f.total_quant.value = length * f.num_cables.value;
	finalCalc();
}

function finalCalc() {
	var f = document.calc_form;
	var length = f.install_length.value;
	var num_conductors = 0;
	var total_cable = f.total_quant.value * 1;
	var num_drops = f.num_drops.value;
	var drop_length = f.drop_length.value;
	var num_cables = 0;
	
	// Cable costs -----------------------------
	var tcable = 0;
	var vcable = 0;
	var hcable = 0;
	for (var i = 0; i < cable_numbers.length; i++) {
		var curr_cable = cable_numbers[i];
		if (eval("f.chk_" + curr_cable + ".checked")) {
			num_cables++;
			switch(curr_cable) {
				case 221203: num_conductors += 3; tcable += (0.659 * length); vcable += (0.347 * length); hcable += (0.216 * length); break;
				case 221004: num_conductors += 4; tcable += (1.061 * length); vcable += (0.666 * length); hcable += (0.456 * length); break;
				case 221204: num_conductors += 4; tcable += (0.740 * length); vcable += (0.433 * length); hcable += (0.29 * length); break;
				case 221407: num_conductors += 7; tcable += (0.756 * length); vcable += (0.543 * length); hcable += (0.331 * length); break;
				case 221618: num_conductors += 18; tcable += (1.106 * length); vcable += (0.994 * length); hcable += (0.756 * length); break;
				case 221604: num_conductors += 4; tcable += (0.308 * length); vcable += (0.246 * length); hcable += (0.168 * length); break;
				case 221812: num_conductors += 12; tcable += (0.67 * length); vcable += (0.494 * length); hcable += (0.372 * length); break;
				case 221805: num_conductors += 5; tcable += (0.325 * length); vcable += (0.237 * length); hcable += (0.155 * length); break;
				case 221404: num_conductors += 4; tcable += (0.441 * length); vcable += (0.330 * length); hcable += (0.189 * length); break;
				case 221612: num_conductors += 12; tcable += (0.765 * length); vcable += (0.670 * length); hcable += (0.504 * length); break;
			}
		}
	}
	setVal("tcable", "$" + toCurrency(tcable));
	setVal("vcable", "$" + toCurrency(vcable));
	setVal("hcable", "$" + toCurrency(hcable));
	
	// Get number of conduits ----------------------------------------------------------------------------
	var conduit = 0;
	var num_conduits = 0;
	if (f.chk_half.checked) {
		conduit = f.con_half.value;
		num_conduits = Math.ceil(num_cables / 3);
	} else if (f.chk_two.checked) {
		conduit = f.con_two.value;
		num_conduits = Math.ceil(num_cables / 4);
	} else {
		conduit = f.con_quarter.value;
		num_conduits = num_cables;
	}
	
	hnum_conduits = 0
	if (num_cables < 4) {
		hnum_conduits = 1;
	} else if (num_cables < 7) {
		hnum_conduits = 2;
	} else if (num_cables < 10) {
		hnum_conduits = 3;
	} else {
		hnum_conduits = 4;
	}
	
	
	// Support System ------------------------------------------------------------------------------------
	var support = length - (num_drops * drop_length);
	setVal("tsupport", "$" + toCurrency(support * 10));
	setVal("vsupport", "$" + toCurrency(support * 10));
	setVal("hsupport", "$" + toCurrency(length * hnum_conduits * f.thhn_cost.value));
	
	// Conduit -------------------------------------------------------------------------------------------
	var conduit = num_drops * drop_length * conduit * num_conduits;
	setVal("tconduit", "$" + toCurrency(conduit * 0.1));
	setVal("vconduit", "$" + toCurrency(conduit));
	setVal("hconduit", "See Support System");
	
	// Coupler -------------------------------------------------------------------------------------------
	var coupler = Math.floor(drop_length / 10) * num_drops * f.coupler_cost.value * num_conduits;
	var hcoupler = (((length * num_cables) - (num_drops * drop_length * num_cables)) / 20) * f.coupler_cost.value + coupler;
	setVal("tcoupler", "$" + toCurrency(coupler * 0.1));
	setVal("vcoupler", "$" + toCurrency(coupler));
	setVal("hcoupler", "$" + toCurrency(hcoupler));
	
	// Connectors ----------------------------------------------------------------------------------------
	var connector = (num_drops * f.connector_cost.value) * num_conduits;
	setVal("tconnector", "$" + toCurrency(connector * 0.1));
	setVal("vconnector", "$" + toCurrency(connector));
	setVal("hconnector", "$" + toCurrency(connector));
	
	// Junction Box --------------------------------------------------------------------------------------
	var junction = num_drops * f.junction_cost.value;
	setVal("tjunction", "$" + toCurrency(junction * 0.1));
	setVal("vjunction", "$" + toCurrency(junction));
	setVal("hjunction", "$" + toCurrency(junction));
	
	// Clamps --------------------------------------------------------------------------------------------
	var clamp = (drop_length * num_drops * num_conduits) / 6 * f.clamp_cost.value;
	setVal("tclamps", "$" + toCurrency(clamp * 0.1));
	setVal("vclamps", "$" + toCurrency(clamp));
	setVal("hclamps", "$" + toCurrency(clamp));
	
	// Material Totals -----------------------------------------------------------------------------------
	var tmaterial = tcable + (support * 10) + (conduit * 0.1) + (coupler * 0.1) + (connector * 0.1) + (junction * 0.1) + (clamp * 0.1);
	setVal("tmaterial", "$" + toCurrency(tmaterial));
	var vmaterial = vcable + (support * 10) + conduit + coupler + connector + junction + clamp;
	setVal("vmaterial", "$" + toCurrency(vmaterial));
	var hmaterial = hcable + (length * hnum_conduits * f.thhn_cost.value) + hcoupler + connector + junction + clamp;
	setVal("hmaterial", "$" + toCurrency(hmaterial));
	
	// Labour --------------------------------------------------------------------------------------------
	var labour = (length / 100) * num_cables * f.labour_rate.value;
	setVal("tlabour", "$" + toCurrency(f.tray2_rate.value * labour));
	setVal("vlabour", "$" + toCurrency(f.vntc_rate.value * labour));
	setVal("hlabour", "$" + toCurrency(9 * labour));
	
	// Totals --------------------------------------------------------------------------------------------
	var ttotal = (f.tray2_rate.value * labour) + tmaterial;
	var vtotal = (f.vntc_rate.value * labour) + vmaterial;
	var htotal = (9 * labour) + hmaterial;
	setVal("ttotal", "$" + toCurrency(ttotal));
	setVal("vtotal", "$" + toCurrency(vtotal));
	setVal("htotal", "$" + toCurrency(htotal));
	url = "popup.asp?ttotal=" + ttotal + "&vtotal=" + vtotal + "&htotal=" + htotal + "&vcable=" + vcable + "&hcable=" + hcable;
	
	// Savings  -------------------------------------------------------------------------------------
	//setVal("vsavings", "$" + toCurrency(vtotal - ttotal));
	//setVal("hsavings", "$" + toCurrency(htotal - ttotal));
	
	// Percent Saved --------------------------------------------------------------------------------
	//setVal("vpercent", toPercent((vtotal - ttotal) / vtotal * 100) + "%");
	//setVal("hpercent", toPercent((htotal - ttotal) / htotal * 100) + "%");
	
	// VNTC Adjust --------------------------------------------------------------------------------
	//var deci = (f.vntc_adj.value / 100);
	//var vadjust = vtotal - (vcable * deci);
	//var hadjust = htotal - (hcable * deci);
	//setVal("vadjust", "$" + toCurrency(vadjust));
	//setVal("hadjust", "$" + toCurrency(hadjust));
	//setVal("vadpercent", toPercent((vadjust - ttotal) / vadjust * 100) + "%"); 
	//setVal("hadpercent", toPercent((hadjust - ttotal) / hadjust * 100) + "%");
	
}


// ########################################################################
// Utility Functions ######################################################
// ########################################################################
function getVal(div_name) {
	return document.getElementById(div_name).innerHTML;
}

function setVal(div_name, val) {
	document.getElementById(div_name).innerHTML = val;
}

function toCurrency(input) {
	if (IsNumeric(input)) {
		var fix = Math.round(input);
		
		return addCommas(fix);
	} else {
		return input;
	}
}

function toPercent(input) {
	if (IsNumeric(input)) {
		var fix = input.toFixed(2);
		return fix;
	} else {
		return input;
	}
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
	Char = sText.charAt(i); 
	if (ValidChars.indexOf(Char) == -1) 
		{
		IsNumber = false;
		}
	}
return IsNumber;
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.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;
}

function centerpopup(varHTM,varWidth,varHeight)
    {
    leftPos = 0
    topPos = 0
    if (screen) {
        leftPos = (screen.width-varWidth)/2
        topPos = (screen.height-varHeight)/2
    }
    newWindow = window.open(url, 'newWin', 'width='+varWidth+',height='+varHeight+',left='+leftPos+',top='+topPos+'')
}

