<?php
function redirect_to($location = NULL)
{
    if ($location != NULL) {
        header("Location: {$location}");
        exit;
    }
}

function confirm_query()
{

    if (!$result_set) {
        die("Database query failed " . mysql_query());
    }
}

function smsg($msg){
    return("<span class=alert bg-success alert-dismissable style='color:#0ba332'>
                        <button aria-hidden=true data-dismiss=alert class=close type=button> × </button>
                        <strong>Success!</strong> $msg 
                    </span>");
}
function emsgCnt($msg,$attempt){

return("<center><span class=alert bg-danger alert-dismissable style='color:#fe3e03'>
                        <button aria-hidden=true data-dismiss=alert class=close type=button> × </button>
                        $msg<br><strong>Remaining Attempt: $attempt </strong>
                    </span></center>");
}


function enquiryMsg(){

    return('<center><div style=" font-style:italic; font-size:20px; padding:5px; color:  #FF4500; font-weight:bolder; padding-bottom:0; padding-top:0">For any enquiry, call 09155114453 / 08033886449</div></center>');

}
function emsg($msg){


    return("<center><span class=alert bg-danger alert-dismissable style='color:#fe3e03'>
                        <button aria-hidden=true data-dismiss=alert class=close type=button> × </button>
                        <strong>Error:  </strong>$msg
                    </span></center>");


  // return('<div style="border:2px solid #DC143C; border-radius:5px; font-size:20px; padding:5px; color:  #FF4500; font-weight:bolder;">Warning: '.$msg.'</div>');
}

function resize($img, $thumb_width, $newfilename) 
{ 
  $max_width=$thumb_width;

    //Check if GD extension is loaded
    if (!extension_loaded('gd') && !extension_loaded('gd2')) 
    {
        trigger_error("GD is not loaded", E_USER_WARNING);
        return false;
    }

    //Get Image size info
    list($width_orig, $height_orig, $image_type) = getimagesize($img);
    
    switch ($image_type) 
    {
        case 1: $im = imagecreatefromgif($img); break;
        case 2: $im = imagecreatefromjpeg($img);  break;
        case 3: $im = imagecreatefrompng($img); break;
        default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
    }
    
    /*** calculate the aspect ratio ***/
    $aspect_ratio = (float) $height_orig / $width_orig;

    /*** calculate the thumbnail width based on the height ***/
    $thumb_height = round($thumb_width * $aspect_ratio);
    

    while($thumb_height>$max_width)
    {
        $thumb_width-=10;
        $thumb_height = round($thumb_width * $aspect_ratio);
    }
    
    $newImg = imagecreatetruecolor($thumb_width, $thumb_height);
    
    /* Check if this image is PNG or GIF, then set if Transparent*/  
    if(($image_type == 1) OR ($image_type==3))
    {
        imagealphablending($newImg, false);
        imagesavealpha($newImg,true);
        $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
        imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
    }
    imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);
    
    //Generate the file, and rename it to $newfilename
    switch ($image_type) 
    {
        case 1: imagegif($newImg,$newfilename); break;
        case 2: imagejpeg($newImg,$newfilename);  break;
        case 3: imagepng($newImg,$newfilename); break;
        default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
    }
 
    return $newfilename;
}

function mysql_prep($value)
{

    $magic_quotes_active = get_magic_quotes_gpc();
    $new_enough_php = function_exists("mysql_real_escape_string");
    if ($new_enough_php) {

        if ($magic_quotes_active) {
            $value = stripslashes($value);
        }
        $value = mysql_real_escape_string($value);
    } else {
        if (!$magic_quotes_active) {
            $value = addslashes($value);
        }
    }
    return $value;
}

function strip_from_zero($marked_string)
{

    $no_zero = str_replace('*0', '', $marked_string);
    $cleaned_string = str_replace('*', '', $no_zero);
    return $cleaned_string;
}


function output_message($message = "")
{
    if (!empty($message)) {
        return "<p style=\"color:purple; font-weight:bolder; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; font-size:20px\">$message</p>";
    } else {
        return "";
    }
}

function error_message($message = "")
{
    if (!empty($message)) {
        return "<span style=\"color:#F00; font-weight:bolder; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; font-size:20px; font-weight:bolder; text-align:center\">$message</span>";
    } else {
        return "";
    }
}

if(!function_exists('classAutoLoader')){
        function classAutoLoader($class_name){
            $class=strtolower($class_name);
            $classFile=$_SERVER['DOCUMENT_ROOT'].'/includes/'."{$class_name}".'.php';
            if(is_file($classFile)&&!class_exists($class)) include $classFile;
        }
    }
    spl_autoload_register('classAutoLoader');

// function __autoload($class_name)
// {
//     $class_name = strtolower($class_name);
//     $path = LIB_PATH . DS . "{$class_name}.php";
//     if (file_exists($path)) {
//         require_once($path);
//     } else {
//         die("The file {$class_name}.php could not be found");
//     }
// }

// function spl_autoload_register($class_name)
// {
//     $class_name = strtolower($class_name);
//     $path = LIB_PATH . DS . "{$class_name}.php";
//     if (file_exists($path)) {
//         require_once($path);
//     } else {
//         die("The file {$class_name}.php could not be found");
//     }
// }


function include_layout_template($template = "")
{

    include(SITE_ROOT . DS . 'includes' . DS . $template);

}


function find_lev($level=''){

    switch ($level) {
    case 'ND I':
        $table_name='ndi_tbl';
        break;
        case 'ND II':
            $table_name='ndii_tbl';
            break;
        case 'HND I':
        $table_name='hndi_tbl';
            break;
    default:
        $table_name='hndii_tbl';
            break;
       
}
 return $table_name;
}
function find_sem($session='', $semester=''){
switch ($session.$semester) {
    case '2018/2019First':
        $sem_id=1;
        break;

    case '2018/2019Second':
        $sem_id=2;
        break;

    case '2019/2020First':
        $sem_id=3;
        break;
    default:
        $sem_id=2;
        break;
}
return $sem_id;

}


function find_prev_lev($level=''){

    switch ($level) {
    case 'ND II':
        $table_name='ndi_tbl';
        break;
        case 'HND II':
            $table_name='hndi_tbl';
            break;
        
    default:
        $table_name='hndi_tbl';
            break;
       
}
 return $table_name;
}
function find_prev_sem($semester=''){
switch ($semester) {
    case 'First':
        $sem_id=1;
        break;
    
    default:
        $sem_id=2;
        break;
}
return $sem_id;

}


function find_prev_ses($session=''){

    return (date("Y")-2).'/'.(date("Y")-1);
}

function cgpa_lookup ($level='', $semester=''){
    $cgpa_fields='';
    $var_switch = $level.$semester;
    switch ($var_switch) {
        case 'ND I'.'First':
            $cgpa_fields='tnu_1,'.'tcp_1,'.'gp_1';
            break;
         case 'ND I'.'Second':
            $cgpa_fields='tnu_2,'.'tcp_2,'.'gp_2';
            break;
        case 'ND II'.'First':
        $cgpa_fields='tnu_3,'.'tcp_3,'.'gp_3';
            break;
         case 'ND II'.'Second':
            $cgpa_fields='tnu_4,'.'tcp_4,'.'gp_4';
            break;

             case 'HND I'.'First':
            $cgpa_fields='tnu_1,'.'tcp_1,'.'gp_1';
            break;
         case 'HND I'.'Second':
            $cgpa_fields='tnu_2,'.'tcp_2,'.'gp_2';
            break;
        case 'HND II'.'First':
        $cgpa_fields='tnu_3,'.'tcp_3,'.'gp_3';
            break;
         case 'HND II'.'Second':
            $cgpa_fields='tnu_4,'.'tcp_4,'.'gp_4';
            break;

        default:
            # code...
            break;
    }
    return $cgpa_fields;
}



function cgpa_lookup_previous ($level='', $semester=''){
    $cgpa_fields='';
    $var_switch = $level.$semester;
    switch ($var_switch) {
        case 'ND I'.'First':
            $cgpa_fields='';
            break;
         case 'ND I'.'Second':
             $cgpa_fields='tnu_1,'.'tcp_1,'.'gp_1';
            //$cgpa_fields='tnu_2,'.'tcp_2,'.'gp_2';
            break;
        case 'ND II'.'First':
         $cgpa_fields='tnu_2,'.'tcp_2,'.'gp_2';
       // $cgpa_fields='tnu_3,'.'tcp_3,'.'gp_3';
            break;
         case 'ND II'.'Second':
             $cgpa_fields='tnu_3,'.'tcp_3,'.'gp_3';
            //$cgpa_fields='tnu_4,'.'tcp_4,'.'gp_4';
            break;


            case 'HND I'.'First':
            $cgpa_fields='';
            break;
         case 'HND I'.'Second':
             $cgpa_fields='tnu_1,'.'tcp_1,'.'gp_1';
            //$cgpa_fields='tnu_2,'.'tcp_2,'.'gp_2';
            break;
        case 'HND II'.'First':
         $cgpa_fields='tnu_2,'.'tcp_2,'.'gp_2';
       // $cgpa_fields='tnu_3,'.'tcp_3,'.'gp_3';
            break;
         case 'HND II'.'Second':
             $cgpa_fields='tnu_3,'.'tcp_3,'.'gp_3';
            //$cgpa_fields='tnu_4,'.'tcp_4,'.'gp_4';
            break;

        default:

         $cgpa_fields='tnu_4,'.'tcp_4,'.'gp_4';
            # code...
            break;
    }
    return $cgpa_fields;
}


function cummulative_result ($level='', $semester=''){
    $cum_fields='';
    $var_switch = $level.$semester;
    switch ($var_switch) {
        case 'ND I'.'First':
               $cum_fields='tnu_1,'.'tcp_1,'.'gp_1';
            break;
         case 'ND I'.'Second':
         $cum_fields='tnu_1,'.'tnu_2,'.'tcp_1,'.'tcp_2,'.'cgpa_1';
            // $cum_fields='tnu_1,'.'tcp_1,'.'gp_1,'.'tnu_2,'.'tcp_2,'.'gp_2,'.'cgpa_1';
            //$cgpa_fields='tnu_2,'.'tcp_2,'.'gp_2';
            break;
        case 'ND II'.'First':
         $cum_fields='tnu_1,'.'tnu_2,'.'tcp_1,'.'tcp_2,'.'cgpa_1';
       // $cgpa_fields='tnu_3,'.'tcp_3,'.'gp_3';
            break;
         case 'ND II'.'Second':
             $cum_fields='tnu_3,'.'tnu_4,'.'tcp_3,'.'tcp_4,'.'cgpa_2';
            //$cgpa_fields='tnu_4,'.'tcp_4,'.'gp_4';
            break;

            case 'HND I'.'First':
               $cum_fields='tnu_1,'.'tcp_1,'.'gp_1';
            break;
         case 'HND I'.'Second':
             $cum_fields='tnu_1,'.'tnu_2,'.'tcp_1,'.'tcp_2,'.'cgpa_1';
            //$cgpa_fields='tnu_2,'.'tcp_2,'.'gp_2';
            break;
        case 'HND II'.'First':
         $cum_fields='tnu_1,'.'tnu_2,'.'tcp_1,'.'tcp_2,'.'cgpa_1';
       // $cgpa_fields='tnu_3,'.'tcp_3,'.'gp_3';
            break;
         case 'HND II'.'Second':
             $cum_fields='tnu_3,'.'tnu_4,'.'tcp_3,'.'tcp_4,'.'cgpa_2';
            //$cgpa_fields='tnu_4,'.'tcp_4,'.'gp_4';
            break;

        default:

         $cum_fields='tnu_4,'.'tcp_4,'.'gp_4';
            # code...
            break;
    }
    return $cum_fields;
}

function sem_short($sem){
    $sem_code='';
    switch ($sem) {
        case 'First':
            $sem_code='A';
            break;
        case 'Second':
        $sem_code='B';
        break;
        default:
            # code...
            break;
    }
    return $sem_code;
}



?>