<?php
/*

	Released under the GNU General Public License 
	
	This contribution by http://cartzone.co.uk Copyright (c) cartZone UK

*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	ini_set('max_execution_time', 300);
	?>
	<style type="text/css">
<!--
#content {
	width:98%;
}
.div {
	margin:0;
	width: 16px;
}
.plan_blank {    
	min-width: 16px;
	}
.plan_half {    
	min-width: 8px;
	}
.seatLink {
	display:block;
	height: 100%;
	min-width:14px;
	font-family:  Arial, Verdana, Helvetica, sans-serif;
	font-size: 9px;
	font-style: normal;
	color: #000;
	text-align:center;
	text-decoration:none;
	}
A.seatLink {
	color: #000;
	}
A.seatLink:visited {
	color: #000;
	}
A.seatLink:hover {
    background:#404040; 
	font-style: normal;
	font-family:  Arial, Verdana, Helvetica, sans-serif;
	font-size: 9px;
	color: #fff;
	}
A.seatLink:active {
	background: #ffdbec;
	color: #000;
	}	
#incart {
	font-style: normal;
	text-align:center;
	color: #000;
    text-decoration:none;
	}
.plan_black {
	background: #202020;
	border: 1px solid #666;
	background-image:url(images/disabled.gif);
	color:#FFFFFF;
	}
/*Color 1*/
.plan_red {
	background: #ffc0c0;
	border: 1px solid #666;
	color:#FFFFFF;
	}
/*Color 2*/
.plan_blue {     
	background: #c0ffff;
	border: 1px solid #666;
	}
/*Color 3*/
.plan_green {    
	background: #80ff80;
	border: 1px solid #666;
	}
/*Color 4*/
.plan_fuchsia {   
	background: #ffc0ff;
	border: 1px solid #666;	
	}
/*Color 5*/
.plan_yellow {   
	background: #ffffc0;
	border: 1px solid #666;
	}
.plan_reserved { 
	min-width: 14px;
	background: #666;
	font-family:  Arial, Verdana, Helvetica, sans-serif;
	font-size: 9px;
	color:#FFFFFF;
	font-weight: normal;
	text-align:center;
	/*padding: 1 1 1 0;*/
	border: 1px solid #666;
	}
.plan_number { 
	min-width: 18px;   
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	background-color: #F7EFEF;
	font-weight: bold;
	text-align:center;
	color: #000;
	}
