$(document).ready(function(){
	$("select.select_states").change(function(){
		var intStateID = $(this).val();
		ResetCombos(1);
		if (intStateID != '0')
			GetServiceTypes(intStateID);
	});

	$("select#service_type").change(function(){
		var intTypeID = $(this).val();
		ResetCombos(2);
		if (intTypeID != '0')
			GetServices(intTypeID);
	});

	$("select#service").change(function(){
		var intServiceID = $(this).val();
		ResetCombos(3);
		if (intServiceID != '0')
			GetUtilities(intServiceID);
	});

	$("select#utility").change(function(){
		$("input#promo_code").removeAttr('disabled');
		$("form#frm_qoute input[type='submit']").removeAttr('disabled');
	});
	
	$("input#promo_code").keydown(function(){
		$("form#frm_qoute input[type='submit']").removeAttr('disabled');
	});
});

function ResetCombos(intStep)
{
	$("input#promo_code").attr('disabled','true');
	$("form#frm_qoute input[type='submit']").attr('disabled','true');
	if(intStep <= 1)
		$("select#service_type").html("<option value='0'>Select the state first</option>");

	if(intStep <= 2)
		$("select#service").html("<option value='0'>Select the service type first</option>");

	if(intStep <= 3)
		$("select#utility").html("<option value='0'>Select the service first</option>");
}

function GetServiceTypes(intStateID)
{
	ShowOverlay();
	ShowLoading();
	var strURL = "pages/process_form.page.php";
	var strData = "action=service_types&sid="+intStateID;

	$.ajax({
		type: "POST",
		url: strURL,
		data: strData,
		success: function(strResult){
			var arrPieces = strResult.split("</option><option");
			var intItemNumbers = arrPieces.length - 1;
			$("#service_type").html(strResult);
			if(intItemNumbers == 1)
			{
				$("#service_type option:eq(1)").attr("selected","selected");
				$("#service_type").change();
			}
			HideLoading();
			HideOverlay();
		}
	});
}

function GetServices(intTypeID)
{
	ShowOverlay();
	ShowLoading();
	var strURL = "pages/process_form.page.php";
	var strData = "action=services&tid="+intTypeID;

	$.ajax({
		type: "POST",
		url: strURL,
		data: strData,
		success: function(strResult){
			var arrPieces = strResult.split("</option><option");
			var intItemNumbers = arrPieces.length - 1;
			$("#service").html(strResult);
			if(intItemNumbers == 1)
			{
				$("#service option:eq(1)").attr("selected","selected");
				$("#service").change();
			}
			HideLoading();
			HideOverlay();
		}
	});
}

function GetUtilities(intServiceID)
{
	ShowOverlay();
	ShowLoading();
	var strURL = "pages/process_form.page.php";
	var strData = "action=utils&sid="+intServiceID;

	$.ajax({
		type: "POST",
		url: strURL,
		data: strData,
		success: function(strResult){
			var arrPieces = strResult.split("</option><option");
			var intItemNumbers = arrPieces.length - 1;
			$("#utility").html(strResult);
			if(intItemNumbers == 1)
			{
				$("#utility option:eq(1)").attr("selected","selected");
				$("#utility").change();
				//$("#frm_qoute").submit();
			}
			HideLoading();
			HideOverlay();
		}
	});
}