	$("document").ready(function() {
		
		/* Initial setup */
		$("#gp_dropdown_container").hide();
		$("#og_1_dropdown_container").hide();
		$("#og_2_dropdown_container").hide();
		$("#gp_form")[0].reset();
		$("#og_1_form")[0].reset();
		$("#og_2_form")[0].reset();
		
		/* get status by AJAX and preselect options */
		
		$.get("/includes/quotebox_ajax.php",function(data) {

			if (data==1) {
				showbox1();
				}
			else if (data==2) {
				showbox2();
				}
			else if (data==3) {
				$("#og_1_dropdown option[value=1]").attr("selected","selected");
				showbox2();
				set_1(1);
				}
			else if (data==4) {
				$("#og_1_dropdown option[value=2]").attr("selected","selected");
				showbox2();
				set_1(2);
				}
			});
		
		/* Events */
		
		
		// Button clicks
		
		$("#gp").click(function() {
			setstate(1);
			showbox1();
			});
		
		$("#og").click(function() {
			setstate(2);
			showbox2();
			});
			
		// Guide price form
			
		$("#gp_dropdown").change(function() {
			set_gp($(this).val());
			});

		$("#gp_form").submit(function() {
			set_gp($("#gp_dropdown").val());
			return false;
			});

		// Online quote form
						
		$("#og_1_dropdown").change(function() {
			rebuild();
			set_1($(this).val());
			});
			
		$("#og_1_form").submit(function() {
			rebuild();
			set_1($("#og_1_dropdown").val());
			return false;
			});
							
		$("#og_2_dropdown").change(function() {
			set_2($(this).val());
			});
			
		$("#og_2_form").submit(function() {
			set_2($("#og_2_dropdown").val());
			return false;
			});
				
				
		/* Functions */
		
		/* Handle Guide Price stage */
		function set_gp(sel) {
			if (sel=="1") window.location="/our-services/moving-within-the-uk/costs/";
			else if (sel=="2") window.location="/our-services/moving-overseas/costs/";
			}
			
		/* Show Online Quote first stage */
		function showbox1() {
			$("#gp_dropdown_container").slideDown();
			$("#og_1_dropdown_container").hide();
			$("#og_2_dropdown_container").hide();
			}

		/* Show Online Quote second stage */
		function showbox2() {
			$("#gp_dropdown_container").hide();
			$("#og_1_dropdown_container").slideDown();
			$("#og_2_dropdown_container").slideUp();
			rebuild();
			}
			
		function rebuild() {
			/* Rebuild Online Quote second stage list */
			$("#og_2_dropdown option").remove();
			$("#og_2_dropdown").append('<option value="" selected>Choose options here</option>');
			$("#og_2_dropdown").append('<option value="1">UK</option>');
			$("#og_2_dropdown").append('<option value="2">Overseas</option>');
			$("#og_2_dropdown").append('<option value="3">UK/Europe</option>');
			$("#og_2_dropdown").append('<option value="4">Overseas</option>');	
			}

		/* Handle Online Quote second stage */
		function set_1(f) {
			if (f=="") return;
			
			$("#og_2_dropdown_container").slideDown();
			
			/* Remove unnecessary items from dropdown */
			
			if (f==1) {
				$("#og_2_dropdown option[value=3]").remove();
				$("#og_2_dropdown option[value=4]").remove();			
				setstate(3);			
			
				}
			else {
				$("#og_2_dropdown option[value=1]").remove();
				$("#og_2_dropdown option[value=2]").remove();
				setstate(4);
				}

			}
		
		/* Redirect following selection of the Online Quote section */
		function set_2(sel) {
			if (sel=="1" || sel=="3") window.location="/quotation";
			else if (sel=="2") window.location="/our-services/moving-overseas/make-an-enquiry/indexoverseas.html";
			else if (sel=="4") window.location="/1stopship";
			}

		/* Store the current state value (AJAX to Session) */
		function setstate(val) {
			$.get("/includes/quotebox_ajax.php?setstate="+val);
			}
			
		});

