<?php
include('../include/session.php');

//$type_1 = $_GET['type_1_text'];
//$type_2 = $_GET['type_2_text'];
//$type_3 = $_GET['type_3_text'];
//$new_qty = $_GET['qty_text'];
//$new_span = $_GET['span_text'];
//$new_press_points = $_GET['press_points_text'];
//$truss_type=$type_1.$type_2.$type_3;
//
//$table_name = $_SESSION['db_prefix']."_trusses_temp";
//$check_query = "SELECT truss_type FROM $table_name WHERE truss_type='".$truss_type."' AND user =  '".$_SESSION['username']."' ";
//$check_result = mySQL_query($check_query);
//$check_num = mysql_num_rows($check_result);
//
//if ($check_num!=0) {
//  $_SESSION['check']=1;
//}
//else {
//  $_SESSION['check']=0;
//
//}

if ($_SESSION['page_view']=='5') {
	
	
		$i=0;
		while ($i<$_SESSION['line_no_2']) {
			 
			$truss_id = $_POST['truss_id_'.$i];
			$truss_qty = $_POST['truss_qty_text_'.$i];
			$span = $_POST['span_text_'.$i];
			$press_points = $_POST['press_points_text_'.$i];
			if (isset($_POST['truss_checkbox_'.$i])) {
				   $select_it = 'yes';
			}
			else {
				 $select_it = 'no';
			}
			if ($select_it<>'add') {		
			$table_name = $_SESSION['db_prefix']."_trusses_temp";
			$check_query = "UPDATE $table_name SET select_it = '".$select_it."', qty = '".$truss_qty."', span = '".$span."', press_points = '".$press_points."' WHERE truss_id = '".$truss_id."' AND  user =  '".$_SESSION['username']."' ";
			execsql($check_query);
			}
		$i++;
		
		}

$table_name = $_SESSION['db_prefix']."_trusses_temp";
$delete_query = "DELETE FROM $table_name WHERE user =  '".$_SESSION['username']."' AND job_no = '".$_SESSION['job_no']."' AND select_it = 'add' "; 
execsql($delete_query);

$_SESSION['error2']= 2;
$_SESSION['alert_message']= '';
$line_num = $_POST['line_num'];
$j=0;

while ($j<$line_num) {
if(isset($_POST['line_checkbox'.$j]) ) {	
$type_1_add = $_POST['type_1_add_select'.$j];
$type_2_add = $_POST['type_2_add_select'.$j];
$type_3_add = $_POST['type_3_add_select'.$j];
$quantity_add = $_POST['quantity_add_text'.$j];
$span_add = $_POST['span_add_text'.$j];
$press_points_add = $_POST['press_points_add_text'.$j];
$truss_type_add=$type_1_add.$type_2_add.$type_3_add;


$table_name = $_SESSION['db_prefix']."_trusses_temp";
$check_query = "SELECT truss_type FROM $table_name WHERE truss_type = '".$truss_type_add."' AND user =  '".$_SESSION['username']." ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
if ($check_num!='0') {
	 $_SESSION['error2']='1';
	 $_SESSION['alert_message']= $_SESSION['alert_message'].'<br>'.$truss_type_add;
}
else
{
$table_name = $_SESSION['db_prefix']."_trusses_temp";
$truss_query = "INSERT INTO $table_name (job_no, truss_type, type_1, type_2, type_3, qty, span, press_points, user, select_it ) VALUES ('".$_SESSION['job_no']."', '".$truss_type_add."', '".$type_1_add."', '".$type_2_add."', '".$type_3_add."', '".$quantity_add."', '".$span_add."', '".$press_points_add."', '".$_SESSION['username']."', 'add') ";
execsql($truss_query);
}

}
$j++;

}
}


if ($_SESSION['page_view']=='6') {

$table_name = $_SESSION['db_prefix']."_trusses_temp";
$delete_query = "DELETE FROM $table_name WHERE user =  '".$_SESSION['username']."' AND job_no = '".$_SESSION['job_no']."' "; 
execsql($delete_query);

$_SESSION['error2']= 2;
$_SESSION['alert_message']= '';
$line_num = $_POST['line_num'];
$j=0;

while ($j<$line_num) {
if(isset($_POST['line_checkbox'.$j]) ) {	
$type_1_add = $_POST['type_1_add_select'.$j];
$type_2_add = $_POST['type_2_add_select'.$j];
$type_3_add = $_POST['type_3_add_select'.$j];
$quantity_add = $_POST['quantity_add_text'.$j];
$span_add = $_POST['span_add_text'.$j];
$press_points_add = $_POST['press_points_add_text'.$j];
$truss_type_add=$type_1_add.$type_2_add.$type_3_add;


$table_name = $_SESSION['db_prefix']."_trusses_temp";
$check_query = "SELECT truss_type FROM $table_name WHERE truss_type = '".$truss_type_add."' AND user =  '".$_SESSION['username']." ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
if ($check_num!='0') {
	 $_SESSION['error2']='1';
	 $_SESSION['alert_message']= $_SESSION['alert_message'].'<br>'.$truss_type_add;
}
else
{
$table_name = $_SESSION['db_prefix']."_trusses_temp";
$truss_query = "INSERT INTO $table_name (job_no, truss_type, type_1, type_2, type_3, qty, span, press_points, user ) VALUES ('".$_SESSION['job_no']."', '".$truss_type_add."', '".$type_1_add."', '".$type_2_add."', '".$type_3_add."', '".$quantity_add."', '".$span_add."', '".$press_points_add."', '".$_SESSION['username']."') ";
execsql($truss_query);
}

}
$j++;

}
}



if (isset($_POST['temp_save_button']) )  {
	  $action="../production/trussesJobWizbase.php?p=2&s=&m=";
}
else
	{ 
	$action="../production/trussesJobWizbase.php?p=3&s=";
	}

?>



<script language="javascript" type="text/javascript">
         window.location="<?php echo $action?>";
</script>