<?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

  Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
define('AUTOCHECK', 'False');
define('DISPLAYTTT', 'True');

  require('includes/application_top.php');
  tep_get_last_access_file();require(DIR_WS_CLASSES . 'currencies.php');
  require('includes/classes/split_page_results_event.php');
  $currencies = new currencies();

  $specials_condition_array = array(array('id' => '0', 'text' => SPECIALS_CONDITION_DROPDOWN_0),
                                    array('id' => '1', 'text' => SPECIALS_CONDITION_DROPDOWN_1),
                                    array('id' => '2', 'text' => SPECIALS_CONDITION_DROPDOWN_2));

  $deduction_type_array = array(array('id' => '0', 'text' => DEDUCTION_TYPE_DROPDOWN_0),
                                array('id' => '1', 'text' => DEDUCTION_TYPE_DROPDOWN_1),
                                array('id' => '2', 'text' => DEDUCTION_TYPE_DROPDOWN_2));
								
    $command=(isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'');
  $id=(isset($HTTP_GET_VARS['id'])?$HTTP_GET_VARS['id']:'');
  $page=(isset($HTTP_GET_VARS['page'])?$HTTP_GET_VARS['page']:'');
  $flag=(isset($HTTP_GET_VARS['flag'])?$HTTP_GET_VARS['flag']:'');
  if($command!=''){
  if($command=='copy_confirm')
  	echo 'insert' . '{}' . $id . '||';
  else	
    echo $command . '{}' . $id . '||';
  	switch($command){
		case 'get_details':
			echo do_get_details($id);
		break;
		case 'new':
		case 'edit':
		case 'new_sales':
			echo get_edit_details($id);
		break;
		case 'insert':
		case 'update':
			if($command=='insert')
				$id=0;
			echo save_details($id);
		break;	
		case 'copy':
			echo copy_details($id,false);
		break;
		case 'copy_confirm':
			echo copy_details($id,true);
		break;
		case 'delete':
			echo delete_details($id,false);
		break;	
		case 'delete_confirm':
			echo delete_details($id,true);
		break;
		case 'page_navigation':
		$search_fnd=false;
			echo get_sales_details();
		break;
		case 'set_flag':
			echo get_status_change($id);
		break;
		case 'fetch_product':
			echo fetch_product($id);
		break;
	}
  	exit;
  }

	function fetch_product($category_id){
	global $languages_id,$sInfo;
	$products_description_query = tep_db_query("select pd.products_id,pd.products_name from ".TABLE_PRODUCTS." p, ".TABLE_PRODUCTS_DESCRIPTION." pd, ".TABLE_PRODUCTS_TO_CATEGORIES." ptc where ptc.products_id=p.products_id and p.products_id=pd.products_id and pd.language_id='$languages_id' and ptc.categories_id='$category_id'");
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="30">&nbsp;</td>
<td>
	<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<?php 
	$sale_products_selected_array = array();
	if(is_object($sInfo))
		$sale_products_selected_array = explode(",",$sInfo->sale_products_selected);
	while($products_description_array = tep_db_fetch_array($products_description_query)){ 
	$checked=true;
	  if(is_object($sInfo))
	   	if(in_array($products_description_array['products_id'],$sale_products_selected_array))
			$checked=true;
		else
			$checked=false;
	?>
	<tr>
	<td class="main"><label><?php echo tep_draw_checkbox_field("products[$category_id][]",$products_description_array['products_id'],'',$checked,'onClick="javascript: chk_category(\''.$category_id.'\');"').'&nbsp;'.$products_description_array['products_name']; ?></label></td>
	</tr>
	<?php } 
	if(tep_db_num_rows($products_description_query)<=0){
	?>
	<tr>
	<td class="main" style="color:#FF0000;">No Products Found</td>
	</tr>
	<?php } ?>
	</table>
</td>
</tr>
</table>
<?php } ?>
<!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/date-picker.js"></script>
<script>
    var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";
	var prev_id='';
	var prev_page_id='';
	var page_id=1;
	var ajx_start=false;
	var row_expand=false;
	var close_obj=false;
	var form_obj=false;
	function do_page_fetch(id,action){
		if(ajx_start) return;
		else {
			ajx_start=true;
		}
		if(action!='new_sales'){
			document.getElementById('new_details').style.display="none";
			document.getElementById('span_new').style.display="none";
			document.getElementById('span_new').innerHTML="";
		}	
		
		document.getElementById('sales_id').value=id;
		command='<?php echo tep_href_link(FILENAME_SALEMAKER);?>' + '?command=' + action + '&id=' + id;
		switch(action){  
			case 'get_details':
				var head=document.getElementById('head_' + id);
				var data=document.getElementById('data_' + id);
				if(prev_id!=id) {
					row_expand=false;
					close_obj=false;
					form_obj=false;
				}	
				if(data.style.display=='' && document.getElementById('data_' + id).innerHTML!='' && close_obj==false)  {
					data.style.display="none";
					data.innerHTML='';
					document.getElementById('data_' + id).innerHTML='';
					head.style.display='';
					row_expand=false;
					ajx_start=false;
					prev_id='';
					return;
				}if(data.style.display=="none" && row_expand==false){ 
					row_expand=true;
					data.style.display="";
					document.getElementById('data_' + id).innerHTML=document.getElementById('loading').innerHTML;
					if(prev_id!=''){
						document.getElementById('head_' + prev_id).style.display="";
						document.getElementById('data_' + prev_id).innerHTML="";
						document.getElementById('data_' + prev_id).style.display="none";
					}
					prev_id=id;
					head.style.display="none"
					document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
					display_flag=1;
				} 
				close_obj=false;
				if(form_obj==true){
				 ajx_start=false;
				 return;
				 }
			break;
			case 'new':
			case 'edit':
				if(action=='new')
					document.getElementById('sales_action').value='insert';
				else 
				document.getElementById('sales_action').value='update';
				document.getElementById('span_details').style.display="none";
				document.getElementById('span_copy').style.display="none";
				document.getElementById('span_copy').innerHTML='';
				document.getElementById('span_delete').style.display="none";
				document.getElementById('span_edit').style.display="";
				document.getElementById('span_edit').innerHTML=document.getElementById('loading').innerHTML;
				
				
			break;
			case 'insert':
				if(prev_id!=''){
					document.getElementById('head_' + prev_id).style.display="";
				 	document.getElementById('data_' + prev_id).style.display="none";
				 }
				prev_id='';
				document.getElementById('span_edit').style.display="none";
				document.getElementById('span_details').style.display="none";
				document.getElementById('span_copy').style.display="none";
				document.getElementById('span_delete').style.display="none";
				do_post_command('sales',command);
				return;
			break;
			case 'update':
				//document.getElementById('span_edit').style.display="none";
				document.getElementById('span_copy').style.display="none";
				document.getElementById('span_delete').style.display="none";
				//document.getElementById('span_details').style.display="";
				//document.getElementById('span_details').innerHTML=document.getElementById('loading').innerHTML;
				do_post_command('sales',command);
				return;
			break;
			case 'copy':
				document.getElementById('sales_action').value='copy';
				document.getElementById('span_details').style.display="none";
				document.getElementById('span_edit').style.display="none";
				document.getElementById('span_edit').innerHTML='';
				document.getElementById('span_delete').style.display="none";
				document.getElementById('span_copy').style.display="";
				document.getElementById('span_copy').innerHTML=document.getElementById('loading').innerHTML;
			break;
			case 'new_sales':
				document.getElementById('sales_action').value='new_sales';
				if(document.getElementById('span_details')) document.getElementById('span_details').style.display="none";
				if(document.getElementById('span_edit')) document.getElementById('span_edit').style.display="none";
				if(document.getElementById('span_copy')) document.getElementById('span_copy').style.display="none";
				if(document.getElementById('span_delete')) document.getElementById('span_delete').style.display="none";
					if(prev_id!='' && document.getElementById('head_' + prev_id)){
						document.getElementById('head_' + prev_id).style.display="";
						document.getElementById('data_' + prev_id).innerHTML="";
						document.getElementById('data_' + prev_id).style.display="none";
					}
					prev_id='';
					document.getElementById('new_details').style.display="";
					document.getElementById('span_new').style.display="";
					document.getElementById('span_new').innerHTML=document.getElementById('loading').innerHTML;
					display_flag=0;
			break;
			case 'copy_confirm':close_obj=true;
				if(prev_id!=''){
					document.getElementById('head_' + prev_id).style.display="";
				 	document.getElementById('data_' + prev_id).style.display="none";
				 }
				prev_id='';
				if(document.getElementById('span_edit')) document.getElementById('span_edit').style.display="none";
				if(document.getElementById('span_details')) document.getElementById('span_details').style.display="none";
				if(document.getElementById('span_copy')) document.getElementById('span_copy').style.display="";
				if(document.getElementById('span_delete')) document.getElementById('span_delete').style.display="none";
				do_post_command('sales',command);
				return;
			break;
			case 'delete':
				document.getElementById('sales_action').value='delete';
				document.getElementById('span_edit').style.display="none";
				document.getElementById('span_copy').style.display="none";
				document.getElementById('span_delete').style.display="";
				document.getElementById('span_delete').innerHTML=document.getElementById("loading").innerHTML;
				document.getElementById('span_details').style.display="none";
			break;
			case 'delete_confirm':
				document.getElementById('span_delete').style.display="";
				document.getElementById('span_delete').innerHTML=document.getElementById("loading").innerHTML;
				
				document.getElementById('span_details').style.display="none";
				prev_id='';
			break;
			case 'close_sales': close_obj=true;
				var span_details=document.getElementById("span_details");
				var span_edit=document.getElementById("span_edit");				
				var span_delete=document.getElementById("span_delete");
				var span_copy=document.getElementById("span_copy");
				
				var img_save = document.getElementById('img_save');
				document.getElementById('new_details').style.display="none";
				document.getElementById('span_new').style.display="none";
				
				if(span_edit.style.display=="") span_edit.style.display="none";
				if(span_delete.style.display=="") span_delete.style.display="none"; 
				if(span_copy.style.display=="") span_copy.style.display="none"; 
				span_details.style.display="";
				if(img_save) img_save.style.display="none"; 
				if(document.getElementById("img_ids")){
					var tr_obj=document.getElementById("img_ids");
					if(tr_obj.childNodes[1].childNodes[0].src==img_src+'template/img_edit_hover.gif')	
						tr_obj.childNodes[1].childNodes[0].src=img_src+'template/img_edit.gif';
					if(tr_obj.childNodes[2].childNodes[0].src==img_src+'template/img_copy_hover.gif')	
						tr_obj.childNodes[2].childNodes[0].src=img_src+'template/img_copy.gif';	 	 			   	  			
					if(tr_obj.childNodes[3].childNodes[0].src==img_src+'template/img_trash_hover.gif')	
						tr_obj.childNodes[3].childNodes[0].src=img_src+'template/img_trash.gif';	 	 			   	  			
					 }	 			   	  	  	 			   	  	 	 			   	  	
					ajx_start=false;
			return;
			case 'close': 
					if(document.getElementById('no_sales')) document.getElementById('no_sales').innerHTML="<?php echo TEXT_NO_SALES;?>";
					document.getElementById('new_details').style.display="none";
					document.getElementById('span_new').style.display="none";
					ajx_start=false;
					document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
					display_flag=1;
			return;
			break;
		}
		do_get_command(command);
		
	}
	function do_result(result){ 
		var splt=Array();
		splt=result.split("{}");
		var spl_arr=Array();
		var sp1=splt[1];
		spl_arr=sp1.split("||");
		switch(splt[0]){
			case 'get_details':
				document.getElementById('head_' + spl_arr[0]).style.display="none";
				document.getElementById('data_' + spl_arr[0]).style.display="";
				document.getElementById('data_' + spl_arr[0]).innerHTML=spl_arr[1];
			break;	
			case 'new':
			case 'edit':
				document.getElementById('img_save').style.display="";
				document.getElementById('span_edit').innerHTML=spl_arr[1];
				//SetCategories();
			break;
			case 'update':
				if(spl_arr[1]=='Error') {
					document.getElementById('span_edit').style.display="";
					document.getElementById('span_details').style.display="none";
					document.getElementById('error_sales').style.display="";
					ajx_start=false;
					return;
				} else {
					document.getElementById('span_edit').style.display="none";
					document.getElementById('span_details').style.display="";
					document.getElementById('span_details').innerHTML=document.getElementById('loading').innerHTML;
					close_obj=true;
					if(document.getElementById("img_ids")){
						var tr_obj=document.getElementById("img_ids");
						if(tr_obj.childNodes[1].childNodes[0].src==img_src+'template/img_edit_hover.gif')	
							tr_obj.childNodes[1].childNodes[0].src=img_src+'template/img_edit.gif';
						if(tr_obj.childNodes[2].childNodes[0].src==img_src+'template/img_copy_hover.gif')	
							tr_obj.childNodes[2].childNodes[0].src=img_src+'template/img_copy.gif';	 	 			   	  			
						if(tr_obj.childNodes[3].childNodes[0].src==img_src+'template/img_trash_hover.gif')	
							tr_obj.childNodes[3].childNodes[0].src=img_src+'template/img_trash.gif';	 	 			   	  			
						 }	 			   	  	  	 			   	  	 	 			   	  	
					document.getElementById('img_save').style.display="none";
					var details=Array();
					details=spl_arr[1].split("{#}");
					var sarr=Array();
					sarr=details[1].split("{##}");
					document.getElementById('head_' + spl_arr[0]).innerHTML=sarr[1];
					document.getElementById('span_details').innerHTML=details[0];
					document.getElementById('row_heading').innerHTML=sarr[0];
				}
			break;
			case 'insert':
				if(spl_arr[1]=='Error') {
					document.getElementById('error_sales').style.display="";
					ajx_start=false;
					return;
				} 
				close_obj=true;
				document.getElementById('span_new').style.display="none";
				document.getElementById('span_new').innerHTML="";
				document.getElementById('new_details').style.display="none";
				var id_value=Array();
				id_value=spl_arr[1].split('{|}');
				var details=Array();
				details=id_value[1].split("{#}");	
				var row=document.getElementById('ref_table');
				var row_obj=row.insertRow(row.rows.length-3);
				var row_len=(row.rows.length-3);
				var cell_obj=row_obj.insertCell(0);
				cell_obj.className="dataTableContent";
				var classname;
				cell_obj.innerHTML=details[0];  
				row_obj.onmouseover=function (){rowOverEffect(this)};
				row_obj.onmouseout=function (){rowOutEffect(this)};
				row_obj.id="row_" + id_value[0];
				//row_obj.onclick=function(){ ((document.getElementById("data_" + id_value[0]).style.display=='none')?do_page_fetch(id_value[0],'get_details'):'')};
				row_obj.onclick=function(){ (do_page_fetch(id_value[0],'get_details'))};
				if((row_len % 2) == 0) classname="dataTableRowEven";
				else classname="dataTableRowOdd";
				row_obj.className=classname;
				row_obj.height="25";
				document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
				display_flag=1;
				command='<?php echo tep_href_link(FILENAME_SALEMAKER);?>' + '?command=page_navigation&page=1';
				do_get_command(command);
			break;
			case 'copy':
				document.getElementById('img_save').style.display="";
				document.getElementById('span_copy').innerHTML=spl_arr[1];
			break;
			case 'new_sales':
				if(document.getElementById('no_sales')) document.getElementById('no_sales').innerHTML="";
				document.getElementById('span_new').innerHTML=spl_arr[1];
				display_header();
				//SetCategories();
			break;
			case 'delete':
				document.getElementById('img_save').style.display="";
				document.getElementById('span_delete').innerHTML=spl_arr[1];
			break;	
			case 'delete_confirm':
				document.getElementById('ref_table').deleteRow(document.getElementById('head_' +spl_arr[0]).parentNode.parentNode.rowIndex);
				command='<?php echo tep_href_link(FILENAME_SALEMAKER);?>' + '?command=page_navigation&page=' + page_id;
				do_get_command(command);
			break;
			case 'page_navigation':
				document.getElementById('ajax_details').innerHTML=spl_arr[1];
			break;
			case 'set_flag' : 
				document.getElementById('head_' + spl_arr[0]).innerHTML=spl_arr[1];
				document.getElementById('row_heading').innerHTML=spl_arr[1];
			break;
			case 'fetch_product':
				document.getElementById('ajax_cat_'+spl_arr[0]).innerHTML=spl_arr[1];
				display_header();
			break;
		}
		ajx_start=false;
	}
	
