// JavaScript Document
$(document).ready(function() {
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').fadeOut(1000);
		$('.window').fadeOut(1000);
		document.getElementById("txtHint").innerHTML="";
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).fadeOut(1000);
		$('.window').fadeOut(1000);
		document.getElementById("txtHint").innerHTML="";
	});	
	
	
	$('.window .sublog').click(function (e) {
		//Cancel the link behavior
		var log_name = $("#log_name");
		var log_pass = $("#log_pass");
		if(log_name.val().length==0 || log_pass.val().length==0){
			document.getElementById("txtHint").innerHTML="گذرواژه و رمز عبور خود را وارد کنید";
			return false;
		}
		else{
			function GetXmlHttpObject(handler){   
   				var objXMLHttp=null  
   				if (window.XMLHttpRequest){   
       				objXMLHttp=new XMLHttpRequest()   
   				}   
   				else if (window.ActiveXObject){   
       				objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")   
   				}   
   				return objXMLHttp   
			}   
  
			function stateChangedLogin(){  
   				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
					document.getElementById("login_response").innerHTML='';
					if(xmlHttp.responseText==-1){
						document.getElementById("txtHint").innerHTML="پست الکترونیک اشتباه است";}
					else if(xmlHttp.responseText==-2){
						document.getElementById("txtHint").innerHTML="رمز عبور اشتباه است";}
					else{
							document.getElementById("txtHint").innerHTML="";
							/*$.post("include/set_login.php", {loghid: xmlHttp.responseText}, function(data){  
								if(data == "1")  
								{  
									log_form.submit(); 
								}  
							});  */
								var cookie;
								var chkb = document.getElementById('autologin');
								if(chkb.checked) cookie=1;else cookie=0;
								$.ajax({
									type: "GET",
									url: "include/set_login.php",
									data: 'user_id='+xmlHttp.responseText+'&cookie='+cookie,
									dataType: 'json',
									beforeSend: function(){
										document.getElementById("login_response").innerHTML='<img align="absmiddle" src="images/20-19.gif">';},
									success: function(msg){	
										if(msg==1) $("#log_form").submit(); 
										else document.getElementById("txtHint").innerHTML="خطا در ورود";
									}
								});
							}
					}   
   				else {   
           			//alert(xmlHttp.status); 
					document.getElementById("login_response").innerHTML='<img align="absmiddle" src="images/20-19.gif">';
   				}  
			}   
  
			// Will populate data based on input   
			function htmlDataLogin(url, qStr){   
   				if (url.length==0){   
       				document.getElementById("txtHint").innerHTML="";   
       				return;   
   				}   
   				xmlHttp=GetXmlHttpObject()   
   				if (xmlHttp==null){   
       				alert ("Browser does not support HTTP Request");   
       				return;   
   				}   
  
   				url=url+"?"+qStr;   
   				url=url+"&sid="+Math.random();   
   				xmlHttp.onreadystatechange=stateChangedLogin;
   				xmlHttp.open("GET",url,true) ;   
   				xmlHttp.send(null);   
			}  

			htmlDataLogin('include/login_chek', 'log_name='+log_name.val()+'&log_pass='+log_pass.val());
			return false;
		}
	})
	
});

