/*
	shared.js
	v1.0 - 24/jan/2008 - Bob Kersten - Initial version.
	(C) Copyright 2008 Fellownet.
	All rights reserved.
*/

function onChangeOptions() {
	// Get the new content for the price form. If we successfully receive new content
	// we're going to fade out, update and fade back in.
	new Ajax.Request('form.price.php', {
		method:'post',
		parameters:Form.serialize('product_form'),
		onSuccess:function(req_) {
			new Effect.BlindUp('priceform', {
				duration:0.3,
				afterFinish:function() {
					$('priceform').update(req_.responseText);
					new Effect.BlindDown('priceform', {
						duration:0.3
					} );
				} 
			} );
		}
	} );
}

function onChangeCartOptions(form_, action_) {
	// This function can be used to make last minute alterations to the shopping cart.
	if (typeof(action_) == 'string') {
		$(form_)['action'].value = action_;
	}

	var doChange = function() {
		new Ajax.Request('form.cart.php', {
			method:'post',
			parameters:Form.serialize(form_),
			onSuccess:function(req_) {
				$('cartform').update(req_.responseText);
				new Ajax.Updater('cart', 'div.cart.php');
			}
		} );
	}

	if ($(form_)['action'].value == 'remove') {
		dialog.open( {
			width:550,
			duration:0,
			method:'post',
			parameters:Form.serialize(form_),
			source:'dialog.confirm.php?rn=' + Math.random(),
			buttons:'Annuleren,Verwijderen',
			button_cancel:'Annuleren',
			button_default:'Verwijderen',
			callback:function(action_, dialog_) {
				if (action_ == 'Verwijderen') {
					dialog_.close(function() {
						Effect.Shrink($('img_' + $(form_)['winkelwagen_id'].value), {
							duration:0.5,
							afterFinish:function() {
								doChange();
							}
						} );
					} );
				} else dialog_.close();
			}
		} );
	} else doChange();

	return false;
}

function onAddToCart(form_, target_) {
	// This function adds the product in the supplied form into the cart. If the addition
	// was successfull we're going to animate the product.
	new Ajax.Request('xf.cart.php', {
		method:'post',
		parameters:Form.serialize(form_),
		onSuccess:function(req_) {
			// Let's see if the product was successfully added to the cart. If not we're
			// going to show the erronious output.
			var results = req_.responseText.toQueryParams();
			if (
				typeof(results['success']) == 'string' &&
				results['success'] == 'true'
			) {
				Effect.Puff($('img_' + $(form_)['product_id'].value), {
					duration:0.5,
					afterFinish:function() {
						if (typeof(target_) == 'string') {
							location.href = target_;
						} else {
							new Ajax.Updater('cart', 'div.cart.php', {
								onComplete:function() {
									dialog.open( {
										width:550,
										duration:0,
										source:'dialog.cart.php?rn=' + Math.random(),
										method:'post',
										parameters:Form.serialize(form_),
										buttons:'Sluiten,Toon winkelwagen',
										button_cancel:'Sluiten',
										callback:function(action_, dialog_) {
											if (action_ == 'Toon winkelwagen') {
												location.href = 'cart.php';
											} else dialog_.close();
										}
									} );
								}
							} );
							Effect.Appear($('img_' + $(form_)['product_id'].value));
						}
					}
				} );
			} else alert(req_.responseText);
		}
	} );
	return false;
}

function onContactFormSubmit(button_) {
	// This function is going to validate the data entered in the detailsForm to check if
	// it is valid or not.
	var fields = $A(document.getElementsByClassName('field_error'));
	fields.each( function(el_) {
		Element.removeClassName(el_, 'field_error');
	} );
	if (button_) button_.style.visibility = 'hidden';
	new Ajax.Request(
		'xf.contact.php?rn=' + Math.random(), {
			method:'post',
			parameters:Form.serialize('contact_form'),
			onComplete:function(req_) {
				var results = req_.responseText.toQueryParams();
				if (results['result'] && results['result'] != 'OK') {
					var errors = $A(results['fields'].split(';'));
					errors.each( function(el_) {
						Element.addClassName(el_, 'field_error');
					} );
					dialog.open( {
						content:results['content'],
						title:results['title'],
						buttons:'Sluiten',
						width:320,
						duration:0,
						callback:function(action_, dialog_) {
							if (button_) button_.style.visibility = 'visible';
							dialog_.close();
							if (field = $(errors[0])) field.focus();
						}
					} );
				} else if (results['result'] && results['result'] == 'OK') {
					dialog.open( {
						content:results['content'],
						title:results['title'],
						buttons:'Sluiten',
						width:320,
						duration:0
					} );
				} else {
					if (button_) button_.style.visibility = 'visible';
					alert(req_.responseText);
				}
			}
		}
	);
	return false;
}

