<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');
/*include('../includes/header_in.php');*/
/*include('../includes/topmenuin.php');*/
header("Cache-Control: no cache");
session_cache_limiter("private_no_expire");
$companyID = $_SESSION['id'];
//include('1-Quality-Procedures1DB.php');
?>

<html><head><meta charset="utf-8"><title>pmAnywhere - Scope Module</title>
   <link href="03-00Scope-Styles1.css" rel="stylesheet" type="text/css" />  
    <!--<link href="pma3buttonstyles.css" rel="stylesheet" type="text/css" />-->
    <script src="jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="ckeditor/ckeditor.js"></script>
    <!--<link href="widgEditor/css/widgEditor.css" rel="stylesheet" type="text/css" />-->
    <!--<script src="widgEditor/scripts/widgEditor.js"></script>-->

</head>

<!-- Hope this text editor works well --->
<!--<style type="text/css" media="all">
	@import "css/info.css";
	@import "css/main.css";
	@import "css/widgEditor.css";
</style>-->
<script>
    function sendforinitiate(bid)
    {
        var c = confirm("Are you sure you want to send this document for review? It will show in the list of Initiated Documents");
        if (c)
        {
            window.location.href = "03-02Scope-01ClientBrief01DB.php?send=" + bid;
        }
        else
        {
            return false;
        }
    }
</script>
<?php $BID = isset($_GET['BID']) ? $_GET['BID'] : 0; ?>
<?php
$cid = $_SESSION['id'];


