$(document).ready(function() {
 $('#EmailtoAgent_m').css('background-position', '0 0px');
  
 $('#EmailtoAgent_m').click(function() { Com_Acc('#EmailtoAgent_b', this) });
 
  $('#btnEmailSubmit').click(function() {
     if(validateEmailMessage())
      callAjaxGrid($('#agentID').val(), 
    $('#TextBox_Name').val(), $('#TextBox_Email').val(), $('#TextBox_Phone').val(), $('#TextBox_Msg').val());    
   });

});

function callAjaxGrid(aid, nam, eml, pho, cmt) {
  $('.loading').html('<div class="loader" title="Loading....."></div>');
  $.ajax({
    url: '/ajax_server/property/svr_agent.aspx',
    data:
        'agentID=' + aid + '&nam=' + URLEncode(nam) + '&eml=' + URLEncode(eml) + '&pho=' + pho + '&cmt=' + URLEncode(cmt) ,
    type: 'GET',
    datatype: 'text/html',
    timeout: 100000,
    error: function(err) {
      $('.loading').empty();
      //alert('There is something wrong with the server, or your session is expired, please relogin or try again...','Error');
    },
    success: function(xml) {
      $('.loading').empty();
       $('#Label_Msg').html(xml);
    }
  });
}      

	function validateEmailMessage()
	{
		document.getElementById('Label_Msg').innerHTML = ''
		if (document.getElementById('TextBox_Name').value == '') {
			document.getElementById('Label_Msg').innerHTML = 'Name is required!'
			return false;
		}

		if (document.getElementById('TextBox_Email').value == '') {
			document.getElementById('Label_Msg').innerHTML = 'Email is required!'
			return false;
		} else if (!validEmail(document.getElementById('TextBox_Email').value)) {
			document.getElementById('Label_Msg').innerHTML = 'Please enter a valid Email.'
			return false;
    }	

		if (document.getElementById('TextBox_Msg').value == '') {
			document.getElementById('Label_Msg').innerHTML = 'Remarks is required!'
			return false;
		}		

		if (document.getElementById('TextBox_Name').value == '' && document.getElementById('TextBox_Email').value == '' && document.getElementById('TextBox_Msg').value == '') return false;
		
		return true;
	}