function onCartFormSubmit(button_) {
	// This function is going to validate the data entered in the cartForm to check if
	// it is valid or not.
	var fields = $A(document.getElementsByClassName('field_error'));
	fields.each( function(el_) {
		Element.removeClassName(el_, 'field_error');
	} );
	if (button_) button_.style.visibility = 'hidden';
	new Ajax.Request(
		'xf.cart.submit.php?rn=' + Math.random(), {
			method:'post',
			parameters:Form.serialize('cart_form'),
			onComplete:function(req_) {
				var results = req_.responseText.toQueryParams();
				if (results['result'] && results['result'] != 'OK') {
					var errors = $A(results['fields'].split(';'));
					errors.each( function(el_) {
						Element.addClassName(el_, 'field_error');
					} );
					dialog.open( {
						content:results['content'],
						title:results['title'],
						buttons:'Sluiten',
						width:320,
						duration:0,
						callback:function(action_, dialog_) {
							if (button_) button_.style.visibility = 'visible';
							dialog_.close();
							if (field = $(errors[0])) field.focus();
						}
					} );
				} else if (results['result'] && results['result'] == 'OK') {
					location.href = results['target'];
				} else {
					if (button_) button_.style.visibility = 'visible';
					alert(req_.responseText);
				}
			}
		}
	);
	return false;
}

function onColorDialogOpen(product_id_, bundle_, index_) {
	dialog.open( {
		width:450,
		duration:0.2,
		source:'dialog.color.php?rn=' + Math.random(),
		method:'post',
		parameters:'product_id=' + product_id_ + '&bundle=' + bundle_ + '&index=' + index_,
		buttons:'Annuleren',
		button_cancel:'Annuleren',
		callback:function(action_, dialog_) {
			dialog_.close();
		}
	} );
	return false;
}

function onColorSelect(value_, color_, index_) {
	try {
		var dlg = dialog.onTop();
		dlg.close( function() {
			$('bundel_' + index_).value = value_;
			if (typeof($('bundel_' + index_).onchange) == 'function') {
				$('bundel_' + index_).onchange();
			}
		} );
	} catch(e_) {}
	return false;
}

function onChangePaymentOptions() {
	new Ajax.Request('form.payment.php', {
		method:'post',
		parameters:Form.serialize('payment_form'),
		onSuccess:function(req_) {
			new Effect.BlindUp('paymentform', {
				duration:0.3,
				afterFinish:function() {
					$('paymentform').update(req_.responseText);
					new Effect.BlindDown('paymentform', {
						duration:0.3
					} );
				} 
			} );
		}
	} );
}

function onPaymentFormSubmit(button_) {
	// This function is going to validate the data entered in the detailsForm to check if
	// it is valid or not.
	if (button_) button_.style.visibility = 'hidden';
	new Ajax.Request(
		'xf.payment.php?rn=' + Math.random(), {
			method:'post',
			parameters:Form.serialize('payment_form'),
			onComplete:function(req_) {
				var results = req_.responseText.toQueryParams();
				if (results['result'] && results['result'] != 'OK') {
					var errors = $A(results['fields'].split(';'));
					errors.each( function(el_) {
						Element.addClassName(el_, 'field_error');
					} );
					dialog.open( {
						content:results['content'],
						title:results['title'],
						buttons:'Sluiten',
						width:320,
						duration:0,
						callback:function(action_, dialog_) {
							if (button_) button_.style.visibility = 'visible';
							dialog_.close();
							if (field = $(errors[0])) field.focus();
						}
					} );
				} else if (results['result'] && results['result'] == 'OK') {
					$('field_check').value = 0;
					$('payment_form').submit();

				} else {
					if (button_) button_.style.visibility = 'visible';
					alert(req_.responseText);
				}
			}
		}
	);
	return false;
}