/*	 function do_hover_change(element,mode){
		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;
	}     
*/
     var display_flag=1;
	 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");
		 var span_move=document.getElementById("span_copy");
	  	 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';
			    if(img_get_src.src==img_src+'template/img_copy.gif') {
					if(span_move && span_move.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_copy_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_copy_hover.gif' && span_move && span_move.style.display=="none") img_get_src.src=img_src+'template/img_copy.gif';
		  }
		 }		 
	 }  	
	function do_hover_change_new(element,eve){ 
		 var span_details=document.getElementById("span_details"); 
		 var span_edit=document.getElementById("span_edit");
		 var span_delete=document.getElementById("span_delete");
		 var span_move=document.getElementById("span_copy");
		 if(display_flag==1){
		 if (element.eve && mode==element.eve) return;
	 	 if (eve==2)
			element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
		else
			element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
		}
	}	
	
	
	function ValidateForm(){
		close_obj=true;
		var id=document.getElementById('sales_id').value;
		var action=document.getElementById('sales_action').value;
		var error_result="";
		if(action=='delete'){
			do_page_fetch(id,'delete_confirm');
			prev_id='';
		} else {
			if(check_form()==true){ 
				form_obj=false;
				if(action=='insert'){
				page_id=1;
					do_page_fetch(0,'insert');
				}else if(action=='update')	{
					
					do_page_fetch(id,'update');
				}else if(action=='copy'){
					do_page_fetch(id,'copy_confirm');
				}else if(action=='new_sales'){
					var command=command='<?php echo tep_href_link(FILENAME_SALEMAKER);?>' + '?command=insert&page=1';
					do_post_command('sales',command);
				}
			}
		}
			return false;		
	}
	
	function change_status(id,flag){
		close_obj=true;
		command='<?php echo tep_href_link(FILENAME_SALEMAKER);?>' + '?command=set_flag&id=' + id + '&flag=' + flag;
		do_get_command(command);
	}
	
	function check_form(){
		var error_message='';
		var date_error=true;
		obj=document.sales.name.value;
		if(obj=='' || str_trim(obj) == ''){
			error_message=error_message +'* <?php echo ERR_SOURCES_NAME;?>\n';
		}
		
		if(document.getElementById('sales_action').value=='insert' || document.getElementById('sales_action').value=='update' || document.getElementById('sales_action').value=='new_sales'){ 
			var discount_type=document.sales.elements['discount_type'];
			for(i=0;i<discount_type.length;i++){ 
				if(discount_type[i].checked && discount_type[i].value=='C') {
					if(document.getElementById('txt_choice_text').value=='' || str_trim(document.getElementById('txt_choice_text').value)=='')
						error_message=error_message +'* <?php echo ERR_CHOICE_TEXT;?>\n';
					if(document.getElementById('txt_choice_warning').value=='' || str_trim(document.getElementById('txt_choice_warning').value)=='')
						error_message=error_message + '* <?php echo ERR_CHOICE_WARNING;?>\n';
				}
			}
			if(isNaN(document.sales.deduction.value) )
				error_message=error_message +'* <?php echo ERR_INVALID_DEDUCTION;?>\n';
			if(isNaN(document.sales.from.value))
				error_message=error_message +'* <?php echo ERR_INVALID_PRODUCTS_RANGE_FROM;?>\n';
			if(isNaN(document.sales.to.value))
				error_message=error_message +'* <?php echo ERR_INVALID_PRODUCTS_RANGE_TO;?>\n';
				
		if(document.sales.end.value!=''){
		if(((document.sales.start.value).indexOf("-")!=-1)){
			date_error=isValidDate(date_format(document.sales.start.value,'','Y-m-d')); 
			} else{
			 date_error=false;	
				error_message=error_message+'<?php echo ERR_INVALID_START_DATE?>\n';
			 }
		}	
		if(document.sales.end.value!='') {
		if(((document.sales.end.value).indexOf("-")!=-1)){
			date_error=isValidDate(date_format(document.sales.end.value,'','Y-m-d'));
			} else{
				error_message=error_message+'<?php echo ERR_INVALID_END_DATE?>\n';
			 date_error=false;	
			 }
			} 
		if(date_error==true){
		var st_date=date_format(document.sales.start.value,'<?php echo EVENTS_DATE_FORMAT;?>','Y-m-d');
		var end_date=date_format(document.sales.end.value,'<?php echo EVENTS_DATE_FORMAT;?>','Y-m-d');
		if(st_date>end_date) 
			error_message=error_message + '* <?php echo ERR_START_DATE;?>\n';
		}	
		}
			
		if(error_message!=''){
			alert(error_message);
			close_obj=true;
			form_obj=true;
			return false;
		}		
		return true;
	}
	
	function do_page_navigation(value,params,name,plink){ 
	if(params=='page_navigation' && document.getElementById("page_''") && plink!=''){
		 page_id = parseInt(document.getElementById("page_''").options[document.getElementById("page_''").selectedIndex].value);
		if(plink=='next')
			page_id=page_id+1;
		else if(plink=='prev')
			page_id=page_id-1;
	}
	prev_page_id=page_id;
	prev_id='';
	command='<?php echo tep_href_link(FILENAME_SALEMAKER);?>' + '?command=page_navigation&page=' + page_id;
	do_get_command(command);
}

