<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
  <script id="MathJax-script" async
          src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
  </script>
<?php
$pgMod="omr_question";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if($_POST){

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

	if($flgEr!=TRUE && $pgAct=="add")
	{
		if($_POST['total_questions']>0){

            $exam       = $_POST['exam'];
			$class      = $_POST['class'];
			$section      = $_POST['section'];
			$subject    = $_POST['parent_subject'];

		     $paperArray = array(
                'exam'      => $exam,
			    'class'     => $class,
			    'section'   => $section,
			    'subject'	=> $subject,
 				'added_by'  => $_SESSION['admin']['uid'],
				'session'   => $_SESSION['admin']['session'],
            );

     	    $flgIn = $db->insertAry("omr_paper", $paperArray);
            $totalmarks = 0;
            for($i=1;$i<=($_POST['total_questions']);$i++){
                $totalmarks+=$_POST["mark_$i"];
                $ans = $_POST["que_$i"];
    				$aryData=array(
                        'exam'      => $exam,
    				    'class'     => $class,
    				    'subject'	=> $subject,
    				    'paper'     => $flgIn,
    				    'question'  => $i,
    				    'answer'    => "$ans",
    				    'mark'      => $_POST["mark_$i"],
         				'added_by'  => $_SESSION['admin']['uid'],
        				'session'   => $_SESSION['admin']['session'],
    				);

                    // if($_POST["que_$i"] != ''){
    					$db->insertAry("omr_question", $aryData);
                    // }

            }

             $db->updateAry("omr_paper", array("total_marks"=>$totalmarks), "where id='$flgIn' and session='".$_SESSION["admin"]["session"]."' ");

		}

        if(!is_null($flgIn))
		{
			$_SESSION['msg']='Added successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}

	}

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

// print_r($_POST);
// die();

            $exam       = $_POST['exam'];
			$class      = $_POST['class'];
			$section    = $_POST['section'];
			$subject    = $_POST['parent_subject'];

		     $paperArray = array(
                'exam'      => $exam,
			    'class'     => $class,
			    'section'   => $section,
			    'subject'	=> $subject,
 				'added_by'  => $_SESSION['admin']['uid'],
				'session'   => $_SESSION['admin']['session'],
            );

     $db->updateAry("omr_paper", $paperArray, "where id='".$_GET['id']."' and session='".$_SESSION["admin"]["session"]."' ");
        $totalmarks = 0;
 	    foreach($_POST['totalquestions'] as $qid){
            $totalmarks+=$_POST["mark_$qid"];
			$aryData=array(
				    'answer'    => $_POST["que_$qid"],
				    'mark'      => $_POST["mark_$qid"]
				);

            // $db->delete("assignment_questions", "where id='".$qid."' and paper_id='".$_GET['id']."' ");

			$qentry = $db->updateAry("omr_question", $aryData, "where id='".$qid."' and paper='".$_GET['id']."' " );

 	    }

         $db->updateAry("omr_paper", array("total_marks"=>$totalmarks), "where id='".$_GET['id']."' and session='".$_SESSION["admin"]["session"]."' ");

        if(!is_null($_GET['id']))
		{
			$_SESSION['msg']='Added successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}

	}

}


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

 	$res=$db->delete("omr_paper","where id='".$_GET['id']."' ");
 	$res=$db->delete("omr_question","where paper='".$_GET['id']."' ");

	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "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 omr_paper WHERE session='".$_SESSION["admin"]["session"]."' and id='".$_GET['id']."'");
}

// if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !='') || ($pgAct=="addmore" && isset($_GET['id']) && trim($_GET['id']) !=''))
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
        $allQuestions = $db->getRows("select * from omr_question where paper='".$aryForm[id]."' ORDER BY id ");
