<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');
$companyID = $_SESSION['id'];
?>

<html><head><meta charset="utf-8"><title>pmAnywhere - Scope Module</title>
   <link href="03-00Scope-Styles0.css" rel="stylesheet" type="text/css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
   <script src="../includes/ckeditor_new/ckeditor.js"></script>
<!--    <script src="ckeditor/ckeditor.js"></script>-->
</head>
<?php
$BID =isset($_GET['sid'])?$_GET['sid']:0;
$WID = $_GET['WID'];
$cid  = $_SESSION['id'];
$scope ='specs-brief';


$result = mysql_query("SELECT specs_scope.*,projects.*,client.* FROM projects,client,specs_scope where specs_scope.companyID='$id' AND specs_scope.SPECID='$BID' AND specs_scope.spec_status='1' AND projects.projectSerial=specs_scope.spec_project AND client.clientSerial=specs_scope.spec_client ");
$row = mysql_fetch_array($result , MYSQL_ASSOC);
$PreparedBy = "";
if($row["spec_preparedby"]==$row["companyID"]){
$resultPre = mysql_query("SELECT firstName, lastName FROM login where loginId='".$row["spec_preparedby"]."'");
//$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["spec_preparedby"]."'");
$PreparedBy .= mysql_result($resultPre,0,0);
$PreparedBy .=" ". mysql_result($resultPre,0,1);
}

?>

<body>
<div>
    <br>
    <fieldset>
        <legend align="center">SPECIFICATIONS<?php //echo $row["title"]; ?></legend>
        <table name="entryform" align="center" class="table1" width="80%" border="0" cellpadding="0">
            <tr>
                <td colspan="6" align="center" valign="top"><span class="projectTitle">
				<?php  
				if(!isset($_GET['iid'])){
					$WID = isset($_GET['WID'])?$_GET['WID']:0;
					$pack_type = 'wbs';
				  
					$result1 = mysql_query("SELECT * FROM globalwbs where WBSID ='$WID' && companyID = '$cid'  ORDER BY WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");
					$row1wbs = mysql_fetch_array($result1 , MYSQL_ASSOC);
					$WBSIDA = $row1wbs["WBSIDA"];
					$WBSIDB = $row1wbs["WBSIDB"];
					$WBSIDC = $row1wbs["WBSIDC"];
					$WBSIDD = $row1wbs["WBSIDD"];
					$WBSIDE = $row1wbs["WBSIDE"];
					$WBSID1 = $row1wbs["WBSID"];
					$parent = $row1wbs["parent"];
					$wbs_code = $WBSIDA.".".$WBSIDB.".".$WBSIDC.".".$WBSIDD.".".$WBSIDE;
				?>				
				<?php /*?><?php echo $row["title"]; ?> - <?php echo $row1wbs["sortCode"]; ?> - <?php */?>
                
                </span>
                </td>
            </tr>
            <tr>
                <td width="9%" align="left" valign="top"><strong>Project</strong>:</td>
                <td width="28%" align="left" valign="top"><?php echo $row["projectCode"]; ?> - <?php echo $row["projectName"]; ?></td>
                <td align="left" valign="top"><strong>Client</strong>:</td>
                <td align="left" valign="top"><?php echo $row["clientCode"]; ?> - <?php echo $row["clientName"]; ?></td>
                <td width="7%" align="left" valign="top"><strong>Consultant</strong></td>
                <td width="23%" align="left" valign="top">
					<?php $resultC = mysql_query("SELECT * FROM consultants WHERE conSerial = '".$row['spec_consultant']."'");
					  $consaltant = mysql_fetch_array($resultC, MYSQL_ASSOC);
					  echo $consaltant["conCode"]." - ".$consaltant['conName']."";
					?>
                </td>
            </tr>
            <tr>
              <td align="left" valign="top"><strong>Title</strong></td>
              <td align="left" valign="top"><?php echo $row["title"]; ?></td>
              <td align="left" valign="top"><strong>Section No</strong></td>
              <td align="left" valign="top"><?php echo $row1wbs["sortCode"]; ?> - <?php echo $wbs_code;?>

				</td>
              <td align="left" valign="top"><strong>Section Name</strong></td>
              <td align="left" valign="top"><?php echo $row1wbs["description"];                 
                }				
                else
                {
					$WID = isset($_GET['WID'])?$_GET['WID']:0;
					$iid = isset($_GET['iid'])?$_GET['iid']:0;
					$result123 = mysql_query("SELECT * FROM globalitems where WBSID = '$WID' && itemID='$iid' ORDER BY itemID ASC");
		
   					$row1item = mysql_fetch_array($result123 , MYSQL_ASSOC);
					echo $row1item["description"];                        
					
					$WID = $iid;
					$pack_type = 'item';
                }
                ?></td>
            </tr>
        </table>
    </fieldset></div>
	
	<table width="100%" align="center">
	  <tbody>
	    <tr>
	      <td align="right"><a href="03-05Scope-01Specifications03-DetailsPV.php?sid=<?php echo $BID; ?>&WID=<?php echo $_REQUEST['WID']; echo ((isset($_REQUEST['iid'])) ? "&iid=".$_REQUEST['iid'] : ""); ?>"><img src="img/print.png" width="12" height="12" alt=""/></a>&nbsp;<a href="03-05Scope-01Specifications02-Pack.php?BID=<?php echo $_REQUEST['sid']?>"><img src="img/deletecb.png" width="12" height="12" alt=""/></a></td>
        </tr>
      </tbody>
  </table>
	
