$(function(){

	$("div#divStep2 input[type='radio']").checkBox(); 
	$("div#divStep2 input[type='checkbox']").checkBox(); 

	
	/*--------Step 1--------*/
	$("div#divStep1 input#btnSubmit").show();		
	var result = false;	
	
	$("div#divStep1 form#frmSignup").submit(function(){
		objForm = $(this);
		objForm.find(".bgAlert").removeClass("bgAlert");
		$("div#divStep1 div#Memberalert").hide();	
		objForm.find("ul#emailinactive").hide();		
		objForm.find("ul#emailexist").hide();
		objForm.find("ul#passwordnotallow").hide();
		//alert(objForm.find("input#email").hasClass("pass"));	
		var Alert = false;
		
		
		//objForm.attr("action","MemberSignUp.asp?step=2");
		//alert($(this).attr("id"));
		//alert(objForm.attr("action"));
		if (!objForm.find("input#email").hasClass("pass")) {
			if (!validateStep1(objForm)) {
				$("div#divStep1 div#Memberalert").show();	
				
				
				return false;
				//return true;
			} else {
				
				// Check Email Exist
				$.ajax({
						url: "inc/ajaxMember.asp",
						data: "actionType=check_email&email="+objForm.find("input#email").val()+"&password="+objForm.find("input#password").val(),
						type: "post",
						cache: false,
						complete: function (result) {
							var data=result.responseText;
							//alert(data);
							if (data=="0") {
								$("div#divStep1 span#Memberalert").hide();	
								objForm.find("ul#emailexist").hide();	
								result = true;
								objForm.find("input#email").addClass("pass");
								//alert("data : "+objForm.hasClass("pass"));
								
								objForm.submit();
							} else if (data=="6") {
								
								$("div#divStep1 span#Memberalert").show();	
								objForm.find("input#password").addClass("bgAlert");
								
								objForm.find("ul#passwordnotallow").show();
								return false;
								
							} else {
								
								$("div#divStep1 span#Memberalert").show();	
								objForm.find("input#email").addClass("bgAlert");
								
								if (data == "1") {
									objForm.find("ul#emailexist").show();		
									
								} else if (data == "2") {
									objForm.find("ul#emailinactive").show();		
									
								}
								return false;	
							}
								
						}
				});
			} 
		} else {
				//alert("pass");
				return true;	
		}
		
		
		return false;
	});
	
	function validateStep1(objForm){
		var valid = true;
		if (objForm.find("select#title").val()=="NULL") {
			objForm.find("select#title").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("select#title").removeClass("bgAlert");	
		} 	

		if ((objForm.find("input#firstname").val()=="") || (objForm.find("input#firstname").val()=="Firstname")) {
			objForm.find("input#firstname").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("input#firstname").removeClass("bgAlert");	
		}	
		
		if ((objForm.find("input#lastname").val()=="") || (objForm.find("input#lastname").val()=="Lastname")) {
			objForm.find("input#lastname").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("input#lastname").removeClass("bgAlert");	
		}	
		
		if (objForm.find("select#countryID").val()=="NULL") {
			objForm.find("select#countryID").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("select#countryID").removeClass("bgAlert");	
		}	
		
		if (objForm.find("input#city").val()=="") {
			objForm.find("input#city").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("input#city").removeClass("bgAlert");	
		}	
		if (objForm.find("input#zipcode").val()=="") {
			objForm.find("input#zipcode").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("input#zipcode").removeClass("bgAlert");	
		}	
		//if (objForm.find("input#phone").val()=="") {
//			objForm.find("input#phone").addClass("bgAlert");
//			valid = false;
//		} else {
//			objForm.find("input#phone").removeClass("bgAlert");	
//		}	
//		if (objForm.find("input#mobile").val()=="") {
//			objForm.find("input#mobile").addClass("bgAlert");
//			valid = false;
//		} else {
//			objForm.find("input#mobile").removeClass("bgAlert");	
//		}	
		if (IsEmpty(objForm.find("input#email").val()) || (!IsEmail2(objForm.find("input#email").val()))) {
			objForm.find("input#email").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("input#email").removeClass("bgAlert");	
		}	
		
		if (objForm.find("input#password").val()=="") {
			objForm.find("input#password").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("input#password").removeClass("bgAlert");	
		}	
		
		if (IsEmpty(objForm.find("input#repassword").val()) || (objForm.find("input#password").val()!=objForm.find("input#repassword").val())) {
			objForm.find("input#repassword").addClass("bgAlert");
			valid = false;
		} else {
			objForm.find("input#repassword").removeClass("bgAlert");	
		}	
		return valid;
	}
	
	/*--------Step 2--------*/
	$("div#divStep2 div#step2Button").show();
	$("div#divStep2 input#enewsAccept:eq(0)").click(function(){
	  
	  panel = $("div#divStep2 div#divEnews");
	  //$("div#divStep2 div#divEnews").show();
	  if (panel.is(":hidden")) {
        //panel.slideDown("slow");
		panel.show();
      } else {
        //panel.hide();
      }
	  //alert("test");
				   
	});
	
	$("div#divStep2 input#enewsAccept:eq(1)").click(function(){
	  
	  panel = $("div#divStep2 div#divEnews");
	 // alert(panel.is(":visible"));
	  if (panel.is(":visible")) {
        //panel.slideUp("slow");
		panel.hide();
      } else {
        //panel.hide();
      }
			   
	});
	
	$("div#divStep2 form#frmSignup").submit(function(){
			
		objForm = $(this);
		// validate form
		
		if (objForm.attr("action") != "MemberSignUp.asp?step=1") {
		
			if (!validateStep2(objForm)) {
				$("div#divStep2 div#Memberalert").show();	
				return false;
				//return true;
			} else {
				return true;	
			}
		
		} else {
			return true;	
		}
		//alert($(this).attr("id"));
		//alert(objForm.attr("action"));
			
			//$(this)
		//alert(objForm.attr("action"));
		return false;
		
	});
	
	$("div#divStep2 img#linkBack").click(function(){
		
		objForm = $("div#divStep2 form#frmSignup");
		objForm.attr("action","MemberSignUp.asp?step=1");
		
		objForm.submit();
		//alert("??");
	});
		
	function validateStep2(objForm){
		var valid = true;
		if (!ValidateOption(objForm.find("input#enewsAccept"))) {
			//objForm.find("input#enewsAccept").addClass("bgAlert");
			objForm.find("div#divEnewsAccept").addClass("bgAlert");
			valid = false;
		} else {
			//objForm.find("input#enewsAccept").removeClass("bgAlert");	
			objForm.find("div#divEnewsAccept").removeClass("bgAlert");
		} 	
		
		if ((objForm.find("input#enewsAccept").eq(0).filter(":checked").length == 1) && !ValidateOption(objForm.find("input#enewsID"))) {
			//objForm.find("input#enewsID").addClass("bgAlert");
			objForm.find("tr.divEnewsID").addClass("bgAlert");
			valid = false;
		} else {
			//objForm.find("input#enewsAccept").removeClass("bgAlert");	
			objForm.find("tr.divEnewsID").removeClass("bgAlert");
		} 	
		
		
		return valid;
	}
	
	
	/*--------Step 3--------*/

	var resultStep3 = false;
	$("div#divStep3 div#step3Button").show();
	$("div#divStep3 form#frmSignup").submit(function(){
		var validStep3 = true;
		
		if (resultStep3) {
			
			return true;
		} else {
		
			objForm = $(this);
			$("div#divStep3 div#Memberalert").hide();	
			//if (objForm.attr("action") == "MemberSignUp.asp?step=2") {
			//	return true;
			//} else if (objForm.attr("action") == "MemberSignUp.asp?step=1") {
			if (objForm.attr("action") != "") {
				return true;
			} else {
				
				if (objForm.find("input#capcha2").val()=="") {
					$("div#divStep3 div#Memberalert").show();	
					objForm.find("input#capcha2").addClass("bgAlert");
					validStep3 = false;
				} else {
					objForm.find("input#capcha2").removeClass("bgAlert");	
				}	
				
				if (validStep3) {
					//alert("valid");
					$.ajax({
								url: "inc/ajaxMember.asp",
								data: objForm.serialize(),
								type: "post",
								cache: false,
								complete: function (result) {
									var data=result.responseText;
									//alert(data);
									if (data =="0" ) {
										//alert("pass");
										//window.location.replace("MemberSignUpThanks.asp");
										resultStep3 = true;
										//objForm.attr("action","inc/incSignUpScript.asp");
										objForm.submit();
										//return true;	
									} else {
										$("div#divStep3 div#Memberalert").show();	
										if (data == "5") {
											// Captcha Wrong
											objForm.find("img#imgCaptcha").attr("src","inc/captchaImage.asp?c="+Math.random());
											objForm.find("input#capcha2").addClass("bgAlert");
											
										} else {
											resultStep3 = true;
											objForm.attr("action","MemberSignUp.asp?step=1&err="+data);
											objForm.submit();
										}
										//alert(data);
										
										//objForm.find("input#email").addClass("bgAlert");
											
									}
										
								}
						});
				}
		
			}
			
		}
		// validate form
		
		//if (objForm.attr("action") != "MemberSignUp.asp?step=1") {
		
		//	objForm.attr("action","MemberSignUp.asp?step=3");
		
		//}
		//alert($(this).attr("id"));
		//alert(objForm.attr("action"));
			
			//$(this)
		//alert(objForm.attr("action"));
		return false;
		
	});
	
	$("div#divStep3 img#linkBack").click(function(){
		
		objForm = $("div#divStep3 form#frmSignup");
		objForm.attr("action","MemberSignUp.asp?step=2");
		
		objForm.submit();
		//alert("??");
	});
		
	
	/*  */
	function ValidateOption(object){
		if (object.filter(":checked").length > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	function IsEmail2(email) {
			var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if (regex.test(email)){
						//var c=Checkemail(email);

						return true;
					}else {return false;}
}

	function isNumeric2(form_value)
		{
			if (form_value.match(/^\d+$/) == null)
				return false;
			else
				return true;
		}

	
});
