var receiveEmails = "yes";

$(document).ready(function () {
	$('a.basic').click(function (e) {
		e.preventDefault();
		$('#basicModalContent').modal();
	});
	
	$("#submit").click(function() {
		r = receiveEmails;
		//alert(r)
		goodmail = false;
		
		goode = document.getElementById("senderEmail").value.indexOf("@");
		if(goode>1) {
			goodmail=true;
		}
		if(r=="yes" && goodmail==false) {
			$(".emailnote").show();
		} 
		if(r=="no" || goodmail==true) {
			$(".emailnote").hide();
		}
		
		n = urlencode(document.getElementById("senderName").value);
		e = urlencode(document.getElementById("senderEmail").value);
		c = urlencode(document.getElementById("senderComments").value);
		h = urlencode(document.getElementById("yaHear").value);
		
		if(goodmail == false) {
			$("#email").show();	
		} else {
			$("#email").hide();	
			//alert('good');
		}
		if(c.length > 0){
			$("#commentsNote").hide()
		}
		
		if(c.length > 0){
			//alert(n+" "+e+" "+c+" "+h+" emails:"+receiveEmails)
			$.ajax({
			method: "get",url: "inc/db_send_contact.php",data: "c="+c+"&n="+n+"&e="+e+"&h="+h+"&r="+r,
				success: function(html) {
				$("#formholder").html(html);
	 			}
 			});
		} 
		else {
			$("#commentsNote").show();
		}
	});
	
	$(".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');
}