$(document).ready(function(){
		forgetid = 0;
		state = 0;
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		// Because member_ok is checked (remove when uncheck)
		$("#newmember").removeClass('hiddenstate');		
		$("#mem_ok").bind('click',function(){
			if($("#mem_ok").is(":checked") == true){
					$("#newmember").slideDown(500);
					$("#newmember").removeClass('hiddenstate');
			}else{
					$("#newmember").slideUp(500);
			}
		});
		//Forgot password
		$("#forgetlink").bind("click",function(){
			if(forgetid == 0 ){								   
				$("#forgetpasswordhide").fadeIn(500);
				forgetid = 1;
			}else{
				$("#forgetpasswordhide").fadeOut(500);
				forgetid = 0;	
			}
		});
		//Country
		
/*		$("#country").bind("change",function(){
			if( $("#country").val()==225){							
				$("#stateview").slideDown(500);
			}else{
				$("#stateview").slideUp(500);
			}
		});*/
		
		//Check Login 
		$("#memberlogin").bind("click",function(){
			
			ajaxPath = $("#ajaxpath").val();	
			emLlogin = $("#memberemail").val();
		    mempwd = $("#memberpwd").val();
			logfrom = $("#loginfrom").val();
			//alert(loginfrom)
			if (!filter.test(emLlogin)){
				alert("Please provide a valid email address.");
				$("#memberemail").select();
			}else if (mempwd == ""){
				alert("Enter your password.");
				$("#memberpwd").focus();
			}else{
				$.get(ajaxPath+"ajax_checklogin.php",{inputeml:emLlogin,inputpwd:mempwd,loginfrom:logfrom},
				function(data){
				//alert(data)
					if (data==1){
						location.href='preview.php';
					}else if(data==2){
						location.href='member_detail.php';
					}else{
						alert("*** Invalid User or Password ***");
						$("#memberemail").select();
					} 
				})
			}//End else
			

			
			
		});
		
		
		

/////////////////////////////////New member Or  sign up ######################

		$("#custregist").bind("click",function(){
	
		if($("#editnow").val()!=""){
			CheckmemberEdit($("#editnow").val());	
		}else{
			if($("#customername").val() == ""){
				alert('Please type your name.');
				$("#customername").focus();
				return false;
			}else if(!filter.test($("#customeremail").val())){
				alert("Please provide a valid email address.");
				$("#customeremail").select();
				return false;
			}else if($("#customertel").val()==""){
				alert("Please enter your telephone number.");
				$("#customertel").focus();
				return false;
			}else if($("#customerzip").val()==""){
				alert("Please enter your zip/postal code.");
				$("#customerzip").focus();
				return false;
			}else if($("#customeraddress").val()==""){
				alert("Please enter your address.");
				$("#customeraddress").focus();
				return false;
			}else if($("#country :selected").val() == ""){
				alert("Please select  your country.");
				$("#country").focus();
				return false;	
			}else{
				newmember();	
			}
		}//End check	
	});	
		
	
});
	//Check password  (for new member)
	function newmember(){
		if($("#mem_ok").is(":checked")==true){
			  if($("#password").val() ==""){
				alert("Please enter  password.");
				$("#password").focus();
			}else  if($("#password").val() != $("#re_password").val()){
				alert('Your password and confirm password not match');	
				$("#re_password").select();
				
			 }else{checkemail('');}
	
		}else{checkemail('');}
	}	//End function 
	
	//Checking already exist email
	function checkemail(memberid){
		ajaxPath = $("#ajaxpath").val();	
	   	$.get(ajaxPath+"ajax_emailalready.php",{email:$("#customeremail").val(),memid:memberid},
	   	function(data){
			//alert(data)
			if(data==1){
				alert("This email already exist.");
				$("#customeremail").select();
			}else{
					//if(confirm("Submit Your Information?")==true){
						//document.register.submit();
						$("#formregister").submit();
				//	}
			}
		});
	}
	
//Edit 	 member Information 
	function Enableul(){
		$("#customername").attr('disabled',false);
		$("#customeremail").attr('disabled',false);
		$("#customertel").attr('disabled',false);
		$("#customerzip").attr('disabled',false);
		$("#customeraddress").attr('disabled',false);
		if($("#country").val() =='225'){
			$("#country").attr('disabled',false);
			$("#state").attr('disabled',false);
		}else{
			$("#country").attr('disabled',false);
		}
			$("#buttonedit").attr('disabled',false);
	}
	
			//Edit member information 
	function CheckmemberEdit(Cusid){
		//	alert(typeof($("#password").val()));

				var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;								
					if($("#customername").val() == ""){
						alert('Please type your name.');
						$("#customername").focus();
						return false;
					}else if(!filter.test($("#customeremail").val())){
						alert("Please provide a valid email address.");
						$("#customeremail").select();
						return false;
					}else if($("#customertel").val()==""){
						alert("Please enter your telephone number.");
						$("#customertel").focus();
						return false;
					}else if($("#customerzip").val()==""){
						alert("Please enter your zip/postal code.");
						$("#customerzip").focus();
						return false;
					}else if($("#customeraddress").val()==""){
						alert("Please enter your address.");
						$("#customeraddress").focus();
						return false;
					}else if($("#country :selected").val() == ""){
						alert("Please select  your country.");
						$("#country").focus();
						return false;	
					}else if(typeof($("#password").val()) !="undefined"){
						 if($("#password").val() != $("#re_password").val()){
							alert('Your password and confirm password not match');	
							$("#re_password").select();
							return false;	
						}else{
								if(Cusid){
									checkemail(Cusid);
								}else if($("#memberid").val()){
									checkemail($("#memberid").val());	
								}else{
									checkemail('');
								}
						}
					}else{//end 
						$("#formregister").submit();
					}
			}//End function 
	
	function GetrightmemberInfo(){
		ajaxPath = $("#ajaxpath").val();	
	   	$.get(ajaxPath+"ajax_customereditform.php",{},
	   	function(data){
		//$(".rightmemberinfo").slideUp(500,function(){
		$(".memdetail").html(data).slideDown(800);//});
		//$("#confirm").addClass("disablebutton");
		//$("#confirm").attr("disabled",true);
		
		});
			
	}

	function Selectpayment(){
		//if(confirm('Confirm submit your information?')==true){
			$("#okstate").val('1');
			$("#formregister").submit();
		//}
	
	}
	function pWdtomember(){
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;				
		if(!filter.test($("#forgetpwd").val())){
			alert("Please provide a valid email address.");
			$("#forgetpwd").select();
		}else{
			ajaxPath = $("#ajaxpath").val();	
			$.get(ajaxPath+"ajax_sendforgetpwd.php",{email:$("#forgetpwd").val()},
			function(data){
				if(data == 0 ){
					alert("Your email address not found. Please change your password again.");
					$("#forgetpwd").select();
				}else{
					$("#forgetpasswordhide").fadeOut(300,function(){
					$("#showtextresult").html('<span style="color:#000">Your password  sent complete.Please check  your mailbox.</span>').slideDown(1000,function(){
					 setTimeout(function(){
					$("#showtextresult").slideUp(1000,function(){forgetid = 0;	$("#forgetpwd").val('');}); }, 3000 );	});});
				}
			});
		}
	}
	

	
	

