var receiveEmails = "yes";

$(document).ready(function () {
	$('a.basic').click(function (e) {
		e.preventDefault();
		$('#basicModalContent').modal();
	});
	
	$("#submit").click(function() {
		goodmail = false;
		goodzip = false;
		
		goode = document.getElementById("senderEmail").value.indexOf("@");
		if(goode<1) {
			goodmail=false;
			$("#emailRequired").show();	
		} else {
			goodmail=true;
			$("#emailRequired").hide();	
		}
		
		goodr = document.getElementById("senderEmailConfirm").value.indexOf("@");
		if(goodr<1) {
			$("#confirmRequired").show();	
		} else {
			$("#confirmRequired").hide();	
		}
		
		n = urlencode(document.getElementById("senderName").value);
		e = urlencode(document.getElementById("senderEmail").value);
		ec = urlencode(document.getElementById("senderEmailConfirm").value);
		z = urlencode(document.getElementById("zipCode").value);
		
		if(e != ec) {
			goodmail=false;
			$("#mismatch").show();
		} else {
			goodmail=true;
			$("#mismatch").hide();
		}
		
		if($("#zipCode").val().length != 5) {
			$("#zipcodeRequired").show();
		} else {
			$("#zipcodeRequired").hide();
			goodzip=true;
		}
		
		if(e == ec && goodmail == true && goodzip==true){
			$.ajax({
			method: "get",url: "inc/db_add_notify.php",data: "n="+n+"&e="+e+"&z="+z,
				success: function(html) {
					$("#formholder").html(html);
	 			}
 			});
			
			if(receiveEmails) {
				
				$.ajax({
				method: "get",url: "inc/db_send_contact.php",data: "c=notify&n="+n+"&e="+e+"&h=&r='yes'",
					success: function(html) {
						
					}
				});
			}
		}
	});
	
	$(".yes").click(function() {
		receiveEmails = "yes";
		emailOptions();
	});
	
	$(".no").click(function() {
		receiveEmails = "no";
		emailOptions();
	});
});

function emailOptions() {
	if(receiveEmails == "no") {
		$("#yesimg").attr({
			src: "images/yes_off.gif"
		});
		$("#noimg").attr({
			src: "images/no_on.gif"
		});
	} else {
		$("#yesimg").attr({
			src: "images/yes_on.gif"
		});
		$("#noimg").attr({
			src: "images/no_off.gif"
		});
	}
}

function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}