<?php
	#Direct Access
	if( ! defined( 'IN_MALL' ) ) exit;
	
	class Functions
	{
		
		public static function mssql_escape($str) {
			$search=array("\\","\0","\n","\r","\x1a","'",'"');
			$replace=array("","","","","","","");
			return str_replace($search,$replace,$str);
		}

		public static function LoggedOut()
		{
			echo <<< EOL
<div id="wrapper">
<body>
    <div style="width: 100%; height: 100%; position: absolute; background-color: rgb(20, 23, 28); z-index: 2147483647; display: none;" id="loading">
        <h1 style="text-align: center; color:#FFF;margin-top:10%;">Loading....</h1>
    </div>

    <div class="login ng-scope" ng-controller="loginController">
        <div class="login-container">
            <div class="login-title">
                <div><h1>Sign in</h1></div>
            </div>
			
			<form method="post" action="#" onsubmit="Login(); return false;" id="form">
<div class="form-group">
				<input type="text" id="username "name="username" placeholder="Username" autocomplete="off" class="form-control login-input login-username" /><br />
				</div>
<div class="form-group">
				<input type="password" id="password" name="password" placeholder="••••••••••" class="form-control login-input login-password" /><br />
				</div>
				<button class="btn btn-default btn-block btn-login">Log in</button/>
				<div class="login-or">
                <div class="separator"><p>OR</p></div>
            </div>
			<input type="button" value="Register" class="btn btn-default btn-block btn-login" onclick="window.location.href='http://atlanticonline.de/?module=register'" />
			</form>
            </div>
    <!-- Load External JS -->
    <script src="css/jquery.js"></script>
    <!-- Load Vendor JS -->
    <script src="css/bootstrap.js"></script>
    <script src="css/angular.js"></script>
    <script src="css/login.js"></script>

</body>
			
			
			
			
			
			<link href="css/login.css" rel="stylesheet" type="text/css">
			<link href="css/bootstrap.css" rel="stylesheet">
			<link href="http://reset5.googlecode.com/hg/reset.min.css" rel="stylesheet" type="text/css">
			<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800" rel="stylesheet" type="text/css">
			<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
			</form>
            </div>
			
EOL;

		}
		
		public static function LoginValidate( $Username , $Password )
		{
			global $Database;
			
			return $Database->prepareAndFetch( "SELECT * FROM tAccounts WHERE sUserName = ? AND sUserPass = ?" , array( $Username , $Password ) );
		}
		
		public static function MakePw( $Username , $Password )
		{
			
			/*$Salt = self::GetUserSalt( $Username );
			
			if( $Salt == null )
			{
				return null;
			}*/
		
			return ( $Password );
		}
		
		public static function SetLogin( $Results )
		{		
			$_SESSION['nEMID'] = $Results['nEMID'];
			
			$_SESSION['userName'] = $Results['sUsername'];
						
			$_SESSION['logIP'] = self::GetIp();
		}
		
		public static function GetUserSalt( $Username )
		{
			global $Database;
		
			return $Database->prepareAndFetch( "SELECT sUserPassSalt FROM tAccounts WHERE sUserName = ?" , array( $Username ) );
		}
		
		public static function LogOut()
		{
			session_destroy();
		}
		
		public static function Redirect( $Url )
		{
			header( 'Location:' . $Url );
		}
		
		public static function GetItemInfo( $Id )
		{
			global $Database;
	
			return $Database->prepareAndFetch( "SELECT * FROM ItemMall.dbo.tItems WHERE nGoodsNo = ? AND nEnabled = 1" , array( $Id ) );
		}
		
		public static function GetUserPoints( $Id )
		{
			global $Database;
	
			return $Database->prepareAndFetch( "SELECT nAGPoints AS nAGPoints FROM tAccounts WHERE nEMID = ?" , array( $Id ) );	
		}
		
		public static function SubtractPoints( $Id , $Amount )
		{
			global $Database;
	
			$Database->prepareAndExecute( "UPDATE tAccounts SET nAGPoints = ( nAGPoints - ? ) WHERE nEMID = ?" , array( $Amount , $Id ) );			
		}

		public static function AddPoints( $Id , $Amount )
		{
			global $Database;
	
			$Database->prepareAndExecute( "UPDATE tAccounts SET nAGPoints = ( nAGPoints + ? ) WHERE nEMID = ?" , array( $Amount , $Id ) );			
		}
		
		public static function AddItem( $UserId , $ItemInfo )
		{
			global $Database;
			
			$Sql = "INSERT INTO tPurchases([nAGID],[nPrice],[nGoodsNo],[nQuantity],[nUsed],[sIP], [nIsGift],[nStatus] )
					VALUES(? , ?,?,1,0,?,0,0)";
					
			$Params = array( $UserId , $ItemInfo['nPrice'] , $ItemInfo['nGoodsNo'] , self::GetIp() );
			
			$Database->prepareAndExecute( $Sql , $Params );
		}
		
		public static function GetIp()
		{
			return $_SERVER['REMOTE_ADDR'];
		}
		
		public static function Error( $Message , $Div = false )
		{
			if( $Div === true )
			{
				echo <<< EOL
				<div class="alert alert-error">
					{$Message}<!--<button type="button" class="close" data-dismiss="alert">&times;</button>-->
				</div>
EOL;
				exit;
			}
		
			echo <<< EOL
			{$Message}
EOL;
			exit;
		}
		
		public static function PrintCategories()
		{
			global $Database;
			
			$i = 0;
			
			$Results = $Database->queryAndFetch("SELECT * FROM ItemMall.dbo.tCats WHERE nEnabled = 1");
			
			foreach( $Results as $Key => $Info )
			{
				$Info = (object) $Info;
			
				echo <<< EOL
				<li id="lbl_{$Info->nCat}"><a href="#" onclick="loadCat({$Info->nCat}); return false;">{$Info->sName}</a></li>
				
EOL;
				$i++;
			}
			
			return $i;
		}
		
		public static function GetCat( $Id )
		{
			global $Database;
			
			return $Database->prepareAndFetch("SELECT nCat FROM ItemMall.dbo.tCats WHERE nEnabled = 1 AND nCat = ?" , array( $Id ) );
		}
		
		public static function GetItemsByCat( $Id )
		{
			global $Database;
			
			return $Database->prepareAndFetch( "SELECT * FROM ItemMall.dbo.tItems WHERE nCat = ? AND nEnabled = 1" , array( $Id ) );
		}
		
		public static function PrintItem( $Info )
		{
			$Unique = ( $Info->nID );
		
			echo <<< EOL
  <div class="item-row col-lg-3 col-md-6 ng-scope ng-repeat="(key, item) in items track by {$Info->sName}">
                                <div class="item-icon"><div class="icon-placeholder" ng-style="{'background-image:'./Template/img/{$Info->sImg}'}" style="background-image: url(./Template/img/{$Info->sImg});"></div></div>
                                <div class="item-info">
                                    <h5 class="ng-binding">{$Info->sName}</h5>
                                    <div class="item-cost ng-binding">{$Info->nPrice}</div>
                                    <div class="item-units ng-binding">{$Info->nLot}<p>Unit</p></div>
                                    <p class="item-details ng-binding" data-toggle="tooltip" data-placement="bottom" title="Scroll for more details">
                                        {$Info->sDescript}
									</p>
									
								</div><button onclick="buyItem({$Info->nGoodsNo})" class="btn btn-default btn-block btn-buy">Buy</button>
                                </div>
								</div>
								</div>
  
    
EOL;
		
		}
		
		public static function BanUser( $Id )
		{
			global $Database;
			
			$Database->prepareAndExecute("UPDATE tAccounts SET sUserPass = ? WHERE nEMID = ?" , array( '-' , $Id ) );
		}
		
		public static function AddDonationLog( $User , $Amount , $Type , $Comment = null )
		{	
			global $Database;
		
			switch( $Comment )
			{
				case 1:
					$Comment = 'Chargeback';
				break;
				
				case 2:
					$Comment = 'Credit Card fraud';
				break;
				
				case 3:
					$Comment = 'Order fraud';
				break;

				case 4:
					$Comment = 'Bad data entry';
				break;

				case 5:
					$Comment = 'Fake / proxy user';
				break;

				case 6:
					$Comment = 'Rejected by advertiser';
				break;

				case 7:
					$Comment = 'Duplicate conversions';
				break;

				case 8:
					$Comment = 'Goodwill credit taken back';
				break;	

				case 9:
					$Comment = 'Cancelled order';
				break;

				case 10:
					$Comment = 'Partially reversed transaction';
				break;				
			}	
			
			$Query = "INSERT INTO ItemMall.dbo.donations([type],[user],[amount],[comment],[date]) VALUES(?,?,?,?, CURRENT_TIMESTAMP);";
			
			$Params = array( $Type , $User , $Amount , $Comment );
			
			$Database->prepareAndExecute( $Query , $Params );
		}

		public static function AddDonationLogP( $User , $Amount , $Type , $Comment, $Trans )
		{	
			global $Database;
			
			$Query = "INSERT INTO ItemMall.dbo.donations([type],[user],[amount],[comment],[date],[trans_id]) VALUES(?,?,?,?, CURRENT_TIMESTAMP,?);";
			
			$Params = array( $Type , $User , $Amount , $Comment, $Trans );
			
			$Database->prepareAndExecute( $Query , $Params );
		}			
	}
	
?>