.button, .button:visited, .reply a{ background:#da1d1d; display: inline-block;font: 14px Helvetica,Arial,Sans-serif; padding: 8px 14px 9px; color: #fff !important; text-decoration: none; border:none; position: relative; cursor: pointer; text-transform:uppercase; }
.button, .button:visited, .reply a{ }
.button:hover, .reply a:hover { background: #da1d1d url(images/alert-overlay.png); display: inline-block; padding: 8px 14px 9px; color: #fff !important; text-decoration: none; border:none; position: relative; cursor: pointer; text-transform:uppercase; }
.button:active, .reply a:active { top: 1px; }

.table_plan {
	margin-bottom: 20px;
	background: #c0c0c0;
	padding: 10px;
	text-align: center;
	border: solid 1px #ddd;
}
.create_plan {
	margin: 0 10px 0 10px;
	background: #ecffeb;
	padding: 0px;
	text-align: center;
	border: solid 1px #ddd;
}
.create_stage {
	margin: 0;
	background: #fff;
	padding: 0px;
	text-align: center;
	border: solid 0px #ddd;
}
.active_plan {
	margin-bottom: 20px;
	background: #fffff5;
	padding: 5px;
	text-align: center;
	border: solid 1px #ddd;
}
.menub {
	margin: 0;
	border: 1px solid #666;
	text-align: center;
}
.menulink {}
h3 a:link, h3 a:visited { font: 18px Helvetica,Arial,Sans-serif; color: #FF0000; text-decoration: underline;}
h3 a:hover {font: 18px Helvetica,Arial,Sans-serif; color: #FF0000; text-decoration: none; }
.red {
	color: #ff0000;
}
li {
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size: 9px;
}
-->
</style>

<?php 
##############################################################
//BEGIN UPLOAD CSV
##############################################################
	//we need function to get the csv
function getcsv($filename, $delim =","){
	$row = 0;
	$dump = array(); //create new array for hold the data csv
	
	$f = fopen ($filename,"r");
	$size = filesize($filename)+1;
	
	while ($data = fgetcsv($f, $size, $delim)) {
		$dump[$row] = $data; //put the data to array
		//echo $data[1]."<br>";
		$row++;
	}
	
	fclose ($f);
	
	return $dump;
}

//this function for insert data to csv
function makeINSERTS($text, $table){
	
	$insert = array(); //make array for hold data insert
	$i = 0;
	
	while (list($key, $val) = @each($text)){
	
		$insert[$i] = "INSERT INTO `".$table."` ( `products_id` , `html` , `products_quantity`, `products_name`, `products_number`, `color_code`, `products_status`,  `products_row` )
		VALUES ('";$insert[$i] .= implode("', '", $val);$insert[$i] .= "')\n";
		
		tep_db_query($insert[$i]);
		$i++;
		
	}
	
	return $insert;
}

//this verify is the file csv upload
if ($_POST["submit"]=="submit") {

	if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
	
		$file=$_FILES['userfile']['tmp_name'];
		
		$tbl = "seat_plan";
		
		$sql = "delete from $tbl";
		tep_db_query($sql);
		
		$CSVarray = getcsv($file);
		$CSVarray = makeINSERTS($CSVarray, "$tbl");
		
		echo "<h3>Upload Success!</h3>";
	} else {
		echo "error upload file";
		exit;
	}
	
}
##############################################################
//END UPLOAD CSV
##############################################################


##############################################################
//SET THE SEAT PLAN VALUES
##############################################################

  function make_field($lname, $value, $label = null){
        $html = "<input value='$value' id='tf_$lname' name='tf_$lname' type='hidden'>";
        if(!empty($label)){
            $html .= "$label: <span id='table_label_$lname'>$value</span><br>";
        }
        return $html;
    }

    function make_table_row($id, $h, $quantity, $name, $number, $color, $status, $row){
		if ($status == 0) {
		$n = "none";
		} else {
		$n = "true";
		}
		if ($status == 3) {
		$color = "";	
		}
        $html = "<td id='td_$id' align='left' style='outline: 1px solid #C0C0C0;background-color:{$color};display:$n'>";
        $html .= make_field("change_$id", 0);
        $html .= make_field("products_id_$id", $id);// "Id");        
		$html .= make_field("products_html_$id", $h); //HTML
        $html .= make_field("products_quantity_$id", $quantity);// "Quantity");
        $html .= make_field("products_name_$id", $name);// "Name");
		$html .= make_field("products_number_$id", $number);// "Number");
		$html .= make_field("color_code_$id", $color); //Color
        $html .= make_field("products_status_$id", $status);// "Status");
		$html .= make_field("products_row_$id", $row);
		
		if ($status == 5) {
		$s = 7;
		} else {
		$s = 16;
		}
		

		$html .=  '<div style="width:' . $s . 'px; height: 16px;text-align:center;">' . $number . '</div>';
		//$html .=  '&nbsp;';
        $html .=  "</td>";
        return $html;
    }



    $state = $_GET['state'];
    $state = empty($state) ? $_POST['state']: $state;

    if($state == 'NEW'){
        $row = $_GET['row_count'];
        $col = $_GET['col_count'];

		$table_html = "<table align='center' id='plan' border='0'>";
        if($row * $col == 0) {
            $table_html .= "<tr><th>empty table</th></tr>";
        }else {
            for ($i = 0; $i < $row; $i ++) {
        
				$table_html .= "<Tr>";
                for ($j = 0; $j < $col; $j ++) {
                    $id = $col * $i + $j + 1;
					//$r = "></\td></\tr><tr><td";
					$r="Y";

                    $table_html .= make_table_row($id,($id != 0 && $j == 0) ? $r : '',0,'','','',2,$i);
                }

				$table_html .= "</Tr>";
            }
        }

		 $table_html .= "</table>";
    }

    if($state == 'EDIT'){
		
		$sql = "select * from seat_plan";
		$result = tep_db_query($sql);
		
        $data = array();
        while ($row = mysqli_fetch_row($result)){
            if(count($data) == 0 || $row['7'] != count($data) - 1){
                $data[] = array();
            }
            $data[count($data) - 1][] = $row;
        }
        
		$table_html = "<table align=\"center\" id=\"plan\" border=\"0\">";
        if(count($data) == 0) {
            $table_html .= "<tr><th>empty table</th></tr>";
        }else {
            for ($i = 0; $i < count($data); $i++) {
 
				$table_html .= "<Tr>";
                for ($j = 0; $j < count($data[$i]); $j++) {
                    $id = count($data[$i]) * $i + $j - 1;
					//print_r ($i);
                    $table_html .= make_table_row($data[$i][$j]['0'], $data[$i][$j]['1'],
                    $data[$i][$j]['2'], $data[$i][$j]['3'], $data[$i][$j]['4'], $data[$i][$j]['5'], $data[$i][$j]['6'],$data[$i][$j]['7']);
                }
				$table_html .= "</Tr>";
            }
        }
		 $table_html .= "</table>";

    }
	
		//dump data to other tables
	if($state == 'REPLACE_2'){
		$id = $FREQUEST->getvalue('id','int','0');	
		
		$sql ="UPDATE seat_plan SET products_status='1' WHERE color_code!=''";
			tep_db_query($sql);
		//first we delete categories because we start over again when we REPLACE a seat plan
		$sql = sprintf("delete from " . TABLE_CATEGORIES . "");
				tep_db_query($sql);
		$sql = sprintf("delete from " . TABLE_CATEGORIES_DESCRIPTION . "");
				tep_db_query($sql);		
		//First we need to empty the existing products table
		$sql = sprintf("delete from " . TABLE_PRODUCTS . "");
				tep_db_query($sql);
		//We also want to empty the products_to_categories table
		$sql = sprintf("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . "");
				tep_db_query($sql);
		//Now we want to empty the products_description table
		$sql = sprintf("delete from " . TABLE_PRODUCTS_DESCRIPTION . "");
				tep_db_query($sql);
		//for this experiment, we are only interested in 
	//#1 creating a main (SHOW) category for osConcert SINGLE
	//#2 creating subsequent parent categories ..1 for each table row (to help categorization...editing etc)
	
		//If all the seat plan tables have been emptied, we need to at least have the main category
		$query = tep_db_query("SELECT * FROM " . TABLE_CATEGORIES . "");
		if(tep_db_num_rows($query)<1){
		$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,date_id) VALUES ('1','0','1','DATE ID')";
		}
		tep_db_query($sql);
		$query = tep_db_query("SELECT * FROM " . TABLE_CATEGORIES_DESCRIPTION . " ");
		if(tep_db_num_rows($query)<1){
		$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id,language_id,categories_name,categories_heading_title) VALUES ('1','1','SHOW 1','SHOW 1')";
		} 
		tep_db_query($sql);

		//We need to empty the categories tables but leave the main category
		$sql = sprintf("delete from " . TABLE_CATEGORIES . "  WHERE categories_id >'1'");
		tep_db_query($sql);	
		$sql = sprintf("delete from " . TABLE_CATEGORIES_DESCRIPTION . "  WHERE categories_id >'1'");
		tep_db_query($sql);
		//Graeme - kill the auto-increment back to 1
		$sql = "ALTER TABLE " . TABLE_CATEGORIES . " AUTO_INCREMENT = 1";
		tep_db_query($sql);
		
		//I will be inserting the values that I need from the newly created seat_plan into the 3 products tables
		
		//but for this experiment I need to find a way to create categories for each of the table rows created in the seat_plan with information from the seat_plan table or from the newly created products tables.
		
		// we should find out the last 'products_row' and add 1 that gives us the amount of rows we need
		// then insert these rows into the 2 categories with parent_id=1 and an incremental sort_order starting from 2
		// the categories_id should always begin with 2 and increment up for each new row regardles of the last_insert_id
		//Graeme
		
		//lets start getting the data from the seat plan
		$get_products_result = array();
		$sort_order=1;																															//set the value for sort order - =1 so that first new insert gets value of 2
		$get_products_result = tep_db_query("SELECT DISTINCT products_row  FROM seat_plan order by products_row ASC ");//grab the unique row numbers
			while($rows = mysqli_fetch_assoc($get_products_result)){																				//start a loop for each number
					$sort_order = $sort_order +1;																								//starts at 2 (1+1 from three lines above)and then increments	
					$cat=$rows["products_row"]+1;
					$categories_name = 'ROW '.$cat;
//for categories				
		$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,categories_status) VALUES ('','1','$sort_order','0')";		
				tep_db_query($sql);	
			$categories_id = mysqli_insert_id();																					//OK now we grab the insert_id from this table (aka the categories_id) and use it for the categories_description table.
		$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id, categories_name,categories_heading_title) VALUES ('$categories_id','$categories_name','$categories_name')";		
				tep_db_query($sql);	
		}
		//Then we want to insert the values from the seat plan table here too
		$sql = "INSERT INTO " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id,categories_id)SELECT sp.products_id, sp.products_row+2 FROM seat_plan sp WHERE sp.products_id >'0'";
				tep_db_query($sql);
		//Then we want to insert the values from the seat plan table
		$sql = "INSERT INTO " . TABLE_PRODUCTS . " (products_id,products_quantity,products_status,color_code)SELECT sp.products_id,sp.products_quantity,sp.products_status,sp.color_code FROM seat_plan sp WHERE sp.products_id >'0'";
				tep_db_query($sql);
		//section id and parent id at '1' denotes a single show version here
		$sql ="UPDATE " . TABLE_PRODUCTS . " SET section_id='1', parent_id='1',products_price_break='N',product_type='P',restrict_to_customers='',product_mode='P'";
			tep_db_query($sql);
		//need a value of '1' where ever client created a color cell as that becomes the fixed template guideline for reset
		$sql ="UPDATE " . TABLE_PRODUCTS . " SET products_sku='1',products_status='1' WHERE color_code!=''";
			tep_db_query($sql);
		//And we will bring the values from our seat plan table
		$sql = "INSERT INTO " . TABLE_PRODUCTS_DESCRIPTION . " (products_id,html,products_name,products_number,products_description)SELECT sp.products_id,sp.html,sp.products_name,sp.products_number,sp.products_name FROM seat_plan sp WHERE sp.products_id >'0'";
				tep_db_query($sql);

		//Need to update some essential default values for actual products/seats
		$gif = "ticket_icon.png";
		$sql = "UPDATE " . TABLE_PRODUCTS . " SET products_quantity='1', products_model='DATE ID', products_image_1='" . $gif . "' WHERE products_status='1'";
				tep_db_query($sql);
		
		$sku_query=tep_db_query("SELECT products_id from " . TABLE_PRODUCTS . " where products_sku='1'");
		while($sku_result=tep_db_fetch_array($sku_query)){
			$sku_id=$sku_result['products_id'];
			tep_db_query($sql);

		// $sql = "UPDATE " . TABLE_PRODUCTS_DESCRIPTION . " SET products_description='" . $name['products_name'] . "' WHERE products_id = '" . (int)$sku_id . "'";
			// tep_db_query($sql);
		 }

		//I want to empty the values wherever the products/seats are not products
		$query = tep_db_query("select products_id from seat_plan where products_status!='1'");
		while ($not = tep_db_fetch_array($query)){
		// I have to adjust the category id values
		$sql = "UPDATE " . TABLE_PRODUCTS_TO_CATEGORIES . " SET categories_id='0' WHERE products_id = '" . (int)$not['products_id'] . "'";
			tep_db_query($sql);
		}
				
	}//end of REPLACE		

