function createSpinControl(divName, inputName, minValue, maxValue, incValue, curValue, updateFunction, width, leftpos)
{
	var spinCtrl = new SpinControl(divName, inputName, minValue, maxValue, incValue);
	spinCtrl.Tag = inputName;
	spinCtrl.SetCurrentValue(curValue);
	spinCtrl.SetWidth(width);
	spinCtrl.GetAccelerationCollection().Add(new SpinControlAcceleration(0.1, 500));
	spinCtrl.GetAccelerationCollection().Add(new SpinControlAcceleration(0.5, 1000));
	spinCtrl.GetAccelerationCollection().Add(new SpinControlAcceleration(1.0, 2000));
	spinCtrl.GetContainer().style.position = 'absolute';
	spinCtrl.GetContainer().style.left = leftpos;
	spinCtrl.AttachValueChangedListener(updateFunction);
	spinCtrl.StartListening();
	return spinCtrl;
}
		
function updateFitAdapterCount()
{
	document.getElementById('fit_adapter_count').value = document.getElementById('fit_adapter').value;
}

function updateFitEll90Count()
{
	document.getElementById('fit_ell90_count').value = document.getElementById('fit_ells90').value;
}

function updateFitTeeRunCount()
{
	document.getElementById('fit_teerun_count').value = document.getElementById('fit_teerun').value;
}

function updateFitCouplingCount()
{
	document.getElementById('fit_coupling_count').value = document.getElementById('fit_coupling').value;
}

function updateFitEll45Count()
{
	document.getElementById('fit_ell45_count').value = document.getElementById('fit_ells45').value;
}

function updateFitTeeBranchCount()
{
	document.getElementById('fit_teebranch_count').value = document.getElementById('fit_teebranch').value;
}

function updateFitGateValveCount()
{
	document.getElementById('fit_gatevalve_count').value = document.getElementById('fit_gatevalve').value;
}

function updateFitBallValveCount()
{
	document.getElementById('fit_ballvalve_count').value = document.getElementById('fit_ballvalve').value;
}

function updatePHeadSpin()
{
	document.getElementById('pressure_head').value = document.getElementById('wiz_pressure_head').value;
}

function updatePipeLengthSpin()
{
	document.getElementById('length').value = document.getElementById('wiz_pipe_length').value;
}

function updateStaticHeadSpin()
{
	document.getElementById('static_head').value = document.getElementById('wiz_static_head').value;
}

function updateTargetGPHSpin()
{
	document.getElementById('target_gph').value = document.getElementById('wiz_target_gph').value;
}

function spinSubmitForm(sender, newVal)
{
  //this.form.submit();
  document.ctl00.submit();
}

var spinCtrl = createSpinControl('spinLengthContainer', 'length', 0, 200, 5, document.getElementById('length').value, spinSubmitForm, 75, '125px');
spinCtrl.SetButtonColor('Blue');

var spinCtrl = createSpinControl('spinTargetGPHContainer', 'target_gph', 0, 9000, 100.0, document.getElementById('target_gph').value, spinSubmitForm, 75, '125px');

var PH_SpinCtrl = createSpinControl('spinPressureHeadContainer', 'pressure_head', 0, 20, 0.2, document.getElementById('pressure_head').value, spinSubmitForm, 75, '125px');
PH_SpinCtrl.SetBackgroundColor('#EDFCC3');
PH_SpinCtrl.SetBorderColor('#148238');
PH_SpinCtrl.SetButtonColor('#148238');
PH_SpinCtrl.SetFontColor('#555533');

var SH_SpinCtrl = createSpinControl('spinSHContainer', 'static_head', 0, 100, 0.2, document.getElementById('static_head').value, spinSubmitForm, 75, '125px');
SH_SpinCtrl.SetButtonColor('Red');

