<?php

session_start();


if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

	/// Update new sequence
	$update_sequence="update subgroup set subgroup_sequentialnumber='".$sequence."' where Increment='".$lastleaf."'";
	$result_sequence=mysql_query($update_sequence,$connection);
	$message    = mysql_error();
	

	
	/// Insert item record
	
	if($editattribute!='1'){
		$insert_item="insert into items (item_code,item_description,item_policy) values('".$item_code."','".$item_desc."','A')";
		
		$result_item=mysql_query($insert_item,$connection);
		$item_id=mysql_insert_id();
		$message    = mysql_error();
	}
	else if($editattribute=='1'){
	
		$update_item="update items set item_code='".$item_code."',
									item_description='".$item_desc."'
					where item_id='".$item_id."'";
		//echo$update_item;exit;
		$result_item=mysql_query($update_item,$connection);
		$message    = mysql_error();
		
			$delete_attr="delete from attributes where item_id='".$item_id."'";
			$result_delete=mysql_query($delete_attr,$connection);
			$message    = mysql_error();
	}


/// Insert into Attributes selected ones with internal key	
for($i=1;$i<=$numrow;$i++){
	$incrementtop='incrementtop'.$i.'1';
	$attr=$$incrementtop;
	
	//if($$incrementtop!=''){
	//	insert_attributes($$incrementtop,$item_id,$connection);
	//}
	
	for($j=2;$j<=50;$j++){
		$increment='increment'.$i.'-'.$j;
		
		if($$increment!='')
			insert_attributes($$increment,$item_id,$connection);
	}
}


header ("Location: ./List_Items_Attributes_Wizard.php?item_id=$item_id&mainaction=editmode&closewindow=1&message=".$message);

function insert_attributes($attr,$item_id,$connection){
	$query_attributes="select increment,subgroup_parent,subgroup_policy from subgroup where increment='".$attr."'";
	//echo$query_attributes."<br>";
	$result_attributes=mysql_query($query_attributes,$connection);
	$row_attr=mysql_fetch_row($result_attributes);
				
	$insert_attributes="insert into attributes (item_id,attribute_parent,attribute_child,attribute_policy)
						values('".$item_id."','".$row_attr[1]."','".$row_attr[0]."','".$row_attr[2]."')";
	echo$insert_attributes."<Br>";
	$result_insert=mysql_query($insert_attributes,$connection);	
	$message = mysql_error();
}

mysql_close($connection);

?>