<?php include('lib/config.php');?>
<?php       
    //===============================================================   
    $toDay = date("Y-m-d");
    $cDateTime = date("mdYHis"); 
    
    //===============================================================
    //============== Update record ==================================
    //===============================================================
    if(isset($_POST['action']) && $_POST['action'] =='updateGenInfo')
    { 		
		$shid = isset($_POST['shid'])?$_POST['shid']:'';
		$name = isset($_POST['name'])?$_POST['name']:'';  
    	$abbreviation = isset($_POST['abbreviation'])?$_POST['abbreviation']:'';		
		
		$areas_of_operations = isset($_POST['areas_of_operations'])?$_POST['areas_of_operations']:'';
		$pwd_oriented = isset($_REQUEST['pwd_oriented'])?$_REQUEST['pwd_oriented']:'N';
		$address = isset($_POST['address'])?$_POST['address']:'';
		$focal_person = isset($_POST['focal_person'])?$_POST['focal_person']:'';    
		$tel_no = isset($_POST['tel_no'])?$_POST['tel_no']:'';
		$email = isset($_POST['email'])?$_POST['email']:'';
		$website_url = isset($_POST['website_url'])?$_POST['website_url']:'';
		
		$category_id = isset($_POST['category_id'])?$_POST['category_id']:'';
		$orientation_sh_interest = isset($_POST['orientation_sh_interest'])?$_POST['orientation_sh_interest']:'';
		$orientation_sh_influence = isset($_POST['orientation_sh_influence'])?$_POST['orientation_sh_influence']:'';
		$sh_current_orientation = isset($_POST['sh_current_orientation'])?$_POST['sh_current_orientation']:'';
		$sh_desired_orientation = isset($_POST['sh_desired_orientation'])?$_POST['sh_desired_orientation']:'';
		
		//===============================================================
		//============== Uploading Files ===========================
		//===============================================================
		
		$oldDocfile = isset($_POST['old_sh_logo'])?$_POST['old_sh_logo']:'';
		
		$uploaddir = 'uploads/stake-holders/';
		
		if(isset($_FILES['sh_logo']['name']) && $_FILES['sh_logo']['name'] != '')
		{			
			$docarr = explode(".", $_FILES['sh_logo']['name']);						
			$docfile = $docarr[0]."_".$cDateTime.'.'.$docarr[1];
			$upload = $uploaddir . $docfile;
			if(move_uploaded_file($_FILES['sh_logo']['tmp_name'], $upload))
			{
				@unlink("uploads/stake-holders/$oldImgfile");			
				$oldDocfile = $docfile;
			}	
		}
		//===============================================================
		$updQry = "UPDATE ".TBL_STAKEHOLDERS." SET 
        name='".addslashes($name)."',
        abbreviation='".addslashes($abbreviation)."',
		sh_logo='".addslashes($oldDocfile)."',		
		areas_of_operations='".addslashes($areas_of_operations)."',
		pwd_oriented='".addslashes($pwd_oriented)."',
        address='".addslashes($address)."',
        focal_person='".addslashes($focal_person)."',
        tel_no='".addslashes($tel_no)."',
        email='".addslashes($email)."',	
		website_url='".addslashes($website_url)."',		
        category_id='".addslashes($category_id)."',
        orientation_sh_interest='".addslashes($orientation_sh_interest)."',
		orientation_sh_influence='".addslashes($orientation_sh_influence)."',
        sh_current_orientation='".addslashes($sh_current_orientation)."',
        sh_desired_orientation='".addslashes($sh_desired_orientation)."',
		date_modified='".$toDay."' 		
		WHERE id = '".$shid."' LIMIT 1";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        
        if($errNo===TRUE )
		{
			//------------------------------------
			$selSH_Query = "SELECT ".TBL_CATEGORIES.".category_title,".TBL_STAKEHOLDERS.".* FROM ".TBL_STAKEHOLDERS." 
			INNER JOIN ".TBL_CATEGORIES." ON (".TBL_STAKEHOLDERS.".category_id=".TBL_CATEGORIES.".category_id)
			WHERE ".TBL_STAKEHOLDERS.".id = '".$shid."' ";
			$shInfo = $con->getSingleRow($selSH_Query);
			//------------------------------------
			$influence = "";
			if(isset($shInfo['orientation_sh_influence']) &&  $shInfo['orientation_sh_influence']== '7')
			{
				$influence = 'Low';
			}
			elseif(isset($shInfo['orientation_sh_influence']) &&  $shInfo['orientation_sh_influence']== '14')
			{
				$influence = 'Medium';
			}
			elseif(isset($shInfo['orientation_sh_influence']) &&  $shInfo['orientation_sh_influence']== '20')
			{
				$influence = 'High';
			}
			//------------------------------------
			$current_orientation = "";
			if(isset($shInfo['sh_current_orientation']) &&  $shInfo['sh_current_orientation']== 'green')
			{
				$current_orientation = 'Supportive';
			}
			elseif(isset($shInfo['sh_current_orientation']) &&  $shInfo['sh_current_orientation']== 'red')
			{
				$current_orientation = 'Resistant';
			}
			elseif(isset($shInfo['sh_current_orientation']) &&  $shInfo['sh_current_orientation']== 'orange')
			{
				$current_orientation = 'Neutral';
			}
			//------------------------------------
			$desired_orientation = "";
			
			if(isset($shInfo['sh_desired_orientation']) &&  $shInfo['sh_desired_orientation']== 'green')
			{
				$desired_orientation = 'Supportive';
			}
			elseif(isset($shInfo['sh_desired_orientation']) &&  $shInfo['sh_desired_orientation']== 'red')
			{
				$desired_orientation =  'Resistant';
			}
			elseif(isset($shInfo['sh_desired_orientation']) &&  $shInfo['sh_desired_orientation']== 'orange')
			{
				$desired_orientation = 'Neutral';
			}
			//------------------------------------		
			$return_arr[] = array("message" => 'ok',"name" => $shInfo['name'],"abbreviation" => $shInfo['abbreviation'],"sh_logo" => "uploads/stake-holders/".$shInfo['sh_logo'],	"category_title" => $shInfo['category_title'],"interest" => $shInfo['orientation_sh_interest'],"influence" => $influence,"current_orientation" => $current_orientation,"desired_orientation" => $desired_orientation);
		}
        else
		{
            $return_arr[] = array("message" => 'err');
		}
                    
        echo json_encode($return_arr);
    }
	elseif(isset($_POST['action']) && $_POST['action'] =='updateIntroduction')
    { 		
		$shid = isset($_POST['shid'])?$_POST['shid']:'';
		$description = isset($_POST['description'])?$_POST['description']:'';
		
		$updQry = "UPDATE ".TBL_STAKEHOLDERS." SET description='".addslashes($description)."',date_modified='".$toDay."' WHERE id = '".$shid."' LIMIT 1";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        
        if($errNo===TRUE )
		{
			$return_arr[] = array("message" => 'ok',"introduction" => $description);
		}
        else
		{
            $return_arr[] = array("message" => 'err');
		}
                    
        echo json_encode($return_arr);
    }	
	elseif(isset($_POST['action']) && $_POST['action'] =='updateSHSummaries')
    { 		
		$shid = isset($_POST['shid'])?$_POST['shid']:'';
		
		$important_for_sh = isset($_POST['important_for_sh'])?$_POST['important_for_sh']:'';
		$sh_contribute = isset($_POST['sh_contribute'])?$_POST['sh_contribute']:'';
		$sh_block = isset($_POST['sh_block'])?$_POST['sh_block']:'';	
		$sh_issues_concerns = isset($_POST['sh_issues_concerns'])?$_POST['sh_issues_concerns']:'';	
		$sh_summary_interaction = isset($_POST['sh_summary_interaction'])?$_POST['sh_summary_interaction']:'';
		
		$updQry = "UPDATE ".TBL_STAKEHOLDERS." SET 
        important_for_sh='".addslashes($important_for_sh)."',
        sh_contribute='".addslashes($sh_contribute)."',
        sh_block='".addslashes($sh_block)."',
        sh_issues_concerns='".addslashes($sh_issues_concerns)."',
        sh_summary_interaction='".addslashes($sh_summary_interaction)."',
		date_added='".$toDay."'
		WHERE id = '".$shid."' LIMIT 1 ";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        
        if($errNo===TRUE )
		{
			//------------------------------------
			$selSH_Query = "SELECT ".TBL_CATEGORIES.".category_title,".TBL_STAKEHOLDERS.".* FROM ".TBL_STAKEHOLDERS." 
			INNER JOIN ".TBL_CATEGORIES." ON (".TBL_STAKEHOLDERS.".category_id=".TBL_CATEGORIES.".category_id)
			WHERE ".TBL_STAKEHOLDERS.".id = '".$shid."' ";
			$shInfo = $con->getSingleRow($selSH_Query);
			//------------------------------------
			$return_arr[] = array("message" => 'ok',"important_for_sh" => $shInfo['important_for_sh'],"sh_contribute" => $shInfo['sh_contribute'],"sh_block" => $shInfo['sh_block'],"sh_issues_concerns" => $shInfo['sh_issues_concerns'],"sh_summary_interaction" => $shInfo['sh_summary_interaction']);
		}
        else
		{
            $return_arr[] = array("message" => 'err',"introduction" => '');
		}
                    
        echo json_encode($return_arr);
    }
	elseif(isset($_POST['action']) && $_POST['action'] =='updateSHupdate')
    { 		
		$shid = isset($_POST['shid'])?$_POST['shid']:''; 
		$uid = isset($_POST['uid'])?$_POST['uid']:'';
		$update_detail = isset($_POST['update_detail'])?$_POST['update_detail']:'';
		
		$updQry = "UPDATE ".TBL_RANKING_HISTORY." SET update_detail='".addslashes($update_detail)."',date_modified='".$toDay."' WHERE id = '".$uid."' LIMIT 1";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        
        if($errNo===TRUE )
		{
			$return_arr[] = array("message" => 'ok',"Uupdate" => $update_detail,"Utimestamp" => date("d-m-Y",strtotime($toDay)) );
		}
        else
		{
            $return_arr[] = array("message" => 'err');
		}
                    
        echo json_encode($return_arr);
    }	
	
	elseif(isset($_POST['action']) && $_POST['action'] =='addInteractions')
    { 		
		$shid = isset($_POST['sid'])?$_POST['sid']:'';
		
		$interaction_no = isset($_POST['interaction_no'])?$_POST['interaction_no']:'';
		$interaction_date = isset($_POST['interaction_date'])?$_POST['interaction_date']:'';
		$interaction_detail = isset($_POST['interaction_detail'])?$_POST['interaction_detail']:'';
		
		//===============================================================
		//============== Uploading Files ===========================
		//===============================================================
		$oldDocfile = isset($_POST['old_sh_logo'])?$_POST['old_sh_logo']:'';
		
		$uploaddir = 'uploads/documents/';
		
		if(isset($_FILES['attachment_file']['name']) && $_FILES['attachment_file']['name'] != '')
		{			
			$docarr = explode(".", $_FILES['attachment_file']['name']);						
			$docfile = $docarr[0]."_".$cDateTime.'.'.$docarr[1];
			$upload = $uploaddir . $docfile;
			if(move_uploaded_file($_FILES['attachment_file']['tmp_name'], $upload))
			{
				@unlink("uploads/documents/$oldImgfile");			
				$oldDocfile = $docfile;
			}	
		}
		//===============================================================
		$newStrategryData = "";
		$updQry = "INSERT INTO ".TBL_INTERACTION_UPDATES." SET 
        sh_id='".addslashes($shid)."',
		interaction_no='".addslashes($interaction_no)."',
        interaction_date='".addslashes($interaction_date)."',
		interaction_detail='".addslashes($interaction_detail)."',
        attachment_file='".addslashes($oldDocfile)."',
		date_added='".$toDay."' ";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        //echo $updQry;
        if($errNo===TRUE )
		{
			?>			
            
              <?php 
				$counter_intupd = 0;
				$selQuery_int_upd = "SELECT * FROM  ".TBL_INTERACTION_UPDATES." WHERE sh_id='".$shid."' ORDER BY id desc;";
				$intupdInfo = $con->getMultiRow($selQuery_int_upd);
				$tot_mappingsInfo = count($intupdInfo);
				if($tot_mappingsInfo > 0)
				{
					 for($int=0;$int<$tot_mappingsInfo;$int++)
					 {
						$counter_intupd++;
			  ?>
			  <?php if(!empty($intupdInfo[$int]['interaction_detail'])){?>                              
			  <tr>
				<td id="interno_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_no'];?></td>
				<td id="interdate_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_date'];?></td>
				<td id="interdetail_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_detail'];?></td>
                <td id="attachment_file_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>">
                                <a href="uploads/documents/<?php echo isset($intupdInfo[$int]['attachment_file'])?$intupdInfo[$int]['attachment_file']:'';?>" target="_blank" title="Click to view or download"><?php echo isset($intupdInfo[$int]['attachment_file'])?$intupdInfo[$int]['attachment_file']:'';?></a></td>
				<td style="text-align:center;">
				<i class="fa fa-edit popup" href="sh-update-interations.php?sid=<?php echo $intupdInfo[$int]['sh_id'];?>&intid=<?php echo $intupdInfo[$int]['id'];?>" style=" cursor:pointer;" title="Update Strategy"></i>&nbsp;
				<i class="fa fa-trash popup" data-title="Delete Strategy" href="del-interations.php?sid=<?php echo $intupdInfo[$int]['sh_id'];?>&intid=<?php echo $intupdInfo[$int]['id'];?>" style=" cursor:pointer;" title="Delete Strategy"></i>
				</td>
			  </tr>                        
			  <?php }?>
			  <?php }
			 }?>
				<script>
					$( document ).ready(function() {   					                 
						$(".popup").colorbox({
							/*onComplete : function() { 
							   $(this).colorbox.resize(); 
							},*/ 
						  escKey: false,
						  overlayClose: false,
						  opacity:"0.2",
						  width:"60%",
						   /*height:"500",
						  title:"To close this dialog click X icon at the right"*/
						});						
					});
                </script>                      
			<?php 
			//$related_documents = '<a href="uploads/documents/'.$oldDocfile.'" target="_blank" title="Click to view or download">'.$oldDocfile.'</a>';            
			//$return_arr[] = array("message" => 'ok',"strategy" => $strategy,"responsibility" => $responsibility,"key_milestones" => $key_milestones,"related_documents" => $related_documents);
		}
        else
		{
            //$return_arr[] = array("message" => 'err',"introduction" => '');
		}
    }
	elseif(isset($_POST['action']) && $_POST['action'] =='updateInteractions')
    { 		
		$shid = isset($_POST['sid'])?$_POST['sid']:'';
		$intid = isset($_POST['intid'])?$_POST['intid']:'';
		
		$interaction_no = isset($_POST['interaction_no'])?$_POST['interaction_no']:'';
		$interaction_date = isset($_POST['interaction_date'])?$_POST['interaction_date']:'';
		$interaction_detail = isset($_POST['interaction_detail'])?$_POST['interaction_detail']:'';
		
		//===============================================================
		//============== Uploading Files ===========================
		//===============================================================
		$oldDocfile = isset($_POST['old_attachment_file'])?$_POST['old_attachment_file']:'';
		
		$uploaddir = 'uploads/documents/';
		
		if(isset($_FILES['attachment_file']['name']) && $_FILES['attachment_file']['name'] != '')
		{			
			$docarr = explode(".", $_FILES['attachment_file']['name']);						
			$docfile = $docarr[0]."_".$cDateTime.'.'.$docarr[1];
			$upload = $uploaddir . $docfile;
			if(move_uploaded_file($_FILES['attachment_file']['tmp_name'], $upload))
			{
				@unlink("uploads/documents/$oldImgfile");			
				$oldDocfile = $docfile;
			}	
		}
		//===============================================================
		$updQry = "UPDATE ".TBL_INTERACTION_UPDATES." SET 
        interaction_no='".addslashes($interaction_no)."',
        interaction_date='".addslashes($interaction_date)."',
		interaction_detail='".addslashes($interaction_detail)."',
        attachment_file='".addslashes($oldDocfile)."',
		date_modified='".$toDay."' 		
		WHERE id = '".$intid."' LIMIT 1";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        
        if($errNo===TRUE)
		{?>
			<?php 
			$counter_intupd = 0;
			$selQuery_int_upd = "SELECT * FROM  ".TBL_INTERACTION_UPDATES." WHERE sh_id='".$shid."' ORDER BY id desc;";
			$intupdInfo = $con->getMultiRow($selQuery_int_upd);
			$tot_mappingsInfo = count($intupdInfo);
			if($tot_mappingsInfo > 0){
				 for($int=0;$int<$tot_mappingsInfo;$int++)
				 {
					$counter_intupd++;
			  ?>
			  <?php if(!empty($intupdInfo[$int]['interaction_detail'])){?>                              
			  <tr>
				<td id="interno_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_no'];?></td>
				<td id="interdate_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_date'];?></td>
				<td id="interdetail_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_detail'];?></td>
                <td id="attachment_file_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>">
                                <a href="uploads/documents/<?php echo isset($intupdInfo[$int]['attachment_file'])?$intupdInfo[$int]['attachment_file']:'';?>" target="_blank" title="Click to view or download"><?php echo isset($intupdInfo[$int]['attachment_file'])?$intupdInfo[$int]['attachment_file']:'';?></a></td>
				<td style="text-align:center;">
				<i class="fa fa-edit popup" href="sh-update-interations.php?sid=<?php echo $intupdInfo[$int]['sh_id'];?>&intid=<?php echo $intupdInfo[$int]['id'];?>" style=" cursor:pointer;" title="Update Strategy"></i>&nbsp;
				<i class="fa fa-trash popup" data-title="Delete Strategy" href="del-interations.php?sid=<?php echo $intupdInfo[$int]['sh_id'];?>&intid=<?php echo $intupdInfo[$int]['id'];?>" style=" cursor:pointer;" title="Delete Strategy"></i>
				</td>
			  </tr>                        
			  <?php }?>
			  <?php }
			 }?>
				<script>
					$( document ).ready(function() {   					                 
						
						$(".popup").colorbox({
						  /*onComplete : function() { 
						    $(this).colorbox.resize(); 
						  },*/
						  escKey: false,
						  overlayClose: false,
						  opacity:"0.2",
						  width:"60%",
						  /*height:"500",
						  title:"To close this dialog click X icon at the right"*/
						});
						
					});
                </script>
            <?php 
			//$attachment_file = '<a href="uploads/documents/'.$oldDocfile.'" target="_blank" title="Click to view or download">'.$oldDocfile.'</a>';
            
			//$return_arr[] = array("message" => 'ok',"interaction_no" => $interaction_no,"interaction_date" => $interaction_date,"interaction_detail" => $interaction_detail,"attachment_file" => $attachment_file);
		}
        else
		{
            $return_arr[] = array("message" => 'err',"" => '');
		}
        
        echo json_encode($return_arr);
    }
	elseif(isset($_POST['action']) && $_POST['action'] =='deleteInteractions')
    { 		
			$intid = isset($_POST['intid'])?$_POST['intid']:'';
			$shid = isset($_POST['sid'])?$_POST['sid']:'';		
			//===============================================================		
			$delQuery = "DELETE FROM ".TBL_INTERACTION_UPDATES." WHERE id='".$intid."' AND sh_id='".$shid."' LIMIT 1;";
			$errNo = $con->deleteRecords($delQuery);
				
			if($errNo===TRUE )
			{
			?>			
            <?php 
			$counter_intupd = 0;
			$selQuery_int_upd = "SELECT * FROM  ".TBL_INTERACTION_UPDATES." WHERE sh_id='".$shid."' ORDER BY id desc;";
			$intupdInfo = $con->getMultiRow($selQuery_int_upd);
			$tot_mappingsInfo = count($intupdInfo);
			if($tot_mappingsInfo > 0){
				 for($int=0;$int<$tot_mappingsInfo;$int++)
				 {
					$counter_intupd++;
			  ?>
			  <?php if(!empty($intupdInfo[$int]['interaction_detail'])){?>                              
			  <tr>
				<td id="interno_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_no'];?></td>
				<td id="interdate_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_date'];?></td>
				<td id="interdetail_<?php echo $shInfo['id'];?>_<?php echo $intupdInfo[$int]['id'];?>"><?php echo $intupdInfo[$int]['interaction_detail'];?></td>
				<td style="text-align:center;">
				<i class="fa fa-edit popup" href="sh-update-interations.php?sid=<?php echo $intupdInfo[$int]['sh_id'];?>&intid=<?php echo $intupdInfo[$int]['id'];?>" style=" cursor:pointer;" title="Update Strategy"></i>&nbsp;
				<i class="fa fa-trash popup" data-title="Delete Strategy" href="del-interations.php?sid=<?php echo $intupdInfo[$int]['sh_id'];?>&intid=<?php echo $intupdInfo[$int]['id'];?>" style=" cursor:pointer;" title="Delete Strategy"></i>
				</td>
			  </tr>                        
			  <?php }?>
			  <?php }
			 }?>
				<script>
					$( document ).ready(function() {   					                 
						
						$(".popup").colorbox({
						  /*onComplete : function() { 
						    $(this).colorbox.resize(); 
						  },*/
						  escKey: false,
						  overlayClose: false,
						  opacity:"0.2",
						  width:"60%",
						  /*height:"500",
						  title:"To close this dialog click X icon at the right"*/
						});
						
					});
                </script>                       
			<?php 
			//$related_documents = '<a href="uploads/documents/'.$oldDocfile.'" target="_blank" title="Click to view or download">'.$oldDocfile.'</a>';            
			//$return_arr[] = array("message" => 'ok',"strategy" => $strategy,"responsibility" => $responsibility,"key_milestones" => $key_milestones,"related_documents" => $related_documents);
		}
        else
		{
            //$return_arr[] = array("message" => 'err',"introduction" => '');
		}
    }
	
	elseif(isset($_POST['action']) && $_POST['action'] =='addStrategies')
    { 		
		$shid = isset($_POST['sid'])?$_POST['sid']:'';
		
		$strategy = isset($_POST['strategy'])?$_POST['strategy']:'';
		$responsibility = isset($_POST['responsibility'])?$_POST['responsibility']:'';
		$key_milestones = isset($_POST['key_milestones'])?$_POST['key_milestones']:'';
		
		//===============================================================
		//============== Uploading Files ===========================
		//===============================================================
		$oldDocfile = isset($_POST['old_sh_logo'])?$_POST['old_sh_logo']:'';
		
		$uploaddir = 'uploads/documents/';
		
		if(isset($_FILES['related_documents']['name']) && $_FILES['related_documents']['name'] != '')
		{			
			$docarr = explode(".", $_FILES['related_documents']['name']);						
			$docfile = $docarr[0]."_".$cDateTime.'.'.$docarr[1];
			$upload = $uploaddir . $docfile;
			if(move_uploaded_file($_FILES['related_documents']['tmp_name'], $upload))
			{
				@unlink("uploads/documents/$oldImgfile");			
				$oldDocfile = $docfile;
			}	
		}
		//===============================================================
		$newStrategryData = "";
		$updQry = "INSERT INTO ".TBL_STRATEGIES." SET 
        sh_id='".addslashes($shid)."',
		strategy='".addslashes($strategy)."',
        responsibility='".addslashes($responsibility)."',
		key_milestones='".addslashes($key_milestones)."',
        related_documents='".addslashes($oldDocfile)."',
		date_added='".$toDay."' ";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        //echo $updQry;
        if($errNo===TRUE )
		{
			?>			
            <?php 
			$counter_str = 0;
			$selQuery_str = "SELECT * FROM  ".TBL_STRATEGIES." WHERE sh_id='".$shid."' ORDER BY id desc;";
			$strInfo = $con->getMultiRow($selQuery_str);
			$tot_mappingsInfo = count($strInfo);
			if($tot_mappingsInfo > 0){
				 for($ru=0;$ru<$tot_mappingsInfo;$ru++)
				 {
					$counter_str++;
			?>
			<tr>
			  <td><?php echo $counter_str;?>.</td>
			  <td id="strategy_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['strategy'])?$strInfo[$ru]['strategy']:'';?></td>
			  <td id="responsibility_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['responsibility'])?$strInfo[$ru]['responsibility']:'';?></td>
			  <td id="key_milestones_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['key_milestones'])?$strInfo[$ru]['key_milestones']:'';?></td>
			  <td id="related_documents_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><a href="uploads/documents/<?php echo isset($strInfo[$ru]['related_documents'])?$strInfo[$ru]['related_documents']:'';?>" target="_blank" title="Click to view or download"><?php echo isset($strInfo[$ru]['related_documents'])?$strInfo[$ru]['related_documents']:'';?></a></td>
			  <td style="text-align:center;"><i class="fa fa-edit popup" href="sh-update-strategies.php?sid=<?php echo $strInfo[$ru]['sh_id'];?>&stid=<?php echo $strInfo[$ru]['id'];?>" style=" cursor:pointer;" title="Update Strategy"></i>&nbsp;
                                <i class="fa fa-trash popup" data-title="Delete Strategy" href="del-strategy.php?sid=<?php echo $strInfo[$ru]['sh_id'];?>&stid=<?php echo $strInfo[$ru]['id'];?>" style=" cursor:pointer;" title="Delete Strategy"></i></td>
			</tr>
			<?php }
			}?> 
				<script>
					$( document ).ready(function() {   					                 
						$(".popup").colorbox({
							/*onComplete : function() { 
							   $(this).colorbox.resize(); 
							},*/ 
						  escKey: false,
						  overlayClose: false,
						  opacity:"0.2",
						  width:"60%",
						   /*height:"500",
						  title:"To close this dialog click X icon at the right"*/
						});						
					});
                </script>                      
			<?php 
			//$related_documents = '<a href="uploads/documents/'.$oldDocfile.'" target="_blank" title="Click to view or download">'.$oldDocfile.'</a>';            
			//$return_arr[] = array("message" => 'ok',"strategy" => $strategy,"responsibility" => $responsibility,"key_milestones" => $key_milestones,"related_documents" => $related_documents);
		}
        else
		{
            //$return_arr[] = array("message" => 'err',"introduction" => '');
		}
    }
	elseif(isset($_POST['action']) && $_POST['action'] =='addFrmStrategies')
    { 
		$shid = isset($_POST['sid'])?$_POST['sid']:'';
		
		$strategy = isset($_POST['strategy'])?$_POST['strategy']:'';
		$responsibility = isset($_POST['responsibility'])?$_POST['responsibility']:'';
		$key_milestones = isset($_POST['key_milestones'])?$_POST['key_milestones']:'';
		
		//===============================================================
		//============== Uploading Files ===========================
		//===============================================================
		$oldDocfile = isset($_POST['old_sh_logo'])?$_POST['old_sh_logo']:'';
		
		$uploaddir = 'uploads/documents/';
		
		if(isset($_FILES['related_documents']['name']) && $_FILES['related_documents']['name'] != '')
		{			
			$docarr = explode(".", $_FILES['related_documents']['name']);						
			$docfile = $docarr[0]."_".$cDateTime.'.'.$docarr[1];
			$upload = $uploaddir . $docfile;
			if(move_uploaded_file($_FILES['related_documents']['tmp_name'], $upload))
			{
				@unlink("uploads/documents/$oldImgfile");			
				$oldDocfile = $docfile;
			}	
		}
		//===============================================================
		$newStrategryData = "";
		$updQry = "INSERT INTO ".TBL_STRATEGIES." SET 
        sh_id='".addslashes($shid)."',
		strategy='".addslashes($strategy)."',
        responsibility='".addslashes($responsibility)."',
		key_milestones='".addslashes($key_milestones)."',
        related_documents='".addslashes($oldDocfile)."',
		date_added='".$toDay."' ";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        //echo $updQry;
        if($errNo===TRUE )
		{
			?>			
            <?php 
			$counter_str = 0;
			$selQuery_str = "SELECT * FROM  ".TBL_STRATEGIES." WHERE sh_id='".$shid."' ORDER BY id desc;";
			$strInfo = $con->getMultiRow($selQuery_str);
			$tot_mappingsInfo = count($strInfo);
			if($tot_mappingsInfo > 0){
				 for($ru=0;$ru<$tot_mappingsInfo;$ru++)
				 {
					$counter_str++;
			?>
			<tr>
			  <td><?php echo $counter_str;?>.</td>
			  <td id="strategy_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['strategy'])?$strInfo[$ru]['strategy']:'';?></td>
			  <td id="responsibility_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['responsibility'])?$strInfo[$ru]['responsibility']:'';?></td>
			  <td id="key_milestones_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['key_milestones'])?$strInfo[$ru]['key_milestones']:'';?></td>
			  <td id="related_documents_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><a href="uploads/documents/<?php echo isset($strInfo[$ru]['related_documents'])?$strInfo[$ru]['related_documents']:'';?>" target="_blank" title="Click to view or download"><?php echo isset($strInfo[$ru]['related_documents'])?$strInfo[$ru]['related_documents']:'';?></a></td>
			</tr>
			<?php }
			}
			//$related_documents = '<a href="uploads/documents/'.$oldDocfile.'" target="_blank" title="Click to view or download">'.$oldDocfile.'</a>';            
			//$return_arr[] = array("message" => 'ok',"strategy" => $strategy,"responsibility" => $responsibility,"key_milestones" => $key_milestones,"related_documents" => $related_documents);
		}
        else
		{
            //$return_arr[] = array("message" => 'err',"introduction" => '');
		}
    }
	elseif(isset($_POST['action']) && $_POST['action'] =='updateStrategies')
    { 		
		$shid = isset($_POST['sid'])?$_POST['sid']:'';
		$stid = isset($_POST['stid'])?$_POST['stid']:'';
		
		$strategy = isset($_POST['strategy'])?$_POST['strategy']:'';
		$responsibility = isset($_POST['responsibility'])?$_POST['responsibility']:'';
		$key_milestones = isset($_POST['key_milestones'])?$_POST['key_milestones']:'';
		
		//===============================================================
		//============== Uploading Files ===========================
		//===============================================================
		$oldDocfile = isset($_POST['old_sh_logo'])?$_POST['old_sh_logo']:'';
		
		$uploaddir = 'uploads/documents/';
		
		if(isset($_FILES['related_documents']['name']) && $_FILES['related_documents']['name'] != '')
		{			
			$docarr = explode(".", $_FILES['related_documents']['name']);						
			$docfile = $docarr[0]."_".$cDateTime.'.'.$docarr[1];
			$upload = $uploaddir . $docfile;
			if(move_uploaded_file($_FILES['related_documents']['tmp_name'], $upload))
			{
				@unlink("uploads/documents/$oldImgfile");			
				$oldDocfile = $docfile;
			}	
		}
		//===============================================================
		$updQry = "UPDATE ".TBL_STRATEGIES." SET 
        strategy='".addslashes($strategy)."',
        responsibility='".addslashes($responsibility)."',
		key_milestones='".addslashes($key_milestones)."',
        related_documents='".addslashes($oldDocfile)."',
		date_modified='".$toDay."' 		
		WHERE id = '".$stid."' LIMIT 1";		
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
        
        if($errNo===TRUE)
		{
			$related_documents = '<a href="uploads/documents/'.$oldDocfile.'" target="_blank" title="Click to view or download">'.$oldDocfile.'</a>';
            
			$return_arr[] = array("message" => 'ok',"strategy" => $strategy,"responsibility" => $responsibility,"key_milestones" => $key_milestones,"related_documents" => $related_documents);
		}
        else
		{
            $return_arr[] = array("message" => 'err',"introduction" => '');
		}
        
        echo json_encode($return_arr);
    }
	elseif(isset($_POST['action']) && $_POST['action'] =='deleteStrategy')
    { 		
		$stid = isset($_POST['stid'])?$_POST['stid']:'';
		$shid = isset($_POST['sid'])?$_POST['sid']:'';		
		//===============================================================		
		$delQuery = "DELETE FROM ".TBL_STRATEGIES." WHERE id='".$stid."' AND sh_id='".$shid."' LIMIT 1;";
		$errNo = $con->deleteRecords($delQuery);
			
        if($errNo===TRUE )
		{
			?>
			
            <?php 
			$counter_str = 0;
			$selQuery_str = "SELECT * FROM  ".TBL_STRATEGIES." WHERE sh_id='".$shid."' ORDER BY id desc;";
			$strInfo = $con->getMultiRow($selQuery_str);
			$tot_mappingsInfo = count($strInfo);
			if($tot_mappingsInfo > 0){
				 for($ru=0;$ru<$tot_mappingsInfo;$ru++)
				 {
					$counter_str++;
			?>
			<tr>
			  <td><?php echo $counter_str;?>.</td>
			  <td id="strategy_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['strategy'])?$strInfo[$ru]['strategy']:'';?></td>
			  <td id="responsibility_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['responsibility'])?$strInfo[$ru]['responsibility']:'';?></td>
			  <td id="key_milestones_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><?php echo isset($strInfo[$ru]['key_milestones'])?$strInfo[$ru]['key_milestones']:'';?></td>
			  <td id="related_documents_<?php echo $shInfo['id'];?>_<?php echo $strInfo[$ru]['id'];?>"><a href="uploads/documents/<?php echo isset($strInfo[$ru]['related_documents'])?$strInfo[$ru]['related_documents']:'';?>" target="_blank" title="Click to view or download"><?php echo isset($strInfo[$ru]['related_documents'])?$strInfo[$ru]['related_documents']:'';?></a></td>
			  <td style="text-align:center;"><i class="fa fa-edit popup" href="sh-update-strategies.php?sid=<?php echo $strInfo[$ru]['sh_id'];?>&stid=<?php echo $strInfo[$ru]['id'];?>" style=" cursor:pointer;" title="Update Strategy"></i>&nbsp;
                                <i class="fa fa-trash popup" data-title="Delete Strategy" href="del-strategy.php?sid=<?php echo $strInfo[$ru]['sh_id'];?>&stid=<?php echo $strInfo[$ru]['id'];?>" style=" cursor:pointer;" title="Delete Strategy"></i></td>
			</tr>
			<?php }
			}?> 
				<script>
					$( document ).ready(function() {   					                 
						
						$(".popup").colorbox({
							/*onComplete : function() { 
							   $(this).colorbox.resize(); 
							},*/ 
						  escKey: false,
						  overlayClose: false,
						  opacity:"0.2",
						  width:"60%",
						   /*height:"500",
						  title:"To close this dialog click X icon at the right"*/
						});
						
					});
                </script>                       
			<?php 
			//$related_documents = '<a href="uploads/documents/'.$oldDocfile.'" target="_blank" title="Click to view or download">'.$oldDocfile.'</a>';            
			//$return_arr[] = array("message" => 'ok',"strategy" => $strategy,"responsibility" => $responsibility,"key_milestones" => $key_milestones,"related_documents" => $related_documents);
		}
        else
		{
            //$return_arr[] = array("message" => 'err',"introduction" => '');
		}
    }
?>