######################################################################


// function tep_get_category_id($categories_id) 
	// {
    // global $FSESSION;

    // $the_category_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where categories_id = '" . tep_db_input($categories_id) . "'" . "");
    // $the_products_category = tep_db_fetch_array($the_category_query);

    // return $the_category['categories_id'];//46=10
	// }

if($state == 'REPLACE_3'){
	
	

		//lets find the last entry
		$sort_query = tep_db_query("select * from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$sort = tep_db_fetch_array($sort_query);
		$first_cat = $sort['categories_id'];
		$sort_order = $sort['sort_order']+1;
		$last_cat = $sort['categories_id']+1;
		//print_r($sort_order);
		
		$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,plan_id) VALUES ($last_cat,'0','$sort_order','3')";
		tep_db_query($sql);
		
		$cat_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$cat = tep_db_fetch_array($cat_query);
		$cat1=$cat['categories_id'];
		
		//$categories_id = mysqli_insert_id();
		
		$categories_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id = '0'");
		$categories = tep_db_fetch_array($categories_query);
		$last_parent = $categories['total'];

		$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id,language_id,categories_name,categories_heading_title) VALUES ((SELECT MAX(categories_id) FROM categories),'1','SHOW $last_parent','SHOW $last_parent')";
		tep_db_query($sql);
		
		$next_cat_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where categories_id=$cat1");
		while($cat2parent = mysqli_fetch_assoc($next_cat_query)){	
		$cat_parent=$cat2parent['categories_id'];
		$cat_parent2=$cat2parent['categories_id']-1;
		//print_r($cat_parent);
		}	
		$get_products_result = array();																															//set the value for sort order - =1 so that first new insert gets value of 2
		$get_products_result = tep_db_query("SELECT DISTINCT products_row FROM seat_plan order by products_row ASC ");//grab the unique row numbers
			while($rows = mysqli_fetch_assoc($get_products_result)){		
			//start a loop for each number
					$sort_order=$sort_order+1;																								//starts at 2 (1+1 from three lines above)and then increments	
					$cat=$rows["products_row"]+1;
					$categories_name = 'Row '.$cat;
					//we need to count these rows and put it to a variable so we can reference it
					$num_rows = mysqli_num_rows($get_products_result);
					$cat_rows=$num_rows+2;
					
//start categories			
			$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,categories_status,plan_id) VALUES ($sort_order,'','$sort_order','0','2')";		
					tep_db_query($sql);	
					
			$cat2_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
			$cat2 = tep_db_fetch_array($cat2_query);
			$cat22=$cat2['categories_id'];		
			//$cat_id = mysqli_insert_id();																					//OK now we grab the insert_id from this table (aka the categories_id) and use it for the categories_description table.
			$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id, categories_name,categories_heading_title) VALUES ((SELECT MAX(categories_id) FROM categories),'$categories_name','$categories_name')";		
					tep_db_query($sql);
					
			// here i have to set the $cat_parent (parent_id=$cat_parent) e.g parent_id=11, just to get it started
			$sql = "UPDATE " . TABLE_CATEGORIES . " set parent_id=$last_cat WHERE categories_id>'" .$cat_parent . "'";		
				tep_db_query($sql);	
			//A section_id = the root categories_id for each seatplan_id
			//I don't know how to do this correctly but this works
			$sql = "UPDATE " . TABLE_CATEGORIES . " set section_id=$cat_parent WHERE categories_id>'" .$cat_parent2 . "'";		
				tep_db_query($sql);
			$sql = "UPDATE " . TABLE_CATEGORIES_DESCRIPTION . " set section_id=$cat_parent WHERE categories_id>'" .$cat_parent2 . "'";		
				tep_db_query($sql);				
			
		}
		//lets find the last entry
		$check_query = tep_db_query("select * from " . TABLE_PRODUCTS_TO_CATEGORIES . " order by products_id desc limit 1");
		$check = tep_db_fetch_array($check_query);
		//$seatplan_id = $check['products_id'];
		
		#########################
		//if($check_query)>0{
		if(tep_db_num_rows($check_query)<1){
			//when starting with empty db
			$seatplan_id = 0;
		}else{
			$seatplan_id = $check['products_id'];
		}
		#########################
		//print_r($seatplan_id);
		
		//Then we want to insert the values from the seat plan table
		$sql = "INSERT INTO " . TABLE_PRODUCTS . " (products_id,products_quantity,products_status,color_code)SELECT sp.products_id+$seatplan_id,sp.products_quantity,sp.products_status,sp.color_code FROM seat_plan sp WHERE sp.products_id >'0'";
			tep_db_query($sql);

		//Then we want to insert the values from the seat plan table here too
		$sql = "INSERT INTO " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id,categories_id,section_id)SELECT sp.products_id+$seatplan_id, sp.products_row+$last_cat+1,$cat_parent FROM seat_plan sp WHERE sp.products_id >'0'";
			tep_db_query($sql);

		
		//section id and parent id at '2' denotes a multi show version here
		$sql ="UPDATE " . TABLE_PRODUCTS . " SET section_id='$last_cat',parent_id='$last_cat',products_price_break='N',product_type='P',products_sku=0,restrict_to_customers='',product_mode='P' WHERE products_id>$seatplan_id";
		tep_db_query($sql);
		
		
		//need a value of '1' where ever client created a color cell as that becomes the fixed template guideline for reset
		$sql ="UPDATE " . TABLE_PRODUCTS . " SET products_sku='1',products_status='1' WHERE color_code!=NULL";
			tep_db_query($sql);
		//I want to empty the values wherever the products/seats are not products
		$query = tep_db_query("select * from " . TABLE_PRODUCTS . " where products_status!='1' and products_id >'0'");
		while ($not = tep_db_fetch_array($query)){
		//$section_id=$not['section_id'];	
		 //I have to adjust the category id values
		$sql = "UPDATE " . TABLE_PRODUCTS_TO_CATEGORIES . " SET categories_id='0' WHERE products_id = '" . (int)$not['products_id'] . "'";
			tep_db_query($sql);
		}
		//And we will bring the values from our seat plan table
		$sql = "INSERT INTO " . TABLE_PRODUCTS_DESCRIPTION . " (products_id,html,products_name,products_number,products_description,section_id)SELECT sp.products_id+$seatplan_id,sp.html,sp.products_name,sp.products_number,sp.products_name,$cat_parent FROM seat_plan sp WHERE sp.products_id >'0'";
				tep_db_query($sql);
		//Need to update some essential default values for actual products/seats
		$gif = "ticket_icon.png";
		$sql = "UPDATE " . TABLE_PRODUCTS . " SET products_quantity='1', products_model='', products_sku='1', products_image_1='" . $gif . "' WHERE products_status='1'";
			tep_db_query($sql);
		
				
		}//end of REPLACE		


