var mainDomainName = location.protocol+location.port+"//"+location.host; 
$(function(){
	$('.popupform').click(function(){
		$('#offerID').val($(this).attr('id').substring(5));		
		$.ajax({
			type: 'POST',
			url: mainDomainName + '/comments.php',
			data: 'checkLogin=yes',
			dataType: 'json',
			success: function(msg){
				if (msg.name != null) {
					$('#commentname').val(msg.name);				
				}
				if (msg.email != null) {
					$('#commentemail').val(msg.email).attr('readonly', 'readonly');
				}
			}
		});				
		$('#loginSubmit').hide();
		$('#commentMsg').empty().hide();
		$('#commentpasswd').val('');
		if ($('.popupcomment').length > 0) {
			$('.popupcomment').modal({
				opacity:50,
				overlayCss: {backgroundColor:"#333333"},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
						dialog.container.hide(200, function () {
							$.modal.close();
						});
					});
				}
			});	
			$('.popupcomment :input').not($("#commentcontent")[0]).each(function() {
				$(this).keyup(function(e) {
					var key = e.which;
					if (key == 13) {
						if ($('#commentpasswd').val() != '') {
							$('#login').val('yes');
						}
						else {
							$('#login').val('no');
						}
						commentSubmit();
					}
				});				
			});
		}
		
		if ($('.merchantpopupcomment').length > 0) {
			$('.merchantpopupcomment').modal({
				opacity:50,
				overlayCss: {backgroundColor:"#333333"},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
						dialog.container.hide(200, function () {
							$.modal.close();
						});
					});
					location.reload();
				}
			});	
			$('.merchantpopupcomment :input').not($("#commentcontent")[0]).each(function() {
				$(this).keyup(function(e) {
					var key = e.which;
					if (key == 13) {
						if ($('#commentpasswd').val() != '') {
							$('#login').val('yes');
						}
						else {
							$('#login').val('no');
						}
						commentSubmit();
					}
				});				
			});
		}

		$("#securityCodeComments").loadSecurityCode();
		$("#securityCodeComments").click(function(){
			$(this).refreshSecurityCode();
		});
		return false;
	});
	
	$('#commentemail').blur(function(){
		if ($(this).val() != '') {
			$.ajax({
			   type: 'POST',
			   url: mainDomainName + '/comments.php',
			   data: 'checkEmail=' + $(this).val(),
			   success: function(msg){
				if (msg.match(/^\[error_message\]:/gi)) {
					$('#commentpasswd').parent().show();
					$('#commentpasswd').val('');
					$('#loginSubmit').show();
				} else {					
					$('#commentpasswd').parent().hide();
					$('#commentpasswd').val('');
					$('#loginSubmit').hide();
				}			     
			   }
			});
		}
	});
	
	$('#loginSubmit').click(function(){
		$('#login').val('yes');
		commentSubmit();
	});
	
	$('#commentSubmit').click(function(){
		$('#login').val('no');
		commentSubmit();
	});
	
	$('#commentsForm').submit(function(e){
		return false;
	});
});

function commentSubmit() {
	var url = mainDomainName + '/coupon.php';
	$('#commentMsg').html('<img src="/images/default/ajax-loader.gif" style="vertical-align:middle" />').show();
	$.ajax({
		type: 'POST',
		url: mainDomainName + '/comments.php',
		data: $('#commentsForm').serialize(),
		success: function(msg){
			if (msg.match(/^\[error_message\]:/gi)) {
				var message = (msg.match(/@/gi)) ? msg.slice(msg.indexOf(':') + 1, msg.indexOf('@')) : msg.slice(msg.indexOf(':') + 1);
				var focuson = (msg.match(/@/gi)) ? msg.slice(msg.indexOf('@') + 1) : '';
				$('#commentMsg').html(message).show();
				$('#'+focuson).focus();
			} else {
				var successMsg = '<table style="width: 100%; text-align: center; margin-top: 0px; height:150px; line-height:20px; color:#666;"><tr><td><br><b>Your comment has been successfully received. It will be posted on the site shortly.</b><br></td></tr></table>';
				$('#commentMsg').parent().parent().html(successMsg);
				setTimeout("location.reload();", 2000);
			}
		}
	});
}
