// JavaScript Document
function checkemail(strng)
{
	var x = strng;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
} 
mailer = {
	ajaxUrl: "/mailer",
	mailtype: 'mail',
	mail: function()
	{

		$('mail_phone_error').style.display='none';
	

		$('mail_mail').addClassName("active");
		$('mail_call').removeClassName("active");		
		this.mailtype = 'mail';
		$('row_mail_phone').style.display = "none";
		$('row_mail_reply').style.display = "";
		$('row_mail_phone2').style.display = "none";
		$('row_mail_reply2').style.display = "";	
		$('mail_name').focus();		
		return false;
	},

	call: function()
	{


		$('mail_reply_error').style.display='none';

		$('mail_call').addClassName("active");
		$('mail_mail').removeClassName("active");		
		this.mailtype = 'call';
		$('row_mail_reply').style.display = "none";
		$('row_mail_phone').style.display = "";
		$('row_mail_reply2').style.display = "none";
		$('row_mail_phone2').style.display = "";	
		$('mail_name').focus();		
		return false;	
		
	},
	
	send: function(totype,toid)
	{
		$('mail_name_error').style.display='none';
		$('mail_phone_error').style.display='none';
		$('mail_reply_error').style.display='none';
		$('mail_subject_error').style.display='none';		
		
		errors = 0;
		
		if (!$F('mail_name').length) { 
			errors++;
			$('mail_name_error').style.display='table-row';
		}
		if (!checkemail($F('mail_reply')) && this.mailtype == 'mail') {
			errors++;		
			$('mail_reply_error').style.display='table-row';
		}

		if (!$F('mail_phone').length && this.mailtype == 'call') {
			errors++;		
			$('mail_phone_error').style.display='table-row';

		}
		
		if (!$F('mail_subject').length) {
			errors++;		
			$('mail_subject_error').style.display='table-row';
		}
		
		if (errors) {return false;}

		new Ajax.Request(this.ajaxUrl, {
	  		parameters:"&type="+this.mailtype+"&name="+$F('mail_name')+"&phone="+$F('mail_phone')+"&mail="+$F("mail_reply")+"&subject="+$F('mail_subject'),						 
		  	onSuccess: function(transport,formname) {
				
				$('mailerfields').reset()
				alert($('success_msg').innerHTML);
				return ;
				
				
			}		
		});				
	
		return false;
		
	}
}
