<?php
/*
osCommerce, Open Source E-Commerce Solutions 
http://www.oscommerce.com 

Copyright (c) 2003 osCommerce 

osCommRes, Services Online 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
    require('includes/application_top.php');
	$command = (isset($HTTP_GET_VARS['command']) ? $HTTP_GET_VARS['command'] : '');
	$server_date = getServerDate(true);
	$message=strtolower($message);
	if(strpos($message,'http:')!==false || strpos($message,'https:')!==false || strpos($message,'www')!==false)  $msg_error .= $lang['Error_submission'];
	$select_module='';
	$module=array();
	$position='';
	if ($command!=''){
		$module=isset($HTTP_GET_VARS["module"])?$HTTP_GET_VARS["module"]:'';
		$set=isset($HTTP_GET_VARS["set"])?$HTTP_GET_VARS["set"]:'';
		$installed_modules=isset($HTTP_GET_VARS["installed_modules"])?$HTTP_GET_VARS["installed_modules"]:'';
		$sort_orders=isset($HTTP_GET_VARS["sort_orders"])?$HTTP_GET_VARS["sort_orders"]:'';

		get_module_details($set);
		// fetch for the AJAX details
		echo 'command||'. $command .'{}module||' . $module;
		$commands[]=$command;

		for ($icnt=0;$icnt<count($commands);$icnt++){
			switch($commands[$icnt]){
				case "ajx_save_round_factor":					
					tep_db_query("update configuration set configuration_value='".$HTTP_POST_VARS['configuration_value']."' where configuration_id='".$HTTP_POST_VARS['config_id']."'");
					echo '{}round_factor';
					exit;
				break;
				case "ajx_get_zone_countries":
					$cfg_value=isset($HTTP_GET_VARS["cfg_value"])?$HTTP_GET_VARS["cfg_value"]:'';
					$cfg_key=isset($HTTP_GET_VARS["cfg_key"])?$HTTP_GET_VARS["cfg_key"]:'';
					$cfg_zone=isset($HTTP_GET_VARS["cfg_zone"])?$HTTP_GET_VARS["cfg_zone"]:'';
					echo '{}cfg_key||' . $cfg_key . '{}result_content||' . tep_cfg_pull_down_zone_except_countries($cfg_zone,$cfg_value,$cfg_key);
					exit;
				case "ajx_remove_module_elements":
					remove_module($module);
					set_installed_modules($module,"remove");
					$commands[]="ajx_get_module_elements";
					break;
				case "ajx_install_module_elements":
					$temp=install_module($module);
					echo "{}sort_order||" .$temp;
					set_installed_modules($module,"install",$temp);
					$commands[]="ajx_get_module_elements";
					break;
				case "ajx_get_module_elements":
					$edit_menu=false;
					echo '{}result_content||';
					echo out_result_module_elements($module);
					break;
				case "ajx_get_module_elements_edit":
					$edit_menu=true;
					echo '{}result_content||';
					echo out_result_module_elements_edit($module);
					break;
				case "ajx_move_up_module":
				case "ajx_move_down_module":
					$pos=(isset($HTTP_GET_VARS['pos'])?(int)$HTTP_GET_VARS['pos']:0);
					move_posupdown($commands[$icnt],$module,$pos);
					set_installed_modules($module,"default");
					exit;
				case "ajx_sort_module_elements":
					$configuration=isset($HTTP_GET_VARS["configuration"])?$HTTP_GET_VARS["configuration"]:array();
					if (is_array($configuration) && count($configuration)>0){
						$sql="";
						reset($configuration);
						while (list($key,$value)=each($configuration)){
							echo "UPDATE configuration set configuration_value=" . $value . " where configuration_key='" . $key . "';\n";
							tep_db_query("UPDATE configuration set configuration_value=" . $value . " where configuration_key='" . $key . "';\n");
						}
					}
					set_installed_modules($module,"default");
					exit;
				case "ajx_save_module_elements":
					save_module($module);
					$commands[]="ajx_get_module_elements";
					break;
			}
		}
		echo '{}result_title||';
		echo out_result_module_title($edit_menu) . "{}position||" . $position;
		exit;
	}

	$module_details=array();
	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	// find the set we are working
	$installed_modules=array();
	tep_get_last_access_file();
	$module_directory='';
	$set = (isset($HTTP_GET_VARS['set']) ? $HTTP_GET_VARS['set'] : '');
	if (tep_not_null($set)) {
		switch ($set) {
			case 'shipping':
				$module_type = 'shipping';
				$module_directory = DIR_FS_CATALOG_MODULES . 'shipping/';
				$module_key = 'MODULE_SHIPPING_INSTALLED';
				$modules_installed=MODULE_SHIPPING_INSTALLED;
				$total_key='MODULE_SHIPPING_%_SORT_ORDER';
				define('HEADING_TITLE', HEADING_TITLE_MODULES_SHIPPING);
				break;
			case 'ordertotal':
				$module_type = 'order_total';
				$module_directory = DIR_FS_CATALOG_MODULES . 'order_total/';
				$module_key = 'MODULE_ORDER_TOTAL_INSTALLED';
				$modules_installed=MODULE_ORDER_TOTAL_INSTALLED;
				$total_key='MODULE_ORDER_TOTAL_%_SORT_ORDER';
				define('HEADING_TITLE', HEADING_TITLE_MODULES_ORDER_TOTAL);
				break;
			case 'sms':
				$module_type='sms';
				$module_directory=DIR_FS_CATALOG_MODULES . 'sms/';
				$module_key = 'MODULE_SMS_GATEWAYS_INSTALLED';
				$modules_installed=MODULE_SMS_GATEWAYS_INSTALLED;
				$total_key='MODULE_SMS_%_SORT_ORDER';
				define('HEADING_TITLE', HEADING_TITLE_MODULES_SMS_GATEWAYS);
				break;
			case 'fulfiller':
				$module_type='fulfiller';
				$module_directory=DIR_FS_CATALOG_MODULES . 'fulfiller/';
				$total_key='MODULE_FULFILLER_%_SORT_ORDER';
				define('HEADING_TITLE', HEADING_TITLE_MODULES_FULFILLER_GATEWAYS);
				break;
			case 'payment':
			default:
				$module_type = 'payment';
				$module_directory = DIR_FS_CATALOG_MODULES . 'payment/';
				$module_key = 'MODULE_PAYMENT_INSTALLED';
				$modules_installed=MODULE_PAYMENT_INSTALLED;
				$total_key='MODULE_PAYMENT_%_SORT_ORDER';
				define('HEADING_TITLE', HEADING_TITLE_MODULES_PAYMENT);
				break;
		}
	}
?>
<!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/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="javascript" src="includes/aim.js"></script>
<script>
	function save_round_factor(){
		command="<?php echo tep_href_link(FILENAME_MODULES,'set=' . $set);?>&command=ajx_save_round_factor";
		do_post_command('frm_round_factor',command);
		if(document.getElementById("img_loading")) document.getElementById("img_loading").style.display="";
	}
	function validate_image(){
		var frm=document.modules;
		var error ="";
		var payment_logo = "";
		for(i=0;i<document.modules.elements.length;i++){
			if(document.modules.elements[i].type.toLowerCase()=="file"){
				payment_logo = document.modules.elements[i];
			}
		}
		var image_types=Array(".png",".jpg",".jpeg",".gif");
		if (payment_logo!="" && payment_logo.value!="" && !check_mime_type(payment_logo,image_types)){
			error="* <?php echo ERR_IMAGE_UPLOAD_TYPE;?>\n";
		}
		if(error!=""){
			alert(error);
			return false;
		}
		return true;
	}
	function getZones(obj) {
	if(document.getElementById(obj)) {
		zid=document.getElementById(obj).options[document.getElementById(obj).selectedIndex].value;
		document.location.href="products_freight.php?zID=" + zid;	
		}	
	}
	function check_selected_values(configcheckbox,controlname,countrycontrol,key){
		var element=document.getElementById(controlname);
		var elementZone=document.getElementById(controlname);
		var checkbox;
		if (is_fetching) return;
		if (!element) return;
		var result='';
		var icnt;
		var count=parseInt(document.getElementById("count_"+controlname).value);

		for (icnt=0;icnt<=count;icnt++){
			checkbox=document.getElementById("check_"+icnt+"_"+controlname);
			if (checkbox.checked){
				result+=','+checkbox.value;
			}
		}
		result=result.substr(1);

		element.value=result;

		var elementCountry=document.getElementById(countrycontrol);

		if (!elementCountry) return;

		var row_content=document.getElementById("ROW_"+key);

		row_content.innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
		
		command="<?php echo tep_href_link(FILENAME_MODULES,'set=' . $set);?>&command=ajx_get_zone_countries&cfg_key="+key+"&cfg_zone="+elementZone.value+"&cfg_value="+elementCountry.value+"&module="+prev_module;
		do_page_ajax(command);
	}
	var is_fetching=false;
	function do_page_ajax(command){

		if (is_fetching) return;
		is_fetching=true;
		do_get_command(command);
	}
   function do_hover_change(element,eve){ 
	 var span_details=document.getElementById("span_details"); 
	 var span_edit=document.getElementById("span_edit");
	 var span_delete=document.getElementById("span_delete");
	 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(span_edit && span_edit.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' && span_edit && span_edit.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(span_delete && span_delete.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' && span_delete && span_delete.style.display=="none") img_get_src.src=img_src+'template/img_trash.gif';
	  }
	 }		 
  }  	
	
</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 //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr> 
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
		  	<td width="10"></td>
		  	<td width="20">
				<?php 
					echo tep_image(DIR_WS_IMAGES . 'template/icon_ascending.gif',IMAGE_ASCENDING,'','',' id="pop_common_sort_display_con" onMouseOver="javscript:javascript:show_cpopup(\'sort_display\',0,20);"');
					echo '<input type="hidden" name="show_sort_order" id="show_sort_order" value="AN">';
					echo "<div id='pop_common_sort_display' style='position:absolute;display:none;width:100px;height:50px;z-index:999;border:1px solid;border-color:#6883CA;background-color:#FFFFFF;padding:3px;'>";
					echo tep_draw_separator('pixel_trans.gif','10','5');
					echo '<br><table class="main" cellpadding="0" cellspacing="0">';
					$popup=array(array("img"=>'icon_ascending.gif',"txt"=>TEXT_ASCENDING,'id'=>'A'),array("img"=>'icon_descending.gif',"txt"=>TEXT_DESCENDING,'id'=>'D'));
				
					for ($icnt=0,$n=count($popup);$icnt<$n;$icnt++){
						echo '<tr height="20" class="tableMenuItem" onMouseOver="javascript:rowOverEffect(this)" onMouseOut="javascript:rowOutEffect(this)"><td valigm="middle" width="18">' . tep_image(DIR_WS_IMAGES . "template/" .$popup[$icnt]["img"],$popup[$icnt]["txt"],'','','align=absmiddle') . '</td><td>';
						echo '<a style="color:#000000;text-decoration:none" href="javascript:sort_module_rows(\'' . $popup[$icnt]["id"] . '\');">' . $popup[$icnt]["txt"] . '</a></td></tr>';
						echo '<tr height="3"><td></td></tr>';
					}
					echo "</td></tr></table></div>";
				?>
			</td>
			<td class="dataTableHeadingContent"><?php echo HEADING_TITLE; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0" id="panel_module">
<?php
	$installed_list=array();
	$file_extension = substr($PHP_SELF, strrpos($PHP_SELF, '.'));
	$directory_array = array();
	if ($dir = @dir($module_directory)) {
		while ($file = $dir->read()) {
			if (!is_dir($module_directory . $file)) {
				if (substr($file, strrpos($file, '.')) == $file_extension) {
					$dir_array[] = $file;
					$class = substr($file, 0, strrpos($file, '.'));
				//echo $class.' ';	
					if (!tep_class_exists($class)) {
						include(DIR_FS_CATALOG_LANGUAGES . $language . '/modules/' . $module_type . '/' . $file);
						include(CHECK_CON . $module_directory . $file);
					}
					if(tep_class_exists($class)) {
						$module = new $class;
						if ($module->check() > 0) {
							if (isset($module->sort_order) && (int)$module->sort_order > 0){
								$directory_array[]=sprintf("%04s",$module->sort_order)  . $file;
								$installed_modules[]=sprintf("%04s",$module->sort_order)  . $file;
							} else {
								$directory_array[]="AAAA" . $file;
								$installed_modules[]="AAAA" . $file;
							}
							
						} else {
							$directory_array[]="AAAA" . $file;
						}
						$modules[$file]=$module;
					}
				}
			}
		}
		sort($directory_array);
		$dir->close();
	}
//print_r($directory_array);

  for ($i=0, $n=sizeof($directory_array); $i<$n; $i++) {  	
  	$fileo= $directory_array[$i];
    $file = substr($directory_array[$i],4);
    $class = substr($file, 0, strrpos($file, '.'));

      $module = $modules[$file];
 
      if ((!isset($HTTP_GET_VARS['module']) || (isset($HTTP_GET_VARS['module']) && ($HTTP_GET_VARS['module'] == $class))) && !isset($mInfo)) {
	
      }
        echo '              <tr id="panel_' . $class . '_row" class="' . (($i+1)%2==0?'dataTableRowEven':'dataTableRowOdd') .  '" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="javascript:toggle_module_panel(this,\'' . $class . '\')">' . "\n";

		$key_array=$module->keys();
		$sort_key='';
		while(list(,$value)=each($key_array)){
			if(strpos($value,'_SORT_ORDER')!==false){
				$sort_key=$value;
				break;
			}
		}
	  
?>
				<td valign="top">
				<div id="panel_<?php echo $class;?>_title">
					<table border="0" cellpadding="0" cellspacing="0" height="25">
						<tr>				
							<td width="10"></td>
							<td class="main" width="20" valign="middle">
								<?php 
									  if (substr($fileo,0,4)!="AAAA") {
										echo tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', IMAGE_INSTALLED, '', '','align=absmiddle');
										$position="normal";
									  } else {
									  	$position="inactive";
										echo tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', IMAGE_REMOVED, '', '','align=absmiddle');
									  }
								?>
							</td>
							<td class="dataTableContent" valign="middle"><?php echo ($set=="payment"?$module->text_title:$module->title); ?></td>
						</tr>
					</table>
				</div>
				<div id="panel_<?php echo $class;?>_content" style="display:none" class="openContent1">
				</div>
				
				</td>
				
              </tr>
			  <script language="javascript">document.getElementById("panel_<?php echo $class;?>_row").cur_position='<?php echo $position;?>';document.getElementById("panel_<?php echo $class;?>_row").sort_order='<?php echo $module->sort_order;?>';document.getElementById("panel_<?php echo $class;?>_row").sort_key='<?php echo $sort_key;?>';
			  document.getElementById("panel_<?php echo $class;?>_row").item_text='<?php echo ($set=="payment"?$module->text_title:$module->title);?>';
			  document.getElementById("panel_<?php echo $class;?>_row").opened=0;
			  </script>
<?php } 
  if($set=='ordertotal'){
  $amount_round_value=array();
  $amount_round_query=tep_db_query("select * from configuration where configuration_key='EVENTS_ORDER_AMOUNT_ROUND'");
  if(tep_db_num_rows($amount_round_query)>0) $amount_round_value=tep_db_fetch_array($amount_round_query);
  ?>
  	<tr style="height:15px"><td>&nbsp;</td></tr>
  	<tr class="openContent_top" style="height:35px" >
  	  <td>
  	  	<form name="frm_round_factor" id="frm_round_factor" action="modules.php" method="post">
  	  	 <table border="0" width="100%" cellspacing="2" cellpadding="2">
  	  		<tr><td colspan="3" class='main'>Settings</td></tr>
  	  		<tr style="height:35px">
  	  			<td width="200" valign="center" class="main" alt="<?php echo $amount_round_value['configuration_description'];?>" title="<?php echo $amount_round_value['configuration_description'];?>"><?php echo $amount_round_value['configuration_title'];?></td>
  	  			<td widht="50" align="left"><?php echo tep_cfg_pull_down_round_factors($amount_round_value['configuration_value']);?></td>
  	  			<td width="800" align="left"><span id="img_loading" style="display:none"><?php echo tep_image('images/24-1.gif','Data Saving');?></td>
  	  		</tr>  	  	
  	  	 	</table>
  	  	 <input type="hidden" name="config_id" id="config_id" value="<?php echo $amount_round_value['configuration_id'];?>">
  	    </form>
  	  </td>
  	</tr>    
  <?php
  } 
  sort($installed_modules);   
  for ($icnt=0,$n=count($installed_modules);$icnt<$n;$icnt++){
  	$installed_modules[$icnt]=substr($installed_modules[$icnt],4);
  }
  /*$check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = '" . $module_key . "'");
  if (tep_db_num_rows($check_query)) {
    $check = tep_db_fetch_array($check_query);
    if ($check['configuration_value'] != implode(';', $installed_modules)) {
      tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . implode(';', $installed_modules) . "', last_modified ='" .  $server_date  ."' where configuration_key = '" . $module_key . "'");
    }
  } else {
    tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Installed Modules', '" . $module_key . "', '" . implode(';', $installed_modules) . "', 'This is automatically updated. No need to edit.', '6', '0','" .  $server_date  . "')");
  } */ 