#######################################################################

######################################################################

if($state == 'REPLACE_4'){

//lets find the last entry
//lets find the last entry
		$sort_query = tep_db_query("select * from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$sort = tep_db_fetch_array($sort_query);
		$first_cat = $sort['categories_id'];
		$sort_order = $sort['sort_order']+1;
		$last_cat = $sort['categories_id']+1;
		//print_r($sort_order);
		
		$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,plan_id) VALUES ($last_cat,'0','$sort_order','4')";
		tep_db_query($sql);
		
		$cat_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$cat = tep_db_fetch_array($cat_query);
		$cat1=$cat['categories_id'];
		
		//$categories_id = mysqli_insert_id();
		
		$categories_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id = '0'");
		$categories = tep_db_fetch_array($categories_query);
		$last_parent = $categories['total'];

		$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id,language_id,categories_name,categories_heading_title) VALUES ((SELECT MAX(categories_id) FROM categories),'1','SHOW $last_parent','SHOW $last_parent')";
		tep_db_query($sql);
		
		$next_cat_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where categories_id=$cat1");
		while($cat2parent = mysqli_fetch_assoc($next_cat_query)){	
		$cat_parent=$cat2parent['categories_id'];
		//print_r($cat_parent);
		}	

}
######################################################################

######################################################################

if($state == 'REPLACE_5'){

//lets find the last entry
		$sort_query = tep_db_query("select * from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$sort = tep_db_fetch_array($sort_query);
		$sort_order = $sort['sort_order']+1;
		$last_cat = $sort['categories_id']+1;
		$next_parent = $sort['categories_id'];
		//print_r($sort_order);
		
		$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,plan_id) VALUES ('$last_cat','$next_parent','$sort_order','2')";
		tep_db_query($sql);
		$categories_id = mysqli_insert_id();
		
		$categories_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id = '0'");
		$categories = tep_db_fetch_array($categories_query);
		$last_parent = $categories['total'];

		$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id,language_id,categories_name,categories_heading_title) VALUES ('$categories_id','1','DAY $last_parent','DAY $last_parent')";
		tep_db_query($sql);
		
		$cat_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where categories_id=$categories_id");
		while($cat2parent = mysqli_fetch_assoc($cat_query)){	
		$cat_parent=$cat2parent['categories_id'];
		//print_r($cat_parent);
		}	

}
######################################################################
//SET PLAN
######################################################################

