$(function(){
//เข็คฟอร์ม Supmit หน้า Landing Page จาก GetResponse ...  ////////////////////////////////////////////////////////////////////////////////
	$("#webform_submit").live("click",function(){
		
		var EmailValidateSuccess = false;	
		var NameValidateSuccess = false;	
		var TelValidateSuccess = false;	
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var emailToVal = $("#email").val();
		if(emailToVal == '') {
			$("#email_error").show("normal");
			$("#email_error1").hide("normal");
		} else if(!emailReg.test(emailToVal)) {
			$("#email_error1").show("normal");
			$("#email_error").hide("normal");
		}else{
			$("#email_error1").hide("normal");
			$("#email_error").hide("normal");			
			EmailValidateSuccess = true;						
		}
		// เข็คฟอร์มชื่อ  ///
		var subjectVal = $("#full_name").val();
		if(subjectVal == '') {
			$("#name_error").show("normal");
			$("#name_error1").hide("normal");			
		}else if((subjectVal.length) < 4){
			$("#name_error1").show("normal");
			$("#name_error").hide("normal");
		}else{
			$("#name_error").hide("normal");
			$("#name_error1").hide("normal");						
			NameValidateSuccess = true;			
		}
		// เช็คเบอร์โทรศัพท์  ///
		var telVal = $("#phone1").val();
		if(telVal == '') {
			$("#tel_error").show("normal");
			$("#tel_error1").hide("normal");
		}else if((telVal.length) < 9){
			$("#tel_error1").show("normal");
			$("#tel_error").hide("normal");
		}else{
			$("#tel_error1").hide("normal");
			$("#tel_error").hide("normal");
			TelValidateSuccess = true;
			
			if((EmailValidateSuccess == true) && (NameValidateSuccess == true) && (TelValidateSuccess == true)){
				
			// บันทึกค่า prospect ลงฐานข้อมูล
			var uidmb = $('#usernamegdi').val();
			var ppname = $('#full_name').val();
			var ppemail = $('#email').val();
			var pptel = $('#phone1').val();
			var pppass = $('#custom_passwd').val();
			var ppupline = $('#custom_sponsorid').val(); // code upline
			var ppemailupline = $("#custom_sponsoremail").val(); // อีเมล์อัพไลน์
			var ppnameupline = $("#custom_sponsorname").val(); // ชื่ออัพไลน์
			var pptelupline = $("#custom_sponsortel").val(); // เบอร์โทรศัพท์อัพไลน์
			var ppcustomcamp = $("#custom_camp").val(); // ชื่อแคมเปญ
			
			//ทำการ post ไปที่ไฟล์ landingpage_to_db.php เพื่อบันทึกข้อมูลลงฐานข้อมูล
			var recorddb = $.post("landingpage_to_db.php",{ ppname:ppname,ppemail:ppemail,pptel:pptel,pppass:pppass,ppupline:ppupline,ppemailupline:ppemailupline,ppnameupline:ppnameupline,pptelupline:pptelupline,ppcustomcamp:ppcustomcamp } ,function(data){
					if(data == "duplicatedmail"){
						$("#boxsubmit").html('<div id="duplicatedmail" style="font-size:14px; color:red;padding-top:90px; text-align:center"><br /><span style="font-size:18px"><b>Opp! มีอีเมล์นี้แล้วในระบบ</b></span><br />ลองไปเช็คอีเมล์ของท่านเพื่อเข้าระบบ <br /><br /><span style="font-size:12px"><a href="index.php?userid='+uidmb+'">ลองใช้อีเมล์ใหม่</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="prospect/">เข้าระบบด้วยอีเมล์เดิม</a></span></div>');
						return false;
					}else if(data == "addbfail"){
						alert("ผิดพลาดไม่สามารถบันทึกข้อมูล ลองใหม่อีกครั้ง");
					}else{
						// ระบบหลัก
						//window.location='http://www.21millionaire.com/prospect/';
						
						// ระบบสำรอง				
						//window.location='http://www.gdicenter.ws/prospect_mail.php?ppnames='+ppname+'&ppemails='+ppemail+'&pptels='+pptel+'&pppasss='+pppass+'&ppuplines='+ppupline+'&ppemailuplines='+ppemailupline+'&ppnameuplines='+ppnameupline+'&ppteluplines='+pptelupline;

						//alert("บันทึกข้อมูลเรียบร้อยแล้ว");
						$("#getresponse").submit();
					}
				}); // end $.post()
			} //end if (EmailValidateSuccess == true) && (NameValidateSuccess == true) && (TelValidateSuccess == true)
		} // end else
		return false;
	});	
	
// ฝากข้อความ
		$("#bntcontact").live("click",function(){
			
			var checkstep1 = false;
			var checkstep2 = false;
			var checkstep3 = false;
				
			var uidmb = $('a[rel=icontact]').attr("alt"); // รับค่ามาจาก link	
			
			var getname = $("#iname").val();
			var getemail = $("#iemail").val();
			var getmessage = $("#imessage").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(getmessage == ''){
					$("#errorformtext").html('ป้อนข้อความก่อน');
				}else{
					$("#errorformtext").html('');
					checkstep3 = true;
				}
						
			if((checkstep1 == true) && (checkstep2 == true) && (checkstep3 == true)){
					$.post("header.php?userid="+uidmb, { ppname:getname, ppemail:getemail, ppmessage:getmessage},
						function(data){
							if(data == "addcommentsuccess"){ // บันทึกเรียบร้อยแล้ว
								$('#formcomment').html('<span style="color:#009900; font-size:12px"><b>ได้รับข้อมูลเรียบร้อยแล้ว</b></span><br> <span style="font-size:12px">จะตอบกลับภายใน 24 ชั่วโมง ทางอีเมล์ที่คุณให้ไว้</span><br>');
							} // end if
					 }); // end post
				} // end if
			
			return false;
		});

}); // End function jQuery   

