<?
	$res=mysql_query("select * from invoices where id='".$_SESSION['invoice']."'")or die(mysql_error());
		$info=mysql_fetch_array($res);
		
			$_GET['id']=$_SESSION['invoice'];
			include "../inc/invoice_save.php";
					
		if(isset($_POST['submit'])){

			$link="http://".$_SERVER['HTTP_HOST'].str_replace('mobile/new_invoice.php','',$_SERVER['SCRIPT_NAME'])."inc/invoicehtml.php?id=".encrypt($_SESSION['invoice']."-".md5($_SESSION['invoice']));
			$_POST['mesajul']=str_replace("[LINK]","<a href='".$link."'>".$link."</a>",$_POST['message']);
			
			if($_POST['email']==''){
					$error="<div class='message error'>
				<i class='icon-ok'></i>
				<p>Please insert a valid email address!</p>
			</div>";
			}else{
				$subject=$_POST['subject'];
				$message=$_POST['mesajul'];
				
				$invoiceloc=str_replace(array('inc/','mobile/'),'',dirname(__FILE__))."/invoices/".$_SESSION['invoice'].".pdf";
 				if(mailer($_POST['email'],$subject,$message,$invoiceloc)){
					$error="<div class='message success'>
				<i class='icon-ok'></i>
				<p>Email Has been sent!</p>
			</div>";
				}else{
					$error="<div class='message error'>
				<i class='icon-ok'></i>
				<p>Email could not be sent!</p>
			</div>";
				} 			
			}
		}
		
?>
		<div class='inset'>        
			<div class='message success'>
				<i class='icon-ok'></i>
				<p>The invoice has been created!</p>
			</div>
			<?=$error?>
			<ul data-role="listview" data-inset="false" data-icon="false" data-divider-theme="b">
	            <li >
					<a href="../inc/invoicepdf.php?id=<?=encrypt($_SESSION['invoice']."-".md5($_SESSION['invoice']))?>" data-ajax="false"><i class='icon-file-text'></i> Download PDF</a>
	            </li>
	            <li >
					<a href="../inc/invoicehtml.php?id=<?=encrypt($_SESSION['invoice']."-".md5($_SESSION['invoice']))?>" target='_blank' data-ajax="false"><i class='icon-file-text'></i> View HTML</a>
	            </li>	         		
	        </ul>
		<div data-role="collapsible" data-content-theme="b">
            <h4>Send Email</h4>
			<div class='inset'>
		<form action='new_invoice.php?step=3' method='post' data-ajax="false">
                <ul data-role="listview">				
	            <li data-role="list-divider">New client</li>				
					<li data-role="fieldcontain">
                        <label for="name2b">Subject:</label>
                        <input type="text" name="subject" id="subject" value="Hello <?=$info['client_name']?> we have issued you an invoice!" data-clear-btn="true" placeholder="">
                    </li>					
					<li data-role="fieldcontain">
                        <label for="name2b">Client email:</label>
                        <input type="text" name="email" id="client_email" value="<?=$info['client_email']?>" data-clear-btn="true" placeholder="">
                    </li>										
					<li data-role="fieldcontain">
                        <label for="name2b">Message:</label>
                         <textarea cols="40" rows="15" name="message" id="client_address" placeholder="" style='height:300px;'>Dear <?=$info['client_name']?>,
We have attached an invoice in the amount of <?=$info['total']?><?=$info['currency']?>.
			
You may pay, view and print the invoice online by visiting the following link: 
[LINK]		
	
Best Regards,
<?=$cfg['name']?></textarea>
                    </li>
		
	                    <li>
                        <fieldset class="ui-grid-a">
                                <div class="ui-block-b"><button type="submit" name='submit'>Send</button></div>
                        </fieldset>
                    </li>				
			</ul>
			</form>
			</div>
        </div>
		</div>	