if($state == 'REPLACE_6'){

//lets find the last entry
		$sort_query = tep_db_query("select * from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$sort = tep_db_fetch_array($sort_query);
		$sort_order = $sort['sort_order']+1;
		$last_cat = $sort['categories_id']+1;
		$next_parent = $sort['categories_id'];
		//print_r($sort_order);
		
		$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,plan_id) VALUES ('$last_cat','$next_parent','$sort_order','3')";
		tep_db_query($sql);
		$cat_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$cat = tep_db_fetch_array($cat_query);
		$cat1=$cat['categories_id'];
		
		//$categories_id = mysqli_insert_id();
		
		$categories_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id = '0'");
		$categories = tep_db_fetch_array($categories_query);
		$last_parent = $categories['total'];

		$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id,language_id,categories_name,categories_heading_title) VALUES ('$cat1','1','PLAN $last_parent','PLAN $last_parent')";
		tep_db_query($sql);
		
		$cat_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where categories_id=$cat1");
		while($cat2parent = mysqli_fetch_assoc($cat_query)){	
		$cat_parent=$cat2parent['categories_id'];
		//print_r($cat_parent);
		}	
$get_products_result = array();																															//set the value for sort order - =1 so that first new insert gets value of 2
		$get_products_result = tep_db_query("SELECT DISTINCT products_row FROM seat_plan order by products_row ASC ");//grab the unique row numbers
			while($rows = mysqli_fetch_assoc($get_products_result)){																				//start a loop for each number
					$sort_order=$sort_order+1;																								//starts at 2 (1+1 from three lines above)and then increments	
					$cat=$rows["products_row"]+1;
					$categories_name = 'ROW '.$cat;
					//we need to count these rows and put it to a variable so we can reference it
					$num_rows = mysqli_num_rows($get_products_result);
					$cat_rows=$num_rows+2;
					
//start categories		
			$last_category_query = tep_db_query("select * from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$last_category = tep_db_fetch_array($last_category_query);
		//$sort_order = $last_cat['sort_order']+1;
		//$last_cat = $last_category['categories_id']+1;
		$next_cat = $last_category['categories_id']+1;

	
			$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,categories_status,plan_id) VALUES ('$next_cat','','$sort_order','0','2')";		
					tep_db_query($sql);	
			$cat2_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
			$cat2 = tep_db_fetch_array($cat2_query);
			$cat22=$cat2['categories_id'];		
			//$cat_id = mysqli_insert_id();																					//OK now we grab the insert_id from this table (aka the categories_id) and use it for the categories_description table.
			$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id, categories_name,categories_heading_title) VALUES ('$cat22','$categories_name','$categories_name')";		
					tep_db_query($sql);
			$sql = "UPDATE " . TABLE_CATEGORIES . " set parent_id=$cat_parent WHERE categories_id>'" .$cat_parent . "'";		
				tep_db_query($sql);		
		}
		//lets find the last entry
		$check_query = tep_db_query("select * from " . TABLE_PRODUCTS_TO_CATEGORIES . " order by products_id desc limit 1");
		$check = tep_db_fetch_array($check_query);
		if(tep_db_num_rows($check_query)>0){
		$seatplan_id = $check['products_id'];
		}else{
		$seatplan_id =0;
		}
		//print_r($seatplan_id);

		//Then we want to insert the values from the seat plan table here too
		$sql = "INSERT INTO " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id,categories_id)SELECT sp.products_id+$seatplan_id, sp.products_row+$last_cat+1 FROM seat_plan sp WHERE sp.products_id >'0'";
			tep_db_query($sql);

		//Then we want to insert the values from the seat plan table
		$sql = "INSERT INTO " . TABLE_PRODUCTS . " (products_id,products_quantity,products_status,color_code)SELECT sp.products_id+$seatplan_id,sp.products_quantity,sp.products_status,sp.color_code FROM seat_plan sp WHERE sp.products_id >'0'";
			tep_db_query($sql);
		
		//section id and parent id at '2' denotes a multi show version here
		$sql ="UPDATE " . TABLE_PRODUCTS . " SET section_id='$last_cat',parent_id='$last_cat',products_price_break='N',product_type='P',restrict_to_customers='',product_mode='' WHERE products_id>$seatplan_id";
			tep_db_query($sql);
		//need a value of '1' where ever client created a color cell as that becomes the fixed template guideline for reset
		$sql ="UPDATE " . TABLE_PRODUCTS . " SET products_sku='1',products_status='1' WHERE color_code!=NULL";
			tep_db_query($sql);
		//I want to empty the values wherever the products/seats are not products
		$query = tep_db_query("select * from " . TABLE_PRODUCTS . " where products_status!='1' and products_id >'".$seatplan_id."'");
		while ($not = tep_db_fetch_array($query)){
		 //I have to adjust the category id values
		$sql = "UPDATE " . TABLE_PRODUCTS_TO_CATEGORIES . " SET categories_id='0' WHERE products_id = '" . (int)$not['products_id'] . "'";
			tep_db_query($sql);
		}
		//And we will bring the values from our seat plan table
		$sql = "INSERT INTO " . TABLE_PRODUCTS_DESCRIPTION . " (products_id,html,products_name,products_number,products_description)SELECT sp.products_id+$seatplan_id,sp.html,sp.products_name,sp.products_number,sp.products_name FROM seat_plan sp WHERE sp.products_id >'0'";
				tep_db_query($sql);
		//Need to update some essential default values for actual products/seats
		$gif = "ticket_icon.gif";
		$sql = "UPDATE " . TABLE_PRODUCTS . " SET products_quantity='1', products_model='DATE ID', products_sku='1', products_image_1='" . $gif . "' WHERE products_status='1'";
			tep_db_query($sql);
}
######################################################################
//ADD MORE PLAN
######################################################################

if($state == 'REPLACE_7'){

//lets find the last entry
		$sort_query = tep_db_query("select * from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$sort = tep_db_fetch_array($sort_query);
		$sort_order = $sort['sort_order']+1;
		$last_cat = $sort['categories_id']+1;
		$next_parent = $sort['categories_id'];
		
//lets find the last parent=0 entry
		$parent_query = tep_db_query("select * from " . TABLE_CATEGORIES . " where parent_id = '0' order by categories_id desc limit 1");
		$parent = tep_db_fetch_array($parent_query);
		//$sort_order = $sort['sort_order']+1;
		//$last_cat = $sort['categories_id']+1;
		$this_parent = $parent['categories_id'];



		
		$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,plan_id) VALUES ('$last_cat','$this_parent','$sort_order','2')";
		tep_db_query($sql);
		$categories_id = mysqli_insert_id();
		
		$categories_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id =$this_parent");
		$categories = tep_db_fetch_array($categories_query);
		$last_parent = $categories['total'];

		$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id,language_id,categories_name,categories_heading_title) VALUES ('$categories_id','1','PLAN $last_parent','PLAN $last_parent')";
		tep_db_query($sql);
		
		$cat_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where categories_id=$categories_id");
		while($cat2parent = mysqli_fetch_assoc($cat_query)){	
		$cat_parent=$cat2parent['categories_id'];
		//print_r($cat_parent);
		}	
