<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');
/*ini_set("upload_max_filesize", "50M");
ini_set("post_max_size", "51M");
ini_set("max_execution_time", "200");
ini_set("max_input_time", "200");*/
error_reporting(0);

//die(var_dump($_POST));
if ( !empty($_POST) ) {

    $companyID = $_POST['companyID'];

    function WBSChildren($WBS, $field, $value)
    {
        $foundLocs = array();

        foreach($WBS as $key => $wbs)
        {
            if ( $wbs[$field] === $value )
                $foundLocs[] = $key;
        }
        return $foundLocs;
    }

    function ITEMSChildren( $ITEMS, $field, $value )
    {
        $foundLocs = array();

        foreach($ITEMS as $key => $item)
        {
            if ( $item[$field] === $value )
                $foundLocs[] = $key;
        }
        return $foundLocs;
    }

// WID => WBS Instance ID,  PNode => WBS Parent Node ID
    function codeBuilder($WID, $PNode){
        $companyID = -1;//$_POST['companyID'];
        //  $companyID = 28;
        $WBSIDA = 0;
        $WBSIDB = 0;
        $WBSIDC = 0;
        $WBSIDD = 0;
        $WBSIDE = 0;
        $SCode = null;

        if ($PNode == 0)
        {

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

        }
        else
        {
            //  echo "select WBSIDA,WBSIDB, WBSIDC, WBSIDD, WBSIDE from globalwbs where WBSID = '$PNode' && companyID='$companyID'";
            $val = mysql_query("select WBSIDA,WBSIDB, WBSIDC, WBSIDD, WBSIDE from globalwbs where WBSID = '$PNode' && companyID='$companyID'");
            $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='$companyID'");
                $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='$companyID'");
                $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='$companyID'");
                $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='$companyID'");
                $WBSIDE = mysql_result($val,0,0);
                $WBSIDE++;

                //			echo "E. ".$WBSIDE;

            }

            //	echo "..".$WBSIDA;
        }

        $code = array(
            'WBSIDA' => $WBSIDA,
            'WBSIDB' => $WBSIDB,
            'WBSIDC' => $WBSIDC,
            'WBSIDD' => $WBSIDD,
            'WBSIDE' => $WBSIDE,
            'sortCode' => $SCode
        );

        return $code;
    }



