<?php
/*

osCommerce, Open Source E-Commerce Solutions 
http://www.oscommerce.com 

Copyright (c) 2003 osCommerce 

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

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

Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  require('includes/classes/split_page_results_event.php');
  require('includes/languages/' . $language . '/products_specials.php');
  $currencies = new currencies();
  $server_dates = getServerDate(true);  
  $page = isset($HTTP_GET_VARS['page'])?(int)$HTTP_GET_VARS['page']:1;
  $command=isset($HTTP_GET_VARS['command'])?tep_db_prepare_input($HTTP_GET_VARS['command']):'';
  $sId = (($HTTP_GET_VARS['sId'])?tep_db_prepare_input($HTTP_GET_VARS['sId']):tep_db_prepare_input($HTTP_POST_VARS['specials_id']));
  if($command != ''){ 
	if($command=='get_details'){ 
		echo_get_details();
	 }else if($command=='setflag'){
	 	$status=$HTTP_GET_VARS['flag'];
	 	 if ($status == '1') 
			tep_db_query("update " . TABLE_SPECIALS . " set status = '1' where specials_id = '" . $sId . "'");
		 elseif ($status == '0') 
			tep_db_query("update " . TABLE_SPECIALS . " set status = '0', date_status_change = '" . $server_dates . "' where specials_id = '" . $sId . "'");
	 }else if($command=='delete_confirm'){
       $special_delete_query=tep_db_query("delete from " . TABLE_SPECIALS . " where specials_id = '" . $sId . "'");		
	   echo 'success_delete'; 
	}else if($command=='edit'){		
		if(!$HTTP_GET_VARS['sId']) echo 'new';
		else if($HTTP_GET_VARS['sId']) echo 'edit';
		edit_function();
	}else if($command=='update'){  								
		if($HTTP_POST_VARS['action_type']=='edit') $HTTP_POST_VARS['products_id']=$HTTP_POST_VARS['products_id_assign'];
		$products_id = tep_db_prepare_input($HTTP_POST_VARS['products_id']);		
        $products_price = tep_db_prepare_input($HTTP_POST_VARS['products_price']);
        $specials_price = tep_db_prepare_input($HTTP_POST_VARS['specials_price']);
	    $expires_dates = ($HTTP_POST_VARS["txt_date_begin"])?tep_convert_date_raw($HTTP_POST_VARS["txt_date_begin"]):$server_dates;						
		if($HTTP_POST_VARS['action_type']=='new') $command='insert';		
	    //CGDiscountSpecials start
		$checkbox_customers_groups = tep_db_prepare_input($HTTP_POST_VARS['checkbox_customers_groups']);
        $customers_groups = tep_db_prepare_input($HTTP_POST_VARS['customers_groups']);
		if ($checkbox_customers_groups == false) $customers_groups = 0;
		$checkbox_customers = tep_db_prepare_input($HTTP_POST_VARS['checkbox_customers']);
        $customers = tep_db_prepare_input($HTTP_POST_VARS['customers']);
		if ($checkbox_customers == false) $customers = 0;
        //CGDiscountSpecials end
        if (substr($specials_price, -1) == '%') $specials_price = ($products_price - (($specials_price / 100) * $products_price));		
		if ($specials_price=='') $specials_price='0';
		 	$sql_data_array = array('specials_new_products_price' => $specials_price,
									'products_id'=>tep_db_prepare_input($HTTP_POST_VARS['products_id']),
									'customers_groups_id'=>$customers_groups,
									'customers_id' =>$customers, 
									'expires_date' =>$expires_dates
									);
		if ($command == 'insert') {											
			$insert_sql_data = array('specials_date_added' => $server_dates);
			$sql_data_array = array_merge($sql_data_array,$insert_sql_data);
			tep_db_perform(TABLE_SPECIALS, $sql_data_array);
			$sId = tep_db_insert_id();			
			echo 'new_specials';								 				             				
		} if($command == 'update') { 
			 $update_sql_data = array('specials_last_modified' => $server_dates);				 
			 $sql_data_array = array_merge($sql_data_array,$update_sql_data);
			 tep_db_perform(TABLE_SPECIALS, $sql_data_array, 'update', "specials_id = '" . (int)$sId . "'");			 			  						 			
		}
		echo_get_details();
	   }else if($command=='page_navigation'){
	   		echo 'page_navigation';
	   		table_write();	
	   }	
    exit;
	}
	?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="javascript" src="includes/menu.js"></script>
<script language="JavaScript" src="includes/date-picker.js"></script>
<?php require('includes/date_format_js.php');?>
<script language="javascript">
var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";	
var max_display="<?php echo MAX_DISPLAY_SPECIAL_PRODUCTS;?>";
var server_date="<?php echo getServerDate();?>";
function check_form(){ 
	  var command="";
	  var frm = document.frm_specials;
	  var id = frm.specials_id.value;
	  var span_details=document.getElementById("span_details");
	  var span_edit=document.getElementById("span_edit");				
	  var span_delete=document.getElementById("span_delete");
   	  var loadings=document.getElementById("loading");
   	  var error_result="";
	  if(frm.action_type) var action_type=frm.action_type.value;
	  if(span_edit && span_edit.style.display=="" || document.getElementById("new_create") && document.getElementById("new_create").innerHTML!=""){	    	
	    	if(span_delete) span_delete.innerHTML="";
	    	var txt_date_begin=document.getElementById("txt_date_begin");
	    	var txt_date_begin=document.getElementById("txt_date_begin");
	  	    var ids="";
	  	    if(server_date) var server_date_obj=date_format(server_date,'y-m-d','y-m-d',true).getTime();
	  	   	if(txt_date_begin && txt_date_begin.value!="") var txt_date_begin_obj=date_format(txt_date_begin.value,'','y-m-d',true).getTime();
	  	    if(txt_date_begin && txt_date_begin.value=="")
	  	    	error_result+="<?php echo ERR_EXPIRY_DATE_EMPTY;?>\n";	  	    	
	  	    else if(server_date_obj>=txt_date_begin_obj)
	  	    	error_result="<?php echo ERR_EXPIRE_DATE_LESSTHAN;?>\n";
	  	    if(error_result!=""){
	  	    	alert(error_result);
	  	    	return false;
	  	    }else {				
				if(span_edit && span_edit.style.display=="" || document.getElementById("new_create") && document.getElementById("new_create").style.display==""){
					if(frm.action_type && frm.action_type.value=='edit')  ids="&sId="+id;													
					command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=update');?>"+ids;
					if(loadings) loadings.style.display=""; 					
					if(span_edit) span_edit.style.display="none";
					if(document.getElementById("new_create")){ document.getElementById("new_create").style.display="none";
					image_new_change();
					}
				}
	  	    }													    					
	  }else if(span_delete && span_delete.style.display==""){ 	
			span_details.innerHTML=span_edit.innerHTML="";
			command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=delete_confirm');?>&sId="+id;						
			if(span_delete) span_delete.style.display="none";			
	  }
	if(command!="") do_post_command('frm_specials',command);
	return false;
  }
 
function check_key(e){ 
	var kc=0;
	if (window.event)
	kc=window.event.keyCode;
	else if (e)
	kc=e.which;
	else
	kc=0;
	if(kc==13) expand_category(1,'1');
 }	 
function image_new_change(){
	document.getElementById('img_new').firstChild.src=img_src+'template/img_new.gif';
//document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
display_flag=1;
}	
var previous_id='';
 function expand_category(id,category,r_type,sta){ 
 if(id==0 && category==2){
 	display_flag=0;
	document.getElementById('img_new').firstChild.src=img_src+'template/img_new_hover.gif';
	//document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('.gif','_hover.gif');
 	if(document.getElementById('no_record'))
		document.getElementById('no_record').style.display='none';
 	//display_flag=0;
 }
 if(id==0 && category==1)
 	if(document.getElementById('no_record'))
		document.getElementById('no_record').style.display='';
 	image_new_change();
		if(category==3 && document.getElementById('head_'+id) && document.getElementById('head_'+id).style.display!='none' && document.getElementById("span_edit") && document.getElementById("span_edit").style.display=='none' && document.getElementById("span_delete") && document.getElementById("span_delete").style.display=='none') return;
		if(!sta) sta="";
		var obj=document.getElementById("page_1");
		if(category=='1' && obj && sta!=""){
 	 		id=parseInt(obj.options[obj.selectedIndex].value);
 	 		if(sta=='next') {
 	 			if(obj.length==obj.selectedIndex+1) return;
 	 			id+=1;
 	 		}else if(sta=='prev') {
 	 			if(obj.selectedIndex==0) return;
 	 			id-=1;
 	 		}
 	 		if(id<=1) id=1;
 	 	}else if(category=='1' && sta=="") id="";
 	 	if(category==1 && document.getElementById("new_create").style.display=="")	{
 	 		document.getElementById("new_create").innerHTML="";
 	 		document.getElementById("new_create").style.display="none";
 	 	}
 	 	if(category==2 && id==0 && document.frm_specials.specials_id && document.frm_specials.specials_id.value>0 && document.getElementById("head_"+document.frm_specials.specials_id.value) && document.getElementById("data_"+document.frm_specials.specials_id.value)){
			if(span_edit) span_edit.innerHTML="";
			document.getElementById("head_"+document.frm_specials.specials_id.value).style.display="none";
			document.getElementById("data_"+document.frm_specials.specials_id.value).style.display="";
			if(document.getElementById("new_create")) document.getElementById("new_create").style.display="";						
 	 	}
 	 	var res_display=document.getElementById("res_display"); 	 	 	 	 	 	 	 	 	 		 	  	 	
 		if(res_display) res_display.style.display="none"; 	 		 	 	
 	 	 if(category!=1 && document.frm_specials && document.frm_specials.specials_id && document.frm_specials.specials_id.value>0)  { 
			var old_ids=document.frm_specials.specials_id.value;			
			var pre_data_id=document.getElementById("data_"+old_ids);
			var pre_head=document.getElementById("head_"+old_ids);			
			var loadings=document.getElementById("loading");
			if(loadings) loadings.style.display="none";
			if(category==1 && pre_head && pre_head.innerHTML!='') pre_head.innerHTML="";									
		 }
			if(id>0 && document.frm_specials.specials_id.value>0 && document.frm_specials.specials_id.value!=id){			   
			   if(pre_head && pre_head.style.display=="") pre_head.style.display="none";
			   if(pre_data_id && pre_data_id.style.display=="none") pre_data_id.style.display="";
			   document.frm_specials.specials_id.value=id;			   
			}
		if(id>0 && category!=1 && document.frm_specials && document.frm_specials.specials_id && document.frm_specials.specials_id.value=="") document.frm_specials.specials_id.value=id; 	 	
 	 	if(category){ 
 	 		var span_details=document.getElementById("span_details");
 	 		var span_edit=document.getElementById("span_edit");			
			var span_delete=document.getElementById("span_delete");								
			var data_id=document.getElementById("data_"+id);	
			var head=document.getElementById("head_"+id);
			var img_save=document.getElementById("img_save");			
			var command="";																		 
 	 		switch(category){ 	 			
 	 			case '1':
 	 			       var ser="";
 	 			       var page="";
 	 			       if(document.getElementById("search") && document.getElementById("search").value!="" && sta=="" && !r_type) {
					   	ser="&search="+document.getElementById("search").value;
						id="";
					   }else if(document.getElementById("search") && document.getElementById("search").value!="" && sta=="" && r_type=='reset'){
					  	 document.getElementById("search").value="";
					   }else if(document.getElementById("search") && document.getElementById("search").value=="" && sta=="" && !r_type){
						display_flag=1;
						document.getElementById('img_new').firstChild.src=img_src+'template/img_new.gif';
						//document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
						return;
					   }else if(document.getElementById("search") && document.getElementById("search").value=="" && sta=="" && r_type=='reset') id=1;
					   if(id!="") page="&page="+id; 
 	 			       command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=page_navigation');?>"+page+ser;
					   if(document.getElementById("ajax_details")) document.getElementById("ajax_details").innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>"; 	 			   					   
 	 			break;
 	 			case 2: 	 			
 	 				 if(span_edit) span_edit.style.display="";
 	 				 if(document.frm_specials.action_type) document.frm_specials.action_type.value='new';
					 document.getElementById('img_new').firstChild.src=img_src+'template/img_new_hover.gif';
					 //document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace(".gif","_hover.gif");
 	 				 document.getElementById("new_create").style.display="";
					 document.getElementById("new_create").innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>";
 	 				 if(document.frm_specials.action_type) document.frm_specials.action_type.value='new'; 	 					
 	 				 command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=edit');?>"; 	 					 	 					 	 					 	 				 	 		
 	 			break; 	 			
 	 			case 3:
 	 				if(document.getElementById("new_create")) {
 	 			   		document.getElementById("new_create").innerHTML="";
 	 			   		document.getElementById("new_create").style.display="none";
						image_new_change();
 	 			    }
 	 			   if(head && head.style.display==""){
 	 			   	  if(span_details.style.display==""){
 	 			   	    head.style.display="none"; 	 			   	    
 	 			   	  	data_id.style.display=""; 	 			   	  	
 	 			   	  }else { 
 	 			   	  	span_details.style.display="";
 	 			   	  	if(span_edit.style.display=="") span_edit.style.display="none";
 	 			   	  	if(span_delete.style.display=="") span_delete.style.display="none"; 	 			   	  	 	 			   	  	 	 			   	  	
 	 			   	  	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';
 	 			   	  		else if(tr_obj.childNodes[2].childNodes[0].src==img_src+'template/img_trash_hover.gif')	
 	 			   	  			tr_obj.childNodes[2].childNodes[0].src=img_src+'template/img_trash.gif';	 	 			   	  			
 	 			   	  	}	 			   	  	  	 			   	  	 	 			   	  	
 	 			   	  }	 	 			    			   	   	 			   
 	 			 }else {  		 	
				 	if(previous_id!='')
						if(document.getElementById('head_'+previous_id))
							document.getElementById('head_'+previous_id).innerHTML=''; 	 			 	
 	 			 	if(data_id && data_id.style.display=="") data_id.style.display="none";
 	 			 	if(pre_head && pre_head.style.display=="") pre_head.style.display="none"; 	 			 	
 	 			 	if(head) {
 	 			 	 	head.style.display=""; 	 			 	 	 	 			 	  	 			
 	 			 		head.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>";	 			 		
 	 			 	}
 	 			 	command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=get_details');?>&sId="+id; 	 			 	
					previous_id=id;
 	 			 }
 	 			break;
 	 			case 4:
 	 					span_edit.style.display=""; 	 					
 	 					span_details.style.display=span_delete.style.display="none";
 	 					if(document.frm_specials.action_type) document.frm_specials.action_type.value='edit';
 	 					span_edit.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>"; 	 					
 	 					command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=edit');?>&sId="+id; 
 	 			break; 	 			
 	 			case 5:
 	 					span_delete.style.display="";
 	 					span_edit.style.display=span_details.style.display="none";
 	 					span_delete.innerHTML='<table border="0" cellspacing="2" cellpadding="1" width="100%"><tr><td width="17" align="center"><?php echo tep_image('images/template/img_trash.gif')?></td>'+
											  '<td width="500" align="left" class="inner_title">Delete Special</td></tr>'+
											  '<tr><td></td><td width="500" align="left" class="main"><?php echo TEXT_DELETE_INTRO;?></td></tr>'+											  				      					       
											  '<tr><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif',50,10);?></td></tr></table>'; 	 					
 	 					if(img_save) img_save.style.display=""; 
 	 			break; 	
 	 			case 6: 	 				
 	 				if(img_src && id && document.frm_specials.specials_id){
 	 					var specials_id=document.frm_specials.specials_id.value; 	 					
 	 					var data_id=document.getElementById("data_"+specials_id); 	 					
 	 					if(data_id) var img=data_id.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0];
 	 					if(img_src+"template/icon_active.gif"==id.src){
 	 						img.src=id.src=img_src+"template/icon_inactive.gif"; 	 						 	 						
 	 						command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=setflag');?>&sId="+specials_id+"&flag=0";
 	 					}else if(img_src+"template/icon_inactive.gif"==id.src){
 	 						img.src=id.src=img_src+"template/icon_active.gif";
 	 						command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=setflag');?>&sId="+specials_id+"&flag=1";
 	 					} 	 						
 	 				}	 				
 	 			break;	
				case 7:
				 if(head && data_id){
		 	 			  	head.style.display="none";
		 	 			  	head.innerHTML="";
		 	 			  	data_id.style.display="";	
		 	 			  } 
		 	 			break;
				
											
 	 		}
 	 		if(command!="") do_get_command(command);
 	 	}		
 	 }
	 
 	function do_result(result){ 
		 if(result.substr(0,11)=='get_details') print_details('get_details',result.substr(11));
		 else if(result.substr(0,14)=='success_delete') print_details('success_delete',result.substr(14));
		 else if(result.substr(0,5)=='close') print_details('close',result.substr(5));
	     else  if(result.substr(0,4)=='edit') print_details('edit',result.substr(4));		 
		 else if(result.substr(0,12)=='new_specials') print_details('new_specials',result.substr(23));
		 else if(result.substr(0,3)=='new' && result!="" && document.getElementById("new_create"))	{
		 display_flag=0;
			document.getElementById("new_create").style.display="";
			document.getElementById("new_create").innerHTML=result.substr(3);
			
		 }	
		 else if(result.substr(0,15)=='page_navigation') print_details('page_navigation',result.substr(15));
 	}
   
   function print_details(category,result){ 	 	 	
 	 	var no_result=""; 	 	
 	 	var include="";
		if(result.substr(0,15)=='No Record Found'){
 	 		no_result="No Record Found";
 	 	}else if(result!='' && result.length>0 && category!='edit' && result.substr(0,15)!='No Record Found'){
 	 		splt_str=result.split("^"); 	 		
 	 		var specials_id=""; 
 	 		var products_id="";	 		 
 	 		var products_name=""; 	 		
 	 		var products_image_1="";
 	 		var specials_date_added="";
 	 		var specials_last_modified="";
 	 		var products_price="";
 	 		var specials_new_products_price="";
 	 		var expires_date="";
 	 		var date_status_change=""; 
 	 		var products_status="";
 	 		var spcl_string="";
 	 		 specials_id=splt_str[0]; 
 	 		 products_name=splt_str[1];
 	 		 products_image_1=splt_str[2];
 	 		 specials_date_added=splt_str[3];
 	 		 specials_last_modified=splt_str[4];
 	 		 products_price=splt_str[5];
 	 		 specials_new_products_price=splt_str[6];
 	 		 expires_date=splt_str[7];
 	 		 date_status_change=splt_str[8]; 
 	 		 products_status=splt_str[9];
 	 		 products_id=splt_str[10];
 	 		 spcl_string=splt_str[11]; 	 		  	 		  	 		   	 		
 	 	} 	
 	 	if(products_id>0 && document.frm_specials.products_id_assign) document.frm_specials.products_id_assign.value=products_id;
 		if(document.frm_specials && document.frm_specials.specials_id) var id=document.frm_specials.specials_id.value; 		
 		var span_details=document.getElementById("span_details");	
 		var span_edit=document.getElementById("span_edit"); 	 		 	 		
 		var span_delete=document.getElementById("span_delete");	
		var head = document.getElementById("head_"+id);	
 		var data_id=document.getElementById("data_"+id);
 		var res_display=document.getElementById("res_display");
 		var img_save=document.getElementById("img_save");		
		if(img_save && category!='get_details') img_save.style.display="";
		switch(category){		 	 		 	 		
 	 		case 'get_details': 
				var head = 'head_'+specials_id;	
			    head=document.getElementById(head); 
				if(head && no_result!=""){
				head.innerHTML=no_result;
				} 	 			 	 		
 	 		else if(head && no_result==""){ 
	 	 		if(data_id){
	 	 			data_id.innerHTML='<table border="0" cellpadding="3" cellspacing="0" width="100%">'+
						          '<tbody><tr>'+
						         	'<td class="dataTableContent" align="left" width="50%"><img src="images/template/'+((products_status==0)?'icon_inactive.gif':'icon_active.gif')+'" alt="Active" title=" Active " border="0" height="10" width="10">&nbsp;&nbsp;&nbsp;'+products_name+'</td>'+						         				         
						         	'<td class="dataTableContent" width="50%"><span class="oldPrice">'+products_price+'</span><span class="specialPrice">'+specials_new_products_price+' '+ spcl_string+'</span></td>'+			         						  			 
						         '</tr>'+
						      	'</tbody></table>'; 

	 	 		}	 		
	 	 		include="<table width='100%' border='0' cellspacing='0' cellpadding='0'  class='openContent_top' >"+	 	 		
				"<tr heigth='40' > "+				  					   			  
				  "<td align='left' width='10'>&nbsp;<img onclick='javascript:expand_category(this,6);' src='images/template/"+((products_status==0)?'icon_inactive.gif':'icon_active.gif')+"' border='0' height='10' width='10'>&nbsp;&nbsp;</td>"+
				  "<td align='left' valign='middle' width='500' class='main' onclick='javascript:expand_category("+specials_id+",7);'>&nbsp;"+products_name+"</td>"+
				  "<td width='100' align='left' class='main' valign='top'>"+
				  	"<table border='0' cellspacing='0' cellpadding='0' style='height:30px;display:inline;position:relative;'>"+
				  	  "<tr class='img_move' id='img_ids'>"+
				  	  	"<td class='cell_bg_popm_left' width='10' >&nbsp;</td>"+				  	  
				  	   	"<td width='10' onclick='javascript:change_class(this,1);expand_category("+specials_id+",4);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);'><img alt='Edit' title='Edit' src='images/template/img_edit.gif'></td>"+
				  	    "<td width='10' onclick='javascript:change_class(this,1);expand_category("+specials_id+",5);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(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><td widht='300' style='cursor:pointer;cursor:hand;' onclick='javascript:expand_category("+specials_id+",7);'>&nbsp;</td>"+
				 "<td width='80' align='right' nowrap='nowrap' id='img_save' style='display:none'><input type='image' src='images/template/img_savel.gif' alt='save' title='save' border='0'>&nbsp;<a href='javascript:expand_category("+specials_id+",3);'><img src='images/template/img_closel.gif' alt='close' title='close' border='0'></a>"+
				"</tr>"+
				"<tr style='height:10'><td id='loading' style='display:none' colspan='6'><img src='images/24-1.gif' alt='Loading...' title='Loading...'></td></tr>"+				
				"<tr>"+
				  "<td colspan='8' class='main'><span style='display:none' id='span_edit'></span><span style='display:none' id='span_delete'></span><span id='span_details'>"+	"<table width='100%' border='0' cellspacing='0' cellpadding='0'>"+
				  		"<tr>"+
				  			"<td width='10' align='left'>&nbsp;&nbsp;<img src='../images/small/"+products_image_1+"' width='100' height='100'></td>"+				  							  			
							"<td width='80%'>"+				  							  			
				  				"<table  width='50%' border='0' cellspacing='5' cellpadding='0' class='main'>";				  			
				  					if(specials_date_added!=""){
				  						include+="<tr>"+
					  						"<td align='left' width='17%' valign='top'><?php echo TEXT_INFO_DATE_ADDED;?></td>"+
					  						"<td align='left' width='33%' valign='top'>"+specials_date_added+"</td>"+
				  						"</tr>";
				  					}	
				  					if(specials_last_modified!=""){		  					
					  					include+="<tr>"+
					  						"<td align='left' width='17%'><?php echo TEXT_INFO_LAST_MODIFIED ;?></td>"+
					  						"<td align='left' width='33%'>"+specials_last_modified+"</td>"+
					  					"</tr>";						
				  					}			
				  					if(products_price!=""){
									include+="<tr>"+
											"<td align='left' width='17%'><?php echo TEXT_INFO_ORIGINAL_PRICE;?></td>"+
											"<td align='left' width='33%'>"+products_price+"</td>"+
										"</tr>";
				  					}
				  					if(specials_new_products_price!=""){
									include+="<tr>"+
											"<td align='left' width='17%'><?php echo TEXT_INFO_NEW_PRICE;?></td>"+
											"<td align='left' width='33%'>"+specials_new_products_price+"</td>"+
										"</tr>";
				  					}
				  					if(expires_date!=""){
									include+="<tr>"+
											"<td align='left' width='17%'><?php echo TEXT_INFO_EXPIRES_DATE;?></td>"+
											"<td align='left' width='33%'>"+expires_date+"</td>"+
										"</tr>";
				  					}
				  					if(date_status_change!=""){
									include+="<tr>"+
											"<td align='left' width='17%'><?php echo TEXT_INFO_STATUS_CHANGE;?></td>"+
											"<td align='left' width='33%'>"+date_status_change+"</td>"+
										"</tr>";
				  					}
							include+="</table></td></tr>";
					include+="<tr><td colspan='8'><img src='images/pixel_trans.gif' border='0' alt='' width='100%' height='20'></td></tr></span>"+
				  "</td>"+
				"</tr>"+
			"</table>";
			head.innerHTML=include;
 	 		} 			 		
 	 	break;
 	 	case 'edit': 	 		 	 		 	 		 	 		
			if(span_edit) span_edit.style.display="";			
 	 		if(span_edit && span_details){ 	 	
 	 			span_edit.innerHTML=result;
 	 			span_details.style.display='none';
				}										
 	 	break; 	 	 	 
 	 	case 'success_delete':
 	    	 if(head && data_id) {
			 	var tbl_obj=head.parentNode.parentNode.parentNode.parentNode;			 				 				 	
			 	if(tbl_obj) tbl_obj.deleteRow(head.parentNode.parentNode.rowIndex);
			 	data_id.innerHTML=head.innerHTML="";
			 	data_id.style.display=head.style.display="none";			 		
			 }			 				
			 if(res_display){
	    	 	res_display.innerHTML="<?php echo TEXT_DELETE_SUCCESS;?>";
 	    	 	res_display.style.display="";
 	    	 	var obj=document.getElementById("page_1");
 	    	 	var len=1; 
 	    	 	if(obj) len=parseInt(obj.options[obj.selectedIndex].value);
 	    	 	command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=page_navigation');?>&page="+len; 	    	 	
 	    	 } 	    	  	    	 			 			 			
 	    	 if(command!="") do_get_command(command);
			 if(document.getElementById("search")) document.getElementById("search").value=""; 								   			   
 	 	break;	
 	 	case 'page_navigation':
 	 		if(document.getElementById("ajax_details"))
 	 		 document.getElementById("ajax_details").innerHTML=result;
 	 	break;
		case 'new_specials':			
				var obj=document.getElementById("page_1");
				var len=1;				
				if(data_id) {
					var tbl_obj=data_id.parentNode.parentNode.parentNode.parentNode;
					var tbl_obj_rows=tbl_obj.rows.length-1; 		
				}			
				if(obj && obj.options.length==obj.selectedIndex || !obj && tbl_obj_rows<max_display){ 
					result_splt=result.split('^');
					if(result_splt[0]>0){ 						
						if(data_id) {    			    						
    						var row_obj=tbl_obj.insertRow(tbl_obj_rows);
    						var row_cell=row_obj.insertCell(0);
    						row_cell.innerHTML='<div id="data_'+specials_id+'">'+
						       '<table border="0" cellpadding="3" cellspacing="0" width="100%">'+
						          '<tbody><tr>'+
						         	'<td class="dataTableContent" align="left" width="50%"><img src="images/template/'+((products_status==0)?'icon_inactive.gif':'icon_active.gif')+'" alt="Active" title=" Active " border="0" height="10" width="10">&nbsp;&nbsp;&nbsp;'+products_name+'</td>'+						         				         
						         	'<td class="dataTableContent" width="50%"><span class="oldPrice">'+products_price+'</span><span class="specialPrice">'+specials_new_products_price+' ' +spcl_string+'</span></td>'+			         						  			 
						         '</tr>'+
						      	'</tbody></table>'+
						      '</div>'+
						      '<div style="display: none;" class="openContent" id="head_'+specials_id+'"></div>';
						    var rows_cnt=tbl_obj.rows.length-2;
    						var tbl=tbl_obj.rows[rows_cnt];
    						tbl.onclick=function () { (document.getElementById("head_"+specials_id) && document.getElementById("head_"+specials_id).style.display!='none')?'':expand_category(specials_id,3);};
    						tbl.style.height="18px"; 
    						if(rows_cnt%2==0) tbl.className="dataTableRowOdd";
    						else tbl.className="dataTableRowEven";
    						tbl.onmouseover=function (){rowOverEffect(this)};
    						tbl.onmouseout=function (){rowOutEffect(this)};
    						if(head && data_id){
    							head.style.display="none";
    							data_id.style.display="";
    						}
    						if(res_display){
					    	 res_display.innerHTML="<?php echo TEXT_INSERTED_SUCCESSFULLY;?>";
				 	    	 res_display.style.display="";
 	    	 			  }
					  }
					}
				}else if(obj && obj.options.length>0 || tbl_obj_rows==max_display){
				 	if(!obj && tbl_obj_rows==max_display) len=2;
				}
				command="<?php echo tep_href_link(FILENAME_SPECIALS,'command=page_navigation');?>&page="+len;
				if(command!="") do_get_command(command);
				if(document.getElementById("search")) document.getElementById("search").value=""; 								   			   
		break;
 	 	} 
 	 	if(res_display.style.display=="") setTimeout("res_display.style.display='none'",2000); 	 	 	 	
 	 	clearTimeout();
	  }
	  
	  function change_class(obj,eve){	 		 	 	
		 var span_details=document.getElementById("span_details"); 
		 var span_edit=document.getElementById("span_edit");
		 var span_delete=document.getElementById("span_delete");	
		 if(obj){		   		    		    
		    var img_get_src=obj.childNodes[0];
			if(img_get_src){
			     if(img_get_src.src==img_src+'template/img_new.gif'){				   		
			   		if(eve==1 || eve==2) img_get_src.src=img_src+'template/img_new_hover.gif';					
			   }else if(eve==3 && img_get_src.src==img_src+'template/img_new_hover.gif') 
			    	img_get_src.src=img_src+'template/img_new.gif';
			    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';		    		    		     		   	
		  }
		 }		 
	  }	
	   var display_flag=1;	
	function do_hover_change(element,mode){ 
	if(display_flag==1){
		if (element.mode && mode==element.mode) return;
		if (mode==2)
			element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
		else
			element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
	}
	element.mode=mode;
	} 
	
	 function open_close(id){
	 var head=document.getElementById("head_"+id);	
	 var data_id=document.getElementById("data_"+id);
	 if(head && head.style.display=="" && data_id ) {
				 head.style.display="none";
					 data_id.style.display="";
					// document.getElementById('content').innerHTML=' ';
					head.innerHTML='';
					 }
	// data_id.style.display="";
	// head.style.display="none" ;

	 }
</script>	
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<div id="spiffycalendar" class="text"></div>
<!-- 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="0">
	 
            <?php 
              /*  $specials_row_query =tep_db_query("select p.products_id, pd.products_name, p.products_price, s.specials_id, s.specials_new_products_price from " . TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "'");
				$specials_count=tep_db_num_rows($specials_row_query);
				echo (($specials_count)?HEADING_TITLE_SEARCH . ' ' . tep_draw_input_field('search','','onkeyup="javascript:check_key(event)"'):'');*/
				$search = '';
   /* if (isset($HTTP_GET_VARS['search']) && tep_not_null($HTTP_GET_VARS['search'])) {
      $keywords = tep_db_input(tep_db_prepare_input($HTTP_GET_VARS['search']));
      $search = " and (pd.products_name like '%" . $keywords . "%' )";
    $specials_query_raw = "select p.products_id, pd.products_name, p.products_price, s.specials_id, s.specials_new_products_price from " . TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = s.products_id order by pd.products_name" . $search;
 	}*/
    //CGDiscountSpecials start
   
    $all_groups=array();
    $customers_groups_query = tep_db_query("select distinct customers_groups_name, customers_groups_id from " . TABLE_CUSTOMERS_GROUPS . " order by customers_groups_id ");
    while ($existing_groups =  tep_db_fetch_array($customers_groups_query)) {
      $all_groups[$existing_groups['customers_groups_id']]=$existing_groups['customers_groups_name'];
    }
	$all_customers=array();
	$customers_query = tep_db_query("select distinct customers_firstname, customers_lastname, customers_id from " . TABLE_CUSTOMERS . " order by customers_id");
    while ($existing_customers = tep_db_fetch_array($customers_query)) {
      $all_customers[$existing_customers['customers_id']]=$existing_customers['customers_lastname'] . " " . $existing_customers['customers_firstname'];
    }
    //CGDiscountSpecials end

    /*$specials_query_raw = "select p.products_id, pd.products_name, p.products_price, s.specials_id, s.specials_new_products_price, s.specials_date_added, s.specials_last_modified, s.expires_date, s.date_status_change, s.status,s.customers_groups_id,s.customers_id from " . TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = s.products_id order by pd.products_name";
    $specials_split = new splitPageResultsEvent($page, MAX_DISPLAY_SPECIAL_PRODUCTS, $specials_query_raw, $specials_query_numrows);
    $specials_query = tep_db_query($specials_query_raw);
	$total_rows = tep_db_num_rows($specials_query);	*/			 
			?>	     	
		<tr class="dataTableHeadingRow" style="height:24px">           
            <td class="pageHeading" align="left" valign="bottom"><a alt="New" title="New" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,1);" id="img_new" href="javascript:expand_category(0,2);"><?php echo tep_image('images/template/img_new.gif');?></a></td>
			<td class="dataTableHeadingContent" align="right"><?php echo HEADING_TITLE_SEARCH . ' ' . tep_draw_input_field('search','','onkeyup="javascript:check_key(event)"') .'&nbsp;';?>
			<td  align="left" valign="absmiddle" width="25"><a alt="Search" title="Search" href="javascript:expand_category(1,'1');"><?php echo tep_image('images/template/img_search.gif');?></a></td>	
         </tr>
         <tr><td colspan="3"><?php  echo tep_draw_separator('pixel_trans.gif',5,10); ?></td></tr>
          <tr>
            <td valign="top" colspan="3">
            <?php echo tep_draw_form('frm_specials', FILENAME_SPECIALS, '', 'post', 'onSubmit="javascript:return check_form();"'); ?>
	            <table border="0" width="100%" cellspacing="0" cellpadding="2">
	              <tr><td id="res_display" class="main" style="display:none"></td></tr>
	              <tr class="dataTableHeadingRow">
	                <td class="dataTableHeadingContent" width="50%"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
	                <td class="dataTableHeadingContent" width="50%" align="left"><?php echo TABLE_HEADING_PRODUCTS_PRICE; ?></td>            
	              </tr>				  
	         	  <tr>
	         		<td colspan="3" id="ajax_details">
		       	   		<?php table_write();?>
				  	</td>
				  </tr>
				  <tr><td colspan="3" style="display:none" class="openContent" id="new_create"></td></tr>
				</table>			
	   	<input type="hidden" id="specials_id" name="specials_id">
	   	<input type="hidden" id="action_type" name="action_type">
	   	<input type="hidden" id="products_id_assign" name="products_id_assign">
	  </form>	    		
	 </table>
	 </td>
	 </tr>
	</table>
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php 
function edit_function(){ 
   global $sId, $action,$HTTP_GET_VARS,$languages_id,$server_dates;       
	//CGDiscountSpecials start
    $specials_array = array();
    $specials_query = tep_db_query("select p.products_id, s.customers_groups_id from " .  TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s where s.products_id = p.products_id");
    while ($specials = tep_db_fetch_array($specials_query)) {
      $specials_array[] = (int)$specials['products_id'].":".(int)$specials['customers_groups_id'];
    }	
    $customers_groups_query = tep_db_query("select distinct customers_groups_name, customers_groups_id from " . TABLE_CUSTOMERS_GROUPS . " order by customers_groups_name");
    $input_groups=array();
    $all_groups=array();
    $sde=0;
    while ($existing_groups = tep_db_fetch_array($customers_groups_query)) {
      $input_groups[$sde++]=array("id"=>$existing_groups['customers_groups_id'],
	                      "text"=>$existing_groups['customers_groups_name']);
	  $all_groups[$existing_groups['customers_groups_id']]=$existing_groups['customers_groups_name'];
    }
	$customers_query = tep_db_query("select distinct customers_firstname, customers_lastname, customers_id from " . TABLE_CUSTOMERS . " order by customers_lastname, customers_firstname");
	$input_customers=array();$all_customers=array();$sde=0;
    while ($existing_customers = tep_db_fetch_array($customers_query)) {
      $input_customers[$sde++]=array("id"=>$existing_customers['customers_id'],
	                      "text"=>$existing_customers['customers_lastname'] . " " . $existing_customers['customers_firstname']);
	  $all_customers[$existing_customers['customers_id']]=$existing_customers['customers_lastname'] . " " . $existing_customers['customers_firstname'];
    }
    //CGDiscountSpecials end	
    $product=array('expires_date'=>$server_dates);	
    if ($HTTP_GET_VARS['sId']) {      
      $product_query = tep_db_query("select p.products_id, pd.products_name, p.products_price, s.specials_new_products_price, s.expires_date,s.customers_groups_id,s.customers_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_SPECIALS . " s where p.products_id = pd.products_id  and pd.language_id='$languages_id' and p.products_id = s.products_id and s.specials_id = '" . $sId . "'");
      $product = tep_db_fetch_array($product_query);      
    }
    $sInfo = new objectInfo($product);        
	// create an array of products on special, which will be excluded from the pull down menu of products
	// (when creating a new product on special)
    $specials_array = array();
    $specials_query = tep_db_query("select p.products_id from " . TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s where s.products_id = p.products_id");
     while ($specials = tep_db_fetch_array($specials_query)) {
        $specials_array[] = $specials['products_id'];
      }     
?>
	<table border="0" width="100%" cellspacing="0" class="openContent" cellpadding="2" id="show_options">
	<?php if(!$HTTP_GET_VARS['sId']){ ?>
			<tr class="openContent_top" style="cursor:pointer; cursor:hand;"><td onClick="javascript:expand_category(0,1);"></td><td width='7%' align='right'><input type='image' src='images/template/img_savel.gif' alt='save' title='save' border='0'>&nbsp;&nbsp;<a href='javascript:expand_category(0,1);'><img src='images/template/img_closel.gif' alt='close' title='close' border='0'></a>&nbsp;&nbsp;</td></tr>
	<?php } ?>		 
      	<tr>
         <td><br><table width="60%" border="0" cellspacing="0" cellpadding="2" id="show_options">
          <tr>
            <td class="main" width="25%"><?php echo TEXT_SPECIALS_PRODUCT; ?>&nbsp;</td>
            <td class="main" width="35%"><?php echo (isset($sInfo->products_name)) ? $sInfo->products_name . ' <small>($' . substr($sInfo->products_price,0,-2) . ')</small>' : tep_draw_products_pull_down('products_id', 'style="font-size:10px"', $specials_array); echo tep_draw_hidden_field('products_price', (isset($sInfo->products_price) ? $sInfo->products_price : '')); ?></td>
          </tr>
		  <!--CGDiscountSpecials start-->
		  <tr>
            <td class="main" width="25%"><?php echo TEXT_SPECIALS_CUSTOMERS; ?>&nbsp;</td>
            <td class="main" width="25%">
				<?php
			        if ($sInfo->customers_id != 0) {
			           echo tep_draw_pull_down_menu('customers', $input_customers, (isset($sInfo->customers_id)?$sInfo->customers_id:''),'');
					   ?><input type="checkbox" name="checkbox_customers" checked value="false" onClick="if (customers.disabled && checkbox_customers.checked) {customers.disabled = false; customers_groups.disabled = true; checkbox_customers_groups.checked = false} else {customers.disabled = true; customers_groups.disabled = true;}"><?php
		            } else {
					   echo tep_draw_pull_down_menu('customers', $input_customers, (isset($sInfo->customers_id)?$sInfo->customers_id:''),'disabled');
					   ?><input type="checkbox" name="checkbox_customers" value="false" onClick="if (customers.disabled && checkbox_customers.checked) {customers.disabled = false; customers_groups.disabled = true; checkbox_customers_groups.checked = false} else {customers.disabled = true; customers_groups.disabled = true;}"><?php
					}
		        ?>
		    </td>
          </tr>
		  <tr>
            <td class="main" width="25%"><?php echo TEXT_SPECIALS_GROUPS; ?>&nbsp;</td>
            <td class="main" width="30%">
				<?php
			       if ($sInfo->customers_groups_id != 0) {
			          echo tep_draw_pull_down_menu('customers_groups', $input_groups, (isset($sInfo->customers_groups_id)?$sInfo->customers_groups_id:''),'');
		              ?><input type="checkbox" name="checkbox_customers_groups" checked value="false" onClick="if (customers_groups.disabled && checkbox_customers_groups.checked) {checkbox_customers_groups.value = true; customers_groups.disabled = false; customers.disabled = true; checkbox_customers.checked = false} else {checkbox_customers_groups.value = false; customers_groups.disabled = true; customers.disabled = true; }"><?php
				   } else {
                      echo tep_draw_pull_down_menu('customers_groups', $input_groups, (isset($sInfo->customers_groups_id)?$sInfo->customers_groups_id:''),'disabled');
				      ?><input type="checkbox" name="checkbox_customers_groups" value="false" onClick="if (customers_groups.disabled && checkbox_customers_groups.checked) {checkbox_customers_groups.value = true; customers_groups.disabled = false; customers.disabled = true; checkbox_customers.checked = false} else {checkbox_customers_groups.value = false; customers_groups.disabled = true; customers.disabled = true; }"><?php
				   }
				?>
		   </td>
          </tr>
          <!--CGDiscountSpecials end-->
          <tr>
            <td class="main" width="25%"><?php echo TEXT_SPECIALS_SPECIAL_PRICE; ?>&nbsp;</td>
            <td class="main" width="30%"><?php echo tep_draw_input_field('specials_price', (isset($sInfo->specials_new_products_price) ? $sInfo->specials_new_products_price : '')); ?></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" width="100" align="left"><?php echo TEXT_SPECIALS_EXPIRES_DATE; ?>&nbsp;</td>
            <td class="main" width="200"><?php echo tep_draw_input_field("txt_date_begin",format_date($sInfo->expires_date),"size=20 width=50",false,'text',false);?><a href="javascript:show_calendar('frm_specials.txt_date_begin',null,null,'<?php echo $date_format;?>');"
							onmouseover="window.status='Date Picker';return true;"
							onmouseout="window.status='';return true;"><?php echo tep_draw_separator('pixel_trans.gif','5','0'); ?><img border="none" align="absmiddle" src="images/icon_calendar.gif"/>  
							</a></td>
		</tr></table></td>	
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="2" id="option_tips">
          <tr>
            <td class="main"><br><?php echo TEXT_SPECIALS_PRICE_TIP; ?></td>            
          </tr>
        </table></td>
	   </tr>	 
	  </tr></td>
	 </tr>
	 <tr><td colspan='3'><img src='images/pixel_trans.gif' border='0' alt='' width='100%' height='20'></td></tr>
	</table>	  
<?php  
  }
function echo_get_details(){
	global $languages_id,$sId,$currencies;	
		$details_query = tep_db_query("select s.specials_id,pd.products_name,p.products_image_1,date_format(s.specials_date_added,'%Y-%m-%d') as specials_date_added,date_format(s.specials_last_modified,'%Y-%m-%d') as specials_last_modified,p.products_price,s.specials_new_products_price,date_format(s.expires_date,'%Y-%m-%d') as expires_date, date_format(s.date_status_change,'%Y-%m-%d') as date_status_change,s.status,p.products_id from " . TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where s.specials_id= '".$sId."' and pd.language_id='".(int)$languages_id."' and p.products_id = pd.products_id and p.products_id = s.products_id order by pd.products_name");
		if(tep_db_num_rows($details_query)>0){
		 	$special = tep_db_fetch_array($details_query);
			while(list($key,$vals)=each($special)){									  			
			  if($key=='specials_date_added' || $key=='specials_last_modified' || $key=='expires_date' || $key=='date_status_change')
				  $vals=format_date($vals);			  			  
			  if($key=='products_price' || $key=='specials_new_products_price')			 		
			  	$vals = $currencies->format($vals);
			  	$str_vals.=$vals."^";			  	  			  			  			  	
		   }
		   $str_vals.=get_products_name($sId);		   	
		 }else $str_vals = 'No Record Found';
		  echo 'get_details'.$str_vals; 
}

function table_write(){	
	global $HTTP_GET_VARS,$page,$languages_id,$currencies;	
	$search="";
	if(($HTTP_GET_VARS['search']) && (($HTTP_GET_VARS['search'])!=""))
    $search = " and (pd.products_name like '%" . (tep_db_input(tep_db_prepare_input($HTTP_GET_VARS['search']))). "%' )";
	//$specials_query_raw = "select p.products_id, pd.products_name, p.products_price, s.specials_id, s.specials_new_products_price,s.customers_groups_id,s.customers_id from " . TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = s.products_id " . $search . "order by pd.products_name";				 		
    $specials_query_raw = "select p.products_id, pd.products_name, p.products_price, s.specials_id, s.specials_new_products_price, s.specials_date_added, s.specials_last_modified, s.expires_date, s.date_status_change, s.status,s.customers_groups_id,s.customers_id from " . TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = s.products_id ".$search." order by s.specials_id desc";		       
  	$all_groups=array();
    $customers_groups_query = tep_db_query("select distinct customers_groups_name, customers_groups_id from " . TABLE_CUSTOMERS_GROUPS . " order by customers_groups_id ");
    while ($existing_groups =  tep_db_fetch_array($customers_groups_query)) {
      $all_groups[$existing_groups['customers_groups_id']]=$existing_groups['customers_groups_name'];
    }
	$all_customers=array();
	$customers_query = tep_db_query("select distinct customers_firstname, customers_lastname, customers_id from " . TABLE_CUSTOMERS . " order by customers_id");
    while ($existing_customers = tep_db_fetch_array($customers_query)) {
      $all_customers[$existing_customers['customers_id']]=$existing_customers['customers_lastname'] . " " . $existing_customers['customers_firstname'];
    }
    //CGDiscountSpecials end 
    $specials_split = new splitPageResultsEvent($page, MAX_DISPLAY_SPECIAL_PRODUCTS, $specials_query_raw, $specials_query_numrows);    
    $specials_query = tep_db_query($specials_query_raw);
	$total_rows = tep_db_num_rows($specials_query);	?>
		<table border="0" cellpadding="0" cellspacing="0" width="100%">
		<?php if($total_rows>0){ 
		 	 $i=0;
		 	while ($specials = tep_db_fetch_array($specials_query)) {
			     $class="dataTableRowEven";
			     if($i%2==0) $class="dataTableRowOdd";
			     $i++;
			     echo '  <tr  class="'.$class.'" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)"  onclick="javascript:if(document.getElementById(\'head_'.$specials['specials_id'].'\').style.display==\'none\') expand_category(' . $specials['specials_id'] . ',3)">' . "\n";
			?>
		<td>
	   	 <div id="data_<?php echo $specials['specials_id'];?>">
			<table border="0" cellpadding="3" cellspacing="0" width="100%">
			   <tr>			   	
				  <td  width="50%" class="dataTableContent" valign="top"><?php 
				  if ($specials['status'] == '1')
						echo tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', IMAGE_ICON_STATUS_GREEN, 10, 10);
					else
						echo tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', IMAGE_ICON_STATUS_RED, 10, 10);
					echo tep_draw_separator('pixel_trans.gif','10','10');
				    echo $specials['products_name'];
				    $special_cg=get_products_name($specials['specials_id']);				    				    				    
				    ?></td>
					<td  class="dataTableContent" align="left" width="50%" valign="top">
					  <span class="oldPrice"><?php echo $currencies->format($specials['products_price']); ?></span> 
					  <span class="specialPrice"><?php echo $currencies->format($specials['specials_new_products_price']).' '.$special_cg;?></span>
				    </td> 
			   </tr>
		   </table>
     	</div>
	 	<div style="display:none" id="head_<?php  echo $specials['specials_id'];?>"  class="openContent"></div>
      </td>
    </tr>
 <?php	} 
 //end of while 
 ?>
         <tr>
           <td colspan="4">
           	  <table border="0" width="100%" cellpadding="0"cellspacing="2">
				 <tr>
                    <td class="smallText" valign="top"><?php echo (($total_rows)?$specials_split->display_count($specials_query_numrows, MAX_DISPLAY_SPECIAL_PRODUCTS, $page, TEXT_DISPLAY_NUMBER_OF_SPECIALS):''); ?></td>
                    <td class="smallText" align="right"><?php echo (($total_rows)?$specials_split->display_script_links($specials_query_numrows, MAX_DISPLAY_SPECIAL_PRODUCTS, MAX_DISPLAY_PAGE_LINKS, $page,1,'expand_category(\'\'','1'):''); ?></td>
                  </tr>
			  </table>
			</td>
		 </tr>
		<?php    	  
	}else echo '<tr><td align="center" id="no_record" class="main">'.TEXT_NO_RECORDS.'</td></tr>';
	if ($HTTP_GET_VARS['search']!="") { 
	 ?>
  		<tr>
  		   <td align="right"><?php echo (($HTTP_GET_VARS['search']!="")?'<a href="javascript:expand_category(1,\'1\',\'reset\');">' . tep_image_button('button_reset.gif', IMAGE_RESET) . '</a>':'');?></td>
  		</tr> 			
<?php }
	echo '</table>';
}

function get_products_name($id)
{   
    global $currencies,$languages_id;
    $all_groups=array();
    $customers_groups_query = tep_db_query("select distinct customers_groups_name, customers_groups_id from " . TABLE_CUSTOMERS_GROUPS . " order by customers_groups_id ");
    while ($existing_groups =  tep_db_fetch_array($customers_groups_query)) {
      $all_groups[$existing_groups['customers_groups_id']]=$existing_groups['customers_groups_name'];
    }
	$all_customers=array();
	$customers_query = tep_db_query("select distinct customers_firstname, customers_lastname, customers_id from " . TABLE_CUSTOMERS . " order by customers_id");
    while ($existing_customers = tep_db_fetch_array($customers_query)) {
      $all_customers[$existing_customers['customers_id']]=$existing_customers['customers_lastname'] . " " . $existing_customers['customers_firstname'];
    }
    //CGDiscountSpecials end
    $specials_query= tep_db_query("select p.products_id, pd.products_name, p.products_price, s.specials_id, s.specials_new_products_price, s.specials_date_added, s.specials_last_modified, s.expires_date, s.date_status_change, s.status,s.customers_groups_id,s.customers_id from " . TABLE_PRODUCTS . " p, " . TABLE_SPECIALS . " s, " . TABLE_PRODUCTS_DESCRIPTION . " pd where pd.language_id='$languages_id' and p.products_id = pd.products_id and s.specials_id='" . $id . "' and p.products_id = s.products_id order by pd.products_name");     
	$specials = tep_db_fetch_array($specials_query);	
		if ($specials['customers_groups_id'] != 0) {
		   $group = " ( [G] -> " . $all_groups[$specials['customers_groups_id']];
		 } else if ($specials['customers_id'] != 0) {
		   $customer = " ( [C] -> " . $all_customers[$specials['customers_id']];
		 } else {
		   $para = " ( ";
		 }
		 $para1= " )";
		return $group . $customer . $para . $para1;
}
?>