$get_products_result = array();																															//set the value for sort order - =1 so that first new insert gets value of 2
		$get_products_result = tep_db_query("SELECT DISTINCT products_row FROM seat_plan order by products_row ASC ");//grab the unique row numbers
			while($rows = mysqli_fetch_assoc($get_products_result)){																				//start a loop for each number
					$sort_order=$sort_order+1;																								//starts at 2 (1+1 from three lines above)and then increments	
					$cat=$rows["products_row"]+1;
					$categories_name = 'ROW '.$cat;
					//we need to count these rows and put it to a variable so we can reference it
					$num_rows = mysqli_num_rows($get_products_result);
					$cat_rows=$num_rows+2;
					
//start categories		
			$last_category_query = tep_db_query("select * from " . TABLE_CATEGORIES . " order by categories_id desc limit 1");
		$last_category = tep_db_fetch_array($last_category_query);
		//$sort_order = $last_cat['sort_order']+1;
		//$last_cat = $last_category['categories_id']+1;
		$next_cat = $last_category['categories_id']+1;

	
			$sql = "INSERT INTO " . TABLE_CATEGORIES . " (categories_id,parent_id,sort_order,categories_status,plan_id) VALUES ('$next_cat','','$sort_order','0','2')";		
					tep_db_query($sql);	
			$cat_id = mysqli_insert_id();																					//OK now we grab the insert_id from this table (aka the categories_id) and use it for the categories_description table.
			$sql = "INSERT INTO " . TABLE_CATEGORIES_DESCRIPTION . " (categories_id, categories_name, categories_heading_title) VALUES ('$cat_id','$categories_name','$categories_name')";		
					tep_db_query($sql);
			$sql = "UPDATE " . TABLE_CATEGORIES . " set parent_id=$cat_parent WHERE categories_id>'" .$cat_parent . "'";		
				tep_db_query($sql);		
		}
		//lets find the last entry
		$check_query = tep_db_query("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " order by products_id desc limit 1");
		$check = tep_db_fetch_array($check_query);
		if(tep_db_num_rows($check_query)>0){
		$seatplan_id = $check['products_id'];
		}else{
		$seatplan_id =0;
		}
		//print_r($seatplan_id);

		//Then we want to insert the values from the seat plan table here too
		$sql = "INSERT INTO " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id,categories_id)SELECT sp.products_id+$seatplan_id, sp.products_row+$last_cat+1 FROM seat_plan sp WHERE sp.products_id >'0'";
			tep_db_query($sql);

		//Then we want to insert the values from the seat plan table
		$sql = "INSERT INTO " . TABLE_PRODUCTS . " (products_id,products_quantity,products_status,color_code)SELECT sp.products_id+$seatplan_id,sp.products_quantity,sp.products_status,sp.color_code FROM seat_plan sp WHERE sp.products_id >'0'";
			tep_db_query($sql);
		
		//section id and parent id at '2' denotes a multi show version here
		$sql ="UPDATE " . TABLE_PRODUCTS . " SET section_id='$last_cat',parent_id='$last_cat',products_price_break='N',product_type='P',restrict_to_customers='',product_mode='' WHERE products_id>$seatplan_id";
			tep_db_query($sql);
		//need a value of '1' where ever client created a color cell as that becomes the fixed template guideline for reset
		$sql ="UPDATE " . TABLE_PRODUCTS . " SET products_sku='1',products_status='1' WHERE color_code!=NULL";
			tep_db_query($sql);
		//I want to empty the values wherever the products/seats are not products
		$query = tep_db_query("select * from " . TABLE_PRODUCTS . " where products_status!='1' and products_id >'".$seatplan_id."'");
		while ($not = tep_db_fetch_array($query)){
		 //I have to adjust the category id values
		$sql = "UPDATE " . TABLE_PRODUCTS_TO_CATEGORIES . " SET categories_id='0' WHERE products_id = '" . (int)$not['products_id'] . "'";
			tep_db_query($sql);
		}
		//And we will bring the values from our seat plan table
		$sql = "INSERT INTO " . TABLE_PRODUCTS_DESCRIPTION . " (products_id,html,products_name,products_number)SELECT sp.products_id+$seatplan_id,sp.html,sp.products_name,sp.products_number FROM seat_plan sp WHERE sp.products_id >'0'";
				tep_db_query($sql);
		//Need to update some essential default values for actual products/seats
		$gif = "ticket_icon.gif";
		$sql = "UPDATE " . TABLE_PRODUCTS . " SET products_quantity='1', products_model='DATE ID', products_sku='1', products_image_1='" . $gif . "' WHERE products_status='1'";
			tep_db_query($sql);
}
######################################################################

    if(!empty($_POST['post']) && ($state == 'SAVE' || $state == 'UPDATE')){

        if($state == 'SAVE'){
     
			$sql = sprintf("delete from seat_plan");
			tep_db_query($sql);		
        }

        foreach ($_POST as $key => $value) {
            if(strpos($key, 'tf_p_id_') === 0){
                $kk = str_replace('tf_p_id_', '', $key);//table_field_products_id
                $id = $value;
                $field_html = $_POST["tf_p_h_$kk"];//table_field_products_html
                $field_quantity = $_POST["tf_p_q_$kk"];//table_field_products_quantity
				$field_name = $_POST["tf_p_p_$kk"];//table_field_products_name
				$field_number = $_POST["tf_p_n_$kk"];//table_field_products_number
				$field_color = $_POST["tf_c_$kk"];//table_field_products_color-code
				$field_status = $_POST["tf_p_s_$kk"];//table_field_products_status
                $field_row = $_POST["tf_p_r_$kk"];//table_field_products_row
                if($state == 'SAVE'){
                    $query = "INSERT INTO seat_plan (products_id, html,  " .
                            "products_quantity, products_name, products_number, color_code, products_status, products_row) " .
                            "VALUES ($id, '{$field_html}','{$field_quantity}', " .
                            "'{$field_name}','{$field_number}','{$field_color}','{$field_status}', '{$field_row}');";		
                }else{
                    $query = "UPDATE seat_plan SET html='{$field_html}', " .
                            "products_quantity='{$field_quantity}', " .
							"products_name='{$field_name}', " .
							"products_number='{$field_number}', " .
							"color_code='{$field_color}', " .
                            "products_status='{$field_status}', " .
                            "products_row='{$field_row}' " .
                            "where products_id=$id";
                }
			   
				tep_db_query($query);
				$seat_plan_id = mysqli_insert_id();	
            }
        }
    }

	?>


    <div id="content">
	<div class="menub">
        <p><a class="button" href="myconcert_update.php?state=CREATE">Create table</a>&nbsp;
        <!--<a href="index.php?state=VIEW">Print database table</a>&nbsp;-->
        <a class="button" href="myconcert_update.php?state=EDIT">Load table</a>&nbsp;
      <!--  <a href="index.php?state=UNHIDE">Unhide</a>&nbsp;-->
      <a class="button" href="myconcert_update.php?state=REPLACE_1">Confirm</a>&nbsp;
      <a class="button" href="myconcert_update.php?state=ACTIVE">View Active</a>&nbsp;
      <a class="button" href="myconcert_update.php?state=UPLOAD">Import</a>&nbsp;
        <a class="button" href="export.php">Export</a>&nbsp;
        <a class="button" href="myconcert_update.php?state=REPLACE_3">Add More</a></p>
        <a class="button" href="myconcert_update.php?state=REPLACE_4">Add SHOW</a></p>
        <a class="button" href="myconcert_update.php?state=REPLACE_5">Add DAY</a></p>
        <a class="button" href="myconcert_update.php?state=REPLACE_6">Add PLAN</a></p>
        <a class="button" href="myconcert_update.php?state=REPLACE_7">Add MORE</a></p>
          <div style="display:<?php if($state == 'NEW' || $state == 'EDIT' || $state == 'REPLACE_1' || $state == 'REPLACE_2' || $state == 'ACTIVE' || $state == 'UPLOAD' || $state == 'SAVE' || $state == 'UPDATE' ): echo "none" ?><?php endif; ?>">
      <h2>How to make your seat plan.</h2>
      <div align="center"><div style="width: 700px;text-align:left">
  <ol>
            <li>Create your table template. Make a grid between 40 columns x 40 rows only and SAVE. This will not be live but 'work in progress'</li>
            <li>SAVE your creation. The Save button is below your seat plan that you are working on.</li>
            <li>Single CLICK on the table cells to enter information</li>
            <li>You will see a POP UP BOX</li>
            <li>Name: write the seat name eg. Seat A1</li>
            <li>Number: The number that will show in the seat plan cell. eg 1</li>
            <li>Status: IMPORTANT Set status = 1 if the cell is a seat Set Status = 4 if the seat is a Row Number</li>
            <li>Always SAVE then LOAD to view progress</li>
            <li>Double CLICK to add cell colors SAVE and LOAD TABLE</li>
            <li>When you are happy CONFIRM and load to the Front End</li>
            <li>Import and export the seat_plan.csv for offsite editing to make it easy.</li>
            <li>Finally take the finished exported file to your own osConcert SINGLE and IMPORT the same way.<br />
            </li>
  </ol></div></div>
      </div>
      
          <?php if($state == 'NEW' || $state == 'EDIT'):
        echo "<div style=\"\">
          <h3>This is your seat plan (seat_plan.csv) BEFORE you make it ACTIVE.<br />