$result = mysql_query("SELECT briefC.*,projects.projectCode,projects.projectName,client.clientName, client.clientCode, client.clientContactName FROM projects,client,briefC where briefC.companyID='$id' AND briefC_id='$BID' AND
 (briefC.briefC_status='1' || briefC.briefC_status='3' || briefC.briefC_status='4') AND projects.projectSerial=briefC.project_id AND client.clientSerial=briefC.client_id ");
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$PreparedBy = "";
$bstatus = $row["briefC_status"];
$scope = 'client-brief';
if ($row["PreparedBy"] == 0) {
    $resultPre = mysql_query("SELECT firstName, lastName FROM login where loginId='" . $id . "'");
    $PreparedBy .= mysql_result($resultPre, 0, 0);
    $PreparedBy .=" " . mysql_result($resultPre, 0, 1);
} else {
    $resultPre = mysql_query("SELECT internal_personnel_firstName, internal_personnel_lastName FROM internal_personnel where internal_personnel_id='" . $row["PreparedBy"] . "'");
    $PreparedBy .= mysql_result($resultPre, 0, 0);
    $PreparedBy .=" " . mysql_result($resultPre, 0, 1);
}
?>

<body>
<div>
    <div><br></div><!-- For errors and warnings-- Save your work, before quitting.. YES | NO>
</div>




<div style="overflow-x:auto;">
<div id="formdiv" > <!--div for the form (style="display:none;")-->
    <!--<table name="entryformheading" align="center" width="80%" class="table1" border="0" cellpadding="0">
         <tr>
          <th>
          <div style='float: left; text-align: left'><button id="hideformdiv" class="button1">&#x25bc;</button>
          Add</div>
          <div style='float: right; text-align: right'>&nbsp;</div>
          </th>
        </tr>
      </table>
    <form align="center" method="post" action="1-Quality-Procedures2.php?procedure_id=<?php //echo $proce_id?>">-->
        <input type="hidden" name="save_text_id" id="save_text_id" value="<?php echo $proce_id?>" />
        <fieldset>
            <legend align="center">CLIENT BRIEF</legend>
            <table name="entryform" align="center" class="table1" width="80%" border="0" cellpadding="0">
                <tr>
                  <td width="11%" align="left" valign="top"><strong>Project Code</strong>:</td>
                  <td width="22%"  align="left" valign="top"><?php echo $row["projectCode"] ?>- <?php echo $row["projectName"] ?></td>
                  <td width="11%" align="left" valign="top"><strong>Title</strong></td>
                  <td width="22%" align="left" valign="top"><span class="projectTitle1"><?php echo $row["briefC_name"]; ?></span></td>
                  <td width="11%" align="left" valign="top"><strong>ID/ Revision</strong>:</td>
                  <td width="23%" align="left" valign="top"><?php echo "CBRF - " . $BID; ?> (<?php echo $row["briefC_rev"]; ?>) </td>
                </tr>
                <tr>
                  <td align="left" valign="top"><strong>Prepared By</strong>:</td>
                  <td align="left" valign="top"><?php echo $PreparedBy; ?></td>
                  <td align="left" valign="top"><strong>Client</strong> <strong>Name</strong>:</td>
                  <td align="left" valign="top"><?php echo $row["clientContactName"]; ?>/ <?php echo $row["clientCode"]; ?>-<?php echo $row["clientName"]; ?></td>
                  <td align="left" valign="top"><strong>Date Initiated</strong>:</td>
                  <td align="left" valign="top"><?php echo $row["creationDate"]; ?></td>
                </tr>
            </table>
        </fieldset>
        </div>
	
	
	
	

<table width="100%" class="table4" align="center" border="0" cellpadding="0">
    <?php
    $query = "SELECT * from scope_brief WHERE scope_type='$scope' AND company_id = '$cid' AND brief_id = '$BID' AND parent=0 AND status = 1 ";
    $result = mysql_query($query);
    $k = 1;
    ?>

    <tbody>

    <tr align="right">
        <td valign="top">
        
            <?php if ($bstatus == 1)
            { ?>
                <image style="cursor: pointer"  class="imgbtn" onclick="showNewMainHeading()" name="newMainHeading" src="img/add.png" title="Add a Main Heading" />&nbsp;&nbsp;

                <input type="image" onClick="return submitallforms()" class="imgbtn" name="save1" src="img/save.png" title="Save All" />
                &nbsp;
                <input type="image" onclick="saveandclose()" class="imgbtn" name="saveclose" src="img/close.png" title="Save and Close" />
                &nbsp;
                <a href="#">
                    <img src="img/send.png" onclick="sendforinitiate(<?php echo $BID; ?>)" width="13" height="13" alt="" title="Send for Review"/></a>
                &nbsp;
                <a href="03-02Scope-01ClientBrief01DB.php?lock=<?php echo $BID; ?>">
                    <img src="img/lockgohome.png" width="13" height="13" alt="" title="Lock and Exit"/></a>

            <?php }else if( $bstatus == 4 ){ ?>
            <a href="03-02Scope-01ClientBrief01DB.php?unlock=<?php echo $BID; ?>">
                <img src="img/lockgohome.png" width="13" height="13" alt="" title="Unlock"/></a>
            <?php }?>
            &nbsp;
            <a href="CBPrintView.php?BID=<?php echo $BID;?>"><img src="img/print.png" width="13" height="13" alt="" title="PrintView"/></a>
            &nbsp;
        </td>
      </tr>
		 </table>


<table width="80%" class="table4" align="center" border="0" cellpadding="0">
    <!--Add Main Heading Form-->

    <?php if ($bstatus == 1){?>
        <tr id="divMainHeading" style="display: none">
            <td valign="top"><br>

            </td>

            <td colspan="3">
                <form id="newmainheadingform" method="post" action="#">

                    <input type="text" name="nheading" style="width:98% !important;" placeholder="New Main Heading" value=""/>
                    <input type="hidden" name="scopename" value="<?php echo $scope ?>">
                    <input type="hidden" name="task" value="newmainheading">
                    <input type="hidden" name="brief_id" value="<?php echo $BID ?>">
                    <input type="hidden" name="parent_id" value="0">
                    <input type="image" class="imgbtn" onclick="savenewmainheading()"
                           name="save2" src="img/save.png" title="Save New Main Heading"/>
                </form>
            </td>

        </tr>

        <?php
    }?>
</table>
	
	
<table width="80%" class="table4" align="center" border="0" cellpadding="0">

    <?php while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

        $query2 = "SELECT * from scope_brief_description WHERE `heading_id` = '" . $row['id'] . "' AND `brief_id` ='" . $BID . "' AND company_id= '$cid'";
        $results = mysql_query($query2);
        $row2 = mysql_fetch_object($results);
        ?>



        <tr id="pheading-<?php echo $row['id']; ?>"  >
			<td valign="top" colspan="4"><br>
				 <div style='float: left; text-align: left'>
                <span> <h3 style="display: inline"><?php  echo $k.'. '.$row['heading']; ?></h3></span></div>
					 <div style='float: right; text-align: right'>
                    <?php if( $bstatus == 3 || $bstatus == 4 ){?>
                    <image style="cursor: pointer; margin-left:5px"  class="imgbtn"  name="openEdit" src="img/edit.png" title="Edit" />&nbsp;
            <input type="image" style="margin-left:5px" class="imgbtn"  name="delete" src="img/delete.png" title="Delete Main Heading" />&nbsp;


        <?php } else{?>
                    <image style="cursor: pointer; margin-left:5px"  class="imgbtn" onclick="openedit('heading','<?php echo $row['id']?>')" name="openEdit" src="img/edit.png" title="Edit Heading" />&nbsp;
            <?php if($row['is_editable'] == 1): ?>
            <input type="image" style="margin-left:5px" class="imgbtn" onclick="deleteMainHeading('<?php echo  $row['id']; ?>')" name="delete" src="img/delete.png" title="Delete Main Heading" />&nbsp;
                         <?php
                         endif;
                         }?>
					
					                    <image style="cursor: pointer"  class="imgbtn" onclick="showNewHeading(<?php echo $row['id']; ?>)" name="newHeading" src="img/add.png" title="Add a Sub-Heading" />&nbsp;
				
				    <?php if ($bstatus == 3 || $bstatus == 4){
                ?>                <input type="image" class="imgbtn" onclick="#" name="save2" src="img/save.png" title="Save All" />


                <?php
            }
            else{
                ?>                <input type="image" class="imgbtn" onClick="return submitallforms()" name="save2" src="img/save.png" title="Save All" />
				
				</div>
					
                 </td>
        </tr>


        <tr class="editfield" style="display: none" id="peditheading-<?php echo $row['id']; ?>">

            <td><?php echo $k; ?></td>
            <td colspan="3">
                <form action="" method="post" id="formforheading-<?php echo $row['id'] ?>">
				<div name="buttoninside" style="position:relative">	
				<input type="text" required style="width:98% !important; padding-left:10px;" name="headingtext" value="<?php echo $row['heading'] ?>"/>
                    <input type="hidden" name="task" value="editheading">
                    <input type="hidden" name="headingid" value="<?php echo $row['id'] ?>">
                    <input type="image" class="imgbtn" onclick="submitheadingform('<?php echo $row['id'] ?>')"
                           name="save3" src="img/save.png" title="Save"/></div>

                </form>

            </td>
        </tr>




        <?php if ($bstatus == 1){?>
            <tr id="div<?php echo $row['id']; ?>" style="display: none">
                <td valign="top"><br>
                    
                </td>

                <td colspan="3">
                    <form id="newheadingform-<?php echo $row['id']; ?>" method="post" action="#">

                        <input type="text" name="nheading" style="width:98% !important;" placeholder="New Sub Heading" value=""/>
                        <input type="hidden" name="scopename" value="<?php echo $scope ?>">
                        <input type="hidden" name="task" value="newheading">
                        <input type="hidden" name="brief_id" value="<?php echo $BID ?>">
                        <input type="hidden" name="parent_id" value="<?php echo $row['id']; ?>">
                        <input type="image" class="imgbtn" onclick="savenewheading(<?php echo $row['id']; ?>)"
                               name="save2" src="img/save.png" title="Save New Heading"/>
                    </form>
                </td>

            </tr>

            <?php
        }?>






        <tr>
            <td valign="top"><br></td>

            <td colspan="2" valign="top">
                <form method="post" action="" id="submittextfor-<?php echo $row['id']; ?>">
                <textarea  <?php if ($bstatus == 3 || $bstatus == 4) echo 'disabled';?>  class="" name="btext-<?php echo $row['id'] ?>" id="btext-<?php echo $row['id'] ?>" rows="5">
                <?php  echo ((@$row2->description) ? @$row2->description : ''); ?>
            </textarea>
                <input type="hidden" name="bdescid" value="<?php echo @$row2->id ?>">
                <input type="hidden" name="headingid" value="<?php echo $row['id'] ?>">
                <input type="hidden" name="scopename" value="<?php echo $scope?>">
                <input type="hidden" name="task" value="changedesc">
                <input type="hidden" name="brief_id" value="<?php echo $BID ?>">
                </form>
                <br>
               
              
        
      
          <?php
            }?>

        

    </tr>




        <?php

        // code for custom headings
        $resultH = mysql_query("SELECT * FROM scope_brief WHERE parent = ".$row['id']." AND scope_type='$scope' AND  company_id = '$cid' AND brief_id = '$BID' ");

        $i = 1;

        while( $rowH = mysql_fetch_object($resultH) ){

            $queryHD ="SELECT * from scope_brief_description WHERE `heading_id` = '".$rowH->id."' AND `brief_id` ='".$BID."' AND company_id= '$cid'";
            $resultHD = mysql_query($queryHD);
            $rowHD = mysql_fetch_object($resultHD);
            ?>
            <tr id="pheading-<?php echo $rowH->id; ?>" >
                <td valign="top" colspan="4">
					<div style='float: left; text-align: left'>
                    <span> <h3 style="
                            margin-left: 52px; display: inline"><?php  echo $k.'.'.$i.'. '.$rowH->heading;  ?></h3></span></div>
					 <div style='float: right; text-align: right'>	
            <?php if( $bstatus == 3 || $bstatus == 4 ){
                ?>
                            <image style="cursor: pointer; margin-left:5px"  class="imgbtn" name="openEdit" src="img/edit.png" title="Edit" />
                <input type="image" style="margin-left:5px" class="imgbtn"  name="delete" src="img/delete.png" title="Delete" />
                <?php }else{?>
                <image style="cursor: pointer; margin-left:5px"  class="imgbtn" onclick="openedit('heading','<?php echo $rowH->id; ?>')" name="openEdit" src="img/edit.png" title="Edit" />
                <input type="image" style="margin-left:5px" class="imgbtn" onclick="deleteHeading('<?php echo  $rowH->id; ?>')" name="delete" src="img/delete.png" title="Delete" />&nbsp;
						 <?php }?>
					
					
                    <?php if ($bstatus == 3 || $bstatus == 4){
                        ?>                <input type="image" class="imgbtn" onclick="#" name="save2" src="img/save.png" title="Save" />


                        <?php
                    }
                    else{
                        ?> 
						 <input type="image" class="imgbtn" onClick="return submitallforms()" name="save2" src="img/save.png" title="Save" /></div>

                         
            </tr>



            <tr style="display: none" class="editfield" id="peditheading-<?php echo $rowH->id; ?>" >
                    <td><?php  echo  $k.'.'.$i; $i++;?></td>
                    <td >
                        <form action="" method="post" id="formforheading-<?php  echo $rowH->id;?>">

                        <input type="text" required style="width:98% !important" name="headingtext" value="<?php echo $rowH->heading; ?>" />
                        <input type="hidden" name="task" value="editheading">
                        <input type="hidden" name="headingid" value="<?php echo $rowH->id;?>">
                        <input type="image" class="imgbtn" onclick="submitheadingform('<?php echo $rowH->id;?>')" name="save4" src="img/save.png" title="Save" />
                </form>

                </td>
            </tr>



                <tr>
                    <td valign="top"><br></td>

                    <td valign="top"><form method="post" action="" id="submittextfor-<?php echo $rowH->id; ?>">
                      <textarea  <?php if ($bstatus == 3 || $bstatus == 4) echo 'disabled';?> class="" name="btext-<?php echo $rowH->id; ?>" id="btext-<?php echo $rowH->id; ?>" rows="5">
                <?php  echo ((@$rowHD->description) ? @$rowHD->description : ''); ?>
            </textarea>
                        <input type="hidden" name="bdescid" value="<?php echo @$rowHD->id ?>">
                        <input type="hidden" name="headingid" value="<?php echo $rowH->id ?>">
                        <input type="hidden" name="scopename" value="<?php echo $scope?>">
                        <input type="hidden" name="task" value="changedesc">
                        <input type="hidden" name="brief_id" value="<?php echo $BID ?>">
                        </form>
                        <br>
                        


                        <?php
                    }?>

            </tr>
            <?php


        }


        $k = $k + 1;
    }
    ?>












    </tbody>




