$(document).ready(function() {
	$("#bul").button();
	$("#uyelogin").dialog("destroy");

		var email = $("#keposta"),
			password = $("#kpass"),
			allFields = $([]).add(email).add(password),
			tips = $(".validateTips");
			
		var ssncc;

		var uemail = $("#eposta"),
			upassword = $("#sifre"),
			ucpassword = $("#sifrecheck"),
			yenisifre = $("#yenisifre"),
			yenisifrecheck = $("#yenisifrecheck"),
			uallFields = $([]).add(uemail).add(upassword).add(ucpassword);
					
	function updateTips(t) {
			tips
				.text(t)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips(n + " uzunlugu en az "+min+" en fazla "+max+" olmalidir.");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		function checkPass() {
			if ( !(upassword.val() == ucpassword.val()) ) {
				upassword.addClass('ui-state-error');
				ucpassword.addClass('ui-state-error');
				updateTips('Sifreler farkli.');
				return false;
			} else {
				return true;
			}

		}
		
		function yenicheckPass() {
			if ( !(yenisifre.val() == yenisifrecheck.val()) ) {
				yenisifre.addClass('ui-state-error');
				yenisifrecheck.addClass('ui-state-error');
				updateTips('Sifreler farkli.');
				return false;
			} else {
				return true;
			}

		}
		
	$("#uyelogin").dialog({
			autoOpen: false,
			width: 270,
			modal: true,
			resizable:false,
		
				buttons: {
			
				'Gönder': function() {
					var bValid = true;
					uallFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(email,"E-posta",6,80);
//					bValid = bValid && checkLength(password,"Sifre",5,16);
				
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"e-posta hatali");
					bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Hatali karakter : a-z 0-9");
					
					if (bValid) {
													  
							  var http, ssnc;
							  var params='kusrs=' + $('#keposta').val() + '&kpass=' + $('#kpass').val();
							  var usere=$('#keposta').val();
							  var browser = navigator.appName;
							  var today=new Date() 
							//  var random_num = (Math.round(today.getTime()));
							
							  if(browser == "Microsoft Internet Explorer"){
								  http = new ActiveXObject("Microsoft.XMLHTTP");
							  }else{http = new XMLHttpRequest();}	
						
								  http.open('post', 'uye/loginkontrol.php',true);
								  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
								  http.setRequestHeader("Content-length", params.length);
								  http.send(params);
								  http.onreadystatechange  = function() {
									  if(http.readyState == 4)
									  {
										  //document.getElementById('ustbar').innerHTML = '<div style="color:red">' + http.responseText +'</div>';
										  $ssnc=http.responseText;								
										  $ssnc1 = $ssnc.split(',,');
										  ssncc='';
										  if ($ssnc1[0]==99) 
										  {
											  ssncc=$ssnc1[0];
											  $('#uyelogin').dialog('close');
											  document.getElementById('create-user').innerHTML='Üye Panel';											  
											  $('#u1').val($ssnc1[1]);										
											  $('#u2').val($ssnc1[2]);
											  location.reload(true);	
										  }
										  if ($ssnc==88) 
										  {
											  password.addClass('ui-state-error'); email.addClass('ui-state-error');
											  updateTips("E-Posta veya Sifre yanlis.");
										  }							 
									  }
								  } //degisiklik.
					http.send(null);
					}	
				},
				'Vazgeç': function(){
					$(this).dialog('close');					
					}


			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
			
		})
		
	$("#uyetebrik").dialog({autoOpen: false,width: 400,height:250,modal: true,resizable:true,
						   buttons: { 'Tamam': function(){$("#sifredegis").dialog('close'); $(this).dialog('close');} }
		})
	$("#uyepanel").dialog({autoOpen: false,width: 400,height:250,modal: true,resizable:true,
						   buttons: { 'Tamam': function(){$("#uyepanel").dialog('close');$(this).dialog('close');} }
		})
	
	$("#sifrehatirlat").dialog({autoOpen: false,width: 400,height:250,modal: true,resizable:true,
							   buttons: { 'Tamam': function(){$("#uyelogin").dialog('close');$(this).dialog('close');} }
		})
	
	$("#create-user").button();  
	$("#create-user").click(function() { 
			
	if ($("#u1").val()=='') {
			document.getElementById("uyelogin").style.visibility="visible";  			
			$("#uyelogin").dialog('open');
			}
	else {document.getElementById("uyepanel").style.visibility="visible"; 	$("#uyepanel").dialog('open') ;}
			
	});
		
	$("#killme").click(function() 	{		
			$("#uyepanel").dialog("close");		location.href='?uye=logout';
			});
	
	
	$("#sifremiunuttum").click(function() 	{

					var bValid = true;
					uallFields.removeClass('ui-state-error');
					
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"e-posta hatali");
								
					if (bValid) {
													  
							  var http, ssnc;
							  var params='kusrs=' + $('#keposta').val();

							  var browser = navigator.appName;
							  var today=new Date() 
							//  var random_num = (Math.round(today.getTime()));
							  if(browser == "Microsoft Internet Explorer"){
								  http = new ActiveXObject("Microsoft.XMLHTTP");
							  }else{http = new XMLHttpRequest();}	
							  							
								  http.open('post', 'sifrem.php',true);
								  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
								  http.setRequestHeader("Content-length", params.length);
								  http.send(params);

								  						
								  http.onreadystatechange  = function() {
									if(http.readyState == 4){
									//document.getElementById('uyelogin').innerHTML = http.responseText;
									$ssnc=http.responseText;								
									if ($ssnc==99) {
										document.getElementById("sifrehatirlat").style.visibility="visible"; 
										$("#sifrehatirlat").dialog("open");
										}
									if ($ssnc==88) {password.addClass('ui-state-error'); 
													email.addClass('ui-state-error');
													updateTips("E-Posta adresiyle herhangi bir üyelik tespit edilemedi.");
													}
									if ($ssnc==77) {	
													updateTips("Bir iç hata olustu. Lütfen daha sonra tekrar deneyin. ");
													}
										 
									  }							
									  
								  } //de?i?iklik.
							http.send(null);
					}	
				});
$("#sifrechange").click(function() 	{document.getElementById("sifredegis").style.visibility="visible";  $("#sifredegis").dialog("open");})															

$("#bul").click(function() {document.getElementById('araform').submit();})
															   
$("#sifredegis").dialog({
			autoOpen: false,
			width: 400,
			modal: true,
			resizable:false,
				buttons: {
			
				'KAYDET': function() {
					var bValid = true;
					uallFields.removeClass('ui-state-error');
					$('#eskisifre').removeClass('ui-state-error');
						bValid = bValid && yenicheckPass();	
						bValid = bValid && checkLength($('#eskisifre'),"Eski Sifre",5,16);
						bValid = bValid && checkLength($('#yenisifre'),"Yeni Sifre",5,16);
						bValid = bValid && checkRegexp(yenisifre,/^([0-9a-zA-Z])+$/,"Hatali karakter : a-z 0-9"); 
					if (bValid) {
													  
							  var http, ssnc;
							  var params='sifre=' +  $('#yenisifre').val() + '&oldpass=' +  $('#eskisifre').val()  + '&id=' +  $('#u2').val();	
						
							  var browser = navigator.appName;
							  var today=new Date() 
							  if(browser == "Microsoft Internet Explorer"){
								  http = new ActiveXObject("Microsoft.XMLHTTP");
							  }else{http = new XMLHttpRequest();}	
							  							
								  http.open('post', 'uye/sifrechange.php?ynt=sifrechange',true);
								  						
								  http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-9");
								  http.setRequestHeader("Content-length", params.length);
								  http.send(params);
								  						
								  http.onreadystatechange  = function() {
									if(http.readyState == 4){
										
									//document.getElementById('ustbar').innerHTML = http.responseText;
									
									$ssnc=http.responseText;								
									//alert($ssnc);
									if ($ssnc==99) {										
										document.getElementById("uyetebrik").style.visibility="visible"; 
										$("#sifredegis").dialog("close");
										$("#uyetebrik").dialog("open");
										}
									if ($ssnc==88) {password.addClass('ui-state-error'); 
													$('#eskisifre').addClass('ui-state-error');
													updateTips("Eski Sifreniz dogrulanamamistir.");
													}
									if ($ssnc==77) {	
													updateTips("Bir iç hata olustu. Lütfen daha sonra tekrar deneyin. ");
													}
										 
									  }							
									  
								  } //de?i?iklik.

					}	
				},
				'Vazgeç': function(){
					$(this).dialog('close');
					}


			},
			close: function() {
				updateTips("");
				$('#eskisifre').val('').removeClass('ui-state-error');
				uallFields.val('').removeClass('ui-state-error');
			}
		})
  });
