$post=JRequest::get('post');


//$post['password']='123456';
//$post['conpassword']='123456';
//$post["name"]='anil@harmis.com';
//$post["username"]='anil@harmis.com';
//$post["email"]='anil@harmis.com';
//$post["country"]='99';
//$post["dob"]='1990/06/30';
//$post["gender"]='1';
//$post["phno"]='938393837';

//$post['devicetoken']='5356a2a1c4079b05036b2a354441218dba8382bfb91518159da1afb4cbeadb91';

$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());
					//$pathway 	=$mainframe->getPathway();
					$config		=JFactory::getConfig();
					$authorize	=JFactory::getACL();
					$document   =JFactory::getDocument();					
					$usersConfig = JComponentHelper::getParams( 'com_users');
					$newUsertype = $usersConfig->get('new_usertype');
					
					if ($newUsertype) 
					{
						$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"]);
                                       $user->set('dob', $post["dob"]);
                                        $user->set('phno', $post["phno"]);
                                       $user->set('gender', $post["gender"]);
                                      $user->set('country', $post["country"]);
                                   $user->set('devicetoken', $post["devicetoken"]);
                                 $user->set('deviceaccess', $post["deviceaccess"]);


					//$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));
				
					$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', '1');
					}
					
					$sql = 'Select id from #__usergroups where title="Registered"'; 
					$db->setQuery($sql);
					$res = $db->LoadObject();
				
				
					
			
	
				//	$user->set('groups', $res->id);
					// If there was an error with registration, set the message and display form
						
					if (!$user->save())
					{
						$_SESSION['name']=$post['name'];
						$_SESSION['email']=$post['email'];
						$_SESSION['creator']=$post['creator'];
						$_SESSION['firstname']=$post['firstname'];
						$_SESSION['title']=$post['title'];
						//$this->setRedirect ( 'index.php?option=' . $option . '&view=register');
						//return false;
					}
	
						
				$sql_temp="insert into #__user_usergroup_map(user_id,group_id) values('".$user->id."','".$res->id."')";
			     $db->setQuery($sql_temp);
     			$db->Query();
 				
				$delete = 'DELETE FROM #__user_usergroup_map WHERE user_id='.$user->id.' AND group_id=8';
				$db->setQuery($delete);
				$db->query();
						
					$sql = 'Select id from #__users where email="'.$user->email.'" '; 
					$db->setQuery($sql);
					$userdetail = $db->LoadObject();
					
					$user->set('id', $userdetail->id);
					$post["uid"]=$userdetail->id;

 $insert='INSERT INTO #__naderapp_userfield (`Country`,`uid`,`gender`,`phno`,`dob`) VALUES ("'.$post["country"].'","'.$userdetail->id.'","'.$post["gender"].'","'.$post["phno"].'","'.$post["dob"].'")';
$db->setQuery($insert);
$db->query();

		$sql="SELECT * FROM #__users WHERE id=".$userdetail->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;
		
		$arr = array('success' => 1, 'username' => $userdata->username, 'profile' => $profile);
				echo json_encode($arr);exit;

		}
		
		else
		{
			$arr = array('success' => '0','message' => 'User Already Exists');
   		 echo json_encode($arr);exit;
		}