
function validateEmail (emailField)
{
	emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_\+&~%-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	if( !emailpat.test( emailField) )
	{
		return false;
	}
	return true;
}

$(document).ready(function() {
	$('#sr_email_alert_link').hover(
		function() {
			$(this).css('text-decoration', 'underline');
			$(this).css('cursor', 'pointer');
		},
		function() {
			$(this).css('text-decoration', 'none');
		}
	);
	$('#sr_email_alert_link_icon').hover(
		function() {
			$(this).css('cursor', 'pointer');
		},
		function() {
			$(this).css('cursor', 'default');
		}
	);
	$('#sr_email_alert_link').click(function() {
		$.blockUI({message: $('#sr_email_alert'), css: {margin: '-200px 0 0 -274px', width: '548px', height: '263px', 'left': '50%', background: '#ffffff'}, fadeIn: false});
		$('.blockUI').css('cursor', 'default');
	});
	$('#sr_email_alert_link_icon').click(function() {
		$.blockUI({message: $('#sr_email_alert'), css: {margin: '-200px 0 0 -274px', width: '548px', height: '263px', 'left': '50%', background: '#ffffff'}, fadeIn: false});
		$('.blockUI').css('cursor', 'default');
	});
	
	$('#sr_email_alert_cancel').hover(
		function() {
			$(this).css('cursor', 'pointer');
		},
		function() {
			$(this).css('cursor', 'default');
		}
	);
	$('#sr_email_alert_cancel').click(function() {
		$.unblockUI({fadeOut: false});
	});
	
	var ea_loaded = 0;
	$('#email_alert_email').focus(function() {
		if ($(this).val() == 'john.smith@example.com' && !ea_loaded) // hack to ensure input div is blurred on page load
		{
			$(this).blur();
			ea_loaded = 1;
		}
		else if ($(this).val() == 'john.smith@example.com')
		{
			$(this).val('');
			$(this).css({'color': '#000', 'font-style': 'normal'});
		}
	});
	
	$('#email_alert_email').blur(function() {
		if ($(this).val() == '')
		{
			$(this).val('john.smith@example.com');
			$(this).css({'color': '#888', 'font-style': 'italic'});
		}
	});
	
	
	$('#sr_email_alert_create').hover(
		function() {
			$(this).css('cursor', 'pointer');
		},
		function() {
			$(this).css('cursor', 'default');
		}
	);
	$('#sr_email_alert_create').click(function() {
		if (!validateEmail($('#email_alert_email').val()))
		{
			$('#sr_email_alert_error').text('* Invalid');
		}
		else if ($('#email_alert_email').val() == '' || $('#email_alert_email').val() == 'john.smith@example.com')
		{
			$('#sr_email_alert_error').text('* Required');
		}
		else
		{
			$('#sr_email_alert_main').css('display', 'none');
			$('#sr_email_alert_sending').css('display', 'block');
			var frequency = '';
			if ($('#email_alert_frequency_daily').attr('checked') == true)
				frequency = 'daily'
			else
				frequency = 'instantly';
			$.post('http://www.property.ie/alerts.php',
				{
					'email_address': $('#email_alert_email').val(),
					'frequency': frequency,
					'affiliate': $('#affiliate').val(),
					'search_url': $('#search_url').val(),
					'search_text': $('#search_text').val(),
					'ip_address': $('#ip_address').val()
				},
				function (data) {
					var resp = $(data).find('resp').text();
					$('#sr_email_alert_sending').css('display', 'none');
					if (resp == 'bad email')
					{
						$('#sr_email_alert_error').html('* Invalid');
					}
					else if (resp == 'ok')
					{
						$('#sr_email_alert_main').html("Your request to set up an email alert has been successful. A verification email will be sent to <b>"+$('#email_alert_email').val()+"</b> shortly. Please click on the link in this email in order to activate your email alert.<br/><div style=\"float:right; margin-top: 14px;\"><img src=\"http://www.property.ie/i/fronts/pie/close_btn.gif\" onclick=\"javascript:$.unblockUI({fadeOut: 100});\" onmouseover=\"javascript:$(this).css('cursor', 'pointer');\" onmouseout=\"javascript:$(this).css('cursor', 'default');\" /></div>");
					}
					else
					{
						$('#sr_email_alert_main').html("There has been an error with setting up this email alert. Please try again later.<br/><div style=\"float:right;\"><img src=\"http://www.property.ie/i/fronts/pie/close_btn.gif\" onclick=\"javascript:$.unblockUI({fadeOut: 100});\" onmouseover=\"javascript:$(this).css('cursor', 'pointer');\" onmouseout=\"javascript:$(this).css('cursor', 'default');\" /></div>");
					}
					$('#sr_email_alert_main').css('display', 'block');
				}, 'xml');
		}
	});
	
	$(".email_alert_frequency").hover(
		function () {
			$(this).css('cursor', 'pointer');
		},
		function () {
			$(this).css('cursor', 'default');
		}
	);
	$(".email_alert_frequency").click(
		function () {
			var id = $(this).attr('id').split('_');
			var other_id;
			id = id[3];
			$('#email_alert_frequency').val(id);
			if (id == 'daily')
				other_id = 'instantly';
			else
				other_id = 'daily'
			$('#email_alert_frequency_'+id).attr('checked', 'checked');
			$('#email_alert_frequency_'+other_id).attr('checked', '');
		}
	);
});