</script>
<script language="JavaScript">
function RowClick(RowValue,cat_id,category_name) { 
  for (i=0; i<document.sales.length; i++) {
    if(document.sales.elements[i].type == 'checkbox') {
      if(document.sales.elements[i].value == RowValue) {
        if(document.sales.elements[i].disabled == false) {
         document.sales.elements[i].checked = !document.sales.elements[i].checked;
        }
      }
    }
  }
  //SetCategories()
}

function all_category(){
	if(document.getElementById('all_categories').checked==true){
		for(i=0;i<document.getElementsByName('categories[]').length;i++)
			document.getElementsByName('categories[]')[i].disabled=true;
	document.getElementById('ajax_sales_maker').style.display='none';		
	}else{
		document.getElementById('ajax_sales_maker').style.display='';		
		for(i=0;i<document.getElementsByName('categories[]').length;i++)
			document.getElementsByName('categories[]')[i].disabled=false;
	}
}

function CategoryClick(cat_path,cat_id,category_name){ 
	if(document.getElementById('check_box_'+cat_id).disabled==false){
		if(document.getElementById('check_box_'+cat_id).checked)
			document.getElementById('check_box_'+cat_id).checked=false;
		else
			document.getElementById('check_box_'+cat_id).checked=true;	
  	add_categories(cat_id,category_name);
	display_header();
	}
}

function CategoryClick1(cat_path,cat_id,category_name){ 
  	add_categories(cat_id,category_name);
	display_header();
}


function display_header(){
	if(document.getElementById('ajax_sales_maker').getElementsByTagName("div").length==1)
		document.getElementById('head_ajax_sales_maker').style.display='none';
	else
		document.getElementById('head_ajax_sales_maker').style.display='';
}

function add_categories(cat_id,category_name){
	if(document.getElementById('check_box_'+cat_id).checked==true){
	/*insert_value="<div class='main' onclick='javascript: fetch_product("+cat_id+");' id='cat_"+cat_id+"'><label for='category_"+cat_id+"'><input name='category_list["+cat_id+"][]' id='category_"+cat_id+"' value='"+cat_id+"' checked type='checkbox'>&nbsp;"+category_name+"</label></div>"+
				  "<div class='main' id='ajax_cat_"+cat_id+"'></div>";
	if(document.getElementById('ajax_sales_maker')){
		document.getElementById('ajax_sales_maker').innerHTML += insert_value;		  
	}*/
	var cat_div = document.createElement("div");
	var cat_ajax_div = document.createElement("div");	
	cat_div.className="main";
	cat_div.id='cat_'+cat_id;
	cat_div.onclick=function(){ fetch_product(cat_id); };
	cat_ajax_div.className="main";
	cat_ajax_div.id="ajax_cat_"+cat_id;
	cat_div.innerHTML="<label for='category_"+cat_id+"'><input name='category_list["+cat_id+"][]' id='category_"+cat_id+"' value='"+cat_id+"' checked type='checkbox'>&nbsp;"+category_name+"</label>";
	document.getElementById('ajax_sales_maker').appendChild(cat_div);
	document.getElementById('ajax_sales_maker').appendChild(cat_ajax_div);
	}else if(document.getElementById('cat_'+cat_id)){
	document.getElementById('cat_'+cat_id).parentNode.removeChild(document.getElementById('cat_'+cat_id));
	document.getElementById('ajax_cat_'+cat_id).parentNode.removeChild(document.getElementById('ajax_cat_'+cat_id));
	}
}	

function fetch_product(cat_id){
	if(document.getElementById('ajax_cat_'+cat_id).innerHTML==''){	
	document.getElementById('ajax_cat_'+cat_id).innerHTML='<span class="smallText">loading...</span>';
	do_get_command('<?php echo tep_href_link(FILENAME_SALEMAKER,'command=fetch_product&id='); ?>'+cat_id);
	document.getElementById('category_'+cat_id).checked=true;
	}else{
	chk_category(cat_id);
	/*for(i=0;i<document.getElementsByName('products['+cat_id+'][]').length;i++)
		document.getElementsByName('products['+cat_id+'][]')[i].checked=true;
	document.getElementById('category_'+cat_id).checked=true;	*/
	}
}

function chk_category(category_id){
	prd_chk = true;

	for(i=0;i<document.getElementsByName('products['+category_id+'][]').length;i++)
		if(document.getElementsByName('products['+category_id+'][]')[i].checked==false)
			prd_chk = false;

	if(prd_chk==false)		
		document.getElementById('category_'+category_id).checked=false;
	else
		document.getElementById('category_'+category_id).checked=true;
}