?>
<input type="hidden" name="src" value="ExamCBSE" />
<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 Assignment</h3>
<div>
<!--<button type="button" onclick="printDiv('printDiv2')" class="btn btn-primary " style="float:right !important; margin-top:5px ">Print</button>-->
</div>
</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" name="myForm" onsubmit="return validateForm()" >

    <div class="control-group">
        <label class="control-label">Select Exam</label>
        <div  class="controls">
            <select name="exam" style="border:solid 1px green;width:100%;">
                <option value=""> -- Select -- </option>
                <?php 
                $allexam = $db->getRows("SELECT * FROM examname order by id");
                foreach($allexam as $exam){ ?>
                    <option value="<?php echo $exam['id']; ?>" <?php if($aryForm['exam']==$exam['id'])echo 'selected'; ?> ><?php echo $exam['name']; ?></option>
                <?php } ?>
            </select>
        </div>
    </div>
      
    <div class="control-group">
        <label class="control-label" for="class">Class</label>
        <div class="controls">
            <select name="class" id="class" onchange="filterSubject()" style="border:solid 1px green;width:100%;" required>
                <option value="">--Select Class--</option>
                <?php
                    $class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."' ");
                    foreach($class as $cla){
                        ?>
                        <option <?php if($cla["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
                            <?php echo $cla["class"] ?>
                        </option>
                        <?php
                    }
                ?>
            </select>
        </div>
    </div>

    <div class="control-group">
        <label class="control-label" for="class">Section</label>
        <div class="controls">
            <select name="section" id="section" onchange="filterSubject()" style="border:solid 1px green;width:100%;" >
                <option value="">--Select Section--</option>
                <?php 
                    $class=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."' order by name ");
                    foreach($class as $cla){
                        ?>
                        <option <?php if($cla["name"]==$aryForm["section"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
                            <?php echo $cla["name"] ?>
                        </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="border:solid 1px green;width:100%;" required>
            <option value="">--Select Subject--</option>
          <?php $allSubjects = $db->getRows("select * from subject where class='$aryForm[class]' and session='".$_SESSION["admin"]["session"]."' 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
                }
            ?>
        </select>
      </div>
    </div>

    <div class="control-group">
    <label class="control-label">Total Question</label>
      <div  class="controls" id="chapter_div">
        <input class="input-block-level" type="text" id="totalque" name="total_questions" value="<?php echo count($allQuestions); ?>" onkeyup="open_question_design()" style="border:solid 1px red;" required <?php if($pgAct=="edit")echo "readonly"; ?> />
      </div>
    </div>
 

<input type="hidden" value="1" id="quemode"/>
<?php 

if((count($allQuestions)>0) && $pgAct=='edit'){
    $i = 0;
    ?>

    <table class="table" border="1">
    <thead>
        <tr>
            <th>Questions</th>
            <th>Choose Correct Answers</th>
            <th>Marks</th>
        </tr>
    </thead>
    <?php
        foreach($allQuestions as $que){
        $i++;
        $qid  = $que['id'];
    ?>
        <input type="hidden" value="<?php echo $qid; ?>" name="totalquestions[]" />
        <!--<input type="hidden" value="<?php echo $que[qtype]; ?>" name="question_type<?php echo $qid; ?>" />-->

		<!--<a href="javascript:void()" onclick="deletequestion('<?php echo $qid; ?>')" class="btn btn-primary" style="float:right;margin-top:-5px;">Delete</a>-->

        <tr>
            <td style="width:150px;"> Question <?php echo $i; ?></td>
            <td>&emsp;&emsp; A <input type="radio" value="0" name="que_<?php echo $que['id']; ?>" <?php if($que['answer']=='0')echo "checked"; ?> /> &emsp;
                             B <input type="radio" value="1" name="que_<?php echo $que['id']; ?>" <?php if($que['answer']=='1')echo "checked"; ?>/> &emsp;
                             C <input type="radio" value="2" name="que_<?php echo $que['id']; ?>" <?php if($que['answer']=='2')echo "checked"; ?>/>&emsp;
                             D <input type="radio" value="3" name="que_<?php echo $que['id']; ?>" <?php if($que['answer']=='3')echo "checked"; ?>/>
            </td>
            <td><input type="number" min="1" name="mark_<?php echo $que['id']; ?>" value="<?php echo $que['mark']; ?>" /> </td>
        </tr>

    <?php   } ?>

    </table>

    <?php   }   ?>

    <div id="questions_design">


    </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=ExamCBSE&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add Questions</button>
    </div>
    <div class="well no-padding">
        <?php /* ?>
        <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 added_by=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 class="control-label">Sub - Subject</label>
                          <div  class="controls" id="sub_subject">

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

                    <select id="chapter" name="chapter" style="width:100%">
                        <option value="">--Select Chapter--</option>
                        <?php $allChapters = $db->getRows("select * from subject_new where sub_subject='".$aryForm["sub_subject"]."' and session='".$_SESSION["admin"]["session"]."' and type='chapter'  order by id ASC ");
                        foreach($allChapters as $chapter){
                            ?>
                            <option <?php if($chapter["id"]==$aryForm["chapter"])echo 'selected'; ?> value="<?php echo $chapter['id']; ?>"><?php echo $chapter['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>
        <?php */ ?>   
    <div id="filterdata">
	<?php
	$aryPgAct=array("src"=>"ExamCBSE", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM omr_paper "); 
	    $i = 0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th style="width:50px;">S.No.</th>
                    <th>Exam</th>
                    <th>Class</th>
                    <th>Section</th>
                    <th>Subject</th>
                    <th>Total Question</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 $db->getVal("select name from examname where id='$clist[exam]' "); ?></td>
                    <td><?php echo $clist["class"]; ?></td>
                    <td><?php echo $clist["section"]; ?></td>
                    <td><?php echo $subject = $db->getVal("select subject from subject where id = '".$clist["subject"]."' ") ?></td>
                    <td><?php echo $totalquestions = $db->getVal("select count(id) from omr_question where paper = '".$clist["id"]."' and session='".$_SESSION["admin"]["session"]."' ") ?></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>
                         <a href="<?php echo URL_ROOT; ?>print/?page_id=omr_sheet&id=<?php echo $clist['id']; ?>&class=<?php echo $clist['class']; ?>&section=<?php echo $clist['section']; ?>" target="_blank" class="btn btn-info">Print</a>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </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>
	</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>
function filterSubject(){

   	var classname = document.getElementById("class").value;
   	var sec = document.getElementById("section").value;

    $.post( "ExamCBSE/ajax.php", { type: "filter_subject" ,classname: classname, section:sec } , function( data ) {
        document.getElementById("parent_subject").innerHTML = data;
	});

}

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

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


function showhideAssignment() {
  var checkBox = document.getElementById("myCheck");
  
  if (checkBox.checked == true){
    document.getElementById("text").style.display = "block";
    document.getElementById("questions_design").style.display = "block";
  } else {
    document.getElementById("text").style.display = "none";
    document.getElementById("questions_design").style.display = "none";
  }
}

function open_question_design(){
get_question_session();
    var questions = document.getElementById('totalque').value;
    var type = document.getElementById('quemode').value;

    var datas='<h4>Choose the correct answers. </h4>';
    if((questions>0) && (type==1)){
        datas += '<table class="table" border="1"><thead><tr><td>Questions</td><td>Choose Correct Answers</td><td>Marks</td></tr></thead><tbody>';
		for(i=1; i<=questions;i++){

        datas += '<tr>';
        datas += '<td style="width:150px;"> Question '+i+'</td> <td>&emsp;&emsp; A <input type="radio" value="0" name="que_'+i+'" /> &emsp; B <input type="radio" value="1" name="que_'+i+'" />&emsp; C <input type="radio" value="2" name="que_'+i+'"/>&emsp; D <input type="radio" value="3" name="que_'+i+'"/> </td> <td><input type="number" min="1" name="mark_'+i+'" value="1" /> </td> ';
        datas += '</tr>';

// 			datas +='<div class="control-group" style="border:solid 1px teal"> ';

//             datas += '<div class="panel-body"  id="questiontype'+i+'"><div class="control-group"> <label class="control-label"> Question '+i+' </label> <div class="controls"> A <input type="radio" /> </div> </div>';

//             datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> (Check correct Answers from all)';

//                 for(var j=1;j<=4;j++){
//                     datas+='<div class="input-group"><div style="display:inline-block;width:70%"><span class="input-group-addon"><input value="'+j+'" type="radio" name="a_section_'+i+'" class="ans_radio"></span> <span class="input-group-addon">'+j+'</span> </div> <div style="display:inline-block;width:29%">    </div> </div>';
//                 }

//     		 datas +='</div> </div>';

//     		 datas += '</div>  </div>';

			}
        datas += '</tbody></table>';
    }
    
    if((questions>0) && (type==2)){

		for(i=1; i<=questions;i++){

    			datas +='<div class="control-group" style="border:solid 1px teal"> <div style="background:#0052CC;color:white;padding:8px;font-size:14px;">Question '+i+' &emsp;<input type="radio" name="question_type'+i+'" value="7" onclick="change_question_type(this.value,'+i+')" style="display:none;" checked/>Subjective &emsp;&emsp;&emsp;&emsp;<input type="radio" name="quemark'+i+'" value="1" checked> 1 Mark &emsp;<input type="radio" name="quemark'+i+'" value="2"> 2 Mark &emsp;<input type="radio" name="quemark'+i+'" value="3"> 3 Mark &emsp;<input type="radio" name="quemark'+i+'" value="5"> 5 Mark &emsp;&emsp; <div style="display:inline-block;" ><select  name="quesession'+i+'" class="quesession"></select></div> </div>';

                 datas += '<div class="panel-body"  id="questiontype'+i+'"><div class="control-group"> <label class="control-label"> Question '+i+' </label> <div class="controls"> <textarea name="question'+i+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;"></textarea> <div style="width:28%;display:inline-block;float:right;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+i+')" id="upload_question'+i+'" name="upload_question'+i+'[]" multiple /> </div>  <div id="question_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+i+'" style="width:68%;border:solid 1px red;height:100px;"> </textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_solution('+i+')" id="upload_solution'+i+'" name="upload_solution'+i+'[]" multiple /> </div> <div id="solution_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '</div>';

    		 datas += '</div>  </div>';

			}

    }
    
    if((questions>0) && (type==3)){

		for(i=1; i<=questions;i++){

    			datas +='<div class="control-group" style="border:solid 1px teal"> <div style="background:#0052CC;color:white;padding:8px;font-size:14px;">Question '+i+' &emsp;<input type="radio" name="question_type'+i+'" value="8" onclick="change_question_type(this.value,'+i+')" style="display:none;" checked/>NCERT &emsp;&emsp;&emsp;&emsp;<input type="radio" name="quemark'+i+'" value="1" checked> 1 Mark &emsp;<input type="radio" name="quemark'+i+'" value="2"> 2 Mark &emsp;<input type="radio" name="quemark'+i+'" value="3"> 3 Mark &emsp;<input type="radio" name="quemark'+i+'" value="5"> 5 Mark &emsp;&emsp; <div style="display:inline-block;" > <select  name="quesession'+i+'" class="quesession"></select> </div> </div>';

                 datas += '<div class="panel-body"  id="questiontype'+i+'"><div class="control-group"> <label class="control-label"> Question '+i+' </label> <div class="controls"> <textarea name="question'+i+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;"></textarea> <div style="width:28%;display:inline-block;float:right;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+i+')" id="upload_question'+i+'" name="upload_question'+i+'[]" multiple /> </div>  <div id="question_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+i+'" style="width:68%;border:solid 1px red;height:100px;"> </textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_solution('+i+')" id="upload_solution'+i+'" name="upload_solution'+i+'[]" multiple /> </div> <div id="solution_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '</div>';

    		 datas += '</div>  </div>';

			}

    }    
    
        document.getElementById("questions_design").innerHTML = datas;
}

		function change_question_type(qtype, que){
		    
		    var datas = '';
		    
			if(qtype==1 || qtype==3){

                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls">(Check correct Answers from all) ';

                for(var j=1;j<=4;j++){
                    datas+='<div class="input-group"><div style="display:inline-block;width:70%"><span class="input-group-addon"><input value="'+j+'" type="radio" name="a_section_'+que+'" class="ans_radio"></span> <span class="input-group-addon">'+j+'</span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'_'+j+'" value="" placeholder="Enter Answer"></div> <div style="display:inline-block;width:29%"> <div id="option_preview'+que+j+'" style="display:inline-block;"></div>  <input type="file" style="display:inline-block;width:150px;" onchange="preview_option('+que+j+')" id="upload_option'+que+j+'" name="upload_option'+que+j+'[]" multiple /> </div> </div>';
                }

        		 datas +='</div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
        
        		 datas += '</div>';			    
			}

            if(qtype==2){
                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';
    
                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls">(Check correct Answers from all) ';

                for(var j=1;j<=4;j++){
                    //datas+='<div class="input-group"><span class="input-group-addon"><input value="'+j+'" type="checkbox" name="a_section_'+que+'[]" class="ans_radio" ></span> <span class="input-group-addon">'+j+'</span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'_'+j+'" value="" placeholder="Enter Answer"></div>';
                    datas+='<div class="input-group"><div style="display:inline-block;width:70%"><span class="input-group-addon"><input value="'+j+'" type="checkbox" name="a_section_'+que+'[]" class="ans_radio"></span> <span class="input-group-addon">'+j+'</span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'_'+j+'" value="" placeholder="Enter Answer"></div> <div style="display:inline-block;width:29%"> <div id="option_preview'+que+j+'" style="display:inline-block;"></div>  <input type="file" style="display:inline-block;width:150px;" onchange="preview_option('+que+j+')" id="upload_option'+que+j+'" name="upload_option'+que+j+'[]" multiple /> </div> </div>';
                }

        		 datas +='</div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
        
        		 datas += '</div>';			    
    			
            }
            
            
            if(qtype==4){
                
                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';
    
                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> ';
    

                datas+='<div class="input-group">(Check correct Answers from all)<br><span class="input-group-addon"><input value="True" type="radio" name="a_section_'+que+'" class="ans_radio" ></span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'" value="TRUE" placeholder="Enter Answer" readonly /></div>';
                datas +='<div class="input-group"><span class="input-group-addon"><input value="False" type="radio" name="a_section_'+que+'" class="ans_radio" ></span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'" value="FALSE" placeholder="Enter Answer" readonly /></div>';


        		 datas +='</div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
        
        		 datas += '</div>';			    
    			
            }
            
            
            if(qtype==5){

                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> <table class="table-condensed"><tr><td colspan="2" style="padding-left:100px;">Column A</td><td colspan="2"  style="padding-left:100px;">Column B</td><td  style="padding-left:40px;">Choose correct answer</td></tr>';

                for(var j=1;j<=4;j++){
   					datas += '<tr>';
					datas += '<td><label style="height:10px;width:10px;background:#000;border-radius:50%;"></label></td> <td><textarea name="o_section_'+que+'_'+j+'" rows="3" cols="30" class="form-control" style="border:solid 1px green;"></textarea></td>';
					datas += '<td>&emsp;&emsp;'+j+'</td><td><textarea  class="form-control" name="m_section_'+que+'_'+j+'"  rows="3" cols="30" style="display:inline-block;border:solid 1px green;"></textarea></td>';
					datas += '<td>&emsp;&emsp;&emsp;&emsp;<span style="display:inline-block;"> 1<br> <input type="radio" name="m_ans'+que+j+'" value="1" /></span> <span style="display:inline-block;">2<br> <input type="radio" name="m_ans'+que+j+'" value="2" /></span> <span style="display:inline-block;">3<br> <input type="radio" name="m_ans'+que+j+'" value="3" /></span> <span style="display:inline-block;"> 4<br> <input type="radio" name="m_ans'+que+j+'" value="4" /></span> </td>';
					datas += '</tr>';

                }

    		 datas +='</table></div> </div>';
    
    		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
    
    		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
    
    		 datas += '</div>';
			
            }
            
            
            if(qtype==6){

                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> <table class="table-condensed"><tr><td colspan="2" style="padding-left:100px;">Column A</td><td colspan="2"  style="padding-left:100px;">Column B</td><td  style="padding-left:40px;">Choose correct answer</td></tr>';

                for(var j=1;j<=4;j++){
   					datas += '<tr>';
					datas += '<td><label style="height:10px;width:10px;background:#000;border-radius:50%;"></label></td> <td><textarea name="o_section_'+que+'_'+j+'" rows="3" cols="30" class="form-control" style="border:solid 1px green;"></textarea></td>';
					datas += '<td>&emsp;&emsp;'+j+'</td><td><textarea  class="form-control" name="m_section_'+que+'_'+j+'"  rows="3" cols="30" style="display:inline-block;border:solid 1px green;"></textarea></td>';
					datas += '<td>&emsp;&emsp;&emsp;&emsp;<span style="display:inline-block;"> 1<br> <input type="checkbox" name="m_ans'+que+j+'[]" value="1" /></span> <span style="display:inline-block;">2<br> <input type="checkbox" name="m_ans'+que+j+'[]" value="2"/></span> <span style="display:inline-block;">3<br> <input type="checkbox" name="m_ans'+que+j+'[]" value="3"/></span> <span style="display:inline-block;"> 4<br> <input type="checkbox" name="m_ans'+que+j+'[]" value="4" /></span> </td>';
					datas += '</tr>';

                }

        		 datas +='</table></div> </div>';
    
        		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"></textarea> </div> </div>';
    
        		 datas += '</div>';

            }
            
            
			if(qtype==7){
                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:68%;border:solid 1px red;height:100px;"> </textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_solution('+que+')" id="upload_solution'+que+'" name="upload_solution'+que+'[]" multiple /> </div> <div id="solution_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';
        		 datas += '</div>';
			}

			if(qtype==8){
                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:68%;border:solid 1px red;height:100px;"> </textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_solution('+que+')" id="upload_solution'+que+'" name="upload_solution'+que+'[]" multiple /> </div> <div id="solution_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';
        		 datas += '</div>';
			}

 
	        document.getElementById("questiontype"+que).innerHTML = datas;
		}
		
		
function deletequestion(qid){
//    alert(qid);
  var r = confirm("Are you sure want to delete?");
  if (r == true) {
    $.post( "ExamCBSE/ajax.php", { type: "delete_question" , qid: qid } , function( data ) {
        location.reload();
	});
  }
}


function preview_question(id)
{
 var total_file=document.getElementById("upload_question"+id).files.length;
     for(var i=0;i<total_file;i++)
     {
      $('#question_preview'+id).append("<div style='display:inline-block;width:50px;height:50px;margin:1px;border:solid 2px red;'><img src='"+URL.createObjectURL(event.target.files[i])+"' style='width:50px;height:50px;'></div>");
     }
}

function preview_option(n)
{
 var total_file=document.getElementById("upload_option"+n).files.length;
     for(var i=0;i<total_file;i++)
     {
      $('#option_preview'+n).append("<div style='display:inline-block;width:40px;height:40px;margin:5px;border:solid 2px red;margin-top:-10px;'><img src='"+URL.createObjectURL(event.target.files[i])+"' style='width:40px;height:40px;'></div>");
     }
}


function preview_solution(id)
{
 var total_file=document.getElementById("upload_solution"+id).files.length;
     for(var i=0;i<total_file;i++)
     {
      $('#solution_preview'+id).append("<div style='display:inline-block;width:40px;height:40px;margin:1px;border:solid 2px red;'><img src='"+URL.createObjectURL(event.target.files[i])+"' style='width:40px;height:40px;'></div>");
     }
}

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



function filterRecord(){
 var classname = document.getElementById("class").value;
 
 var subject = document.getElementById("mySelect1").value;
  //alert(classname);

 var subsubject = document.getElementById("subSubjectid").value;  
 var chapter = document.getElementById("chapter").value;   

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



function get_question_session(){
    $.post( "ExamCBSE/ajax.php", { type: "get_question_session"}, function( data ) {
        $('.quesession').html(data);
    });
}


function validateForm() {
  var x = document.forms["myForm"]["total_questions"].value;
  var status = 0;
  for(i=1;i<=x;i++){ 
        var que = document.forms["myForm"]["question"+i+""].value;
        var qtype = document.forms["myForm"]["question_type"+i+""].value;
        if(qtype==1 || qtype==3 || qtype==4){
        var ans = document.forms["myForm"]["a_section_"+i+""].value;
            if(que !="" && ans==""){
                status = 1;
                alert('Please select an option in Question No. '+i+' ');
                break;
            }
        }
  }

  if (status == 1) {
    return false;
  }
  else return true;
}

function clearDate(){
    document.getElementById("livedate").value = "";
}

function clearTime(){
    document.getElementById("livetime").value = "";
}

</script>