If you are happy with it, you can <a class=\"menulink\" href=\"myconcert_update.php?state=REPLACE_1\">Confirm</a> and activate your new creation to the front end</h3></div>"
		  ?><?php endif; ?>
 <?php if($state == 'REPLACE_2'): 
            echo "<div style=\"\">
          <h3>NOW View Your The Seat Plan You Just Created ' <a href=\"myconcert_update.php?state=ACTIVE\">View Active</a>&nbsp' Seat Plan</h3></div>"
		  ?><?php endif; ?>
          <?php if($state == 'ACTIVE'): 
     echo "<h3 align=\"center\">This is the ACTIVE seat plan that will be shown in the ' <a href=\"../index.php?cPath=1\" target=\"_blank\">Front End</a>&nbsp' now!<br /><a class=\"menulink\" href=\"export.php\">Export</a> this to your osConcert SINGLE</h3>"
		  ?><?php endif; ?>
         <?php if($state == 'SAVE' || $state == 'UPDATE'):
          echo "<div style=\"\"><h3>Now click <a class=\"menulink\" href=\"myconcert_update.php?state=EDIT\">Load Table</a></h3></div>"
		  ?><?php endif; ?>
	</div>
        <!--create stage-->
         <?php if($state == 'EDIT'): ?>
        <div align="center" style="margin:auto; width:600px; border: black 0px solid; height:90px"><div class="MainPhoto">
        <img src="images/stage/main/stage_1.png" alt="Stage 1"/>
    </div></div><?php endif; ?>
        
	  <div id="content">
        <?php if($state == 'NEW' || $state == 'EDIT'): ?>
            <form id="form_table" action="myconcert_update.php" method="POST">
                <input name="post" value="post" type="hidden">                               
                <input name="state" value="<?php echo ($state == 'NEW' ? 'SAVE' : 'UPDATE') ?>" type="hidden">
                <div class="table_plan">
                    <?php echo $table_html; ?><br>
                    <input value="Save" type="submit">
                </div>
            </form>
        <?php endif; ?>

        </div>
        
        <!--create table-->
                  <?php if($state == 'CREATE'): ?>
            <div>
                <form action="myconcert_update.php" method="get" id="form_create" onsubmit="javascript: if(!checkValues(event)) return false;">
                    <script type="text/javascript">
                    	function checkValue(event,obj)
                    	{
							if(event!=null)
							{
								var keynum;
								if(window.event) // IE
								{
									keynum = event.keyCode
								}
								else if(event.which) // Netscape/Firefox/Opera
								{
									keynum = event.which
								}
								if(keynum==8 || keynum==46)
								{
									return;
								}
								
								if(keynum!=13)
								{
									var keychar = String.fromCharCode(keynum)
									var numcheck = /\d/
									if(!numcheck.test(keychar))
									{
										obj.value=obj.value.substr(0,obj.value.length-1);
										return false;
									}
								}
							}
							var minVal=parseInt(obj.getAttribute("min"));
							var maxVal=parseInt(obj.getAttribute("max"));
							var toAlert=false;
							if(!isNaN(parseInt(obj.value)))
							{
								if(parseInt(obj.value)<0)
								{
									obj.value=Math.abs(obj.value);
								}
								
								if(parseInt(obj.value)< minVal || parseInt(obj.value)>maxVal)
								{
									toAlert=true;
								}
							}
							else
							{
								toAlert=true;
							}

							if(toAlert==true)
							{
									alert("The value must be an integer number between "+minVal+" and "+maxVal+"!\r\nPlease change accordingly.");
									obj.select();
							}
							document.forms["form_create"].submit.disabled=(toAlert);
							return !toAlert;
						}
						function checkValues(event)
						{
							var rows=checkValue(null,document.getElementById('row_field'));
							var cols=checkValue(null,document.getElementById('col_field'));
							if(rows==false || cols==false || (rows==false && cols==false))
							{
								event.cancelBubble=true;
								event.stopPropagation=true;
								return false;
							}
							else
							{
								return true;
							}
						}
                    </script>
            <div class="create_plan">
                <form action="myconcert_update.php" method="get">
                  <input name="state" value="NEW" type="hidden" />
              <table border="0" align="center">
                            <td align="left">Column count:</td>
                      <td align="left">
                    <input id="col_field" name="col_count" size="10"type="text" max="40" min="1" onkeyup="checkValue(event,this)">
                            </td>
                      </tr>
                        <tr>
                        <tr>
                            <td align="left">Row count:</td>
                      <td align="left">
                    <input id="row_field" name="row_count" size="10"type="text" max="40" min="1" onkeyup="checkValue(event,this)">
                            </td>
                      </tr>
                        <tr>
                            <td colspan="2" align="center"><br>
                                <input value="Create Table Template" type="submit">
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
        <?php endif; ?>
