<?php
function priceRoundUp($value)
{
    global $priceRoundUp;
    
    if($priceRoundUp)
    {
        return ceil($value);
    }
    else
    {
        return $value;
    }
}
function formatDate($date)
{
    global $DateFormatUS;
    
    if($DateFormatUS)
    {
        return date("m/d/Y", strtotime($date));
    }
    else
    {
        return date("d/m/Y", strtotime($date));
    }
}
function displayCurrency($str)
{
	$string = htmlentities($str);
    if(empty($string))
	{
		$string = $str;
	}
	return $string;
}

function sqlDate($dateStr)
{
	$dateStr = explode('/', $dateStr);
	$dateStr = @$dateStr[2] . '-' . @$dateStr[1] . '-' . @$dateStr[0];
	return $dateStr;
}

function stripRequestCharacters($string)
{
    $stripCharacters = str_replace("'","''", $string);
    $stripCharacters = str_replace("<","", $stripCharacters);
    $stripCharacters = str_replace(">","", $stripCharacters);
    $stripCharacters = str_replace("&","", $stripCharacters);
    return $stripCharacters;
}

function generateReference($websiteId, $prefix)
{
    if(!empty($prefix))
    {
        $reference = $prefix . '-' . $websiteId . '-';
    }
    else
    {
        $reference = $websiteId . '-';
	}
    for ($digit = 0; $digit < 3; $digit++)
	{
		$r = rand(0,1);
		$c = ($r==0)? rand(65, 90) : rand(97, 122);
		$reference .= strtoupper(chr($c));
	} 
	$reference .= date("ymdHis");
    return $reference;
}

function sendMail($sender, $recipient, $cc, $bcc, $subject, $text, $html, $file1, $file2, $fileName1, $fileName2)
{
    $mail = new PHPMailer;

    //$mail->SMTPDebug = 3;                               // Enable verbose debug output

    //$mail->isSMTP();                                      // Set mailer to use SMTP
    //$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    //$mail->SMTPAuth = true;                               // Enable SMTP authentication
    //$mail->Username = 'user@example.com';                 // SMTP username
    //$mail->Password = 'secret';                           // SMTP password
    //$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    //$mail->Port = 587;                                    // TCP port to connect to

    $mail->From = $sender;
    $mail->FromName = $sender;
    //$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress($recipient);               // Name is optional
    $mail->addReplyTo($sender, $sender);
    if(!empty($cc))
    {
        $mail->addCC($cc);
    }
    if(!empty($bcc))
    {
        $mail->addBCC($bcc);
    }
    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    if(!empty($file1)) { $mail->AddStringAttachment($file1, $fileName1); }
    if(!empty($file2)) { $mail->AddStringAttachment($file2, $fileName2); }
    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = $subject;
    $mail->Body = $html;
    $mail->AltBody = $text;

    if(!$mail->send())
    {
        return 0; //$mail->ErrorInfo;
    }
    else
    {
        return 1;
    }
}

function encodeSql($string)
{
	$stripCharacters = str_replace("'", "\'", $string);
	$stripCharacters = str_replace("\x00", "", $stripCharacters);
	$stripCharacters = str_replace("\n", "\\n", $stripCharacters);
	$stripCharacters = str_replace("\r", "\\r", $stripCharacters);
	$stripCharacters = str_replace('"', '\"', $stripCharacters);
	$stripCharacters = str_replace("\x1a", "", $stripCharacters);
	return $stripCharacters;
}
    
function genRandomString() 
{
	$length = 10;
	$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
	$string = '';    

	for ($p = 0; $p < $length; $p++) {
		$string .= $characters[mt_rand(0, strlen($characters)-1)];
	}
	return $string;
}

function selectTable($table, $name, $value, $blank)
{
	global $mysqli;

	print '<select name="' . $name . '" id="' . $name . '">'. PHP_EOL;
	print '<option value="">' . $blank . '</option>'. PHP_EOL;
	if ($result = $mysqli->query("SELECT Id, Name FROM " . $table . " ORDER BY Name"))
	{
		//print $result->num_rows;
		while ($row = $result->fetch_object())
		{
			print '<option value="' . $row->Id . '"';
			if ($value == $row->Id)
			{
				print ' selected="selected"';
			}
			print '>' . $row->Name . '</option>'. PHP_EOL;
		}
	}
	else
	{
		print $mysqli->error;
		exit;
	}
	print '</select>'. PHP_EOL;
	$result->close();
}

function selectYesNo( $name, $value )
{
	print '<select name="' . $name . '" id="' . $name . '" style="width: 100px;">'. PHP_EOL;
	print '<option value="0"';
	if ($value == 0)
	{
		print ' selected="selected"';
	}
	print '>No</option>' . PHP_EOL;
	print '<option value="1"';
	if ($value == 1)
	{
		print ' selected="selected"';
	}
	print '>Yes</option>'. PHP_EOL;
	print '</select>'. PHP_EOL;
}

$dayName = array(
    0 => 'Sunday',
    1 => 'Monday',
    2 => 'Tuesday',
    3 => 'Wednesday',
    4 => 'Thursday',
    5 => 'Friday',
    6 => 'Saturday'
);

function selectWeekday( $name, $value )
{
	print '<select name="' . $name . '" id="' . $name . '" style="width: 100px;">'. PHP_EOL;
	print '<option value="1"';
	if ($value == 1)
	{
		print ' selected="selected"';
	}
	print '>Monday</option>'. PHP_EOL;
	print '<option value="2"';
	if ($value == 2)
	{
		print ' selected="selected"';
	}
	print '>Tuesday</option>'. PHP_EOL;
	print '<option value="3"';
	if ($value == 3)
	{
		print ' selected="selected"';
	}
	print '>Wednesday</option>'. PHP_EOL;
	print '<option value="4"';
	if ($value == 4)
	{
		print ' selected="selected"';
	}
	print '>Thursday</option>'. PHP_EOL;
	print '<option value="5"';
	if ($value == 5)
	{
		print ' selected="selected"';
	}
	print '>Friday</option>'. PHP_EOL;
	print '<option value="6"';
	if ($value == 6)
	{
		print ' selected="selected"';
	}
	print '>Saturday</option>'. PHP_EOL;
	print '<option value="0"';
	if ($value == 0)
	{
		print ' selected="selected"';
	}
	print '>Sunday</option>' . PHP_EOL;
	print '</select>'. PHP_EOL;
}

function displayYesNo($value)
{
	if ($value == 1)
	{
		return 'Yes';
	}
	else
	{
		return 'No';
	}
}

function post_captcha($user_response) {
	$fields_string = '';
	$fields = array(
		'secret' => '6LfkYioUAAAAADK9Sa2upNV8V3WFzcVRKjQabetc',
		'response' => $user_response
	);
	foreach($fields as $key=>$value)
	$fields_string .= $key . '=' . $value . '&';
	$fields_string = rtrim($fields_string, '&');

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
	curl_setopt($ch, CURLOPT_POST, count($fields));
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

	$result = curl_exec($ch);
	curl_close($ch);

	return json_decode($result, true);
}
?>