<?php
include('../includes/con2pma3.php');
$var = "";
if(isset($_REQUEST['Check']))
{
	session_name("AdminSession");	
	
	if (isset($_SESSION['loggedin']) && ($_SESSION['loggedin']==true))
	{
		$id = 0;
		$var = "&Check=1";
	}
	else
	header("location: ../Admin/index.php?msg=YOU ARE NOT LOGGED IN");

}
else
{
	//include('../includes/checkSession.php');
	$id = $_SESSION['id'];
	$var = "";
}

$WID = $_REQUEST['WID'];
//include('../includes/con2db.php');
 $SCode =  "WBS";
 $PNode =  $_POST['WBSL'];
 $writeupwbs = $_POST['WBSN'];
 $description =$_POST['WBSD'];
 $creationDate =  date('Y-m-d');
 //$sector =  $_POST['sector'];
 if(isset($_REQUEST['del'])){
$WBSID = $_REQUEST['del'];
		 $val = mysql_query("SELECT WBSID, WBSIDA, WBSIDB, WBSIDC,WBSIDD, WBSIDE FROM globalwbs where WBSID ='$WBSID' && companyID = '$id'");
			 mysql_result($val,0,0);
			$WBSIDA = mysql_result($val,0,1);
	  		$WBSIDB = mysql_result($val,0,2);
	  		$WBSIDC = mysql_result($val,0,3);
	  		$WBSIDD = mysql_result($val,0,4);
	  		$WBSIDE = mysql_result($val,0,5);
			$WBSID = mysql_result($val,0,0);
		if($WBSIDB==0 && $WBSIDC==0 && $WBSIDD==0 && $WBSIDE==0)
		{ 

  				 mysql_query("delete from globalwbs WHERE gwbslist_id = '$WID' && WBSIDA = '$WBSIDA'   && companyID = '$id'");
				 
				 ////////////////////////////////////////////////////////////////////////////////////////				 
				 
$result = mysql_query("SELECT * FROM globalwbs where companyID = '$id' && WBSIDA >= '$WBSIDA' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");

   		while($row = mysql_fetch_array($result , MYSQL_ASSOC))
  		{
  		 	$WBSID = $row["WBSID"];
			$WBSIDA = $row["WBSIDA"];
			$WBSIDA--;
			mysql_query("UPDATE globalwbs SET WBSIDA = '$WBSIDA' WHERE `WBSID` = '$WBSID' && companyID='$id'");
		}		
		//////////////////////////////////////////////////////////////////////////////////////////////////////////
				//echo "dsf";
		}
		else if($WBSIDC==0 && $WBSIDD==0 && $WBSIDE==0)
		{
  				mysql_query("delete from globalwbs WHERE gwbslist_id = '$WID' && `WBSIDA` = '$WBSIDA' &&  WBSIDB = '$WBSIDB' && companyID = '$id'");				
				
				//////////////////////////////////////////////////////////////////////////////////////////////////////////////				
				$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA = '$WBSIDA' &&  WBSIDB >= '$WBSIDB' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");		

   		while($row = mysql_fetch_array($result , MYSQL_ASSOC))
  		{
  		 	$WBSID = $row["WBSID"];
			$WBSIDB = $row["WBSIDB"];
			$WBSIDB--;
			mysql_query("UPDATE globalwbs SET WBSIDB = '$WBSIDB' WHERE `WBSID` = '$WBSID' && companyID='$id'");
		}		
		/////////////////////////////////////////////////////////////////////////////////////////////////////
		}
		else if( $WBSIDD==0 && $WBSIDE==0)
		{

  				mysql_query("delete from globalwbs WHERE gwbslist_id = '$WID' && `WBSIDA` = '$WBSIDA'&&  WBSIDB = '$WBSIDB' &&  WBSIDC = '$WBSIDC' && companyID = '$id'");			
				
								//////////////////////////////////////////////////////////////////////////////////////////////////////////////				
				
				$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA = '$WBSIDA' &&  WBSIDB = '$WBSIDB' &&  WBSIDC >= '$WBSIDC' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");		

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

  		 	$WBSID = $row["WBSID"];
			$WBSIDC = $row["WBSIDC"];
			$WBSIDC--;
			mysql_query("UPDATE globalwbs SET WBSIDC = '$WBSIDC' WHERE `WBSID` = '$WBSID' && companyID='$id'");
		}
		
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////		

		}
		else if( $WBSIDE==0)
		{
  				mysql_query("delete from globalwbs WHERE gwbslist_id = '$WID' && `WBSIDA` = '$WBSIDA'&&  WBSIDB = '$WBSIDB' &&  WBSIDC = '$WBSIDC' &&  WBSIDD = '$WBSIDD' && companyID = '$id'");
			
								//////////////////////////////////////////////////////////////////////////////////////////////////////////////
				
				
				$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA = '$WBSIDA' &&  WBSIDB = '$WBSIDB' &&  WBSIDC = '$WBSIDC' &&  WBSIDD >= '$WBSIDD' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");

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

  		{
  		 	$WBSID = $row["WBSID"];
			$WBSIDD = $row["WBSIDD"];
			$WBSIDD--;
			mysql_query("UPDATE globalwbs SET WBSIDD = '$WBSIDD' WHERE `WBSID` = '$WBSID' && companyID='$id'");

		}		
		/////////////////////////////////////////////////////////////////////////////////////////////////////

		}

		else
		{
		mysql_query("delete from globalwbs WHERE gwbslist_id = '$WID' && `WBSIDA` = '$WBSIDA'&&  WBSIDB = '$WBSIDB' &&  WBSIDC = '$WBSIDC' &&  WBSIDD = '$WBSIDD' &&  WBSIDE = '$WBSIDE' && companyID = '$id'");
		
		
								//////////////////////////////////////////////////////////////////////////////////////////////////////////////				
				
				$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA = '$WBSIDA' &&  WBSIDB = '$WBSIDB' &&  WBSIDC = '$WBSIDC' &&  WBSIDD >= '$WBSIDD' &&  WBSIDE >= '$WBSIDE' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");

		

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

  		{

  		 	$WBSID = $row["WBSID"];

			$WBSIDE = $row["WBSIDE"];

			$WBSIDE--;

			

			mysql_query("UPDATE globalwbs SET WBSIDE = '$WBSIDE' WHERE `WBSID` = '$WBSID' && companyID='$id'");

		}		
		/////////////////////////////////////////////////////////////////////////////////////////////////////		
		}
		header("location: WBS2.php?msg=INFORMATION IS deleted from wbs&WID=$WID".$var);

}
else if(isset($_REQUEST['AddBefore'])){
$WBSID1 = $PNode;
		 $val = mysql_query("SELECT WBSID, WBSIDA, WBSIDB, WBSIDC,WBSIDD, WBSIDE FROM globalwbs where WBSID ='$WBSID1' && companyID = '$id'");

			mysql_result($val,0,0);
			$WBSIDA = mysql_result($val,0,1);
	  		$WBSIDB = mysql_result($val,0,2);
	  		$WBSIDC = mysql_result($val,0,3);
	  		$WBSIDD = mysql_result($val,0,4);
	  		$WBSIDE = mysql_result($val,0,5);
			$WBSID = mysql_result($val,0,0);
	if($WBSIDB==0 && $WBSIDC==0 && $WBSIDD==0 && $WBSIDE==0)
		{		
$val = mysql_query("SELECT WBSIDA FROM globalwbs where WBSID = '$WBSID1' && gwbslist_id = '$WID' && companyID = '$id'");
 $WBSIDA1 = mysql_result($val,0,0);
$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA >= '$WBSIDA1' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");
while($row = mysql_fetch_array($result , MYSQL_ASSOC))
  		{
  		 	$WBSID = $row["WBSID"];
			$WBSIDA = $row["WBSIDA"];
			$WBSIDA++;
			mysql_query("UPDATE globalwbs SET WBSIDA = '$WBSIDA' WHERE `WBSID` = '$WBSID' && companyID='$id'");
		 $c1++;
		}
		if(mysql_query("insert into globalwbs values('','WBS','NULL','$WBSIDA1','0','0','0','0','0','$description','$writeupwbs','$creationDate','$WID','$id')"))
						
						{
						header("location: WBS2.php?msg=ADDED BEFORE&WID=$WID".$var);
						}
						
						else
						
						{
							header("location: WBS2.php?msg=NOT ADDED BEFORE&WID=$WID".$var);
						
						}

		}
else 	if($WBSIDC==0 && $WBSIDD==0 && $WBSIDE==0)
		{		
$val = mysql_query("SELECT WBSIDA, WBSIDB FROM globalwbs where WBSID = '$WBSID1' && gwbslist_id = '$WID' && companyID = '$id'");
 $WBSIDA1 = mysql_result($val,0,0);
 $WBSIDB1 = mysql_result($val,0,1);
$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA = '$WBSIDA1' && WBSIDB >= '$WBSIDB1' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");		

   		while($row = mysql_fetch_array($result , MYSQL_ASSOC))
  		{
  		 	$WBSID = $row["WBSID"];
			$WBSIDB = $row["WBSIDB"];
			$WBSIDB++;
			mysql_query("UPDATE globalwbs SET WBSIDB = '$WBSIDB' WHERE `WBSID` = '$WBSID' && companyID='$id'");
		}
						if(mysql_query("insert into globalwbs values('','WBS','NULL','$WBSIDA1','$WBSIDB1','0','0','0','0','$description','$creationDate','$writeupwbs','$WID','$id')"))
						
						{
							
							header("location: WBS2.php?msg= ADDED BEFORE&WID=$WID".$var);
						}
						
						else
						
						{
							header("location: WBS2.php?msg=NOT ADDED BEFORE&WID=$WID".$var);
						
						}

		}
		
else 	if($WBSIDD==0 && $WBSIDE==0)

		{ 
		
$val = mysql_query("SELECT WBSIDA, WBSIDB, WBSIDC FROM globalwbs where WBSID = '$WBSID1' && gwbslist_id = '$WID' && companyID = '$id'");

 $WBSIDA1 = mysql_result($val,0,0);
 $WBSIDB1 = mysql_result($val,0,1);
 $WBSIDC1 = mysql_result($val,0,2);

$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA = '$WBSIDA1' && WBSIDB = '$WBSIDB1' && WBSIDC >= '$WBSIDC1' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");		

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

  		{
  		 	$WBSID = $row["WBSID"];
			$WBSIDC = $row["WBSIDC"];
			$WBSIDC++;
			mysql_query("UPDATE globalwbs SET WBSIDC = '$WBSIDC' WHERE `WBSID` = '$WBSID' && companyID='$id'");

		}
						if(mysql_query("insert into globalwbs values('','WBS','NULL','$WBSIDA1','$WBSIDB1','$WBSIDC1','0','0','0','$description','$writeupwbs','$creationDate','$WID','$id')"))
						
						{
							
							header("location: WBS2.php?msg= ADDED BEFORE&WID=$WID".$var);
						}
						
						else
						
						{
							header("location: WBS2.php?msg=NOT ADDED BEFORE&WID=$WID".$var);						
						}
		}
		else 	if($WBSIDE==0)
		{		
$val = mysql_query("SELECT WBSIDA, WBSIDB, WBSIDC, WBSIDD FROM globalwbs where WBSID = '$WBSID1' && gwbslist_id = '$WID' && companyID = '$id'");
 $WBSIDA1 = mysql_result($val,0,0);
 $WBSIDB1 = mysql_result($val,0,1);
 $WBSIDC1 = mysql_result($val,0,2);
 $WBSIDD1 = mysql_result($val,0,3);	
$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA = '$WBSIDA1' && WBSIDB = '$WBSIDB1' && WBSIDC = '$WBSIDC1' && WBSIDD >= '$WBSIDD1' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");
   		while($row = mysql_fetch_array($result , MYSQL_ASSOC))
  		{
  		 	$WBSID = $row["WBSID"];
			$WBSIDD = $row["WBSIDD"];
			$WBSIDD++;
			mysql_query("UPDATE globalwbs SET WBSIDD = '$WBSIDD' WHERE `WBSID` = '$WBSID' && companyID='$id'");

		}

						if(mysql_query("insert into globalwbs values('','WBS','NULL','$WBSIDA1','$WBSIDB1','$WBSIDC1','$WBSIDD1','0','0','$description','$writeupwbs','$creationDate','$WID','$id')"))
						
						{
							
							header("location: WBS2.php?msg= ADDED BEFORE&WID=$WID".$var);
						}
						
						else
						
						{
							header("location: WBS2.php?msg=NOT ADDED BEFORE&WID=$WID".$var);
						
						}

		}		
				else { 
		
$val = mysql_query("SELECT WBSIDA, WBSIDB, WBSIDC, WBSIDD, WBSIDE FROM globalwbs where WBSID = '$WBSID1' && gwbslist_id = '$WID' && companyID = '$id'");

 $WBSIDA1 = mysql_result($val,0,0);
 $WBSIDB1 = mysql_result($val,0,1);
 $WBSIDC1 = mysql_result($val,0,2);
 $WBSIDD1 = mysql_result($val,0,3);
 $WBSIDE1 = mysql_result($val,0,4);

$result = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' && companyID = '$id' && WBSIDA = '$WBSIDA1' && WBSIDB = '$WBSIDB1' && WBSIDC = '$WBSIDC1' && WBSIDD = '$WBSIDD1' && WBSIDE >= '$WBSIDE1' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");	

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

  		{
  		 	$WBSID = $row["WBSID"];
			$WBSIDE = $row["WBSIDE"];
			$WBSIDE++;
			mysql_query("UPDATE globalwbs SET WBSIDE = '$WBSIDE' WHERE `WBSID` = '$WBSID' && companyID='$id'");

		}

						if(mysql_query("insert into globalwbs values('','WBS','NULL','$WBSIDA1','$WBSIDB1','$WBSIDC1','$WBSIDD1','$WBSIDE1','0','$description','$writeupwbs','$creationDate','$WID','$id')"))
						
						{
							
							header("location: WBS2.php?msg= ADDED BEFORE&WID=$WID".$var);
						}
						
						else
						
						{
							header("location: WBS2.php?msg=NOT ADDED BEFORE&WID=$WID".$var);
						
						}
		}
}
else if(isset($_REQUEST['Edit'])){
$WBSID = $_REQUEST['Edit'];
if(mysql_query("UPDATE globalwbs SET writeupwbs = '$writeupwbs' , `creationDate` = '$creationDate', description = '$description' WHERE `WBSID` = '$WBSID' && companyID='$id'"))
{
header("location: WBS2.php?msg= INFORMATION IS Updated&WID=$WID".$var);

}
else
{
	header("location: WBS2.php?msg= INFORMATION IS  NOT Updated&WID=$WID".$var);

	}
}

