var locURL = "../ajax-html/";
var loadingImg = "../images/loadingAnimation.gif";
$(document).ready(function(){
	$('#promo').change(function(){
		$.ajax({
			type: "POST",
			cache: false,
			url: locURL + "check_promo_code.php",
			data: "promo=" + $('#promo').val() + "&membership_type=" + $('#membership_type').val(),
			success: function(msg){
				if( msg == 'nogood' ){
					alert( 'Sorry Promo Code Not Accepted!' );
					$('#promo').val('');
				}else if( msg != 'nogood' ){
					alert( 'Promo Code Accepted!' );
					$('#promo').attr('readonly','true');
					update_price( msg );
				}
			}
		});
	});
	function update_price( discount ){
		// change price
	//	var price = $('#price').val();
	//	var newTotal = parseFloat( price - discount );
		$('#price').html('$'+discount+' <span style="color:#F8DD2F;background-color:#CE3229;">(Promo Code Accepted)</span>');
	}
	// load specialty-page1 this will be first choice
	$("#spec-div").load(locURL + 'specialty-page1.php');
	$("#spec-div").ajaxComplete(function(request,settings){
		$("#spec-interest").click(function(){
			interestClick();
			return(false);
		});
		$("#spec-location").click(function(){
			$("#spec-div").html('');
			locationSelect();
			return(false);
		});
	});
});
function interestClick(){
	$("#spec-div").html('');
	$.ajax({
		url: locURL + "get-int-name.php",
		data: "get_select=amenity",
		cache: false,
		success: function(html){
			$("#spec-div").html('');
			$("#spec-div").append(html);
			$("#interest-add").click(function(){
				interestAdd();
				return(false);
			});
			$("#region").change(function(){
				regionChange( false );
				return(false);
			});
		}
	});
	return(false);
}
function interestAdd(){
	if( $("#interest").val() ){
		if( $("#country").val() ){
			// if there's a country need to get that passed through
			$.ajax({
				type: "POST",
				url: locURL + "get-int-name.php",
				cache: false,
				data: "amenity_id="+$("#interest").val()+"&country_id="+$("#country").val(),
				success: function(html){
					$("#spec-list-div").append(html);
					$.ajax({
						url: locURL + "specialty-page1.php",
						cache: false,
						success: function(html){
							$("#spec-div").html(html);
						}
					});
				}
			});
			return(false);
		}
		else{
			$.ajax({
				type: "POST",
				url: locURL + "get-int-name.php",
				cache: false,
				data: "amenity_id="+$("#interest").val(),
				success: function(html){
					$("#spec-list-div").append(html);
					$.ajax({
						url: locURL + "specialty-page1.php",
						cache: false,
						success: function(html){
							$("#spec-div").html(html);
						}
					});
				}
			});
			return(false);
		}
	}
	return(false);
}
function locationSelect(){
	$.ajax({
		url: locURL + "get-int-name.php",
		data: "get_select=region",
		cache: false,
		success: function(html){
			$("#spec-div").html('');
			$("#spec-div").append(html);
			$("#region").change(function(){
				regionChange( true );
				return(false);
			});
		}
	});
	return(false);
}
function regionChange( loc ){
	var regionId = $("#region").val();
	$("#region").parent().html('');
	$.ajax({
		url: locURL + "get-int-name.php",
		data: "get_select=country&region_id=" + regionId,
		cache: false,
		success: function(html){
			$("#spec-div").append(html);
			if( loc == true ){
				$("#spec-div").append("<button id=\"location-add\">Add</button>");
			}
			$("#location-add").click(function(){
				locationAddClick();
				return(false);
			});
		}
	});
	return(false);
}
function locationAddClick(){
	var countryId = $("#country").val();
	$("#country").parent().html('');
	$.ajax({
		url: locURL + "get-int-name.php",
		data: "country_id=" + countryId,
		cache: false,
		success: function(html){
			$("#spec-list-div").append(html);
		}
	});
	$.ajax({
		url: locURL + "specialty-page1.php",
		cache: false,
		success: function(html){
			$("#spec-div").html('');
			$("#spec-div").html(html);
		}
	});
	return(false);
}

