// JavaScript Document
	$(document).ready(function(){
		st=$("#countperson").val();
		get_selectTreatment(st,'');
		
		checkBranch();
		
		//block reserve
		$('#b_reserv, #reserv_day, #reserv_mon, #reserv_year').change(function(){
			checkBranch();
		});
	});
	
	function checkBranch(){
		/*if($('#b_reserv').val() == 3){
			current_date = new Date($("#reserv_year").val() +"-"+ $("#reserv_mon").val() +"-"+ $("#reserv_day").val());
			expire_time = new Date("2011-09-30");
			//alert(current_date+" ---- "+expire_time+" -- "+(y +"-"+ m +"-"+ d));
			if(current_date > expire_time || isNaN(current_date)){
				alert("Ratchadaphisek is closing on 30th september 2011. We apologize for any inconvenience this may causes. Should the new location is inconvenient, we welcome you to visit any of our other branches.");
				$("#reserv_day").val('30');
				$("#reserv_mon").val('09');
				$("#reserv_year").val('2011');
			}
		}else if($('#b_reserv').val() == 6){
			current_date = new Date($("#reserv_year").val() +"-"+ $("#reserv_mon").val() +"-"+ $("#reserv_day").val());
			alert(current_date);
			start_time = new Date("2011-10-11");
			if(current_date < start_time || isNaN(current_date)){
				alert("Terminal21 branch will be opened on 11 October 2011");
				$("#reserv_day").val('11');
				$("#reserv_mon").val('10');
				$("#reserv_year").val('2011');
			}
		}*/
	}


	//Get Treatment Show
	function get_selectTreatment(pson,act){
		if($("#treat_id").val()!=""){
			$("#objsele").val($("#treat_id").val());
		}else{
			$("#objsele").val('');
		}
		d=$("#reserv_day").val();
		m=$("#reserv_mon").val();
		y=$("#reserv_year").val();
		if(d.length){dserv=y+"-"+m+"-"+d;}else{dserv="";}

		br=$("#b_reserv").val();
		trmt=$("#treat_id").val();
		$.get("person_no.php", { pno: pson,brch:br,tmt:trmt,dservice:dserv},
		function(data){ 
				dml=data.split("||");
				$(".reserv_info").fadeOut(300,function(){
				$(".reserv_info").html(dml[0]).fadeIn(400);
				$(".treatmentlist").html(dml[1]).fadeIn(400);
			});	
		});
	}
	
	//Add Data Into Array
	function addvalue(tx){
 		var n = $("input:checked").length;
	if(n==0){document.getElementById("submit").disabled=true;}else{document.getElementById("submit").disabled=false;}
		var datasel=$("#objsele").val();
		var strcheckbox="checkbox_"+tx;
		datachk=document.getElementById(strcheckbox);
	if(datachk.checked== true){
		if(datasel==""){strsel=datachk.value;}else{strsel = datasel+"###"+datachk.value;}
		$("#objsele").val(strsel);
	}else{
		str=datasel.split("###");
		for(s=0;s<str.length;s++){if(str[s]==datachk.value){str[s]="";}else if(str[s]=="###"){str[s]="";}else{str[s]=str[s];}}
		sfv=new Array();
		for(s=0;s<str.length;s++){if(str[s]!=""){sfv.push(str[s]);}} Textstr=sfv.join("###");$("#objsele").val(Textstr); }
	}//end function 	
	
	//Show Information
	function showdata(ps,typac,branc){
		if(typac!="show"){document.getElementById("submit").disabled=true;}
			treatsel=$("#objsele").val();
			$.get("add_session.php", { pno: ps,treatment:treatsel,act:typac,Bran:branc},
			function(data){ 
		$(".reserv_info").fadeOut(300,function(){
			$(".reserv_info").html(data).fadeIn(400);
		});
		$(".treatmentlist").html("");
		$('.edit_link').click(function(){
				$('.current_person').removeClass('current_person');		   
				$(this).parent().parent().addClass('current_person');
			});	
		});
	}
	
	//Edit
	function edit_reserv(Pno,valtreat){
			$("#objsele").val(valtreat);
			treatsel=$("#objsele").val();
			br=$("#b_reserv").val();
			$.get("person_no.php", {pno: Pno,brch:br,action:"edit"},
			function(data){ $(".treatmentlist").fadeOut(300,function(){	$(".treatmentlist").html(data).fadeIn(400);});});
		}
		
		//Delete Unset Session
		function Clear_session(UserNo,Bran){	
		if(UserNo!=""){
			if(confirm('Confirm delete?')==true){
			$.get("clear_session.php", {user:UserNo},function(data){showdata(data,'show',Bran);});	}
		}else{
			if((Bran>0) || (Bran=="") ){
				if(confirm("If you will change branch, all entered \n Reservation Information will be deleted. \nWould you like to continue changing branch?")==true){	
					$.get("clear_session.php", {user:UserNo},function(data){get_selectTreatment(1);});	}
				}else{
					$.get("clear_session.php", {user:UserNo},function(data){get_selectTreatment(1);});	
				}
			}	
	}

