<?php
require("../../inc/header.inc");
require("./comm_func.php");
?>

<?php
$stepList=GetStepList($wizard_instance);
?>

<body leftmargin="15" rightmargin="10" onload="javascript:adjust();" onresize="javascript:adjust();" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<script language="javascript">
var step_index=<?php echo $stepList[StepCount];?>;

function init()
{
	for(var i=0;i<step_index;i++)
	{
		var prev_val=document.getElementById('step_related_table_'+i).value;
		getTableList(i);
		
		var sel_obj=document.getElementById('step_related_table_'+i);
		
		
		for(var j=0;j<sel_obj.length;j++)
		{
			//alert(prev_val+' '+sel_obj.options[j].value);
			if(prev_val == sel_obj.options[j].value)
			{
				sel_obj.options[j].selected=true;
				break;
			}
		}
	}
	
}

function create_new_step()
{
	document.getElementById('step_id_'+step_index).style.display='';
	document.getElementById('delete_step_id').style.display='';
	step_index++;
	document.getElementById('step_count').value++;
	if(step_index == 24)
		document.getElementById('create_step_id').style.display='none';
}

function delete_step()
{
	document.getElementById('step_id_'+(step_index-1)).style.display='none';
	document.getElementById('create_step_id').style.display='';
	step_index--;
	document.getElementById('step_count').value--;
	if(step_index == 0)
		document.getElementById('delete_step_id').style.display='none';
}

function create_wizard()
{
	if(document.getElementById('wizard_title').value.length == 0)
	{
		alert("Please input the wizard title");
		return;
	}
	var tmpi=document.getElementById('step_count').value-0;
	if(tmpi == 0)
	{
		alert("Please add one step at least");
		return;
	}
	for(i=0;i<tmpi;i++)
	{
		if(document.getElementById('step_title_'+i).value.length == 0)
		{
			alert("Please input the title of step-"+(i+1));
			return;
		}
	}
	
	document.getElementById('modify_wizard_flag').value=1;
	document.myform.submit();
}

function detele_wizard(w_instance)
{
	document.getElementById('delete_wizard_flag').value=1;
	document.myform.submit();
}

function getTableList(step_i)
{
	var sysTypeNo=document.getElementById('step_related_system_'+step_i).value;
	var fixurl="./get_table_list.php?sysTypeNo="+sysTypeNo+"&step_pos="+step_i;
	var browserr = navigator.appName;
	if(browserr == "Microsoft Internet Explorer")
	{
		request_oo = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_oo = new XMLHttpRequest();
	}
	request_oo.open("GET",fixurl,"false");

	request_oo.onreadystatechange=function()
	{ 
		getResponse(); 
	}
	request_oo.send(null);
}

function getResponse()
{
	if(request_oo.readyState == 3)
	{
		//alert("not finish");
	}
	if(request_oo.readyState == 4)
	{
		var response = request_oo.responseText;
		
		var items=response.split(',');
		var step_pos=items[0].replace(/(^\s*)|(\s*$)/g, "");
		//alert(response);
		var sel_obj=document.getElementById('step_related_table_'+step_pos);
		for(i=sel_obj.length-1;i>=0;i--)
		{
			sel_obj[i]=null;
		}
		var opt_len=0;
		for(i=1;i<items.length;i++)
		{
			var oid=items[i].replace(/(^\s*)|(\s*$)/g, "");
			i++;
			var nam=items[i].replace(/(^\s*)|(\s*$)/g, "");
			sel_obj[opt_len]=new Option(nam,oid);
			opt_len++;
		}
		
	}
}

