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

Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
	$default_template=DEFAULT_TEMPLATE;
	$command = (isset($HTTP_GET_VARS['command']) ? $HTTP_GET_VARS['command'] : '');
	$server_date = getServerDate(true);
	$message=strtolower($message);
	if(strpos($message,'http:')!==false || strpos($message,'https:')!==false || strpos($message,'www')!==false){
		$msg_error .= $lang['Error_submission'];
	}
tep_get_last_access_file();
$server_date = getServerDate(true);
$first_template_name="";
if ($command!=''){
	$cID=isset($HTTP_GET_VARS["cID"])?$HTTP_GET_VARS["cID"]:'';
   echo 'command||'. $command ;
   if($cID!='')
	  echo '{}template_id||' . $cID;
   $commands[]=$command;
   for ($icnt=0;$icnt<count($commands);$icnt++){
	 switch($commands[$icnt]){
	 case 'ajx_sql_file_exists':
	 	$template_name=$HTTP_GET_VARS['template_name'];
		if($template_name=="")
			$template_name=$first_template_name;
		$template_sql_file = DIR_FS_TEMPLATES . "/" . $template_name . "/" . $template_name . ".sql";
        if(file_exists($template_sql_file)){
			echo '{}sql_exists||1';
        }
		else
		{
			echo '{}template_array||';
		    $template_query = tep_db_query("select * from " . TABLE_TEMPLATE);
		    while($template = tep_db_fetch_array($template_query))
			{
				if($default_template!=$template["template_name"])
				    echo $template["template_id"] . '|' . $template["template_name"] . '^';
				else
				    echo $template["template_id"] . '|' . $template["template_name"] . ' (default)^';
			}
		}
	 	break;
	 case 'ajx_fixweight': 
		 if (($HTTP_GET_VARS['flag'] == "left") || ($HTTP_GET_VARS['flag'] == "right")) 
	 		tep_template_box_realign($cID,$HTTP_GET_VARS["flag"]);
    	 break;
     case 'ajx_setflag': //set the status of a template active.
	 	change_template_status($cID,$HTTP_GET_VARS['flag']);
        break;
     case 'ajx_set_template_display': //set the status of a template active buttons.
	 	change_template_button_status($cID,$HTTP_GET_VARS['iID'],$HTTP_GET_VARS['case'],$HTTP_GET_VARS['flag']);
		$commands[]='ajx_edit';
        break;
    case 'ajx_move_up_template':
	 case 'ajx_move_down_template':	 //set the status of a template active buttons.
	 	move_pos_updown($commands[$icnt],$cID,$HTTP_GET_VARS['loc'],$HTTP_GET_VARS['col']);
		$commands[]='ajx_edit';
        break;
    case 'ajx_move_right_left': //set the status of a news item.
		move_right_left($cID,$HTTP_GET_VARS['iID'],$HTTP_GET_VARS['flag']);
		$commands[]='ajx_edit';
        break;
     case 'ajx_save':
		 update_template($cID);
		 if ($HTTP_POST_VARS['default'] == 'on') 
		 	echo '{}change_default||' . $cID;
		 $commands[]="ajx_get_details";
	     break;
     case 'ajx_insert':
	  	insert_template();
        break;
	case 'ajx_delete':
		$delete_menu=true;
		echo '{}result_content||';
		echo out_result_template_delete_confirm($cID);
        break;
     case 'ajx_deleteconfirm':
	 	echo '{}remove_row||';
		delete_template($cID,$HTTP_GET_VARS['delete_image']);
		$cID='';
		break;
	case 'ajx_get_details':
		$edit_menu=false;
		echo '{}result_content||';
		echo out_result_template_details($cID);
        break;
	case 'ajx_edit':
		$edit_menu=true;
		echo '{}result_content||';
		echo out_result_template_edit($cID);
        break;
	case 'ajx_new':
		echo '{}result_new||';
		echo out_result_template_new();
		$commands[]="ajx_sql_file_exists";
        break;
	}
   }
   	if($cID!='')
		echo '{}result_title||' . out_result_template_title($edit_menu,$delete_menu) . "{}position||" . $position;;
	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/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="javascript" src="includes/aim.js"></script>
<script language="javascript" type="text/javascript">
	var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";

	function validate_image(){
		var image_types=Array(".png",".jpg",".jpeg",".gif");
		var frm = document.new_template;
		var error = "";
		if (frm.elements["template_image"].value!="" && !check_mime_type(frm.elements["template_image"],image_types)){
			error="* <?php echo ERR_IMAGE_UPLOAD_TYPE;?>\n";
		}
		if(error!=""){
			alert(error);
			return false;
		}
		return true;
	}
	function do_page_ajax(command){
			
		if (is_fetching) return;
		is_fetching=true;
		do_get_command(command);
	}

	function do_hover_change2(element,eve){
		 var edit_display=document.getElementById("span_edit");
		 var delete_display=document.getElementById("span_delete");
		
		 if(element){		   		    		    
		    var img_get_src=element.childNodes[0];
			//alert(img_get_src);
			if(img_get_src){
			
			    if(img_get_src.src==img_src+'template/img_edit.gif') {
					if(edit_display && edit_display.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_edit_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_edit_hover.gif' && edit_display && edit_display.style.display=="none") img_get_src.src=img_src+'template/img_edit.gif';
			    if(img_get_src.src==img_src+'template/img_trash.gif') {
					if(delete_display && delete_display.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_trash_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_trash_hover.gif' && delete_display && delete_display.style.display=="none") img_get_src.src=img_src+'template/img_trash.gif';
		  }
		}		 
	} 
	var display_flag=true;
	function do_hover_change(element,mode){
		if(display_flag==true){
			if (element.mode && mode==element.mode) return;
			if (mode==2)
				element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
			else
				element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
		}
		element.mode=mode;
	} 	
</script>

	<script language="javascript">
		var prev_template_id='';
		var is_fetching=false;
		var row_title_html='<table border="0" cellpadding="0" cellspacing="0" height="25" style="padding-top:4px;"><tr><td width="10"></td><td class="main" width="20" valign="top">#STATUS_IMAGE#<td class="dataTableContent" valign="top">#TITLE_TEXT#</td></tr></table>';
		var cur_row='';
		function do_new_close()
		{
			document.getElementById("panel_new").style.display="none";
			//document.getElementById("panel_new_action").style.display="none";
		    document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
			display_flag=true;

		}
		function toggle_template_panel(row,template_id){
			if (is_fetching) return;
			document.getElementById("panel_new").style.display="none";
			//document.getElementById("panel_new_action").style.display="none";
			var panel=document.getElementById("panel_"+template_id+"_content");
			var title=document.getElementById("panel_"+template_id+"_title");
			var row=document.getElementById("panel_"+template_id+"_row");
			if (panel.style.display=="none"){
				if (prev_template_id!=''){
					hide_template_id_panel(prev_template_id);
				}
				show_template_id_panel(template_id);
				prev_template_id=template_id;
				row.onclick="";
				do_page_fetch("ajx_get_details",template_id);
				row.opened=1;
			} else {
				row.position='';
				hide_template_id_panel(template_id);
				prev_template_id='';
				row.opened=0;
			}
		}
		function show_template_id_panel(template_id){
			var panel=document.getElementById("panel_"+template_id+"_content");
			var title=document.getElementById("panel_"+template_id+"_title");

			panel.innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
			panel.style.display="";
			title.className="openContent_top";
		}
		function hide_template_id_panel(template_id){
			var panel=document.getElementById("panel_"+template_id+"_content");
			var title=document.getElementById("panel_"+template_id+"_title");
			var row=document.getElementById("panel_"+template_id+"_row");

			panel.style.display="none";
			if (row.text_title){
				temp_str=row_title_html;
				rexp=/#TITLE_TEXT#/g;
				if(document.getElementById("default_template"))
				default_template_id=document.getElementById("default_template").innerHTML;
				if(typeof(default_template_id)!="undefined" && default_template_id!=template_id)
					temp_str=temp_str.replace(rexp,row.text_title);
				else
					temp_str=temp_str.replace(rexp,"<b>" + row.text_title + " (default)</b>");
				rexp=/#STATUS_IMAGE#/g;

				if (row.position!="inactive")
					temp_str=temp_str.replace(rexp,"<img src='<?php echo DIR_WS_IMAGES;?>template/icon_active.gif' border='0' title='<?php echo IMAGE_INSTALLED;?>' align='absmiddle'>");
				else
					temp_str=temp_str.replace(rexp,"<img src='<?php echo DIR_WS_IMAGES;?>template/icon_inactive.gif' border='0' title='<?php echo IMAGE_REMOVED;?>' align='absmiddle'>");
				
				title.innerHTML=temp_str;
			}
			
			if (row.rowIndex%2==0){
				row.className="dataTableRowOdd";
			} else {
				row.className="dataTableRowEven";
			}
			row.onclick=function () {toggle_template_panel(row,template_id)};
			title.className="";
		}
		function do_page_fetch(cmd,template_id,pos,col,iid){
			command="<?php echo tep_href_link(FILENAME_TEMPLATE_CONFIGURATION,'');?>?command="+cmd;
			switch(cmd){
				case "ajx_fixweight":
				case "ajx_setflag":
					command=command+"&cID="+template_id + "&flag=" + pos;
					break;
				case "ajx_sql_file_exists":
					command=command+"&template_name=" + template_id.value;
					break;
				case "ajx_edit":
					if(document.getElementById('span_edit')) document.getElementById('span_edit').style.display='';
					if(document.getElementById('span_delete')) document.getElementById('span_delete').style.display='none';
					command=command+"&cID="+template_id;
					break;
				case "ajx_get_details":
					command=command+"&cID="+template_id;
					document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
					display_flag=true;
					break;
				case "ajx_delete":
					if(document.getElementById('span_edit')) document.getElementById('span_edit').style.display='none';
					if(document.getElementById('span_delete')) document.getElementById('span_delete').style.display='';
					command=command+"&cID="+template_id;
					break;
				case "ajx_deleteconfirm":
					if(document.getElementById('span_edit')) document.getElementById('span_edit').style.display='none';
					if(document.getElementById('span_delete')) document.getElementById('span_delete').style.display='';
					command=command+"&cID="+template_id;
					if(document.getElementById('delete_image').checked)
					command+='&delete_image=on';
					break;
				case "ajx_set_template_display":
					if(iid)
						command=command+"&cID="+template_id+"&iID="+iid+"&flag="+col+"&case="+pos;
					else
						command=command+"&cID="+template_id+"&flag="+col+"&case="+pos;
					break;
				case "ajx_move_right_left":
					command=command+"&cID="+template_id+"&iID="+iid+"&flag="+col;
					break;
				case "ajx_move_up_template":
				case "ajx_move_down_template":
					command=command+"&cID="+template_id+"&loc="+pos+"&col="+col;
					break;
				case "ajx_close_template_id_elements":
					hide_template_id_panel(template_id);
					toggle_template_panel(document.getElementById("panel_"+template_id+"_row"),template_id);
					return;
				case "ajx_new":
					if (prev_template_id!=''){
						hide_template_id_panel(prev_template_id);
					}
					document.getElementById("panel_new").style.display="";
					document.getElementById("panel_new").innerHTML=document.getElementById("ajx_load_content_img").innerHTML;
					display_flag=false;
					break;
				case "ajx_save":
					if (is_fetching) return;
					do_post_command("config_input",command+"&cID="+template_id);
					return;
				case "ajx_insert":
					//if (!AIM.submit(document.config_input, {'onStart' : startCallback, 'onComplete' : completeCallback}))return;
					//AIM.submit(document.config_input, {'onStart' : startCallback, 'onComplete' : completeCallback});
					//do_post_command("config_input",command);
					document.config_input.submit();
					return;
			}
			do_page_ajax(command);
		}
		function do_result(result){ 
			var cmd;
			var id;
			var icnt;
			var n;
			var sort_order;
			var cfg_key
			cmds_splt=result.split("{}");

			for (icnt=0,n=cmds_splt.length;icnt<n;icnt++){
				cmd_splt=cmds_splt[icnt].split("||");
				switch(cmd_splt[0]){
					case "template_id":
						template_id=cmd_splt[1];
						break;
					case "sql_exists":
						if(cmd_splt[1]==1)
							document.getElementById("show_infobox_sql").style.display="none";
						break;
					case "template_array":
						temp=cmd_splt[1].split('^');
						template=new Array();
						for(i=0;i<temp.length-1;i++)
						{
							content=temp[i];
							temp2=content.split('|');
							template[i]=new Array(2);
							template[i][0]=temp2[0];
							template[i][1]=temp2[1];
						}
						fill_template(template);
						break;
					case "position":
						document.getElementById("panel_"+template_id+"_row").position=cmd_splt[1];
						break;
					case "result_new":
						document.getElementById("panel_new").innerHTML=cmd_splt[1];
						document.getElementById("panel_new").style.display="";
						//document.getElementById("panel_new_action").style.display="";
						display_flag = false;
						break;
					case "new_template":
						temp=cmd_splt[1].split("^");
						template_id=temp[0];
						template_name=temp[1];
						table=document.getElementById("panel_template");
						row_line = '<tr id="panel_' + template_id +  '_row" class="dataTableRowOdd" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="javascript:toggle_template_panel(this,\'' + template_id + '\')">';
						col_line = '<td valign="top">';
						append_row='<div id="panel_' + template_id + '_title">';
						append_row+='<table border="0" cellpadding="0" cellspacing="0" height="25" style="padding-top:4px;">';
						append_row+='<tr><td width="10"></td>';
						append_row+='<td class="main" width="20" valign="middle">';
						append_row+='<?php echo tep_image(DIR_WS_IMAGES . "template/icon_active.gif", IMAGE_INSTALLED, "", "","align=absmiddle");?>';
						append_row+='</td>';
						append_row+='<td class="dataTableContent" valign="middle" id="panel_'+ template_id +'_row_text">';
						append_row+=template_name;
						append_row+='</tD></tr></table></div><div id="panel_' + template_id + '_content" style="display:none" class="openContent1">';
						append_row+='</div>';
						if(document.all){
							r=table.insertRow();
							r.id = "panel_"+template_id+"_row";
							cell=r.insertCell();
							cell.valign = "top";
							cell.innerHTML = append_row;
						}else{
							cell = table.appendChild(document.createElement("table"));
							cell.width = "100%";
							cell.innerHTML = row_line+col_line+append_row+"</td></tr>";
						}
						document.getElementById("panel_new").style.display="none";
						//document.getElementById("panel_new_action").style.display="none";
						document.getElementById('img_new').firstChild.src=document.getElementById('img_new').firstChild.src.replace('_hover.gif','.gif');
						toggle_template_panel(document.getElementById("panel_" + template_id +  "_row"),template_id);
					    display_flag=true;
					   
						break;
					case "change_default":
						if(document.getElementById("default_template"))prev_default=document.getElementById("default_template").innerHTML;
						if(typeof(prev_default)!="undefined" && prev_default!=""){
							text_val=document.getElementById("panel_"+prev_default+"_row").innerHTML;
							text_val=text_val.replace('<b>','');
							text_val=text_val.replace('</b>','');
							text_val=text_val.replace('(default)','');
							if(document.all){
								table = document.getElementById("panel_"+prev_default+"_row");
								for(i=0;i<table.cells.length;i++)table.removeChild(table.cells[i]);
								cell=table.insertCell();
								cell.valign = "top";
								cell.innerHTML = text_val;
							}else{
								document.getElementById("panel_"+prev_default+"_row").innerHTML=text_val;
							}
						}
						document.getElementById("default_template").innerHTML=cmd_splt[1];
						break;
					case "sort_order":
						sort_order=cmd_splt[1];
						break;
					case "title":
						document.getElementById("panel_"+template_id+"_row").text_title=cmd_splt[1];
						break;
					case "command":
						cmd=cmd_splt[1];
						break;
					case "cfg_key":
						cfg_key=cmd_splt[1];
						break;
					case "result_title":
						document.getElementById("panel_"+template_id+"_title").innerHTML=cmd_splt[1];
						break;
					case "remove_row":
						document.getElementById("panel_"+template_id+"_title").style.display="none";
						document.getElementById("panel_"+template_id+"_row").style.display="none";
						break;
					case "result_content":
						document.getElementById("panel_"+template_id+"_content").innerHTML=cmd_splt[1];
						if (cmd=="ajx_remove_template_id_elements"){
							move_row(template_id,'',-1);
						} else if (cmd=="ajx_install_template_id_elements"){
							document.getElementById("panel_"+template_id+"_row").sort_order=sort_order;
							move_row(template_id,'',sort_order-1);
						}
						//toggle_template_panel(document.getElementById("panel_" + template_id +  "_row"),template_id);
						break;
				}
			}
			is_fetching=false;
		}
		 function fill_template(template){
			with (document.config_input) {	
				for(i=info_box_sql.options.length;i>=0;i--) {
					info_box_sql.options[i]=null;
				}
				for(i=0; i < template.length; i++){
						var Option_value=new Option(template[i][1],template[i][0]);
						info_box_sql.options[info_box_sql.options.length] = Option_value;
						if(template[i][1].indexOf('(default)')>=0)
							info_box_sql.options[i].selected=true;
				}	
				if(info_box_sql.options.length>0)
					document.getElementById("show_infobox_sql").style.display="";
			}
		}
		function move_row(from_template_id,to_template_id,pos){
			var icnt;
			var toRow;
			fromRow=document.getElementById("panel_"+from_template_id+"_row");
			table=fromRow.parentNode;
			var count=get_installed_count(table);
			
			if (to_template_id=="" && pos==-1){
				table.appendChild(fromRow);
				realign_row_class(table);
				return;
			} else {
				pos=get_proper_position(table,pos,count);
				if (pos && pos>=count){
					pos=count;
				} else if (pos && pos<0){
					pos=0;
				}
			}
			toRow=table.rows[pos];
			table.insertBefore(fromRow,toRow);
			realign_row_class(table);
		}
		function get_proper_position(table,pos,count){
			for (icnt=0;icnt<count;icnt++){
				if (pos>=table.rows[icnt].sort_order) return icnt;
			}
			return 0;
		}
		function get_installed_count(table){
			var icnt;
			var n;
			for (icnt=0,n=table.rows.length;icnt<n;icnt++){
				if (table.rows[icnt].cur_position=="inactive") break;
			}
			return icnt++;
		}
		function realign_row_class(table){
			var icnt,n;
			for (icnt=0,n=table.rows.length;icnt<n;icnt++){
				if (table.rows[icnt].opened) continue;
				if (table.rows[icnt].className.indexOf("hover")!=-1) continue;
				if (icnt%2==0){
					table.rows[icnt].className="dataTableRowEven";
				} else {
					table.rows[icnt].className="dataTableRowOdd";
				}
			}
		}
		function move_updown(template_id,type){
			var row=document.getElementById("panel_"+template_id+"_row");
			var index=row.rowIndex;
			var count=get_installed_count(row.parentNode);
			if (type=="up" && index==0) return;
			if (type=="down" && index==count-1) return;
			if (type=="up"){
				row.parentNode.insertBefore(row,row.parentNode.rows[index-1]);
			} else {
				if (index==row.parentNode.rows.length-2)
					row.parentNode.appendChild(row);
				else
					row.parentNode.insertBefore(row,row.parentNode.rows[index+2]);
			}
			realign_row_class(row.parentNode);
			do_page_fetch("ajx_move_"+type+"_template_id",template_id,row.sort_order);
		}
		function sort_template_id_rows(mode){
			var table=document.getElementById("panel_template");
			var icnt,n,jcnt;
			var text1,text2;
			for (icnt=0,n=table.rows.length;icnt<n-1;icnt++){
				if (table.rows[icnt].cur_position=="inactive") break;
				for (jcnt=icnt+1;jcnt<n;jcnt++){
					if (table.rows[jcnt].cur_position=="inactive") break;
					text1=document.getElementById(table.rows[icnt].id+"_text").innerHTML;
					text2=document.getElementById(table.rows[jcnt].id+"_text").innerHTML;
					if (mode=="A" && text2<text1){
						move_by_index(table,jcnt,icnt,'Y');
					} else if (mode=="D" && text2>text1){
						move_by_index(table,jcnt,icnt,'Y');
					}
				}
			}
			for (;icnt<n-1;icnt++){
				for (jcnt=icnt+1;jcnt<n;jcnt++){
					text1=document.getElementById(table.rows[icnt].id+"_text").innerHTML;
					text2=document.getElementById(table.rows[jcnt].id+"_text").innerHTML;
					if (mode=="A" && text2<text1){
						move_by_index(table,jcnt,icnt,'N');
					} else if (mode=="D" && text2>text1){
						move_by_index(table,jcnt,icnt,'N');
					}
				}
			}
			//realign_row_class(table.rows[0].parentNode);
		}
		function move_by_index(table,from,to,active){
			var sort_order1=table.rows[from].sort_order;
			var sort_order2=table.rows[to].sort_order;

			table.rows[to].parentNode.insertBefore(table.rows[from],table.rows[to]);
			
			if (Math.abs(from-to)>1){
				table.rows[to].parentNode.insertBefore(table.rows[to+1],table.rows[from]);
			}
			if (active!='Y') return;
			
			table.rows[from].sort_order=sort_order1;
			table.rows[to].sort_order=sort_order2;
		}
	function startCallback() {
		// make something useful before submit (onStart)
		return true;
	}

	function completeCallback(response) {
		// make something useful after (onComplete)
		do_result(response);
	}
	</script>

<!-- body_eof //-->
</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="3" cellpadding="1">
  <tr> 
<!-- body_text //-->
    <td width="100%" valign="top">
		<?php echo tep_draw_form("config_input",FILENAME_TEMPLATE_CONFIGURATION,'command=ajx_insert','post',' onsubmit="javascript:return AIM.submit(document.config_input, {\'onStart\' : startCallback, \'onComplete\' : completeCallback});" enctype="multipart/form-data"');?>
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
       <tr>
	   	  <td align="left" colspan="2" class="smallText">
		  	<Table width="100%">
				<tr class="dataTableHeadingRow" style="height:25px">
					<td width="90%" valign="bottom">
						<?php echo '<a href="javascript:do_page_fetch(\'ajx_new\');" id="img_new" onmouseover="javascript:do_hover_change(this,2);" onmouseout="javascript:do_hover_change(this,1);">' . tep_image('images/template/img_new.gif',IMAGE_NEW,'','',' align="absmiddle"') .'</a>';?>
					</td>
				</tr>
			</Table>
		</td></tr>
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr class="dataTableHeadingRow">
		  	<td width="10"></td>
		  	<td width="20" class="dataTableHeadingContent">
				<?php 
					echo tep_image(DIR_WS_IMAGES . 'template/icon_ascending.gif',IMAGE_ASCENDING,'','',' id="pop_common_sort_display_con" onMouseOver="javscript:javascript:show_cpopup(\'sort_display\',0,20);"');
					echo '<input type="hidden" name="show_sort_order" id="show_sort_order" value="AN">';
					echo "<div id='pop_common_sort_display' style='position:absolute;display:none;width:100px;height:50px;z-index:999;border:1px solid;border-color:#6883CA;background-color:#FFFFFF;padding:3px;'>";
					echo tep_draw_separator('pixel_trans.gif','10','5');
					echo '<br><table class="main" cellpadding="0" cellspacing="0">';
					$popup=array(array("img"=>'icon_ascending.gif',"txt"=>TEXT_ASCENDING,'id'=>'A'),array("img"=>'icon_descending.gif',"txt"=>TEXT_DESCENDING,'id'=>'D'));
					for ($icnt=0,$n=count($popup);$icnt<$n;$icnt++){
						echo '<tr height="20" class="tableMenuItem" onMouseOver="javascript:rowOverEffect(this)" onMouseOut="javascript:rowOutEffect(this)"><td valigm="middle" width="18">' . tep_image(DIR_WS_IMAGES . "template/" .$popup[$icnt]["img"],$popup[$icnt]["txt"],'','','align=absmiddle') . '</td><td>';
						echo '<a style="color:#000000;text-decoration:none" href="javascript:sort_template_id_rows(\'' . $popup[$icnt]["id"] . '\');">' . $popup[$icnt]["txt"] . '</a></td></tr>';
						echo '<tr height="3"><td></td></tr>';
					}
					echo "</td></tr></table></div>";
				?>
			</td>
			<td class="dataTableHeadingContent"><?php echo HEADING_TITLE; ?></td>
          </tr>
        </table></td>
      </tr>
		<tr><Td id="panel_new" style="display:none"></Td>
			<!--<Td id="panel_new_action" style="display:none" nowrap align="right" valign="top">
			</Td>-->
</tr>

      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0" id="panel_template">
<?php 
		$template_query = tep_db_query("select * from " . TABLE_TEMPLATE . "  order by template_name asc");
		$i=0;
		 while ($template = tep_db_fetch_array($template_query)) {
			$tInfo_array = ($template);
			$tInfo = new objectInfo($tInfo_array);
			$i++;
			if (DEFAULT_TEMPLATE == $tInfo->template_name){
				echo "<div id='default_template' style=display:none>" . $tInfo->template_id . "</div>";
				$default_template_id=$tInfo->template_id;
			}
			echo '<tr id="panel_' . $tInfo->template_id . '_row" class="' . ($i%2==0?'dataTableRowEven':'dataTableRowOdd') .  '" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="javascript:toggle_template_panel(this,\'' .$tInfo->template_id . '\')">' . "\n";
			echo '<td valign="top">';
			echo '<div id="panel_' . $tInfo->template_id . '_title">';
			echo '<table border="0" cellpadding="0" cellspacing="0" height="25" style="padding-top:4px;">';
			echo '<tr>';
			echo '<td width="10"></td>';
			echo '<td class="main" width="20" valign="middle">';
			if ($tInfo->active) {
				echo tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', IMAGE_INSTALLED, '', '','align=absmiddle');
				$position="normal";
			} else {
				$position="inactive";
				echo tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', IMAGE_REMOVED, '', '','align=absmiddle');
			}
			echo '</td>';
			echo '<td class="dataTableContent" valign="middle" id="panel_' . $tInfo->template_id . '_row_text">';
			if (DEFAULT_TEMPLATE != $tInfo->template_name)
				echo $tInfo->template_name;
			else
				echo '<b>' . $tInfo->template_name .' (default)</b>';
			echo '</tD>';
			echo '</tr>';
			echo '</table>';
			echo '</div>';
			echo '<div id="panel_' . $tInfo->template_id . '_content" style="display:none" class="openContent1">';
			echo '</div>';
			echo '</td>';
			echo '</tr>';
			echo '<script language="javascript">';
			echo 'document.getElementById("panel_' . $tInfo->template_id . '_row").cur_position="' . $position . '";';
			echo 'document.getElementById("panel_' . $tInfo->template_id . '_row").sort_order="' . $i . '";';
			echo 'document.getElementById("panel_' . $tInfo->template_id . '_row").sort_key="' . $tInfo->template_name . '";';
			echo '</script>';
		}
?>
	</table>
	</td>
          </tr>
        </table>
		<div id="ajx_load_content_img" style="display:none"><div style="height:30px;margin-left:30px"><img src="images/24-1.gif"></div>
			<?php if($default_template_id!=""){?>
			  	<script>
					//toggle_template_panel(document.getElementById("panel_<?php echo $default_template_id;?>_row"),'<?php echo $default_template_id?>');
				</script>
			  <?php }?>
		</td>
      </tr>
    </table>
	</form>
	</td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); 
function get_template_info($t_id)
{
	if($t_id!='')
	{
	  $template_query = tep_db_query("select * from " . TABLE_TEMPLATE . "  where template_id='" .$t_id . "'");
	  $template = tep_db_fetch_array($template_query);
	  $tInfo_array = ($template);
	  $tInfo = new objectInfo($tInfo_array);
	  return $tInfo;
	}
}
function image_resize($image){
	
	$images = getimagesize($image);
//	print_r($images);
	$width = $images[0];
	$height = $images[1];
	$co_width = $width/5;
	$width = $width-$co_width;
	$co_height = $height/3;
	$height = $height - $co_height;
	return tep_image($image,'',$width,$height);
}
function out_result_template_details($cID)
{
  global $position;
    $tInfo=get_template_info($cID);	
	if(!$tInfo->active)
		$position="inactive";
	else
		$position="active";
	$result='<div style="margin-top:-30px;padding-left:30px;vertical-align:top"><table border="0" cellpadding="0" cellspacing="0" width="100%" class="openContent">
		<tr>';
	//print_r(getimagesize(DIR_WS_IMAGES . $tInfo->template_image));	
	if($tInfo->template_image!='' && file_exists(DIR_WS_IMAGES . $tInfo->template_image))
		$result.='<td valign="top" align="center" width="150">' . image_resize(DIR_WS_IMAGES . $tInfo->template_image) . '</td>';
		//$result.='<td valign="top" align="center" width="150">' . tep_image(DIR_WS_TEMPLATES.DEFAULT_TEMPLATE.'/'.DIR_WS_IMAGES . $tInfo->template_image,'','95','113') . '</td>';
	$result.='<td valign="top">' .
			'<table border="0" cellpadding="2" cellspacing="5" width="100%">
				<tr><td width="120" class="main">'.TEXT_TEMPLATENAME . '</td>
					<td valign="top"  class=main><b>'. $tInfo->template_name . '</b></td></tr>
					<tr><td class="main" >' .TEXT_DATE_ADDED . '</td><td class="main" id="panel_' . $tInfo->template_id . '_row_text">'. format_date($tInfo->date_added).'</td></tr>';
	if (tep_not_null($tInfo->last_modified))
		$result.='<Tr><td class="main" >' . TEXT_LAST_MODIFIED . '</td><td class=main>' .  format_date($tInfo->last_modified) . '</td></Tr>';
	$result.='<tr height=50><td colspan="2"><br></td></tr></table></td></tr><tr><td>' . tep_draw_separator('pixel_trans.gif','1','15') . '</td></tr></table></div>';	
	$result.='{}title||' . $tInfo->template_name;
	return $result;
}

 function out_result_template_title($edit=false,$delete=false){
	  global $cID,$default_template;
	  $tInfo=get_template_info($cID);
	  $active=$tInfo->active;
	  $show_image=($active?"icon_active.gif":"icon_inactive.gif");
	  $alt_text=($active?" Inactive":" active");
	  $sql = "select count(*) as count from " . TABLE_TEMPLATE;
	  $query = tep_db_query($sql);
	  $result_sql = tep_db_fetch_array($query);
		$result='
					<table border="0" cellpadding="0" cellspacing="0" height="25" width="100%" style="padding-top:10px;">
						<tr height="40">				
							<td width="10"></td>';
							if($default_template!=$tInfo->template_name)
								$result .= '<td class="main" width="20" valign="top"><a href="javascript:do_page_fetch(\'ajx_setflag\',\'' . $tInfo->template_id . '\',\'' . $active . '\');">' .	tep_image(DIR_WS_IMAGES . 'template/' . $show_image,sprintf(TEXT_CLICK_TO,$alt_text)) . '</a></td>';
							else
								$result .= '<td class="main" width="20" valign="top">' .	tep_image(DIR_WS_IMAGES . 'template/' . $show_image,sprintf(TEXT_CLICK_TO,$alt_text)) . '</td>';
				$result.=		'<td class="smallText" valign="top" id="panel_' . $tInfo->template_id . '_row_text">' . (($default_template!=$tInfo->template_name)?$tInfo->template_name:'<b>' . $tInfo->template_name. ' (default)</b>') . '</td>';
				if ($edit || $delete){
					$cmd=($edit)?'ajx_save':'ajx_deleteconfirm';
					$result.=		'<td width="150" class="main" align="right" style="padding-left:10px;padding-right:10px;padding-top:3px;"><a href="javascript:do_page_fetch(\'' .$cmd . '\',\'' . $tInfo->template_id . '\');">' . tep_image(DIR_WS_IMAGES .  'template/img_savel.gif',IMAGE_SAVE) .'</a>&nbsp;
										<a href="javascript:do_page_fetch(\'ajx_close_template_id_elements\',\'' . $tInfo->template_id . '\');">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif',IMAGE_CLOSE_EDIT) . '</a>
										</td>
							 ';
				}
				$result.='
								</tr>
							</table>
							<center>
							<table border="0" cellpadding="0" cellspacing="0" style="width:100;height:30;display:inline;position:relative;top:-40px;">
								<tr class="img_move" id="img_ids">
									<td width="10" class="cell_bg_popm_left">&nbsp;</td>' .
									(($edit)? '<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change2(this,1);do_page_fetch(\'ajx_edit\',\'' . $tInfo->template_id . '\')";>' . tep_image(DIR_WS_IMAGES . 'template/img_edit_hover.gif',IMAGE_EDIT,'','','id="img_edit"') . '</td>':'<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change2(this,1);do_page_fetch(\'ajx_edit\',\'' . $tInfo->template_id . '\')" onMouseOver="javascript:do_hover_change2(this,2);" onMouseOut="javascript:do_hover_change2(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/img_edit.gif',IMAGE_EDIT,'','','id="img_edit"') . '</td>') .  
								//	'<td width="10%" onclick="javascript:do_hover_change2(this,1);do_page_fetch(\'ajx_edit\',\'' . $tInfo->template_id . '\')"; onMouseOver="javascript:do_hover_change2(this,2);" onMouseOut="javascript:do_hover_change2(this,3);"><img alt="Edit" title="Edit" src="images/template/img_edit.gif"></td>
									(($result_sql['count']>1)?(($delete)? '<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change2(this,1);do_page_fetch(\'ajx_delete\',\'' . $tInfo->template_id . '\')";>' . tep_image(DIR_WS_IMAGES . 'template/img_trash_hover.gif',IMAGE_DELETE,'','','id="img_delete"') . '</td>':'<td width="10%" style="background:#DEE4F5;" OnClick="javascript:do_hover_change2(this,1);do_page_fetch(\'ajx_delete\',\'' . $tInfo->template_id . '\')" onMouseOver="javascript:do_hover_change2(this,2);" onMouseOut="javascript:do_hover_change2(this,3);">' . tep_image(DIR_WS_IMAGES . 'template/img_trash.gif',IMAGE_DELETE,'','','id="img_delete"') . '</td>'):'') . 
								//	'<td width="10%" onclick="javascript:do_hover_change2(this,1);do_page_fetch(\'ajx_delete\',\'' . $tInfo->template_id . '\')" onMouseOver="javascript:do_hover_change2(this,2);" onMouseOut="javascript:do_hover_change2(this,3);"><img alt="Delete" title="Delete" src="images/template/img_trash.gif"></td> 
									'<td width="10" class="cell_bg_popm_right">&nbsp;</td>
								</tr>
								<tr><td><span id="span_edit" style="display:none">&nbsp;</span></td></tr>
								<tr><td><span id="span_delete" style="display:none">&nbsp;</span></td></tr>
							</table>
							</center>
						';
			return $result;
		}

function out_result_template_edit($cID)
{
	global $position;
	$tInfo=get_template_info($cID);
	$position="edit";
	$result='<div style="margin-top:-30px;padding-left:10px;vertical-align:top;width:100%;"><table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr> 
					<td class="main" width="20">' . tep_image(DIR_WS_IMAGES . 'template/img_edit.gif') .
					'</td><td class="smalltext"><b>' . '<font color="#6666FF">' . TEXT_EDIT . '</font>' . '</b></td>' . 
				'</tr>
			</table>
			' ;// tep_draw_form("config_input",FILENAME_TEMPLATE_CONFIGURATION,'command=ajx_save&cID=' . $cID,'post',' enctype="multipart/form-data"') ;
	echo $result;
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%" >
  <tr>
    <td width="100%" valign="top" class="smalltext">
    <table border="0" cellpadding="5" cellspacing="0" width="100%">
	  <tr>
	  	<td width="20%"  colspan="1" class="smalltext"><?php echo TEXT_SET_DEFAULT;?></td>
        <td width="40%"  colspan="1" class="smalltext"><?php echo tep_draw_hidden_field('template_name', $tInfo->template_name); ?>
         <?php if (DEFAULT_TEMPLATE != $tInfo->template_name) 
		 			echo  tep_draw_checkbox_field('default','on');
				else
					echo tep_draw_checkbox_field('default','on',true)?>
		</td>		
         <td width="20%"  colspan="1" class="smalltext">
			<?php echo TEXT_SITE_WIDTH;?> 
		</td>
		<td width="40%"  colspan="1" class="smalltext">	
			<?php echo tep_draw_input_field('site_width', $tInfo->site_width,'size="3"');?>
		   </td>
      </tr>
      <tr>
        <td class="smalltext" nowrap><?php echo TEXT_INCLUDE_CART_IN_HEADER;?></td>
		<td><?php 
     	 if ($tInfo->cart_in_header == 'yes') {
 			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'cart_in_header\',\'no\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
     	 } else {
 			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'cart_in_header\',\'yes\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
	      }
		?>
        </td>
        <td class="smalltext" nowrap> <?php echo TEXT_INCLUDE_LANGUAGES_IN_HEADER;?></td>
		<td class="smalltext"><?php 
      if ($tInfo->languages_in_header == 'yes') {
 			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'languages_in_header\',\'no\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
      } else {
 			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'languages_in_header\',\'yes\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
      }
?>
       </td>
    </tr>
	<tr>
        <td  class="smalltext" >
        <?php echo TEXT_TEMPLATE_CELLPADDING_MAIN;?></td>
		<td class="smalltext">
<?php
    $select_box = '<select name="template_cellpadding_main" " style="width: 50" class="inputNormal">';
	for ($i = 0; $i <= 10; $i++) {
      $select_box .= '<option value="' . $i . '"';
      if ($i == $tInfo->template_cellpadding_main) $select_box .= ' SELECTED';
    	  $select_box .= '>' . $i . '</option>';
		}
	 $select_box .= "</select>";
	echo $select_box;
	?>
	</td>
     <td  class="smalltext" >
	 <?php echo TEXT_TEMPLATE_CELLPADDING_SUB;?></td>
	 <td class="smalltext">
        <?php
    $select_box = '<select name="template_cellpadding_sub" " style="width: 50" class="inputNormal">';
	for ($i = 0; $i <= 10; $i++) {
      $select_box .= '<option value="' . $i . '"';
      if ($i == $tInfo->template_cellpadding_sub) $select_box .= ' SELECTED';
      $select_box .= '>' . $i . '</option>';
	}
    $select_box .= "</select>";
	echo $select_box;
?>	
        </td>
      </tr>
<tr>
     <td class="smalltext" nowrap>
     <?php echo TEXT_INCLUDE_HEADER_LINK_BUTTONS;?>
	 </td>
     <td colspan="3">
		<Table id="show_link">
		<tr>
		<td>
		<?php
			  if ($tInfo->show_header_link_buttons == 'yes') 
					echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'show_header_link_buttons\',\'no\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
			  else
					echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'show_header_link_buttons\',\'yes\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
			?>		
		</td>
		<!--<?php// if ($tInfo->show_header_link_buttons == 'yes') {?>
        <td width="17%"   align="center" class="smalltext">
        Link Button1<br>
        <?php //echo tep_draw_input_field("T1",'',' size="10"');?></td>
        <td width="17%"  align="center" class="smalltext">
        Link Button2<br>
        <?php //echo tep_draw_input_field("T2",'',' size="10"');?></td>
        <td width="17%"  align="center" class="smalltext">
        Link Button3<br>
        <?php //echo tep_draw_input_field("T3",'',' size="10"');?></td>
        <td width="16%" align="center" class="smalltext">
        Link Button4<br>
        <?php// echo tep_draw_input_field("T4",'',' size="10"');?></td>
        <td width="16%"  align="center" class="smalltext">
        Link Button5<br>
        <?php //echo tep_draw_input_field("T5",'',' size="10"');?></td>
		<?php // }?>-->
      </tr>
	  </table>
	 </td>
	</tr>
  </table>
    <table border="0" cellpadding="5" cellspacing="0" width="99%" >
	<tr>
		<Td style="background:#DEE4F5;" height=40 class="smalltext" width="35%" colspan="2"><b><?php echo tep_image(DIR_WS_IMAGES . 'template/img_bullet1.gif') . '&nbsp;&nbsp;&nbsp;&nbsp;' . '<font color="#6666FF">' . TEXT_LEFT_COLUMN . '</font>'  ;?></b></Td>
		<Td  class="smalltext" width="30%" colspan="2"><b><?php echo tep_image(DIR_WS_IMAGES . 'template/img_bullet1.gif') . '&nbsp;&nbsp;&nbsp;&nbsp;' . '<font color="#6666FF">' . TEXT_MAIN_TABLE .  '</font>' ;?></b></Td>
		<Td style="background:#DEE4F5;" class="smalltext" width="35%" colspan="2"><b><?php echo tep_image(DIR_WS_IMAGES . 'template/img_bullet1.gif') . '&nbsp;&nbsp;&nbsp;&nbsp;' . '<font color="#6666FF">' . TEXT_RIGHT_COLUMN . '</font>' ;?></b></Td>
	</tr>
     <tr>
	 	<td width="10%" style="background:#DEE4F5;" class="smallText" nowrap><?php echo TEXT_INCLUDE_COLUMN_LEFT ;?></td>
		<td width="25%" style="background:#DEE4F5;" class="smallText">
		   <?php
		  if ($tInfo->include_column_left == 'yes') {
  	   			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'include_column_left\',\'no\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
		  } else {
  	   			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'include_column_left\',\'yes\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
		  }
		?>
		</td>
        <td class="smalltext" width="10%" nowrap>Show Page descriptions</td>
        <td class="smalltext" width="20%">
         <?php
	      if ($tInfo->show_page_descriptions == 'yes') {
	   			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'show_page_descriptions\',\'no\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
	      } else {
	   			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'show_page_descriptions\',\'yes\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
	      }
		?>
        </td>
		<td width="10%" style="background:#DEE4F5;" class="smallText" nowrap><?php echo TEXT_INCLUDE_COLUMN_RIGHT;?></td>
		<td width="25%" style="background:#DEE4F5;" class="smallText">
		     <?php  if ($tInfo->include_column_right == 'yes') 
			   			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'include_column_right\',\'no\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
					else
			   			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'include_column_right\',\'yes\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
				?>
		</td>
	 </tr>
      <tr>
        <td  class="smalltext" style="background:#DEE4F5;" nowrap>
        Left column cellpadding
        </td>
        <td class="smalltext" style="background:#DEE4F5;">
        <?php
		$select_box = '<select name="template_cellpadding_left" " style="width: 50" class="inputNormal">';
		for ($i = 0; $i <= 10; $i++) {
			  $select_box .= '<option value="' . $i . '"';
			  if ($i == $tInfo->template_cellpadding_left) $select_box .= ' SELECTED';
			  $select_box .= '>' . $i . '</option>';
		}
		$select_box .= "</select>";
		echo $select_box;
		?></td>
        <td class="smalltext" nowrap>Include Main Table Border</td>
		<td class="smallText">
         <?php
	      if ($tInfo->main_table_border == 'yes') {
  			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'main_table_border\',\'no\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
	      } else {
  			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'main_table_border\',\'yes\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
	      }
		?></td>
      	<td class="smalltext" style="background:#DEE4F5;" nowrap>Right Column cell padding</td>
        <td class="smalltext" style="background:#DEE4F5;">
        <?php
			$select_box = '<select name="template_cellpadding_right" " style="width: 50" class="inputNormal">';
			for ($i = 0; $i <= 10; $i++) {
				  $select_box .= '<option value="' . $i . '"';
				  if ($i == $tInfo->template_cellpadding_right) $select_box .= ' SELECTED';
				  $select_box .= '>' . $i . '</option>';
			}
			$select_box .= "</select>";
			echo $select_box;
		?></td> 
       </tr>
      <tr>
        <td class="smalltext" style="background:#DEE4F5;" nowrap>
        <?php echo TEXT_COLUMN_LEFT_WIDTH;?>
        </td>
        <td style="background:#DEE4F5;" >
         <?php echo tep_draw_input_field('box_width_left', $tInfo->box_width_left,'size="3"');?></td>
         <td class="smalltext">Show Customer Greeting?</td>
		 <td class="smalltext">
         <?php
     	 if ($tInfo->customer_greeting == 'yes') {
			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'customer_greeting\',\'no\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
	   } else {
			echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'customer_greeting\',\'yes\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
      }
?></td>
        <td  class="smalltext" style="background:#DEE4F5;" nowrap><?php echo TEXT_COLUMN_RIGHT_WIDTH;?></td>
        <td class="smalltext" style="background:#DEE4F5;">
        <?php echo tep_draw_input_field('box_width_right', $tInfo->box_width_right,'size="3"');?></td>
      </tr>
	 <tr>
        <td width="100%" colspan="2"  style="background:#DEE4F5;" valign="top">
			<table border="0" cellpadding="3" cellspacing="0" width="100%">
		    <tr>
				 <td class="smalltext" colspan="2">
					<?php echo "&nbsp;&nbsp;&nbsp;&nbsp;<b> Infoboxes </b>";?>
				 </td>
	        </tr>

				<?php
				 $icnt=1;
 				 $infobox_sql = tep_db_query("select max(location) as loc_limit from " . TABLE_INFOBOX_CONFIGURATION . " where template_id = '" . $cID . "' and display_in_column = 'left' and infobox_display='yes' group by template_id");
				 if(tep_db_num_rows($infobox_sql))
				 {
				 	$row=tep_db_fetch_array($infobox_sql);
					$loc_limit=$row["loc_limit"];
				 }
				 $infobox_sql = "select infobox_id, display_in_column, infobox_file_name, location,infobox_display from " . TABLE_INFOBOX_CONFIGURATION . " where template_id = '" . $cID . "' and display_in_column = 'left'  order by infobox_display desc,location asc";
				 $infobox_query = tep_db_query($infobox_sql);
 				 while ($infobox = tep_db_fetch_array($infobox_query)) {
					  $infid = $infobox['infobox_id'];
					  $infcol = $infobox['display_in_column'];
					  $infname = $infobox['infobox_file_name'];
					  $infloc = $infobox['location'];
					  $infdis = $infobox['infobox_display'];
				?>
				<tr>
					<td><?php if($infdis=='yes') 
							echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'infobox_display\',\'no\',\'' . $infid . '\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
						 else
							echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'infobox_display\',\'yes\',\'' . $infid . '\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
							?>
					</td>
    				<td class="smalltext"><?php echo $infname;?></td>
<?php if($infdis=='yes')
	  {
	  	echo "<td width=10>";
	    if ($icnt>1) {
		  echo '<a href="javascript:do_page_fetch(\'ajx_move_up_template\',\'' . $tInfo->template_id  . '\',\'' . $infloc . '\',\'' . $infcol . '\');">' . tep_image(DIR_WS_IMAGES . 'template/img_arrow_up.gif', IMG_MOVE_UP) . '</a>';
	    }
		echo "</td><td width=10>";
	    if ($loc_limit!=$infloc) {
		  echo '<a href="javascript:do_page_fetch(\'ajx_move_down_template\',\'' . $tInfo->template_id  . '\',\'' . $infloc . '\',\'' . $infcol . '\');">' . tep_image(DIR_WS_IMAGES . 'template/img_arrow_down.gif', IMG_MOVE_DOWN) . '</a>';
	    }
		echo "</td>";
		$icnt++;
	 }
	 else
	 {
	 echo "	<td colspan=2></td>";
	 }
?>
<td align="right">
 <?php echo '<a href="javascript:do_page_fetch(\'ajx_move_right_left\',\'' . $tInfo->template_id . '\',\'\',\'right\',\'' . $infid . '\');">' . tep_image(DIR_WS_IMAGES . 'template/arrow_right.gif', TEXT_MOVE_RIGHT) . '</a>&nbsp;&nbsp;';?>
</td>
</tr>
<?php
    }
?>
	</table>
	</td>
<td colspan="2" valign="top">
	<Table cellpadding="5" cellspacing="0" border="0">
		<Tr><td class="smalltext" width="66%"  align="left" colspan="2" >
		     <?php if ($tInfo->customer_greeting == 'yes') {
					echo 'Text for Customer Logged On<br>' . tep_draw_textarea_field('edit_customer_greeting_personal', 'soft', '40', '5', $tInfo->edit_customer_greeting_personal,'style="border-style: outset; border-width: 3"') . '<br>Text for Customer Re-Logged On<br>';
					echo tep_draw_textarea_field('edit_customer_greeting_personal_relogon', 'soft', '40', '5', $tInfo->edit_customer_greeting_personal_relogon,'style="border-style: outset; border-width: 3"') . '<br>Text for Guest greeting<br>';
					echo tep_draw_textarea_field('edit_greeting_guest', 'soft', '40', '5', $tInfo->edit_greeting_guest,'style="border-style: outset; border-width: 3"'); 
				}?>
			</td>
		</Tr>
	      <tr>
        <td class="smalltext" width="66%"  align="center" colspan="2" >
        Include Which Modules On Main Page?</td>
      </tr>
      <tr>
        <td class="smalltext" width="66%"  align="center" colspan="2">
        <?php echo tep_draw_input_field('include_module_one', $tInfo->module_one);?></td>
      </tr>
      <tr>
        <td class="smalltext" width="66%"  align="center" colspan="2" >
        <?php echo tep_draw_input_field('include_module_two', $tInfo->module_two);?></td>
      </tr>
      <tr>
        <td class="smalltext" width="66%"  align="center" colspan="2" >
        <?php echo tep_draw_input_field('include_module_three', $tInfo->module_three);?></td>
      </tr>
      <tr>
        <td class="smalltext" width="66%"  align="center" colspan="2" >
        <?php echo tep_draw_input_field('include_module_four', $tInfo->module_four);?></td>
      </tr>
      <tr>
        <td class="smalltext" width="66%" align="center" colspan="2" >
        <?php echo tep_draw_input_field('include_module_five', $tInfo->module_five);?></td>
      </tr>
      <tr>
        <td class="smalltext" width="66%"  align="center" colspan="2" >
        <?php echo tep_draw_input_field('include_module_six', $tInfo->module_six);?></td>
      </tr>

	</Table>
</td>
 <td colspan="2" style="background:#DEE4F5;" valign="top">
			<table border="0" cellpadding="3" cellspacing="0" width="100%">
			  <tr>
				 <td class="smalltext" colspan="2">
					<?php echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b> Infoboxes </b>";?>
				 </td>
	         </tr>
				<?php
				 $icnt=1;
				 $loc_limit=0;
  				 $infobox_sql = tep_db_query("select max(location) as loc_limit from " . TABLE_INFOBOX_CONFIGURATION . " where template_id = '" . $cID . "' and display_in_column = 'right' and infobox_display='yes' group by template_id");
				 if(tep_db_num_rows($infobox_sql))
				 {
				 	$row=tep_db_fetch_array($infobox_sql);
					$loc_limit=$row["loc_limit"];
				 }
				 $infobox_query = tep_db_query("select infobox_id, display_in_column, infobox_file_name, location,infobox_display from " . TABLE_INFOBOX_CONFIGURATION . " where template_id = '" . $cID . "' and display_in_column = 'right'  order by infobox_display desc,location asc");
 				 while ($infobox = tep_db_fetch_array($infobox_query)) {
					  $infid = $infobox['infobox_id'];
					  $infcol = $infobox['display_in_column'];
					  $infname = $infobox['infobox_file_name'];
					  $infloc = $infobox['location'];
					  $infdis = $infobox['infobox_display'];
				?>
	  			<tr>
					<td><?php if($infdis=='yes') 
							echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'infobox_display\',\'no\',\'' . $infid . '\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_active.gif', sprintf(TEXT_CLICK_TO,"inactive"), 10, 10) . '</a>';
						 else
							echo '<a href="javascript:do_page_fetch(\'ajx_set_template_display\',\'' . $tInfo->template_id . '\',\'infobox_display\',\'yes\',\'' . $infid . '\');">' . tep_image(DIR_WS_IMAGES . 'template/icon_inactive.gif', sprintf(TEXT_CLICK_TO,"active"), 10, 10) . '</a>';
						?>
					</td>
    				<td class="smalltext"><?php echo $infname;?></td>
<?php if($infdis=='yes')
	  {
	  	echo "<td width=10>";
	    if ($icnt>1) {
		  echo '<a href="javascript:do_page_fetch(\'ajx_move_up_template\',\'' . $tInfo->template_id  . '\',\'' . $infloc . '\',\'' . $infcol . '\');">' . tep_image(DIR_WS_IMAGES . 'template/img_arrow_up.gif', IMG_MOVE_UP) . '</a>';
	    }
		echo "</td><td width=10>";
	    if ($loc_limit!=$infloc) {
		  echo '<a href="javascript:do_page_fetch(\'ajx_move_down_template\',\'' . $tInfo->template_id  . '\',\'' . $infloc . '\',\'' . $infcol . '\');">' . tep_image(DIR_WS_IMAGES . 'template/img_arrow_down.gif', IMG_MOVE_DOWN) . '</a>';
	    }
		echo "</td>";
		$icnt++;
	 }
	 else
	 {
	 echo "	<td colspan=2></td>";
	 }
?>
<td align="right">
 <?php echo '<a href="javascript:do_page_fetch(\'ajx_move_right_left\',\'' . $tInfo->template_id . '\',\'\',\'left\',\'' . $infid . '\');">' . tep_image(DIR_WS_IMAGES . 'template/arrow_left.gif', TEXT_MOVE_LEFT) . '</a>&nbsp;&nbsp;';?>
</td>
</tr>
<?php
    }
?>
	</table>
	</td>
      </tr>
	  <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td></tr>
    </table>
<!--</form>--></div>
<?php 
 echo '{}title||' . $tInfo->template_name;
}

function out_result_template_delete_confirm($cID)
{
 $tInfo=get_template_info($cID);
?>
	<table width=100% cellpadding="5" border="0">
	<tr><td class="smallText"><?php echo tep_image(DIR_WS_IMAGES . 'template/img_trash.gif',IMAGE_REMOVE,'','',' align=absmiddle') . ' <b>' . TEXT_HEADING_DELETE_TEMPLATE . '</b>';?></td></tr>
	<tr><td class="smallText"><?php echo TEXT_DELETE_INTRO . ' - <b>'  . $tInfo->template_name;?></b></td></tr>
	<tr><td class="smallText"><?php echo tep_draw_checkbox_field('delete_image', '1', true) . ' ' . TEXT_DELETE_IMAGE;?></td></tr>
	<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td></tr>
	</table>
<?php }
function delete_template($cID,$delete_image)
{
      if ($delete_image == 'on') {
       	  $theme_query = tep_db_query("select template_image from " . TABLE_TEMPLATE . " where template_id = '" . (int)$cID . "'");
          $theme = tep_db_fetch_array($theme_query);
          $image_location = DIR_FS_DOCUMENT_ROOT . DIR_WS_CATALOG_IMAGES . $theme['template_image'];
          if (file_exists($image_location)) @unlink($image_location);
      }
      tep_db_query("delete from " . TABLE_TEMPLATE . " where template_id = '" . (int)$cID . "'");
      tep_db_query("delete from " . TABLE_INFOBOX_CONFIGURATION . " where template_id = '" . (int)$cID . "'");
}
function update_template($cID)
{
	global $server_date,$HTTP_POST_VARS,$default_template;
        $template_name = tep_db_prepare_input($HTTP_POST_VARS['template_name']);
		$template_cellpadding_main = tep_db_prepare_input($HTTP_POST_VARS['template_cellpadding_main']);
		$template_cellpadding_left = tep_db_prepare_input($HTTP_POST_VARS['template_cellpadding_left']);
		$template_cellpadding_right = tep_db_prepare_input($HTTP_POST_VARS['template_cellpadding_right']);
		$template_cellpadding_sub = tep_db_prepare_input($HTTP_POST_VARS['template_cellpadding_sub']);
		$box_width_left = tep_db_prepare_input($HTTP_POST_VARS['box_width_left']);
		$box_width_right = tep_db_prepare_input($HTTP_POST_VARS['box_width_right']);
		$include_module_one = tep_db_prepare_input($HTTP_POST_VARS['include_module_one']);
		$include_module_two = tep_db_prepare_input($HTTP_POST_VARS['include_module_two']);
		$include_module_three = tep_db_prepare_input($HTTP_POST_VARS['include_module_three']);
		$include_module_four = tep_db_prepare_input($HTTP_POST_VARS['include_module_four']);
		$include_module_five = tep_db_prepare_input($HTTP_POST_VARS['include_module_five']);
		$include_module_six = tep_db_prepare_input($HTTP_POST_VARS['include_module_six']);
		$edit_customer_greeting_personal = tep_db_prepare_input($HTTP_POST_VARS['edit_customer_greeting_personal']);
		$edit_customer_greeting_personal_relogon = tep_db_prepare_input($HTTP_POST_VARS['edit_customer_greeting_personal_relogon']);
		$edit_greeting_guest = tep_db_prepare_input($HTTP_POST_VARS['edit_greeting_guest']);
		$site_width = tep_db_prepare_input($HTTP_POST_VARS['site_width']);
		$sql_data_array = array('template_name' => $template_name,
								'template_cellpadding_main' => $template_cellpadding_main,
								'template_cellpadding_left' => $template_cellpadding_left,
								'template_cellpadding_right' => $template_cellpadding_right,
								'template_cellpadding_sub' => $template_cellpadding_sub,
								'box_width_left' => $box_width_left,
								'box_width_right' => $box_width_right,
								'module_one' => $include_module_one,
								'module_two' => $include_module_two,
								'module_three' => $include_module_three,
								'module_four' => $include_module_four,
								'module_five' => $include_module_five,
								'module_six' => $include_module_six,
								'edit_customer_greeting_personal' => $edit_customer_greeting_personal,
								'edit_customer_greeting_personal_relogon' => $edit_customer_greeting_personal_relogon,
								'edit_greeting_guest' => $edit_greeting_guest,
								'site_width' => $site_width);
	       $update_sql_data = array('last_modified' => $server_date);
           $sql_data_array = array_merge($sql_data_array, $update_sql_data);
           tep_db_perform(TABLE_TEMPLATE, $sql_data_array, 'update', "template_id = '" . (int)$cID . "'");
	       if ($HTTP_POST_VARS['default'] == 'on') {
    		    tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $template_name . "' where configuration_key = 'DEFAULT_TEMPLATE'");
				tep_db_query("update " . TABLE_TEMPLATE . " set active = '1', last_modified='". $server_date ."' where template_id = '" . (int)$cID . "'");
				$default_template=$template_name;
		    }
 }
function insert_template()
{
	global $server_date,$HTTP_POST_VARS;
		  $template_image = new upload('template_image');
		  $template_image->set_destination(DIR_FS_ADMIN . DIR_WS_CATALOG_IMAGES_ROOT);
		  if ($template_image->parse() && $template_image->save()) {
			   $template_image_name = $template_image->filename;
				$update_sql_data = array('template_image' => $template_image_name);
	 	} 
		$template_name=$HTTP_POST_VARS['template_name'];
		$default_template_id=isset($HTTP_POST_VARS['info_box_sql'])?$HTTP_POST_VARS['info_box_sql']:0;
		if($default_template_id>0){
			$template_sql = "select * from " . TABLE_TEMPLATE . " where template_id=" . $default_template_id;
			$template_query = tep_db_query($template_sql);
			$template_result = tep_db_fetch_array($template_query);
			$sql_data_array = array('template_name' => $template_name,
							'template_cellpadding_main' => $template_result["template_cellpadding_main"],
							'template_cellpadding_left' => $template_result["template_cellpadding_left"],
							'template_cellpadding_right' => $template_result["template_cellpadding_right"],
							'template_cellpadding_sub' => $template_result["template_cellpadding_sub"],
							'include_column_left' => $template_result["include_column_left"],
							'include_column_right' => $template_result["include_column_right"],
							'site_width' => $template_result["site_width"],
							'active'=>$template_result["active"],
							'edit_customer_greeting_personal' =>$template_result["edit_customer_greeting_personal"],
							'edit_customer_greeting_personal_relogon' =>$template_result["edit_customer_greeting_personal_relogon"],
							'edit_greeting_guest' =>$template_result["edit_greeting_guest"],	
							'date_added' => $server_date);
		}else{
			$sql_data_array = array('template_name' => $template_name,
							'template_cellpadding_main' => 3,
							'template_cellpadding_left' => 2,
							'template_cellpadding_right' => 2,
							'template_cellpadding_sub' => 2,
							'include_column_left' => 'yes',
							'include_column_right' => 'no',
							'site_width' => 796,
							'active'=>1,
							'edit_customer_greeting_personal'=>'',
							'edit_customer_greeting_personal_relogon' =>'',
							'edit_greeting_guest' =>'',
							'date_added' => $server_date);
		}

		if(isset($update_sql_data))
       		 $sql_data_array = array_merge($sql_data_array, $update_sql_data);

        tep_db_perform(TABLE_TEMPLATE, $sql_data_array);
        $cID = tep_db_insert_id();
        
		// check if the template image directory exists
		if (!file_exists(DIR_FS_TEMPLATES . "/" . $template_name)) {
    		if(@ mkdir(DIR_FS_TEMPLATES . "/" . $template_name, 0744)===false){
  		       	tep_session_register("config_error");
				tep_set_session_value("config_error",sprintf(TEMP_CREATION_FOLDER_ERROR,$template_name));
				$error=true;
			} 
		} 
		if (!file_exists(DIR_FS_TEMPLATES . "/" . $template_name . '/boxes')) {
		    if( @mkdir(DIR_FS_TEMPLATES . "/" . $template_name . '/boxes', 0744)===false){
  		       	tep_session_register("config_error");
				tep_set_session_value("config_error",sprintf(TEMP_CREATION_FOLDER_ERROR,$template_name));
				$error=true;
			} 
  		}
		if (!file_exists(DIR_FS_TEMPLATES . "/" . $template_name . '/images')) {
		if( @ mkdir(DIR_FS_TEMPLATES . "/" . $template_name . '/images', 0744)===false){
  		       	tep_session_register("config_error");
				tep_set_session_value("config_error",sprintf(TEMP_CREATION_FOLDER_ERROR,$template_name));
				$error=true;
			}
  		}
		$infobox_sql=$HTTP_POST_VARS['info_box_sql'];
		if($infobox_sql!=''){
			$sql_sql = "select * from " . TABLE_INFOBOX_CONFIGURATION . " where template_id='" . $infobox_sql . "'";
			$sql_query=tep_db_query($sql_sql);
			while($row=tep_db_fetch_array($sql_query)){
				$sql_array=array("template_id"=>$cID,
								 "infobox_file_name"=>$row['infobox_file_name'],
								 "infobox_define"=>$row['infobox_define'],
								 "infobox_display"=>$row['infobox_display'],
								 "display_in_column"=>$row['display_in_column'],
								 "location"=>$row['location'],
								 "sort_order"=>$row['sort_order'],
								 "box_heading"=>$row['box_heading'],
								 "box_template"=>$row['box_template'],
								 "box_heading_font_color"=>$row['box_heading_font_color'],
								 "date_added"=>$server_date);
				tep_db_perform(TABLE_INFOBOX_CONFIGURATION, $sql_array);
			}				
		}else{
			// template_name is the same as the template_id
			$template_sql_file = DIR_FS_TEMPLATES . "/" . $template_name . "/" . $template_name . ".sql";
			$data = "";
			if(file_exists($template_sql_file)){
				if (($fp = fopen($template_sql_file,"r"))) {
					while (!feof($fp))$data .= fgets($fp);
					$data = str_replace('#tID#', $cID, $data); 
					$data = str_replace(';', '', $data); 
					tep_db_query($data);
					tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $template_name . "' where configuration_key = 'DEFAULT_TEMPLATE'");
				}
			}
		}
		echo '{}new_template||' . $cID . "^" . $template_name . "{}";
 }