<!--
<table align="center" width="69%" border="0" cellpadding="0">
    <tbody>
    <tr>
    	<td align="right">
<!--
			<a href="03-05Scope-01Specifications02-Pack.php?BID=<?php echo $_REQUEST['sid']?>"><img src="img/cancel.png" width="20" height="20" alt="" /></a>&nbsp;


<a href="03-05Scope-01Specifications03-DetailsPV.php?sid=<?php echo $BID; ?>&WID=<?php echo $_REQUEST['WID']; echo ((isset($_REQUEST['iid'])) ? "&iid=".$_REQUEST['iid'] : ""); ?>"><img src="img/print.png" width="20" height="20" alt=""/></a></td>
    </tr>
    </tbody>
</table>
-->
	
<table width="80%" class="table3 spec_detail" align="center" border="0" cellpadding="0">

    <tbody>

<tr>
    <td colspan="2">
        <h2 align="center">GENERAL</h2>
    </td>
</tr>

    <?php
    $query ="SELECT * from specs_scope_brief WHERE scope_type='$scope' AND (company_id= 0 OR company_id = '$cid') AND (brief_id= 0 OR brief_id = '$BID') AND brief_type ='General' AND (pack_id = 0 OR pack_id = '".$WID."')";
    $result = mysql_query($query);
    $k=1;

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

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

        ?>
            <tr>
            	<td valign="top" colspan="2">
                 	<h3><?php  echo '1.'.$k.'. '.$row['heading']; ?></h3>
                </td>
            </tr>
            <tr>
                <td colspan="2" valign="bottom">
                    <form method="post" action="javascript:void(0);" id="submittextfor-<?php echo $row['id']; ?>">
                        <textarea   style="width:97%" 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?>">
                        <input type="hidden" name="pack_id" value="<?php echo $WID?>">
                        <input type="hidden" name="pack_type" value="<?php echo $pack_type?>">
                    </form>
                 </td>
	            <td valign="bottom" width="96">
                	<input name="save2" type="image" class="imgbtn" title="Save" onClick="return submitallforms()" src="img/save.png" />
                    <?php if($row['company_id'] > 0 && $row['company_id'] == $companyID){?>
					
                    &nbsp;
					<a href="javascript:void(0);" onClick="deleteHeading(<?php echo $row['id']?>);"><img src="img/delete.png" width="19" alt="Delete" title="Delete"/></a>					
					<?php }	?>
					<!--
					&nbsp;
					<a href="03-05Scope-01Specifications03-DetailsPV.php?sid=<?php echo $BID; ?>&WID=<?php echo $_REQUEST['WID']; echo ((isset($_REQUEST['iid'])) ? "&iid=".$_REQUEST['iid'] : ""); ?>"><img src="img/print.png" width="20" height="20" alt=""/></a>
					-->
                </td>
            </tr>
            <tr><td>&nbsp;</td></tr>
        
        <?php
        $k=$k+1;
    }
    ?>
    
    
            <tr>
                <td nowrap width="5%" valign="top"><strong>New Para</strong></td>
                <td>
    				<form id="gnewheadingform" method="post" action="javascript:void(0);">
                        <input type="text" name="nheading" value="" onKeyDown="if(event.keyCode==13) {return submitallforms();}" />
                        <input type="hidden" name="scopename" value="<?php echo $scope?>">
                        <input type="hidden" name="brieftype" value="General">
                        <input type="hidden" name="task" value="newheading">
                        <input type="hidden" name="brief_id" value="<?php echo $BID?>">
                        <input type="hidden" name="pack_id" value="<?php echo $WID?>">
                        <input type="hidden" name="pack_type" value="<?php echo $pack_type?>">
                    </form>
                </td>
                <td>
                    <input type="image" class="imgbtn" onClick="return submitallforms()" name="save2" src="img/save.png" title="Save" />
                </td>                
            </tr>
        
        <!--<div class="col-md-1 col-sm-2 panel-edit" style="float:right"><a href="#" onclick="savenewheading()"><input type="image" src="../images/icon-save7.png" width="12" height="12" border="0"></a></div> -->



