<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

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

Copyright 2007 ZacWare Pty. Ltd 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

  require('includes/application_top.php');
  require('includes/classes/split_page_results_event.php');
  tep_get_last_access_file();
  
  $command=(isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'');
  $rid=(isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:'');
  $page=(isset($HTTP_GET_VARS['page'])?(int)$HTTP_GET_VARS['page']:'');
  	
  if($command!=''){
   echo $command . '{}' . $id . '||';
  	switch($command){
		case 'get_details':
			echo do_get_details($rid);
		break;
		case 'new':
		case 'edit':
			echo get_edit_details($id);
		break;
		case 'insert':
		case 'update':
			if($command=='insert')
				$id=0;
			echo save_details($id);
		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_survey_details();
		break;
	}
  	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>
    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_close(id) {
		var head=document.getElementById('head_' + id);
		var data=document.getElementById('data_' + id);
		if(data.style.display=='' && data.innerHTML!='' && close_obj==false)  {
			data.style.display="none";
			data.innerHTML='';
			head.style.display='';
			row_expand=false;
			ajx_start=false;
			prev_id='';
		}
		close_obj=false;
		if(form_obj==true){
			 ajx_start=false;
			 return;
		 }
	}
	function do_page_fetch(id,action){
		if(ajx_start) return;
		else {
			ajx_start=true;
		} 
		if(action!='new' && action!='insert'){
			document.getElementById('new_details').style.display="none";
			document.getElementById('span_new').style.display="none";
			document.getElementById('span_new').innerHTML="";
		}	
		document.getElementById('ref_id').value=id;
		command='<?php echo tep_href_link(FILENAME_REFERRALS);?>' + '?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=="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;
				}
			break;
			//case 'new':
			case 'edit':
				close_obj=true;
				document.getElementById('ref_action').value='update';
				document.getElementById('span_details').style.display="none";
				document.getElementById('span_edit').style.display="";
				document.getElementById('span_delete').style.display="none";
				document.getElementById('span_edit').innerHTML=document.getElementById('loading').innerHTML;
				
			break;
			case 'insert':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_delete')) document.getElementById('span_delete').style.display="none";
				do_post_command('referrals',command);
				return;
			break;
			case 'update':close_obj=true;
				document.getElementById('span_edit').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('referrals',command);
				return;
			break;
			case 'new':
				document.getElementById('ref_action').value='insert';
				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_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 'delete':close_obj=true;
				document.getElementById('ref_action').value='delete';
				document.getElementById('span_delete').style.display="";
				document.getElementById('span_edit').style.display="none";
				document.getElementById('span_delete').innerHTML=document.getElementById("loading").innerHTML;
				document.getElementById('span_details').style.display="none";
			break;
			case 'delete_confirm':
				document.getElementById('span_delete').innerHTML=document.getElementById("loading").innerHTML;
				document.getElementById('span_details').style.display="none";
				prev_id='';
			break;
			case 'close_referrals': 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 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"; 	 			   	  	 	 			   	  	
				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_trash_hover.gif')	
						tr_obj.childNodes[2].childNodes[0].src=img_src+'template/img_trash.gif';	 	 			   	  			
					 }	 			   	  	  	 			   	  	 	 			   	  	
				
				ajx_start=false;
			return;
			break;
			case 'close': 
					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){ 
	//alert (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];
				document.getElementById('span_delete').style.display="none";
			break;
			case 'update':
				document.getElementById('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_trash_hover.gif')	
						tr_obj.childNodes[2].childNodes[0].src=img_src+'template/img_trash.gif';	 	 			   	  			
				}	 			   	  	  	 			   	  	 	 			   	  	
				var details=Array();
				details=spl_arr[1].split("{#}"); 
				document.getElementById('head_' + spl_arr[0]).innerHTML=details[1];
				document.getElementById('span_details').innerHTML=details[0];
				document.getElementById('row_heading').innerHTML=details[1];
			break;
			case 'insert':
				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-1);
				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'):'')};
				if((row_len % 2) == 0) classname="dataTableRowEven";
				else classname="dataTableRowOdd";
				row_obj.className=classname;
				row_obj.height="25px";
				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_REFERRALS);?>' + '?command=page_navigation&page=1';
				do_get_command(command);
			break;
			case 'new':
				document.getElementById('span_new').innerHTML=spl_arr[1];
			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_REFERRALS);?>' + '?command=page_navigation&page=' + page_id;
				do_get_command(command);
			break;
			case 'page_navigation':
				document.getElementById('ajax_details').innerHTML=spl_arr[1];
			break;
		}
		ajx_start=false;
	}
	
	 
	 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");
	  	 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';
		  }
		 }		 
	 } 
	 
	 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");
		 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('ref_id').value;
		var action=document.getElementById('ref_action').value; 
		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');
				}	
			}
		}
			return false;		
	}
	
	function check_form(){
	obj=document.referrals.sources_name.value;
		if(obj=='' || str_trim(obj)==''){
			alert('<?php echo ERR_SOURCES_NAME;?>');
			form_obj=true;
			close_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_REFERRALS);?>' + '?command=page_navigation&page=' + page_id;
	do_get_command(command);
}