function move_pos_updown($command,$cID,$pos,$col){
	global $server_date;
	if ((int)$pos>0){
		if ($command=='ajx_move_up_template'){
			$query=tep_db_query("select infobox_id,location from ".TABLE_INFOBOX_CONFIGURATION ." where location <=" . $pos ." and template_id ='" . $cID . "' and display_in_column='" . $col . "' order by location desc limit 2");
		} else if($command=='ajx_move_down_template'){
			$query=tep_db_query("select infobox_id,location from ".TABLE_INFOBOX_CONFIGURATION ." where location >=" . $pos ." and template_id ='" . $cID . "' and display_in_column='" . $col . "' order by location limit 2");
		}
		if (tep_db_num_rows($query)>0){
			$details=array();
			$update_sql="UPDATE " .TABLE_INFOBOX_CONFIGURATION . " set location='{1}' where infobox_id='{4}';\nUPDATE " . TABLE_INFOBOX_CONFIGURATION . " set location='{3}' where infobox_id='{2}';";
			$icnt=1;
			while($result=tep_db_fetch_array($query)){
				$update_sql=str_replace("{" . $icnt . "}",$result["location"],$update_sql);
				$icnt++;
				$update_sql=str_replace("{" . $icnt . "}",$result["infobox_id"],$update_sql);
				$icnt++;
			}
			$splt_sql=split(";",$update_sql);
			tep_db_query($splt_sql[0]);
			tep_db_query($splt_sql[1]);
		}
	}
}

