<?php
session_start();
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
if($_SESSION['userid']!='' && $_SESSION['username']!=''){
	header("Location:chatwindow.php");
	exit;
}
if(isset($_POST['Login']) && ($_POST['Login']!='')){
	$username = $_POST['name'];
	$password = $_POST['password'];
	$sql = mysql_query("SELECT * FROM `mychat_user` WHERE `username` = '".$username."' && `password` = '".$password."'");
	if(mysql_num_rows($sql)>0){
		$row = mysql_fetch_array($sql);
		mysql_query("UPDATE `mychat_user` SET `status`=1,`session_id`='".session_id()."',`lastlogin`='".date('Y-m-d H:i:s')."' WHERE `id`='".$row['id']."'");
		mysql_query("INSERT INTO `mychat_user_login_info`(`userid`, `logintime`, `addrres`, `cityname`, `zipcode`, `lat`, `long`) VALUES ('".$row['id']."','".date("Y-m-d H:i:s")."','".$_POST['Address']."','".$_POST['locality']."','".$_POST['postal_code']."','".$_POST['Latitude']."','".$_POST['Longitude']."')");
		$_SESSION['userid'] = $row['id'];
		$_SESSION['username'] = $row['username'];
		$_SESSION['loginid'] = mysql_insert_id();
		header("Location:chatwindow.php");
		exit;
	}else if($username =='admin' && $password =='pradosh'){
		header("Location:admin.php");
		exit;
	}else{
		$error = 'Please check login details';
	}
}

?>
<!DOCTYPE html>
	<head>
		<title>Login</title>
		<link rel="stylesheet" href="style.css">
		<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
		<script type="text/javascript">
			var componentForm = {
				locality: 'long_name',
				postal_code: 'short_name'
			};
			var geocoder;geocoder = new google.maps.Geocoder();
			function initialize() {
				if (navigator.geolocation) {
					navigator.geolocation.getCurrentPosition(showPosition);
				} else { 
				   alert("Geolocation is not supported by this browser.");
				}
			}
			
			function showPosition(position) {
				document.getElementById('Latitude').value = position.coords.latitude;
				document.getElementById('Longitude').value = position.coords.longitude;
				codeLatLng();
			}
				
				
			function codeLatLng() {
				var lat = document.getElementById('Latitude').value;
				var lng = document.getElementById('Longitude').value;
				var latlng = new google.maps.LatLng(lat, lng);
				geocoder.geocode({'latLng': latlng}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						if (results[1]) {
							for (var i = 0; i < results[0].address_components.length; i++) {
								var addressType = results[0].address_components[i].types[0];
								var val = results[0].address_components[i][componentForm[addressType]];
								if(val != ''){
									if(addressType=='postal_code'){
										var val = results[0].address_components[i][componentForm[addressType]];
										document.getElementById('postal_code').value = val;
									}else{
										if(addressType=='locality'){
											var val = results[0].address_components[i][componentForm['locality']];
											document.getElementById('locality').value = val;
										}
									}
								}
							}
							var address= (results[1].formatted_address);
							document.getElementById('Address').value= results[1].formatted_address;
						}else{
							alert("No results found");
						}
					}else{
						alert("Geocoder failed due to: " + status);
					}
				});
			}
				
			</script>
	</head>
	<body onLoad="initialize()">
		<section class="container">
			<div class="login">
				<h1 style="text-align:center">Login to personal chat</h1>
				<div><?=$error?></div>
				<form autocomplete="off" method='post'>
					<input name="Latitude" type="hidden" value ='' id="Latitude" />
					<input name="Longitude" type="hidden" value='' id="Longitude" />
					<input name="Address" type="hidden" id="Address"/>
					<input name="locality" type="hidden" id="locality"/>
					<input name="postal_code" type="hidden" id="postal_code"/>
					<p><input type="text" name="name" value="" placeholder="Username" autocomplete="off"></p>
					<p><input type="password" name="password" value="" placeholder="Password" autocomplete="off"></p>
					<p class="submit"><input type="submit" name="Login" value="Login"></p>
				</form>
			</div>
		</section>
	</body>
</html>