<?php

if($_REQUEST['mode']!='update'){
include_once("../includes/site_config.php");

include('../session_handler.inc');
$Sess_UserType = $_SESSION['Sess_UserType'];

if ($Sess_UserType != "A") {
	exit;
}
}


if (isset($_GET['mode'])) $mode=$_GET['mode'];
if (isset($_POST['mode'])) $mode=$_POST['mode'];
if (isset($_POST['mode'])){ 
include_once("../message.php");
restrict_demo(); 
}
$lstRegion=$_POST['lstRegion'];
//echo 'Region = '.$lstRegion.'<br>';
$WCountry=$_POST['WCountry'];
//echo 'Country = '.$WCountry.'<br>';
$WState =$_POST['WState'];
//echo 'State = '.$WState.'<br>';
$WCity=(array)$_POST['WCity'];
//echo '<pre>';
//print_r($WCity);
//echo '<br>';
# retrieve the template

switch ($mode) {
    case 'addregion':
        if (!empty($_POST['newRegion'])) {
            $newRegion=$_POST['newRegion'];
            $query="INSERT INTO geo_region ( grg_name , grg_order)
                    VALUES ('$newRegion', '1000')";
            $result = mysql_query($query,$link) or die(mysql_error());
        }
        break;
    case 'editregion':
		echo "<script> window.location='$CONST_ADMIN_LINK_ROOT/admin_countryedit.php?country_id=$WCountry'</script>";
        //header('Location: '.$CONST_ADMIN_LINK_ROOT.'/admin_regionedit.php?region_id='.$lstRegion.'&PHPSESSID='.session_id());
        break;
    case 'delregion':
        $query="DELETE FROM geo_region WHERE grg_regionid = $lstRegion";
        $result = mysql_query($query,$link) or die(mysql_error());

        $select_countries = "SELECT gcn_countryid
                                FROM geo_country
                                 WHERE
                                    gcn_regionid = $lstRegion";
        $result_countries = mysql_query($select_countries,$link) or die(mysql_error());
        $newCountries = array();
        while ($aCountries = mysql_fetch_array($result_countries)) {
            $newCountries[] = $aCountries['gcn_countryid'];
        }
        if (!empty($newCountries)) {
            $query="DELETE FROM geo_state WHERE gst_countryid IN (".join(',',$newCountries).")";
            $result = mysql_query($query,$link) or die(mysql_error());
            $query="DELETE FROM geo_country WHERE gcn_countryid  IN (".join(',',$newCountries).")";
            $result = mysql_query($query,$link) or die(mysql_error());
        }
        unset($WCountry);
        break;
    case 'addcountry':
        if (!empty($_POST['newCountry'])) {
            $newCountry=$_POST['newCountry'];
            $query="INSERT INTO geo_country ( gcn_name , gcn_status , gcn_order,gcn_regionid)
                    VALUES ('$newCountry', '1', '1000','$lstRegion')";
            $result = mysql_query($query,$link) or die(mysql_error());
        }
        break;
    case 'editcountry':
		echo "<script> window.location='$CONST_ADMIN_LINK_ROOT/admin_countryedit.php?country_id=$WCountry'</script>";
        //header('Location: '.$CONST_ADMIN_LINK_ROOT.'/admin_countryedit.php?country_id='.$WCountry.'&PHPSESSID='.session_id());
        break;
    case 'delcountry':
        $query="DELETE FROM geo_city WHERE gct_countryid = $WCountry";
        $result = mysql_query($query,$link) or die(mysql_error());
        $query="DELETE FROM geo_state WHERE gst_countryid = $WCountry";
        $result = mysql_query($query,$link) or die(mysql_error());
        $query="DELETE FROM geo_country WHERE gcn_countryid = $WCountry";
        $result = mysql_query($query,$link) or die(mysql_error());
        unset($WCountry);
        break;
    case 'addstate':
        if (!empty($_POST['newState'])) {
            $newState=$_POST['newState'];
            $query="INSERT INTO geo_state ( gst_name, gst_countryid)
                    VALUES ('$newState', '$WCountry')";
            $result = mysql_query($query,$link) or die(mysql_error());
        }
        break;
    case 'editstate':
		echo "<script> window.location='$CONST_ADMIN_LINK_ROOT/admin_stateedit.php?state_id=$WState'</script>";
        header('Location: '.$CONST_ADMIN_LINK_ROOT.'/admin_stateedit.php?state_id='.$WState.'&PHPSESSID='.session_id());
        break;
    case 'delstate':
        $query="DELETE FROM geo_city WHERE gct_stateid = $WState";
        $result = mysql_query($query,$link) or die(mysql_error());
        $query="DELETE FROM geo_state WHERE gst_stateid = $WState";
        $result = mysql_query($query,$link) or die(mysql_error());
        unset($WState);
        break;
    case 'addcity':
        if (!empty($_POST['newCity'])) {
            $newCity=$_POST['newCity'];
            $query="INSERT INTO geo_city ( gct_name, gct_countryid, gct_stateid)
                    VALUES ('$newCity', '$WCountry', '$WState')";
            $result = mysql_query($query,$link) or die(mysql_error());
        }
        break;
    case 'editcity':
		
		echo "<script> window.location='$CONST_ADMIN_LINK_ROOT/admin_cityedit.php?city_id=$WCity[0]'</script>";
        //header('Location: admin_cityedit.php?city_id='.$WCity[0].'&PHPSESSID='.session_id());
        break;
    case 'delcity':
        $query="DELETE FROM geo_city WHERE gct_cityid in ('".join("','",(array)$WCity)."')";
        $result = mysql_query($query,$link) or die(mysql_error());
        break;
    case 'update':
        include('geography.php');
        $f = fopen('../geography.js', 'w');
        fwrite($f, get_geography_js());
        fclose($f);
        break;
}

