function getFileExtension(filename)
{
	var ext = /^.+\.([^.]+)jQuery/.exec(filename);
	return ext == null ? "" : ext[1];
}
var w4p_pdf = '';
function personalization_form () {
	jQuery('.update-preview').css('display','block');
	function update_preview () {
		jQuery('div.w4p-preview-button span.text').css('display', 'inline');
		jQuery('img.ajax-loader').css('display', 'inline');
		var update_preview_button = jQuery('button.update-preview').unbind('click').hide();
		
		page = 'page-1';
		//alert(jQuery('#product_addtocart_form').serialize());
		jQuery.ajax({
			url: preview_controller_url,
			type: 'POST',
			data: jQuery('#product_addtocart_form').serialize() + '&w4p-From=' + page.split('-')[1],
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				jQuery('div.w4p-preview-button span.text, img.ajax-loader').css('display', 'none');
				jQuery(update_preview_button).bind('click', update_preview).show();
				alert(preview_generation_response_error_text + textStatus); 
			},
			dataType:'json',
			success: function (data, textStatus) {
				if (!data || data.error) return ;
				//w4p_img = data.img;
				w4p_img = data.image;
				if (!data.image) w4p_img = data.img;
				w4p_pdf = data.final;
				if (w4p_img.substr(0, 7) != 'http://') {
				  alert(preview_generation_error_text);
				} else if (getFileExtension(w4p_img) != 'pdf') {
					jQuery('#image').attr('src', w4p_img+ '?' + Math.random());
					jQuery('#image').attr('alt', 'Click to view final pdf');
					jQuery('#image').attr('title', 'Click to view final pdf');
					
				}else{
					//alert(data);
					//window.open (data,"web4print preview"); 
					//jQuery('#image').css('display','none');
					jQuery('#pdfpreview').html("");
					web4printPDF = new PDFObject({
						url: data,
						id: "myPDF",
						width: "680px",
						height: "450px",
						pdfOpenParams: {
							navpanes: 0,
							statusbar: 0,
							view: "FitH",
							pagemode: "thumbs"
						}
					}).embed("pdfpreview");
					if (web4printPDF){
						jQuery('#pdfpreview').css('display','block');
					}else{
						window.open (data,"web4print preview"); 
					}
				}
				jQuery('div.w4p-preview-button span.text, img.ajax-loader').css('display', 'none');
				jQuery(update_preview_button).bind('click', update_preview).show(); 
			}
		});
		return false;
	}
	jQuery('.update-preview').css('display','block');
	jQuery('button.update-preview').click(update_preview);
	jQuery("#image").click(function() {
		if (w4p_pdf){
			window.open (w4p_pdf,"web4print preview"); 
		}
	});
}
