$(document).ready(function() {

	$('#as_above').change(function(){
		if($(this).val() == 1){
			$('#ship_street').val($('#bill_street').val());
			$('#ship_city').val($('#bill_city').val());
			$('#ship_state').val($('#bill_state').val());
			$('#ship_postcode').val($('#bill_postcode').val());
			$(this).val('0');
		}else if($(this).val() == 0){
			$(this).val('1');
		}
	});
	
	$("input[type=radio]").click(function() {
		if ($(this).attr("name")=="paymethod") {
			if ($(this).val()=="Credit Card") $("#carddetails").show();
			else $("#carddetails").hide();
		}
	});

	$("input[type=checkbox]").change(function() {
		if ($(this).attr("name")=="shippingbox" && $(this).attr("checked")==true) {
			$("#ship_street").val($("#bill_street").val());
			$("#ship_city").val($("#bill_city").val());
			$("#ship_state").val($("#bill_state").val());
			$("#ship_postcode").val($("#bill_postcode").val());
			$("#ship_country").val($("#bill_country").val());
		}
	});

	$("table.cart").find("td").each(function(i) {
		if ($(this).text().substring(0,1)=="$" || $(this).text().substring(0,2)=="-$") 
		{
			$(this).css("text-align", "right");
			$(this).css("font-size", "14px");
		}
	});

	
	// Dynamic Category and Subcategory filters
	/*$("#catFilter").change(function(){
		$("#discountsPane").html("Select a subcategory");
		$.getJSON("./admin/subFilterSelect.php",{ cat_id: $(this).val()}, function(j){

		var myOptions = '';

		for (var i = 0; i < j.length; i++) {
			try {
				myOptions += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			} catch (exc) {
				//No one likes IE6
			}
		}

		$("#subFilter").html(myOptions);
		});
	});

   $("#subFilter").change(function () {
	   if ( $("#productsPane") )
	   {
			///.....
			$("#productsPane").load("./admin/listProducts.php", { sub_id: $(this).val() }, function() { 
				$("a>img[src=images/icons/application_delete.png]").each(deleteButtonHover);
				$("a>img[src=images/icons/application_edit.png]").each(editButtonHover);

				$("a.delete").click(attachDeleteLinks);
			});
	   }
   });*/
   
	// Dynamic Category and Subcategory filters
	$("#catFilter").change(function(){
		$("#discountsPane").html("Select a subcategory");
		$.getJSON("./subFilterSelect.php",{ cat_id: $(this).val()}, function(j){

		var myOptions = '';

		for (var i = 0; i < j.length; i++) {
			try {
				myOptions += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			} catch (exc) {
				//No one likes IE6
			}
		}

		$("#subFilter").html(myOptions);
		});
	});

	// Google address validation
	$("#stepOneSubmitButton").click(function() {
		if ($("#delivery_method").val()=="-1") {
			alert("Please select a delivery option.");
			return false;
		}
		var doSubmit = false;

		var address = "";
		address = document.custForm.ship_street.value + " " + document.custForm.ship_city.value + " " + document.custForm.ship_state.value + " " + document.custForm.ship_country.value + " " + document.custForm.ship_postcode.value;
	
		 if(geocoder==null) {
			initialize();
		 }
		 
		 geocoder.getLatLng(address, function(point) {
			  if (point) {
				delivery = new GLatLng(point.lat(), point.lng());
				//alert("Distance (m): "+delivery.distanceFrom(base));
				dist = delivery.distanceFrom(base)/1000;
				document.custForm.distance.value = Math.round(dist);
				$("#custForm").submit();
			  } else {
					// Now let's try finding the address without the street (so city, state, postcode)
					address = document.custForm.ship_city.value + " " + document.custForm.ship_state.value + " " + document.custForm.ship_country.value + " " + document.custForm.ship_postcode.value;
					geocoder.getLatLng(address, function(point) {
					  if (point) {
						delivery = new GLatLng(point.lat(), point.lng());
						dist = delivery.distanceFrom(base)/1000;
						document.custForm.distance.value = Math.round(dist);
					  } else {			// Really couldn't work it out...
						  dist = -1;
						  document.custForm.distance.value = dist;
					  }
					});
			  }
		});

	});

	var cartQtyChangedFlag = false;

	$(".cartQty").change(function () {
		cartQtyChangedFlag=true;
	});

		// If we go to a link, check that we didn't just change the qty in cart
		$("a").click( function() {
			if (cartQtyChangedFlag) { 
				var decision = confirm('You have not updated your quantity changes. Would you like to update now?');
				if(decision) {	// Would like to save
					$("#update").click();
					return false;
				} 
				cartQtyChangedFlag = false;
			}
		});
		
		$("#update").click( function() {
			$("#updateForm").submit();
		});

		$("#searchButton").click( function() {
			$("#searchForm").submit();
		});


		$("#shopping").click( function() {
			if (cartQtyChangedFlag) { 
				var decision = confirm('You have not updated your quantity changes. Would you like to update now?');

				if(decision) {	// Would like to save
					$("#update").click();
					cartQtyChangedFlag = false;
					return false;
				} 
				cartQtyChangedFlag = false;
				
			}
			window.location='Hay/';
		});

		$("#checkout").click( function() {
			if (cartQtyChangedFlag) { 
				var decision = confirm('You have not updated your quantity changes. Would you like to update now?');

				if(decision) {	// Would like to save
					$("#update").click();
					cartQtyChangedFlag = false;
					return false;
				} 
				cartQtyChangedFlag = false;
				
			}
			if($("#valid").val()=="false")
				window.location='cart.php?msg=qty';
			else
				window.location='order.php';
		});

		$("#quote").change( function() {
			$("#stepOneSubmitButton").toggle();
		});

		$("#registerButton").click( function(e) {
			// Calculate distance and then submit form
			e.preventDefault();
			getDist();
			setTimeout("$(\"#custForm\").submit();",1500);
						
		});

		$('#s1').cycle({ 
			fx:    'fade', 
			speed:  2500,
			timeout: 5000,
			delay: -2000,
			random:  1
		 });
		
		$('#s2').cycle({ 
			fx:    'fade', 
			speed:  2500,
			timeout: 5000, 
			random:  1,
			delay: -1000
		 });
		
		$('#s3').cycle({ 
			fx:    'fade', 
			speed:  2500,
			timeout: 5000,
			random:  1,
			delay: 0
		 });
});