?>
            </table>
			</td>
			
			<script language="javascript">
				var prev_module='';
				var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";
				var row_title_html='<table border="0" cellpadding="0" cellspacing="0" height="25"><tr><td width="10"></td><td class="main" width="20" valign="middle">#STATUS_IMAGE#<td class="dataTableContent" valign="middle" style="padding-top:4px;">#TITLE_TEXT#</td></tr></table>';
				var cur_row='';
				function toggle_module_panel(row,module){
					if (is_fetching) return;
					if (prev_module==module) return;
					var panel=document.getElementById("panel_"+module+"_content");
					var title=document.getElementById("panel_"+module+"_title");
					var row=document.getElementById("panel_"+module+"_row");
					if (panel.style.display=="none"){
						if (prev_module!=''){
							row.opened=0;
							hide_module_panel(prev_module);
						}
						show_module_panel(module);
						prev_module=module;
						row.onclick="";
						do_page_fetch("ajx_get_module_elements",module);
						row.opened=1;
					} else {
						row.position='';
						hide_module_panel(module);
						prev_module='';
						row.opened=0;
					}
				}
				function show_module_panel(module){ 
					var panel=document.getElementById("panel_"+module+"_content");
					var title=document.getElementById("panel_"+module+"_title");
					panel.innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
					panel.style.display="";
					title.className="openContent";
				}
				function hide_module_panel(module){
					
					var panel=document.getElementById("panel_"+module+"_content");
					var title=document.getElementById("panel_"+module+"_title");
					var row=document.getElementById("panel_"+module+"_row");
					
					panel.style.display="none";
					if (row.item_text){
						temp_str=row_title_html;
						rexp=/#TITLE_TEXT#/g;
						temp_str=temp_str.replace(rexp,row.item_text);
						rexp=/#STATUS_IMAGE#/g;

						if (row.position!="inactive")
							temp_str=temp_str.replace(rexp,"<img src='<?php echo DIR_WS_IMAGES;?>template/icon_active.gif' border='0' title='<?php echo IMAGE_INSTALLED;?>' align='absmiddle'>");
						else
							temp_str=temp_str.replace(rexp,"<img src='<?php echo DIR_WS_IMAGES;?>template/icon_inactive.gif' border='0' title='<?php echo IMAGE_REMOVED;?>' align='absmiddle'>");
						title.innerHTML=temp_str;
					}
					
					if ((row.rowIndex+1)%2==0){
						row.className="dataTableRowEven";
					} else {
						row.className="dataTableRowOdd";
					}
					row.onclick=function () {toggle_module_panel(row,module)};
					row.opened=0;
					title.className="";
				}
				function do_page_fetch(cmd,module,pos){ 
					command="<?php echo tep_href_link(FILENAME_MODULES,'set=' . $set);?>&command="+cmd;
					switch(cmd){
						case "ajx_get_module_elements":
						case "ajx_install_module_elements":
							command=command+"&module="+module;
							break;
						case "ajx_get_module_elements_edit":
							if(document.getElementById("span_edit")) document.getElementById("span_edit").style.display="";
							if(document.getElementById("span_delete")) document.getElementById("span_delete").style.display="none";
							command=command+"&module="+module;
							break;
						case "ajx_remove_module_elements":
							if(document.getElementById("span_edit")) document.getElementById("span_edit").style.display="none";
							if(document.getElementById("span_delete")) document.getElementById("span_delete").style.display="";
							command=command+"&module="+module;
							break;
						case "ajx_move_up_module":
						case "ajx_move_down_module":
							command=command+"&module="+module+"&pos="+pos;
							break;
						case "ajx_close_module_elements":
							hide_module_panel(module);
							prev_module='';
							return;
						case "ajx_save_module_elements":
							if (is_fetching) return;
							input_image=document.getElementById('hasImages').value;
							if (input_image=="yes"){
								if (!AIM.submit(document.config_input, {'onStart' : startCallback, 'onComplete' : completeCallback})) return;
								document.config_input.submit();
							} else {
								do_post_command("config_input",command+"&module="+module);
							}
							return;
						case "ajx_sort_module_elements":
							command=command+"&module="+module;
							break;
							
					}
					var modules="";
					var table;
					var modsplt;
					var sort_orders="";
					
					table=document.getElementById("panel_module");
					for (icnt=0,n=table.rows.length;icnt<n;icnt++){
						if (table.rows[icnt].cur_position=="inactive") break;
						 str_pos1 = table.rows[icnt].id.indexOf('_');
						 str_pos2 = table.rows[icnt].id.lastIndexOf('_');
						 str_pos3=str_pos2-str_pos1;
						 modules += table.rows[icnt].id.substr(str_pos1+1,str_pos3-1)+'.php;';
						//modsplt=table.rows[icnt].id.split("_");
						//modules+=modsplt[1]+'_'+modsplt[2]+".php;";
						sort_orders+=table.rows[icnt].sort_order+";";
					}
					if (modules!=''){
						modules=modules.substr(0,modules.length-1);
						sort_orders=sort_orders.substr(0,sort_orders.length-1);
						command+="&installed_modules="+modules+"&sort_orders="+sort_orders;
					
					}
					//do_hover_change(document.getElementById("td_edit"),1);
					do_page_ajax(command);
					
				}
				function do_result(result){
					var cmd;					
					var id;
					var icnt;
					var n;
					var sort_order;
					var cfg_key;
					cmds_splt=result.split("{}");
					for (icnt=0,n=cmds_splt.length;icnt<n;icnt++){
						cmd_splt=cmds_splt[icnt].split("||");
						switch(cmd_splt[0]){							
							case 'round_factor':
							  if(document.getElementById("img_loading")) document.getElementById("img_loading").style.display="none";
							  return;
							break;
							case "module":
								module=cmd_splt[1];
								break;
							case "position":
								document.getElementById("panel_"+module+"_row").position=cmd_splt[1];
								break;
							case "sort_order":
								sort_order=cmd_splt[1];
								break;
							case "command":
								cmd=cmd_splt[1];
								break;
							case "cfg_key":
								cfg_key=cmd_splt[1];
								break;
							case "result_title":
								document.getElementById("panel_"+module+"_title").innerHTML=cmd_splt[1];
								break;
							case "result_content":
								if (cmd=="ajx_get_zone_countries"){
									element=document.getElementById("ROW_"+cfg_key);
									if (!element) break;
									element.innerHTML=cmd_splt[1];
									break;
								}
								document.getElementById("panel_"+module+"_content").innerHTML=cmd_splt[1];
								if (cmd=="ajx_remove_module_elements"){
									move_row(module,'',-1);
									document.getElementById("panel_"+module+"_row").cur_position="inactive";
								} else if (cmd=="ajx_install_module_elements"){
									document.getElementById("panel_"+module+"_row").sort_order=sort_order;
									document.getElementById("panel_"+module+"_row").cur_position="active";
									move_row(module,'',sort_order-1);
									prev_module=module;
								}
								break;
						}
					}
					
					is_fetching=false;
				}
				function move_row(from_module,to_module,pos){
					var icnt;
					var toRow;
					fromRow=document.getElementById("panel_"+from_module+"_row");
					table=fromRow.parentNode;
					var count=get_installed_count(table);

					if (to_module=="" && pos==-1){
						table.appendChild(fromRow);
						realign_row_class(table);
						return;
					} else {
						pos=get_proper_position(table,pos,count);
					}
					if (!pos || pos<0) pos=0;
					toRow=table.rows[pos];
					table.insertBefore(fromRow,toRow);
					for (icnt=1;icnt<=1000;icnt++);

					realign_row_class(table);
				}
				function get_proper_position(table,pos,count){
					var icnt;
					for (icnt=0;icnt<count;icnt++){
						if (pos<=table.rows[icnt].sort_order) break;
					}
					return icnt;
				}
				function get_installed_count(table){
					var icnt;
					var n;
					for (icnt=0,n=table.rows.length;icnt<n;icnt++){
						if (table.rows[icnt].cur_position=="inactive") break;
					}
					return icnt;
				}
				function realign_row_class(table){
					var icnt,n;
					for (icnt=0,n=table.rows.length;icnt<n;icnt++){
						if (table.rows[icnt].opened && table.rows[icnt].opened==1) {
							continue;
						}
						if (table.rows[icnt].className.indexOf("hover")!=-1) continue;
						if ((icnt+1)%2==0){
							table.rows[icnt].className="dataTableRowEven";
						} else {
							table.rows[icnt].className="dataTableRowOdd";
						}
					}
				}
				function move_updown(module,type){
					var row=document.getElementById("panel_"+module+"_row");
					var index=row.rowIndex;
					var count=get_installed_count(row.parentNode);
					var temp;
					if (count<=1) return;
					if (type=="up" && index==0) return;
					if (type=="down" && index==count-1) return;

					var sort_order=row.sort_order;
					if (type=="up"){
						row.parentNode.insertBefore(row,row.parentNode.rows[index-1]);
						temp=row.sort_order;
						row.sort_order=row.parentNode.rows[row.rowIndex+1].sort_order;
						row.parentNode.rows[row.rowIndex+1].sort_order=temp;
					} else {
						if (index==row.parentNode.rows.length-2)
							row.parentNode.appendChild(row);
						else
							row.parentNode.insertBefore(row,row.parentNode.rows[index+2]);
						temp=row.sort_order;
						row.sort_order=row.parentNode.rows[row.rowIndex-1].sort_order;
						row.parentNode.rows[row.rowIndex-1].sort_order=temp;
					}
					do_page_fetch("ajx_move_"+type+"_module",module,sort_order);
					realign_row_class(row.parentNode);
				}
				function sort_module_rows(mode){
					var table=document.getElementById("panel_module");
					var icnt,n,jcnt;
					var text1,text2;
					var params;
					hide_cpopup(true);
					for (icnt=0,n=table.rows.length;icnt<n-1;icnt++){
						if (table.rows[icnt].cur_position=="inactive") break;
						for (jcnt=icnt+1;jcnt<n;jcnt++){
							if (table.rows[jcnt].cur_position=="inactive") break;
							text1=table.rows[icnt].item_text;
							text2=table.rows[jcnt].item_text;
							if (mode=="A" && text2<text1){
								move_by_index(table,icnt,jcnt,'Y');
							} else if (mode=="D" && text2>text1){
								move_by_index(table,icnt,jcnt,'Y');
							}
						}
					}
					for (;icnt<n-1;icnt++){
						for (jcnt=icnt+1;jcnt<n;jcnt++){
							text1=table.rows[icnt].item_text;
							text2=table.rows[jcnt].item_text;
							if (mode=="A" && text2<text1){
								move_by_index(table,icnt,jcnt,'N');
							} else if (mode=="D" && text2>text1){
								move_by_index(table,icnt,jcnt,'N');
							}
						}
					}
					realign_row_class(table.rows[0].parentNode);
					params="";
					for (icnt=0;icnt<n;icnt++){
						if (table.rows[icnt].cur_position=="inactive") break;
						params+="&configuration["+table.rows[icnt].sort_key+"]="+table.rows[icnt].sort_order;
					}
					do_page_fetch("ajx_sort_module_elements",params);
				}
				function move_by_index(table,from,to,active){
					var sort_order1=table.rows[from].sort_order;
					var sort_order2=table.rows[to].sort_order;
					if (Math.abs(from-to)<=1){
						table.rows[to].parentNode.insertBefore(table.rows[to],table.rows[from]);
					} else {
						table.rows[to].parentNode.insertBefore(table.rows[from],table.rows[to]);
						table.rows[to].parentNode.insertBefore(table.rows[to],table.rows[from]);
					}

					
					if (active!='Y') return;
					
					table.rows[from].sort_order=sort_order1;
					table.rows[to].sort_order=sort_order2;
				}
			function startCallback() {
				// make something useful before submit (onStart)
				return true;
			}
	
			function completeCallback(response) {
				// make something useful after (onComplete)
				do_result(response);
			}
			</script>
          </tr>
        </table>
		<div id="ajx_load_content_img" style="display:none"><div style="height:30px;margin-left:30px"><img src="images/24-1.gif"></div></div>
		</td>
      </tr>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php');
		
		function out_result_module_elements($module){ 
			global $select_module,$position,$set;
			if (!load_module($module)) return;
			if (!$select_module->check()){
				$position="inactive";
				$result='<div style="margin-top:-20px;padding-left:30px;vertical-align:top"><table border="0" cellpadding="0" cellspacing="0"  height="40" width="90%">
						<tr>
							<td align="right" class="main"><a href="javascript:do_page_fetch(\'ajx_install_module_elements\',\'' . $module  .'\');">' . tep_image_button('button_module_install.gif',IMAGE_INSTALL) . '</a>&nbsp;</td>
						</tr>
						</table></div>
						';
				return $result;
			}
			$position="active";
			$result='<div style="margin-top:-30px;padding-left:30px;vertical-align:top"><table border="0" cellpadding="0" cellspacing="0" width="100%">
						<tr>
							{{IMAGE_ELEMENT}}
							<td class="main">';// . $select_module->description . '<br><br>
							$result.='<table border="0" cellpadding="0" cellspacing="0" width="100%">
								<tr>
									<td valign="top" width="50%">
									<table border="0" cellpadding="2" cellspacing="0">
					';
			
			$module_keys=$select_module->keys();
			
			//print_r($module_keys);
			$col_total=ceil((count($module_keys)-1)/2);
			$icnt=0;
			$module_sort_order="";
			switch ($set) {
				case 'shipping':
					$module_sort_order='MODULE_SHIPPING_%_SORT_ORDER';	
					break;
				case 'fulfiller':
					$module_sort_order='MODULE_FULFILLER_%_SORT_ORDER';
					break;
				case 'ordertotal':
					$module_sort_order='MODULE_ORDER_TOTAL_%_SORT_ORDER';	
					break;	
				case 'sms':
					$module_sort_order='MODULE_SMS_%_SORT_ORDER';
					break;
				case 'payment':
				default:
					$module_sort_order='MODULE_PAYMENT_%_SORT_ORDER';
					break;	
			}
							
			$key_value_query = tep_db_query("select configuration_title, configuration_value, configuration_description, use_function, set_function from " . TABLE_CONFIGURATION . " where configuration_key  in ('" . join("','",$module_keys) . "') and configuration_key not like '" . $module_sort_order . "' order by sort_order");
			while($key_value = tep_db_fetch_array($key_value_query)){
				if ($icnt>=$col_total){
					$result.='	</table>
								</td>
								<td valign="top" width="50%">
								<table border="0" cellpadding="2" cellspacing="0">
							';
					$icnt=0;
				}
				$config_value=$key_value['configuration_value'];
				
				if ($key_value['use_function']) {
					$use_function = $key_value['use_function'];
					if (ereg('->', $use_function)) {
					  $class_method = explode('->', $use_function);
					  if (!is_object(${$class_method[0]})) {
						include(DIR_WS_CLASSES . $class_method[0] . '.php');
						${$class_method[0]} = new $class_method[0]();
					  }
					  $config_value= tep_call_function($class_method[1], $key_value['configuration_value'], ${$class_method[0]});
					} else {
					  $config_value= tep_call_function($use_function, $key_value['configuration_value']);
					}
				}
				if ($key_value['configuration_title']!='Image'){
					$result.='<tr><td class="main">' . $key_value['configuration_title'] . '</td><td class="main">' . $config_value . '</td></tr>';
					$icnt++;
				} else {
					$mod_image=$config_value;
					$image_array = array(".png",".jpg",".jpeg",".gif");
					for($i=0,$j=sizeof($image_array);$i<$j;$i++){
						if(file_exists(DIR_WS_IMAGES . $config_value . $image_array[$i])){
							$mod_image=$config_value . $image_array[$i];
							$i = $j;
						}
					}
				}

			}
			if ($icnt<=$col_total){
				$result.='	</table>
								</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
							</tr>
							';
			}
			$result.='		</form>
							</table>
							</td>
						</tr>
					</table></div>
					';
			if ($mod_image!='')
				$result=str_replace("{{IMAGE_ELEMENT}}",'<td valign="top" align="center" width="150">' . tep_image(DIR_WS_IMAGES . $mod_image,'',103,33) . '</td>',$result);
			else
				$result=str_replace("{{IMAGE_ELEMENT}}",'',$result);
			return $result;
		}
		function out_result_module_title($edit=false){
			global $select_module,$module,$set;
			$active=$select_module->check();
			$show_image=($active?"icon_active.gif":"icon_inactive.gif");
				$result='
							<table border="0" cellpadding="0" cellspacing="0" width="100%" class="openContent">
								<tr height="40" bgcolor="#EDF1FE"  class="openContent_top" >				
									<td width="10" onclick="javascript:do_page_fetch(\'ajx_close_module_elements\',\'' . $module . '\');"></td>
									<td class="main" width="20" valign="top"  style="padding-top:4px" onclick="javascript:do_page_fetch(\'ajx_close_module_elements\',\'' . $module . '\');">' .	tep_image(DIR_WS_IMAGES . 'template/' . $show_image, IMAGE_INSTALLED) . '</td>
									';

			if ($active){
				$result.='		<td width="40" valign="top"  style="padding-top:4px" ><a href="javascript:move_updown(\'' . $module . '\',\'up\');">' . tep_image(DIR_WS_IMAGES . 'template/img_arrow_up.gif',IMAGE_ORDER_UP,'','','align=top') . '</a>&nbsp;
									<a href="javascript:move_updown(\'' . $module . '\',\'down\');">' . tep_image(DIR_WS_IMAGES . 'template/img_arrow_down.gif',IMAGE_ORDER_DOWN,'','','align=top') . '</a></td>';

				$result.=		'<td class="smallText" valign="top"  style="padding-top:4px" onclick="javascript:do_page_fetch(\'ajx_close_module_elements\',\'' . $module . '\');">' . ($set=="payment"?$select_module->text_title:$select_module->title) . '</td>';
				if ($edit){
					$result.=		'<td width="150" class="main" align="right" style="padding-left:10px;padding-right:10px;padding-top:3px;"><a href="javascript:do_page_fetch(\'ajx_save_module_elements\',\'' . $module . '\');">' . tep_image(DIR_WS_IMAGES .  'template/img_savel.gif',IMAGE_SAVE) .'</a>&nbsp;
										<a href="javascript:do_page_fetch(\'ajx_close_module_elements\',\'' . $module . '\');">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif',IMAGE_CLOSE_EDIT) . '</a>
										</td>
							 ';
				}
				$result.='
								</tr>
							</table>
							<center>
							<table border="0" cellpadding="0" cellspacing="0" style="width:100;height:20;display:inline;position:relative;top:-40px;">
								<tr id="img_ids">
									<td width="10" class="cell_bg_popm_left">&nbsp;</td>' .
									(($edit)? '<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change(this,1);do_page_fetch(\'ajx_get_module_elements_edit\',\'' . $module . '\')";>' . tep_image(DIR_WS_IMAGES . 'template/img_edit_hover.gif',IMAGE_EDIT,'','','id="img_edit"') . '</td>':'<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change(this,1);do_page_fetch(\'ajx_get_module_elements_edit\',\'' . $module . '\')" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/img_edit.gif',IMAGE_EDIT,'','','id="img_edit"') . '</td>') .  
								 //'<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change(this,1);do_page_fetch(\'ajx_get_module_elements_edit\',\'' . $module . '\')" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/'.(($edit)?'img_edit_hover.gif':'img_edit.gif'),IMAGE_EDIT,'','','id="img_edit"') . '</td>
									'<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change(this,1);do_page_fetch(\'ajx_remove_module_elements\',\'' . $module . '\')" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/img_trash.gif',IMAGE_REMOVE) . '</td>
									<td width="10" class="cell_bg_popm_right">&nbsp;</td>
								</tr>
								<tr><td style="display:none" id="span_edit"></td></tr>
								<tr><td style="display:none" id="span_delete"></td></tr>
							</table>
							</center>
						';
			} else {
				$result.=		'<td class="smallText" valign="top" style="padding-top:4px" onclick="javascript:do_page_fetch(\'ajx_close_module_elements\',\'' . $module . '\');">' . ($set=="payment"?$select_module->text_title:$select_module->title) . '</td>
								</td>
								</tr>
							</table>
						';
			}
			return $result;
		}
		function out_result_module_elements_edit($module){
			global $select_module,$position,$set;
			if (!load_module($module)) return;
			$position="edit";
			$result='<div style="margin-top:-30px;padding-left:10px;vertical-align:top;width:100%;"><table border="0" cellpadding="0" cellspacing="0" width="100%">
						<tr>
							<td class="main" width="20">' . tep_image(DIR_WS_IMAGES . 'template/img_edit.gif') .
							'</td><td class="main">' . TEXT_EDIT .
						'</tr>
					</table>
					' . tep_draw_form("config_input",FILENAME_MODULES,'command=ajx_save_module_elements&set=' . $set . '&module=' . $module,'post',' enctype="multipart/form-data"') .
					'<table border="0" cellpadding="0" cellspacing="0" width="100%">
						<tr>
							<td width="20">
							<td class="smallText">
							<table border="0" cellpadding="0" cellspacing="0" width="100%">
								<tr>
									<td valign="top" width="50%">
									<table border="0" cellpadding="2" cellspacing="0">
					';
			$module_keys=$select_module->keys();
			
		//	print_r($module_keys);
			
			$col_total=ceil((count($module_keys)-1)/2);
			//$col_total--;
			$icnt=0;
			$has_images="no";
			$key_value_query = tep_db_query("select configuration_key,configuration_title, configuration_value, configuration_description, use_function, set_function from " . TABLE_CONFIGURATION . " where configuration_key  in ('" . join("','",$module_keys) . "') order by sort_order");
			
		//	echo "select configuration_key,configuration_title, configuration_value, configuration_description, use_function, set_function from " . TABLE_CONFIGURATION . " where configuration_key  in ('" . join("','",$module_keys) . "') order by sort_order";
			
			while($key_value = tep_db_fetch_array($key_value_query)){
				if ($icnt>=$col_total){
					$result.='	</table>
								</td>
								<td valign="top" width="50%">
								<table border="0" cellpadding="2" cellspacing="0">
							';
					$icnt=0;
				}
				$config_input='';

				$key=$key_value["configuration_key"];
				
				if (strpos($key,"_SORT_ORDER")!==false) continue;
				if($key_value['set_function'] == "tep_cfg_user_defined_text_field(1,") {
				  $config_input = tep_cfg_user_defined_text_field(1,'configuration[' . $key . ']', $key_value['configuration_value']);
				} else if($key_value['set_function'] == "tep_cfg_user_defined_text_field(2,"){
				  $config_input = tep_cfg_user_defined_text_field(2,'configuration[' . $key . ']', $key_value['configuration_value']);
				} else if( ($key_value['set_function'] == "tep_cfg_pull_down_zone_classes(") || ($key_value['set_function'] == "tep_cfg_pull_down_shipping_zone_classes(")){
					$temp_split=split("_",$key);
					$country_key=$country_control='default';
					if((strpos($key,"_PAYMENT")!==false || strpos($key,"_SHIPPING")!==false) && strpos($key,"_EXCEPT")===false){
						if($temp_split[3]=='ZONE') 
							$country_key="MODULE_" . $temp_split[1] . "_" . $temp_split[2] . "_EXCEPT_COUNTRY";
						else 
							$country_key="MODULE_" . $temp_split[1] . "_" . $temp_split[2] . "_" . $temp_split[3] ."_EXCEPT_COUNTRY";
						$country_control='configuration[' . $country_key . ']';
					}
					if(($key_value['set_function'] == "tep_cfg_pull_down_shipping_zone_classes("))
					  $config_input = tep_cfg_pull_down_shipping_zone_classes($key_value['configuration_value'],$key, $country_control,$country_key) . '</div>';
					else 
					  $config_input = tep_cfg_pull_down_zone_classes($key_value['configuration_value'],$key, $country_control,$country_key) . '</div>';
				} else if ($key_value['set_function'] == "tep_cfg_file_field(") {
					$config_input = tep_cfg_file_field('configuration[' . $key . ']', $key_value['configuration_value']);
				} else if ($key_value['set_function']) {
				      //echo $key_value['set_function'].'<br>';
					  eval('$config_input .= ' . $key_value['set_function'] . "'" . $key_value['configuration_value'] . "', '" . $key . "');");
				} else {
				  $config_input = tep_draw_input_field('configuration[' . $key . ']', $key_value['configuration_value']);
				}

				$result.='<tr><td class="main" valign="top" width="200"><b>' . $key_value['configuration_title'] . '</b><br>' . $key_value['configuration_description'] . '</td>
								<td width="10"><td class="main" valign="top" nowrap><div id="ROW_' . $key_value["configuration_key"] . '">' . $config_input . '</div></td></tr><tr  height="5"><td>&nbsp;</td></tr>';
				$icnt++;
				if ($has_images!='yes' && strpos($config_input,"type='file'")!==false){
					$has_images="yes";
				}
			}
			
			if ($icnt<=$col_total){
				$result.='	</table>
								</td>
							</tr>
							';
			}
			$result.='<input type="hidden" id="hasImages" value="' . $has_images . '">';
			$result.='
						</table>
						</td>
						</tr>
						</tr>
							  <td >&nbsp;</td>
							  <td style="padding-right:20px" align="right">
								<table border="0" cellpadding="0" cellspacing="0">
									<tr height="30" >
										<td  width="150" valign="top" class="main" align="right"><a href="javascript:do_page_fetch(\'ajx_save_module_elements\',\'' . $module . '\');">' . tep_image(DIR_WS_IMAGES .  'template/img_savel.gif',IMAGE_SAVE) .'</a>&nbsp;
												<a href="javascript:do_page_fetch(\'ajx_close_module_elements\',\'' . $module . '\');">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif',IMAGE_CLOSE_EDIT) . '</a>
										</td>
									</tr>
								</table>
							</td>
						</tr>
						<tr >
							<td>&nbsp;</td>
						</tr>
						
					</table>
					</form>
					</div>
					';
			return $result;
		}
		function get_module_details($set){
			global $module_details;
			switch ($set) {
				case 'shipping':
					$module_details["type"] = 'shipping';
					$module_details["directory"] = DIR_FS_CATALOG_MODULES . 'shipping/';
					$module_details["key"]= 'MODULE_SHIPPING_INSTALLED';
					$module_details["installed"]=MODULE_SHIPPING_INSTALLED;
					$module_details["total_key"]='MODULE_SHIPPING_%_SORT_ORDER';
					break;
				case 'ordertotal':
					$module_details["type"] = 'order_total';
					$module_details["directory"] = DIR_FS_CATALOG_MODULES . 'order_total/';
					$module_details["key"] = 'MODULE_ORDER_TOTAL_INSTALLED';
					$module_details["installed"]=MODULE_ORDER_TOTAL_INSTALLED;
					$module_details["total_key"]='MODULE_ORDER_TOTAL_%_SORT_ORDER';
					break;
				case 'sms':
					$module_details["type"]='sms';
					$module_details["directory"]=DIR_FS_CATALOG_MODULES . 'sms/';
					$module_details["key"] = 'MODULE_SMS_GATEWAYS_INSTALLED';
					$module_details["installed"]=MODULE_SMS_GATEWAYS_INSTALLED;
					$module_details["total_key"]='MODULE_SMS_%_SORT_ORDER';
					break;
				case 'fulfiller':
					$module_details["type"]='fulfiller';
					$module_details["directory"]=DIR_FS_CATALOG_MODULES . 'fulfiller/';
					$module_details["key"]='MODULE_FULFILLER_GATEWAYS_INSTALLED';
					$module_details["installed"]=MODULE_FULFILLER_GATEWAYS_INSTALLED;
					$module_details["total_key"]='MODULE_FULFILLER_%_SORT_ORDER';
					break;
				case 'payment':
				default:
					$module_details["type"] = 'payment';
					$module_details["directory"] = DIR_FS_CATALOG_MODULES . 'payment/';
					$module_details["key"] = 'MODULE_PAYMENT_INSTALLED';
					$module_details["installed"]=MODULE_PAYMENT_INSTALLED;
					$module_details["total_key"]='MODULE_PAYMENT_%_SORT_ORDER';
					break;
			}			
		}
		function load_module($module){
			global $module_details,$PHP_SELF,$language;
			$file_extension = substr($PHP_SELF, strrpos($PHP_SELF, '.'));
			if (file_exists($module_details["directory"] . $module . $file_extension)) {
				if (!tep_class_exists($module)) {
					include(DIR_FS_CATALOG_LANGUAGES . $language . '/modules/' . $module_details["type"] . "/" . $module . $file_extension);
					include(CHECK_CON . $module_details["directory"] . $module . $file_extension);
				}
				$GLOBALS["select_module"] = new $module;
				return true;
			}
			return false;
		}
		function remove_module($module){ 
			//$module;
			global $select_module,$module_details;
			
			if (!load_module($module)) return;
	/*		$module_keys=$select_module->keys();
			$current_key='';
			while(list(,$value) = each($module_keys)){
				if(strpos($value, "_SORT_ORDER") !==false){
					$current_key=$value;
					break;
				}
			}

			if ($current_key!=''){ 
				$query=tep_db_query("select configuration_value from ".TABLE_CONFIGURATION ." where configuration_key='".$current_key."'");
				$result=tep_db_fetch_array($query);
				tep_db_query("update ".TABLE_CONFIGURATION ." set configuration_value=cast(configuration_value as unsigned)-1 where configuration_key like '".$module_details["total_key"] ."' and cast(configuration_value as unsigned)>" .$result['configuration_value']);
			} */
			$select_module->remove();
		}
		function install_module($module){
			global $select_module,$module_details;			
			
			if (!load_module($module)) return;
			$select_module->install();
			$module_keys=$select_module->keys();
			$current_key="";
			while(list($key,$value) = each($module_keys)){
				if(strpos($value,"_SORT_ORDER")!==false){
					$current_key=$value;
					break;
				}	
			} //while
			if ($current_key!=''){
				$sql=tep_db_query("select configuration_value from ".TABLE_CONFIGURATION ." where configuration_key ='". $current_key . "'");
				if(tep_db_num_rows($sql)>0){
					$result=tep_db_fetch_array($sql);
					$sort_order=(int)$result['configuration_value'];
				} else {
					$sort_order=1;
				}
			} //$current_key
			return $sort_order;
		}
		function move_posupdown($command,$module,$pos){
			global $module_details;
			if ($pos>0){
				if ($command=='ajx_move_up_module'){
					$query=tep_db_query("select configuration_key,configuration_value from ".TABLE_CONFIGURATION ." where cast(configuration_value as unsigned) <=" . $pos ." and configuration_key like '" . $module_details["total_key"] . "' order by cast(configuration_value as unsigned) desc limit 2");
				} else if($command=='ajx_move_down_module'){
					$query=tep_db_query("select configuration_key,configuration_value from ".TABLE_CONFIGURATION ." where cast(configuration_value as unsigned) >=" . $pos ." and configuration_key like '" . $module_details["total_key"] . "' order by cast(configuration_value as unsigned) limit 2");
				}
				if (tep_db_num_rows($query)>0){
					$details=array();
					$update_sql="UPDATE configuration set configuration_value='{1}' where configuration_key='{4}';\nUPDATE configuration set configuration_value='{3}' where configuration_key='{2}';";
					$icnt=1;
					while($result=tep_db_fetch_array($query)){
						$update_sql=str_replace("{" . $icnt . "}",$result["configuration_value"],$update_sql);
						$icnt++;
						$update_sql=str_replace("{" . $icnt . "}",$result["configuration_key"],$update_sql);
						$icnt++;
					}
					$splt_sql=split(";",$update_sql);
					
					tep_db_query($splt_sql[0]);
					tep_db_query($splt_sql[1]);
				}
			}
		}
		function save_module($module){
			global $HTTP_POST_VARS,$HTTP_POST_FILES,$CUSTOM_FILES,$HTTP_GET_VARS;
				$param=$HTTP_POST_VARS['configuration'];

				if(isset($HTTP_POST_FILES['configuration'])){
					while (list($key, $value) = each($HTTP_POST_FILES['configuration'])) {
						while(list($key1,$value1)=each($value)){
							$CUSTOM_FILES[$key1][$key] = $value1;
						}
					}
					if (count($CUSTOM_FILES)>0){
						reset($CUSTOM_FILES);
						while(list($key,$value)=each($CUSTOM_FILES)){
							if (trim($value["name"])=="") unset($CUSTOM_FILES[$key]);
						}
					}
					if (count($CUSTOM_FILES)>0){
						reset($CUSTOM_FILES);
						$upload_fs_dir = DIR_FS_CATALOG . DIR_WS_IMAGES;
						$upload_ws_dir = DIR_FS_ADMIN  . DIR_WS_IMAGES;
						$extensions = array(".png",".gif",".jpg",".jpeg");
						for($i=0,$count=sizeof($extensions);$i<$count;$i++){
							if(file_exists($upload_ws_dir . $HTTP_GET_VARS['module'] . $extensions[$i])){
								@unlink($upload_ws_dir . $HTTP_GET_VARS['module'] . $extensions[$i]);
							}
							if(file_exists($upload_fs_dir . $HTTP_GET_VARS['module'] . $extensions[$i])){
								@unlink($upload_fs_dir . $HTTP_GET_VARS['module'] . $extensions[$i]);
							}
						}
	
						while(list($key,$value)=each($CUSTOM_FILES)){
							$extension = substr($value['name'],strpos($value['name'],"."));
							if ($extension=="" || !in_array($extension,$extensions)) continue;
							if (strpos($key,"MODULE_PAYMENT_")!==false){
								$CUSTOM_FILES[$key]["name"]=$HTTP_GET_VARS['module'] . $extension;
							}
							$upload = new upload($key, $upload_ws_dir);
							$file_name = $upload->filename;
							@copy($upload_ws_dir . $file_name,$upload_fs_dir . $file_name);
							$param[$key]=$file_name;
						}
					}
				}
				
				while (list($key, $value) = each($param)) {
					if(is_array($value)){ $value=array_remove_null_value($value);  $value = implode( ", ", $value); }
					tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $value . "' where configuration_key = '" . $key . "'");
				}			
		}
		function array_remove_null_value($value){
			if(is_array($value)){
			$my_array=array();
				for($i=0;$i<count($value);$i++)
					if($value[$i]!=NULL)
						$my_array[]=$value[$i];
			return $my_array;
			}
		}
		function set_installed_modules($module,$mode,$sort_order=0){
			global $installed_modules,$sort_orders,$module_details;
			switch($mode){
				case "remove":
					$installed_modules=str_replace(";" . $module  . ".php;",";",";" . $installed_modules .";");
					$installed_modules=substr($installed_modules,1,-1);
					break;
				case "install":
					if ($installed_modules=="") {
						$installed_modules=$module . ".php";
						break;
					}
					$mod_splt=split(";",$installed_modules);
					$sot_splt=split(";",$sort_orders);
					$temp_module="";
					for ($icnt=0,$n=count($mod_splt);$icnt<$n;$icnt++){
						if ($sot_splt[$icnt]>=$sort_order) {
							$temp_module=$mod_splt[$icnt];
							break;
						}
					}
					if ($temp_module!=''){
						$installed_modules=str_replace(";".  $temp_module  . ";",";" . $module . ".php;" . $temp_module . ";",";" . $installed_modules .";");
						$installed_modules=substr($installed_modules,1,-1);
					} else {
						$installed_modules.=";" . $module .".php";
					}	
					
					break;
			}
			tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $installed_modules . "' where configuration_key = '" . $module_details["key"] . "'");
			//@unlink("temp.txt");
			//file_put_contents("temp.txt",$installed_modules);
		}
 ?>