<?php
$pgMod="add_econtent";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);



if($_POST){
    print_r($_POST);
    // die(); 
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['class']) || trim($_POST['class'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select Class.");
	}
    elseif(!isset($_POST['parent_subject']) || trim($_POST['parent_subject'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select subject.");
	}

//     $checkExists = $db->getRow("select * from econtent where class='".$_POST["class"]."' and parent_subject='".$_POST["parent_subject"]."' and sub_subject='".$_POST["sub_subject"]."' and chapter='".$_POST["chapter"]."' ");

//     if(count($checkExists)>1  && $pgAct=="add"){
// 		$flgEr=TRUE;
// 		array_push($alert_err,"Assignment under this Class->Subject->Sub subject->Chapter already exists. Please use 'Add more' option to add content.");
//     }
	
	
	if($flgEr!=TRUE && $pgAct=="add")
	{
        $assignmentArray = array(
                        "class"=> $_POST["class"],
                        "subject" => $_POST["parent_subject"],
                        // "document" => $_POST["keyterms_file"],
                        // "image" => $_POST["mindmap_file"],
                        // "key_notes" => $_POST["keynotes_file"],
                        "session"    => $_SESSION["admin"]["session"],
                        "added_by"   => $_SESSION["admin"]["uid"]
                        );
 	    $flgIn = $db->insertAry("econtent", $assignmentArray);

	if(is_array($_POST["videolink"]) && count($_POST["videolink"])>0)
	{
		for($i=0; $i<count($_POST["videolink"]); $i++){
			if(trim($_POST["videolink"][$i])!=""){
			    $url_string=$_POST["videolink"][$i];
                $url= str_replace('watch?v=','embed/', $url_string);
                $videodetail=$_POST["videodetail"][$i];
				$insAry = array(
                		        "econtent_id" => $flgIn,
				                "name"=> $url,
            		            "type" => "youtube_link",
            		            "description" => $videodetail,
            		            "session" => $_SESSION["admin"]["session"]
            		            );
				$db->insertAry("econtent_files",$insAry);
			}
		}
	}

    if(isset($_FILES["key_terms"]["name"]) && !empty($_FILES["key_terms"]["name"])){
        $countfiles = count($_FILES["key_terms"]["name"]);
            $keyterms = array();
            for($i=0;$i<$countfiles;$i++){
        		$lfilename = basename($_FILES['key_terms']['name'][$i]);
        		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
        		$lnewfile=md5(microtime()).".".$lext;
        		if(move_uploaded_file($_FILES['key_terms']['tmp_name'][$i],"uploads/econtent/".$lnewfile))
            		{
            		    $keytermArray = array(
            		        "econtent_id" => $flgIn,
            		        "name" => $lnewfile,
            		        "ordering" => $_POST["keyterms_order$i"],
            		        "description" => $_POST["keyterms_description$i"],
            		        "type" => "document",
            		        "session" => $_SESSION["admin"]["session"]
            		        );
            		        $db->insertAry("econtent_files", $keytermArray);
            		  //  $keyterms []=$lnewfile;
            		}
            }
// 		$_POST["key_terms"]=json_encode($keyterms);
	}

        if ($flgIn>0) {
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
        }


	}
	
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{

        $assignmentArray = array(
            "class"=> $_POST["class"],
            "subject" => $_POST["parent_subject"],
            // "sub_subject" => $_POST["sub_subject"],
            // "chapter" => $_POST["chapter"],
        );
 	    $flgUp = $db->updateAry("econtent", $assignmentArray, "where id='".$_GET['id']."' and session='".$_SESSION["admin"]["session"]."' ");
	    
	    if(count($_POST['keytermid'])> 0){
    	   $i=0;
    	   foreach($_POST['keytermid'] as $kt){
    		    $keytermArray = array(
    		        "ordering" => $_POST["keyterms_order$i"],
    		        "description" => $_POST["keyterms_description$i"],
    		        );
 		    $flgUp = $db->updateAry("econtent_files", $keytermArray, "where id='".$_POST['keytermid'][$i]."' ");
 		    
    	       $i++;
    	   }
	    }
	    
	    if(count($_POST['videosid'])> 0){
    	   $i=0;
    	   foreach($_POST['videosid'] as $video){
    		    $videosArray = array(
    		        "ordering" => $_POST["videos_order$i"],
    		        "description" => $_POST["videos_description$i"],
    		        );
 		    $flgUp = $db->updateAry("econtent_files", $videosArray, "where id='".$_POST['videosid'][$i]."' ");
    	       $i++;
    	   }
	    }

		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
		
	}
	
	elseif($pgAct=="addmore" && isset($_GET['id']) && trim($_GET['id'])!='')
	{

	    $flgIn = $_GET['id'];

        $assignmentArray = array(
                "document" => $_POST["keyterms_file"],
                // "image" => $_POST["mindmap_file"],
                // "key_notes" => $_POST["keynotes_file"],
                // "practical_projects" => $_POST["practical_file"],
                );
 	    $flgUp = $db->updateAry("econtent", $assignmentArray, "where id='".$_GET['id']."' ");	    
 	    
 	    
    	if(is_array($_POST["videolink"]) && count($_POST["videolink"])>0)
    	{
    		for($i=0; $i<count($_POST["videolink"]); $i++){
    			if(trim($_POST["videolink"][$i])!=""){
    			    $url_string=$_POST["videolink"][$i];
                    $url= str_replace('watch?v=','embed/', $url_string);
    			    $videodetail=$_POST["videodetail"][$i];
    				$insAry = array(
                    		        "econtent_id" => $flgIn,
    				                "name"=> $url,
                		            "type" => "youtube_link",
                		            "description" => $videodetail,
                		            "session" => $_SESSION["admin"]["session"]
                		            );
    				$db->insertAry("econtent_files",$insAry);
    			}
    		}
    	}

        if(isset($_FILES["key_terms"]["name"]) && !empty($_FILES["key_terms"]["name"])){
            $countfiles = count($_FILES["key_terms"]["name"]);
                $keyterms = array();
                for($i=0;$i<$countfiles;$i++){
            		$lfilename = basename($_FILES['key_terms']['name'][$i]);
            		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
            		$lnewfile=md5(microtime()).".".$lext;
            		if(move_uploaded_file($_FILES['key_terms']['tmp_name'][$i],"uploads/keyterms/".$lnewfile))
                		{
                		    $keytermArray = array(
                		        "econtent_id" => $flgIn,
                		        "name" => $lnewfile,
                		        "ordering" => $_POST["keyterms_order$i"],
                		        "description" => $_POST["keyterms_description$i"],
                		        "type" => "document",
                		        "session" => $_SESSION["admin"]["session"]
                		        );
                		        $db->insertAry("econtent_files", $keytermArray);
                		}
                }
    	}

		redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));

	}
	
 		
}

elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
    $files = $db->getRows("select * from econtent_files where econtent_id='".$_GET['id']."' and session='".$_SESSION["admin"]["session"]."'");
    if(count($files)>0){
        foreach($files as $file){
                $img = $file['name'];
                unlink("uploads/econtent/$img");
        }
    }
 	$res=$db->delete("econtent","where session='".$_SESSION["admin"]["session"]."' and id='".$_GET['id']."' ");
 	$res=$db->delete("econtent_files","where session='".$_SESSION["admin"]["session"]."' and econtent_id='".$_GET['id']."' ");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
}


if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
?>
<div class="alert alert-success">
 <a class="close" data-dismiss="alert">×</a>
 <i class="icon icon-thumbs-up-alt"></i> <b></b><?php echo $_SESSION['msg'] ?></i>
</div>
<?php
unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
?>
<div class="alert alert-error">
 <a class="close" data-dismiss="alert">×</a>
 <i class="icon icon-warning-sign"></i> <b></b> <?php echo $alert_err[0] ?></i>
</div>
<?php

}


if(($pgAct=="edit" && $_GET["id"]!="") || ($pgAct=="addmore" && $_GET["id"]!=""))
{
	$aryForm=$db->getRow("SELECT * FROM econtent WHERE id='".$_GET['id']."'");
	
	$assignmentData=$db->getRows("SELECT * FROM econtent_files WHERE econtent_id='".$_GET['id']."' order by ordering ASC ");

}
 
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !='') || ($pgAct=="addmore" && isset($_GET['id']) && trim($_GET['id']) !=''))
{

	$aryPgAct=array("src"=>"Notice", "page_id"=>$pgMod,"action"=>"add");
    
?>

    <!--<form method="get" action="">-->
    <input type="hidden" name="src" value="Notice" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
<div class="row-fluid">
            <div class="span12">
            <div class="top-bar">
<h3><i class="icon-cog"></i>Add Econtent</h3>

</div>
<div class="well no-padding"  id="printDiv2">
<div class="controls controls-row">

<div style="padding:20px;">
  <form method="post" action="" enctype="multipart/form-data" >
    
    <div class="control-group">
    <label class="control-label">Select Class</label>
      <div  class="controls">
      <select name="class" id="class" style="width:100%;" onchange="filterSubject()">
      <option value="">--Select Class--</option>
      <?php $allClasses = $db->getRows("select * from class where school_id=1 and status=1");
            foreach($allClasses as $class){
                ?>
                <option <?php if($class["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $class['class']; ?>"><?php echo $class['class']; ?></option>
                <?php
                }
            ?>
        </select>
      </div>
      </div>
      
    <div class="control-group">
    <label class="control-label">Subject</label>
      <div  class="controls" id="parent_subject">
        <select id="mySelect1" name="parent_subject" style="width:100%;" >
            <option value="">--Select Subject--</option>
          <?php 
          if(($pgAct=="edit" && $_GET["id"]!="") || ($pgAct=="addmore" && $_GET["id"]!=""))
            {
              $allSubjects = $db->getRows("select * from subject_new where id='$aryForm[subject]' and class='$aryForm[class]' and type='subject' order by id ASC ");
                foreach($allSubjects as $sub){
                    ?>
                        <option <?php if($sub["id"]==$aryForm["subject"])echo 'selected';?> value="<?php echo $sub['id']; ?>"><?php echo $sub['subject']; ?></option>
                    <?php
                }  
            }else {
                $allSubjects = $db->getRows("select * from subject_new where class='$aryForm[class]' and session='".$_SESSION["admin"]["session"]."' and type='subject' order by id ASC ");
                foreach($allSubjects as $sub){
                ?>
                <option <?php if($sub["id"]==$aryForm["parent_subject"])echo 'selected';?> value="<?php echo $sub['id']; ?>"><?php echo $sub['subject']; ?></option>
                <?php
                }
            }
            ?>
        </select>
      </div>
    </div>
    

    
    <div class="control-group">
    <label class="control-label">Document</label>
      <div  class="controls">
          <?php if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''){
            echo "<div style='min-height:30px;' />";
            $i = 0;
              foreach($assignmentData as $kt){ 
              if($kt['type']=="document"){
                  $aryPgAct["id"]=$kt['id'];
                  ?>
                      <div style="width:250px;display:inline-block;margin:10px;border:solid 2px red;">
                          <input type="hidden" name="keytermid[]"  value="<?php echo $kt['id']; ?>">
                          <embed src="../uploads/econtent/<?php echo $kt['name']; ?>" style="width:250px;height:100px;"><br>
                          <input type="number" value="<?php echo $kt['ordering']; ?>" style="width:50px;height:40px;border:solid 2px green;" min="1" name="keyterms_order<?php echo $i; ?>" />
                          <textarea style="width:155px;border:solid 2px blue;" name="keyterms_description<?php echo $i; ?>"><?php echo $kt['description'];?></textarea>
                          <a href='../uploads/econtent/<?php echo $kt['name']; ?>' style='color:blue;' download><button type="button" class="btn" style="background:green;color:white;width:120px;">Download</button></a>
                          <a href="javascript:;" onclick="delete_file('<?php echo $kt['id']; ?>')" class="confirmation"><button type="button" class="btn btn-danger" style="width:120px;">Delete</button></a>   
                      </div>
                  <?php
                 }$i++;
              }
              echo "</div>";
          } else {
              $countkeyterms = $db->getVal("select count(id) from econtent_files where econtent_id='".$_GET['id']."' and type='key_terms' ");
              //echo $countkeyterms;
          ?>
          <div style="margin-top:20px;width:100%;">
              <input type="file" name="key_terms[]" onchange="preview_keyterms('<?php echo $countkeyterms; ?>')" id="upload_keyterms" multiple/><br>
              <input type="radio" value="image" name="keyterms_file" checked <?php if($aryForm['key_terms']=='image' && $countkeyterms>0)echo "checked=checked"; if($countkeyterms>0){echo 'onclick="javascript: return false;"'; } ?>  > Image
              <input type="radio" value="pdf" name="keyterms_file" <?php if($aryForm['key_terms']=='pdf' && $countkeyterms>0)echo "checked=checked"; if($countkeyterms>0){echo 'onclick="javascript: return false;"'; } ?>  > Pdf <br>
              <?php if($countkeyterms>0)echo "<span style='color:red;font-size:10px;'>You can't change file type.</span>" ; ?>
          </div>
            <div id="keyterms_preview" style="display:inline-block;"></div>
        <?php } ?>          
        </div>
    </div>

    <div class="control-group">
    <label class="control-label">Videos</label>
      <div  class="controls"> 
          <?php if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''){
            echo "<div style='min-height:30px;' />";
            $i = 0;
/*              foreach($assignmentData as $videos){
              if($videos['type']=="videos"){
                  ?> 
                      <div style="width:250px;display:inline-block;margin:10px;border:solid 2px red;">
                        <input type="hidden" name="videosid[]" value="<?php echo $videos['id']; ?>">
                        <video width="250" height="140" controls>
                          <source src="../uploads/videos/<?php echo $videos['name']; ?>">
                        </video>
                          <input type="number" value="<?php echo $videos['ordering']; ?>" style="width:50px;height:40px;border:solid 2px green;" min="1" name="videos_order<?php echo $i; ?>" />
                          <textarea style="width:155px;border:solid 2px blue;" name="videos_description<?php echo $i; ?>"><?php echo $videos['description'];?></textarea>
                          <a href='../uploads/videos/<?php echo $videos['name']; ?>' style='color:blue;' download><button type="button" class="btn" style="background:green;color:white;width:120px;">Download</button></a>
                            <a href="javascript:;" onclick="delete_file('<?php echo $videos['id']; ?>')" class="confirmation"><button type="button" class="btn btn-danger" style="width:120px;">Delete</button></a>
                      </div>
                  <?php $i++; 
                 }
              } */
            echo "<label>YouTube Videos</label>";
            foreach($assignmentData as $videos){
              if($videos['type']=="youtube_link"){
                  ?> 
                      <div style="width:250px;display:inline-block;margin:10px;border:solid 2px red;">
                        <input type="hidden" name="videosid[]" value="<?php echo $videos['id']; ?>">
                        <iframe width="250" height="140" src="<?php echo $videos['name']; ?>" frameborder="0" allowfullscreen>
                        </iframe>
                          <!--<a href='../uploads/videos/<?php echo $videos['name']; ?>' style='color:blue;' download><button type="button" class="btn" style="background:green;color:white;width:120px;">Download</button></a>-->
                            <textarea style="width:225px;border:solid 2px blue;" name="videos_description<?php echo $i; ?>"><?php echo $videos['description'];?></textarea>
                            <a href="javascript:;" onclick="delete_file('<?php echo $videos['id']; ?>')" class="confirmation"><button type="button" class="btn btn-danger" style="width:120px;">Delete</button></a>
                            
                      </div>
                  <?php $i++;
                 }
              }
              echo "</div>";
          } else {
              $countvideos = $db->getVal("select count(id) from econtent_files where econtent_id='".$_GET['id']."' and type='videos' ");
          ?>
          <!--<div style="margin-top:20px;width:100%;">-->
          <!--    <input type="file" name="videos[]" onchange="preview_videos('<?php echo $countvideos; ?>')" id="upload_videos" multiple/>-->
          <!--</div>-->
          <!-- <div id="videos_preview" style="display:inline-block;"></div>-->

            <table  class="table">
            <tr><td colspan="3" style="color:black;">Video Link:</td></tr>
              <tfoot>
                <tr>
                  <td colspan="2"><a id="Addlink" href="javascript:;">
                    <button type="button" class="btn btn-info">Add Other</button>
                    </a>
                  </td>
                </tr>
              </tfoot>
              <tbody id="tbodylink">
                <tr>
                  <td>(1)</td>
                  <td><input type="text" placeholder="Enter url" class="input-block-level" name="videolink[]"></td>
                  <td><textarea name="videodetail[]" placeholder="Video Description"></textarea></td>
                </tr>                  
              </tbody>
              </table>
      <?php } ?>
      </div> 
    </div>



        <div class="form-actions">
			<input type="submit" value="submit" class="btn btn-primary">
			<button type="button" class="btn"> Cancel</button>
		</div>
    </form>
    
</div>

        </div>
        <br>
</div>
<!-- / Forms: Content -->
</div>
    </div>

<?php

    
}
else {
    
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <button style="float:right; margin-top:5px" onClick="window.location='./?src=Notice&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add Econtent</button>
    </div>
    <div class="well no-padding">
  
    <div style="z-index: 99999;margin: 30px;">

        <table>
            <tr>
                <td>
                    <label class="control-label">Select Class</label>
                    <select name="class" id="class" style="width:100%;" onchange="filterSubject()">
                      <option value="">--Select Class--</option>
                      <?php $allClasses = $db->getRows("select * from class where school_id=1 and status=1");
                        foreach($allClasses as $class){
                            ?>
                            <option <?php if($class["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $class['class']; ?>"><?php echo $class['class']; ?></option>
                            <?php
                            }
                        ?>
                    </select>
                </td>
                <td>
                    <label class="control-label">Subject</label>
                                        <div  class="controls" id="parent_subject">

                    <select id="mySelect1" name="parent_subject" style="width:100%;"  onchange="changeSubject()">
                        <option value="">--Select Subject--</option>
                      <?php $allSubjects = $db->getRows("select * from subject_new where class='$aryForm[class]' and session='".$_SESSION["admin"]["session"]."' and type='subject' order by id ASC ");
                            foreach($allSubjects as $sub){
                            ?>
                            <option <?php if($sub["id"]==$aryForm["parent_subject"])echo 'selected';?> value="<?php echo $sub['id']; ?>"><?php echo $sub['subject']; ?></option>
                            <?php
                            }
                        ?>
                    </select>  
                    </div>
                </td>

                <td>
                    <label></label>
                    <button class="btn" style="margin-top:20px;font-size:15px;padding:3px 8px;" onclick="filterRecord()">Search</button>
                </td>
            </tr>
        </table>

    </div>
    
    <div id="filterdata">
            
	<?php
	$aryPgAct=array("src"=>"Notice", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM econtent  "); 
	    $i = 0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th style="width:50px;">S.No.</th>
                    <th>Class</th>
                    <th>Subject</th>
                    <!--<th>Sub-subjects</th>-->
                    <!--<th>Chapters</th>-->
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["class"]; ?></td>
                    <td><?php echo $subject = $db->getVal("select subject from subject_new where id = '".$clist["subject"]."' and type='subject' ") ?></td>
                    <!--<td><?php echo $sub_subject = $db->getVal("select subject from subject_new where id = '".$clist["sub_subject"]."' and type='sub_subject' ") ?></td>-->
                    <!--<td><?php echo $chapter = $db->getVal("select subject from subject_new where id = '".$clist["chapter"]."' and type='chapter' ") ?></td>-->
                    <td>
                    <?php $aryPgAct['action']="addmore"; ?>
                     <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-primary">Add More</button></a>
                    <?php $aryPgAct['action']="edit"; ?>
                     <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a>
                    <?php $aryPgAct['action']="delete"; ?>
                         <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                	<th></th>
                    <th colspan="3"></th>
                </tr>
            </tfoot>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div>
	
	</div></div></div>
	<?php

}
?>
    
    <!--</form>-->


<style type="text/css">
@media print {
    #myPrntbtn {
        display :  none;
    }
}

@media screen {
   #myPrntbtn1 {
        display: none;
    }
}
</style>

<script type="text/javascript">
	$(document).ready(function(e) {
		var oTable = $('.table').dataTable( {
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": true,
			"bSort": true,
			"bInfo": true,
			"bAutoWidth": false
		});
	});

	</script>
	
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>


<script>
function filterSubject(){
   	var classname = document.getElementById("class").value;
    $.post( "Notice/ajax.php", { type: "filter_subject" ,classname: classname } , function( data ) {
        document.getElementById("parent_subject").innerHTML = data;
	});
}

function changeSubject(){
    var subject = document.getElementById("mySelect1").value;
    $.post( "Notice/ajax.php", { type: "filterSubSubject" ,subject: subject } , function( data ) {
        document.getElementById("sub_subject").innerHTML = data;
	});
    
}

function filterChapter(){
    var subject = document.getElementById("subSubjectid").value;
    $.post( "Notice/ajax.php", { type: "filterChapter" ,subject: subject } , function( data ) {
        document.getElementById("chapter_div").innerHTML = data;
	});
	
}

function delete_file(id){
  var r = confirm("Are you sure want to delete?");
  if (r == true) {
    $.post( "Notice/ajax.php", { type: "delete_econtent_file" , id: id } , function( data ) {
        location.reload();
//        alert(data);
	});
  }

}
 

function preview_keyterms(n)
{
document.getElementById('keyterms_preview').innerHTML = '';
var total_file=document.getElementById("upload_keyterms").files.length;
    
var filetype = $("input:radio[name=keyterms_file]:checked").val()

var bool = true;
if(filetype=='image'){
   for(var i=0;i<total_file;i++){
       var file =document.getElementById("upload_keyterms").files[i];
        if(!file.type.match('image.*')){
            document.getElementById("upload_keyterms").value = "";
            alert('Please select Image file to upload.');
            bool= false;
            break;
        }
   }
}
else if(filetype=='pdf') {
   for(var i=0;i<total_file;i++){
       var file =document.getElementById("upload_keyterms").files[i];
        if(!file.type.match('application/pdf')){
            document.getElementById("upload_keyterms").value = "";
            alert('Please select PDF file to upload.');
            bool= false;
            break;
        }
   }
}
if(bool){
 var j =1;
 if(n>0){
     var num  = (parseInt(n)+1);
     for(var i=0;i<total_file;i++)
     {
      $('#keyterms_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><embed src='"+URL.createObjectURL(event.target.files[i])+"' style='width:250px;height:100px;'><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+num+"' name='keyterms_order"+i+"'><textarea placeholder='Description' name='keyterms_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
      num++;
     }
 }
 else {
     for(var i=0;i<total_file;i++)
     {
      $('#keyterms_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><embed src='"+URL.createObjectURL(event.target.files[i])+"' style='width:250px;height:100px;'><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+j+"' name='keyterms_order"+i+"'><textarea placeholder='Description' name='keyterms_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
      j++;
     }
 }
}
}

function preview_mindmap(n)
{
document.getElementById('mindmap_preview').innerHTML = '';
var total_file=document.getElementById("upload_mindmap").files.length;

var filetype = $("input:radio[name=mindmap_file]:checked").val()

var bool = true;
if(filetype=='image'){
   for(var i=0;i<total_file;i++){
       var file =document.getElementById("upload_mindmap").files[i];
        if(!file.type.match('image.*')){
            document.getElementById("upload_mindmap").value = "";
            alert('Please select image file to upload.');
            bool= false;
            break;
        }
   }
}
else if(filetype=='pdf') {
   for(var i=0;i<total_file;i++){
       var file =document.getElementById("upload_mindmap").files[i];
        if(!file.type.match('application/pdf')){
            document.getElementById("upload_mindmap").value = "";
            alert('Please select PDF file to upload.');
            bool= false;            
            break;
        }
   }
}
if(bool){
var j =1;
 if(n>0){
     var num  = (parseInt(n)+1);
     for(var i=0;i<total_file;i++)
     {
      $('#mindmap_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><embed src='"+URL.createObjectURL(event.target.files[i])+"' style='width:250px;height:100px;'><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+num+"' name='mindmap_order"+i+"'><textarea placeholder='Description' name='mindmap_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
      num++;         
     }
 }
 else {
     for(var i=0;i<total_file;i++)
     {
      $('#mindmap_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><embed src='"+URL.createObjectURL(event.target.files[i])+"' style='width:250px;height:100px;'><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+j+"' name='mindmap_order"+i+"'><textarea placeholder='Description' name='mindmap_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
      j++;
     }
    }
 }
}

function preview_keynotes(n) 
{
document.getElementById('keynotes_preview').innerHTML = '';    
 var total_file=document.getElementById("upload_keynotes").files.length;
 var filetype = $("input:radio[name=keynotes_file]:checked").val()

var bool = true;
if(filetype=='image'){
   for(var i=0;i<total_file;i++){
       var file =document.getElementById("upload_keynotes").files[i];
        if(!file.type.match('image.*')){
            document.getElementById("upload_keynotes").value = "";
            alert('Please select image file to upload.');
            bool= false;
            break;
        }
   }
}
else if(filetype=='pdf') {
   for(var i=0;i<total_file;i++){
       var file =document.getElementById("upload_keynotes").files[i];
        if(!file.type.match('application/pdf')){
            document.getElementById("upload_keynotes").value = "";
            alert('Please select PDF file to upload.');
            bool= false;            
            break;
        }
   }
}
if(bool){
var j =1;
 if(n>0){
     var num  = (parseInt(n)+1);
     for(var i=0;i<total_file;i++)
     {
      $('#keynotes_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><embed src='"+URL.createObjectURL(event.target.files[i])+"' style='width:250px;height:100px;'><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+num+"' name='keynotes_order"+i+"'><textarea placeholder='Description' name='keynotes_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
      num++;
     }
 }
else {
 for(var i=0;i<total_file;i++)
 {
  $('#keynotes_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><embed src='"+URL.createObjectURL(event.target.files[i])+"' style='width:250px;height:100px;'><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+j+"' name='keynotes_order"+i+"'><textarea placeholder='Description' name='keynotes_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
  j++;
  }
 }
}
}

function preview_practical(n) 
{
document.getElementById('practical_preview').innerHTML = '';
var total_file=document.getElementById("upload_practical").files.length;
var filetype = $("input:radio[name=practical_file]:checked").val()

var bool = true;
if(filetype=='image'){
   for(var i=0;i<total_file;i++){
       var file =document.getElementById("upload_practical").files[i];
        if(!file.type.match('image.*')){
            document.getElementById("upload_practical").value = "";
            alert('Please select image file to upload.');
            bool= false;
            break;
        }
   }
}
else if(filetype=='pdf') {
   for(var i=0;i<total_file;i++){
       var file =document.getElementById("upload_practical").files[i];
        if(!file.type.match('application/pdf')){
            document.getElementById("upload_practical").value = "";
            alert('Please select PDF file to upload.');
            bool= false;            
            break;
        }
   }
}
if(bool){ 
var j =1;
 if(n>0){
     var num  = (parseInt(n)+1);
     for(var i=0;i<total_file;i++)
     {
       $('#practical_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><embed src='"+URL.createObjectURL(event.target.files[i])+"' style='width:250px;height:100px;'><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+num+"' name='practical_order"+i+"'><textarea placeholder='Description' name='practical_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
      num++;
     }
 }
else {
 for(var i=0;i<total_file;i++)
 {
  $('#practical_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><embed src='"+URL.createObjectURL(event.target.files[i])+"' style='width:250px;height:100px;'><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+j+"' name='practical_order"+i+"'><textarea placeholder='Description' name='practical_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
  j++;
 }
}
}
}

function preview_videos(n) 
{
 document.getElementById('videos_preview').innerHTML = '';    
 var total_file=document.getElementById("upload_videos").files.length;
 var j =1;
  if(n>0){
     var num  = (parseInt(n)+1);
     for(var i=0;i<total_file;i++)
     {
      $('#videos_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><video style='width:250px;height:100px;'><source src='"+URL.createObjectURL(event.target.files[i])+"'></video><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+num+"' name='videos_order"+i+"'><textarea placeholder='Description' name='videos_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
      num++;   
     }
  }
  else {
 for(var i=0;i<total_file;i++)
 {
  $('#videos_preview').append("<div style='display:inline-block;width:250px;margin:10px;border:solid 2px red;'><video style='width:250px;height:100px;'><source src='"+URL.createObjectURL(event.target.files[i])+"'></video><br><input type='number' style='width:50px;height:40px;border:solid 2px green;' placeholder='No.' min='1' value='"+j+"' name='videos_order"+i+"'><textarea placeholder='Description' name='videos_description"+i+"' style='width:160px;border:solid 2px blue;'></textarea></div>");
  j++;
 }
}
}


function filterRecord(){
 
 var classname = document.getElementById("class").value;
 var subject = document.getElementById("mySelect1").value;
 var subsubject = document.getElementById("subSubjectid").value;  
 var chapter = document.getElementById("chapter").value;   

    $.post( "Notice/ajax.php", { type: "filterRecord" ,classname: classname ,subject: subject ,subsubject: subsubject , chapter: chapter} , function( data ) {
//alert(data);
  document.getElementById('filterdata').innerHTML = data;
    });
}

	//$(document).ready(function() {
    //$('#example').DataTable();
//} );


	$("#Addlink").click(function(){
		var rowCount = ($('#tbodylink tr').length)+1;
		var add_tr_skill='<tr><td>('+rowCount+')</td><td><input class="input-block-level" type="text" placeholder="Enter url" name="videolink[]"></td><td><textarea name="videodetail[]" placeholder="Video Description"></textarea></td><td><a class="delete" href="javascript:;"><button type="button" class="btn btn-info">Remove</button></a></td></tr>';
		$("#tbodylink").append($(add_tr_skill));
		$('#tbodylink td a.delete').click(function() { 
			$(this).parent().parent().remove(); 
		});	
	});


// function validateForm() {
//   var sclass = document.forms["myForm"]["class"].value;
//   var parent_subject = document.forms["myForm"]["parent_subject"].value;
//   var sub_subject = document.forms["myForm"]["sub_subject"].value;
//   var chapter = document.forms["myForm"]["chapter"].value;
  
//     alert("select * from econtent where class='".sclass."' and parent_subject='".parent_subject."' and sub_subject='".sub_subject."' and chapter='".chapter."' ");  
// //   alert(sclass);
// //   alert(parent_subject);
// //   alert(sub_subject);
// //   alert(chapter);
// //   if (x == "") {
// //     alert("Name must be filled out");
//     return false;
// //   }
// }

</script>