function discount_action(){
	var discount_type=document.sales.elements['discount_type'];
	if(discount_type) {
		for(i=0;i<discount_type.length;i++) {
			if(discount_type[i].checked && discount_type[i].value=='C') {
				document.getElementById('choice_text').style.display="";
				document.getElementById('choice_warning').style.display="";
			} else  {
				document.getElementById('choice_text').style.display="none";
				document.getElementById('choice_warning').style.display="none";
			}
		}
	}
		
}
</script>
<?php include('includes/date_format_js.php'); ?>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<!-- 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>
						<table border="0" width="100%" cellspacing="0" cellpadding="0">
							<tr>
								<td valign="top">
									<table border="0" width="100%" cellspacing="0" cellpadding="2">
										
										<tr>
											<td><?php echo tep_draw_form('sales',FILENAME_SALEMAKER,'','post','onSubmit="return ValidateForm();"');
														$group_query=tep_db_query("select count(*) from " . TABLE_CUSTOMERS_GROUPS . " where customers_groups_discount>0");
											?>
											<input type="hidden" id="sales_id"><input type="hidden" id="sales_action">
												<table cellpadding="0" cellspacing="0" border="0" width="100%">
													<?php //if(tep_db_num_rows($group_query)<=0) { ?>
													   <tr class="dataTableHeadingRow" >	
															<td align="left"><?php echo '<a href="javascript:do_page_fetch(\'\',\'new_sales\');" id="img_new" onmouseover="javascript:do_hover_change_new(this,2);" onmouseout="javascript:do_hover_change_new(this,1);">' . tep_image('images/template/img_new.gif','New Sales','','22','22') .'</a>';?></td>
														</tr>
													<?php //} ?>
													<tr><td><?php echo tep_draw_separator('pixel_trans.gif','10','5');?></td></tr>
													<tr class="dataTableHeadingRow">
														<td class="dataTableHeadingContent" align="left"><?php echo TABLE_HEADING_SALE_NAME; ?></td>
													</tr>
													<div id="loading" style="display:none"><div style="height:30px;margin-left:30px"><img src="images/24-1.gif"></div></div>
													<?php 
														/*if(tep_db_num_rows($group_query)>0) {
													?>	
													<tr><td><?php echo tep_draw_separator('pixel_trans.gif','10','10');?></td></tr>
													<tr>
														<td align="center"><b>
															<?php echo TEXT_DISCOUNTS;?>
															</b>
														</td>
													</tr>	
													<?php 
														} else { */
													?>
													<tr>
														<td id="ajax_details">
														<?php get_sales_details(); ?>
														</td>
													</tr>
													<?php // } ?>	
												</table></form>
											</td>
										</tr>			
										
									</table>
								</td>
							</tr>  
						</table>
					</td>
				</tr>
			</table>
		</td>
		<!-- body_text_eof //-->
	</tr>