</tbody>

</table>

<table width="80%" class="table3 spec_detail" align="center" border="0" cellpadding="0">

    <tbody>
    <tr>
        <td colspan="2">
            <h2 align="center">PRODUCTS</h2>
        </td>
    </tr>
    <?php
    $query ="SELECT * from specs_scope_brief WHERE scope_type='$scope' AND (company_id= 0 OR company_id = '$cid') AND (brief_id= 0 OR brief_id = '$BID') AND brief_type='Products' AND (pack_id = 0 OR pack_id = '".$WID."')";
 $result = mysql_query($query);
$k = 1;

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

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

    ?>
            <tr>
            	<td valign="top" colspan="2">
                   <h3><?php  echo '2.'.$k.'. '.$row['heading']; ?></h3>
                </td>
            </tr>
            <tr>
                <td colspan="2" valign="top">
                    <form method="post" action="javascript:void(0);" id="submittextfor-<?php echo $row['id']; ?>">
                        <textarea   style="width:97%" 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?>">
                        <input type="hidden" name="pack_id" value="<?php echo $WID?>">
                        <input type="hidden" name="pack_type" value="<?php echo $pack_type?>">
					</form>
                 </td>
                <td valign="bottom" width="96">
                	<input type="image" class="imgbtn" onClick="return submitallforms()" name="save2" src="img/save.png" title="Save" />
                    <?php if($row['company_id'] > 0 && $row['company_id'] == $companyID){?>                    
                    		&nbsp;<a href="javascript:void(0);" onClick="deleteHeading(<?php echo $row['id']?>);"><img src="img/delete.png" width="19" alt="Delete" title="Delete"/></a>					
					<?php }	?>
                </td>
            </tr>
            <tr><td>&nbsp;</td></tr>
        
        <?php
    $k=$k+1;
    }
    ?>
      <tr>
            <td nowrap width="5%" valign="top"><strong>New Para</strong></td>
           	<td>
                <form id="pnewheadingform" method="post" action="javascript:void(0);">
                    <input type="text" name="nheading" value="" onKeyDown="if(event.keyCode==13) {return submitallforms();}" />
                    <input type="hidden" name="scopename" value="<?php echo $scope?>">
                    <input type="hidden" name="brieftype" value="Products">
                    <input type="hidden" name="task" value="newheading">
                    <input type="hidden" name="brief_id" value="<?php echo $BID?>">
                    <input type="hidden" name="pack_id" value="<?php echo $WID?>">
                    <input type="hidden" name="pack_type" value="<?php echo $pack_type?>">
                </form>
            </td>
            <td>
                <input type="image" class="imgbtn" onClick="return submitallforms()" name="save2" src="img/save.png" title="Save" />               
            </td>
        </tr>    
    <!--<div class="col-md-1 col-sm-2 panel-edit" style="float:right"><a href="#" onclick="savenewheading()"><input type="image" src="../images/icon-save7.png" width="12" height="12" border="0"></a></div> -->
</tbody>
</table>


<table width="80%" class="table3 spec_detail" align="center" border="0" cellpadding="0">

    <tbody>

    <tr>
        <td colspan="2">
            <h2 align="center">EXECUTION</h2>
        </td>
    </tr>
    <?php
    $query ="SELECT * from specs_scope_brief WHERE scope_type='$scope' AND (company_id= 0 OR company_id = '$cid') AND (brief_id= 0 OR brief_id = '$BID') AND brief_type='Execution' AND (pack_id = 0 OR pack_id = '".$WID."')";
    $result = mysql_query($query);
    $k=1;

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

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

        ?>
            <tr>
            	<td valign="top" colspan="2">
                	<h3><?php  echo '3.'.$k.'. '.$row['heading']; ?></h3>
                </td>
            </tr>
            <tr>
                <td valign="top" colspan="2">
                  <form method="post" action="javascript:void(0);" id="submittextfor-<?php echo $row['id']; ?>">  
                    <textarea   style="width:97%" 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?>">
                    <input type="hidden" name="pack_id" value="<?php echo $WID?>">
                    <input type="hidden" name="pack_type" value="<?php echo $pack_type?>">
				  </form>                 
                </td>
                <td valign="top" width="96">
                	<input type="image" class="imgbtn" onClick="return submitallforms()" name="save2" src="img/save.png" title="Save" />
                    <?php if($row['company_id'] > 0 && $row['company_id'] == $companyID){?>                    
                    		&nbsp;<a href="javascript:void(0);" onClick="deleteHeading(<?php echo $row['id']?>);"><img src="img/delete.png" width="19" alt="Delete" title="Delete"/></a>					
					<?php }	?>
                </td>
            </tr>
            <tr><td>&nbsp;</td></tr>
        
        <?php
        $k=$k+1;
    }
    ?>
    
        <tr>
            <td nowrap width="5%" valign="top"><strong>New Para</strong></td>
            <td>
              <form id="enewheadingform" method="post" action="javascript:void(0);">
                <input type="text" name="nheading" value="" onKeyDown="if(event.keyCode==13) {return submitallforms();}" />
                <input type="hidden" name="scopename" value="<?php echo $scope?>">
                <input type="hidden" name="brieftype" value="Execution">
                <input type="hidden" name="task" value="newheading">
                <input type="hidden" name="brief_id" value="<?php echo $BID?>">
                <input type="hidden" name="pack_id" value="<?php echo $WID?>">
                <input type="hidden" name="pack_type" value="<?php echo $pack_type?>">
              </form>
            </td>
            <td>
                <input type="image" class="imgbtn" onClick="return submitallforms()" name="save2" src="img/save.png" title="Save" />               
            </td>
        </tr>
    
    </tbody>
