$(document).ready(function() {
	$("[name='frmContact'] input:not([type='submit']):not([type='image'])").focus(function() {
		$(this).parents("td").addClass("focus");
	});
	$("[name='frmContact'] select").focus(function() {
		$(this).parents("td").addClass("focus");
	});
	$("[name='frmContact'] textarea").focus(function() {
		$(this).parents("td").addClass("focus");
	});
	$("[name='frmContact'] :not([type='submit'])").blur(function() {
		$(this).parents("td").removeClass("focus");
	});
	
	$("[name='frmDownload'] input:not([type='submit']):not([type='image'])").focus(function() {
		$(this).parents("td").addClass("focus");
	});
	$("[name='frmDownload'] select").focus(function() {
		$(this).parents("td").addClass("focus");
	});
	$("[name='frmDownload'] textarea").focus(function() {
		$(this).parents("td").addClass("focus");
	});
	$("[name='frmDownload'] :not([type='submit'])").blur(function() {
		$(this).parents("td").removeClass("focus");
	});
	
	$("[name='frmDownload']").submit(function() {
		return false;
	});
	$("[name='frmDownload'] input[type='image']").click(function() {
//	$(".subSend").click(function() {
		download_file = download_file ? download_file : "";
		
		$.post('/includes/ajax/register-file-download.php',{file:download_file,fname:$("[name='frmDownload'] [name='name[txtFName]']").val(),lname:$("[name='frmDownload'] [name='name[txtLName]']").val(),email:$("[name='frmDownload'] [name='txtEMail']").val(),phone:$("[name='frmDownload'] [name='txtPhone']").val(),company:$("[name='frmDownload'] [name='txtCompany']").val()},function(xml) {
			errors = false;
			$("[name='frmDownload'] [name='frmDownload']").find(".error").remove();
			// see if there are any new errors
			$(xml).find("error").each(function() {
				errors = true;
				error_text = "<p class=\"error\">"+$(this).text()+"</p>";
				switch($(this).attr("element")) {
				case 'name':
					$("[name='frmDownload'] [name='name[txtFName]']").closest("table").find("label").after(error_text);
					break;
				case 'email':
					$("[name='frmDownload'] [name='txtEMail']").closest("table").find("label").after(error_text);
					break;
				default:
				}
			});

			if (!errors) {
				// success
				pageTracker._trackPageview('/whitepaper-download/');

				// output button to download file
				$("#download-form").html("<p>Thank You! Your download should start automatically. If the download didn't start automatically, <a target=\"_blank\" href=\"/includes/download-file.php?file="+download_file+"\">click here</a> to download the file now.</p>");
				
				day = new Date();
				id = day.getTime();
				eval("page"+id+"=window.open('/public/"+download_file+"','"+id+"','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=1680,height=1050');");
				
				// change download links to point directly to file
				$(".files .pdf").each(function() {
					file = $(this).find("[name='file']").val();
					$(this).find("a.thickbox").each(function() {
						$(this).attr("href","/includes/download-file.php?file="+file);
						// unbind thickbox event from this link
						$(this).unbind("click");
					})
				});
			}
		});
		return false;
	});
	
	$("ul.files li a").click(function() {
		download_file = $(this).siblings("[name='file']").val();
	})
	$(".btn.download").click(function() {
		download_file = $(this).siblings("[name='file']").val();
	});
	$("#continue-to-file").live("click",function() {
		// refresh page, and allow browser to follow link to file
		window.location.href = window.location.href;
	});
	
});