function move_right_left($cID,$iID,$pos)
{
      if(($pos == 'left') || ($pos== 'right') ) {
			tep_db_query("update " . TABLE_INFOBOX_CONFIGURATION . " set display_in_column = '" . $pos . "' where infobox_id = '" . (int)$iID . "'");
			tep_template_box_realign($cID,'left');
			tep_template_box_realign($cID,'right');
     }
}
function change_template_button_status($cID,$iID,$case,$flag)
{
	global $server_date;
     if(($flag == 'yes') || ($flag == 'no')) {
	        if($case != 'infobox_display'){
	            tep_db_query("update " . TABLE_TEMPLATE . " set  $case = '" . $flag . "',last_modified='". $server_date ."' where template_id = '" . (int)$cID . "'");
         	}else{
				if($flag=='no')
				    tep_db_query("update " . TABLE_INFOBOX_CONFIGURATION . " set $case = '" . $flag . "',last_modified='". $server_date ."',location=0 where infobox_id = '" . (int)$iID . "'");
				else
				    tep_db_query("update " . TABLE_INFOBOX_CONFIGURATION . " set $case = '" . $flag . "',last_modified='". $server_date ."',location=999 where infobox_id = '" . (int)$iID . "'");
			  $info_query=tep_db_query("select location,display_in_column from " . TABLE_INFOBOX_CONFIGURATION . " where infobox_id='" . (int)$iID. "'");
        	  if(tep_db_num_rows($info_query)>0) {
			    $info_result=tep_db_fetch_array($info_query);
				tep_template_box_realign($cID,$info_result["display_in_column"]);
			 }
          }
    }
}

