<?php
class functions
{
    function check_email($email)
    {
        if (ereg("^.+@.+\..+$", $email))
            return true;
    } 
    function returnErrors($errors)
    {
        $x .= '<ul id="returned_errors">';
        foreach($errors as $error)
        {
          $x .= '<li>'.$error.'</li>';
        }
        $x .= '</ul>';
        return $x;
    }
    function check_email_availability($email)
    {
    	global $db,$conn;		
    	$result = $db->query("SELECT * FROM user WHERE email='".$email."' ");
		if ($db->num_rows($result) > 0){
			return false;
		}else{
			return true;
		}		             
    } 
	function checkBox2int($var){
		if($var=='on'){
			return 1;
		}else{
			return 0;
		};
	}
	function pageIsSelected($curPage) {
		 global $page;
	   if ($curPage == $page->topCategory ) {
        return 'id="active"';
      }
    }
        function validateRegistrant2($edit=false)
    {
    }    
function HTMLemail($autobody,$signature_block="") {
global $config;
$prefix = $config->frontEndURL;
if(isset($signature_block) && $signature_block !=""){
	$signature_image = jackycrm::$signatureBlock[$signature_block].".jpg";
	$img = "<img src='".$config->frontEndURL."/admin/images/".$signature_image."' width='164px' style='float:left;margin:10px'>";
	$name_of_sig = ucfirst(jackycrm::$signatureBlock[$signature_block]);
	$designation = "Office Manager";
}else{
	//$signature_image = "cate_signature.jpg";
	//$img = "<img src='".$config->frontEndURL."/admin/images/".$signature_image."' style='width:164;float:left;margin:10px'>";
	//$name_of_sig = "Kate Callow";
	//$designation = "National Account Manager";
}
$htmlemail = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>'.$config->siteName.' Email</title>
<style>
body {
	background:#C8C8C8;
}

table, td {
	background: #FFFFFF;
}
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="border:0px solid #999;">
  <tr>
    <td align="center"><img src="'.$config->frontEndURL.'/admin/images/logo.jpg" style="width:200px;float:right;margin:10px"></td>
  </tr>
  <tr>
    <td style="padding: 15px 10px 15px 10px;font-family: Arial;font-size:13px;"><p>'.nl2br($autobody).'</p></td>
  </tr>
  <!--<tr>
    <td align="left">
		'.$img.'
		<div style="clear:both;"></div>
		<div style="margin:0 10px 0 10px;"><b>'.$name_of_sig.'</b><br>
		<b>'.$designation.'</b></div>
		<img src="'.$config->frontEndURL.'/admin/images/logo.png" style="width:164;float:left;margin:10px">
		<div style="clear:both;"></div>
		<div style="margin:0 10px 0 10px;font-family: arial,sans-serif;"><p>Unit 1, The Mill House,<br>The Stables Business Park,<br>Rooksbridge,<br>Somerset,<br>BS26 2TT<br><br>Office:   01934 750203<br>Fax: 01934 310573</p></div>
	</td>
  </tr>-->
  <tr>
    <td style="padding: 15px 30px 15px 30px;font-family: calibri;font-size:11px;color:blue;font-style:italic;"><p>This email may contain confidential information.  If you are not the intended recipient (or have received this email in error) please notify the sender immediately and destroy this email.  Any authorised copying, disclosure or distribution of the material in this email is strictly forbidden</p></td>
  </tr>
  <tr>
     <td style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:9px;color:#666666;text-align:center;padding:5px;height:58px;">
		<b>&copy;'.date("Y").' '.$config->siteName.' . All rights reserved. - '.date("Y").' </b><br />
	</td>
  </tr>
</table>
</body>
</html>';
return $htmlemail;
}

function HTMLemailWithoutSignatureBlock($autobody,$admin_id = "") {
global $config;
if(isset($admin_id) && $admin_id !=""){
	$consultant_name = utility::getTableFieldById("admin","name","id",$admin_id);
	$designation = utility::getTableFieldById("admin","designation","id",$admin_id);
	$phone = utility::getTableFieldById("admin","phone","id",$admin_id);
	$phone1 = utility::getTableFieldById("admin","phone1","id",$admin_id);
	$email_admin = utility::getTableFieldById("admin","email_admin","id",$admin_id);
}
$htmlemail = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>'.$config->siteName.' Email</title>
<style>
body {
	background:#C8C8C8;
}

table, td {
	background: #FFFFFF;
}
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="border:0px solid #999;">
  <tr>
    <td align="center"><img src="cid:logoimg" style="width:200px;float:right;margin:10px"></td>
  </tr>
  <tr>
    <td style="padding: 15px 10px 15px 10px;font-family: Arial;font-size:13px;"><p>'.nl2br($autobody).'</p></td>
  </tr>';
  if(isset($admin_id) && $admin_id !=""){
  $htmlemail .= '<tr>
    <td align="left">
		<div style="clear:both;"></div>
		<div style="margin:0 10px 0 10px;font-family: arial,sans-serif;">
			<p>Yours sincerely<br><img src="cid:signatureimg" style="width:100px;float:left;"></p>
		</div>
		<div style="clear:both;"></div>
		<div style="margin:0 10px 0 10px;font-family: arial,sans-serif;">
			<p>'.$consultant_name.'<br>'.$designation.'<br>'.$phone.'<br>'.$phone1.'<br>'.$email_admin.'</p>
		</div>
	</td>
  </tr>';
  }else{
	$htmlemail .= '<div style="margin:0 10px 0 10px;font-family: arial,sans-serif;"><p>'.nl2br($config->companyAddress).'<br><br>Phone: '.$config->companyPhone.'</p></div>';
  }
  $htmlemail .= '<tr>
    <td style="padding: 15px 30px 15px 30px;font-family: calibri;font-size:11px;color:blue;font-style:italic;"><p>This email may contain confidential information.  If you are not the intended recipient (or have received this email in error) please notify the sender immediately and destroy this email.  Any authorised copying, disclosure or distribution of the material in this email is strictly forbidden</p></td>
  </tr>
  <tr>
     <td style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:9px;color:#666666;text-align:center;padding:5px;height:58px;">
		<b>&copy;'.date("Y").' '.$config->siteName.' . All rights reserved. - '.date("Y").' </b><br />
	</td>
  </tr>
</table>
</body>
</html>';
return $htmlemail;
}
function cleanurl($url){
$url=trim($url);
$str= str_replace('/','-',strtolower(str_replace(' ','-',$url)));
$str=str_replace('&','',$str);
$str=str_replace('--','-',$str);
return $str;
}  

function setSessionShippingPostcode() {
	global $db,$conn;
	$result = $db->query("SELECT * from shipping_postcode");
	$shipping_postcode = "";
		if ($db->num_rows($result) > 0){
			while($row = $db->next_record($result)){
				$shipping_postcode = $shipping_postcode.",".$row['shipping_postcode'];
			}
		}
	return substr($shipping_postcode,1);
}
  

function base64Encode($plain) {
  $output = "";
  $output = base64_encode($plain);
  return $output;
}
function base64Decode($scrambled) {
  $output = "";
  $scrambled = str_replace(" ","+",$scrambled);
  $output = base64_decode($scrambled);
  return $output;
}
function simpleXor($InString, $Key) {
  $KeyList = array();
  $output = "";
  for($i = 0; $i < strlen($Key); $i++){
    $KeyList[$i] = ord(substr($Key, $i, 1));
  }
  for($i = 0; $i < strlen($InString); $i++) {
    $output.= chr(ord(substr($InString, $i, 1)) ^ ($KeyList[$i % strlen($Key)]));
  }
  return $output;
}
function getToken($thisString) {
  $Tokens = array(
    "Status",
    "StatusDetail",
    "VendorTxCode",
    "VPSTxId",
    "TxAuthNo",
    "Amount",
    "AVSCV2", 
    "AddressResult", 
    "PostCodeResult", 
    "CV2Result", 
    "GiftAid", 
    "3DSecureStatus", 
    "CAVV" );
  $output = array();
  $resultArray = array();
  for ($i = count($Tokens)-1; $i >= 0 ; $i--){
    $start = strpos($thisString, $Tokens[$i]);
    if ($start !== false){
      $resultArray[$i]->start = $start;
      $resultArray[$i]->token = $Tokens[$i];
    }
  }
  sort($resultArray);
  for ($i = 0; $i<count($resultArray); $i++){
    $valueStart = $resultArray[$i]->start + strlen($resultArray[$i]->token) + 1;
    if ($i==(count($resultArray)-1)) {
      $output[$resultArray[$i]->token] = substr($thisString, $valueStart);
    } else {
      $valueLength = $resultArray[$i+1]->start - $resultArray[$i]->start - strlen($resultArray[$i]->token) - 2;
	  $output[$resultArray[$i]->token] = substr($thisString, $valueStart, $valueLength);
    }      
  }
  return $output;
}
function randomise() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
}

}
