<?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");



/// Main Attributes
$query_attributes="select Increment,SubGroup_Code,SubGroup_Name from subgroup where increment in (select subgroup_parent from subgroup)
and subgroup_parent is null and subgroup_type='A' or subgroup_type='Attribute' ";

$max=50;

print <<<HERE

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<SCRIPT type="text/javascript" src="chainedselects.js"></script>
<script type="text/javascript" language="javascript">

function nextpage(){

	var itemcode=document.attributes.item_code.value;
	var codelength=itemcode.length;
	
	if(codelength>20){
		alert('Item Code too long !');
	}
	else{
		document.attributes.action="List_Items_Attributes_Actions.php";
		document.attributes.submit();
	}
}

function previouspage(){
	document.attributes.action="List_Items_AttributesParents_Wizard.php";
	document.attributes.submit();
}

function set_nexttoplevel(v1){
	
	var max=document.attributes.max.value;
	var num=document.attributes.numrow.value;
	
	/// Emptying the value of next drop downs
	for(i=2;i<=max;i++){
		document.attributes["increment"+v1+'-'+i].value='';
	}
	
	/// Emptying the values of leafs
	for(i=1;i<=num;i++){
		document.attributes["leaf"+i].value='';
	}
	
	document.attributes["incrementtop"+v1+'1'].value=document.attributes["nameselecttt"+v1].value;
	document.attributes.action="List_Items_Attributes_Wizard.php";
	document.attributes.submit();
}


function set_nextlevel(v1,v2){
	var s=parseInt(v2)+parseInt(1);
	var max=document.attributes.max.value;
	var num=document.attributes.numrow.value;
	
	document.attributes.lastleaf.value='';
	
	/// Emptying the value of next drop downs
	for(i=s;i<=max;i++){
		document.attributes["increment"+v1+'-'+i].value='';
	}
	
	document.attributes["increment"+v1+'-'+v2].value=document.attributes["nameselect"+v1+'-'+v2].value;
	document.attributes.action="List_Items_Attributes_Wizard.php";
	document.attributes.submit();
}

