﻿	$(function(){
		// เข้ามาครั้งแรกโหลด comment เข้ามาด้วย
		/*$('#commentarea').html('<div id ="loading" align ="center"  style ="text-align:center;padding-top:20px;font-size:12px;"><img src="image_lp/loading1.gif"  /><br><br>กำลังโหลดข้อมูล...</div>');
		$('#commentarea').load("showprospect_comment.php?randval="+ Math.random());
		*/
		// คลิ๊กปุ่มส่งความคิดเห็น
		$("#bntcomment").click(function(){
			
			var checkstep1 = false;
			var checkstep2 = false;
			var checkstep3 = false;
			
			var getname = $("#namecomment").val();
			var getemail = $("#emailcomment").val();
			var gettxtcomment = $("#textcomment").val();
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			
			if(getname == ''){
					$("#errorformname").html('ป้อนชื่อก่อน');
				}else{
					$("#errorformname").html('');
					checkstep1 = true;
				}
				
			if(getemail == ''){
					$("#errorformemail").html('ป้อนอีเมล์ก่อน');
				}else if(!emailReg.test(getemail)) {
					$("#errorformemail").html('รูปแบบอีเมล์ไม่ถูกต้องลองใหม่');
				}else{
					$("#errorformemail").html('');
					checkstep2 = true;
				}
			
			if(gettxtcomment == ''){
					$("#errorformtext").html('ป้อนข้อความก่อน');
				}else{
					$("#errorformtext").html('');
					checkstep3 = true;
				}
			
			if((checkstep1 == true) && (checkstep2 == true) && (checkstep3 == true)){
					$("#loadingcomment").fadeIn("normal");				
					$.post("showprospect_comment.php", { ppname: getname,  ppemail: getemail,pptext:gettxtcomment },
						function(data){
							if(data == "addcommentsuccess"){ // บันทึกเรียบร้อยแล้ว
							
								$('#commentarea').html('<div id ="loading" align ="center"  style ="text-align:center;padding-top:20px;font-size:12px;"><img src="image_lp/loading1.gif"  /><br><br>กำลังโหลดข้อมูล...</div>');
								$('#commentarea').load("showprospect_comment.php?randval="+ Math.random());
								
								$("#namecomment").val('');
								$("#emailcomment").val('');
								$("#textcomment").val('');
								$("#namecomment").focus();
								$('#loadingcomment').fadeOut(500, function() {
								$(this).remove();
								});
							} // end if
					 }); // end post
				} // end if
			
			return false;
		});	
	});