</table>
<div class="loading" style="display:none;">Loading&#8230;</div><br><br>


<script>
    $("textarea").each(function(){
        CKEDITOR.replace($(this).attr("name"));
    });
</script>


<script>

    function saveandclose() {
        $('.loading').show();
        $('form[id^=submittextfor]').each(function () {
            formid = ($(this).attr("id"));
            submittextform($("#"+formid+" input[name=headingid]").val());
        });

        window.location.href = '03-02Scope-01ClientBrief01.php';

    }

    function submitallforms(){
        $('.loading').show();
        $('form[id^=submittextfor]').each(function () {
            formid = ($(this).attr("id"));
            submittextform($("#"+formid+" input[name=headingid]").val());
        });
alert("All data saved successfully.");
location.reload();
        return false;
    }


    function submittextform(id){
        var ht = $('body .cke_editable.cke_contents_ltr').html();
        $("#btext-" + id).val(ht);
        for (instance in CKEDITOR.instances) {
            CKEDITOR.instances[instance].updateElement();
        }
        formData = $("#submittextfor-"+id).serialize();
        url = "briefActions1.php";
        $.ajax({
            url: url,
            type: 'POST',
            data: formData,
            success: function (data) {
                if (data == 0) {
                    //alert("Descripton updated successfully with empty data");
                }

                else {
                    $("#btext-" + id).html(data);
                    //alert("Descripton updated successfully");
                }
            },
            error: function (e) {

            }
        });
        return false;
    }



    /*function submittextform(id) {
        var ptext = $("#btext-" + id).val();
        var ht = $('body .cke_editable.cke_contents_ltr').html();
        $("#btext-" + id).val(ht);
        for (instance in CKEDITOR.instances) {
            CKEDITOR.instances[instance].updateElement();
        }
        formData = $("#submittextfor-" + id).serialize();
        url = "briefActions1.php";
        $.ajax({
            url: url,
            type: 'POST',
            data: formData,
            success: function (data) {


                if (data == 0) {

                    alert("Descripton updated successfully with empty data");
                }

                else {

                    $("#btext-" + id).html(data);
                    alert("Descripton updated successfully");

                }
            },
            error: function (e) {

            }
        });

    }*/
    function submitheadingform(id){

        formData = $("#formforheading-"+id).serialize();
        url = "briefActions1.php";
        $.ajax({
            url: url,
            type: 'POST',
            data: formData,
            success: function (data) {


                if (data == 0) {

                    alert('error');
//                  location.reload();
                }

                else {
                    // $("#headingf-" + id).html(data);
                    closeedit('heading', id);
                    alert("Heading updated successfully");
                    //$("#alert-" + id).show();
                    // setTimeout(function () {
                    //     $("#alert-" + id).hide();
                    // }, 2000);
                }
            },
            error: function (e) {
                alert("Something went wrong.");
            }
        });

    }
    function savenewheading(id){


        formData = $("#newheadingform-"+id).serialize();
        url = "briefActions1.php";
        $.ajax({
            url: url,
            type: 'POST',
            data: formData,
            success: function (data) {
                if (data == 1) {
                    alert(" Sub-Heading added successfully");
//                  location.reload();
                }

                else {
                    alert(' Not added.');
                }
            },
            error: function (e) {

            }
        });
    }
    function savenewmainheading(id){


        formData = $("#newmainheadingform").serialize();
        url = "briefActions1.php";
        $.ajax({
            url: url,
            type: 'POST',
            data: formData,
            success: function (data) {
                if (data == 1) {
                    alert("New Main Heading added successfully");
//                  location.reload();
                }

                else {
                    alert(' Not added.');
                }
            },
            error: function (e) {

            }
        });
    }

    function deleteHeading(id){
        if (id > 0) {
            if(confirm("Are you sure you want to delete sub-heading ?")) {

                url = "briefActions1.php";

                $.ajax({
                    url: url,
                    type: 'POST',
                    data: {task:'deleteheading', hid:id},
                    success: function (data) {
                        if (data == 1) {
                            location.reload();
                        }

                        else {
                            alert("Something went wrong. Please try later");
                        }
                    },
                    error: function (e) {

                    }
                });
            } else {
                return 0;
            }



        }

    }


    function deleteMainHeading(id){
        if (id > 0) {
            if(confirm("Are you sure you want to delete main heading ?")) {

                url = "briefActions1.php";

                $.ajax({
                    url: url,
                    type: 'POST',
                    data: {task:'deleteheading', hid:id},
                    success: function (data) {
                        if (data == 1) {
                            location.reload();
                        }

                        else {
                            alert("Something went wrong. Please try later");
                        }
                    },
                    error: function (e) {

                    }
                });
            } else {
                return 0;
            }



        }

    }


    function openedit(str, id){

        var eid = "pedit"+str+"-"+id;
        var pid = "p"+str+"-"+id;
        $("#"+pid).hide();
        $("#"+eid).show();
    }
    function closeedit(str, id){

        var eid = "pedit"+str+"-"+id;
        //    var pid = "p"+str+"-"+id;
        $("#"+eid).hide();
        //$("#"+pid).show();

    }

    function showNewHeading( id )
    {

        $('#div'+id).toggle();
    }
    function showNewMainHeading(  )
    {

        $('#divMainHeading').toggle();
    }
</script>

</body>
</html>

<?php include('../includes/footer.php'); ?>