</script>
<?php
	adjust_head();

	adjust_title_head();
  echo "<table id=\"table_up\" border=\"0\" width=\"100%\">";
	echo "<tr>";
		echo "<td width=\"50%\">";
		echo "Template > Function Wizard";
		echo "</td>";
		echo "<td width=\"50%\" align=\"right\">";
		echo "&nbsp;";
		echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td width=\"50%\">&nbsp;</td>";
		echo "<td width=\"50%\" align=right>";
		echo "&nbsp;";
		echo "<a href=\"./wizard_param_mng.php\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
		echo "</td>";
	echo "</tr>";
	echo "</table>";
	
	adjust_title_tail();
	adjust_content_head();
	echo "<br>";
	
	if(0)
		echo "wizard_instance=$wizard_instance<br>";
	
	$the_wizard=GetWizard($wizard_instance);
	
	$mibSystem=GetMibSystem();
	//echo "<pre>";
	//print_r($mibSystem);
	//echo "</pre>";
	
	$tabConf=GetTabConf($mibSystem[0][SysTypeNo]);
	$tabCount=count($tabConf);
	//echo "<pre>";
	//print_r($tabConf);
	//echo "</pre>";

	echo "<form name=\"myform\" action=\"./wizard_param_mng.php\" method=\"post\">";
	echo "<table border=0 cellspacing=0 cellpadding=0 width=100%>";
	
	//Wizard Title
	echo "<tr>";
	echo "<td width=20%>";
	echo "Function Title:";
	echo "</td>";
	echo "<td width=80%>";
	echo "<input type=edit class=text size=64 name=\"wizard_title\" value=\"{$the_wizard[WizardTitle]}\">";
	echo "</td>";
	echo "</tr>";
	
	//Wizard Description
	echo "<tr>";
	echo "<td style=\"vertical-align:top;\">";
	echo "Function Description:";
	echo "</td>";
	echo "<td>";
	echo "<textarea class=text cols=63 rows=5 name=\"wizard_desc\">";
	echo "{$the_wizard[WizardDescription]}";
	echo "</textarea>";
	echo "</td>";
	echo "</tr>";
	echo "</table>";
	
	echo "<input type=hidden name=\"step_count\" value=\"{$stepList[StepCount]}\">";
	//echo "StepCount={$stepList[StepCount]}<br>";
	echo "<input type=hidden name=\"wizard_instance\" value=\"{$wizard_instance}\">";
	
	echo "<input type=hidden name=\"create_wizard_flag\" value=0>";
	echo "<input type=hidden name=\"delete_wizard_flag\" value=0>";
	echo "<input type=hidden name=\"modify_wizard_flag\" value=0>";
	
	for($i=0;$i<$MAX_STEP_COUNT;$i++)
	{
		if($i < $stepList[StepCount])
			echo "<div id=\"step_id_$i\">";
		else
			echo "<div id=\"step_id_$i\" style=\"display:none;\">";
		echo "<hr width=68% align=left size=5>";
		
		echo "<b>Step ".($i+1)."</b><br>";
		echo "<table border=0 cellspacing=0 cellpadding=0 width=100%>";
		
		//Step Title
		echo "<tr>";
		echo "<td width=20%>";
		echo "Title:";
		echo "</td>";
		echo "<td width=80%>";
		if($i < $stepList[StepCount])
			echo "<input type=edit class=text size=64 name=\"step_title_$i\" value=\"{$stepList[$i][StepTitle]}\">";
		else
			echo "<input type=edit class=text size=64 name=\"step_title_$i\">";
		echo "</td>";
		echo "</tr>";
		
		//Step Description
		echo "<tr>";
		echo "<td style=\"vertical-align:top;\">";
		echo "Function Description:";
		echo "</td>";
		echo "<td>";
		echo "<textarea class=text cols=63 rows=5 name=\"step_desc_$i\">";
		if($i < $stepList[StepCount])
		{
			echo "{$stepList[$i][StepDesc]}";
		}
		echo "</textarea>";
		echo "</td>";
		echo "</tr>";
		
		//Related System
		echo "<tr>";
		echo "<td>";
		echo "Related System:";
		echo "</td>";
		echo "<td>";
		echo "<select name=\"step_related_system_$i\" onchange=\"javascript:getTableList('$i');\">";
		for($j=0;$j<$mibSystem[SystemCount];$j++)
		{
			if($i < $stepList[StepCount] && $stepList[$i][RelatedSystemTypeNo]==$mibSystem[$j][SysTypeNo])
				echo "<option value=\"{$mibSystem[$j][SysTypeNo]}\" selected>";
			else
				echo "<option value=\"{$mibSystem[$j][SysTypeNo]}\">";
			echo "{$mibSystem[$j][SysTypeName]}";
			echo "</option>";
		}
		echo "</select>";
		echo "</td>";
		echo "</tr>";
		
		//Related table
		echo "<tr>";
		echo "<td>";
		echo "Related Table:";
		echo "</td>";
		echo "<td>";
		echo "<select name=\"step_related_table_$i\">";
		for($j=0;$j<$tabCount;$j++)
		{
			if($i < $stepList[StepCount] && strcmp($stepList[$i][RelatedTableOID],$tabConf[$j][OID]) == 0)
				echo "<option value=\"{$tabConf[$j][OID]}\" selected>";
			else
				echo "<option value=\"{$tabConf[$j][OID]}\">";
			echo "{$tabConf[$j][Name]}";
			echo "</option>";
		}
		echo "</select>";
		echo "</td>";
		echo "</tr>";
		
		
		
		echo "</table>";
		echo "</div>";
	}
	
	//Button
	echo "<br>";
	echo "<input id=\"create_step_id\" type=button class=button value=\"Create Step\" onclick=\"javascript:create_new_step();\">";
	echo "&nbsp;";
	echo "<input id=\"delete_step_id\" type=button class=button value=\"Delete Step\" onclick=\"javascript:delete_step();\">";
	echo "&nbsp;";
	echo "<input type=button class=button value=\"Save Wizard\" onclick=\"javascript:create_wizard();\">";
	echo "&nbsp;";
	echo "<input type=button class=button value=\"Delete Wizard\" onclick=\"javascript:detele_wizard('{$wizard_instance}');\">";
	
	
	
	echo "</form>";
	adjust_content_tail();
?>

<script language="javascript">
init();
</script>