$(document).ready(function () {

	$('a#showFittings').click(function (e) {
		//Preload data for fittings dialog
		createSpinControl('spinAdapters', 'fit_adapter', 0, 50, 1, document.getElementById('fit_adapter_count').value, updateFitAdapterCount, 40, '80px');
		createSpinControl('spinElbows', 'fit_ells90', 0, 50, 1, document.getElementById('fit_ell90_count').value, updateFitEll90Count, 40, '80px');
		createSpinControl('spinTeeRun', 'fit_teerun', 0, 50, 1, document.getElementById('fit_teerun_count').value, updateFitTeeRunCount, 40, '80px');
		createSpinControl('spinCoupling', 'fit_coupling', 0, 50, 1, document.getElementById('fit_coupling_count').value, updateFitCouplingCount, 40, '80px');
		createSpinControl('spinElbow45', 'fit_ells45', 0, 50, 1, document.getElementById('fit_ell45_count').value, updateFitEll45Count, 40, '80px');
		createSpinControl('spinTeeBranch', 'fit_teebranch', 0, 50, 1, document.getElementById('fit_teebranch_count').value, updateFitTeeBranchCount, 40, '80px');
		createSpinControl('spinGateValve', 'fit_gatevalve', 0, 50, 1, document.getElementById('fit_gatevalve_count').value, updateFitGateValveCount, 40, '80px');
		createSpinControl('spinBallValve', 'fit_ballvalve', 0, 50, 1, document.getElementById('fit_ballvalve_count').value, updateFitBallValveCount, 40, '80px');
	
		e.preventDefault();
		$('#fittingsModalContent').modal({
			onShow: function (dialog) {
			    // increase the dimensions of the container
			    dialog.container.css({
					height: 170,
					width: 400
				});
			},
			onOpen: function (dialog) {
				dialog.overlay.fadeIn(1000);
				dialog.container.fadeIn(1000);
				dialog.data.fadeIn(1000);
			},
			onClose: function(dialog) {
				//FadeOut
				//dialog.data.fadeOut(1000);
				//dialog.container.fadeOut(1000);
				//dialog.overlay.fadeOut(1000);

				document.getElementById('energy_cost1').value = document.getElementById('kw_state').value;
				document.getElementById('energy_rate_value').value = document.getElementById('kw_state').value;
				document.getElementById('selected_state').value = document.getElementById('kw_state').selectedIndex;
				document.ctl00.submit();
			}
		});
	});

	$('a#showWizard').click(function (e) {
		//Preload data for Wizard
		document.getElementById('kw_state_wizard').selectedIndex = document.getElementById('selected_state').value;
		document.getElementById('energy_cost').value = document.getElementById('energy_rate_value').value;
		document.getElementById('wizard_pipe_diameter').selectedIndex = document.getElementById('pipe_diameter').selectedIndex;
		
		//Preload data for Wizard 1
		createSpinControl('spinWizTargetGPHContainer', 'wiz_target_gph', 0, 9000, 100, document.getElementById('target_gph').value, updateTargetGPHSpin, 75, '125px');

		//Preload data for Wizard 2
		createSpinControl('spinWizStaticHeadContainer', 'wiz_static_head', 0, 100, 1, document.getElementById('static_head').value, updateStaticHeadSpin, 75, '125px');
		createSpinControl('spinWizPHeadContainer', 'wiz_pressure_head', 0, 20, 0.2, document.getElementById('pressure_head').value, updatePHeadSpin, 75, '125px');
		
		//Preload data for Wizard 3
		createSpinControl('spinWizLengthContainer', 'wiz_pipe_length', 0, 400, 5, document.getElementById('length').value, updatePipeLengthSpin, 75, '125px');
		
		e.preventDefault();
		$('#Wizard_1').modal({
			onShow: function (dialog) {
			    // increase the dimensions of the container
			    dialog.container.css({height: 350});
			},
			onOpen: function (dialog) {
				dialog.overlay.fadeIn(1000);
				dialog.container.fadeIn(1000);
				dialog.data.fadeIn(1000);
			}
		});
	});

	$('a#stateSelection').click(function (e) {
		//Preload data for State KW Selection
		document.getElementById('kw_state').selectedIndex = document.getElementById('selected_state').value;
		document.getElementById('energy_cost1').value = document.getElementById('energy_rate_value').value;
	
		e.preventDefault();
		$('#stateKWModalContent').modal({
			onShow: function (dialog) {
			    // increase the dimensions of the container
			    dialog.container.css({
					height: 100,
					width: 400
				});
			},
			onOpen: function (dialog) {
				dialog.overlay.fadeIn(1000);
				dialog.container.fadeIn(1000);
				dialog.data.fadeIn(1000);
			},
			onClose: function(dialog) {
				//FadeOut
				//dialog.data.fadeOut(1000);
				//dialog.container.fadeOut(1000);
				//dialog.overlay.fadeOut(1000);

				document.ctl00.submit();
			}
		});
	});

	$('input#wiz1Next').click(function () {
		$.modal.close();
		$('#Wizard_2').modal({
			onShow: function (dialog) {
			    // increase the dimensions of the container
			    dialog.container.css({height: 250});
			}
		});
	});
	
	$('input#wiz2Next').click(function () {
		$.modal.close();
		$('#Wizard_3').modal({
			onShow: function (dialog) {
			    // increase the dimensions of the container
			    dialog.container.css({height: 300, width:400});
			}
		});
	});
	
	$('input#wiz2Previous').click(function () {
		$.modal.close();
		$('#Wizard_1').modal({
			onShow: function (dialog) {
			    // increase the dimensions of the container
			    dialog.container.css({height: 350});
			}
		});
	});
	
	$('input#wiz3Finish').click(function () {
		$.modal.close();
		document.ctl00.submit();
	});
	
	$('input#wiz3Previous').click(function () {
		$.modal.close();
		$('#Wizard_2').modal({
			onShow: function (dialog) {
			    // increase the dimensions of the container
			    dialog.container.css({height: 250});
			}
		});
	});
	
	$('select#wizard_pipe_diameter').change(function () {
		document.getElementById('pipe_diameter').selectedIndex = document.getElementById('wizard_pipe_diameter').selectedIndex;
	});
	
	$('select#kw_state_wizard').change(function () {
		//updateKWState();
		document.getElementById('energy_cost').value = document.getElementById('kw_state_wizard').value;
		document.getElementById('energy_rate_value').value = document.getElementById('kw_state_wizard').value;
		document.getElementById('selected_state').value = document.getElementById('kw_state_wizard').selectedIndex;
	});
	
	$('select#kw_state').change(function () {
		//updateKWState();
		document.getElementById('energy_cost1').value = document.getElementById('kw_state').value;
		document.getElementById('energy_rate_value').value = document.getElementById('kw_state').value;
		document.getElementById('selected_state').value = document.getElementById('kw_state').selectedIndex;
	});
	
});
