

Shop.Forms = new Class({
	initialize: function(parent) {
		this.shop = this.parent = parent;
	},

	login: function(response) {

		response = JSON.decode(response);
		if(!response.success) {
			window.shop.frame.showErrors(response.errors);
		}
		else {

			var c = new Chain();
			c.chain(function() {
				window.shop.session.refresh(c);
			}).chain(function() {
				if(window.shop.session.billing_info_ok) window.shop.frame.showSection('account');
				else window.shop.frame.showSection('account', { key: 'account-modify-billing' } );

				window.shop.updateSections();
			}).callChain();
		}
	},

	logout: function() {
		var c = new Chain();
		c.chain(function() {
			new Request({
				method: 'post',
				url: '/site/shop/ajax_session_logout',
				onSuccess: function(response) {
					c.callChain();
				}
			}).send();

		}.bind(this)).chain(function() {
			this.parent.session.refresh(c);
		}.bind(this)).chain(function() {
			this.parent.frame.showSection('account');
			this.parent.updateSections();
		}.bind(this)).callChain();

	},

	register: function(response) {
		response = JSON.decode(response);

		if(!response.success) {
			window.shop.frame.showErrors(response.errors);
		}
		else {
			var c = new Chain().chain(function() {
				window.shop.session.refresh(this);
			}).chain(function() {
				window.shop.frame.showSection('account', { key: 'account-modify-billing' } );
				window.shop.updateSections();
			}).callChain();
		}
	},

	modify_account: function(response) {
		response = JSON.decode(response);
		if(!response.success) {
			window.shop.frame.showErrors(response.errors);
		}
		else {
			var c = new Chain().chain(function() {
				window.shop.session.refresh(this);
			}).chain(function() {
				window.shop.frame.showSection('account');	
				window.shop.updateSections();		
			}).callChain();
		}
	},

	modify_billing: function(response) {
		response = JSON.decode(response);
		if(!response.success) {
			window.shop.frame.showErrors(response.errors);
		}
		else {
			var c = new Chain().chain(function() {
				window.shop.session.refresh(this);
			}).chain(function() {
				window.shop.frame.refreshSection();
				window.shop.updateSections();	
			}).callChain();
		}
	},

	modify_shipping: function(response) {
		response = JSON.decode(response);
		if(!response.success) {
			window.shop.frame.showErrors(response.errors);
		}
		else {
			var c = new Chain().chain(function() {
				window.shop.session.refresh(this);
			}).chain(function() {
				window.shop.frame.refreshSection();
				window.shop.updateSections();
			}).callChain();
		}
	},

	forgot_password: function(response) {
		response = JSON.decode(response);
		if(!response.success) {
			window.shop.frame.showErrors(response.errors);
		}
		else {
			window.shop.frame.showSection('account', { key: 'account-forgot-password-sent' } );
		}
	},


	payment: function(response) {
		response = JSON.decode(response);
		if(!response.success) {
			window.shop.frame.showErrors(response.errors);
		}
		else {
			var c = new Chain().chain(function() {
				window.shop.session.refresh(this);
			}).chain(function() {
				window.shop.frame.showSection('payment' );
				window.shop.cart.refresh('cart-totals');
				window.shop.updateSections();		
			}).callChain();
		}

	},

	setShippingOption: function(shipcode) {
		var c = new Chain();

		c.chain(function() {
			var q = {
				'shipping_option': shipcode
			};

			new Request({
				method: 'post',
				url: '/site/shop/set_shipping_option',
				data: q,
				onSuccess: function(response) {
					c.callChain();
				}
			}).send();

		}.bind(this)).chain(function() {

			if( this.parent.options.currentStep == 'shipping') {
				this.parent.frame.refreshSection();
			}
			this.parent.updateSections();
			this.parent.cart.refresh('cart-totals');
		}.bind(this)).callChain();

	},

	submitOrder: function() {

		// double check that the order is complete
		for(var k in this.parent.sections) {
			if(k != 'summary' && this.shop.sections[k] && !this.shop.sections[k].checkStatus()) {
				alert('Error submitting order, please complete all steps.');
				window.shop.frame.showSection(k);
				window.shop.updateSections();
				return false;
			}
		}

		if( window.__shopSubmittingOrder ) return;
		window.__shopSubmittingOrder = true;

		var c = new Chain();
		c.chain(function() {
			//window.shop.frame.showSection('summary', { key: 'order_confirmation' } );
			window.shop.frame.showLoading('summary');
			c.callChain();
		}).chain(function(response) {
			new Request({
				method: 'post',
				url: '/site/shop/submitOrder',
				onSuccess:function(response) {
					c.callChain(response);
				}
			}).send();
		}).chain(function(response) {

			window.shop.session.refresh(c);
		}).chain(function() {
			document.location = window.shop.options.orderConfirmationUrl;
		}).chain(function() {
			//window.shop.frame.showSection('summary', { key: 'order_confirmation' } );
			//window.shop.cart.update();
			//setTimeout('window.shop.forms.orderCleanup();', 5000);
		}).callChain();
	},

	orderCleanup: function() {
		new Request({
			method: 'post',
			url: '/site/shop/orderCleanup',
			onSuccess:function(response) {

				//window.shop.session.refresh();
			}
		}).send();


	}

});









