function isValidEmail(str)
  {
    var s = $.trim(str);
    var at = "@";
    var dot = ".";
    var lat = s.indexOf(at);
    var lstr = s.length;
    var ldot = s.indexOf(dot);

    if (s.indexOf(at)==-1 ||
       (s.indexOf(at)==-1 || s.indexOf(at)==0 || s.indexOf(at)==lstr) ||
       (s.indexOf(dot)==-1 || s.indexOf(dot)==0 || s.indexOf(dot)==lstr) ||
       (s.indexOf(at,(lat+1))!=-1) ||
       (s.substring(lat-1,lat)==dot || s.substring(lat+1,lat+2)==dot) ||
       (s.indexOf(dot,(lat+2))==-1) ||
       (s.indexOf(" ")!=-1))
    {
      return false;
    }

    return true;
  }
$(document).ready(function() {


	$('#commentform').submit(function() {

		var comment = $('#comment').val();
		var author = $('#author').val();
		var email = $('#email').val();		
		var message = '';
		var focus = '';
		var check='';
		if( author != undefined)
		{
			if(author =='')
			{
				message = 'ขออภัยค่ะ กรุณาใส่ชื่อด้วยค่ะ';
				focus = '#author';
			}
		}
		if(message=='')
		{			
			if( email != undefined)
			{
				check = isValidEmail(email);
				if(!check)
				{
					message = 'ขออภัยค่ะ กรุณาใส่อีเมล์ที่ถูกต้อง';
					focus = '#email';
				}
			}
		}
		if(message!='')
		{
			alert(message);
			$(focus).focus();
			return false;
		}
		else if ( comment == '')
		{
			alert('ขออภัยค่ะ กรุณาใส่คอมเมนท์ด้วยค่ะ'); 
			$('#comment').focus();
			return false;
		}

	});


});