function EmailPopup()
{
	var emailPopup = document.createElement('div');
	emailPopup.id = 'email_popup';
	$j(emailPopup).html(
		'<div id="prop_details">'+
			'<div id="email_popup-title">EMAIL ASSOCIATE</div>'+
			'<div id="email_popup-message"></div>'+
			'<div style="padding: 4px;">'+
				'<form id="email_form" name="email_form"><dl class="form" style="width: 330px;">'+
					'<dt>Your Name</dt>'+
					'<dd><input type="text" name="e_name" id="e_name" value="" /></dd>'+
					'<dt>Your E-mail</dt>'+
					'<dd><input type="text" name="e_email" id="e_email" value="" /></dd>'+
					'<dt>Your Telephone</dt>'+
					'<dd><input type="text" name="e_phone" id="e_phone" value="" /></dd>'+
					'<dt>Contact Preference</dt>'+
					'<dd class="radio" style="text-align: left;"><input type="radio" name="e_contact" id="e_contact" value="phone" />&nbsp;Phone<br/><input type="radio" name="e_contact" id="e_contact" value="email" />&nbsp;E-mail</dd>'+
					'<dt>Comments</dt>'+
					'<dd><textarea name="e_comments" id="e_comments" ></textarea></dd>'+
				'</dl>'+
				'<div>'+
				'<div class="button" style="width: 60px;" id="email_send"><div>SEND</div></div><div id="email_cancel" class="button" style="width: 60px;"><div>CLOSE</div></div>'+
				'</div></form>'+
			'</div>'+
		'</div>'
		);
			
	var emailMask = document.createElement('div');
	emailMask.id = 'email_popup-mask';
	
	$j(emailMask).hide();
	$j(emailPopup).hide();
	
	$j(document.body).prepend(emailPopup);
	$j(document.body).prepend(emailMask);

	var object = this;
	$j('#email_cancel').bind('click',function(){object.hide()});
	$j('#email_send').bind('click',function(){object.sendMail()});	

	var dl = $j(emailPopup);
	dl.css('width', '350px');
	dl.css('height', '350px');
	
	this.hide();	
}

EmailPopup.prototype = {
	show: function(agent_id, agent_name)
	{
		$j('#email_popup-title').html('EMAIL ' + agent_name);
		this.agent_id = agent_id;
		$j('#e_name').val('');
		$j('#e_email').val('');
		$j('#e_telephone').val('');
		$j('#e_contact').val('phone');
		$j('#e_comments').val('');
		
		$j('#email_popup-message').hide();
		
		var dl = $j('#email_popup')
		var dialogTop = $j(document).scrollTop() + (Math.abs($j(window).height() - dl.height()) / 2);
		dl.css('left', ($j(window).width() - dl.width()) / 2);
		dl.css('top', (dialogTop >= 25) ? dialogTop : 25);
		dl.show();
		
		$j('#email_popup-mask').css('height', $j(window.document).height()+'px');
		$j('#email_popup-mask').show();
	},
	hide: function()
	{
		$j('#email_popup').hide();
		$j('#email_popup-mask').hide();
	},
	sendMail: function()
	{
		var object = this;
		
		var field = $j('#e_name')[0];
		if(field.value.length == 0)
		{
			$j('#email_popup-message').html('Please enter your name.');
			$j('#email_popup-message').show('slow');
			field.focus();
			return false;
		}
		
		field = $j('#e_email')[0];
		if(field.value.length == 0 || !this.validateEmail(field.value))
		{
			$j('#email_popup-message').html('Please enter a valid e-mail address.');
			$j('#email_popup-message').show('slow');
			field.focus();
			return false;
		}
	
		
		$j.post('/ajax/email_associate.php', {'agent_id': this.agent_id, 'name': $j('#e_name').val(), 'email': $j('#e_email').val(), 'phone': $j('#e_phone').val(),
					'contact': $j("input[@name='e_contact']:checked").val(), 'comments': $j('#e_comments').val()}, object.emailResponse, 'json');
	},
	emailResponse: function(data, textStatus)
	{
		if(data.status == 'error')
		{
			$j('#email_popup-message').html(data.message);
			$j('#email_popup-message').show('slow');
			return false;
		}
		
		$j('#email_popup-message').html('Thank you. Your email has been sent.');
		$j('#email_popup-message').show('slow');
	},
	validateEmail: function(email)
	{
		var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (regex.test(email)) return true;
           
		return false;
	} 
}
