<?php  
    $start=$end="";
    if(isset($_POST['update']))
    {
        $start = mysql_real_escape_string($_POST['start']);
        $end = mysql_real_escape_string($_POST['end']);
        $id = mysql_real_escape_string($_GET['id']);
        
        $startDate = date_format(date_create($start),"Y-m-d");
        $endDate = date_format(date_create($end),"Y-m-d");
        $currentDate = date("Y-m-d");
        
        if(!$startDate){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Enter Start date</span>
                </div>';            
        }else
        /*if($startDate <= $currentDate){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Start Date must be greator than today&CloseCurlyQuote;s date </span>
                </div>';
        }else*/
        if(!$endDate){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Enter End date</span>
                </div>';
        }else
        if($endDate <= $startDate){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">End date must be <b>greator</b> than Start date!</span>
                </div>';
        }else
        {
            
            if(!$select = mysql_query("Select start,end FROM semisterperiod WHERE date(end) >= '$startDate' AND periodID != '$id' "))
            {
                echo'<div class="alert alert-danger absolue center text-center" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                        <span class="text-danger">'.mysql_error().'</span>
                    </div>';
            }else{
                $count = mysql_num_rows($select);
                if($count > 0){
                    echo'<div class="alert alert-danger absolue center text-center" role="alert">
                           <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                               <span aria-hidden="true">×</span>
                           </button>
                           <span class="text-danger">This period collides with another existing semester period</span>
                       </div>';   
                }else
                {
                    if(!$insert = mysql_query("UPDATE semisterperiod SET start='$startDate',end='$endDate',updated_by='$stafNo' WHERE periodID='$id' "))
                    {
                        echo'<div class="alert alert-danger absolue center text-center" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <span class="text-danger">'.mysql_error().'</span>
                            </div>';
                    }else
                    {
                        $start=$end="";
                        $_SESSION['success']="true";
                        header('Location:period.php');
                        echo "<script type='text/javascript'> document.location = 'period.php'; </script>";
                        exit();
                    }
                }
            }
        }
    }  
?>