<!--creat table end -->
        
          <!--dump table-->
         <?php if($state == 'REPLACE_1'): ?>
                <form action="myconcert_update.php?state=REPLACE_2" method="post">
                    <input name="replace2" value="REPLACE_2" type="hidden">
                    <table align="center" border="0">
                        <tr>
                            <td colspan="2" align="center"><br>
                                <input value="Confirm and REPLACE Seat Plan" type="submit">
                            </td>
                        </tr>
                    </table>
                </form>
            <?php endif; ?>
        <!--dump table end -->
     <?php if($state == 'UPLOAD'): ?>
    <form enctype="multipart/form-data" method="post">
        <h2 align="center">Upload "SEAT PLAN" File (must be the same as exported):</h2> 
            <div align="center">
              <INPUT NAME="userfile" TYPE="file">
              <INPUT TYPE="submit" VALUE="submit" name="submit"> 
              <p>seat_plan.csv</p>
            </div>
    </form><?php endif; ?>
<!--show active seat plan from database-->
    <div id="content">
    <?php if($state == 'ACTIVE'): 
		  ?>
      <div class="active_plan">
<table border="0" cellpadding="0" cellspacing="1" align="center">
 <tr>    
 <!-- seat plan //-->
    <?php
$plan_query = tep_db_query("select distinct p.products_status, p.color_code, p.products_id, pd.products_name, pd.products_number, pd.html, p.parent_id from " . TABLE_PRODUCTS . " p join ".TABLE_PRODUCTS_TO_CATEGORIES . " p2c on p2c.products_id=p.products_id join ".TABLE_PRODUCTS_DESCRIPTION . " pd on pd.products_id=p.products_id where p.products_id >'0' and p.parent_id = '1' order by p.products_id asc");
	 while ($plan = tep_db_fetch_array($plan_query)) {

	 $pid = $plan['products_id'];
	 $html = $plan['html'];
	 $status = $plan['products_status'];
	 $number = $plan['products_number'];
	 $color = $plan['color_code'];
	 $pname = $plan['products_name'];
	 $h = "></td></tr><tr><td";
 ?><td<?php 
 if ($html=="Y"){
 echo $h;
 } ?> id="<?php ?>" class="plan_<?php
 if($status == 0) echo "reserved";
 elseif($status == 2) echo "blank";
 elseif($status == 5) echo "half";
 elseif($status == 4) echo "number";
 elseif($status == 1) echo $color;
?>">
 <?php
 if($status == 0) echo '<div class="div">' . $number . '';
 elseif($status == 2) echo tep_draw_separator('pixel_trans.gif', BLANK_IMAGE_WIDTH, BLANK_IMAGE_HEIGHT);
 elseif($status == 5) echo tep_draw_separator('pixel_trans.gif', HALF_BLANK_IMAGE_WIDTH, BLANK_IMAGE_HEIGHT);
 elseif($status == 4) echo '<div class="div">' . $number . '';
 else {
?>
<div class="div" id="">
<a title="<?php echo $pname; ?>" class="seatLink" href=""><?php echo $number; ?></a>
</div>
<?php
}
?></td>

 <?php }?>

 </tr>
 </table>
<br /></div><?php endif; ?></div>
<div id="editor" class="jqmWindow" style="display: none;"  align="center">
        <div class="jqDrag">Press key and drag ...</div>
        <div id="editor_field">
            <table border="0">
            	<tr>
                    <td width="75%" align="left">Name:</td>
                    <td width="25%" align="left">
                        <input id="name_field" name="name_field" size="16"type="text">
                    </td>
                </tr>
                <tr>
                    <td width="75%" align="left">Number:</td>
                    <td width="25%" align="left">
                        <input id="number_field" name="number_field" size="10"type="text">
                    </td>
                </tr>
                <tr>
                    <td width="75%" align="left">Status:</td>
                    <td width="25%" align="left">
                        <input id="status_field" name="status_field" size="10"type="text">
                    </td>
                </tr>
            </table>
        </div>

        <div id="editor_color">
            <input id="color_field" name="color_field" size="20"type="text">
            <table id="color_table" border="1" cellpadding="0" cellspacing="0">
                <tr>
                    <td align="center" bgcolor="NULL"><span class="red">X</span></td>
                    <td bgcolor="<?php echo COLOR_1; ?>">&nbsp;</td>
                    <td bgcolor="<?=COLOR_2?>">&nbsp;</td>
                </tr>
                <tr>
                    <td bgcolor="<?php echo COLOR_3; ?>">&nbsp;</td>
                    <td bgcolor="<?php echo COLOR_4; ?>">&nbsp;</td>
                    <td bgcolor="<?php echo COLOR_5; ?>">&nbsp;</td>
                </tr>
            </table>
        </div>
        <input onclick="saveEditorHandler();" value="Save" type="button">
        <input onclick="closeEditorHandler();" value="Close" type="button">
    </div>
