<?php
/*
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from Zac
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require('includes/classes/split_page_results_event.php');
  $command=isset($HTTP_GET_VARS["command"])?$HTTP_GET_VARS["command"]:'';
  $zone_id=isset($HTTP_GET_VARS["zone_id"])?$HTTP_GET_VARS["zone_id"]:'';
  $page=isset($HTTP_GET_VARS["page"])?$HTTP_GET_VARS["page"]:1;
  $country_id=isset($HTTP_GET_VARS["country_id"])?$HTTP_GET_VARS["country_id"]:'';
  $assoc_id=isset($HTTP_GET_VARS['assoc_id'])?$HTTP_GET_VARS['assoc_id']:'';
  $server_date = getServerDate(true);
  $languages = tep_get_languages();
  switch($command){
  	case 'page_navigation_zone':
  		echo 'page_navigation_zone^';
  		geo_zone_list();
  		exit;
  		break;
   	case 'load_details':
		echo 'load_details^';
		echo $zone_id.'^';
		$zone_name="";
		$zones_query = tep_db_query("select geo_zone_id, geo_zone_name, geo_zone_description from " . TABLE_GEO_ZONES . " where geo_zone_id='".$zone_id."' ");
		if(tep_db_num_rows($zones_query)>0){
		$zones_array=tep_db_fetch_array($zones_query);
		$zone_name=$zones_array["geo_zone_name"];
		$zone_description=$zones_array['geo_zone_description'];
		}
		$zones_country_query = tep_db_query("select a.association_id, a.zone_country_id, c.countries_name, a.zone_id, a.geo_zone_id, a.last_modified, a.date_added, z.zone_name from " . TABLE_ZONES_TO_GEO_ZONES . " a left join " . TABLE_COUNTRIES . " c on a.zone_country_id = c.countries_id left join " . TABLE_ZONES . " z on a.zone_id = z.zone_id where a.geo_zone_id = " . $zone_id . " order by association_id");
		?>
		<table border="0" cellpadding="0" class="openContent" cellspacing="0" width="100%">
		<tr class="openContent_top">
		<td class="main"><?php echo tep_image(DIR_WS_IMAGES . "template/icon_active.gif").'&nbsp;&nbsp;' .$zone_name; ?>
		</td>	
		<td align="left" width="400">
			<table style="background:#DEE4F5;cursor:pointer;" width="50" border="0" cellpadding="0" id="header_operation_id" cellspacing="0">
				<tr id="img_ids">
				<td class="cell_bg_popm_left">&nbsp;</td>
					<?php echo   '<td width="10%" onClick="javascript:do_hover_change(this,1);void attributes_edit();" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);"><img alt="Edit" title="Edit" src="images/template/img_edit.gif"></td>'; ?>
					<?php // Don't Delete, If condition because Defalut Zone Id's 1 =>USA,2=>world Zone,3 => Australlia?>
					<?php if($zone_id>3)echo   '<td width="10%" onClick="javascript:do_hover_change(this,1);drop_geo_zones(' . $zone_id . ');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);"><img alt="Delete" title="Delete" src="images/template/img_trash.gif"></td>'; ?>
				<td class="cell_bg_popm_right">&nbsp;</td>	
				</tr>
			</table>
			<table style="background:#DEE4F5; display:none;" width="50" border="0" cellpadding="0" id="icon_operation" cellspacing="0">
				<tr>
				<td class="cell_bg_popm_left">&nbsp;</td>
					<?php echo   '<td width="10%" onClick="javascript:do_hover_change(this,1);void attributes_edit();" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);"><img alt="Edit" title="Edit" src="images/template/img_edit.gif"></td>'; ?>
					<?php // Don't Delete, If condition because Defalut Zone Id's 1 =>USA,2=>world Zone,3 => Australlia?>
					<?php if($zone_id>3)echo   '<td width="10%" onClick="javascript:do_hover_change(this,1);drop_geo_zones(' . $zone_id . ');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);"><img alt="Delete" title="Delete" src="images/template/img_trash.gif"></td>'; ?>
				<td class="cell_bg_popm_right">&nbsp;</td>	
				</tr>
			</table>
		</td>	
		<td width="100" id="header_edit_display" style="display:none;"  align="right"><?php echo '<a  href="javascript: save_geo_zones('.$zone_id.')">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','Save').'</a>'.'&nbsp;&nbsp;<a href="javascript: base_zone_close('.$zone_id.');">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','Close').'</a>'; ?></td>
		<td width="100" id="header_delete_display" style="display:none;" align="right">&nbsp;</td>
		</tr>
		<tr><td colspan="3"><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td></tr>	
		<tr id="edit_details_id" style="display:none;">
		<td colspan="3" class="main" align="center"><?php echo TABLE_HEADING_ZONE_NAME.'&nbsp;&nbsp;'.tep_draw_input_field('geo_zone_name',$zone_name) . '&nbsp;&nbsp;' . TABLE_HEADING_ZONE_DESCRIPTION.'&nbsp;&nbsp;'.tep_draw_input_field('geo_zone_description',$zone_description) ; ?></td>
		</tr>
		<tr> <td colspan="3" class="main"><table width="100%" cellpadding="5" cellspacing="5" border="0"> 
		<tr><td width="100">&nbsp;</td><td id="option_value_details_content">
		<?php $row=1; $class_name="";
		if(tep_db_num_rows($zones_country_query)>10)
			echo '<div style="height:200;overflow:scroll;border:solid 1px #FFFFFF;">';
		while($zones_country_array=tep_db_fetch_array($zones_country_query)){ 
			if(isset($HTTP_GET_VARS['country_id']) && $zones_country_array['zone_country_id']==$HTTP_GET_VARS['country_id']) {
				$zInfo[] = new objectInfo($zones_country_array);
			}
			if($row%2==0) $class_name='subdataTableRowEven'; else $class_name='subdataTableRowOdd';
		?>
		<div class="main"  id="opt_row_<?php echo $zones_country_array["association_id"]; ?>" onClick="javascript:opt_row_focus(<?php echo $zones_country_array["association_id"]; ?>,<?php echo $zones_country_array['geo_zone_id']; ?>);">
		<div style="" class="<?php echo $class_name; ?>" onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);" id="opt_<?php echo $zones_country_array["association_id"]; ?>"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$zones_country_array["countries_name"]; ?></div>
		</div>
		<div id="ajax_opt_row_<?php echo $zones_country_array["association_id"]; ?>">
		<div id="ajax_opt_<?php echo $zones_country_array["association_id"]; ?>" style="display:none;"></div>
		</div>
		<?php } 
		if(tep_db_num_rows($zones_country_query)>10)
			echo '</div>';
		?>  </td></tr>
		<tr><td>&nbsp;</td>
		<td>
			<div id="new_zone_value_content" style="display:none;" style="background:#DEE4F5;">
				<form name="country_detail" action="shop_geo_zones.php">
				<table width="100%" cellpadding="2" cellspacing="2" border="0">
					<tr>
						<td class="main"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;New Country';	?></td>
						<td align="right"><?php echo '<a href="javascript:void save_new_zone_value(' . $zone_id .');">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','Save').'</a>&nbsp;&nbsp;<a href="javascript: close_new_zone_value();">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','Close').'</a>'; ?></td>
					</tr>
					<tr>
						<td class="main" align="left" width="350"><?php echo 'Country ' .tep_draw_pull_down_menu('zone_country_id', tep_get_countries(TEXT_ALL_COUNTRIES), $zInfo->zone_country_id, 'onChange="update_zone(this.form);"'); ?></td>
						<td class="main" align="left" width="250"><?php echo 'Zone ' .tep_draw_pull_down_menu('sel_zone_id', tep_prepare_country_zones_pull_down($zInfo->zone_country_id), $zInfo->zone_id); ?></td>
					</tr>
				</table>
				</form>
			</div>
		</td></tr>  </table>
		</td></tr>
		</table>
		<?php
	
		exit;
		break;
	 case 'save_geo_zones':
	 	echo 'save_geo_zones^';
		$geo_zone_name=isset($HTTP_GET_VARS["geo_zone_name"])?$HTTP_GET_VARS["geo_zone_name"]:'';
		$geo_zone_description=isset($HTTP_GET_VARS["geo_zone_description"])?$HTTP_GET_VARS["geo_zone_description"]:'';
		tep_db_query("update ".TABLE_GEO_ZONES." set geo_zone_name='$geo_zone_name', geo_zone_description='$geo_zone_description' where geo_zone_id='$zone_id'");
		echo $zone_id.'^';
		echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$geo_zone_name;
		exit;
		break;
	case 'drop_geo_zones':
		$country_query=tep_db_query("Select * from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id='" . (int)$zone_id . "' ");
		if(tep_db_num_rows($country_query)<=0) {
			echo 'drop_geo_zones^';
			echo $zone_id.'^';
			// Don't Delete, If condition because Defalut Zone Id's 1 =>USA,2=>world Zone,3 => Australlia
			if($zone_id>3){
				tep_db_query("delete from ".TABLE_GEO_ZONES." where geo_zone_id='$zone_id'");
				tep_db_query("delete from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . (int)$zone_id . "'");
			}
		}
		else {
			echo 'drop_fail_services_options^';
			echo $zone_id;
		}
		exit;
		break;
	case 'opt_load_details':
		echo 'load_opt_details^';
		echo $assoc_id.'^'; 
		$zInfo="";
	    $zones_country_query = tep_db_query("select a.association_id, a.zone_country_id, c.countries_name, a.zone_id, a.geo_zone_id, a.last_modified, a.date_added, z.zone_name from " . TABLE_ZONES_TO_GEO_ZONES . " a left join " . TABLE_COUNTRIES . " c on a.zone_country_id = c.countries_id left join " . TABLE_ZONES . " z on a.zone_id = z.zone_id where a.geo_zone_id = " . $HTTP_GET_VARS['zone_id'] . " order by association_id");
		if(tep_db_num_rows($zones_country_query)>0) {
			while($zones_country_array=tep_db_fetch_array($zones_country_query)) {
				if(isset($HTTP_GET_VARS['assoc_id']) && $zones_country_array['association_id']==$HTTP_GET_VARS['assoc_id'])
					$zInfo = new objectInfo($zones_country_array);
			}
		}	
		?>
		<table border="0" cellpadding="0" cellspacing="0" width="100%">
		<tr style="background:#DEE4F5;">
		<td colspan="3" class="main"><?php echo tep_image(DIR_WS_IMAGES . "template/icon_active.gif").'&nbsp;&nbsp;'.$zInfo->countries_name; ?></td>
		</tr>
		<tr>
		<td width="365">&nbsp;</td>
		<td align="center">
		<table width="100%" border="0" style="background:#DEE4F5;" cellpadding="0" cellspacing="0">
		<tr>
		<td class="cell_bg_popm_left">&nbsp;</td>
		<?php echo  '<td style="cursor:pointer;" onclick="javascript:do_hover_change1(this,1);drop_zone_countries(' . $zInfo->association_id . ',' . $zInfo->geo_zone_id . ');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);"><img alt="Delete" title="Delete" src="images/template/img_trash.gif"></td>'; ?>
		<td class="cell_bg_popm_right">&nbsp;</td>
		</tr>
		</table>
		</td>
		<td width="365" align="right"><?php echo '<a href="javascript: save_zone_countries('. $zInfo->association_id . ')">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','Save').'</a>&nbsp;<a href="javascript:close_option_value(' . $zInfo->association_id . ')">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','Close').'</a>'; ?></td>
		</tr>
		<tr><td colspan="3">
			<form name="zone_country_detail" action="shop_geo_zones.php">
				<table cellpadding="2" cellspacing="0">
				<tr>
					<td colspan="2" class="main" align="left"><?php echo 'Country ' .tep_draw_pull_down_menu('zone_country_id', tep_get_countries(TEXT_ALL_COUNTRIES), $zInfo->zone_country_id, 'onChange="update_zone(this.form);"'); ?></td>
					<td class="main" align="left"><?php echo 'Zone ' .tep_draw_pull_down_menu('sel_zone_id', tep_prepare_country_zones_pull_down($zInfo->zone_country_id), $zInfo->zone_id); ?></td>
				</tr>
				</table>
			</form>	
		</tr>
		<tr>
		<td id="delete_options" style="display:none" class="main">&nbsp;</td> 
		</tr>
		</table>
		<?php
		exit;
		break;
	case 'save_zone_countries':
		echo 'save_zone_countries^';
		$zone_country_id=isset($HTTP_GET_VARS['zone_country_id'])?$HTTP_GET_VARS['zone_country_id']:'';
		$sel_zone_id=isset($HTTP_GET_VARS['sel_zone_id'])?$HTTP_GET_VARS['sel_zone_id']:'';
		$country_id=isset($HTTP_GET_VARS['country_id'])?$HTTP_GET_VARS['country_id']:'';
		$assoc_id=isset($HTTP_GET_VARS['assoc_id'])?$HTTP_GET_VARS['assoc_id']:'';
		echo $assoc_id.'^';
		tep_db_query("update " . TABLE_ZONES_TO_GEO_ZONES . " set zone_country_id = '" . (int)$zone_country_id . "', zone_id = " . (tep_not_null($sel_zone_id) ? "'" . (int)$sel_zone_id . "'" : 'null') . ", last_modified =now() where association_id = '" . (int)$assoc_id . "'");
		$country_name_query=tep_db_query("Select countries_name from countries where countries_id='" . $zone_country_id . " '");
		$country_name_array=tep_db_fetch_array($country_name_query);
		echo '<div class="dataTableRowOdd" onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);"  id="opt_'.$assoc_id.'">'.tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$country_name_array['countries_name'].'</div>';
		exit;
		break;
	case 'save_new_zone':
		echo 'save_new_zone^';
		$zone_name=isset($HTTP_GET_VARS["zone_name"])?$HTTP_GET_VARS["zone_name"]:'';
		$zone_description=isset($HTTP_GET_VARS['zone_description'])?$HTTP_GET_VARS['zone_description']:'';
        tep_db_query("insert into " . TABLE_GEO_ZONES . " (geo_zone_name, geo_zone_description, date_added) values ('" . tep_db_input($zone_name) . "', '" . tep_db_input($zone_description) . "','" . $server_date . "')");
		exit;
		break;
	 case 'save_new_zone_value':
	 	echo 'save_new_zone_value^'; 
		$zone_country_id=isset($HTTP_GET_VARS["zone_country_id"])?tep_db_prepare_input($HTTP_GET_VARS["zone_country_id"]):'';
		$sel_zone_id=isset($HTTP_GET_VARS['sel_zone_id'])?$HTTP_GET_VARS['sel_zone_id']:'';
		tep_db_query("insert into " . TABLE_ZONES_TO_GEO_ZONES . " (zone_country_id, zone_id, geo_zone_id, date_added) values ('" . (int)$zone_country_id . "', '" . (int)$sel_zone_id . "', '" . (int)$zone_id . "','" . $server_date . "')");

	    $zones_country_query = tep_db_query("select a.association_id, a.zone_country_id, c.countries_name, a.zone_id, a.geo_zone_id, a.last_modified, a.date_added, z.zone_name from " . TABLE_ZONES_TO_GEO_ZONES . " a left join " . TABLE_COUNTRIES . " c on a.zone_country_id = c.countries_id left join " . TABLE_ZONES . " z on a.zone_id = z.zone_id where a.geo_zone_id = " . $HTTP_GET_VARS['zone_id'] . " order by association_id");
		?>
		<?php while($zones_country_array=tep_db_fetch_array($zones_country_query)){ ?>
		<div class="main" id="opt_row_<?php echo $zones_country_array["association_id"]; ?>" onClick="javascript:opt_row_focus(<?php echo $zones_country_array["association_id"]; ?>,<?php echo $zones_country_array["geo_zone_id"]; ?>);">
		<div class="dataTableRowOdd" onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);" id="opt_<?php echo $zones_country_array["association_id"]; ?>"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$zones_country_array["countries_name"]; ?></div>
		</div>
		<div id="ajax_opt_row_<?php echo $zones_country_array["association_id"]; ?>">
		<div id="ajax_opt_<?php echo $zones_country_array["association_id"]; ?>" style="display:none;"></div>
		</div>
		<?php } ?>
		<?php
		echo '^'.$zones_country_array["association_id"];
	 	exit;
		break;
	 case 'drop_zone_countries':
		echo 'drop_zone_countries^';
		tep_db_query("delete from " . TABLE_ZONES_TO_GEO_ZONES . " where association_id = '" . (int)$HTTP_GET_VARS['assoc_id'] . "'");
		echo $HTTP_GET_VARS['assoc_id'];
		$country_count_query=tep_db_query("Select * from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id='" . $HTTP_GET_VARS['zone_id'] . "' ");
		if(tep_db_num_rows($country_count_query)<=0) {
			echo '^show_header_icon';
		}	
	 	exit;
		break;
	 }
  tep_get_last_access_file();                                              
  $languages = tep_get_languages();
  $zones_query = tep_db_query("select geo_zone_id, geo_zone_name, geo_zone_description, last_modified, date_added from " . TABLE_GEO_ZONES . " order by geo_zone_name");
  $cnt=0;
  if(tep_db_num_rows($zones_query)>0) {
   while($zones_array=tep_db_fetch_array($zones_query)){
		$zid_array[]=array('id'=>$zones_array['geo_zone_id'],'text'=>$zones_array['geo_zone_name']);
	}
}	 
//print_r($zid_array);
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html;" charset="<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="javascript" src="includes/menu.js"></script>
	<script language="javascript">
	 var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";
	function update_zone(theForm) {
  var NumState = theForm.sel_zone_id.options.length;
  var SelectedCountry = "";

  while(NumState > 0) {
    NumState--;
    theForm.sel_zone_id.options[NumState] = null;
  }         

  SelectedCountry = theForm.zone_country_id.options[theForm.zone_country_id.selectedIndex].value;

<?php echo tep_js_zone_list('SelectedCountry', 'theForm', 'sel_zone_id'); 
?>

}
</script>
	<script language="javascript">
	<?php 
	 $zones_query = tep_db_query("select geo_zone_id from " . TABLE_GEO_ZONES . " order by geo_zone_id");
 	 $zones_country_query = tep_db_query("select a.association_id, a.zone_country_id, c.countries_name, a.zone_id, a.geo_zone_id, a.last_modified, a.date_added, z.zone_name from " . TABLE_ZONES_TO_GEO_ZONES . " a left join " . TABLE_COUNTRIES . " c on a.zone_country_id = c.countries_id left join " . TABLE_ZONES . " z on a.zone_id = z.zone_id  order by association_id");
	?>
	var zones_id_array=new Array();
	var zone_country_id_array=new Array();
	var zid="";
	var cid="";
	<?php $icnt=0; 
	while($zones_country_array=tep_db_fetch_array($zones_country_query)){ ?>
	zone_country_id_array[<?php echo $icnt; ?>]=<?php echo $zones_country_array["association_id"]; ?>;
	<?php $icnt++; } ?>
	<?php $cnt=0; while($zones_array=tep_db_fetch_array($zones_query)){ ?>
	zones_id_array[<?php echo $cnt; ?>]=<?php echo $zones_array["geo_zone_id"]; ?>;
	<?php $cnt++; } 
	?>
	</script>
<script language="javascript">
	function opt_row_focus(opt_id,zone_id){ 
		cid=opt_id;
		document.getElementById('header_operation_id').style.display='none';
		document.getElementById('edit_details_id').style.display='none';
		document.getElementById('header_edit_display').style.display='none';
		document.getElementById('header_delete_display').style.display='none';
		for(i=0,len=zone_country_id_array.length;i<len;i++){
			if(zone_country_id_array[i]!=opt_id && document.getElementById('ajax_opt_'+zone_country_id_array[i])){
			document.getElementById('ajax_opt_'+zone_country_id_array[i]).innerHTML='';
			document.getElementById('ajax_opt_'+zone_country_id_array[i]).style.display='none';
			if(document.getElementById('opt_'+zone_country_id_array[i]))
				document.getElementById('opt_'+zone_country_id_array[i]).style.display='';
			}
		}
	if(document.getElementById('ajax_opt_'+opt_id)){	
		document.getElementById('ajax_opt_'+opt_id).style.display='';
		document.getElementById('ajax_opt_'+opt_id).innerHTML=document.getElementById('ajax_image_load').innerHTML; 
	}
	var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=opt_load_details&assoc_id='); ?>"+opt_id+"&zone_id="+zone_id;
	do_get_command(command);
			document.getElementById('new_zone_value_content').style.display='none';
	}
	function close_option_value(opt_id){
		var tr1=document.getElementById("img_ids");
		var imgsrc;
		var node;
		if(tr1)
		for(var j=0;j<tr1.cells.length;j++){
			td=tr1.cells[j];
			imgsrc='';
			for(var i=0;i<td.childNodes.length;i++){
				node=td.childNodes[i];
				if(node.tagName=='IMG'){
					imgsrc=node.src;
					if(imgsrc==img_src+'template/img_edit_hover.gif')
					   td.childNodes[i].src = img_src+'template/img_edit.gif';
					else if(imgsrc==img_src+'template/img_trash_hover.gif')
					   td.childNodes[i].src = img_src+'template/img_trash.gif';
					break;
				}
			}
		}
		document.getElementById('ajax_opt_'+opt_id).innerHTML='';
		document.getElementById('opt_'+opt_id).style.display='';
		document.getElementById('icon_operation').style.display='none';
		document.getElementById('header_operation_id').style.display='';
		document.getElementById('header_edit_display').style.display='none';
		document.getElementById('header_delete_display').style.display='none';
	}
	function zone_focus(zone_id) {
		row_focus(zone_id);
	}
	function row_focus(zone_id){ 
		zid=zone_id;
		close_new_zone(); 
		for(i=0,len=zones_id_array.length;i<len;i++){
			if(zone_id!=zones_id_array[i] && document.getElementById('ajax_'+zones_id_array[i])){
			document.getElementById('ajax_'+zones_id_array[i]).innerHTML='';
			document.getElementById('ajax_'+zones_id_array[i]).style.display='none';
			document.getElementById('attrib_'+zones_id_array[i]).style.display='';
			}
		}
		if(document.getElementById('ajax_'+zone_id)){
		document.getElementById('ajax_'+zone_id).style.display='';
		document.getElementById('ajax_'+zone_id).innerHTML=document.getElementById('ajax_image_load').innerHTML; 
		}
		//document.goto.to_zone_id.value=zone_id; 
	var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=load_details&zone_id='); ?>"+zone_id;
	do_get_command(command);
	
	}
	function save_geo_zones(option_id){
	var geo_zone_name=document.getElementById('geo_zone_name').value;
	var geo_zone_description=document.getElementById('geo_zone_description').value;
	error=false; error_result="";
	if(geo_zone_name=="" || str_trim(geo_zone_name)=="") {
		error=true;
		error_result="<?php echo ERR_TEXT_ZONE_NAME; ?>\n";
	}
	else if(geo_zone_description=="" || str_trim(geo_zone_description)=="") {
		error=true;
		error_result="<?php echo ERR_TEXT_ZONE_DESCRIPTION; ?>\n";
	}
		if(error) {
			alert(error_result);
		}
		else {
		var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=save_geo_zones&zone_id='); ?>"+option_id+'&geo_zone_name='+geo_zone_name+'&geo_zone_description='+geo_zone_description;
		document.getElementById('attrib_'+option_id).style.background="#FFFFFF";
		document.getElementById('attrib_'+option_id).style.display='';
		//document.getElementById('ajax_'+option_id).innerHTML=document.getElementById('ajax_image_load').innerHTML;
		do_get_command(command);
		}
	}
	function save_zone_countries(opt_id){
	var zone_country_id=document.getElementById('zone_country_id').value;
	var sel_zone_id=document.getElementById('sel_zone_id').value;
	var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=save_zone_countries&assoc_id='); ?>"+opt_id+'&zone_country_id='+zone_country_id+'&sel_zone_id='+sel_zone_id;
	document.getElementById('opt_'+opt_id).style.background="#FFFFFF";
	document.getElementById('opt_'+opt_id).style.display=''; 
//	document.getElementById('ajax_opt_'+opt_id).innerHTML=document.getElementById('ajax_image_load').innerHTML;
	do_get_command(command);	
	}
	function drop_geo_zones(option_id){
	document.getElementById('header_delete_display').style.display='';
	var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=drop_geo_zones&zone_id='); ?>"+option_id;
	do_get_command(command);
	}
	
	function drop_zone_countries(opt_id,z_id){
	document.getElementById('header_delete_display').style.display='';
	var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=drop_zone_countries&assoc_id='); ?>"+opt_id+"&zone_id="+z_id;
	do_get_command(command);
	}
	function drop_fail(){
	document.getElementById('attrib_delete_flag').innerHTML='';
	document.getElementById('attrib_delete_flag').style.display='';
	}
	
	function page_navigation(arg1,agr2,agr3,agr4){
		var page_no=1;
		if(agr2==1) var obj=document.getElementById("page_1");
		else if(agr2==2) var obj=document.getElementById("page_2");
		if(agr2 && agr4){
			if(obj){
 	 			page_no=parseInt(obj.options[obj.selectedIndex].value);
 	 		if(agr4=='next') {
 	 			if(obj.length==obj.selectedIndex+1) return;
 	 			page_no+=1;
 	 		}else if(agr4=='prev') {
 	 			if(obj.selectedIndex==0) return;
 	 			page_no-=1;
 	 		}
 	 		if(page_no<=1) page_no=1;
 	 		}
 	 		
		}
		if(agr2==1) command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=page_navigation_zone'); ?>&page="+page_no;
		else if(agr2==2) command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=page_navigation_attribute'); ?>&page="+page_no;		
		if(command!="") do_get_command(command);
	}	
	function do_result(result){
	var token=result.split('^');
	var page_no=1;
	var option_id=token[1]; var opt_id=token[1]; var attributes_id=token[1];
		switch(token[0]){
		case 'load_details':
			document.getElementById('attrib_'+option_id).style.display='none';
			document.getElementById('ajax_'+option_id).innerHTML=token[2];
			break;
		case 'save_geo_zones':
			document.getElementById('attrib_'+option_id).innerHTML=token[2];
			document.getElementById('attrib_'+option_id).style.display='';
			document.getElementById('ajax_'+option_id).innerHTML='';
			document.getElementById('ajax_'+option_id).style.display='none';
			break;
		case 'drop_geo_zones':
			var row_index=document.getElementById('geo_zone_row_'+option_id).rowIndex;
			document.getElementById('header_delete_display').style.display='';
			document.getElementById('services_option_table').deleteRow(row_index);
			if(document.getElementById("page_1")){
 	 			page_no=parseInt(document.getElementById("page_1").options[document.getElementById("page_1").selectedIndex].value); 	 			
 	 			if(page_no<=1) page_no=1;
 	 		}
			var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=page_navigation_zone');?>&page="+page_no;
			do_get_command(command);
			break;
		case 'drop_fail_services_options':
			document.getElementById('header_delete_display').style.display='none';
			document.getElementById('attrib_delete_flag').style.display='';
			document.getElementById('attrib_delete_flag').innerHTML="<?php echo TEXT_WARNING_OF_DELETE; ?>";
			setTimeout('drop_fail()',2000);
			var tr1=document.getElementById("img_ids");
			var imgsrc;
			var node;
			if(tr1)
			for(var j=0;j<tr1.cells.length;j++){
				td=tr1.cells[j];
				imgsrc='';
				for(var i=0;i<td.childNodes.length;i++){
					node=td.childNodes[i];
					if(node.tagName=='IMG'){
						imgsrc=node.src;
						if(imgsrc==img_src+'template/img_edit_hover.gif')
						   td.childNodes[i].src = img_src+'template/img_edit.gif';
						else if(imgsrc==img_src+'template/img_trash_hover.gif')
						   td.childNodes[i].src = img_src+'template/img_trash.gif';
						break;
					}
				}
			}
			break;	
		case 'load_opt_details':
			document.getElementById('opt_'+opt_id).style.display='none';
			document.getElementById('ajax_opt_'+opt_id).innerHTML=token[2];
			break;
		case 'save_zone_countries':
			document.getElementById('opt_row_'+opt_id).innerHTML=token[2];
			document.getElementById('ajax_opt_'+opt_id).innerHTML='';
			document.getElementById('ajax_opt_'+opt_id).style.display='none';
			break;
		case 'save_new_zone':
			if(document.getElementById("page_1")){
 	 			page_no=parseInt(document.getElementById("page_1").options.length)+1;
 	 			if(page_no<=1) page_no=1;
 	 		}
			command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=page_navigation_zone'); ?>&page="+page_no;
			do_get_command(command);
			break;
		case 'save_new_zone_value': 
			close_new_zone_value();
			document.getElementById('option_value_details_content').innerHTML=token[1];
			zone_country_id_array[zones_id_array.length-1]=token[2];
			document.getElementById('icon_operation').style.display='none';
			document.getElementById('header_operation_id').style.display='';
			break;
		case 'drop_zone_countries':
				close_new_zone_value();
				if(token[2]!="" && token[2]=='show_header_icon')
						document.getElementById('icon_operation').style.display='';
				document.getElementById('opt_row_'+opt_id).innerHTML='';
				document.getElementById('ajax_opt_'+opt_id).innerHTML='';
				document.getElementById('ajax_opt_'+opt_id).style.display='none';
				document.getElementById('header_edit_display').style.display='none';
				document.getElementById('header_delete_display').style.display='none';

			break;
		case 'page_navigation_zone':			
			document.getElementById('main_options_value_table').innerHTML=token[1];
			break;	
		}
	}
	
	function do_hover_change(element,eve){
		 var edit_display=document.getElementById('header_edit_display');
		 var delete_display=document.getElementById('header_delete_display');
	  	 if(element){		   		    		    
		    var img_get_src=element.childNodes[0];
			if(img_get_src){
			    if(img_get_src.src==img_src+'template/img_edit.gif') {
					if(edit_display && edit_display.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_edit_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_edit_hover.gif' && edit_display && edit_display.style.display=="none") img_get_src.src=img_src+'template/img_edit.gif';
			    if(img_get_src.src==img_src+'template/img_trash.gif') {
					if(delete_display && delete_display.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_trash_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_trash_hover.gif' && delete_display && delete_display.style.display=="none") img_get_src.src=img_src+'template/img_trash.gif';
		  }
		}		 
	}
	
	function do_hover_change1(element,eve){
		 var delete_display=document.getElementById('delete_options');
	  	 if(element){		   		    		    
		    var img_get_src=element.childNodes[0];
			if(img_get_src){
			    if(img_get_src.src==img_src+'template/img_trash.gif') {
					if(delete_display && delete_display.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_trash_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_trash_hover.gif' && delete_display && delete_display.style.display=="none") img_get_src.src=img_src+'template/img_trash.gif';
		  }
		}		 
	}



function change_new_image(){
document.getElementById('header_new_operation').firstChild.src=document.getElementById('header_new_operation').firstChild.src.replace('_hover.gif','.gif');
}
	
	function new_zone(option_id){
	display_flag=0;
	change_new_image();
		if(zid!="") {
			new_zone_value(cid);
			return;
		}	
		close_new_zone();
		if(document.getElementById('attrib_'+option_id) && document.getElementById('ajax_'+option_id) ){
			document.getElementById('ajax_'+option_id).innerHTML='';
			document.getElementById('ajax_'+option_id).style.display='none';
			document.getElementById('attrib_'+option_id).style.display='';
		}
		if(document.getElementById('new_zone_content')) document.getElementById('new_zone_content').style.display='';
		document.getElementById('header_new_operation').firstChild.src=document.getElementById('header_new_operation').firstChild.src.replace('.gif','_hover.gif');
		
	}
	function new_zone_value(opt_id){
		if(document.getElementById('opt_'+opt_id) && document.getElementById('ajax_opt_'+opt_id)){
			document.getElementById('opt_'+opt_id).style.display='';
			document.getElementById('ajax_opt_'+opt_id).innerHTML='';
			document.getElementById('ajax_opt_'+opt_id).style.display='none';
			}
		if(document.getElementById('new_zone_value_content'))
		 document.getElementById('new_zone_value_content').style.display='';
		 document.getElementById('header_new_operation').firstChild.src=document.getElementById('header_new_operation').firstChild.src.replace('.gif','_hover.gif');
	}
	function save_new_zone(){
	var geo_zone_name=document.getElementById('geo_zone_name').value;
	var geo_zone_description=document.getElementById('geo_zone_description').value;

	error=false; error_message="";
		if(geo_zone_name=='' || str_trim(geo_zone_name)==''){
		error=true; error_message="<?php echo ERR_TEXT_ZONE_NAME; ?>\n";
		}
		else if(geo_zone_description=='' || str_trim(geo_zone_description)==''){
		error=true; error_message="<?php echo ERR_TEXT_ZONE_DESCRIPTION; ?>\n";	
		}
		if(error==true)
			alert(error_message);
		else{
		//change_new_image();
		var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=save_new_zone&zone_name='); ?>"+geo_zone_name+"&zone_description="+geo_zone_description;
		close_new_zone();
		do_get_command(command);
		}
	}
	function save_new_zone_value(opt_id){
	display_flag=1;
	var zone_country_id=document.getElementById('zone_country_id').value;
	var sel_zone_id=document.getElementById('sel_zone_id').value;
		if(zone_country_id=='')
			alert('<?php echo EMPTY_OPTION_VALUE; ?>');
		else{
		//change_new_image();
		var command="<?php echo tep_href_link(FILENAME_GEO_ZONES,'command=save_new_zone_value&zone_country_id='); ?>"+zone_country_id+"&zone_id="+opt_id+"&sel_zone_id="+sel_zone_id;
		do_get_command(command);
		}
	}
	function close_new_zone(){
	
	//change_new_image();
	if(document.getElementById('new_zone_content')) document.getElementById('new_zone_content').style.display='none';
	if(document.getElementById('new_zone_name')) document.getElementById('new_zone_name').value='';
	document.getElementById('header_new_operation').firstChild.src=document.getElementById('header_new_operation').firstChild.src.replace('_hover.gif','.gif');
	}
	function close_new_zone_value(){
	//change_new_image();
	display_flag=1;
	document.getElementById('new_zone_value_content').style.display='none';
	document.getElementById('zone_country_id').value='';
	document.getElementById('sel_zone_id').value='';
	document.getElementById('header_new_operation').firstChild.src=document.getElementById('header_new_operation').firstChild.src.replace('_hover.gif','.gif');
	}
	function attributes_edit(){
	document.getElementById('edit_details_id').style.display='';
	document.getElementById('header_edit_display').style.display='';
	document.getElementById('header_delete_display').style.display='none';
	
	}
</script>
<script language=JavaScript>

function base_zone_close(option_id){
	document.getElementById('attrib_'+option_id).style.display='';
	document.getElementById('ajax_'+option_id).innerHTML='';
	document.getElementById('ajax_'+option_id).style.display='none';
}
</script>   
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- Ajax Work Starts -->
<table border="0" width="100%" cellpadding="2" cellspacing="0">
<tr><td align="right">
	<form name="goto" action="shop_geo_zones.php" method="post">
	<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr class="dataTableHeadingRow">
	<td class="dataTableHeadingContent" width="200"><?php echo '<a id="header_new_operation" href="javascript:new_zone();" onMouseOver="javascript:do_hover_change2(this,2);" onMouseOut="javascript:do_hover_change2(this,1);" >'.tep_image(DIR_WS_IMAGES.'template/img_new.gif','New Zone','22','22').'</a>'; ?></td>
	</tr>
	</table>
	</form>
</td></tr>
<tr class="dataTableRow">
	<td width="100%" class="main"><b><?php echo HEADING_TITLE; ?></b></td>
</tr>
<tr>
<td id="main_options_value_table">
	<?php echo geo_zone_list();?>			
</td>
</tr>
	
</table>
<div id="ajax_image_load" style="display:none;"><?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?></div>

<div id="new_service_attribute_content" style="display:none;"></div>
<!-- Ajax Works Ends -->
<?php $zone_query=tep_db_query("Select * from geo_zones");
if(tep_db_num_rows($zone_query)<=0) {
?>
<script language="javascript">
	document.getElementById('new_zone_content').style.display='';
</script>
<?php } ?>
<!-- body_text_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>



<?php
function geo_zone_list(){  
	global	$languages_id,$page;
	$zones_query_raw = "select geo_zone_id, geo_zone_name, geo_zone_description, last_modified, date_added from " . TABLE_GEO_ZONES . " order by geo_zone_name";
    $zones_split = new splitPageResultsEvent($page, MAX_DISPLAY_SEARCH_RESULTS, $zones_query_raw, $zones_query_numrows);
    $zones_query = tep_db_query($zones_query_raw);
	$row_count=0;	
	?>
	<div style="color:#FF0000;" id="attrib_delete_flag"></div>   	
	<table id="services_option_table" width="100%" cellpadding="2" cellspacing="2" border="0">
	<?php $row=1; while($zones_array=tep_db_fetch_array($zones_query)){ 
		if($row%2==0) $class_name="dataTableRowEven"; else $class_name="dataTableRowOdd";
		$row++;
	?>
	<tr id="geo_zone_row_<?php echo $zones_array["geo_zone_id"]; ?>">
	<td class="main">
	<div id="attrib_row_<?php echo $zones_array["geo_zone_id"]; ?>" onClick="javascript:row_focus(<?php echo $zones_array["geo_zone_id"]; ?>);">
	<div onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);" id="attrib_<?php echo $zones_array["geo_zone_id"]; ?>" class="<?php echo $class_name; ?>"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif') .'&nbsp;&nbsp;'. $zones_array["geo_zone_name"]; ?></div>
	</div>
	<div id="ajax_row_<?php echo $zones_array["geo_zone_id"]; ?>"></div>
	<div id="ajax_<?php echo $zones_array["geo_zone_id"]; ?>" style="display:none;"></div>
	</div> 
	</td>
	</tr>
	<?php } ?>	
	</table>
	<table border="0" width="100%" cellpadding="0"cellspacing="2">
		<tr>
      	<td colspan="4">
   	   	<table border="0" width="100%" cellpadding="0"cellspacing="2">
		 <tr>
            <td class="smallText" valign="top"><?php echo $zones_split->display_count($zones_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $page, TEXT_DISPLAY_NUMBER_OF_TAX_ZONES); ?></td>
            <td class="smallText" align="right"><?php echo $zones_split->display_script_links($zones_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $page,1,'page_navigation(\'\'','1'); ?></td>
          </tr>
	   </table>
	  </td>
	</tr>
	</table>
	<div id="new_zone_content" style="display:none;">
	<table width="100%" class="openContent"  cellpadding="2" cellspacing="0" border="0">
	<tr class="openContent_top">
		<td class="main"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;New Zone';	?></td>
		<td align="right"><?php echo '<a href="javascript:void save_new_zone();">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','Save').'</a>&nbsp;&nbsp;<a id="close_new_zone_id" href="javascript: close_new_zone();">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','Close').'</a>'; ?></td>
	</tr>
	<tr><td align="center" class="main"><?php echo TEXT_INFO_ZONE_NAME . '&nbsp;&nbsp;' . tep_draw_input_field('geo_zone_name'); ?></td><td align="left" class="main"><?php echo TEXT_INFO_ZONE_DESCRIPTION . '&nbsp;&nbsp;' . tep_draw_input_field('geo_zone_description'); ?></td></tr>
	<tr><td colspan="2"><br></td></tr>
	</table>
	</div>
<?php }
?>

<script language="javascript">
	var display_flag=1;
		function do_hover_change2(element,mode){
		
		if(display_flag==1){
		if (element.mode && mode==element.mode) return;
		if (mode==2)
			element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
		else
			element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
		}
		element.mode=mode;
		}
</script>