else{
  	  $WBSIDA = 0;
	  $WBSIDB = 0;
	  $WBSIDC = 0;
	  $WBSIDD = 0;
	  $WBSIDE = 0;
		if ($PNode=="0.0.0.0.0")
		{

			$val = mysql_query("select MAX(WBSIDA) from globalwbs where gwbslist_id='$WID' && companyID='$id'");
			$WBSIDA = mysql_result($val,0,0);
			$WBSIDA++;
			$SCode =  "MWBS";

		}
		else
		{
			$val = mysql_query("select WBSIDA,WBSIDB, WBSIDC, WBSIDD, WBSIDE from globalwbs where WBSID = '$PNode' && companyID='$id'");			
			$SCode =  "WBS";
			$WBSIDA = mysql_result($val,0,'WBSIDA');
			$WBSIDB = mysql_result($val,0,'WBSIDB');
			$WBSIDC = mysql_result($val,0,'WBSIDC');
			$WBSIDD = mysql_result($val,0,'WBSIDD');
			$WBSIDE = mysql_result($val,0,'WBSIDE');

			if($WBSIDB==0)

			{
				$val = mysql_query("select MAX(WBSIDB) from globalwbs where WBSIDA = '$WBSIDA' && gwbslist_id='$WID' && companyID='$id'");
				$WBSIDB = mysql_result($val,0,0);
				$WBSIDB++;

//				echo "B. ".$WBSIDB;

				}
				else if($WBSIDC==0)
				{

					$val = mysql_query("select MAX(WBSIDC) from globalwbs where WBSIDA = '$WBSIDA' && WBSIDB = '$WBSIDB' && gwbslist_id='$WID' && companyID='$id'");
					$WBSIDC = mysql_result($val,0,0);
					$WBSIDC++;

	//				echo "C. ".$WBSIDC;

					}
					else if($WBSIDD==0)
					{
						$val = mysql_query("select MAX(WBSIDD) from globalwbs where WBSIDA = '$WBSIDA' && WBSIDB = '$WBSIDB' && WBSIDC = '$WBSIDC' && gwbslist_id='$WID' && companyID='$id'");
						$WBSIDD = mysql_result($val,0,0);
						$WBSIDD++;

		//				echo "D. ".$WBSIDD;

						}
						else if($WBSIDE==0)
					{

						$val = mysql_query("select MAX(WBSIDE) from globalwbs where WBSIDA = '$WBSIDA' && WBSIDB = '$WBSIDB' && WBSIDC = '$WBSIDC' && WBSIDD = '$WBSIDD' && gwbslist_id='$WID' && companyID='$id'");
						$WBSIDE = mysql_result($val,0,0);
						$WBSIDE++;

			//			echo "E. ".$WBSIDE;

						}

		//	echo "..".$WBSIDA;
			}
if(mysql_query("insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$PNode','$description','$writeupwbs','$creationDate','$WID','$id')"))

{
	header("location: WBS2.php?WID=$WID".$var);
}
else
{
header("location: WBS2.php?WID=$WID".$var);
}
}
?> 