// JavaScript Document
$(function() {
	//$('#voltageCalc').submit(function(){					
	$('#voltageCalcSubmit').click(function(){									
		var volts = $("input[@name='volts']").val();
		var watts = $("input[@name='watts']").val();
		var amps = $("input[@name='amps']").val();
		var msg = '<p class="message">There was an error processing your calculation</p><p class="message">Please try again.</p>';
		
		// find errors
		if ((volts == "") && (watts == "") && (amps == "")) {
			$("fieldset").append(msg);
			//$(".message").fadeIn(3000, function(){ $(".message").fadeOut(2000); });
			$(".message").fadeIn(3000);
			$(".message").fadeOut(3000, function(){ $(".message").remove(); });
			//alert(msg);
		}
		else if ((volts > 0) && (watts > 0) && (amps > 0)) { 
			msg='<p class="message">Only fill in two fields</p>';
			$("fieldset").append(msg);
			$(".message").fadeIn(3000);
			$(".message").fadeOut(3000, function(){ $(".message").remove(); })
		}
		
		// 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); }
		
		return false;
	});
	
	$('#voltageCalcClear').click(function(){ $("form").resetForm(); });	


/* 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'; }
	/*$(".dvrResult").append(dvrResult + ' Gigabytes' + '<p>' + daysStorage +'<br>'+  numCameras +'<br>' 
						   + fps +'<br>'+  hours +'<br>' + daysWk +'<br>' +'<p>');*/
	$(".dvrResult").append('<div>'+dvrResult + ' Gigabytes'+'</div>');
	$(".dvrResult").fadeIn(3000);
	$(".dvrResult").fadeTo(2000,1);
	$(".dvrResult").fadeOut(2000, function(){$(".dvrResult > div").remove(); });
});

$('#dvrClear').click(function(){ $("form").resetForm(); });	

/* DVR storage calculator */


/* Voltage Drop Calculator */
$('#voltDrop_Submit').click(function(){ 
$(".volt_res").remove();
var voltDrop_volts = $("select[@name='voltDrop_volts']").val();
var voltDrop_amps = $("input[@name='voltDrop_amps']").val();
var voltDrop_ft = $("input[@name='voltDrop_ft']").val();
var volt_result ="";
var awg = new Array();

awg[10] = 10380
awg[12] = 6530
awg[14] = 4110
awg[16] = 2560
awg[18] = 1620
awg[20] = 1020
awg[22] = 642
awg[24] = 404

	$.each([10,12,14,16,18,20,22,24], function (i, n) {
	volt_result = voltDrop_volts - Math.round(((21.32*voltDrop_ft*voltDrop_amps)/awg[n])*100)/100; 
	if (volt_result < 0) { volt_result = voltDrop_volts;}
	var gLayer = "div#awg_"+n;
	$(gLayer).append("<span class='volt_res'>"+volt_result+" volts</span>");
	});

}); // end click submit

$('#voltDrop_Clear').click(function(){ 
	$("form").resetForm();
	$.each([10,12,14,16,18,20,22,24], function (i, n) {
		$(".volt_res").remove();
	});
									 });



/* Voltage Drop Calculator */
});
	
	

/* Lens calculator */ 

function NullToZero(x){
	if(x==null || x=="")
		x=0;
}
	function LensCalc(){
	
			var CCDh, CCDv, vFOV, hFOV, DtO, LfL;
			
			// 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
			
			switch (document.camtype.CCD.value){
				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;
			}
			LfL = document.camtype.length.value;			
			DtO = document.camtype.distance.value;			
			hFOV = document.camtype.horiz.value;
			vFOV = document.camtype.vert.value;

	if((LfL > 0 && DtO > 0) || (LfL > 0 && (hFOV > 0 || vFOV > 0)) || (DtO > 0 && (hFOV > 0 || vFOV > 0)))
	{		
		if(document.camtype.length.value==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));
				document.camtype.length.value = Math.round(LfL*10)/10;
				document.camtype.horiz.value = 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));
				document.camtype.length.value = Math.round(LfL*10)/10;
				document.camtype.vert.value = 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
				
				document.camtype.horiz.value = Math.round(((CCDh * DtO)/LfL)*10)/10;
				document.camtype.vert.value = 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
					
					document.camtype.distance.value = Math.round(((vFOV * LfL)/CCDv)*10)/10;
					DtO = Math.round(((vFOV * LfL)/CCDv)*10)/10;
					document.camtype.horiz.value = Math.round(((CCDh * DtO)/LfL)*10)/10;
				}
				else{
				
					// Calculate the distance to object and vertical field of view
					
					document.camtype.distance.value = Math.round(((hFOV * LfL)/CCDh)*10)/10;
					DtO = Math.round(((hFOV * LfL)/CCDh)*10)/10;
					document.camtype.vert.value = Math.round(((CCDv * DtO)/LfL)*10)/10;			
				}
			}
		}
	}
	}
	
/* Lens calculator */ 	
