
$(document).ready(function () {
    $('#PrintAndTipHolder #print-tip-div #tipsLink').click(function (e) {
        e.preventDefault();

        $('#TipPopUpForm').modal({
            closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
            position: ["20%", ],
            overlayId: 'tip-overlay',
            containerId: 'tip-container',
            onOpen: tipFriendOpen
            //onShow: tipFriendShow
            //onClose: tipFriend.close
        });

        $('#TipPopUpForm #tipButton').click(function (e) {
            e.preventDefault();
            var msg = $('#TipPopUpForm .tip-message');
            if (validateForm(msg)) {
                msg.html("Skickar...");
                $.post("/PostTip.ashx?StrMethodName=EmailTip",
                    {
                        Recieptient: $('#TipPopUpForm #recieptient').val(),
                        Sender: $('#TipPopUpForm #sender').val(),
                        CourseId: $('#TipPopUpForm #courseId').val()
                    },
                    function (response) {
                        msg.html(response).fadeIn(100);
                        setTimeout(function () { $.modal.close(); }, 2000);
                    });
            }
        });
    });
});

function validateForm(msgCtrl) {
    var msg = "";

    var email = $('#TipPopUpForm #recieptient').val();
	if (!email) {
	    msg += 'Email is required. ';
	}
	else {
		if (!validateEmail(email)) {
		    msg += 'Email is invalid. ';
		}
    }

    email = $('#TipPopUpForm #sender').val();
    if (!email) {
        msg += 'Email is required. ';
    }
    else {
        if (!validateEmail(email)) {
            msg += 'Email is invalid. ';
        }
    }

    msgCtrl.html(msg);
    if (msg.length > 0) {
		return false;
	}
	else {
		return true;
	}
}
function validateEmail(email) {
	var at = email.lastIndexOf("@");

	// Make sure the at (@) sybmol exists and  
	// it is not the first or last character
	if (at < 1 || (at + 1) === email.length)
		return false;

	// Make sure there aren't multiple periods together
	if (/(\.{2,})/.test(email))
		return false;

	// Break up the local and domain portions
	var local = email.substring(0, at);
	var domain = email.substring(at + 1);

	// Check lengths
	if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
		return false;

	// Make sure local and domain don't start with or end with a period
	if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
		return false;

	// Check for quoted-string addresses
	// Since almost anything is allowed in a quoted-string address,
	// we're just going to let them go through
	if (!/^"(.+)"$/.test(local)) {
		// It's a dot-string address...check for valid characters
		if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
			return false;
	}

	// Make sure domain contains only valid characters and at least one period
	if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
		return false;	

	return true;
}

function tipFriendOpen(dialog) {
	dialog.overlay.fadeIn('fast', function () {
	    dialog.container.slideDown('fast', function () {
	        dialog.data.fadeIn('fast');
	    });
	});
}

function error(xhr) {
    alert(xhr.statusText);
}
/*
function tipFriendShow(dialog) {
	$('#TipPopUpForm .contact-send').click(function (e) {
		e.preventDefault();
		// validate form
		if (contact.validate()) {
			var msg = $('#TipPopUpForm .contact-message');
			msg.fadeOut(function () {
				msg.removeClass('contact-error').empty();
			});
			$('#TipPopUpForm .contact-title').html('Sending...');
			//$('#TipPopUpForm form').fadeOut(200);
		$('#TipPopUpForm .tip-content').animate({
				height: '80px'
			}, function () {
				$('#TipPopUpForm .contact-loading').fadeIn(200, function () {
					$.ajax({
						url: 'data/contact.php',
						data: $('#TipPopUpForm form').serialize() + '&action=send',
						type: 'post',
						cache: false,
						dataType: 'html',
						success: function (data) {
							$('#TipPopUpForm .contact-loading').fadeOut(200, function () {
								$('#TipPopUpForm .contact-title').html('Thank you!');
								msg.html(data).fadeIn(200);
							});
						},
						error: contact.error
					});
				});
			});
		}
		else {
			if ($('#TipPopUpForm .contact-message:visible').length > 0) {
				var msg = $('#TipPopUpForm .contact-message div');
				msg.fadeOut(200, function () {
					msg.empty();
					contact.showError();
					msg.fadeIn(200);
				});
			}
			else {
				$('#TipPopUpForm .contact-message').animate({
					height: '30px'
				}, contact.showError);
			}
					
		}
	});
}

		close: function (dialog) {
			$('#TipPopUpForm .contact-message').fadeOut();
			//$('#TipPopUpForm .contact-title').html('Goodbye...');
			//$('#TipPopUpForm form').fadeOut(200);
			$('#TipPopUpForm .tip-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 (!$('#TipPopUpForm #contact-name').val()) {
				contact.message += 'Name is required. ';
			}

            var email = $('#TipPopUpForm #recieptient').val();
			if (!email) {
				contact.message += 'Email is required. ';
			}
			else {
				if (!contact.validateEmail(email)) {
					contact.message += 'Email is invalid. ';
				}
            }

            email = $('#TipPopUpForm #sender').val();
            if (!email) {
                contact.message += 'Email is required. ';
            }
            else {
                if (!contact.validateEmail(email)) {
                    contact.message += 'Email is invalid. ';
                }
            }


			if (contact.message.length > 0) {
				return false;
			}
			else {
				return true;
			}
		},
		validateEmail: function (email) {
			var at = email.lastIndexOf("@");

			// Make sure the at (@) sybmol exists and  
			// it is not the first or last character
			if (at < 1 || (at + 1) === email.length)
				return false;

			// Make sure there aren't multiple periods together
			if (/(\.{2,})/.test(email))
				return false;

			// Break up the local and domain portions
			var local = email.substring(0, at);
			var domain = email.substring(at + 1);

			// Check lengths
			if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
				return false;

			// Make sure local and domain don't start with or end with a period
			if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
				return false;

			// Check for quoted-string addresses
			// Since almost anything is allowed in a quoted-string address,
			// we're just going to let them go through
			if (!/^"(.+)"$/.test(local)) {
				// It's a dot-string address...check for valid characters
				if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
					return false;
			}

			// Make sure domain contains only valid characters and at least one period
			if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
				return false;	

			return true;
		},
		showError: function () {
			$('#TipPopUpForm .tip-message')
				.html($('<div class="contact-error"></div>').append(contact.message))
				.fadeIn(200);
		}
	};
    */