function  change_template_status($cID,$flag)
{
	global $server_date;
	if(($flag == 0) || ($flag == 1) ) {
	  if ($cID) {
		tep_db_query("update " . TABLE_TEMPLATE . " set active = '" . !$flag . "', last_modified='". $server_date ."' where template_id = '" . (int)$cID . "'");
	  }
	}
}?>

<?php
function out_result_template_new()
{
 global $first_template_name;
	$result='<div style="margin-top:10px;padding-left:10px;vertical-align:top;width:100%;" class="openContent" ><table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr  class="openContent_top">
					<td class="main" width="20">' . tep_image(DIR_WS_IMAGES . 'icons/attributes_blue.gif') .
					'</td><td class="smalltext"><b>' . TEXT_HEADING_NEW_TEMPLATE . '</b></td>' .
					//'<td class="smalltext" align="right"><a href="javascript:do_page_fetch(\'ajx_insert\');">' . tep_image(DIR_WS_IMAGES .  'template/img_savel.gif',IMAdGE_SAVE) . '</a>&nbsp;<a href="javascript:do_new_close();">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif',IMAGE_CLOSE_EDIT) . tep_draw_separator('pixel_trans.gif','10','10') . '</a>' . 
					'<td class="smalltext" align="right"><input type="image" src="'.DIR_WS_IMAGES .  'template/img_savel.gif'.'">&nbsp;<a href="javascript:do_new_close();">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif',IMAGE_CLOSE_EDIT) . tep_draw_separator('pixel_trans.gif','10','10') . '</a>' . 
				'</tr>
			</table><br>
			' ;// tep_draw_form("config_input",FILENAME_TEMPLATE_CONFIGURATION,'command=ajx_insert','post',' enctype="multipart/form-data"') ;
	echo $result;
	echo tep_draw_hidden_field('hasImages','yes');
