<?php 


/**
 * Function for breaking text 
 * @param unknown $text
 * @param number $length
 */

ini_set('memory_limit','384M');

function break_text($text, $length=200){
	//return '';
	if(strlen($text)<$length+10) return $text;//don't cut if too short
	$text = strip_tags($text);
	$break_pos = strpos($text, ' ', $length);//find next space after desired length
	$visible = substr($text, 0, $break_pos);
	return balanceTags($visible);
}

function _getMondayOfWeek($date)
{
	$date =  strtotime($date);
	if (date('w', $date) == 1)
		return $date ;
	else if ( date('w', $date) == 0 )
		return strtotime(
            'next monday',
             $date );
	else 
		return strtotime(
				'last monday',
				$date);
}

function _shorten_string($string, $wordsreturned)
{
    $array = explode(" ", $string);
    if (count($array)<=$wordsreturned)
    {
        $retval = $string;
    }
    else
    {
        array_splice($array, $wordsreturned);
        $retval = implode(" ", $array)." ...";
    }
    return $retval;
}

function _timeSince($original) {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'min'),
        array(1 , 'sec'),
    );

    $original = strtotime($original);
    $today = time(); /* Current unix time  */
    //$since = $original - 3;
    $since = $today - $original;
    // echo "<br>:".$since;
    //if (floor($since/3600/24) > 365){
    if ($since < 0){
        return date('d M Y', $original);
    }


    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {

        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];

        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";

    if ($i + 1 < $j) {
        // now getting the second item
        $seconds2 = $chunks[$i + 1][0];
        $name2 = $chunks[$i + 1][1];

        // add second item if its greater than 0
        if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
            $print .= ($count2 == 1) ? ', 1 '.$name2 : " $count2 {$name2}s";
        }
    }
    return $print;
}

function _getWeekOfMonth($date)
{
    //Get the first day of the month.
    $firstOfMonth = strtotime(date("Y-m-01", $date));
    //Apply above formula.
    return intval(date("W", $date)) - intval(date("W", $firstOfMonth)) + 1;
}

function _encrypt_decrypt($action, $string) {
	$output = false;
	
	$key = 'S4K3T3D9sktd!!Raipur';
	
	// initialization vector 
	$iv = md5(md5($key));
	
	if( $action == 'encrypt' ) {
		$output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv);
		$output = base64_encode($output);
	}
	else if( $action == 'decrypt' ){
		$output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, $iv);
		$output = rtrim($output, "");
	}
	return $output;
}

$GLOBALS['currDate'] = date("Y-m-d");
$GLOBALS['currDateTime'] = date("Y-m-d H:i:s") ;


$GLOBALS['STtax'] = 14;
$GLOBALS['SBCesstax'] = 0.5;
$GLOBALS['currDateTime'] = date('Y-m-d H:i:s');
$GLOBALS['currDate']= date('Y-m-d');
$GLOBALS['currTime']= date('H:i:s');