//$myfile = fopen( "E:/Work/pmanywhere/import wbs/BI-25-00026.xer", 'r');
//$filecontents = file_get_contents("E:/Work/pmanywhere/import wbs/more/Additional Scope PCS Rev. 03.xer")
    //  or die("Unable to open file");
    $WID = null;
    $fileName = null;


    $WID = $_POST['WID'];
    $target_dir = "uploads/";
    $fileName = basename($_FILES["fileToUpload"]["name"]);

    $target_file = $target_dir . $fileName;

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {


        $filecontents = file_get_contents($target_file);
//$filecontents = file_get_contents("E:/Work/pmanywhere/import wbs/test.xer");
//$filecontents = file_get_contents("E:/Work/pmanywhere/import wbs/BI-25-00026.xer");

        $lines = preg_split('/PROJWBS/', $filecontents);

        $data = end($lines);

        $projWBS = preg_split('/%T/', $data);

        $projWBS = $projWBS[0];

        $lines = preg_split('/[\n]+/', $projWBS, -1, PREG_SPLIT_NO_EMPTY);

//echo '---------------------------------------------------------------------------------------------------<br><br>WBS<br><br>';

        $WBSIDIndex = null;
        $projectIDIndex = null;
        $WBSNameIndex = null;
        $WBSParentIDIndex = null;
        $WBS = array();
//Build array of WBS
        foreach ($lines as $line) {
// table fields in $record array [ 1 => WBSID, 2 => ProjectID, 10 => WBSName, 11 => ParentWBSID ]
            $fields = preg_split('/[\t]/', $line, -1);


            if ($fields[0] == '%F') {
                $WBSIDIndex = array_search('wbs_id', $fields);
                $projectIDIndex = array_search('proj_id', $fields);
                $WBSNameIndex = array_search('wbs_name', $fields);
                $WBSParentIDIndex = array_search('parent_wbs_id', $fields);
                //print_r($fields);
            }

            if ($fields[0] == '%R') {
                $WBS[] = array('WBSID' => $fields[$WBSIDIndex],
                    'projectID' => $fields[$projectIDIndex],
                    'WBSName' => $fields[$WBSNameIndex],
                    'WBSParent' => $fields[$WBSParentIDIndex]);


            }


        }/*
// Show WBS on Screen
foreach( $WBS as $wbs ){
       echo 'WBS ID : '.$wbs[ 'WBSID' ];
        echo '<br>Project ID :'.$wbs[ 'projectID' ];
        echo '<br>WBS Name :'.$wbs[ 'WBSName' ];
        echo '<br>Parent WBS ID :'.$wbs[ 'WBSParent' ].'<br><br>';
}
*/
//echo '---------------------------------------------------------------------------------------------------<br><br>ITEMS<br><br>';
        $ITEMS = array();
        $itemLines = preg_split('/TASK/', $filecontents);

        $itemData = $itemLines[1];

        $projItems = preg_split('/%T/', $itemData);

        $projItems = $projItems[0];


        $itemLines = preg_split('/[\n]+/', $projItems, -1, PREG_SPLIT_NO_EMPTY);
        $itemIDIndex = null;
        $itemProjectIDIndex = null;
        $itemWBSIDIndex = null;
        $itemNameIndex = null;

//Build array of ITEMS
        foreach ($itemLines as $line) {
// table fields in $record array [ 1 => itemID, 2 => ProjectID, 3 => WBSID, 16 => itemName ]
            //  $itemFields = preg_split('/[\t]+/', $line, -1, PREG_SPLIT_NO_EMPTY);
            $itemFields = preg_split('/[\t]/', $line, -1);


            if ($itemFields[0] == '%F') {
                $itemIDIndex = array_search('task_id', $itemFields);
                $itemProjectIDIndex = array_search('proj_id', $itemFields);
                $itemWBSIDIndex = array_search('wbs_id', $itemFields);
                $itemNameIndex = array_search('task_name', $itemFields);
            }


            if ($itemFields[0] == '%R') {
                $ITEMS[] = array('itemID' => $itemFields[$itemIDIndex],
                    'projectID' => $itemFields[$itemProjectIDIndex],
                    'itemWBSID' => $itemFields[$itemWBSIDIndex],
                    'itemName' => $itemFields[$itemNameIndex]);


            }
        }

//Display ITEMS on screen
        /*foreach( $ITEMS as $item ){

            echo 'Item ID : '.$item[ 'itemID' ];
            echo '<br>Project ID :'.$item[ 'projectID' ];
            echo '<br>WBS ID :'.$item[ 'itemWBSID' ];
            echo '<br>Item Name :'.$item[ 'itemName' ].'<br><br>';
        }
        */
        $creationDate = date('Y-m-d');
// Traversing WBS one by one


        /*if(mysql_query("insert into wbslist set `WBSList_for` = '1', `WBSList_code` = 'AdeelTest', WBSList_title = 'test', `WBSList_type` = '2',
                                             `WBSList_file_name` = '', `WBSList_remarks` = 'None', `WBSList_datecreation` = '2017/01/01',
                                              `WBSList_companyid` = '28', `WBSList_status` = '1'"))
            $WID = mysql_insert_id();
        $companyID = 28;

        */

        $firstWBS = true;

        $exceededLevels = false;

        foreach ($WBS as $key => &$wbs) {

// ---------------------------------------------LEVEL 1 ------------------------------------------------------
            //MWBS
            $WBSIDA = null;
            $WBSIDB = null;
            $WBSIDC = null;
            $WBSIDD = null;
            $WBSIDE = null;
            $sortCode = null;
            $PNode = null;
            $PNode1 = null;

            $WBSName = $wbs['WBSName'];
            if ($firstWBS || $wbs['WBSParent'] == 0) {
                $PNode = 0;
                $firstWBS = false;
            } else {
                $exceededLevels = true;
                continue;
            }


            /* echo '<br><br>Key : '.$key;
             echo '<br>WBS ID : '.$wbs[ 'WBSID' ];
             echo '<br>Project ID :'.$wbs[ 'projectID' ];
             echo '<br>WBS Name :'.$wbs[ 'WBSName' ];
             echo '<br>Parent WBS ID :'.$wbs[ 'WBSParent' ].'<br><br>';*/


            // Getting sortCode and Abbreviations A,B,C,D,E
            $abbrevCode = codeBuilder($WID, $PNode);

            $WBSIDA = $abbrevCode['WBSIDA'];
            $WBSIDB = $abbrevCode['WBSIDB'];
            $WBSIDC = $abbrevCode['WBSIDC'];
            $WBSIDD = $abbrevCode['WBSIDD'];
            $WBSIDE = $abbrevCode['WBSIDE'];
            $sortCode = $abbrevCode['sortCode'];

            $sql = "INSERT INTO globalwbs VALUES('','$sortCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$PNode','$WBSName','',
                                                        '$creationDate','$WID','-1')";

            mysql_query($sql);
            $PNode1 = mysql_insert_id();
// ---------------------------------------------End LEVEL 1 ------------------------------------------------------
//WBS
            // Getting Children IDs of current WBS in WBS and ITEMS
            $WBSChildrenIndex = WBSChildren($WBS, 'WBSParent', $wbs['WBSID']);
            $ITEMSChildrenIndex = ITEMSChildren($ITEMS, 'itemWBSID', $wbs['WBSID']);
// Adding items
            if (count($ITEMSChildrenIndex) > 0) {
                foreach ($ITEMSChildrenIndex as $itemIndex) {
                    $itemName = $ITEMS[$itemIndex]['itemName'];
                    $itemParent = $PNode1;
                    if (mysql_query("INSERT INTO globalitems VALUES('NULL','ITEM','$itemParent','$itemName','','0','$creationDate','0','$companyID')"))
                        //  echo '<br><br>'.$itemName.' added under WBS  : '.$wbs['WBSName'].'<br><br>';
                        unset($ITEMS[$itemIndex]);

                }
            }
            unset($WBS[$key]);
            // echo 'Deleted Key : '.$key."<br><br>";

// ---------------------------------------------LEVEL 2 ------------------------------------------------------
            if (count($WBSChildrenIndex) > 0) {
//echo '<br><br>------------------Level 2---------------<br><br>';
                foreach ($WBSChildrenIndex as $childIndex) {

                    $WBSName = $WBS[$childIndex]['WBSName'];
                    $PNode = $WBS[$childIndex]['WBSParent'];
                    $PNode2 = null;

                    /*    echo '<br><br>WBS ID : '.$WBS[$childIndex][ 'WBSID' ];
                        echo '<br>Project ID :'.$WBS[$childIndex][ 'projectID' ];
                        echo '<br>WBS Name :'.$WBS[$childIndex][ 'WBSName' ];
                        echo '<br>Parent WBS ID :'.$WBS[$childIndex][ 'WBSParent' ].'<br><br>';*/

                    $sortCode = null;
                    $WBSIDA = null;
                    $WBSIDB = null;
                    $WBSIDC = null;
                    $WBSIDD = null;
                    $WBSIDE = null;

                    // Getting sortCode and Abbreviations A,B,C,D,E
                    $abbrevCode = codeBuilder($WID, $PNode1);

                    $WBSIDA = $abbrevCode['WBSIDA'];
                    $WBSIDB = $abbrevCode['WBSIDB'];
                    $WBSIDC = $abbrevCode['WBSIDC'];
                    $WBSIDD = $abbrevCode['WBSIDD'];
                    $WBSIDE = $abbrevCode['WBSIDE'];
                    $sortCode = $abbrevCode['sortCode'];

                    $sql2 = "INSERT INTO globalwbs VALUES('','$sortCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$PNode1','$WBSName','',
                                                        '$creationDate','$WID','-1')";
                    mysql_query($sql2);
                    $PNode2 = mysql_insert_id();

                    // Getting Children IDs of Level 2 WBS in WBS and ITEMS
                    $WBSChildrenIndex2 = WBSChildren($WBS, 'WBSParent', $WBS[$childIndex]['WBSID']);
                    $ITEMSChildrenIndex2 = ITEMSChildren($ITEMS, 'itemWBSID', $WBS[$childIndex]['WBSID']);
// Adding items
                    if (count($ITEMSChildrenIndex2) > 0) {
                        foreach ($ITEMSChildrenIndex2 as $itemIndex2) {
                            $itemName2 = $ITEMS[$itemIndex2]['itemName'];
                            $itemParent2 = $PNode2;
                            if (mysql_query("INSERT INTO globalitems VALUES('NULL','ITEM','$itemParent2','$itemName2','','0','$creationDate','0','$companyID')"))
                                //echo '<br><br>'.$itemName2.' added under WBS  : '.$WBS[$childIndex]['WBSName'].'<br><br>';
                                unset($ITEMS[$itemIndex2]);

                        }
                    }


                    // Remove Child from WBS array after adding in database
                    unset($WBS[$childIndex]);
                    // echo 'Deleted Key : '.$childIndex."<br><br>";
// ---------------------------------------------LEVEL 3 ------------------------------------------------------
                    if (count($WBSChildrenIndex2) > 0) {
                        //   echo '<br><br>------------------Level 3---------------<br><br>';
                        foreach ($WBSChildrenIndex2 as $childIndex2) {

                            $WBSName = $WBS[$childIndex2]['WBSName'];
                            $PNode = $WBS[$childIndex2]['WBSParent'];
                            $PNode3 = null;
                            /*
                                                echo '<br><br>WBS ID : '.$WBS[$childIndex2][ 'WBSID' ];
                                                echo '<br>Project ID :'.$WBS[$childIndex2][ 'projectID' ];
                                                echo '<br>WBS Name :'.$WBS[$childIndex2][ 'WBSName' ];
                                                echo '<br>Parent WBS ID :'.$WBS[$childIndex2][ 'WBSParent' ].'<br><br>';*/

                            $sortCode = null;
                            $WBSIDA = null;
                            $WBSIDB = null;
                            $WBSIDC = null;
                            $WBSIDD = null;
                            $WBSIDE = null;

                            // Getting sortCode and Abbreviations A,B,C,D,E
                            $abbrevCode = codeBuilder($WID, $PNode2);

                            $WBSIDA = $abbrevCode['WBSIDA'];
                            $WBSIDB = $abbrevCode['WBSIDB'];
                            $WBSIDC = $abbrevCode['WBSIDC'];
                            $WBSIDD = $abbrevCode['WBSIDD'];
                            $WBSIDE = $abbrevCode['WBSIDE'];
                            $sortCode = $abbrevCode['sortCode'];

                            $sql3 = "INSERT INTO globalwbs VALUES('','$sortCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$PNode2','$WBSName','',
                                                        '$creationDate','$WID','-1')";
                            mysql_query($sql3);
                            $PNode3 = mysql_insert_id();


                            // Getting Children IDs of Level 3 WBS in WBS and ITEMS
                            $WBSChildrenIndex3 = WBSChildren($WBS, 'WBSParent', $WBS[$childIndex2]['WBSID']);
                            $ITEMSChildrenIndex3 = ITEMSChildren($ITEMS, 'itemWBSID', $WBS[$childIndex2]['WBSID']);

                            // Adding items
                            if (count($ITEMSChildrenIndex3) > 0) {
                                foreach ($ITEMSChildrenIndex3 as $itemIndex3) {
                                    $itemName3 = $ITEMS[$itemIndex3]['itemName'];
                                    $itemParent3 = $PNode3;
                                    if (mysql_query("INSERT INTO globalitems VALUES('NULL','ITEM','$itemParent3','$itemName3','','0','$creationDate','0','$companyID')"))
                                        //  echo '<br><br>'.$itemName3.' added under WBS  : '.$WBS[$childIndex2]['WBSName'].'<br><br>';
                                        unset($ITEMS[$itemIndex3]);

                                }
                            }

                            // Remove Child from WBS array after adding in database
                            unset($WBS[$childIndex2]);
                            //  echo 'Deleted Key : '.$childIndex2."<br><br>";
// ---------------------------------------------LEVEL 4 ------------------------------------------------------
                            if (count($WBSChildrenIndex3) > 0) {
                                //     echo '<br><br>------------------Level 4---------------<br><br>';
                                foreach ($WBSChildrenIndex3 as $childIndex3) {

                                    $WBSName = $WBS[$childIndex3]['WBSName'];
                                    $PNode = $WBS[$childIndex3]['WBSParent'];
                                    $PNode4 = null;

                                    /* echo '<br><br>WBS ID : '.$WBS[$childIndex3][ 'WBSID' ];
                                     echo '<br>Project ID :'.$WBS[$childIndex3][ 'projectID' ];
                                     echo '<br>WBS Name :'.$WBS[$childIndex3][ 'WBSName' ];
                                     echo '<br>Parent WBS ID :'.$WBS[$childIndex3][ 'WBSParent' ].'<br><br>';*/

                                    $sortCode = null;
                                    $WBSIDA = null;
                                    $WBSIDB = null;
                                    $WBSIDC = null;
                                    $WBSIDD = null;
                                    $WBSIDE = null;

                                    // Getting sortCode and Abbreviations A,B,C,D,E
                                    $abbrevCode = codeBuilder($WID, $PNode3);

                                    $WBSIDA = $abbrevCode['WBSIDA'];
                                    $WBSIDB = $abbrevCode['WBSIDB'];
                                    $WBSIDC = $abbrevCode['WBSIDC'];
                                    $WBSIDD = $abbrevCode['WBSIDD'];
                                    $WBSIDE = $abbrevCode['WBSIDE'];
                                    $sortCode = $abbrevCode['sortCode'];

                                    $sql4 = "INSERT INTO globalwbs VALUES('','$sortCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$PNode3','$WBSName','',
                                                        '$creationDate','$WID','-1')";
                                    mysql_query($sql4);
                                    $PNode4 = mysql_insert_id();

                                    // Getting Children IDs of Level 4 WBS in WBS and ITEMS
                                    $WBSChildrenIndex4 = WBSChildren($WBS, 'WBSParent', $WBS[$childIndex3]['WBSID']);
                                    $ITEMSChildrenIndex4 = ITEMSChildren($ITEMS, 'itemWBSID', $WBS[$childIndex3]['WBSID']);

                                    // Adding items
                                    if (count($ITEMSChildrenIndex4) > 0) {
                                        foreach ($ITEMSChildrenIndex4 as $itemIndex4) {
                                            $itemName4 = $ITEMS[$itemIndex4]['itemName'];
                                            $itemParent4 = $PNode4;
                                            if (mysql_query("INSERT INTO globalitems VALUES('NULL','ITEM','$itemParent4','$itemName4','','0','$creationDate','0','$companyID')"))
                                                // echo '<br><br>'.$itemName4.' added under WBS  : '.$WBS[$childIndex3]['WBSName'].'<br><br>';
                                                unset($ITEMS[$itemIndex4]);

                                        }
                                    }

                                    // Remove Child from WBS array after adding in database
                                    unset($WBS[$childIndex3]);
                                    // echo 'Deleted Key : '.$childIndex3."<br><br>";
// ---------------------------------------------LEVEL 5 ------------------------------------------------------
                                    if (count($WBSChildrenIndex4) > 0) {

                                        // echo '<br><br>------------------Level 5---------------<br><br>';
                                        foreach ($WBSChildrenIndex4 as $childIndex4) {

                                            $WBSName = $WBS[$childIndex4]['WBSName'];
                                            $PNode = $WBS[$childIndex4]['WBSParent'];

                                            /*     echo '<br><br>WBS ID : '.$WBS[$childIndex4][ 'WBSID' ];
                                                 echo '<br>Project ID :'.$WBS[$childIndex4][ 'projectID' ];
                                                 echo '<br>WBS Name :'.$WBS[$childIndex4][ 'WBSName' ];
                                                 echo '<br>Parent WBS ID :'.$WBS[$childIndex4][ 'WBSParent' ].'<br><br>';*/

                                            $sortCode = null;
                                            $WBSIDA = null;
                                            $WBSIDB = null;
                                            $WBSIDC = null;
                                            $WBSIDD = null;
                                            $WBSIDE = null;

                                            // Getting sortCode and Abbreviations A,B,C,D,E
                                            $abbrevCode = codeBuilder($WID, $PNode4);

                                            $WBSIDA = $abbrevCode['WBSIDA'];
                                            $WBSIDB = $abbrevCode['WBSIDB'];
                                            $WBSIDC = $abbrevCode['WBSIDC'];
                                            $WBSIDD = $abbrevCode['WBSIDD'];
                                            $WBSIDE = $abbrevCode['WBSIDE'];
                                            $sortCode = $abbrevCode['sortCode'];

                                            $sql5 = "INSERT INTO globalwbs VALUES('','$sortCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$PNode4','$WBSName','',
                                                        '$creationDate','$WID','-1')";

                                            mysql_query($sql5);
                                            $PNode5 = mysql_insert_id();

                                            // checking items
                                            $ITEMSChildrenIndex5 = ITEMSChildren($ITEMS, 'itemWBSID', $WBS[$childIndex4]['WBSID']);

                                            // Adding items
                                            if (count($ITEMSChildrenIndex5) > 0) {
                                                foreach ($ITEMSChildrenIndex5 as $itemIndex5) {
                                                    $itemName5 = $ITEMS[$itemIndex5]['itemName'];
                                                    $itemParent5 = $PNode5;
                                                    if (mysql_query("INSERT INTO globalitems VALUES('NULL','ITEM','$itemParent5','$itemName5','','0','$creationDate','0','$companyID')"))
                                                        //  echo '<br><br>'.$itemName5.' added under WBS  : '.$WBS[$childIndex4]['WBSName'].'<br><br>';
                                                        unset($ITEMS[$itemIndex5]);

                                                }
                                            }


                                            // Remove Child from WBS array after adding in database
                                            unset($WBS[$childIndex4]);
                                            // echo 'Deleted Key : '.$childIndex4."<br><br>";
                                        }


                                    }
// ---------------------------------------------End LEVEL 5 ------------------------------------------------------
                                }


                            }
// ---------------------------------------------End LEVEL 4 ------------------------------------------------------
                        }


                    }
// ---------------------------------------------End LEVEL 3 ------------------------------------------------------
                }


            }
// ---------------------------------------------End LEVEL 2 ------------------------------------------------------
//echo '<br><br>**************************************************<br><br>';
        }

        if( $exceededLevels)
            header('location: 03-04Scope-01WBS01-IV01.php?WID='.$WID.'&fileName='.$fileName.'&msg=2');
        else
            header('location: 03-04Scope-01WBS01-IV01.php?WID='.$WID.'&fileName='.$fileName.'&msg=1');
    }else
        header('location: 03-04Scope-01WBS01-IV01.php?WID='.$WID.'&fileName='.$fileName.'&msg=3');
}else{
    header('location: 03-04Scope-01WBS01-IV01.php?WID='.$WID.'&fileName='.$fileName.'&msg=4');

}?>