var receiveEmails = "yes";

$(document).ready(function () {
	
	$("#submit").click(function() {
	goodfile=false;
	goodmail=false;
	str = $("#upload").val()
	len = $("#upload").val().length;
	ext = str.substring(len-4,len);
	
	if(ext == ".jpg" || ext == ".JPG" || ext == ".png" || ext == ".PNG" || ext == ".gif" || ext == ".GIF" || ext == ".bmp" || ext == ".BMP" ) {
		goodfile=true;	
		$("#imageBad").hide();	
	} else {
		if(str!="") {
			$("#imageBad").show();
		}
	}
	
	goode = document.getElementById("senderEmail").value.indexOf("@");
	if(goode<1) {
		goodmail=false;
		$("#emailRequired").show();	
	} else {
		goodmail=true;
		$("#emailRequired").hide();	
	}
	if(str=="") {
		$("#needImage").show();	
	}else {
		$("#needImage").hide();	
	}
	
	if(goodmail ==true && goodfile==true) {
		
		n = urlencode(document.getElementById("senderName").value);
		e = urlencode(document.getElementById("senderEmail").value);
		
		if(receiveEmails) {
				$.ajax({
				method: "get",url: "inc/db_send_contact.php",data: "c=upload&n="+n+"&e="+e+"&h=&r='yes'",
					success: function(html) {
						//
					}
				});
			}
		
		document.uploadEnergy.submit();
		$("#formholder").html("<p class=\"blue centered\" style=\"padding-top:70px;line-height:22px;\">IT MAY TAKE US A FEW DAYS TO REVIEW YOUR PHOTO.<br /> <span style=\"color:#aaaaaa;\">UNTIL THEN, MIGHT WE SUGGEST YOU QUENCH YOUR EXCITEMENT WITH A BOTTLE OF AVITAE?</span><br /><img src=\"images/avitae_1c.png\" width=\"107\" height=\"85\"></p>");
	}
	
	});
	
	$(".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');
}