</script>
</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="0">
				<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('referrals',FILENAME_REFERRALS,'','post','onSubmit="return ValidateForm();"');?>
												<input type="hidden" id="ref_id"><input type="hidden" id="ref_action">
												<table cellpadding="0" cellspacing="0" width="100%" border="0">
													<tr class="dataTableHeadingContent"><td>
														<?php echo  '<a  class="dataTableHeadingContent" href="javascript:do_page_fetch(\'\',\'new\');" 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','','22','22') . '</a>';?>
													</td></tr>	
													<tr><td><?php echo tep_draw_separator('pixel_trans.gif','10','10');?></td></tr>
													<tr class="dataTableHeadingRow">
														<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_REFERRALS; ?></td>
													</tr>	
													<div id="loading" style="display:none"><div style="height:30px;margin-left:30px"><img src="images/24-1.gif"></div></div>
													<tr>
														<td id="ajax_details">
															<?php get_survey_details(); ?>
														</td>
													</tr>
												</table></form>
											</td>
										</tr>		
									</table>
								</td>
										<!-- body_text_eof //-->
							</tr>
						</table>
					</td>
				</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_survey_details(){
		global $page,$sources_split,$sources_query_numrows;
	  $sources_query_raw = "select sources_id, sources_name from " . TABLE_SOURCES . " order by sources_id desc";
	  $sources_split = new splitPageResultsEvent($page, MAX_DISPLAY_SEARCH_RESULTS, $sources_query_raw, $sources_query_numrows);
	  $sources_query = tep_db_query($sources_query_raw); $row=0;
	  echo '<table cellpadding="0" cellspacing="0" border="0" width="100%"  id="ref_table">';
	  if(tep_db_num_rows($sources_query)>0){
		  while ($sources = tep_db_fetch_array($sources_query)) {
				if($row %2 ==0) $class="dataTableRowEven";
				else $class="dataTableRowOdd"; $row++;
		  
			  echo '              <tr  height="25" id="row_"' . $sources['sources_id'].'" class="' . $class .'" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="javascript:if(document.getElementById(\'data_' . $sources['sources_id']. '\').style.display==\'none\')do_page_fetch(\''. $sources['sources_id'] . '\',\'get_details\');">' . "\n";
		?>
						<td class="dataTableContent" colspan="2"><div id="head_<?php echo $sources['sources_id'];?>"><?php echo $sources['sources_name']; ?></div><div id="data_<?php echo $sources['sources_id'];?>" style="display:none"></div></td>
					  </tr>
	<?php
		  }
	  } else echo '<tr><td class="main" align="center">' . TEXT_NO_REFERRALS . '</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_HEADING_NEW_REFERRAL;?></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="4">
						<table border="0" width="100%" cellspacing="0" cellpadding="2">
						  	<tr>
								<td class="smallText" valign="top"><?php echo $sources_split->display_count($sources_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $page, TEXT_DISPLAY_NUMBER_OF_SOURCES); ?></td>
								<td class="smallText" align="right"><?php echo $sources_split->display_script_links($sources_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $page, 'page_navigation','do_page_navigation(\'\'' ,'\'\''); ?></td>
                  			</tr>	
						</table>
					</td>
				</tr>			
	<?php	echo '</table>';
	}
	
	function do_get_details($id){
		  $sources_query_raw = tep_db_query("select sources_id, sources_name from " . TABLE_SOURCES . " where sources_id='" . (int)$id . "'order by sources_name");
		  $sources_result=tep_db_fetch_array($sources_query_raw);
		  
		$result='<table width="100%" border="0" cellspacing="0" cellpadding="0" class="openContent">'.
				'<tr heigth="40" bgcolor="#EDF1FE" class="openContent_top" onClick="javascript:do_close(' . $id . ');">';  	
		$result.=		  '<td width="40" valign="top" align="center">&nbsp;</td>'.
				  '<td align="left" valign="middle" width="350" class="inner_title" id="row_heading">'. $sources_result['sources_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 . ',\'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;cursor:hand;cursor:pointer"><img src="images/template/img_savel.gif" alt="save" title="save" border="0" onClick="javascript:ValidateForm();">&nbsp;&nbsp;<img src="images/template/img_closel.gif" alt="close" title="close" border="0" onclick="javascript:do_page_fetch(' . $id .',\'close_referrals\');" style="cursor:pointer;cursor:hand">&nbsp;&nbsp;</td>'.
				'</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="2">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr>';
					$result.='<tr><td class="main" width="125">' . TEXT_REFERRALS_NAME . '</td><td class="main">' . $sources_result['sources_name'] . '</td><tr>';
							
					$result.='<tr><td colspan="2">' . 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">2</span></td></tr>';
					$result.='<tr><td colspan="5"><span id="span_delete" style="display:none"  class="openContent">3</span></td></tr>';
			$result .='</table>';
			
			return $result;
		  
	}
	
	function get_edit_details($id){
		global $details;
		  $sources_query_raw = tep_db_query("select sources_id, sources_name from " . TABLE_SOURCES . " where sources_id='" . (int)$id . "'order by sources_name");
		  $sources_result=tep_db_fetch_array($sources_query_raw);
		  $result.='<table cellpadding="2" cellspacing="2" border="0" width="100%" align="center">'.
		  			'<tr><td width="40" valign="top" align="center">&nbsp;</td><td class="main" width="175">' .TEXT_REFERRALS_NAME .'</td><td class="main">' . tep_draw_input_field('sources_name',$sources_result['sources_name']) . '</td></tr>'.
					'<tr><td colspan="3">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr>'.
					'</table>'; ?>
			<?	echo  $result;	
	}
	
	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);

        if ($id<=0) {
          tep_db_perform(TABLE_SOURCES, $sql_data_array);
          $id = tep_db_insert_id();
		  $new=true;
		  echo $id . '{|}';
        } elseif ($id>0) {
          tep_db_perform(TABLE_SOURCES, $sql_data_array, 'update', "sources_id = '" . $id . "'");
		  $new=false;
        }
		 echo get_updated_details($id,$new);
		
	}
	function get_updated_details($id,$new){
		  $sources_query_raw = tep_db_query("select sources_id, sources_name from " . TABLE_SOURCES . " where sources_id='" . (int)$id . "'order by sources_name");
		  $sources_result=tep_db_fetch_array($sources_query_raw);
		  
		if($new){
				$result='<div id="head_'. $sources_result['sources_id'] .'">'. $sources_result['sources_name'].'</div><div id="data_'. $sources_result['sources_id'].'" style="display:none"></div>';
		} else {
			$result='<table cellpadding="2" cellspacing="2" border="0" width="100%">';
					$result.='<tr><td colspan="2">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr>';
					$result.='<tr><td class="main" width="125">' . TEXT_REFERRALS_NAME . '</td><td class="main">' . $sources_result['sources_name'] . '</td><tr>';
					$result.='<tr><td colspan="2">' . tep_draw_separator('pixel_trans.gif','10','10') . '</td></tr></table>';
		}
		return $result . '{#}' . $sources_result['sources_name'] ;
	}
	
	function delete_details($id,$delete){
		if($delete){
       		 tep_db_query("delete from " . TABLE_SOURCES . " where sources_id = '" .(int)$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_HEADING_DELETE_REFERRAL . '</td></tr>'.
					'<tr><td width="15"></td><td class="main">' . TEXT_DELETE_INTRO . '</td></tr>'.
					'<tr><td colspan="2">'. tep_draw_separator('pixel_trans.gif','10','10') . '</td<tr>'.
					'</table>' ;
					return $result;
		}
	}
?>