$query="SELECT *
        FROM geo_region
        WHERE grg_regionid > 0
        ORDER BY grg_order, grg_name";
$regions = mysql_query($query,$link) or die(mysql_error());

if (isset($lstRegion)) {

    $query = "
            SELECT * FROM geo_country
            WHERE gcn_countryid > 0
            AND gcn_regionid = $lstRegion
            ORDER BY gcn_order,gcn_name";
        $countries = mysql_query($query,$link) or die(mysql_error());

          $query = "
            SELECT * FROM geo_country
            WHERE
                gcn_regionid = $lstRegion
            ORDER BY gcn_name";
////          echo $query;
          $checkcountry = mysql_query($query,$link) or die(mysql_error());
          if (mysql_num_rows($checkcountry) == 0) unset($WCountry);
          $is_countries = mysql_num_rows($checkcountry);

    if (isset($WCountry)) {
        $query = "
            SELECT * FROM geo_state
            WHERE gst_stateid > 0
            AND gst_countryid = $WCountry
            ORDER BY gst_name";
        $states = mysql_query($query,$link) or die(mysql_error());
        if (isset($WState)) {
          $query = "
            SELECT * FROM geo_state
            WHERE gst_stateid = $WState
            AND gst_countryid = $WCountry
            ORDER BY gst_name";
          $checkstate = mysql_query($query,$link) or die(mysql_error());
          if (mysql_num_rows($checkstate) == 0) unset($WState);
        }
        if (isset($WState)) {
          $query = "
            SELECT * FROM geo_city
            WHERE
                gct_cityid > 0
                AND gct_countryid = $WCountry
                AND gct_stateid = $WState
            ORDER BY gct_name";
          $cities = mysql_query($query,$link) or die(mysql_error());
        } else {
          $query = "
            SELECT * FROM geo_state
            WHERE gst_stateid > 0
            AND gst_countryid = $WCountry
            ORDER BY gst_name
            LIMIT 1";
//          echo $query;
//          echo '<br>';
          $state = mysql_query($query,$link) or die(mysql_error());
          if (mysql_num_rows($state) > 0) {
              $_state = mysql_fetch_object($state);
              $query = "
                SELECT * FROM geo_city
                WHERE
                    gct_cityid > 0
                    AND gct_countryid = $WCountry
                    AND gct_stateid = $_state->gst_stateid
                ORDER BY gct_name";
//              echo $query;
//          echo '<br>yes';
              $cities = mysql_query($query,$link) or die(mysql_error());
          } else {
              $query = "
                SELECT * FROM geo_city
                WHERE
                    gct_cityid > 0
                    AND gct_countryid = $WCountry
                ORDER BY gct_name";
//              echo $query;
//          echo '<br>no';
              $cities = mysql_query($query,$link) or die(mysql_error());
          }
        }
    }
    else {
        $query="SELECT *
            FROM geo_country
            WHERE gcn_countryid > 0
            AND gcn_regionid = $lstRegion
            ORDER BY gcn_order, gcn_name
            LIMIT 1";

        $country = mysql_query($query,$link) or die(mysql_error());
        $_country = mysql_fetch_object($country);

///////////////////////////
if ($is_countries)     {
        $query = "
            SELECT * FROM geo_state
            WHERE gst_stateid > 0
            AND gst_countryid = $_country->gcn_countryid
            ORDER BY gst_name";
        $states = mysql_query($query,$link) or die(mysql_error());
        if (isset($WState)) {
          $query = "
            SELECT * FROM geo_city
            WHERE
                gct_cityid > 0
                AND gct_countryid = $_country->gcn_countryid
                AND gct_stateid = $WState
            ORDER BY gct_name";
          $cities = mysql_query($query,$link) or die(mysql_error());
        } else {
          $query = "
            SELECT * FROM geo_state
            WHERE gst_stateid > 0
            AND gst_countryid = $_country->gcn_countryid
            ORDER BY gst_name
            LIMIT 1";
//          echo $query;
          $state = mysql_query($query,$link) or die(mysql_error());
          if (mysql_num_rows($state) > 0) {
            $_state = mysql_fetch_object($state);
            $query = "
                SELECT * FROM geo_city
                WHERE
                    gct_cityid > 0
                    AND gct_countryid = $_country->gcn_countryid
                    AND gct_stateid = $_state->gst_stateid
                ORDER BY gct_name";
            $cities = mysql_query($query,$link) or die(mysql_error());
          } else {
            $query = "
                SELECT * FROM geo_city
                WHERE
                    gct_cityid > 0
                    AND gct_countryid = $_country->gcn_countryid
                ORDER BY gct_name";
            $cities = mysql_query($query,$link) or die(mysql_error());
          }
        }


    }
////////////////////
    }
} else {

    $query="SELECT *
            FROM geo_region
            WHERE grg_regionid > 0
            ORDER BY grg_order, grg_name
            LIMIT 1";
    $region = mysql_query($query,$link) or die(mysql_error());
    $_region = mysql_fetch_object($region);

    $query="SELECT *
        FROM geo_country
        WHERE gcn_countryid > 0
        AND gcn_regionid = $_region->grg_regionid
        ORDER BY gcn_order, gcn_name";
    $countries = mysql_query($query,$link) or die(mysql_error());

//    if (isset($WCountry)) {
//        $query = "
//            SELECT * FROM geo_state
//            WHERE gst_stateid > 0
//            AND gst_countryid = $WCountry
//            ORDER BY gst_name";
//        $states = mysql_query($query,$link) or die(mysql_error());
//        if (isset($WState)) {
//          $query = "
//            SELECT * FROM geo_state
//            WHERE gst_stateid = $WState
//            AND gst_countryid = $WCountry
//            ORDER BY gst_name";
//          $checkstate = mysql_query($query,$link) or die(mysql_error());
//          if (mysql_num_rows($checkstate) == 0) unset($WState);
//        }
//        if (isset($WState)) {
//          $query = "
//            SELECT * FROM geo_city
//            WHERE
//                gct_cityid > 0
//                AND gct_countryid = $WCountry
//                AND gct_stateid = $WState
//            ORDER BY gct_name";
//          $cities = mysql_query($query,$link) or die(mysql_error());
//        } else {
//          $query = "
//            SELECT * FROM geo_state
//            WHERE gst_stateid > 0
//            AND gst_countryid = $WCountry
//            ORDER BY gst_name
//            LIMIT 1";
//          $state = mysql_query($query,$link) or die(mysql_error());
//          if (mysql_num_rows($state) > 0) {
//              $_state = mysql_fetch_object($state);
//              $query = "
//                SELECT * FROM geo_city
//                WHERE
//                    gct_cityid > 0
//                    AND gct_countryid = $WCountry
//                    AND gct_stateid = $_state->gst_stateid
//                ORDER BY gct_name";
//              $cities = mysql_query($query,$link) or die(mysql_error());
//          } else {
//              $query = "
//                SELECT * FROM geo_city
//                WHERE
//                    gct_cityid > 0
//                    AND gct_countryid = $WCountry
//                ORDER BY gct_name";
//              $cities = mysql_query($query,$link) or die(mysql_error());
//          }
//        }
//    }
//    else {
        $query="SELECT *
            FROM geo_country
            WHERE gcn_countryid > 0
                    AND gcn_regionid = $_region->grg_regionid
            ORDER BY gcn_order, gcn_name
            LIMIT 1";
        $country = mysql_query($query,$link) or die(mysql_error());
        $_country = mysql_fetch_object($country);

        $query = "
            SELECT * FROM geo_state
            WHERE gst_stateid > 0
            AND gst_countryid = '$_country->gcn_countryid'
            ORDER BY gst_name";

        $states = mysql_query($query,$link) or die(mysql_error());


          $query = "
            SELECT * FROM geo_state
            WHERE gst_stateid > 0
            AND gst_countryid = $_country->gcn_countryid
            ORDER BY gst_name
            LIMIT 1";
          $state = mysql_query($query,$link) or die(mysql_error());
          if (mysql_num_rows($state) > 0) {
            $_state = mysql_fetch_object($state);
            $query = "
                SELECT * FROM geo_city
                WHERE
                    gct_cityid > 0
                    AND gct_countryid = $_country->gcn_countryid
                    AND gct_stateid = $_state->gst_stateid
                ORDER BY gct_name";
            $cities = mysql_query($query,$link) or die(mysql_error());
          } else {
            $query = "
                SELECT * FROM geo_city
                WHERE
                    gct_cityid > 0
                    AND gct_countryid = $_country->gcn_countryid
                ORDER BY gct_name";
            $cities = mysql_query($query,$link) or die(mysql_error());
          }
//        }
//    }



}
//if (isset($lstRegion)) {
//          $query = "
//            SELECT * FROM geo_country
//            WHERE
//                gcn_regionid = $lstRegion
//            ORDER BY gcn_name";
////          echo $query;
//          $checkRegion = mysql_query($query,$link) or die(mysql_error());
//          if (mysql_num_rows($checkRegion) == 0) unset($states);
//          if (mysql_num_rows($checkRegion) == 0) unset($cities);
//}

