$('document').ready(function() {
	$('#popup').each(function() {
		var element = $(this);
		element
			.css('position','absolute')
			.css('left',(($(window).width()-element.outerWidth(true))/2)+'px')
			.appendTo($(document.body))
			.hide();
	})
});

function popupSubmit() {
	$('#popupErrors').text('');
	var data = 'process='+$('#popup').attr('process');
	$('#popup input').each(function() {
		var element = this;
		var name = null;
		var value = null;
		if(element.type.toLowerCase()=='checkbox') {
			if(element.checked) {
				name = element.name;
				value = element.value;
			}
		} else {
			name = element.name;
			value = element.value;
		}
		if(name!=null) {
			if(data!='') data += '&';
			data += escape(name)+'='+escape(value);
		}
	});
	$.ajax({
		type: 'POST',
		url: window.location,
		data: data,
		dataType: "json",
		success: function(data) {
			if(data.error) {
				var popupError = $('#popupErrors');
				for(var i=0;i<data.error.length;i++) {
					$('<div>')
						.text(data.error[i])
						.appendTo(popupError);
				}
			} else if(data.redirect) {
				$('#popup').hide();
				window.location = data.redirect;
			}
		}
	});
	return false;
}

function showPopup(process) {
	switch(process) {
		case 'whitePaper':
			var image = '/images/whitepaper.png';
			var main = 'FREE White Paper:';
			var sub = 'Automating the Timesheet Process for the Staffing Industry';
			var italic = 'Key Considerations for Adoption and Vendor Selection';
			break;
		case 'brochure':
			var image = '/images/pdf.png';
			var main = 'Download the Brochure';
			var sub = '';
			var italic = '';
			break;
	}
	window.scroll(0,0);
	var popup = $('#popup');
	$('#popupImage').attr('src',image);
	$('#popupMain').text(main);
	$('#popupSub').text(sub);
	$('#popupItalics').text(italic);
	popup.attr('process',process);
	$('#popupErrors').text('');
	popup.show();
}
