﻿Event.observe(window, 'load', function() {

	var form = $$('form')[0];
	if ($(form)) {
		var formName = $(form).identify();

		if (formName == 'couponForm')
			Event.observe('couponForm', 'submit', function(event){
				$('generatedcoupon').update('<p>Данный купон дает Вам право на 5% скидку на все виды услуг в нашей стоматологической клинике. Для того чтобы распечатать купон нажмите на изображении правой кнопкой мыши -> Печать рисунка</p><center><img src="http://php.clinicdent.ru/coupon/pic.php?' + $('couponForm').serialize() + '" width="431" height="280" border="0" alt="Скидка 5%" /></center>');
				Event.stop(event);
			});
		if (formName == 'couponFormEng')
			Event.observe('couponFormEng', 'submit', function(event_eng){
				$('generatedcoupon').update('<p>This is your 5% discount for all types of services of our clinic. You would need to print it and to show it on checkout.</p><center><img src="http://php.clinicdent.ru/coupon/pic.php?' + $('couponFormEng').serialize() + '" width="431" height="280" border="0" alt="Discount 5%" /></center>');
				Event.stop(event_eng);
			});
		if (formName == 'feedbackForm')
			Event.observe('feedbackForm', 'submit', function(event){
				new Ajax.Request('http://php.clinicdent.ru/feedback/mail.php', {
					method:'get',
					parameters: $('feedbackForm').serialize(true),
					onCreate: function(){
						//alert('a request has been initialized!');
						$$("input[type='submit']").each(function(v) {v.disabled = true;})
					}, 
					onSuccess: function(resp) {
						alert('Спасибо! Ваше сообщение отправлено.');
						$$("input[type='submit']").each(function(v) {v.disabled = false;});
						$('feedbackForm').reset()
					},
					onFailure: function(){
						alert('Произошла ошибка при отправке... Попробуйте отправить еще раз позже.');
						$$("input[type='submit']").each(function(v) {v.disabled = false;})
					}
				});
				Event.stop(event);
			});
		if (formName == 'feedbackFormEng')
			Event.observe('feedbackFormEng', 'submit', function(event){
				new Ajax.Request('http://php.clinicdent.ru/feedback/mail.php', {
					method:'get',
					parameters: $('feedbackFormEng').serialize(true),
					onCreate: function(){
						//alert('a request has been initialized!');
						$$("input[type='submit']").each(function(v) {v.disabled = true;})
					}, 
					onSuccess: function(resp) {
						alert('Thank you! Your message has been delivered.');
						$$("input[type='submit']").each(function(v) {v.disabled = false;});
						$('feedbackFormEng').reset()
					},
					onFailure: function(){
						alert('Something went wrong... Try again later.');
						$$("input[type='submit']").each(function(v) {v.disabled = false;})
					}
				});
				Event.stop(event);
			});
	}
});
