// JavaScript Document

function initializeReset() {
// resets forms	
	$('.clearBtn').click(function(){  
	   $("input").val(""); 
	  $("select").each(function() {
	      $("option:first-child").attr("selected", "selected");
	  });
        $(".results-box > p").html(''); 
        $(".errorMsg").html("");  
	    return false;
	});	
}
function initializeTools(){
$(function() {
	// hide tools except for the first one	

	$('h4 ~ .calculator-wrapper').each(function(i) {
        $(this).toggle(1);  // hide all calcs except for the first one
        //$('h4:first > a > span').html('-'); // show - sign on first calc 	    
	});

	
	$('h4 > a').click(function(){
       var spanValue = $(this).children('span').html(); // test for - or +
       if (spanValue == '-') {
            $(this).children('span').html('+');
       }
       else if (spanValue == '+' ) {
            $(this).children('span').html('-');
       }
	   var id = '#' + $(this).attr('rel'); // animate the calculator
	   $(id).slideToggle();
	   return false;
	});
	
	
	
	// voltage calc 				
	$('#losingSub').click(function(){	
	    
	    var storeSelect = $("select[name='storeSelect']").val();
	    var grossAnSales = $("input[name='grossAnSales']").val();
	    var pm1 = $("input[name='pm1']").val();
	    var pm2 = $("input[name='pm2']").val();
	    var pm = (pm1 + '.' + pm2)/100;
	    
	    var secInvest = $("input[name='secInvest']").val();
	    var cp1 = $("input[name='cp1']").val();
	    var cp2 = $("input[name='cp2']").val();
	    var cp = (cp1 + '.' + cp2)/100;
	    
	    var bottomL = grossAnSales * pm;
	    var estLoss = storeSelect * grossAnSales;
	    var perBl = (Math.round((estLoss / bottomL)* 10000))/100;
	    
	    var annSavings = estLoss * cp;
	    var monthsReturn = (Math.round((secInvest /annSavings)*1200))/100;  
	    
	    var roi = (Math.round((annSavings / secInvest)*100));
	    var netProfit = annSavings - (secInvest * 0.7);
	    var res = "<table><tr><td width=\"250\">Your Bottom Line: </td>  <td>$"+ bottomL +"</td></tr>";
 	    res += "<tr><td>Your Estimated Annual Loss: </td>  <td>$"+ estLoss +"</td></tr>";
 	    res += "<tr><td>% of Bottom Line: </td>  <td>"+ perBl  +"%</td></tr>";
 	    res += "<tr><td>Annual savings from investment: </td>  <td>$"+ annSavings +"</td></tr>";
 	    res += "<tr><td>Months to repayment: </td>  <td>"+ monthsReturn  +"</td></tr>";
 	    res += "<tr><td>Return on investment: <br>(assumes no business growth)</td><td>"+ roi +"%</td></tr>";
 	    res += "<tr><td>Net Profit: <br>(after security investment & loss prevention)</td><td>$"+ netProfit +"</td></tr>";
 	    res += "</table>";
 	    
	    $("#losing > .results-box > p").html(res);
	    
	    return false;
	});
	
	// voltage calc 				
	$('#voltageCalcSubmit').click(function(){									
		var volts = $("input[name='volts']").val();
		var watts = $("input[name='watts']").val();
		var amps = $("input[name='amps']").val();
		var msg = 'There was an error processing your calculation. <br />Please try again.';
		
		// find errors
		if ((volts == "") && (watts == "") && (amps == "")) {
			msg = 'You must complete at least two fields';
			$("#voltageCalc > .errorMsg").html(msg);
		}
		else if ((volts > 0) && (watts > 0) && (amps > 0)) { 
			msg='Only fill in two fields';
			$("#voltageCalc > .errorMsg").html(msg);
		}
		// calculate amps
		else if ((volts >0) && (watts > 0)){ amps = watts/volts;  $("input[name='amps']").val(amps); }
		
		// calculate volts
		else if ((amps >0) && (watts > 0)){ volts = watts/amps;  $("input[name='volts']").val(volts); }
		
		// calculate watts
		else if ((amps >0) && (volts > 0)){ watts = amps*volts;  $("input[name='watts']").val(watts); }
		
		else { msg = 'There was an error processing your calculation. <br />Please try again.';  $("#voltageCalc > .errorMsg").html(msg);}
		
		return false;
	}); // end voltage click


/* DVR storage calculator */

$('#dvrSubmit').click(function(){ 
	var daysStorage = $("input[name='daysStorage']").val();
	var numCameras = $("select[name='numCameras']").val();
	var fps = $("select[name='fps']").val();
	var hours = $("select[name='hours']").val();
	var daysWk = $("select[name='daysWk']").val();

	var dvrResult = Math.round(((numCameras * fps * hours * 3600 * 3)/1000000)* ((daysStorage/7)* daysWk));
	if (dvrResult <= 0) { dvrResult = 'Less than 1'; }
	$(".results-box p").html('<span>' + dvrResult + ' Gigabytes' + '</span>');
	return false;
}); // end dvr storage click

/* Voltage Drop Calculator */
$('#voltageDropSubmit').click(function(){ 
$(".volt_res").remove();
var voltDrop_volts = $("select[name='voltDrop_volts']").val();
var voltDrop_acdc = $("select[name='voltDrop_acdc']").val();
var voltDrop_amps = $("input[name='voltDrop_amps']").val();
var voltDrop_ft = $("input[name='voltDrop_ft']").val();
var voltDrop_awg = $("input[name='voltDrop_awg']").val();
var msg = null;

var volts = voltDrop_volts;
var amp = voltDrop_amps;
var length = voltDrop_ft;
var awg = voltDrop_awg;
var ohm = 0;

if (awg == 36) { ohm = 371.0; }
else if (awg == 34) { ohm = 237.0; }
else if (awg == 32) { ohm = 164.0; }
else if (awg == 30) { ohm = 103.0; }
else if (awg == 28) { ohm = 64.9; }
else if (awg == 26) { ohm = 37.3; }
else if (awg == 24) { ohm = 23.3; }
else if (awg == 22) { ohm = 14.7; }
else if (awg == 20) { ohm = 10.0; }
else if (awg == 18) { ohm = 6.4; }
else if (awg == 16) { ohm = 4.0; }
else if (awg == 14) { ohm = 2.5; }
else if (awg == 12) { ohm = 1.6; }
else if (awg == 10) { ohm = 1.1; }
else if (awg == 8) { ohm = .62; }
else if (awg == 6) { ohm = .40; }
else if (awg == 4) { ohm = .24; }
else if (awg == 2) { ohm = .16; }
else if (awg == 1) { ohm = .13; }
else if (awg == '1/0') { ohm = .096; }
else if (awg == '2/0') { ohm = .077; }
else if (awg == '3/0') { ohm = .062; }
else if (awg == '4/0') { ohm = .049; }
else { msg = "Error"; }

var vDrop = ((ohm * 2) * (length / 1000) * amp);
var vFinal = (volts - vDrop);

$('#voltageDropCalc > .results-box > p').html('Voltage Drop = ' + vDrop.toFixed(2) + '<br />' + 'Voltage at Load = ' + vFinal);

return false;
}); // end click submit
/* Voltage Drop Calculator */



	

/* Lens calculator */ 

function NullToZero(x){
	if(x==null || x=="")
		x=0;
}
	function LensCalc(){
	
			var CCDh, CCDv, vFOV, hFOV, DtO, LfL, ccd;
			
			// CCDh = CCD Width Variable
			// CCDv = CCD Height Variable
			// vFOV = Vertical Field of View
			// hFOV = Horizontal Field of View
			// DtO = Distance to Object
			// LfL = Lens Focal Length
			
			var formElements = $("div#lensCalc > *");
			
			
			LfL = $('input[name="length"]').val();			
			DtO = $('input[name="distance"]').val();			
			hFOV =$('input[name="horiz"]').val();
			vFOV = $('input[name="vert"]').val();
		    ccd = $('select[name="ccd"]').val();
			
			
					
			switch (ccd){
				case "1_3":
					CCDv = 3.6;
					CCDh = 4.8;
					break;
				case "1_2":
					CCDv = 4.8;
					CCDh = 6.4;
					break;
				case "2_3":
					CCDv = 6.6;
					CCDh = 8.8;
					break;
				case "1_1":
					CCDv = 9.6;
					CCDh = 12.8;
			}
			
			

	if((LfL > 0 && DtO > 0) || (LfL > 0 && (hFOV > 0 || vFOV > 0)) || (DtO > 0 && (hFOV > 0 || vFOV > 0)))
	{		
		if(LfL==0){
			if(hFOV == 0){
			
				// Calculate horizontal field of view and lens type
				// from vertical field of view and distance to object
				
				LfL = (CCDv * (DtO/vFOV));
				$('input[name="length"]').val(Math.round(LfL*10)/10);  
				$('input[name="horiz"]').val( Math.round(((CCDh * DtO)/LfL)*10)/10);
			}
			if(vFOV == 0){
			
				// Calculate vertical field of view and lens type
				// from horizontal field of view and distance to object
				
				LfL = (CCDh * (DtO/hFOV));
				$('input[name="length"]').val(Math.round(LfL*10)/10);
				$('input[name="vert"]').val(Math.round(((CCDv * DtO)/LfL)*10)/10);			
			}
		}
		else if(LfL > 0){
			if(DtO > 0){
			
				// Calculate horizontal and vertical field of view
				// from lens type and distance to object
				
				$('input[name="horiz"]').val(Math.round(((CCDh * DtO)/LfL)*10)/10);
				$('input[name="vert"]').val(Math.round(((CCDv * DtO)/LfL)*10)/10);			
			}
			if(vFOV > 0 || hFOV > 0){
				
				// Calculate distance to object from either horizontal field of view
				// or vertical vield of view and lens type
				
				if(vFOV > 0){
				
					// Calculate the distance to object and horizontal field of view
					
					$('input[name="distance"]').val(Math.round(((vFOV * LfL)/CCDv)*10)/10);
					DtO = Math.round(((vFOV * LfL)/CCDv)*10)/10;
					$('input[name="horiz"]').val(Math.round(((CCDh * DtO)/LfL)*10)/10);
				}
				else{
				
					// Calculate the distance to object and vertical field of view
					
					$('input[name="distance"]').val(Math.round(((hFOV * LfL)/CCDh)*10)/10);
					DtO = Math.round(((hFOV * LfL)/CCDh)*10)/10;
					$('input[name="vert"]').val(Math.round(((CCDv * DtO)/LfL)*10)/10);			
				}
			}
		}
	}
	
	/*
	    $("#lensCalc > .errorMsg").html(
	    '<p>lfl: ' + LfL +'</p>' +
	    '<p>dto: ' + DtO +'</p>' +
	    '<p>hFov: ' + hFOV +'</p>' +
	    '<p>vFov: ' + vFOV  +'</p>' +
	    '<p>CCD: ' + ccd +'</p>'
	    ); */
	
	
	}
	
/* Lens calculator */ 	

$('#lensCalcSubmit').click(function(){ LensCalc();  return false;  });


});
	}