</table>
</td>
</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 get_sales_details(){
	global $page;
    $salemaker_sales_query_raw = "select sale_id, sale_status, sale_name,sale_discount_type, sale_deduction_value, sale_deduction_type, sale_pricerange_from, sale_pricerange_to, sale_specials_condition, sale_categories_selected, sale_categories_all, sale_date_start, sale_date_end, sale_date_added, sale_date_last_modified, sale_date_status_change,apply_to_cross_sale from " . TABLE_SALEMAKER_SALES . " order by sale_id desc";
    $salemaker_sales_split = new splitPageResultsEvent($page, MAX_DISPLAY_SEARCH_RESULTS, $salemaker_sales_query_raw, $salemaker_sales_query_numrows);
    $salemaker_sales_query = tep_db_query($salemaker_sales_query_raw); $row=0;
	
	echo '<table cellpadding="0" cellspacing="0" border="0" width="100%" id="ref_table">';
	if(tep_db_num_rows($salemaker_sales_query)>0){
		while ($salemaker_sales = tep_db_fetch_array($salemaker_sales_query)) {
				if($row %2 ==0) $class="dataTableRowEven";
				else $class="dataTableRowOdd"; $row++;
			//echo '                  <tr class="' . $class .'"  height="25" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="javascript:if(document.getElementById(\'data_' . $salemaker_sales['sale_id']. '\').style.display==\'none\') do_page_fetch(\'' . $salemaker_sales['sale_id']. '\',\'get_details\');">' . "\n";
			echo '                  <tr class="' . $class .'"  height="25" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="javascript:if(document.getElementById(\'data_' . $salemaker_sales['sale_id']. '\').style.display==\'none\') do_page_fetch(\'' . $salemaker_sales['sale_id']. '\',\'get_details\');">' . "\n";			
		  if ($salemaker_sales['sale_status'] == '1') {
		   $status= tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', '', 10, 10);
		  } else {
		   $status=tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', '', 10, 10) ;
		  }
			echo '<td  class="dataTableContent"><div id="head_' . $salemaker_sales['sale_id'] .'">' .$status . tep_draw_separator('pixel_trans.gif','10','10') .  $salemaker_sales['sale_name']  ; ?>
			
			</div><div id="data_<?php echo $salemaker_sales['sale_id'];?>" style="display:none"></div></td>
		  </tr> 
<?php 
		}
	} else echo '<tr><td class="main" align="center" id="no_sales">' . TEXT_NO_SALES . '</td></tr>';
?>		<tr id="new_details" style="display:none" class="openContent">
			<td>	
				<table cellpadding="0" cellspacing="0" border="0" width="100%">
					<tr class="openContent_top">
						<td class="inner_title"><?php echo TEXT_NEW_SALES;?></td>
						<td width="240" align="right"><input type="image" src="images/template/img_savel.gif" alt="save" title="save" border="0">&nbsp;&nbsp;<a href="javascript:do_page_fetch('','close');"><img src="images/template/img_closel.gif" alt="close" title="close" border="0"></a>&nbsp;&nbsp;</td>
					</tr>
					<tr><td><div id="span_new" style="display:none"></div></td></tr>
				</table>
			</td>
		</tr>			
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif','10','10');?></td></tr>
		<tr>
			<td colspan="7">
				<table border="0" width="100%" cellpadding="0"cellspacing="2">
					<tr>
						<td class="smallText" valign="top"><?php echo $salemaker_sales_split->display_count($salemaker_sales_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $page, TEXT_DISPLAY_NUMBER_OF_SALES); ?></td>
						<td class="smallText" align="right"><?php echo $salemaker_sales_split->display_script_links($salemaker_sales_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $page,'page_navigation','do_page_navigation(\'\'','\'\''); ?></td>
					</tr>
				</table>
			</td>
		</tr>	  
</table>
<?php }


	function do_get_details($id){
		global $languages_id,$language,$deduction_type_array;
		$salemaker_sales_query = tep_db_query("select sale_id, sale_status, sale_name,sale_discount_type, choice_text,choice_warning,sale_deduction_value, sale_deduction_type, sale_pricerange_from, sale_pricerange_to, sale_specials_condition, sale_categories_selected, sale_categories_all, sale_date_start, sale_date_end, sale_date_added, sale_date_last_modified, sale_date_status_change,apply_to_cross_sale from " . TABLE_SALEMAKER_SALES . " where sale_id='" . $id . "'");
		$salemaker_sales_result = tep_db_fetch_array($salemaker_sales_query); $row=0;
		$result='<table width="100%" border="0" cellspacing="0" cellpadding="0"   class="openContent">'.
				'<tr heigth="40" bgcolor="#EDF1FE"  class="openContent_top" onclick="javascript:do_page_fetch(\'' . $id. '\',\'get_details\');">';  	
		$result.=		  '<td width="40" valign="top" align="center">&nbsp;</td>';
			if ($salemaker_sales_result['sale_status'] == '1') {
				   //$status='<a href="javascript:change_status(\''.$id . '\',\'0\'); ">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', '', 10, 10) . '</a>';
				   $status=tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', '', 10, 10,'onClick="javascript:change_status(\''.$id . '\',\'0\');"');
				  } else {
				   //$status='<a href="javascript:change_status(\''.$id . '\',\'1\'); ">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', '', 10, 10) . '</a>';
				   $status=tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', '', 10, 10,'onClick="javascript:change_status(\''.$id . '\',\'1\');"');
				  }		
		$result.=		  '<td align="left" valign="middle" width="350" class="inner_title" id="row_heading">'. $status . tep_draw_separator('pixel_trans.gif','10','10') . $salemaker_sales_result['sale_name'].'</td>'.
				  '<td width="470" align="left" class="main" valign="top">'.
				  	'<table border="0" cellspacing="0" cellpadding="0" style="width:100;height:30px;display:inline;position:relative;">'.
				  	  '<tr class="img_move" id="img_ids">'.
				  	  	'<td class="cell_bg_popm_left" width="10">&nbsp;</td>';
							//$result.=	'<td width="10%" onclick="javascript:do_hover_change(this,1);do_page_fetch(\'\',\'new\');" onmouseover="javascript:do_hover_change(this,2);" onmouseout="javascript:do_hover_change(this,1);"><img alt="New" title="New" src="images/template/img_copy.gif"></td>';
					$result.=   '<td width="10%" onclick="javascript:do_hover_change(this,1);do_page_fetch(' . $id . ',\'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>'.
								'<td width="10%" onclick="javascript:do_hover_change(this,1);do_page_fetch(' . $id . ',\'copy\');" onmouseover="javascript:do_hover_change(this,2);" onmouseout="javascript:do_hover_change(this,3);"><img alt="Copy" title="Copy" src="images/template/img_copy.gif"></td>'.
				  	   			'<td width="10%" onclick="javascript:do_hover_change(this,1);do_page_fetch(' . $id . ',\'delete\');" 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" width="10">&nbsp;</td>'.
				  	  '</tr>'.
				  	'</table>'.
				  '</td>';
			//$result.=	 '<td width="240" align="right" id="img_save" style="display:none"><input type="image" src="images/template/img_savel.gif" alt="save" title="save" border="0">&nbsp;&nbsp;<a href="javascript:do_page_fetch(' . $id .',\'close_sales\');"><img src="images/template/img_closel.gif" alt="close" title="close" border="0"></a>&nbsp;&nbsp;</td>';
			$result.=	 '<td width="70" align="right" id="img_save" style="display:none">' . 
							'<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td  align="right">' .'<img  src="images/template/img_savel.gif" onclick="ValidateForm();" alt="save" title="save" border="0"></td><td  align="right"><img src="images/template/img_closel.gif" alt="close" onclick="do_page_fetch(' . $id . ',\'close_sales\');" title="close" border="0">&nbsp;&nbsp;</td></tr></table>'.
							'</td>';
			if($salemaker_sales_result['sale_discount_type']=='S')
				$sales_discount_type=TEXT_SALES_MAKER;
			else if($salemaker_sales_result['sale_discount_type']=='C')	
				$sales_discount_type=TEXT_CUSTOMER_CHOICE;
			
			$result.=	'</tr>';
					$result.= '<tr><td width="40" valign="top" align="center">&nbsp;</td><td colspan="4"><div id="span_details" class="openContent"><table cellpadding="2" cellspacing="2" border="0" width="100%">';
					$result.='<tr><td colspan="4">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr>';
					$result.='<tr><td class="main" width="125">' . TEXT_SALEMAKER_NAME . '</td><td class="main" width="175">' . $salemaker_sales_result['sale_name'] . '</td><td class="main" width="125">' . TEXT_SALEMAKER_DEDUCTION . '</td><td class="main">' . $salemaker_sales_result['sale_deduction_value'] . '</td><tr>';
					$result.='<tr><td class="main" width="125">' . TEXT_SALEMAKER_DATE_START . '</td><td class="main" width="175">' . (($salemaker_sales_result['sale_date_start'] == '0000-00-00') ? TEXT_SALEMAKER_IMMEDIATELY : format_date($salemaker_sales_result['sale_date_start'])) . '</td><td class="main" width="125">' . TEXT_SALEMAKER_DATE_END . '</td><td class="main">' . (($salemaker_sales_result['sale_date_end'] == '0000-00-00') ? TEXT_SALEMAKER_NEVER : format_date($salemaker_sales_result['sale_date_end'])) . '</td><tr>';
					$result.='<tr><td class="main" width="125">' . TEXT_SALEMAKER_DEDUCTION_TYPE . '</td><td class="main" width="175">' . $deduction_type_array[$salemaker_sales_result['sale_deduction_type']]['text'] . '</td><td class="main" width="125">' . TEXT_DISCOUNT_TYPE . '</td><td class="main">' . $sales_discount_type . '</td><tr>';
					$result.='<tr><td colspan="4">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr><tr><td class="main" colspan="4"><b>' . TEXT_TIPS . '</b></td></tr>';
					$result.='<tr><td colspan="4" class="main"><div style="width:80%;height:100;overflow:auto;">' . INFO_TEXT . '</div></td></tr>';
					$result.='<tr><td colspan="4">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr></table></div></td></tr>';
					
					$result.='<tr><td colspan="5"><span id="span_edit" style="display:none" class="openContent"></span></td></tr>';
					$result.='<tr><td colspan="5"><span id="span_copy" style="display:none" class="openContent"></span></td></tr>';
					$result.='<tr><td colspan="5"><span id="span_delete" style="display:none" class="openContent"></span></td></tr>';
			$result .='</table>';
			return $result;
	}

	function get_edit_details($id){
		global $deduction_type_array,$specials_condition_array,$languages_id,$sInfo;
		$salemaker_sales_query = tep_db_query("select sale_id, sale_status, sale_name,choice_text,choice_warning,sale_discount_type, sale_deduction_value, sale_deduction_type, sale_pricerange_from, sale_pricerange_to, sale_specials_condition, sale_categories_selected, sale_categories_all, sale_date_start, sale_date_end, sale_date_added, sale_date_last_modified, sale_date_status_change,sale_products_selected,apply_to_cross_sale from " . TABLE_SALEMAKER_SALES . " where sale_id='" . $id . "'");
		$salemaker_sales_result = tep_db_fetch_array($salemaker_sales_query); 
		if(tep_db_num_rows($salemaker_sales_query)>0)
			$sInfo=new objectInfo($salemaker_sales_result);
		$row=0; 
		if($sInfo->sale_discount_type=='') 
			$sInfo->sale_discount_type='S';
		?>
		<table cellpadding="3" cellspacing="3" border="0" width="100%" align="center">	  
			<tr id="error_sales" style="display:none" class="main" align="center"><td colspan="2"><font color="#ff0000"><?php echo ERROR_SALE_MAKER;?></font></td></tr>
			<tr valign="top">
				<td valign="top">
					
					<table border="0" cellspacing="0" cellpadding="2">
						<tr>
							<td class="main" width="200"><?php echo TEXT_SALEMAKER_NAME; ?>&nbsp;</td>
							<td class="main"><?php echo tep_draw_input_field('name', $sInfo->sale_name, 'size="37"'); ?></td>
						</tr>
						 <tr>
							<td class="main"><?php echo TEXT_DISCOUNT_TYPE; ?>&nbsp;</td>
							<td class="main"><?php echo tep_draw_radio_field('discount_type', 'S',($sInfo->sale_discount_type=='S')?true:false,'',' onClick="javascript:discount_action();"') . TEXT_SALES_MAKER . '&nbsp;' . tep_draw_radio_field('discount_type', 'C',($sInfo->sale_discount_type=='C')?true:false,'',' onClick="javascript:discount_action();"') . TEXT_CUSTOMER_CHOICE; ?></td>
						 </tr>
						 <?php $style='style="display:none"';
						 	if($sInfo->sale_discount_type=='C')
								$style='style="display:\'\'"';
								
						 ?>
						<tr id="choice_text" <?php echo $style;?>>
							<td class="main" width="200"><?php echo TEXT_CHOICE_TEXT; ?>&nbsp;</td>
							<td class="main"><?php echo tep_draw_input_field('txt_choice_text', $sInfo->choice_text, 'size="37"'); ?></td>
						</tr>
						<tr id="choice_warning" <?php echo $style;?>>
							<td class="main" width="200"><?php echo TEXT_CHOICE_WARNING; ?>&nbsp;</td>
							<td class="main"><?php echo tep_draw_input_field('txt_choice_warning', $sInfo->choice_warning, 'size="37"'); ?></td>
						</tr>
						
						<tr>
						<td colspan="2">
						<table border="0"  cellspacing="0" cellpadding="2">
						<?php
						$categories_query = tep_db_query("select c.categories_id, c.parent_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . $languages_id . "'");
						$categories_array = array();
						while($categories = tep_db_fetch_array($categories_query)) {
						  $categories_array[] = $categories;
						}
						$n = sizeof($categories_array);
						for($i = 0; $i < $n; $i++) {
						  $categories_array[$i]['path'] = $categories_array[$i]['categories_id'];
						  $categories_array[$i]['indent'] = 0;
						  $parent = $categories_array[$i]['parent_id'];
						  while($parent != 0) {
							$categories_array[$i]['indent']++;
							for($j = 0; $j < $n; $j++) {
							  if($categories_array[$j]['categories_id'] == $parent) {
								$categories_array[$i]['path'] = $parent . '_' . $categories_array[$i]['path'];
								$parent = $categories_array[$j]['parent_id'];
								break;
							  }
							}
						  }
						  $categories_array[$i]['path'] = '0_' . $categories_array[$i]['path'];
						}
						
						$order_changed = true;
						while($order_changed) {
						  $order_changed = false;
						  for($i = 0, $n = (sizeof($categories_array) - 1); $i < $n; $i++) {
							if($categories_array[$i]['path'] > $categories_array[$i + 1]['path']) {
							  $tmp = $categories_array[$i];
							  $categories_array[$i] = $categories_array[$i + 1];
							  $categories_array[$i + 1] = $tmp;
							  $order_changed = true;
							}
						  }
						}
						
						
						$main_selected = ($sInfo->sale_categories_all=='' && $sInfo->sale_categories_selected=='' && $sInfo_sale_products_selected=='')?true:false;
						echo "      <tr>\n";
						echo '        <td valign="bottom">' . tep_draw_separator('pixel_trans.gif', '4', '1') . tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif') . "</td>\n";
						echo '        <td class="main"><br>' . TEXT_SALEMAKER_ENTIRE_CATALOG . "</td>\n";
						echo "      </tr>\n";
						echo '      <tr>' . "\n";
						echo '        <td colspan="2" class="main"><label for="all_categories">' . tep_draw_checkbox_field('all_categories', '1', $main_selected,'','onClick="javascript: all_category();"').'&nbsp;&nbsp;'.TEXT_SALEMAKER_TOP . "</label></td>\n";
						echo '        <td class="main">&nbsp;</td>';
						echo "      </tr>\n";
						echo "      <tr>\n";
						echo '        <td valign="bottom">' . tep_draw_separator('pixel_trans.gif', '4', '1') . tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif') . "</td>\n";
						echo '        <td class="main"><br>' . TEXT_SALEMAKER_CATEGORIES . "</td>\n";
						echo "      </tr>\n";
						
						/* str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $category['indent']) . */
						$prefix_category_name="";
						$sale_categories_selected_array = explode(',', $sInfo->sale_categories_selected);
						$sale_categories_all_array = explode(',', $sInfo->sale_categories_all);
						
						foreach($categories_array as $category) {
							if($category['parent_id'] != 0){ 
							$category_path_array = @explode("_",$category['path']);
							$prefix_category_name = "";
								for($i=0;$i<count($category_path_array);$i++){
								$category_name_query = tep_db_query("select categories_name from ".TABLE_CATEGORIES." c, ".TABLE_CATEGORIES_DESCRIPTION." cd where c.categories_id=cd.categories_id and cd.language_id='$languages_id' and cd.categories_id='{$category_path_array[$i]}'");
								$category_name_array = tep_db_fetch_array($category_name_query);
								$prefix_category_name .= "{$category_name_array['categories_name']} > ";
								}
							$prefix_category_name = substr($prefix_category_name,2);
							$prefix_category_name = substr($prefix_category_name,0,-2);
							}else{
							$prefix_category_name = " ".$category['categories_name'];
							}
						 
						  if ( in_array($category['categories_id'],$sale_categories_selected_array) || in_array($category['categories_id'],$sale_categories_all_array) ) 
							$selected = true;						  
						  else 
							$selected = false;
						  
						  
						  $selected = ($selected==true)?'checked':'';
						  // onClick="javascript: CategoryClick(\'' . $category['path'] . '\',\''.$category['categories_id'].'\',\''.$category['categories_name'].'\')"
						  echo '<tr>' . "\n";
						  //echo '<td width="10">' . tep_draw_checkbox_field('categories[]', $category['path'], $selected,'','') . "</td>\n";
						  echo '<td colspan="2" class="main">' . "<input type='checkbox' ".(($main_selected==true)?'disabled':'').' onClick="javascript: CategoryClick1(\'' . $category['path'] . '\',\''.$category['categories_id'].'\',\''.$category['categories_name'].'\')"'." name='categories[]' id='check_box_{$category['categories_id']}' $selected value='{$category['categories_id']}' style='border: medium none ;' class='inputNormal' onblur='javascript:toggle_focus(this,1);' onfocus='javascript:toggle_focus(this,2);'>". "$prefix_category_name</td>\n";
						  echo '<td>&nbsp;</td>';
						  echo '</tr>' . "\n";
						}
						?>
						</table>
						</td>
						</tr>
						
						
					</table>
				</td>
				<td colspan="2">
					<table width="100%" cellpadding="3" cellspacing="3" border="0">
						<tr>
							<td class="main"><?php echo TEXT_SALEMAKER_DEDUCTION; ?>&nbsp;</td>
							<td class="main"><?php echo tep_draw_input_field('deduction', $sInfo->sale_deduction_value, 'size="8"') . TEXT_SALEMAKER_DEDUCTION_TYPE . tep_draw_pull_down_menu('type', $deduction_type_array, $sInfo->sale_deduction_type); ?></td>
						</tr>
						<tr>
							<td class="main"><?php echo TEXT_SALEMAKER_PRICERANGE_FROM; ?>&nbsp;</td>
							<td class="main"><?php echo tep_draw_input_field('from', $sInfo->sale_pricerange_from, 'size="8"') . TEXT_SALEMAKER_PRICERANGE_TO . tep_draw_input_field('to', $sInfo->sale_pricerange_to, 'size="8"'); ?></td>
						</tr>
						<tr>
							<td class="main"><?php echo TEXT_SALEMAKER_SPECIALS_CONDITION; ?>&nbsp;</td>
							<td class="main"><?php echo tep_draw_pull_down_menu('condition', $specials_condition_array, $sInfo->sale_specials_condition); ?></td>
						</tr>
						<tr>
							<?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
							<td class="main"><?php echo TEXT_SALEMAKER_DATE_START . '('.$date_format . ')'; ?>&nbsp;</td>
							<td class="main" align="left"><?php echo tep_draw_input_field("start",format_date($sInfo->sale_date_start),"size=10",false,'text',false);?>
								<a href="javascript:show_calendar('sales.start',null,null,'<?php echo $date_format;?>');"
								onmouseover="window.status='Date Picker';return true;"
								onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/ align="absmiddle">  
							</a>
							</td>
						</tr>
						<tr>
							<td class="main"><?php echo TEXT_SALEMAKER_DATE_END. '('.$date_format . ')'; ?>&nbsp;</td>
							<td class="main" align="left"><?php echo  tep_draw_input_field("end",format_date($sInfo->sale_date_end),"size=10",false,'text',false);?>
								<a href="javascript:show_calendar('sales.end',null,null,'<?php echo $date_format;?>');"
								onmouseover="window.status='Date Picker';return true;"
								onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/ align="absmiddle">  
							</a>
							</td>
						</tr>	
						<tr>
							<td colspan="2" class="main"><?php echo tep_draw_checkbox_field('apply_to_cross_sale','Y',($sInfo->apply_to_cross_sale=='Y')?true:false) . '&nbsp;' . TEXT_APPLY_TO_CROSS_SALES;?></td>
						</tr>	
						<tr>
							<td colspan="2" class="main"><font color="#FF0000"><?php echo TEXT_FORCED_ITEM_WARNING;?></font></td>
						</tr>	
					</table>
			</td>
		</tr>
		
		<tr>
		<td id="ajax_sales_maker">
		<div id="head_ajax_sales_maker" class="main" style="padding-bottom:3px;padding-left:5px;"><?php echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif').TEXT_SALEMAKER_PRODUCTS; ?></div>
		<?php echo list_sales_maker_categories(); ?></td>
		</tr>
		
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif','10','10');?></td></tr>
	</table>
<?php	}

	function list_sales_maker_categories(){
	global $languages_id,$sInfo;
	if($sInfo->sale_categories_all !=""){
	$sale_categories_all = $sInfo->sale_categories_all;
	
	if(substr($sale_categories_all,-1)==',')
		$sale_categories_all = substr($sInfo->sale_categories_all,0,-1);
	if($sale_categories_all{0}==',')
		$sale_categories_all = substr($sale_categories_all,1);
	
	$sales_categories_query = tep_db_query("select cd.categories_id,cd.categories_name from ".TABLE_CATEGORIES." c, ".TABLE_CATEGORIES_DESCRIPTION." cd where c.categories_id=cd.categories_id and cd.language_id='$languages_id' and cd.categories_id in ($sale_categories_all) ");
	while($sales_categories_array = tep_db_fetch_array($sales_categories_query)){
	?>
	<div class="main" id="cat_<?php echo $sales_categories_array['categories_id']; ?>" onClick="javascript:fetch_product('<?php echo $sales_categories_array['categories_id']; ?>')">
	<label for="category_<?php echo $sales_categories_array['categories_id']; ?>"><input name="category_list[<?php echo $sales_categories_array['categories_id']; ?>][]" checked="checked" id="category_<?php echo $sales_categories_array['categories_id']; ?>" value="<?php echo $sales_categories_array['categories_id']; ?>" type="checkbox">&nbsp;<?php echo $sales_categories_array['categories_name']; ?></label>
	</div>
	<div class="main" id="ajax_cat_<?php echo $sales_categories_array['categories_id']; ?>"></div>
	<?php
	}
	}
	if($sInfo->sale_categories_selected!=''){
	$sale_categories_selected = $sInfo->sale_categories_selected;
	if(substr($sale_categories_selected,-1)==',')
		$sale_categories_selected = substr($sInfo->sale_categories_selected,0,-1);
	if($sale_categories_selected{0}==',')
		$sale_categories_selected = substr($sale_categories_selected,1);
	
	$sales_categories_query = tep_db_query("select cd.categories_id,cd.categories_name from ".TABLE_CATEGORIES." c, ".TABLE_CATEGORIES_DESCRIPTION." cd where c.categories_id=cd.categories_id and cd.language_id='$languages_id' and cd.categories_id in ($sale_categories_selected) ");
	while($sales_categories_array = tep_db_fetch_array($sales_categories_query)){
	?>
	<div class="main" id="cat_<?php echo $sales_categories_array['categories_id']; ?>" onClick="javascript:fetch_product('<?php echo $sales_categories_array['categories_id']; ?>')">
	<label for="category_<?php echo $sales_categories_array['categories_id']; ?>"><input name="category_list[<?php echo $sales_categories_array['categories_id']; ?>][]" id="category_<?php echo $sales_categories_array['categories_id']; ?>" value="<?php echo $sales_categories_array['categories_id']; ?>" type="checkbox">&nbsp;<?php echo $sales_categories_array['categories_name']; ?></label>
	</div>
	<div class="main" id="ajax_cat_<?php echo $sales_categories_array['categories_id']; ?>"><?php echo fetch_product($sales_categories_array['categories_id']); ?></div>
	<?php
	}
	}
	}
	function save_details($id){ 
		global $HTTP_POST_VARS;
        $sources_name = tep_db_prepare_input($HTTP_POST_VARS['sources_name']);
        $sql_data_array = array('sources_name' => $sources_name);
		$error=false;
		
        /*if (tep_not_null($categories)) {
	      $categories_selected = array();
          $categories_all = array();
		 
          foreach(tep_db_prepare_input($categories) as $category_path) {
		  	$pop=explode('_', $category_path);
			$category = array_pop($pop);
			$categories_selected[] = $category;
            $categories_all[] = $category;
            foreach(tep_get_category_tree($category) as $subcategory) {
              if ($subcategory['id'] != '0') {
                $categories_all[] = $subcategory['id'];
              }
            }
          }
          asort($categories_selected);
          $categories_selected_string = implode(',', array_unique($categories_selected));
          asort($categories_all);
          $categories_all_string = ',' . implode(',', array_unique($categories_all)) . ',';
        } else {
          $categories_selected_string = 'null';
          $categories_all_string = 'null';
        }*/
		$categories=$HTTP_POST_VARS['categories'];
		$categories_all_string=","; $categories_selected_string=","; $products_selected_string=",";
		$all_categories = isset($HTTP_POST_VARS['all_categories'])?$HTTP_POST_VARS['all_categories']:'';
		if($all_categories==1){
		$categories_all_string = '';
		}else{
		$category_list = isset($HTTP_POST_VARS['category_list'])?$HTTP_POST_VARS['category_list']:'';
		$products = isset($HTTP_POST_VARS['products'])?$HTTP_POST_VARS['products']:'';
		for($i=0,$n=sizeof($categories);$i<$n;$i++){
			
			if(is_array($category_list) &&  array_key_exists($categories[$i],$category_list)){
			$categories_all_string .= "{$categories[$i]},";
			}else{
				if(is_array($products[$categories[$i]]))
					while(list($key,$val) = each($products[$categories[$i]]))
						$products_selected_string .= "$val,";
			$categories_selected_string .= "{$categories[$i]},";
			}
		}
		
		//$categories_all_string = substr($categories_all_string,1);	
		//$categories_selected_string = substr($categories_selected_string,1);
		//$products_selected_string = substr($products_selected_string,1);
		}
		if($categories_all_string==',')
			$categories_all_string='';
		if($categories_selected_string==',')
			$categories_selected_string='';
		if($products_selected_string==',')
			$products_selected_string='';
		$sel_product_list='';	
			
			$select_cat_ids=substr($categories_all_string,1,-1);
			
			if($categories_all_string!='') {
				$sWhere=" where categories_id in(" .$select_cat_ids . ")";
				$select_cat_query=tep_db_query("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . $sWhere);
				while($select_cat_result=tep_db_fetch_array($select_cat_query)) {
					$sel_product_list.=$select_cat_result['products_id'] . ",";
				}
				if($sel_product_list!='') $sel_product_list=substr($sel_product_list,0);
			}
			
		
		
		$select_pro_ids=substr($products_selected_string,1);
		if($select_pro_ids!='')
			$sel_product_list.= $select_pro_ids;
		$sel_product_list=substr($sel_product_list,0,-1);	
		if($sel_product_list!='') {	
			$sales_products_query=tep_db_query("select p.products_price_break from " . TABLE_PRODUCTS . " p where p.products_price_break='Y' and products_id in (" . $sel_product_list . ")");
			if(tep_db_num_rows($sales_products_query)>0) 
				$error=true;
		}	
		if(!$error)  {
			$start_date = tep_convert_date_raw(tep_db_prepare_input($HTTP_POST_VARS['start']));
			/*if($start_date!='')
				$start_date = (getServerDate() < $start_date) ? $start_date : getServerDate(); */
				
			$end_date = tep_convert_date_raw(tep_db_prepare_input($HTTP_POST_VARS['end']));
			$apply_to_cross_sale=tep_db_prepare_input($HTTP_POST_VARS['apply_to_cross_sale']);
			if($apply_to_cross_sale!='Y')
				$apply_to_cross_sale='N';
			/*if($end_date!='')
				$end_date = (getServerDate() < $end_date) ? $end_date : getServerDate(); */
	
			$salemaker_sales_data_array = array('sale_name' => tep_db_prepare_input($HTTP_POST_VARS['name']),
												'sale_deduction_value' => tep_db_prepare_input($HTTP_POST_VARS['deduction']),
												'sale_deduction_type' => tep_db_prepare_input($HTTP_POST_VARS['type']),
												'sale_pricerange_from' => tep_db_prepare_input($HTTP_POST_VARS['from']),
												'sale_pricerange_to' => tep_db_prepare_input($HTTP_POST_VARS['to']),
												'sale_specials_condition' => tep_db_prepare_input($HTTP_POST_VARS['condition']),
												'sale_categories_selected' => $categories_selected_string,
												'sale_products_selected' => $products_selected_string,
												'sale_categories_all' => $categories_all_string,
												'sale_date_start' => (($start_date == '') ? '0000-00-00' : $start_date),
												'sale_date_end' => (($end_date == '') ? '0000-00-00' : $end_date),
												'sale_discount_type'=> tep_db_prepare_input($HTTP_POST_VARS['discount_type']),
												'apply_to_cross_sale'=>$apply_to_cross_sale);
					$salemaker_sales_data_array['choice_text']='';
					$salemaker_sales_data_array['choice_warning']='';		
				if($HTTP_POST_VARS['discount_type']=='C') {
					$salemaker_sales_data_array['choice_text']=tep_db_prepare_input($HTTP_POST_VARS['txt_choice_text']);
					$salemaker_sales_data_array['choice_warning']=tep_db_prepare_input($HTTP_POST_VARS['txt_choice_warning']);
				}
												
			  if($salemaker_sales_data_array['sale_deduction_value'] == "") $salemaker_sales_data_array['sale_deduction_value']=0;
			  if($salemaker_sales_data_array['sale_pricerange_from'] == "") $salemaker_sales_data_array['sale_pricerange_from']=0;
			  if($salemaker_sales_data_array['sale_pricerange_to'] == "") $salemaker_sales_data_array['sale_pricerange_to']=0;
												
			if ($id<=0) {
			  $salemaker_sales['sale_status'] = 0;
			  $salemaker_sales_data_array['sale_date_added'] = 'now()';
			  $salemaker_sales_data_array['sale_date_last_modified'] = '0000-00-00';
			  $salemaker_sales_data_array['sale_date_status_change'] = '0000-00-00';
			  tep_db_perform(TABLE_SALEMAKER_SALES, $salemaker_sales_data_array, 'insert');
			  $id = tep_db_insert_id();
			  $new=true;
			  echo $id . '{|}';
			} elseif ($id>0) { 
			  $salemaker_sales_data_array['sale_date_last_modified'] = 'now()';
			  tep_db_perform(TABLE_SALEMAKER_SALES, $salemaker_sales_data_array, 'update', "sale_id = '" . $id . "'");
			  $new=false;
			}
			 echo get_updated_details($id,$new);
		} else {
			echo 'Error';			
		}
	}
	function get_updated_details($id,$new){
		global $languages_id,$deduction_type_array;
		$salemaker_sales_query = tep_db_query("select sale_id, sale_status, sale_name, sale_discount_type,sale_deduction_value, sale_deduction_type, sale_pricerange_from, sale_pricerange_to, sale_specials_condition, sale_categories_selected, sale_categories_all, sale_date_start, sale_date_end, sale_date_added, sale_date_last_modified, sale_date_status_change,apply_to_cross_sale from " . TABLE_SALEMAKER_SALES . " where sale_id='" . $id . "'");
		$salemaker_sales_result = tep_db_fetch_array($salemaker_sales_query); 
		
		if ($salemaker_sales_result['sale_status'] == '1') {
      	// $status='<a href="javascript:change_status(\''.$salemaker_sales_result['sale_id'] . '\',\'0\'); ">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', '', 10, 10) . '</a>';
		 $status=tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', '', 10, 10,'onClick="javascript:change_status(\''.$salemaker_sales_result['sale_id'] . '\',\'0\'); "');
		 $sstatus= tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', '', 10, 10);
		  } else {
		   //$status='<a href="javascript:change_status(\''.$salemaker_sales_result['sale_id'] . '\',\'1\'); ">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', '', 10, 10) . '</a>';
		   $status=tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', '', 10, 10,'onClick="javascript:change_status(\''.$salemaker_sales_result['sale_id'] . '\',\'1\'); "');
		   $sstatus= tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', '', 10, 10);
		  }
	  
		if($new){
				$result='<div id="head_'. $salemaker_sales_result['sale_id'] .'">'. $status. tep_draw_separator('pixel_trans.gif','10','10') . $salemaker_sales_result['sale_name'].'</div><div id="data_'. $salemaker_sales_result['sale_id'].'" style="display:none"></div>';
		} else {
			if($salemaker_sales_result['sale_discount_type']=='S')
				$sales_discount_type=TEXT_SALES_MAKER;
			else if($salemaker_sales_result['sale_discount_type']=='C')	
				$sales_discount_type=TEXT_CUSTOMER_CHOICE;
			$result='<table cellpadding="2" cellspacing="2" border="0" width="100%">';
					$result.='<tr><td colspan="4">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr>';
					$result.='<tr><td class="main" width="125">' . TEXT_SALEMAKER_NAME . '</td><td class="main" width="175">' . $salemaker_sales_result['sale_name'] . '</td><td class="main" width="125">' . TEXT_SALEMAKER_DEDUCTION . '</td><td class="main">' . $salemaker_sales_result['sale_deduction_value'] . '</td><tr>';
					$result.='<tr><td class="main" width="125">' . TEXT_SALEMAKER_DATE_START . '</td><td class="main" width="175">' . (($salemaker_sales_result['sale_date_start'] == '0000-00-00') ? TEXT_SALEMAKER_IMMEDIATELY : format_date($salemaker_sales_result['sale_date_start'])) . '</td><td class="main" width="125">' . TEXT_SALEMAKER_DATE_END . '</td><td class="main">' . (($salemaker_sales_result['sale_date_end'] == '0000-00-00') ? TEXT_SALEMAKER_NEVER : format_date($salemaker_sales_result['sale_date_end'])) . '</td><tr>';
					$result.='<tr><td class="main" width="125">' . TEXT_SALEMAKER_DEDUCTION_TYPE . '</td><td class="main" width="175">' . $deduction_type_array[$salemaker_sales_result['sale_deduction_type']]['text'] . '</td><td class="main" width="125">' . TEXT_DISCOUNT_TYPE . '</td><td class="main">' . $sales_discount_type . '</td><tr>';
					$result.='<tr><td colspan="4">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr>';
					$result.='<tr><td class="main" colspan="4"><b>' . TEXT_TIPS . '</b></td></tr>'.
							'<tr><td colspan="4" class="main"><div style="width:80%;height:100;overflow:auto;">' . INFO_TEXT . '</div></td></tr>'.
							'</table>';
		} 
			return $result . '{#}' . $status . tep_draw_separator('pixel_trans.gif','10','10') . $salemaker_sales_result['sale_name'] . '{##}' .  $sstatus . tep_draw_separator('pixel_trans.gif','10','10') . $salemaker_sales_result['sale_name'] ;
	}
	
	function delete_details($id,$delete){
		if($delete){
      		  tep_db_query("delete from " . TABLE_SALEMAKER_SALES . " where sale_id = '" . $id . "'");
		} else {
		$result.='<table cellpadding="2" cellspacing="2" border="0" width="100%">'.
					'<tr><td width="15">' . tep_image('images/template/img_trash.gif') . '</td><td class="inner_title">' . TEXT_INFO_HEADING_DELETE_SALE . '</td></tr>';
						$result.='<tr><td width="15"></td><td class="main">' . TEXT_INFO_DELETE_INTRO . '</td></tr>';
					$result.='<tr><td colspan="2">'. tep_draw_separator('pixel_trans.gif','10','10') . '</td<tr>'.
					'</table>' ;
						return $result;
		}	
	}
	
	function get_status_change($id){
		global $flag;
			$salemaker_data_array = array('sale_status' => tep_db_prepare_input($flag),
										  'sale_date_last_modified' => 'now()',
										  'sale_date_status_change' => 'now()');
		
			tep_db_perform(TABLE_SALEMAKER_SALES, $salemaker_data_array, 'update', "sale_id = '" . tep_db_prepare_input($id) . "'");
			
		$salemaker_sales_query = tep_db_query("select sale_id, sale_status, sale_name,sale_discount_type, sale_deduction_value, sale_deduction_type, sale_pricerange_from, sale_pricerange_to, sale_specials_condition, sale_categories_selected, sale_categories_all, sale_date_start, sale_date_end, sale_date_added, sale_date_last_modified, sale_date_status_change,apply_to_cross_sale from " . TABLE_SALEMAKER_SALES . " where sale_id='" . $id . "'");
		$salemaker_sales_result = tep_db_fetch_array($salemaker_sales_query); $row=0;

			if ($salemaker_sales_result['sale_status'] == '1') {
				   //$status='<a href="javascript:change_status(\''.$id . '\',\'0\'); ">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', '', 10, 10) . '</a>';
				   $status=tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', '', 10, 10,'onClick="javascript:change_status(\''.$id . '\',\'0\'); "');
				  } else {
				  //$status='<a href="javascript:change_status(\''.$id . '\',\'1\'); ">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', '', 10, 10) . '</a>';
				   $status=tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', '', 10, 10,'onClick="javascript:change_status(\''.$id . '\',\'1\'); "');
				  }		
			return $status . tep_draw_separator('pixel_trans.gif','10','10') . $salemaker_sales_result['sale_name'];
	}
	function copy_details($id,$copy){
		global $HTTP_POST_VARS;
		$salemaker_sales_query = tep_db_query("select sale_id, sale_status, sale_name, sale_deduction_value, sale_deduction_type, sale_pricerange_from, sale_pricerange_to, sale_specials_condition, sale_categories_selected, sale_categories_all, sale_date_start, sale_date_end, sale_date_added, sale_date_last_modified, sale_date_status_change,apply_to_cross_sale from " . TABLE_SALEMAKER_SALES . " where sale_id='" . $id . "'");
		$salemaker_sales_result = tep_db_fetch_array($salemaker_sales_query); 
		if(!$copy){
		$result='<table cellpadding="2" cellspacing="2" border="0" width="100%">'.
					'<tr><td width="15">' . tep_image('images/template/img_copy.gif') . '</td><td class="inner_title">' . TEXT_INFO_HEADING_COPY_SALE . '</td></tr>';
						$result.='<tr><td width="15"></td><td class="main">' . 
										'<table cellpadding="0" cellspacing="0" border="0" width="100%">'.
											'<tr><td>'.
											'<tr>'.
													'<td class="main" width="200">'. sprintf(TEXT_INFO_COPY_INTRO, $salemaker_sales_result['sale_name']). '</td>'.
													'<td class="main">' . tep_draw_input_field('name', '', 'size="37"') . '</td>'.
											'</tr>'.
										'</table>';
								 '</td></tr>';
					$result.='<tr><td colspan="2">'. tep_draw_separator('pixel_trans.gif','10','10') . '</td<tr>'.
					'</table>';
		
			return $result;		
		} else {
            $salemaker_sales_result['sale_id'] = '';
            $salemaker_sales_result['sale_name'] = $HTTP_POST_VARS['name'];
            //$salemaker_sales_result['sale_status'] = 0;
            $salemaker_sales_result['sale_date_added'] = 'now()';
            $salemaker_sales_result['sale_date_last_modified'] = '0000-00-00';
            $salemaker_sales_result['sale_date_status_change'] = '0000-00-00';
			
			  tep_db_perform(TABLE_SALEMAKER_SALES, $salemaker_sales_result, 'insert');
			  $id = tep_db_insert_id();
			  $new=true;
			  echo $id . '{|}';
			   echo get_updated_details($id,$new);
		}	

	}
?>