?>
<table border="0" cellpadding="5" cellspacing="0" width="100%" class="dataTableContent">
  <tr>
    <td width="100%" valign="top" class="smalltext" colspan="2"><b><?php echo TEXT_NEW_INTRO?></b></td>
  </tr>
  <tr><td class="smallText">
  <?php 
	if ($handle = opendir(DIR_FS_TEMPLATES)) {
		$template_sql = "select template_name from " . TABLE_TEMPLATE;
		$template_query = tep_db_query($template_sql);
		$template_array = array();
		while($template_result = tep_db_fetch_array($template_query)){
			$template_array[] = $template_result["template_name"];
		}
		/* This is the correct way to loop over the directory. */
		while (false !== ($file = readdir($handle))) { 
			if(is_dir(DIR_FS_TEMPLATES . '/' . $file) && stristr($curr_templates.".,..,content", $file) == FALSE){
				if($first_template_name=="")
				$first_template_name=$file;
				$dirs[] = $file;
				if(!in_array($file,$template_array))
				$dirs_array[] = array('id' => $file,
									'text' => $file);			
			}
		}
		closedir($handle); 
	}
      
      if(count($dirs_array) == 0){
	  	echo  "Template Name" . '&nbsp;&nbsp;' . tep_draw_input_field('template_name');
      }
      else{
        sort($dirs_array);
        echo TEXT_TEMPLATE_NAME . '&nbsp;&nbsp;' . tep_draw_pull_down_menu('template_name', $dirs_array, '', "style='width:150;' onchange=javascript:do_page_fetch('ajx_sql_file_exists',this);");
      }
    ?></td>
	  <Td class="smallText" nowrap>  
      <?php echo TEXT_TEMPLATE_IMAGE . '&nbsp;&nbsp;' . tep_draw_file_field('template_image');?>
	 </Td></tr>
	 <tr><td style="display:none" id="show_infobox_sql" colspan="2"><?php echo TEXT_INFOBOX_CONFIGURATION;?>&nbsp;<select name="info_box_sql" class="inputNormal"></select></td></tr>
	 <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td></tr>
	 </table>
	 <!--</form>-->
	 </div>
 <?php }
?>
