function ShowSuccessPopup()
{

	var tpl = '<div style="text-align:center;">';
	tpl += 'Thank for you feedback.<br /><br />';
	tpl += '</div>';

	var msg = jQuery('<div />').addClass('popupwindow').css({
		position: 'absolute',
		left: parseInt(jQuery('body').width() / 2 - 150),
		width: 300,
		top: (window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop) + 300,
		display:'none'
	}).html('<div class="close_div" style="margin-bottom: 5px;"></div><p style="margin:0 auto;text-align: center">'+tpl+'</p>'+'<div class="popupbtns"></div>');

	jQuery('.close_div', msg).append(
		jQuery('<img src="/images/close_popup.png" alt="close" id="close_button" />').click(
			function() {
				msg.fadeIn('slow', function()
				{
					msg.remove();
				});
			}
		)
	);

	jQuery('.popupbtns', msg).append(
		jQuery('<input type="button" class="gofield" value="Ok" />').click(
			function(){
				msg.fadeIn('slow', function(){
					msg.remove();
				});
			}
		)
	);

	jQuery('body').append(msg);

	jQuery('#close_button').mousedown(function(e)
	{
		e.cancelBubble = true;
		if (e.stopPropagation)
			e.stopPropagation();
	});

	try {
		jQuery('.popupwindow').draggable({ handle: '.close_div' });
	} catch (ex) {}

	msg.fadeIn('slow');
}

function CheckText(text, error_field)
{
	error_field.innerHTML = '';
	if (text == '')
	{
		error_field.innerHTML = 'Empty text<br />';
		error_field.style.display = 'block';
		return false;
	}

	return true;
}

function CheckEMails(myMail, myError, emailCanBeEmpty)
{
	var valid = true;
	myError.innerHTML = '&nbsp;';
	var re = new RegExp(/^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,4})$/);

	if (typeof(emailCanBeEmpty) == 'undefined') emailCanBeEmpty = false;

	if (myMail == '')
	{
		if (emailCanBeEmpty) return true;

		myError.innerHTML = '<b>Please enter a valid email address.</b><br />';
		valid = false;
	}
	else if (!re.test(myMail.toLowerCase()))
	{
		myError.innerHTML = '<b>Please enter a valid email address.</b><br />';
		valid = false;
	}

	return valid;
}


function ShowOkBox(message)
{
	var tpl = '<div style="text-align:center;">';
	tpl += message;
	tpl += '</div>';

	var popupWidth = 300;

	var msg = jQuery('<div />').addClass('popupwindow').css({
		position: 'absolute',
		left: parseInt(jQuery('body').width() / 2 - (popupWidth/2)),
		width: popupWidth,
		top: (window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop) + 200,
		display:'none'
	}).html('<div class="close_div" style="margin-bottom: 5px;"></div><div><p style="margin:0 auto;text-align: center">' + tpl + '</p></div>'+'<div class="popupbtns"></div>');

	jQuery('.close_div', msg).append(
		jQuery('<img src="/images/close_popup.png" alt="close" id="close_button" />').click(
			function() {
				msg.fadeIn('slow', function()
				{
					msg.remove();
				});
			}
		)
	);

	jQuery('.popupbtns', msg).append(
		jQuery('<input type="button" class="gofield" value="Ok" />').click(function ()
				{
					msg.fadeIn('slow', function(){
								msg.remove();
							}
						);
					})
	);

	jQuery('body').append(msg);

	jQuery('#close_button').mousedown(function(e)
	{
		e.cancelBubble = true;
		if (e.stopPropagation)
			e.stopPropagation();
	});

	try {
		jQuery('.popupwindow').draggable({ handle: '.close_div' });
	} catch (ex) {}

	msg.fadeIn('slow');
	return false;
}

var popups = new Object();
popups['send_to_friend'] = false;

