<?php
/* **
 * @application PhpMyShopping
 * @part        Admin boutique
 * @file        ajax.php
 * @path        ./admin/
 * @author      Stephane DINE <stephane@phpmyshopping.org>
 * @site        http://www.phpmyshopping.org
 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
 *
 * PHP version 5
 */

session_start();

// require_once '../../admin/include/class.lib.php';
// $data = new Lib();

require_once './include/class.catalog.php';
$data = new Catalog();

switch($_REQUEST['param']) {
/** -------------------------------------------------------------------------------------
 * Generation de la liste des pays
 */
case 'delImg':
	$data->deleteProductImg($_REQUEST['param2'], $_REQUEST['param3']);
	echo 'delImg_'.$_REQUEST['param3'].'|<b class="green">Image &eacute;ffac&eacute;e</b>';
	break;

/** -------------------------------------------------------------------------------------
 * Generation de la liste des pays
 */
case 'Country':
	$newSql = 'SELECT ID_Country, CountryName, CountryCode, Status FROM Country WHERE Status = "A"';
	$list = '<tr bgcolor="#FFFFFF">
	  <select id="countryList" name="fieldGeo[ID_Country]" size="1" onChange="sndReq(\'State\')">
	  <option value="X">--[ Choisissez le pays ]--</option>
	  '.$data->liste_select('fieldGeo[ID_Country]', array('ID_Country', 'CountryName'), 'Country', 'CountryName', $DefaultCountry, false, '', $newSql, false).'
	  <div id="State"></div>';
	echo 'Country|'.$list;

	break;

/** -------------------------------------------------------------------------------------
 * Generation de la liste des etats
 */
case 'State':
	$stateList = '<select size="1" id="stateList" name="fieldGeo[ID_State]" onChange="sndReq2(\'City\')">
	<option value="X">--[ Choisissez de l\'&eacute;tat/d&eacute;partement ]--</option>';
	$sqlState = 'SELECT ID_State, StateName FROM State WHERE ID_Country = '.$_REQUEST['IdCountry'];

	$queryState = $data->query($sqlState);
	while ($resultState = mysql_fetch_array($queryState)) {
		$stateList .= '<option value="'.$resultState['ID_State'].'">'.$resultState['StateName'].'</option>';
	}

	$stateList .= '</select><br><div id="City"></div>';
	echo 'State|<br>'.$stateList;
	break;

/** -------------------------------------------------------------------------------------
 * Generation de la liste des villes
 */
case 'City':
	$cityList = '<select size="1" name="fieldGeo[ID_City]">
	<option value="X">--[ Choisissez de la ville ]--</option>';
	$sqlCity = 'SELECT ID_City, CityName, PostCode FROM City WHERE ID_State = '.$_REQUEST['IdState'];

	$queryCity = $data->query($sqlCity);
	while ($resultCity = mysql_fetch_array($queryCity)) {
		$cityList .= '<option value="'.$resultCity['ID_City'].'">'.$resultCity['CityName'].' ('.$resultCity['PostCode'].')</option>';
	}

	$cityList .= '</select><br><div id="NoCity"><br>Ville non trouv&eacute;e ?
	<a href="javascript:sndReq(\'NoCity\')">cliquez ici</a></div>';
	echo 'City|<br>'.$cityList;
	break;

/** -------------------------------------------------------------------------------------
 * * Affichage du champ de saisie pour une nouvelle ville
 */
case 'NoCity':
	echo 'NoCity|<br>Ville : <input type="text" size="35" maxlength="60" name="fieldGeo[NewCityName]"><br><br>
	Code postal : <input type="text" size="10" maxlength="6" name="fieldGeo[PostCode]">';
	break;

/** -------------------------------------------------------------------------------------
 * Modification du status d'un pays
 */
case 'ModifStatusPays_'.$_REQUEST['ID_Country']:
	if ($_REQUEST['Status'] == 'A') {
		$CountryStatus = 'L';
	} else {
		$CountryStatus = 'A';
	}

	$sql = 'UPDATE Country SET Status = "'.$CountryStatus.'" WHERE ID_Country = '.$_REQUEST['ID_Country'];
	$data->query($sql);

	$status = '<div id="ModifStatusPays_'.$_REQUEST['ID_Country'].'">
	<a href="javascript:modifStatusPays(\''.$_REQUEST['ID_Country'].'\',\''.$CountryStatus.'\')">
	<img src="./images/'.$CountryStatus.'.png" border="0" /></a>
	</div>';
	echo 'ModifStatusPays_'.$_REQUEST['ID_Country'].'|'.$status;
	break;

/** -------------------------------------------------------------------------------------
 * Affichage du champ de saisie pour une nouvelle ville
 */
case 'HelpMe':
	//$data->Log(0, $_SESSION['ID_User'], 'Affichage du champ de saisie d\'une nouvelle ville');
	if ($_REQUEST['arg'] == 'View') {
		$div = '<div class="HelpMe">'.$data->viewImage('help').'<br><a href="javascript:help(\'Close\')">Fermer l\'aide !</a></div>';
	} else {
		$div = '';
	}

	echo 'HelpMe|'.$div;
	break;

/** -------------------------------------------------------------------------------------
 * Suppression de l'image du logo de la boutique
 */
case 'deleteImgLogo':
	unlink('../images/'.$_REQUEST['arg2']);
	$field['Logo'] = '0';
	$data->update_sql('Shop', $field, 1);
	echo 'deleteImgLogo|<br /><b class="red">Image effac&eacute;e !</b><br /><br />';
	break;

/** -------------------------------------------------------------------------------------
 *
 */
case 'ID_Cat':
	$field = '<div id="addField"><input type="text" name="ID_Cat" id="ID_Cat" size="50" maxlength="50" /><a href="javascript:saveData()"><img src="./images/A.png" title="Enregistrer votre saisie" border="0" /></a></div>';
	echo 'ID_Cat|'.$field;
	break;

/** -------------------------------------------------------------------------------------
 *
 */
case 'ID_SCat':
	$field = '<div id="addField2"><input type="text" name="ID_SCat" id="ID_SCat" size="50" maxlength="50" /><a href="javascript:saveDataSubCat()"><img src="./images/A.png" title="Enregistrer votre saisie" border="0" /></a></div>';
	echo 'ID_SCat|'.$field;
	break;

/** -------------------------------------------------------------------------------------
 *
 */
case 'saveData':
	if ($_REQUEST['data']) {
		$sql = 'INSERT INTO Category (CategoryName) VALUES ("'.$_REQUEST['data'].'")';
		$data->query($sql);
	}

	$sqlListCategory = 'SELECT ID_Category, CategoryName
						FROM Category
						WHERE Status != "D"
						GROUP BY ID_Category
						ORDER BY CategoryName ASC';

	$listCategory = $data->liste_select('champ[ID_Category]', array('ID_Category', 'CategoryName'), 'Category', 'CategoryName', $data->last_id(), false, '', $sqlListCategory);

	$list = '<div id="listSelect">'.$listCategory.'</div>';
	$list .= '<div id="ID_Cat">&nbsp;<a href="javascript:addData(\'ID_Cat\')" class="formButton">Cr&eacute;er une cat&eacute;gorie</a></div>';
	echo 'list1|'.$list;
	break;

/** -------------------------------------------------------------------------------------
 *
 */
case 'saveDataSubCat':
	if ($_REQUEST['data']) {
		$sql = 'INSERT INTO SubCategory (SubCategoryName) VALUES ("'.$_REQUEST['data'].'")';
		$data->query($sql);
	}

	$sqlListSubCategory = 'SELECT ID_SubCategory, SubCategoryName
						   FROM SubCategory
						   WHERE Status != "D"
						   GROUP BY ID_SubCategory
						   ORDER BY SubCategoryName ASC';

	$listSubCategory = $data->liste_select('champ[ID_SubCategory]', array('ID_SubCategory', 'SubCategoryName'), 'SubCategory', 'SubCategoryName', $data->last_id(), false, '', $sqlListSubCategory);

	$list = '<div id="listSelect2">'.$listSubCategory.'</div>';
	$list .= '<div id="ID_SCat">&nbsp;<a href="javascript:addData(\'ID_SCat\')" class="formButton">Cr&eacute;er une sous-cat&eacute;gorie</a></div>';
	echo 'list2|'.$list;
	break;

/** -------------------------------------------------------------------------------------
 * Generation de la liste des etats
 */
case 'stateList':
	$sqlState = 'SELECT ID_State, StateName FROM State WHERE ID_Country = '.$_REQUEST['id'].' ORDER BY StateName ASC';
	$queryState = $data->query($sqlState);

	while ($resultState = mysql_fetch_array($queryState)) {
		$stateList .= '<option value="'.$resultState['ID_State'].'">'.$resultState['StateName'].'</option>';
	}

	$stateListHtml .= '<select name="ID_State" id="ID_State" onChange="geoData(\'cityList\', \'ID_State\')">
	<option value="X">--[ Choisissez votre &eacute;tat ou d&eacute;partement ]--</option>'.$stateList.'</select>';
	echo 'stateList|'.$stateListHtml;
	break;

/** -------------------------------------------------------------------------------------
 * Generation de la liste des etats
 */
case 'cityList':
	$sqlCity = 'SELECT ID_City, CityName, PostCode FROM City WHERE ID_State = '.$_REQUEST['id'].' ORDER BY CityName ASC';
	$queryCity = $data->query($sqlCity);

	while ($resultCity = mysql_fetch_array($queryCity)) {
		// if ($resultCity['PostCode'] > 0) {
			// $PostCode = ' - '.$resultCity['PostCode'];
		// } else {
			// $PostCode = '';
		// }

		$cityList .= '<option value="'.$resultCity['ID_City'].'">'.$resultCity['CityName'].' - '.$resultCity['PostCode'].'</option>'; // $PostCode.
	}

	$cityListHtml .= '<select name="data[ID_City]" id="ID_City"><option value="X">--[ Choisissez votre ville ]--</option>'.$cityList.'</select>';
	echo 'cityList|'.$cityListHtml;
	break;

/** -------------------------------------------------------------------------------------
 * Defaut
 */
default:
}
?>
