$(document).ready(function(){

		$("input#start_date, input#stop_date").datepicker({
				showOn:'button', buttonImage: 'images/button/btn_calendar.gif', buttonImageOnly: true,
				changeMonth: true, changeYear: true,
				beforeShow: customdateRange,
				dateFormat: 'yy-mm-dd'
			});
for(i=1;i<7;i++){
	if ($("div.img a.image"+i).length > 0 ) {
		$("div.img a.image"+i).lightBox();
	}
	if($("div.img a.image_program"+i).length > 0){
		$("div.img a.image_program"+i).lightBox();
	}
}
for(i=0;i<12;i++){
	if($("a.images_product"+i).length > 0){
			$("a.images_product"+i).lightBox();
	}
}

for(i=1;i<13;i++){
	if($("a.imagegallery"+i).length > 0){
			$("a.imagegallery"+i).lightBox();
	}
}
	$("a.image").lightBox();
	$("a.imagegallery").lightBox();
	$("a.image_program").lightBox();

	$("form#frmenew #btnregis_enew").click(function() {

			// Email

				var validationError = false;
				if ( !$("form#frmenew input#txt_email").val() ) {
					$("form#frmenew input#txt_email").addClass("focus");
					$("form#frmenew input#txt_email").next("span").removeClass().addClass("error").html("Please enter a valid your email address.");
					alert('กรุณากรอกข้อมูลอีเมล์ด้วยคะ');
					$("form#frmenew input#txt_email").focus();
					validationError = true;
				}else{
					if((IsEmail($("form#frmenew input#txt_email").val()))==false){
						$("form#frmenew input#txt_email").focus();
						//$("form#frmenew input#txt_email").next("span").removeClass().addClass("error").html("Please enter a valid your email address.");
						validationError = true;
						alert('กรุณากรอกข้อมูลอีเมล์ให้ถูุกต้องตามรูปแแบบด้วยคะ')
					}
				}

				$("form#frmenew input[type=text], form#frmenew input[type=password], form#frmenew select, form#frmProfile textarea").each( function(){

					$(this).focus( function() {
						$(this).select();
						if ( !$(this).hasClass("focus") ) $(this).addClass("focus");

					});
					$(this).blur( function() {
						//$(this).parent().parent().children("th").removeClass("error");
						//$("form#frmJoinUs label#err"+$(this).attr("id") ).empty();
						$(this).next("span").empty();

						if ( $(this).hasClass("focus") ) $(this).removeClass("focus");
					});
				});

		//alert(validationError);
			if(validationError==false){
				var str =  $("form#frmenew").serialize();
				var url=$("form#frmenew").attr("action");
				//alert(url);
					$.ajax({
						url: "api/enew.php",
						data: str,
						type: "post",
						cache: false,
						complete: function (result) {
							//alert(result.responseText );
							//return false;
							switch( result.responseText ) {

								//return false;
								case "1":
									alert("แก้ไข  e-newletter เรียบร้อยแล้วคะ");
									window.location.href =url;

								break;
								case "0":
									alert("สมัครรับ e-newletter เรียบร้อยแล้ว");
									window.location.href = url;
									break;
								case "2":
									alert("email นี้ มีอยู่ในระบบแล้ว");
									window.location.href = url;
									break;
								case "3":
									alert("ยกเลิกข้อมูลเรียบร้อยแล้วคะ");
									window.location.href = url;
									break;
								case "4":
									alert("เปลี่ยนแปลงข้อมูลเรียบร้อยแล้วคะ");
									window.location.href = url;
									break;
								case "5":
									alert("emaiนี้ ไม่สามารถ unsubscribe ได้เนื่องจากยังไม่ได้สมัครรับ  e-newletter ");
									window.location.href = url;
									break;
								}
						},
						error: function (result) {
							document.location.reload();
						}
					});

			}
			return false;

	});

	$("a#btsearch").click(function(e) {

			var validationError = false;
			var amphur = $("select#amphur_id").val();

			if (amphur == "") {
				alert("please insert Amphur ");
				$("select#amphur_id").focus();
				validationError = true;
				return false;
			}

			var form = $("form#frm_search");
			form.submit();
			return !validationError;

	});
	$("a#bt_Search").click(function(e) {
		var validationError = false;
		var start_date = $("input#start_date").val();
		var stop_date = $("input#stop_date").val();
		var program = $("select#program").val();
		var keyword = $("input#keyword").val();
			if(start_date=="" && stop_date=="" && program=="" && keyword==""){
				 validationError = true;
				 alert('กรุณากรอกข้อมูลที่ต้องการค้นหาด้วยนะคะ');
			}else if(start_date!="" && stop_date!="" ){
					validationError = false;
			}else if(start_date!="" && stop_date=="" ){
					validationError = true;
					alert('กรุณากรอกวันที่สิ้นสุดด้วยนะคะ');
			}else if(start_date=="" && stop_date!="" ){
					validationError = true;
					alert('กรุณากรอกวันที่เริ่มต้นด้วยนะคะ');
			}
		//	alert(validationError);
			var form = $("form#frmsearchdata");
			if(validationError==false){
				form.submit();
			}
			return !validationError;

	});

	if ($("div.flash").length > 0 ) {
		$("div.flash").each(function() {
			wmode = ( $(this).metadata().flashvars.wmode != undefined ? $(this).metadata().flashvars.wmode :  "opaque" ); // opaque, transparent;
			$( this ).flash({
					src: $(this).metadata().flash,
					width: $(this).metadata().width,
					height: $(this).metadata().height,
					wmode: wmode, menu: "false",
					flashvars: { title: $(this).metadata().flashvars.title }
				}, { version: 8 }
			);
		});
	}

	if ($("div#Print").length > 0 ) {
		document.getElementById('print_area').style.display = 'none';
		$("a.btPrint").click(function(e) {
			e.preventDefault();
			print_sample_report();


		});
	}

});

function customdateRange(input) {
			return {
				minDate: (input.id == "stop_date" ? $("input#start_date").datepicker("getDate") : null),
				maxDate: (input.id == "start_date" ? $("input#stop_date").datepicker("getDate") : null)
			};
}
function IsEmail(email) {
			var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if (regex.test(email)) return true;
					else return false;
}