function check_increment(num){
	x=document.attributes;
	var numberattr=x.numrow.value;
	
	
	/// Emptying the values of leafs
	for(i=1;i<=numberattr;i++){
		document.attributes["leaf"+i].value='';
	}
	
	if(document.attributes["checkboxincrement"+num].checked)
		document.attributes["topattr"+num].value=1;
	else document.attributes["topattr"+num].value=0;
	
	document.attributes.action="List_Items_Attributes_Wizard.php";
	document.attributes.submit();
	
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Choose Attribute</title>


<script type="text/javascript" src="dropdown.js"></script>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="short.css" type="text/css" />
<link rel="stylesheet" href="dropdown.css" type="text/css" />
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}
.selectstyle {font-size: 12px; font-family:arial,verdana,geneva; color:#000000; width=100px}

</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
<LINK REL="stylesheet" TYPE="text/css" MEDIA="all" HREF="tabs.css">
<script language="javascript" src="exampleconfig.js"></script>
</head>
<body >
<form name='attributes' method='post'>

<input type='hidden' name='max' value='$max'>
<input type='hidden' name='item_id' value='$item_id'>
<input type='hidden' name='editattribute' value='$editattribute'>

HERE;

$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"<table border=0 width='100%'>";
echo '<tr><td colspan="9" align="left" valign="top">';


print <<< HERE
		<tr><th height="15" colspan="9" align="left" valign="top"><span class="style3">Item Attributes </span></th></tr>
		<tr><td height="15" colspan="9" align='center'><font size='1' color='#FF0000' face='Verdana'><b>$message</b></font></td></tr>
       
HERE;

echo"<tr bgcolor='#F7F7F7' height='25'><td  align='center'>
	<span class='style11'>Code</span> &nbsp;<input type='text' name='item_codeall' value='$item_codeall' size='40' readonly></td>
	<td align='left'><span class='style11'>Description</span> &nbsp;<input type='text' name='item_descall' value='$item_descall' size='60' readonly></td></tr>";
	
echo"<tr bgcolor='#F7F7F7' height='25'><td colspan='7'><span class='style11'>Please Choose your Item's Attributes:</span></td></tr>";
echo"<tr><td  valign='top' colspan='2' bgcolor='#F7F7F7'><table border='0'><tr >";
$i=1;
$n=1;
$width=100/$numrow;
$width=$width.'%';
	
$result_attributes=mysql_query($query_attributes, $connection);
$numrow=mysql_num_rows($result_attributes);
echo"<input type='hidden' name='numrow' value='$numrow'>";
for($i=1;$i<=$numrow;$i++){
	$nametopattr='topattr'.$i;
	$topattr='topattr'.$i;
	$nameleaf='leaf'.$i;
	$leaf='leaf'.$i;

	if($n>5) {
		echo"</td></tr><tr height='30px'><td></td></tr><tr>";
		$n=1;
	}
	
//// Displaying leafs in drop downs	

		echo"<td  valign='top'><table border='0'><tr><td width=$width ></td>";
		
		$query_attrmaster="select Increment,SubGroup_Code,SubGroup_Name from subgroup where increment in (select subgroup_parent from subgroup)
		and subgroup_parent is null and (subgroup_type='A' or subgroup_type='Attribute')";
		
		/// Removing already selected attribute from current drop down
		for($inc=1;$inc<=$numrow;$inc++){
		
			$incrementtop='incrementtop'.$inc.'1';
		
			if($$incrementtop!='' && $inc!=$i)
				$query_attrmaster=$query_attrmaster." and increment<>".$$incrementtop;
		}
		$result_attrmaster=mysql_query($query_attrmaster, $connection);
		
		$nameselecttt='nameselecttt'.$i;
		$nameincrementtop='incrementtop'.$i.'1';
		$incrementtop='incrementtop'.$i.'1';
		
		echo"<input type='hidden' name='$nameincrementtop' value='".$$incrementtop."'>";
		
		echo"<tr><td><select name=$nameselecttt onchange=set_nexttoplevel($i); class='selectstyle'>";
		echo"<option value=''>&nbsp;</option>";
		while ($row_attrmaster=mysql_fetch_row($result_attrmaster)){
			if($row_attrmaster[0]==$$nameselecttt)
				echo "<option value='$row_attrmaster[0]'  selected>$row_attrmaster[2]</option>";
			else echo "<option value='$row_attrmaster[0]'>$row_attrmaster[2]</option>";
		}
		echo"</td></tr>";
		
		
		$$leaf=$$incrementtop;
		
		if($$leaf!='')
		$lastleaf=$$leaf;
		
		
		for($j=2;$j<=$max;$j++){
		
			$numrowdetail=0;
			$nameselect='nameselect'.$i.'-'.$j;
			$nameincrement='increment'.$i.'-'.$j;
			$increment='increment'.$i.'-'.$j;
			$p=$j-1;
		
			$prvincrement='increment'.$i.'-'.$p;
			if($j==2)
				$$prvincrement=$$incrementtop;
		
			echo"<input type='hidden' name='$nameincrement' value='".$$increment."'>";
			
			$query_attr="select Increment,SubGroup_Code,SubGroup_Name from subgroup where subgroup_parent='".$$prvincrement."'";
			$result_attr=mysql_query($query_attr, $connection);
			$numrowdetail='numrowdetail'.$i.'-'.$j;
			$$numrowdetail=mysql_num_rows($result_attr);
			
			if($$numrowdetail>0){
				echo"<tr><td><select name=$nameselect onchange=set_nextlevel($i,$j); class='selectstyle'>";
				echo"<option value=''>&nbsp;</option>";
				while ($row_attr=mysql_fetch_row($result_attr)){
					if($row_attr[0]==$$nameselect)
						echo "<option value='$row_attr[0]'  selected>$row_attr[2]</option>";
					else echo "<option value='$row_attr[0]'>$row_attr[2]</option>";
				}
				echo"</td></tr>";
			}
			if($$increment!='')
				$$leaf=$$increment;
		
	
		}
		echo"</table></td>";
		$n=$n+1;
	echo"<input type='hidden' name='$nameleaf' value='".$$leaf."'>";
}
echo"</tr></table></td></tr>";

//// Getting leaf name _ policy / Filling item code&desc
$item_code='';
$item_desc='';

for($i=1;$i<=$numrow;$i++){

	$leaf='leaf'.$i;
	$query_leaf="select subgroup_name
				from subgroup
				where increment='".$$leaf."'";
	//echo$query_leaf."<br>";
	$result_leaf=mysql_query($query_leaf,$connection);
	while($row_attr=mysql_fetch_row($result_leaf)){
	
		$subgroup_name=$row_attr[0];
		$query_policy="select PolicyAttribute('$subgroup_name')";
		$result_policy=mysql_query($query_policy,$connection);
		$row_policy=mysql_fetch_row($result_policy);
		$abr=$row_policy[0];
		
		if($$leaf!='' && $skip!='1'){
			$item_code=$item_code.$abr;
			$item_desc=$item_desc.''.$abr;
			$skip=1;
		}
		else if($$leaf!='' && $skip=='1'){
			$item_code=$item_code.'-'.$abr;
			$item_desc=$item_desc.' '.$abr;
		}
		
	}
	
		if($$leaf!="")
			$lastleaf=$$leaf;
}


/// Getting last leaf sequence number
	if($item_code!=''){
	
		
		$query_sequence="select subgroup_sequentialnumber from subgroup where Increment='".$lastleaf."'";
		
		$result_sequence=mysql_query($query_sequence,$connection);
		$row_seq=mysql_fetch_row($result_sequence);
		$sequence=$row_seq[0];
		$length=strlen($sequence);
		$sequence=$sequence+1;
		$sequence=str_pad($sequence, $length, '0', STR_PAD_LEFT);
		echo"<input type='hidden' name='sequence' value='$sequence'>";
		echo"<input type='hidden' name='lastleaf' value='$lastleaf'>";
		/// Concat Item Code
		$item_code=$item_code.'-'.$sequence;
	}
	
echo"</tr>";
echo"<input type='hidden' name='item_code' value='$item_code'>
	<input type='hidden' name='item_desc' value='$item_desc'>";
	
echo"<tr>
<td align='right' colspan='7'><table><tr><td>";
echo"<input type='button'  value='Cancel '"; 
echo"onclick='window.close()' $style title='Close Window'>" ;
echo"
<input type='button'  value='Finish>>>'"; 
echo"onclick='nextpage()' $style title='Next Step'>" ;
echo"</td></tr></table></td></tr></table>";
	
/// Closing window after item insert	
if($closewindow=='1'){
	echo"<script type='text/javascript'>";
	echo"	var itemid=document.attributes.item_id.value;";
	echo"	opener.document.items.mainaction.value='editmode';";
	echo"	opener.document.items.item_id.value=itemid;";
	echo"	opener.document.items.action='Form_items.php';";
	echo"	opener.document.items.submit();";
	echo"	self.close();";
	echo"</script>";
}


/// Concatening item code
	echo"<script type='text/javascript'>";
	echo"document.attributes.item_codeall.value=document.attributes.item_code.value;";
	echo"document.attributes.item_descall.value=document.attributes.item_desc.value;";
	echo"</script>";

print <<<HERE
</form>
</body>

HERE;

?>