function shop_payment_method_change(val) {
	var show_this = null;
	var hide_this = null;

	var which_method = '';

	switch(val) {
		case 'visa':
		case 'mc':
		case 'amex':
		case 'disc':
		case 'dinc':
			show_this = $('divShop_payment_cc');
			hide_this = $('divShop_payment_mo');
			which_method = 'cc';
			break;

		case 'check':
		case 'money_order':
			show_this = $('divShop_payment_mo');
			hide_this = $('divShop_payment_cc');
			which_method = 'mo';
			break;

		default:
			hide_this = ($('divShop_payment_cc').getStyle('display') != 'none') ? $('divShop_payment_cc') : $('divShop_payment_mo');
	}

	var c = new Chain();
	c.chain(function() {
		if(hide_this) {
			if(hide_this.getStyle('display') != 'none') {
				var fx_hide = new Fx.Tween(hide_this, { duration: 200 });
				
				fx_hide.start('opacity', 0).chain(function() {
					this.start('height', 1);
				}).chain(function() {
					this.element.setStyle('display', 'none');
					c.callChain();
				});
			}
			else this.callChain();
		}
		else this.callChain();

	}).chain(function() {
		// save payment method
		new Request({
			method: 'post',
			url: '/site/shop/account-save-paymentmethod?paymentmethod='+val+'&'+window.shop.options.session_name+'='+window.shop.options.session_id,
			onComplete:function() {
				c.callChain();
			}
		}).send();
	}).chain(function() {
		window.shop.session.refresh(c);
	}).chain(function() {
		//alert(val + ' ' + window.shop.session.section_ok_payment);
		//alert(window.shop.session.section_ok_payment);
		window.shop.updateSections();
		c.callChain();
	}).chain(function() {
		// show appropriate frames
		if(show_this && show_this.getStyle('display') == 'none') {
			show_this.setStyle('opacity', '0');
			show_this.setStyle('overflow', 'hidden');
			show_this.setStyle('height', 1);
			show_this.setStyle('display', 'block');

			new Fx.Tween(show_this, {
				duration: 200,
				onComplete: function() {
					//c.callChain();
				}
			}).start('height', show_this.scrollHeight).chain(function() {
				this.start('opacity', 1);
			});
		}
		else this.callChain();

	}).callChain();


}




function shop_shipping_copy_billing(f) {
	var s = window.shop.session;

	var flds = [
		'firstname',
		'lastname',
		'company',
		'address_1',
		'address_2',
		'city',
		'state',
		'zip',
		'country'
	];
	for(var i=0; i<flds.length; ++i) {
		f['shipping_'+flds[i]].value = s['billing_'+flds[i]];
	}

	f['shipping_phone'].value = s['dayphone'];
}


function shop_totals_set_shipping_zip() {
	document.cookie = 'shop_shipping_zip='+$('txt_shop_shipping_zip').value;
	document.cookie = 'sssf=1';


	var c = new Chain();
	c.chain(function() {
		shop_totals_show_select_shipping();
	});
	window.shop.cart.refresh('cart-totals', c);
}


function shop_totals_show_select_shipping() {
	document.cookie = 'sssf=0';
	

	var div = $('shopTotalsSelectShipping');
	if(!div || div.getStyle('display') != 'none') return shop_totals_hide_select_shipping();

	div.setStyles({
		display: 'block',
		width: window.shop.frame.div['frameRight'].getWidth()-2,
		height: 1,
		overflow: 'hidden',
		top: window.shop.container.getTop() + window.shop.container.getHeight() - 3
	});

	var toHeight = window.shop.frame.div['cart-totals'].getHeight();
	if(div.scrollHeight > toHeight) toHeight = div.scrollHeight;

	new Fx.Morph(div, {
		duration: 200
	}).start({
		height: toHeight,
		top: div.getTop() - toHeight
	});
}


function shop_totals_hide_select_shipping() {
	document.cookie = 'sssf=0';

	var div = $('shopTotalsSelectShipping');
	if(!div || div.getStyle('display') == 'none') return;

	
	new Fx.Morph(div, {
		duration: 200,
		onComplete: function() {
			this.element.style.display = 'none';
		}
	}).start({
		height: 1,
		top: div.getTop() + div.getHeight()
	});

}