function trim(string)
{
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

function GetAvalibleMessages(prefix)
{
	var res = Query('POST', '/send-to-friend/avalible.php?prefix=' + encodeURIComponent(prefix), null, true);

	if (res != null)
	{
		return res;
	}
	return 0;
}

function ShowSendToFriend()
{
	if (popups['send_to_friend'])
	{
		return false;
	}

	popups['send_to_friend'] = true;

	/* var templateMsg = 'I thought you might find this stock information useful. Check out:\n';
	templateMsg += '<a href="' + window.location.href + '">' + window.location.href + '</a>\n';
	templateMsg += '(Click on the URL or paste it in the address bar of your browser)\n\n'; */

	var templateMsg = 'I thought you might find this stock information useful.\n\n';

	var MAX_FRIENDS_MAILS = 5;
	var maxMessages = Math.min(MAX_FRIENDS_MAILS, GetAvalibleMessages(''));
	var maxMessagesText = '';

	if (maxMessages == 0)
	{
		popups['send_to_friend'] = false;
		ShowOkBox('Sorry! You can send only 25 mails per day');
		return;
	}

	if (maxMessages != 1)
	{
		maxMessagesText = '<br />(up to ' + maxMessages + ' addresses separated by comma)';
	}

	var tpl = '<div style="text-align:center;">';
	tpl += '<b>Enter your email address:</b><br />';
	tpl += '<input type="text" id="sender_email"  class="send-to-friend-control" /><br /><div class="error error_padding" id="sender_error">&nbsp;</div>';
	tpl += '<b>Enter your friend\'s email address' + maxMessagesText + ':</b><br />';
	tpl += '<input type="text" id="friends_email" class="send-to-friend-control" /><br /><div class="error error_padding" id="friends_error">&nbsp;</div>';
	tpl += '<b>Send a personalized message:</b><br />';
	tpl += '<textarea id="message_to_friend" rows="10" class="send-to-friend-control">' + templateMsg + '</textarea><br /><br /><div class="error_padding" id="sending_error">&nbsp;</div>';
	tpl += '<b>URL link to the article:</b><br />';
	tpl += '<input type="text" class="send-to-friend-control" value="' + window.location.href + '" style="color:#000;background-color:#AAA;border:1px solid #CCC;" readonly="true" /><br /><br />';
	tpl += '</div>';

	var popupWidth = 400;

	var msg = jQuery('<div />').addClass('popupwindow').css({
		position: 'absolute',
		left: parseInt(jQuery('body').width() / 2 - (popupWidth/2)),
		width: popupWidth,
		top: (window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop) + 100,
		display:'none'
	}).html('<div class="close_div"></div><p style="margin:0 auto;text-align: center">' + tpl + '</p>'+'<div class="popupbtns"></div>');

	jQuery('.popupbtns', msg).append(
		jQuery('<input type="button" class="gofield" value="Send" style="margin-right: 5px;" />').click(function ()
				{
					var valid = true;
					var myMail = document.getElementById('sender_email').value;
					var myError = document.getElementById('sender_error');
					var friendsMails = document.getElementById('friends_email').value.split(',');

					var mailsCount = Math.min(maxMessages, friendsMails.length);
					for (i = 0; i < mailsCount; i++)
					{
						friendsMails[i] = trim(friendsMails[i]);
					}

					var friendsError = document.getElementById('friends_error');
					friendsError.innerHTML = '&nbsp';

					var msgText = document.getElementById('message_to_friend').value;
					var sendingError = document.getElementById('sending_error');

					myError.innerHTML = '<br />';
					sendingError.className = 'error_padding';
					sendingError.innerHTML = '<br />';

					if (friendsMails.length > maxMessages)
					{
						friendsError.innerHTML = '<b>You can only enter a maximum of ' + maxMessages +  ' email addresses</b>';
						return;
					}

					valid = CheckEMails(myMail, myError);

					for (i = 0; i < mailsCount; i++)
					{
						valid = CheckEMails(friendsMails[i], friendsError, i > 0) && valid;
					}

					if (valid)
					{
						myError.innerHTML = '<br />';
						sendingError.innerHTML = 'Sending...';

						msgText += '\n<a href="' + window.location.href + '">' + window.location.href + '</a>\n';
						msgText += '(Click on the URL or paste it in the address bar of your browser)\n\n';

						var data = 'email=' + encodeURIComponent(myMail) + '&message=' + encodeURIComponent(msgText) + '&friendemail=' + encodeURIComponent(document.getElementById('friends_email').value);

						var res = Query('POST', '../send-to-friend/send.php',  data, true);

						if (res != null && res.toLowerCase().indexOf('ok') == 0)
						{
							popups['send_to_friend'] = false;
							msg.fadeIn('slow', function(){msg.remove();});

							var resMail = '';
							for (i = 0; i < mailsCount; i++)
							{
								resMail += (resMail == '' || friendsMails[i] == ''  ? '' : ', ') + (friendsMails[i] != '' ? friendsMails[i] : '');
							}

							ShowOkBox('Message to ' + resMail + ' has been successfully sent.');
						}
						else
						{
							sendingError.className += ' error';
							sendingError.innerHTML = res;
						}
					}
				}
		)
	);

	jQuery('.close_div', msg).append(
		jQuery('<img src="/images/close_popup.png" alt="close" id="close_button" />').click(
			function() {
				msg.fadeIn('slow', function()
				{
					msg.remove();
					popups['send_to_friend'] = false;
				});
			}
		)
	);

	jQuery('.popupbtns', msg).append(
		jQuery('<input type="button" class="gofield" value="Close" style="margin-left: 5px;" />').click(
			function() {
				msg.fadeIn('slow', function() {
					msg.remove();
					popups['send_to_friend'] = false;
				});
			}
		)
	);

	jQuery('body').append(msg);

	jQuery('#close_button').mousedown(function(e)
	{
		e.cancelBubble = true;
		if (e.stopPropagation)
			e.stopPropagation();
	});

	try {
		jQuery('.popupwindow').draggable({ handle: '.close_div' });
	} catch (ex) {}

	msg.fadeIn('slow');
	return true;
}

function ShowSendToFriendIGU()
{
	if (popups['send_to_friend_igu'])
	{
		return false;
	}

	popups['send_to_friend_igu'] = true;

	var MAX_FRIENDS_MAILS = 5;
	var maxMessages = Math.min(MAX_FRIENDS_MAILS, GetAvalibleMessages('_igu'));
	var maxMessagesText = '';

	if (maxMessages == 0)
	{
		popups['send_to_friend_igu'] = false;
		ShowOkBox('Sorry! You can send only 25 mails per day');
		return;
	}

	if (maxMessages != 1)
	{
		maxMessagesText = '<br />(up to ' + maxMessages + ' addresses separated by comma)';
	}

	var templateMsg = 'I thought you might find this article useful. \n';
	//templateMsg += '<a href="' + window.location.href + '">' + window.location.href + '</a>\n';
	//templateMsg += '(Click on the URL or paste it in the address bar of your browser)\n\n';

	var tpl = '<div style="text-align:center;">';
	tpl += '<b>Enter your email address:</b><br />';
	tpl += '<input type="text" id="sender_email"  class="send-to-friend-control" /><br /><div class="error error_padding" id="sender_error"><br /></div>';
	tpl += '<b>Enter your friend\'s email address' + maxMessagesText + ':</b><br />';
	tpl += '<input type="text" id="friends_email" class="send-to-friend-control" /><br /><div class="error error_padding" id="friends_error"><br /></div>';
	tpl += '<b>Send a personalized message: </b><br />';
	tpl += '<textarea id="message_to_friend" rows="10" class="send-to-friend-control">' + templateMsg + '</textarea><br /><br /><div class="error_padding" id="sending_error"><br /></div>';
	tpl += '<b>URL link to the article:</b><br />';
	tpl += '<input type="text" class="send-to-friend-control" value="' + window.location.href + '" style="color:#000;background-color:#AAA;border:1px solid #CCC;" readonly="true" /><br /><br />';
	tpl += '</div>';

	var popupWidth = 400;

	var msg = jQuery('<div />').addClass('popupwindow').css({
		position: 'absolute',
		left: parseInt(jQuery('body').width() / 2 - (popupWidth/2)),
		width: popupWidth,
		top: (window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop) + 100,
		display:'none'
	}).html('<div class="close_div"></div><p style="margin:0 auto;text-align: center">' + tpl + '</p>'+'<div class="popupbtns"></div>');

	jQuery('.popupbtns', msg).append(
		jQuery('<input type="button" class="gofield" value="Send" style="margin-right: 5px;" />').click(function ()
				{
					var valid = true;
					var myMail = document.getElementById('sender_email').value;
					var myError = document.getElementById('sender_error');
					var friendsMails = document.getElementById('friends_email').value.split(',');

					var mailsCount = Math.min(maxMessages, friendsMails.length);
					for (i = 0; i < mailsCount; i++)
					{
						friendsMails[i] = trim(friendsMails[i]);
					}

					var friendsError = document.getElementById('friends_error');
					friendsError.innerHTML = '&nbsp';

					var msgText = document.getElementById('message_to_friend').value;
					var sendingError = document.getElementById('sending_error');

					myError.innerHTML = '<br />';
					sendingError.className = 'error_padding';
					sendingError.innerHTML = '<br />';

					if (friendsMails.length > maxMessages)
					{
						friendsError.innerHTML = '<b>You can only enter a maximum of ' + maxMessages +  ' email addresses</b>';
						return;
					}
					valid = CheckEMails(myMail, myError);

					for (i = 0; i < mailsCount; i++)
					{
						valid = CheckEMails(friendsMails[i], friendsError, i > 0) && valid;
					}

					if (valid)
					{
						myError.innerHTML = '<br />';
						sendingError.innerHTML = 'Sending...';

						msgText += '<a href="' + window.location.href + '">' + window.location.href + '</a>\n';
						msgText += '(Click on the URL or paste it in the address bar of your browser)\n\n';

						var data = 'prefix=_igu&template=IGU&email=' + encodeURIComponent(myMail) + '&message=' + encodeURIComponent(msgText) + '&friendemail=' + encodeURIComponent(document.getElementById('friends_email').value);
						var res = Query('POST', '../send-to-friend/send.php',  data, true);

						if (res != null && res.toLowerCase().indexOf('ok') == 0)
						{
							popups['send_to_friend_igu'] = false;
							msg.fadeIn('slow', function(){msg.remove();});

							var resMail = '';
							for (i = 0; i < mailsCount; i++)
							{
								resMail += (resMail == '' || friendsMails[i] == ''  ? '' : ', ') + (friendsMails[i] != '' ? friendsMails[i] : '');
							}

							ShowOkBox('Message to ' + resMail + ' has been successfully sent.');
						}
						else
						{
							sendingError.className += ' error';
							sendingError.innerHTML = res;
						}
					}
				}
		)
	);

	jQuery('.close_div', msg).append(
		jQuery('<img src="/images/close_popup.png" alt="close" id="close_button" />').click(
			function() {
				msg.fadeIn('slow', function()
				{
					msg.remove();
					popups['send_to_friend_igu'] = false;
				});
			}
		)
	);

	jQuery('.popupbtns', msg).append(
		jQuery('<input type="button" class="gofield" value="Cancel" style="margin-left: 5px;" />').click(
			function() {
				msg.fadeIn('slow', function() {
					msg.remove();
					popups['send_to_friend_igu'] = false;
				});
			}
		)
	);

	jQuery('body').append(msg);

	jQuery('#close_button').mousedown(function(e)
	{
		e.cancelBubble = true;
		if (e.stopPropagation)
			e.stopPropagation();
	});

	try {
		jQuery('.popupwindow').draggable({ handle: '.close_div' });
	} catch (ex) {}

	msg.fadeIn('slow');
	return true;
}