</table>
<div class="loading" style="display:none;">Loading&#8230;</div>
<script>
    $("textarea").each(function(){
		CKEDITOR.replace($(this).attr("name"),
		{
			width: "100%",
			height:"150px",
			toolbar: [
						{ name: 'document', items: [ 'Source', '-', 'NewPage', 'Preview', '-', 'Templates' ] },	// Defines toolbar group with name (used to create voice label) and items in 3 subgroups.
						[ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo', 'Bold', 'Italic', 'Strike', '-', 'RemoveFormat','NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'Styles', 'Format', 'Table' ],			// Defines toolbar group without name.
						
					]
		});
	});
</script>
<script>
    function submitallforms(){
		$('.loading').show();
		$('form[id^=submittextfor]').each(function () {
			formid = ($(this).attr("id"));
			submittextform($("#"+formid+" input[name=headingid]").val());
		});
		if(savegnewheading()){
			setTimeout(function(){
				saveenewheading();
				setTimeout(function(){
					savepnewheading();					
					setTimeout(function(){ 
						window.location = "03-05Scope-01Specifications03-Details.php?sid=<?php echo $_REQUEST['sid']?>&WID=<?php echo $_REQUEST['WID']; echo ((isset($_REQUEST['iid'])) ? "&iid=".$_REQUEST['iid'] : ""); ?>";
					}, 1000);
				},1000)
			},1000)				
		}	
		
		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 = "briefActions2.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 submitheadingform(id){

        formData = $("#formforheading-"+id).serialize();
        url = "briefActions2.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-" + id).html("Heading updated successfully");
                    $("#alert-" + id).show();
                    setTimeout(function () {
                        $("#alert-" + id).hide();
                    }, 2000);
                }
            },
            error: function (e) {

            }
        });
		return false;
    }

    function savepnewheading(){

        if($('#pnewheadingform input[name=nheading]').val() != ""){
			formData = $("#pnewheadingform").serialize();
			url = "briefActions2.php";
			$.ajax({
				url: url,
				type: 'POST',
				data: formData,
				success: function (data) {
					if (data == 0) {	
					   // alert('error');
					   // location.reload();
					} else {	
					  // alert("Heading added successfully");	
					}
				},
				error: function (e) {
	
				}
			});
		}
		return true;
    }

    function saveenewheading(){

        if($('#enewheadingform input[name=nheading]').val() != ""){
			formData = $("#enewheadingform").serialize();
			url = "briefActions2.php";
			$.ajax({
				url: url,
				type: 'POST',
				data: formData,
				success: function (data) {
					if (data == 0) {	
					   // alert('error');
					   // location.reload();
					} else {
						//alert("Heading added successfully");
					}
				},
				error: function (e) {
	
				}
			});			
		}
		return true;
    }

    function savegnewheading(){
		if($('#gnewheadingform input[name=nheading]').val() != ""){
			formData = $("#gnewheadingform").serialize();
			url = "briefActions2.php";
			$.ajax({
				url: url,
				type: 'POST',
				data: formData,
				success: function (data) {
					if (data == 0) {	
					   // alert('error');
					   // location.reload();
					} else {
					   // alert("Heading added successfully");					
					}
				},
				error: function (e) {
	
				}
			});			
		}
		return true;
    }

    function deleteHeading(id){
        if (id > 0) {
            if(confirm("Its advisable to save your work. All unsaved work will be lost. Are you sure you want to delete this heading.")){
                    url = "briefActions2.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) {

                        }
                    });                
			}
        }
		return false;
    }
</script>

</body>
</html>

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