//Service : Register

$post = JRequest::get ('post');  
global $HTTP_RAW_POST_DATA;
 
$post = json_decode($HTTP_RAW_POST_DATA, true);
/*$post['password']='123456';
$post['conpassword']='123456';
$post["name"]='vishal';
$post["username"]='vishal@harmis.com';
$post["email"]='vishal@harmis.com';
//$post["country"]='99';
$post["dob"]='12/12/1990';
$post["gender"]='1';
//$post["phno"]='938393837';

$post['devicetoken']='5356a2a1c4079b05036b2a354441218dba8382bfb91518159da1afb4cbeadb91';
$post['deviceaccess']='1';*/



$sql = 'Select username from #__users where username="'.$post["username"].'"'; 
$db->setQuery($sql);
$userexist = $db->LoadObject();
 
$pass=md5($post['password']);


if(!$userexist)
{
				$mainframe = JFactory::getApplication();
				$user 		= clone(JFactory::getUser());
									
				$usersConfig = JComponentHelper::getParams( 'com_users');
				/*$newUsertype = $usersConfig->get('new_usertype');
				
				if ($newUsertype) 
				{
					$newUsertype = 'Registered';
				}*/
				$newUsertype = 'Registered';
				$user->set('id', 0);
				$user->set('name', $post["name"]);
				$user->set('username', $post["username"]);
				$user->set('password', $pass);
				$user->set('usertype', $newUsertype);
				$user->set('email', $post["email"]);
				/*if(@$post["dob"])
				$user->set('dob', $post["dob"]);
				if(@$post["gender"])
				$user->set('gender', $post["gender"]);*/

			
				$date =JFactory::getDate();
				$jdate = new JDate;
				$user->set('registerDate', $jdate->toSql());
	
				// If user activation is turned on, we need to set the activation information
				 $useractivation = $usersConfig->get( 'useractivation' );
				
			//	$useractivation = 0;
				/*if ($useractivation == '1')
				{
					jimport('joomla.user.helper');
					$user->set('activation', JApplication::getHash( JUserHelper::genRandomPassword()) );
					$user->set('block', '0');
				}*/
				$user->set('block', '0');
				$sql = 'Select id from #__usergroups where title="Registered"'; 
				$db->setQuery($sql);
				$res = $db->LoadObject();
				
				
					
				//$user->save();
	
			 
				if($user->save())
				{	if($user->id!=739 && $user->id!=742){
					$delete = 'DELETE FROM #__user_usergroup_map WHERE user_id='.$user->id.' AND group_id=8';
					$db->setQuery($delete);
					$db->query();
					
					
					$delete = 'DELETE FROM #__user_usergroup_map WHERE user_id='.$user->id;
					$db->setQuery($delete);
					$db->query();
					}	
						
					$sql = 'Select id from #__usergroups where title="Registered" OR title="Application user"';; 
					$db->setQuery($sql);
					$res = $db->LoadObjectList();
							
				for($i=0;$i<count($res);$i++)
				{			
					$sql_temp="insert into #__user_usergroup_map (user_id,group_id) values('".$user->id."','".$res[$i]->id."')";
					$db->setQuery($sql_temp);
					$db->Query();
				}	
							
					
					$insert_appuser='INSERT INTO #__jetour_appusers (`user_id`,`deviceaccess`,`dob`,`gender`,`pushnotification`,`favoritenotification`) VALUES ("'.$user->id.'","'.$post["deviceaccess"].'","'.$post["dob"].'","'.$post["gender"].'","1","1")';
					$db->setQuery($insert_appuser);
					$db->query();	 
								 
					
					$sql="SELECT id FROM #__jetour_appalldevices WHERE devicetoken ='".$post['devicetoken']."' AND deviceaccess=".$post['deviceaccess'] ;	
					$db->setQuery($sql);
					$devicefound = $db->loadResult();
					
					if($devicefound){
						$update='UPDATE #__jetour_appalldevices SET user_id="'.$user->id.'",deviceaccess="'.$post['deviceaccess'].'" WHERE  devicetoken="'.$post['devicetoken'].'"';
						$db->setQuery($update);
						$db->Query();
						 
					}else{
						$deinsert='INSERT INTO #__jetour_appalldevices (`user_id`,`deviceaccess`,`devicetoken`) VALUES ("'.$user->id.'","'.$post['deviceaccess'].'","'.$post['devicetoken'].'")';
						$db->setQuery($deinsert);
						$db->Query();
						 
					}
					
					
					$sql="SELECT u.id,u.name,u.username,u.email,au.dob,au.gender FROM #__users as u LEFT JOIN #__jetour_appusers as au ON au.user_id=u.id WHERE u.id=".$user->id;
					$db->setQuery($sql);
					$userdata = $db->LoadObject();
					$profile = array();
					$profile['userId']   = $userdata->id;
					$profile['name']   = $userdata->name;
					$profile['username']  = $userdata->username;
					$profile['email']  = $userdata->email;
					$profile['gender']  = $userdata->gender;
					$profile['dob']  = $userdata->dob;
					$profile['favoritecount']  = 0;
					
					$arr = array('success' => 1, 'username' => $userdata->username, 'profile' => $profile);
					echo json_encode($arr);exit;
			}
			else
			{
				$mess = 'Please try agan with other detail';
				$arr = array('success' => 0, 'message' => $mess);	
				echo json_encode($arr); exit;
			}
	}else
		{
			$arr = array('success' => '0','message' => 'User Already Exists');
			echo json_encode($arr);exit;
		}