<?php

if( $a == 'translate' ){
	if( isset( $_POST['sOption'] ) ){
		$aCountries = $oDB->executeSQL( 'SELECT `iCountry`, `sCountry` FROM `' . $oDB->throwTableName( DB_COUNTRIES ) . '` ORDER BY `iCountry`', true, DataBase::ASSOCIATIVE );
		foreach( $_POST['aCountries'] as $iCountry => $sCountry ) if( $sCountry != $aCountries[$iCountry] ){
			$oDB->update( DB_COUNTRIES, Array( 'sCountry' => $sCountry ), Array( 'iCountry' => $iCountry ) );
		}
		$aRegions = $oDB->executeSQL( 'SELECT `iRegion`, `sRegion`, `iCountry` FROM `' . $oDB->throwTableName( DB_REGIONS ) . '` ORDER BY `iCountry`, `sRegion`', true, DataBase::ASSOCIATIVE, 'iRegion' );
		foreach( $_POST['aRegions'] as $iRegion => $aRegion ){
			if( $aRegion['iDelete'] ){
				$oDB->deleteInFile( DB_REGIONS, $iRegion, 'iRegion' );
			} elseif( $aRegion['sRegion'] != $aRegions[$iRegion]['sRegion'] || $aRegion['iCountry'] != $aRegions[$iRegion]['iCountry'] ){
				$oDB->update( DB_REGIONS, Array( 'iCountry' => $aRegion['iCountry'], 'sRegion' => $aRegion['sRegion'] ), Array( 'iRegion' => $iRegion ) );
			}
		}
		if( isset( $_POST['aNewRegions'] ) ){
			$_POST['aNewRegions'] = array_transpose( $_POST['aNewRegions'] );
			foreach( $_POST['aNewRegions'] as $aNewRegion ){
				$oDB->save( DB_REGIONS, $aNewRegion );
			}
		}
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $p . '&sOption=save' . '&activeTabId=' . $_POST['activeTabId'] );
		exit;
	}
	if( isset( $_GET['sOption'] ) ){
		$content .= $oTpl->parse( 'message_done.tpl' );
	}
	$aCountries = $oDB->executeSQL( 'SELECT `iCountry`, `sCountry` FROM `' . $oDB->throwTableName( DB_COUNTRIES ) . '` ORDER BY `iCountry`', true, DataBase::ASSOCIATIVE );
	$oTpl->assign( 'aCountries', $aCountries );
	$aRegions = $oDB->executeSQL( 'SELECT `iRegion`, `sRegion`, `iCountry` FROM `' . $oDB->throwTableName( DB_REGIONS ) . '` ORDER BY `iCountry`, `sRegion`', true, DataBase::ASSOCIATIVE, 'iRegion' );
	$oTpl->assign( 'aRegions', $aRegions );
	$content .= $oTpl->parse( 'countries_regions_list.tpl' );
}