objSS=document.getElementById('status_section');

$(function(){
		   Init_Login_File();
		   });

function Init_Login_File(){
	$('#button_logout').click(function(){Logout();});
	$('#button_action').click(function(){Login(true);});
	$('#button_public_login').click(function(){
											 $('#username').val('public');
											 $('#password').val('public');
											 Login();
											 });
}

function Logout(){
	$.ajax({
	  type: 'POST',
	  url: js_dir+'ajax_funcs/logout.php',
	  data: '',
	  success: function(ajax_return){
		$('#cabOwnerLoginMenu').slideUp('fast',function(){$('#cabOwnerLogin').slideDown('fast');});
	  }
	});
}

function Login(is_owner){
	var username=$('#username').val();
	var password=$('#password').val();
	
	if (username=='' || password==''){
		//objSS.innerHTML='<h2>Please type in both your username and password.</h2>';
		$('#status_section').html('<h2>Please type in both your username and password.</h2>');
		//alert('Please type in both your username and password.');
		return(false);
	}
	
	//objSS.innerHTML='';
	$('#status_section').html('');
	/*
	url=js_dir+'ajax_funcs/login.php';
	params='username='+username+'&password='+password;
	type='post';
	callback_func='Login_Response()';
	doAjax(url,params,type,callback_func);
	*/
	$.ajax({
	  type: 'POST',
	  url: js_dir+'ajax_funcs/login.php',
	  data: 'username='+username+'&password='+password+'&hitstats='+is_owner,
	  success: function(ajax_return){
		Login_Response(ajax_return);
	  }
	});
	//objSS.innerHTML='<h2>Communicating with server, please wait...</h2>';
	$('#status_section').html('<h2>Communicating with server, please wait...</h2>');
}

function Login_Response(ajax_return){
	if (ajax_return=='0'){
		//objSS.innerHTML='<h2>Username and/or password is not matching. Please try again.</h2>';
		$('#status_section').html('<h2>Username and/or password is not matching. Please try again.</h2>');
		return(false);
	}
	//objSS.innerHTML='<h1>Successfully logged in</h1><p>&nbsp;</p><h2>Please close this window, and click the "attachments" link again to see list of availables files.</h2>';
	//objSS.innerHTML='<h1>Successfully logged in</h1><p>&nbsp;</p><h2>Please <a href="viewfiles.php?tname='+tname+'&id='+id+'">click here</a> to see list of availables files.</h2>';
	if (typeof tname=='undefined')tname='';
	if (typeof id=='undefined')id='';
	
	if (tname!='' && id!=''){
		$('#status_section').html('<h1>Successfully logged in</h1><p>&nbsp;</p><h2>Please <a href="viewfiles.php?tname='+tname+'&id='+id+'">click here</a> to see list of availables files.</h2>');
	}else{
		if (ajax_return=='00'){
			$('#status_section').html('<h1>Successfully logged in</h1>');
		}else{
			//$('#status_section').html('<h1>Successfully logged in</h1><p>&nbsp;</p><h2>Please <a href="hitstats.php?KeepThis=true&TB_iframe=true&height=400&width=600" class="thickbox" title="Hit Stats">click here</a> to see your unit statistics.</h2>');
			document.location=document.location;
		}
	}
}