//Next Step
	function Check_Sparserv(){
		d=$("#reserv_day").val();
		m=$("#reserv_mon").val();
		y=$("#reserv_year").val();
		dsel = y+m+d;
		today=$("#today").val();
		svday=$("#dservice").val();
		id_pro=$("#idpro").val();
		period_start = $('#period_start').val();

		var todayc = new Date()
	 	ccc=(todayc.getUTCFullYear())+"/"+(todayc.getMonth()+1)+"/"+todayc.getDate();
		strDate1 = y+"/"+m+"/"+d;
		strDate2 = ccc;	
		datDate1 = Date.parse(strDate1); //reserve date
		datDate2 = Date.parse(strDate2); 
		datediff = ((datDate1-datDate2)/(24*60*60*1000)) ;
		
		reserv_date = Date.parse(y+"-"+m+"-"+d);

		//alert(reserv_date + " - " + Date.parse(period_start));
		
		if(datediff < 3){
			alert('Reservations should be made 3 days in advance.');
			return false;
		}
		
	//	if((parseInt(dsel)-today)<3){alert('Reservations should be made 3 days in advance.');return false;}
		else if(parseInt(dsel)>svday && id_pro !="" ){
			alert("You are out of promotion period");
			return false;
		}else if(reserv_date < Date.parse(period_start) && id_pro != ""){
			alert("You are out of promotion period");
			return false;
		}else{
			if(confirm("Check your reservation information.\nAre you ready go to next step?")==true){
				document.reserv_step1.submit();
			}
		}
	}
		
		//Add Remember ME	
		function addremember(){
			if(document.getElementById("rememer_me").checked==true){$("#rememer_me").val("remember");
			}else{$("#rememer_me").val("");}
		}
		
		//Function CHECK LOGIN
		function check_customerLogin(){
			var Path=$("#urlpath").val();
			user=$("#user_namelogin").val();
			pwd=$("#pass_wordlogin").val();
			rmb=$("#rememer_me").val();	
			if(user==""){alert('Enter username / email.');$("#user_namelogin").focus();}
			else if(pwd==""){alert('Enter password.');$("#pass_wordlogin").focus();}
			else{$.get(Path+"ajax_chklogin.php", { eml:user,pwd:pwd,sck:rmb},	function(data){ 
				strresult=data.split("#");
				if(strresult[0]=="notfound"){alert(" Your username/email  not found");$("#user_namelogin").select();	}
				else if(strresult[0]=="notallow"){alert("Please activate your account");}
				else if(strresult[0]=="incorect"){alert("Your password incorect");$("#pass_wordlogin").select();}
				else if(strresult[0]=="pass"){document.loginform.submit();}});}
		}
		//Show Require username+password Form
		function showform_datalogin(){
			if(document.getElementById('use_mem').checked==true){ $("#use_mem").val(1);$(".datalogin").css('display','block');}
			else{$(".datalogin").css('display','none');	$("#use_mem").val(0);$("#user_pwd").val('');	
						$("#user_cmfpwd").val('');$("#user_hint").val('');	$("#user_display").val('');}
		}
	//Check form  Register 
	function Check_reservStep2(){
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		Registype=$("#typeregist").val();
		if($("#userpri_email").val()==""){alert("Please enter your email.");$("#userpri_email").focus();}
		else if (!filter.test($("#userpri_email").val())){	alert("Please provide a valid email address.");$("#userpri_email").select();}
		else if($("#user_prefix").val()==0){	alert("Select your prefix.");$("#user_prefix").focus();}
		else if($("#user_firstname").val()==""){alert("Enter your firstname.");$("#user_firstname").focus();}
		else if($("#user_lastname").val()==""){alert("Enter your lastname.");$("#user_lastname").focus();}
		else if($("#user_country").val()==0){alert("Select your country.");$("#user_country").focus();}
		else if($("#user_telno").val()==""){	alert("Enter your telephone number");$("#user_telno").focus();}
		else if($("#user_captchatxt").val()==""){alert("Enter captcha.");$("#user_captchatxt").focus();}
		else if($("#user_captchatxt").val()!=""){var capt=$("#user_captchatxt").val();
		$.get("../check_captcha.php", { cptxt:capt},function(data){if(data==1){
		alert("The characters you entered didn't match the word verification.\nPlease try again.");
		$("#user_captchatxt").select();}else{Usercheck_register(Registype);}});}
	}
	
	
	//Information For login
	function Usercheck_register(Tp){
		 if( Tp=="Reserv"){
			if(document.getElementById('use_mem').checked==true){
			if($("#user_display").val()==""){alert("Enter your display name.");$("#user_display").focus();	}
			else if($("#user_display").val()!=""){var disname=$("#user_display").val();
			$.get("../check_displayname.php", { dispname:disname},function(data){
			if(data=="notpass"){alert("This display name already exist.");$("#user_display").select();}
			else{checkpaasword_login();}});}
			}else{confirmSubmit();}
		 }else{
			if($("#user_display").val()==""){alert("Enter your display name.");$("#user_display").focus();	}
			else if($("#user_display").val()!=""){var disname=$("#user_display").val();
			$.get("../check_displayname.php", { dispname:disname},function(data){
			if(data=="notpass"){alert("This display name already exist.");$("#user_display").select();}
			else{checkpaasword_login();}});}
		 }
	}
	//Function CheckPassword Login
	function checkpaasword_login(){
		if($("#user_pwd").val()==""){alert("Enter your password.");$("#user_pwd").focus();}
		else if($("#user_pwd").val().length<6){alert("Enter password minimum of 6 characters in length.");$("#user_pwd").select();}
		else if($("#user_pwd").val() !=$("#user_cmfpwd").val()){
		alert("Your password and Confirm password not match ");$("#user_cmfpwd").focus();}
		else if($("#user_hint").val()==""){alert("Enter your hint.");$("#user_hint").focus();	}
		else if($("#user_hint").val()==$("#user_pwd").val()){alert("The hint must be different from your password.") ;
		$("#hint").focus();return false;}else{var pemail=$("#userpri_email").val();
		$.get("../check_email.php", { premail:pemail},function(data){if(data==1){alert("This email already exist.");
		$("#userpri_email").select();}else {confirmSubmit();	}});	}
	}
	//Confirm Register
	function confirmSubmit(){
		Registype=$("#typeregist").val();
		if(Registype=="Register"){/*Register*/}else{//Reerv
		if(confirm("Submit Your Information?")==true){document.user_register.submit();}}
	}
	
	
