// create download pop-up dialog
$(document).ready(function () {
	$("#download-dialog").html('<div id="download-text"></div><div id="dialog-content" class="dialog-content"></div>').dialog({
		autoOpen: false,
		modal: true,
		width: 500,
		title: 'Download Publication',
		buttons: {
			"Request File": function(){
				//var file = 'proxy.php?file=file_request.php?path=';
				var file='/pubs/file_request.php';
				var email = $('input:text[name=email]').val();
				var serial = $('input:text[name=serial]').val()
				var path = '?' + 'serial=' + serial + '&email=' + email;
				var url = file + path; //encodeURIComponent(path);
				$("#downloadForm").validate();
				if ($("#downloadForm").valid()){
					$('.ui-dialog-buttonpane button:first-child').hide();
					$('.ui-dialog-buttonpane button:last-child').hide();
					sendEmail(url);
					$("#dialog-content").prepend('<div id="wait-animation"><img src="/pubs/wait-animation.gif" alt="wait animation" /></div>');
				};
			},
			"Close": function(){
				$(this).dialog("close");
			}
		}
	});
});

function fileRequest(serial,title){
	// open request dialog box
	var newHtml = "<p><span class='bold'>" + title + "</span></p>";
	var dialogContent = '<p>Please enter your e-mail address below and we will send you a copy of this publication.</p>' +
			'<form name="downloadForm" id="downloadForm" action="proxy.php" method="get">' +
			'<label for="email">E-mail Address: </label><input type="text" name="email" size="30"><br />' +
			'<input type="text" name="serial" style="display:none">' +
			'<p class="disclaimer"><input type="checkbox" name="agree">' +
			'<label for="agree">I agree to only use this file for my personal use. I understand that this copy may not be further distributed or reproduced without the approval of the publisher.</label>' +
			'</p></form>';
	$("#download-text").html(newHtml);
	$("#dialog-content").html(dialogContent);
	$('input:text[name=serial]').val(serial);
	$("#download-dialog").dialog('open');
	//$('.ui-dialog-buttonpane button:first-child').hide();
	$('.ui-dialog-buttonpane button:first-child').show();
	$("#downloadForm").validate({
		rules: {
			email: {
				required: true,
				email: true
			},
			agree: {
				required: true
			}
		},
		messages: {
			agree: " *Agreement to the usage guidelines is required<br />",
			email: "<br />*Please enter a valid e-mail address"
		}
	});
}

//Send an XMLHTTP request:
function sendEmail(url) {
	xmlHTTP = getXMLHTTPObject();

	if (xmlHTTP == null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	xmlHTTP.onreadystatechange = requestStateChanged;
	xmlHTTP.open("GET", url, true);
	xmlHTTP.send(null);
}

// ------------------------------------------------------------------

// Update an HTML element (with id = "includerefs") with the response text returned by the XMLHTTP request:
function requestStateChanged() {

	if (xmlHTTP.readyState == 4 || xmlHTTP.readyState == "complete") {
		var response = xmlHTTP.responseText;
		var check = response.search("<div id='response'");
		if (!response) {
			alert("There was an error")
		}
		else {
			//alert("Success!");
			if(check > -1){
				response = response.substr(check);
			};
			$("#dialog-content").html(response);
			/*$("#dialog-content").html("<div id='response'><p>The publication you requested has been e-mailed to you.</p>" +
		 	"<p>If you do not receive the file for some reason, please contact the <a href='mailto:breity@berkeley.edu'>webmaster</a> for assistance.</p>" +
		  	"<p>Thank you!</p></div>");*/
			$('.ui-dialog-buttonpane button:last-child').show();
			//$('#wait-animation').hide();
		};
	}

}