//echo '<br>';
//echo 'Region = '.$lstRegion.'<br>';
//echo 'Country = '.$WCountry.'<br>';
//echo 'State = '.$WState.'<br>';
//print_r($WCity);
//echo '<br>';

$area = 'member';

?>
<?php include("include/header.php"); ?>

<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;Edit Geography</div></div>
<div class="clear"></div>
<div class="admin-page">
  <table width="<?php print("$CONST_TABLE_WIDTH"); ?>" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
    <!--DWLayoutTable-->
    <tr>
      <td width="793" align="right">
      <?php 	  //require_once("$CONST_INCLUDE_ROOT/user_status.inc.php");	  require_once("../user_status.inc.php");	  ?>      </td>
    </tr>
    <tr>
      <td><table width="100%"  border="0" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>">
        <form method='post' name='frmGeography' action='admin_geography.php<?php print("?PHPSESSID=".session_id()); ?>' >
          <input type='hidden' name='mode' value='view'>
          <tr valign="middle">
          <tr class="normal">
            <td  align="left"><?php echo SEARCH_REGION?></td>
            <td  align="left"> <select class="input" size="1" name="lstRegion" tabindex="1" onChange="frmGeography.mode.value='view';frmGeography.WCountry.value='';frmGeography.WState.value=''; submit(); return true;">
                <?php
						while ($sql_array = mysql_fetch_object($regions)) {
							if ($lstRegion == $sql_array->grg_regionid)
								print("<option value='$sql_array->grg_regionid' selected>$sql_array->grg_name</option>\n");
							else
								print("<option value='$sql_array->grg_regionid'>$sql_array->grg_name</option>\n");
						}
				?>
              </select></td>
            <td  align="left"><input type="submit" name="Submit" value="<?php echo BUTTON_EDIT ?>" class="normal" onClick="frmGeography.mode.value='editregion'; return true;"> <input type="submit" name="Submit" value="<?php echo BUTTON_REMOVE ?>" class="normal" onClick="frmGeography.mode.value='delregion'; return true;"></td>
            <td  align="left"><input type="input" class="input" name="newRegion"> <input type="submit" name="Submit" value="<?php echo BUTTON_ADD ?>" class="normal" onClick="frmGeography.mode.value='addregion'; return true;"></td>
          </tr>
          <tr class="normal">
            <td  align="left"><?php echo SEARCH_COUNTRY?></td>
            <td  align="left"> <select class="input" size="1" name="WCountry" tabindex="1" onChange="frmGeography.mode.value='view'; submit(); return true;">
                <?php
						while ($sql_array = mysql_fetch_object($countries)) {
							if ($WCountry == $sql_array->gcn_countryid)
								print("<option value='$sql_array->gcn_countryid' selected>$sql_array->gcn_name</option>\n");
							else
								print("<option value='$sql_array->gcn_countryid'>$sql_array->gcn_name</option>\n");
						}
				?>
              </select></td>
            <td  align="left"><input type="submit" name="Submit" value="<?php echo BUTTON_EDIT ?>" class="normal" onClick="frmGeography.mode.value='editcountry'; return true;"> <input type="submit" name="Submit" value="<?php echo BUTTON_REMOVE ?>" class="normal" onClick="frmGeography.mode.value='delcountry'; return true;"></td>
            <td  align="left"><input type="input" class="input" name="newCountry"> <input type="submit" name="Submit" value="<?php echo BUTTON_ADD ?>" class="normal" onClick="frmGeography.mode.value='addcountry'; return true;"></td>
          </tr>
          <tr class="normal">
            <td  align="left" valign="top"><?php echo SEARCH_STATE?></td>
            <td  align="left" valign="top"> <select class="input" size="1" name="WState" tabindex="1" onChange="frmGeography.mode.value='view'; submit(); return true;">
                <?php
						while ($states && ($sql_array = mysql_fetch_object($states))) {
							if ($WState==$sql_array->gst_stateid )
								print("<option value='$sql_array->gst_stateid' selected>$sql_array->gst_name</option>\n");
							else
								print("<option value='$sql_array->gst_stateid'>$sql_array->gst_name</option>\n");
						}
				?>
              </select> </td>
            <td  align="left"><input type="submit" name="Submit" value="<?php echo BUTTON_EDIT ?>" class="normal" onClick="frmGeography.mode.value='editstate'; return true;"> <input type="submit" name="Submit" value="<?php echo BUTTON_REMOVE ?>" class="normal" onClick="frmGeography.mode.value='delstate'; return true;"></td>
            <td  align="left" valign="top"><input type="input" class="input" name="newState"> <input type="submit" name="Submit" value="<?php echo BUTTON_ADD ?>" class="normal" onClick="frmGeography.mode.value='addstate'; return true;"></td>
          </tr>
          <tr class="normal">
            <td  align="left"><?php echo SEARCH_CITY?></td>
            <td  align="left"> <select class="input" size="4" name="WCity[]" tabindex="1" multiple>
                <?php
                        while ($cities && ($sql_array = mysql_fetch_object($cities))) {
                            if ($lstDescriptions==$sql_array->gct_cityid)
                                print("<option value='$sql_array->gct_cityid' selected>$sql_array->gct_name</option>\n");
                            else
                                print("<option value='$sql_array->gct_cityid'>$sql_array->gct_name</option>\n");
                        }
                ?>
              </select></td>
            <td  align="left"><input type="submit" name="Submit" value="<?php echo BUTTON_EDIT ?>" class="normal" onClick="frmGeography.mode.value='editcity'; return true;"> <input type="submit" name="Submit" value="<?php echo BUTTON_REMOVE ?>" class="normal" onClick="frmGeography.mode.value='delcity'; return true;"></td>
            <td  align="left"><input type="input" class="input" name="newCity"> <input type="submit" name="Submit" value="<?php echo BUTTON_ADD ?>" class="normal" onClick="frmGeography.mode.value='addcity'; return true;"></td>
          </tr>
          <tr>
            <td  align="left"></td>
            <td  align="left"></td>
            <td  align="left"></td>
            <td  align="left"></td>
          </tr>
<? //if($GEOGRAPHY_JAVASCRIPT){?>
          <tr>
            <td colspan="4" align="left" valign="top" class="tdfoot"> <center>
                <input type="submit" name="Submit" value="<?php echo BUTTON_UPDATE ?>" class="normal"  onClick="frmGeography.mode.value='update'; return true;">
              </center></td>
          </tr>
<? //}?>
        </form>
      </table></td>
    </tr>
    <tr>
      <td height="37">&nbsp;</td>
    </tr>
    <tr>
      <td height="40" valign="top"><strong>Note:</strong> When you Edit, Delete or Add a new Country, City or State, you will need to click the Update Now button. You will also need to delete your local browser cache to see the changes.</td>
    </tr>
</table>
</div><div class="admin-page-bottom"></div></div>
<?php include("include/footer.php"); ?>