$(document).ready(function()
{
	

	//Geri dön tuşu
	
	$("#cancel_button").click(function() 
	{
		 $("#msgbox").removeClass();
		 $("#recaptcha").hide();
		 $("#msgbox").hide();
		 $("#msgbox1").hide();
		 $("#reg_form_box").show();
		 document.reg_form.cancel.value=2;
		 
		 return false;	
		
	});
	
	
	
	//Geri Dön tuşu bitti
	
	//Form işleniyor...
	
	$("#reg_form").submit(function()
	{
		
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Form İşleniyor....').fadeIn(1000);
		
		
		$("#sifre").hide();
		
		var username =  document.reg_form.username.value;
		var email = 	document.reg_form.email.value;
		var password = 	document.reg_form.password.value;
		var password1 = document.reg_form.password1.value;
		var sex = 		document.reg_form.sex.value;
		var desc = 		document.reg_form.desc.value;
		var cancel = 	document.reg_form.cancel.value;
		var validate = 	document.reg_form.validate.value;
		var birthday_day = 			document.reg_form.birthday_day.value;
		var birthday_month =		document.reg_form.birthday_month.value;
		var birthday_year = 		document.reg_form.birthday_year.value;
		
		//Validation name
		if (username == "") {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('İsminizi giriniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.username.focus();   
      					return false;  
	  
    	}   
		
		
		var GoodChars = "aAbBcçÇCdDeEfFgGğĞhHıIİijJkKlLmMnNoOöÖpPrRsSşŞtTuUüÜvVyYzZwWxXqQ "
		for (i =0; i <= username.length -1; i++) {
			
			
						if (GoodChars.indexOf(username.charAt(i)) == -1) {
		
							$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
			
			  					//Hataları al ve göster
			 					 $(this).html('Geçersiz karakter girdiniz...').addClass('messageboxerror').fadeTo(900,1);
			 

							});		   
      					
	   							document.reg_form.username.focus();   
      							return false;  
						} 
		} 
		
		var parcala = username.split(" ");
		
		if(parcala.length<2)
		{
			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Soyisminizi giriniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.username.focus();   
      					return false;  	
		}
		
		//Validation email
		
		if (email == "") {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('E-Posta adresinizi giriniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.email.focus();   
      					return false;  
	  
    	}   
		
		
		if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)

		{
					
					
					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('E-Posta geçerli değil...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.email.focus();   
      					return false;

   		} 
		
		
		//Validation Password
		
		if (password == "") {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Şifrenizi giriniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.password.focus();   
      					return false;  
	  
    	}  
		
		
		
		
				if (password.length <6) {
       
    					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Şifreniz 6 karakterden kısa olamaz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.password.focus();   
      					return false;  
		
		
				}
				
				
				var GoodChars = "0123456789aAbBcCdDeEfFgGhHijJkKlLmMnNoOpPrRsStTuUvVyYzZwWxXqQ@_-"
		for (i =0; i <= password.length -1; i++) {
			
			
						if (GoodChars.indexOf(password.charAt(i)) == -1) {
		
							$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
			
			  					//Hataları al ve göster
			 					 $(this).html('Geçersiz karakter girdiniz...').addClass('messageboxerror').fadeTo(900,1);
			 

							});		   
      					
	   							document.reg_form.password.focus();  
								$("#sifre").show();
      							return false;  
						} 
		} 
		
		
		if (password1 == "") {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Şifrenizi tekrar giriniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.password1.focus();   
      					return false;  
	  
    	}  
		
		
		if(password!=password1){
			
								
								$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
			
			  					//Hataları al ve göster
			 					 $(this).html('Farklı şifre girdiniz...').addClass('messageboxerror').fadeTo(900,1);
			 

							});		   
      					
	   							document.reg_form.password1.focus();  
      							return false;  
								
			
			
		}
		
		
		//Validation Gender
		
		if ((sex == "0") || (sex=="")) {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Cinsiyetinizi seçiniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.sex.focus();   
      					return false;  
	  
    	}  
		
		
		if (birthday_day == "-1") {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Doğum gününüzü giriniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					  
      					return false;  
	  
    	}  
		
		if (birthday_month == "-1") {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Doğum gününüzü giriniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					  
      					return false;  
	  
    	}  
		
		if (birthday_year == "-1") {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Doğum gününüzü giriniz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					  
      					return false;  
	  
    	}  
		var currentTime = new Date();
		var year = currentTime.getFullYear()-18;
		
		
		if (birthday_year > year) {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Yaşınız 18 den küçük olamaz...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					  
      					return false;  
	  
    	}  
		//Validation Description
		
		if (desc == "") {   
		
		
      					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('Kendinizi tanımlayınız...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.desc.focus();   
      					return false;  
	  
    	}  
		
		if ((desc.length <10) || (desc.length >500)) {
       
    					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			
			  			//Hataları al ve göster
			 			 $(this).html('En az 10 en çok 500 karakter kullanınız...').addClass('messageboxerror').fadeTo(900,1);
			 

					});		   
      					
	   					document.reg_form.desc.focus();   
      					return false;  
		
		
				}
		
		


		//check the username exists or not from ajax
		
		$.post("check_user.php",{ email:email,validate:validate,rand:Math.random()} ,function(data)
        {
			
			switch(data){
							case "failure":
											$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
											{ 
			
			  									//Hataları al ve göster
			 								 	$(this).html('Bu E-Posta Adresi Zaten Kayıtlı').addClass('messageboxerror').fadeTo(900,1);
			 

											});		   
      					
	   											document.reg_form.email.focus();   
      											return false;  
							break;
							
							case "bos":
											$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
											{ 
			
			  									//Hataları al ve göster
			 								 	$(this).html('Lütfen E-Posta Adresinizi Giriniz').addClass('messageboxerror').fadeTo(900,1);
			 

											});		   
      					
	   											document.reg_form.email.focus();   
      											return false;  
							break;
							
							
							case "ok":
											$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
											{ 
											  		//add message and change the class of the box and start fading
											  		$(this).html('Lütfen Bekleyin.....').addClass('messageboxok').fadeTo(900,1,
											  function()
											  { 
												 	//ikinci dive geçiş
												 	$("#reg_form_box").hide();
												 	$("#recaptcha").show();
												 
													 if(cancel==2){
													 Recaptcha.reload();
													 }
												 
											  });return false;
											  
											});
											
							break;
	
							default:
							
											$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
											{ 
			
			  									//Hataları al ve göster
			 								 	$(this).html(data).addClass('messageboxerror').fadeTo(900,1);
			 

											});		   
      					
	   											document.reg_form.email.focus();   
      											return false;  
							
							
							break;
	
		 

		  }
		  
         
				
        });
 		return false; //not to post the  form physically
	});
	
	
	$("#rec").click(function() 
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox1").show();
		$("#msgbox1").removeClass().addClass('messagebox1').text('Form İşleniyor....').fadeIn(1000);
		
		var username =  			document.reg_form.username.value;
		var email = 				document.reg_form.email.value;
		var password = 				document.reg_form.password.value;
		var password1 = 			document.reg_form.password1.value;
		var sex = 					document.reg_form.sex.value;
		var birthday_day = 			document.reg_form.birthday_day.value;
		var birthday_month =		document.reg_form.birthday_month.value;
		var birthday_year = 		document.reg_form.birthday_year.value;
		var country = 				document.reg_form.country.value;
		var city = 					document.reg_form.city.value;
		var desc = 					document.reg_form.desc.value;
		var recaptcha = 			document.reg_form.recaptcha_response_field.value;
		var recaptcha_challange = 	document.reg_form.recaptcha_challenge_field.value;
	

			  		
		$.post("registration.php",{ username:username,email:email,password:password,sex:sex,birthday_day:birthday_day,birthday_month:birthday_month,birthday_year:birthday_year,country:country,			city:city,desc:desc,recaptcha_response_field:recaptcha,recaptcha_challenge_field:recaptcha_challange,rand:Math.random() } ,function(sonuc)
        							{
									
									
										switch(sonuc){
										
													case "mailgonderildi":
													
													 $("#msgbox1").fadeTo(200,0.1,function() //start fading the messagebox
													{ 
			
			  											$(this).html('Lütfen Bekleyin.....').addClass('messageboxok1').fadeTo(900,1,
														  function()
														  { 
																//ikinci dive geçiş
																$("#recaptcha").hide();
																$("#complete").show();
															 
															 
														  });return false;
													});		
													
													break;
													
													case "guvyanlis":
													
													 $("#msgbox1").fadeTo(200,0.1,function() //start fading the messagebox
													{ 
			
			  											//Hataları al ve göster
			 			 								$(this).html('Güvenlik Kodu Yanlış').addClass('messageboxerror1').fadeTo(900,1);
														Recaptcha.reload();
														return false;
													});		
													
													break;

													case "failure":
													
													 $("#msgbox1").fadeTo(200,0.1,function() //start fading the messagebox
													{ 
			
			  											//Hataları al ve göster
			 			 								$(this).html('Veritabanı Hatası').addClass('messageboxerror1').fadeTo(900,1);
			 
														 return false;
													});		
													
													break;
													
													case "bos":
													
													 $("#msgbox1").fadeTo(200,0.1,function() //start fading the messagebox
													{ 
			
			  											//Hataları al ve göster
			 			 								$(this).html('Boş alanlar var').addClass('messageboxerror1').fadeTo(900,1);
			 
														 return false;
													});		
													
													break;
													
													case "dogumgunu":
													
													 $("#msgbox1").fadeTo(200,0.1,function() //start fading the messagebox
													{ 
			
			  											//Hataları al ve göster
			 			 								$(this).html('Doğumgününüzü giriniz').addClass('messageboxerror1').fadeTo(900,1);
			 
														 return false;
													});		
													
													break;
													
													case "isimgecersiz":
													
													 $("#msgbox1").fadeTo(200,0.1,function() //start fading the messagebox
													{ 
			
			  											//Hataları al ve göster
			 			 								$(this).html('Geçersiz karakter girdiniz').addClass('messageboxerror1').fadeTo(900,1);
			 
														 return false;
													});		
													
													break;
													
													case "emailgecersiz":
													
													 $("#msgbox1").fadeTo(200,0.1,function() //start fading the messagebox
													{ 
			
			  											//Hataları al ve göster
			 			 								$(this).html('E-Posta geçerli değil').addClass('messageboxerror1').fadeTo(900,1);
			 
														 return false;
													});		
													
													break;
			
													default:
									
													$("#msgbox1").fadeTo(200,0.1,function() //start fading the messagebox
													{ 
					
														//Hataları al ve göster
														$(this).html(sonuc).addClass('messageboxerror1').fadeTo(900,1);
					 										return false;  
		
													});		   
														
									
									
													break;
			
		
				  						}
										
									
										
									}); 
										return false; 
					

				
        });
		

	
});