$(document).ready(function(){
	jQuery.validator.addMethod("yourName", function(value, element) {
    	return this.optional(element) || value.match(/^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/);
	});
	
	$(".apply-now").click(function(){		
		var eml = $(this).attr("id");
		var jobTitle = $(this).closest(".sponsored-listings").find(".job-title").text();
		var company = $(this).closest(".sponsored-listings").find(".company").text();
		var location = $(this).closest(".sponsored-listings").find(".location").text();
		$("#blind").css({"opacity": 0.8}).fadeIn(750);
		$("#apply-now").append('<div class="close"><span style="color: #ff0000">Close</span> [<span style="color: #ff0000">x</span>]</div><form id="applyNow" enctype="multipart/form-data" method="post" action="apply-now.php"><input type="hidden" name="eml" value="'+eml+'" /><div id="head"><ul><li class="jT">You are Applying for: '+jobTitle+'<input type="hidden" name="jobTitle" value="'+jobTitle+'" /></li><li class="aC">'+company+'<input type="hidden" name="company" value="'+company+'" /></li><li class="aL">'+location+'<input type="hidden" name="location" value="'+location+'" /></li></ul></div><div id="apply-left"><h3>Step 1:</h3><span style="color: #F00">*</span> Indicates required fields<br /><br /><dl><dt><span style="color: #F00">*</span> <span style="color: #000">Your Name:</span></dt><dd><input name="name" type="text" style="width: 240px;" /></dd><dt><span style="color: #F00">*</span> <span style="color: #000">Your Email:</span></dt><dd><input name="email" type="text" style="width: 240px;" /></dd></dl><b>Privacy notice:</b> Your name, your resume, and the email address you entered will be saved to comply with Day Trader Jobs&#039;s internal reporting needs and to allow us to contact you regarding your application, if necessary. We will not disclose this information to anyone other than this employer.</div><div id="apply-right"><h3>Step 2:</h3>Add your own cover letter or note below. To send your resume without a note, leave this box empty.<br /><br /><b>Note:</b> Use plain text only. HTML is not supported.<br /><textarea name="note" style="width: 540px; height: 100px; display: block; margin-top: 4px; margin-bottom:10px;"></textarea><h3>Step 3:</h3><span style="line-height: 16px">Attach Resume:</span><br /><div id="selectFile"><input type="text" id="fileName" style="width: 195px;" readonly="readonly"><input type="button" value="Browse" class="file_input_button" /><input type="file" name="resume" class="file_input_hidden" /></div><b>Note:</b> Microsoft Word files (97 and newer), 500 KB or smaller.<br /><br /><input name="accept" type="checkbox" value="Yes" /> <span style="color: #F00">*</span> I have read, and I accept, Day Trader Jobs&#039;s <a href="http://www.daytraderjobs.com/terms-of-use.php">Terms of Use</a> and <a href="http://www.daytraderjobs.com/privacy-policy.php">Privacy Policy</a><br /><br /><div class="cancel">Cancel</div><input id="submitButton" type="submit" value="Apply Now" style="float: right; margin-right: 20px;"></div></form>').slideDown(1000);
		
		$('.close').click(function(){
			$("#apply-now").slideUp(750);
			$("#apply-now").children().remove();
			$("#blind").fadeOut(1000);
		});
		
		$('#submitButton').addClass('submitDisabled').attr('disabled', 'disabled');
		
		$('.file_input_hidden').change(function(){
			var fileName = $(this).val();
			$("#fileName").attr({ value: fileName });
		});
		
		$('input:checkbox').click(function() {
			var accepted = $('input:checkbox:checked');
			if (accepted.length) {
				$('#submitButton').removeClass('submitDisabled').removeAttr('disabled');
			} else {
				$('#submitButton').addClass('submitDisabled').attr('disabled', 'disabled');
			}
		});
		
		$('.cancel').click(function(){
			$("#apply-now").slideUp(750);
			$("#apply-now").children().remove();
			$("#blind").fadeOut(1000);
		});
		
		$("#applyNow").validate({
			rules: {
				name: {
					required: true,
					yourName: true
				},
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				name: "Please enter your full name.",
				email: "Please enter a valid email address."
			},
			submitHandler: function(form){
				$(form).ajaxSubmit({
					success: function(){
						$('#applyNow').fadeOut(250);
						$('#apply-now').append('<div id="thank-you">Your application was sent successfully.<br /><br />Please expect a response within the next two business days.<br /><br /></div>').fadeIn(1000);
					}
				});
			}
		});
	});
	
	$(".applyNow").click(function(){		
		var eml = $(this).attr("id");
		var jobTitle = $(this).closest(".listings").find(".job-title").text();
		var company = $(this).closest(".listings").find(".company").text();
		var location = $(this).closest(".listings").find(".location").text();
		$("#blind").css({"opacity": 0.8}).fadeIn(750);
		$("#apply-now").append('<div class="close"><span style="color: #ff0000">Close</span> [<span style="color: #ff0000">x</span>]</div><form id="applyNow" enctype="multipart/form-data" method="post" action="apply-now.php"><input type="hidden" name="eml" value="'+eml+'" /><div id="head"><ul><li class="jT">You are Applying for: '+jobTitle+'<input type="hidden" name="jobTitle" value="'+jobTitle+'" /></li><li class="aC">'+company+'<input type="hidden" name="company" value="'+company+'" /></li><li class="aL">'+location+'<input type="hidden" name="location" value="'+location+'" /></li></ul></div><div id="apply-left"><h3>Step 1:</h3><span style="color: #F00">*</span> Indicates required fields<br /><br /><dl><dt><span style="color: #F00">*</span> <span style="color: #000">Your Name:</span></dt><dd><input name="name" type="text" style="width: 240px;" /></dd><dt><span style="color: #F00">*</span> <span style="color: #000">Your Email:</span></dt><dd><input name="email" type="text" style="width: 240px;" /></dd></dl><b>Privacy notice:</b> Your name, your resume, and the email address you entered will be saved to comply with Day Trader Jobs&#039;s internal reporting needs and to allow us to contact you regarding your application, if necessary. We will not disclose this information to anyone other than this employer.</div><div id="apply-right"><h3>Step 2:</h3>Add your own cover letter or note below. To send your resume without a note, leave this box empty.<br /><br /><b>Note:</b> Use plain text only. HTML is not supported.<br /><textarea name="note" style="width: 540px; height: 100px; display: block; margin-top: 4px; margin-bottom:10px;"></textarea><h3>Step 3:</h3><span style="line-height: 16px">Attach Resume:</span><br /><div id="selectFile"><input type="text" id="fileName" style="width: 195px;" readonly="readonly"><input type="button" value="Browse" class="file_input_button" /><input type="file" name="resume" class="file_input_hidden" /></div><b>Note:</b> Microsoft Word files (97 and newer), 500 KB or smaller.<br /><br /><input name="accept" type="checkbox" value="Yes" /> <span style="color: #F00">*</span> I have read, and I accept, Day Trader Jobs&#039;s <a href="http://www.daytraderjobs.com/terms-of-use.php">Terms of Use</a> and <a href="http://www.daytraderjobs.com/privacy-policy.php">Privacy Policy</a><br /><br /><div class="cancel">Cancel</div><input id="submitButton" type="submit" value="Apply Now" style="float: right; margin-right: 20px;"></div></form>').slideDown(1000);
		
		$('.close').click(function(){
			$("#apply-now").slideUp(750);
			$("#apply-now").children().remove();
			$("#blind").fadeOut(1000);
		});
		
		$('#submitButton').addClass('submitDisabled').attr('disabled', 'disabled');
		
		$('.file_input_hidden').change(function(){
			var fileName = $(this).val();
			$("#fileName").attr({ value: fileName });
		});
		
		$('input:checkbox').click(function() {
			var accepted = $('input:checkbox:checked');
			if (accepted.length) {
				$('#submitButton').removeClass('submitDisabled').removeAttr('disabled');
			} else {
				$('#submitButton').addClass('submitDisabled').attr('disabled', 'disabled');
			}
		});
		
		$('.cancel').click(function(){
			$("#apply-now").slideUp(750);
			$("#apply-now").children().remove();
			$("#blind").fadeOut(1000);
		});
		
		$("#applyNow").validate({
			rules: {
				name: {
					required: true,
					yourName: true
				},
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				name: "Please enter your full name.",
				email: "Please enter a valid email address."
			},
			submitHandler: function(form){
				$(form).ajaxSubmit({
					success: function(){
						$('#applyNow').fadeOut(250);
						$('#apply-now').append('<div id="thank-you">Your application was sent successfully.<br /><br />Please expect a response within the next two business days via email or telephone.<br /><br />Thank you.<br /></div>').fadeIn(1000);
					}
				});
			}
		});
	});
});
