// JavaScript Document
var displayForm	= false;
var id = 0;

// prepare the form when the DOM is ready 
jQuery(document).ready(function() {
	jQuery('a.confirm').click(function(e) {
		e.preventDefault();
		id = jQuery(this).attr('id').replace('link-','');

		confirm("Do you really want to remove this product?", function () {
			jQuery.ajax({
				type: 'get',
				url: '/store/library/delCart.php',
				data: 'ajax=1&delete=' + id,
				beforeSend: function() {
					//jQuery("#record-" + id).animate({'backgroundColor':'#fb6c6c'},300);
				},
				success: function() {
					window.location.href = 'cart.php';
					//jQuery("#record-" + id).remove();
					//getCartTotals();
				}
			});
		});
	});
});

function confirm(message, callback) {
	jQuery('#confirm').modal({
		closeHTML:"<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId:'confirm-overlay',
		containerId:'confirm-container', 
		onShow: function (dialog) {
			jQuery('.message', dialog.data[0]).append(message);

			// if the user clicks "yes"
			jQuery('.yes', dialog.data[0]).click(function () {
				// call the callback
				if (jQuery.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				jQuery.modal.close();
			});
		}
	});
}

jQuery(document).ready(function() {
	var options = { 
		target: '#resultTarget', // target identifies the element(s) to update with the server response 
		clearForm: false,		 // clear all form fields after successful submit
		url: 'process.php',		 // override for form's 'action' attribute  
		success: showResponse	 // post-submit callback 
	}; 

	// show a simple loading indicator
	var loader = jQuery('<div id="loader" style="background-color:#000000;color:#ffffff;padding:10;border: 1px solid #000;z-index: 99;">&nbsp;&nbsp;Processing...<br/><img src="/VPanel/images/loader.gif" alt="loading..." /></div>')
		.css({position: "absolute", top: "25em", left: "35em"})
		.appendTo("body")
		.hide();
	jQuery().ajaxStart(function() {
		loader.show();
	}).ajaxStop(function() {
		loader.hide();
	}).ajaxError(function(a, b, e) {
		throw e;
	});
	
	jQuery.validator.addMethod("pageRequired", function(value, element) {
		var $element = $(element)
		return !this.optional(element);
	}, jQuery.validator.messages.required)

	var v = jQuery("#cartForm").validate({
		errorClass: "warning",
		onkeyup: false,
		onblur: false,
		submitHandler: function(form) {
			jQuery("#resultTarget").empty();
			jQuery(form).ajaxSubmit(options);
		}
	});

}); 

// post-submit callback 
function showResponse(responseText, statusText)  { 
	if(!displayForm) {
		window.location.href = 'cart.php';
	}

	jQuery('#resultTarget').fadeIn('slow'); 
} 
