$(document).ready(function () {

	$('#contactLink, #contactForm1 a:eq(0)').click(function (e) {
		e.preventDefault();
		link = 2;
		action = 'feedback';
		pop_h = 190;
		$.get("SmartForms.php?page=sendfeedback", function(data){
			$(data).modal({
				close: false,
				overlayId: 'contact-overlay',
				containerId: 'contact-container',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	});

	var img = ['loading.gif','send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = 'http://static.tastykhana.in/img/contact/' + this;
	});
});

var contact = {
	message: null,
	open: function (dialog) {
		var title = $('#contact-container .contact-title').html();
		dialog.overlay.show('fast');
			dialog.container.fadeIn('fast', function () {
				dialog.data.fadeIn('fast', function () {
					$('#contact-container .contact-content').animate({
						height: pop_h
					}, function () {
						$('#contact-container .contact-title').html(title);
						$('#contact-container form').fadeIn(0, function () {
							$('#contact-container #contact-name').focus();
						});
					});
				});
			});
	},
	show: function (dialog) {
		$('#contact-container .contact-send').click(function (e) {
			e.preventDefault();
			if(link == 10)
			validation = contact.validateForReview();
			else if(link == 11)
			validation = contact.validateAddress();
			else if(link == 22)
			validation = contact.validateForModify();
			else if(link == 101)
			validation = contact.validateForFavourite();
			else if( link == 33 )
			validation = contact.validatePlayerReply();
			else
			validation = contact.validate();
			if (validation) {
				$('#contact-container .contact-message').fadeOut(function () {
					$('#contact-container .contact-message').removeClass('contact-error').empty();
				});
				$('#contact-container .contact-title').html('Sending...');
				$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: '80px'
				}, function () {
					$('#contact-container .contact-loading').fadeIn(200, function () {
						$.ajax({
							url: 'SmartForms.php?action='+action,
							data: $('#contact-container form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (xhr) {
								$('#contact-container .contact-loading').fadeOut(200, function () {
									$('#contact-container .contact-title').html('Thank you!');
									$('#contact-container .contact-message').html(xhr.responseText).fadeIn(1);
									dialog.data.fadeOut(2000, function () {
								dialog.container.fadeOut(400, function () {
							dialog.overlay.fadeOut(400, function () {
						$.modal.close();
					});
				});
			});
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
				if ($('#contact-container .contact-message:visible').length > 0) {
					var msg = $('#contact-container .contact-message div');
					msg.fadeOut(200, function () {
						msg.empty();
						contact.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('#contact-container .contact-message').animate({
						height: '100px'
					}, contact.showError);
					$('#contact-container .contact-content').animate({
						height: pop_h+100+'px'
					});
				}

			}
		});
	},
	close: function (dialog) {
		$('#contact-container .contact-message').fadeOut();
		$('#contact-container .contact-title').html('Goodbye...');
		$('#contact-container form').fadeOut(200);
		$('#contact-container .contact-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		contact.message = '';
		if(link != '3')
		{
		if (!$('#contact-container #contact-name').val()) {
			contact.message += '<li>Name is required.</li>';
		}
		}
        if(link !='2'){
		var email = $('#contact-container #contact-email').val();
		if (!email) {
			contact.message += '<li>Email is required.</li>';
		}
		else {
			if (!contact.validateEmail(email)) {
				contact.message += '<li>Email is invalid.</li>';
			}
		}
		}
        if(link != '3')
        {
		if (!$('#contact-container #contact-message').val()) {
			contact.message += '<li>Message is required.</li>';
		}
        }
		if (contact.message.length > 0) {
		    contact.message = '<ul>'+contact.message+'</ul>'
			return false;
		}
		else {
			return true;
		}
	},
	validateForReview: function () {
		contact.message = '';

		if (!$('#contact-container #contact-name').val()) {
			contact.message += '<li>Subject is required.</li>';
		}
		if (!$('#contact-container #contact-message').val()) {
			contact.message += '<li>Review is required.</li>';
		}
		if (contact.message.length > 0) {
		    contact.message = '<ul>'+contact.message+'</ul>'
			return false;
		}
		else {
			return true;
		}
	},
	validateAddress: function() {
		contact.message = '';
		if (!$('#contact-container #contact-name').val()) {
			contact.message += '<li>Address Tag required.</li>';
		}
		if (!$('#contact-container #contact-message').val()) {
			contact.message += '<li>Address required.</li>';
		}
		if (contact.message.length > 0) {
		    contact.message = '<ul>'+contact.message+'</ul>'
			return false;
		}
		else {
			return true;
		}
	},
	validateForFavourite: function () {
		contact.message = '';

		if (!$('#contact-container #contact-message').val()) {
			contact.message += '<li>Comment is required.</li>';
		}
		if (contact.message.length > 0) {
		    contact.message = '<ul>'+contact.message+'</ul>'
			return false;
		}
		else {
			return true;
		}
	},
	validateForModify: function () {
		contact.message = '';

		if (!$('#contact-container #guests').val()) {
			contact.message += '<li>No. of guests is required.</li>';
		}
		if (!$('#contact-container #bookingdate').val()) {
			contact.message += '<li>Booking date is required.</li>';
		}
		if (!$('#contact-container #time').val()) {
			contact.message += '<li>Booking time is required.</li>';
		}

		if (contact.message.length > 0) {
		    contact.message = '<ul>'+contact.message+'</ul>'
			return false;
		}
		else {
			return true;
		}
	},
	validatePlayerReply: function() {

		contact.message = '';
		if (!$('#contact-container #answer').val()) {
			contact.message += '<li>Please enter answer for this clue.</li>';
		}
		if (contact.message.length > 0) {
		    contact.message = '<ul>'+contact.message+'</ul>'
			return false;
		}else {
			return true;
		}
	},
	validateEmail: function (email) {
		var at = email.lastIndexOf("@");

		if (at < 1 || (at + 1) === email.length)
			return false;

		if (/(\.{2,})/.test(email))
			return false;

		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
			return false;

		if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
			return false;

		if (!/^"(.+)"$/.test(local)) {
			if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
				return false;
		}

		if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
			return false;

		return true;
	},
	showError: function () {
		$('#contact-container .contact-message')
			.html($('<div class="contact-error">').append(contact.message))
			.fadeIn(200);
	}
};


function saveReviewReply(id){
                if(validateReviewReply(id)){
                   $('#reply-'+id+' form').fadeOut(200);
                   $('#reply-'+id+' .contact-loading').fadeIn(200, function () {
                        $.ajax({
							url: 'reviewajaxlib.php?action=reply&reviewid='+id,
							data: $('#reply-'+id+' form').serialize() + '&action=reply&reviewid='+id,
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (xhr) {
								$('#reply-'+id+' .contact-loading').fadeOut(200, function () {
								$('#reply-'+id+' .contact-title').html('Thank you!');
									$('#reply-'+id).fadeOut(400, function () {
								$('#reply-'+id).fadeOut(400, function () {
							$('#reply-'+id).fadeOut(400, function () {
							$('#reply-'+id+' #contact-title').html('');
					});
				});
			});
        });
      }
     });
   });
   } else {
   $('#reply-'+id+' #contact-title').html('<p>Error:'+message);
   }
}
function closeReviewReply(id){
$('#reply-'+id+' #contact-title').html('');
$('#reply-'+id).fadeOut(200);
}
function validateReviewReply(id){
message = '';
if (!$('#post-'+id+' #username').val()) {
			message += ' * Name is required, ';
}
if (!$('#post-'+id+' #replymsg').val()) {
			message += ' * Message is required.</p> ';
}
if (message.length > 0) {
			return false;
		}
		else {
			return true;
		}
}
function validateWriteReview( ){
message = '';
if (!$('#writereview #subject').val()) {
			message += ' *Subject is required. ';
}
if (!$('#writereview #post').val()) {
			message += ' *Review Message is required. ';
}
if (message.length > 0) {
			return false;
		}
		else {
			return true;
		}
}
