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

$email = $_GET['email_text'];
$access_id = $_GET['access_id'];
$user_name = $_GET['user_name'];

$table_name = $_SESSION['db_prefix']."_users";
$email_query = "UPDATE $table_name SET email = '".$email."' WHERE access_id = '".$access_id."' ";
execsql($email_query);

	
if ((isset($_GET['late_alert_checkbox'])) OR (isset($_GET['late_email_checkbox']))) {
	
	if (isset($_GET['late_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['late_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['late_days_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'late' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('late', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'late' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'late' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


if ((isset($_GET['absent_alert_checkbox'])) OR (isset($_GET['absent_email_checkbox']))) {
	
	if (isset($_GET['absent_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['absent_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['absent_days_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'absent' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('absent', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'absent' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'absent' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
if ((isset($_GET['leave_alert_checkbox'])) OR (isset($_GET['leave_email_checkbox']))) {
	
	if (isset($_GET['leave_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['leave_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'leave' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('leave', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'leave' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'leave' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}	
	
	
if ((isset($_GET['quotation_alert_checkbox'])) OR (isset($_GET['quotation_email_checkbox']))) {
	
	if (isset($_GET['quotation_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['quotation_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['quotation_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'quotation' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('quotation', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'quotation' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'quotation' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}

if ((isset($_GET['receive_order_alert_checkbox'])) OR (isset($_GET['receive_order_email_checkbox']))) {
	
	if (isset($_GET['receive_order_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['receive_order_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['receive_order_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'receive order' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('receive order', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'receive order' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'receive order' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
	

if ((isset($_GET['order_value_alert_checkbox'])) OR (isset($_GET['order_value_email_checkbox']))) {
	
	if (isset($_GET['order_value_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['order_value_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'order value' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('order value',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'order value' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'order value' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
if ((isset($_GET['non_stock_order_alert_checkbox'])) OR (isset($_GET['non_stock_order_email_checkbox']))) {
	
	if (isset($_GET['non_stock_order_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['non_stock_order_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'non-stock order' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('non-stock order',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'non-stock order' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'non-stock order' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
		

if ((isset($_GET['mworks_job_alert_checkbox'])) OR (isset($_GET['mworks_job_email_checkbox']))) {
	
	if (isset($_GET['mworks_job_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['mworks_job_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'new mworks job' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('new mworks job',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'new mworks job' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'new mworks job' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
if ((isset($_GET['job_inspection_alert_checkbox'])) OR (isset($_GET['job_inspection_email_checkbox']))) {
	
	if (isset($_GET['job_inspection_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['job_inspection_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'job inspected' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('job inspected',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'job inspected' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'job inspected' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
if ((isset($_GET['job_invoiced_alert_checkbox'])) OR (isset($_GET['job_invoiced_email_checkbox']))) {
	
	if (isset($_GET['job_invoiced_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['job_invoiced_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'job invoiced' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('job invoiced',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'job invoiced' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'job invoiced' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}	
	
if ((isset($_GET['job_completed_alert_checkbox'])) OR (isset($_GET['job_completed_email_checkbox']))) {
	
	if (isset($_GET['job_completed_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['job_completed_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'job completed' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('job completed',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'job completed' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'job completed' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
		

if ((isset($_GET['job_comment_alert_checkbox'])) OR (isset($_GET['job_comment_email_checkbox']))) {
	
	if (isset($_GET['job_comment_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['job_comment_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'job comment' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('job comment',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'job comment' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'job comment' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}

if ((isset($_GET['m_order_value_alert_checkbox'])) OR (isset($_GET['m_order_value_email_checkbox']))) {
	
	if (isset($_GET['m_order_value_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['m_order_value_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'm-order value' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('m-order value',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'm-order value' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'm-order value' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
if ((isset($_GET['m_order_stock_alert_checkbox'])) OR (isset($_GET['m_order_stock_email_checkbox']))) {
	
	if (isset($_GET['m_order_stock_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['m_order_stock_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'm-order stock' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('m-order stock',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'm-order stock' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'm-order stock' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}	
	
if ((isset($_GET['m_non_stock_order_alert_checkbox'])) OR (isset($_GET['m_non_stock_order_email_checkbox']))) {
	
	if (isset($_GET['m_non_stock_order_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['m_non_stock_order_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'm-non-stock order' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('m-non-stock order',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'm-non-stock order' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'm-non-stock order' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


if ((isset($_GET['m_production_alert_checkbox'])) OR (isset($_GET['m_production_email_checkbox']))) {
	
	if (isset($_GET['m_production_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['m_production_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'm-job production' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('m-job production',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'm-job production' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'm-job production' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}



if ((isset($_GET['m_job_invoiced_alert_checkbox'])) OR (isset($_GET['m_job_invoiced_email_checkbox']))) {
	
	if (isset($_GET['m_job_invoiced_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['m_job_invoiced_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'm-job invoiced' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('m-job invoiced',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'm-job invoiced' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'm-job invoiced' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}

		
if ((isset($_GET['m_job_completed_alert_checkbox'])) OR (isset($_GET['m_job_completed_email_checkbox']))) {
	
	if (isset($_GET['m_job_completed_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['m_job_completed_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'm-job completed' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('m-job completed',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'm-job completed' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'm-job completed' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}



//==================start maintenance
if ((isset($_GET['maintenance_quoted_alert_checkbox'])) OR (isset($_GET['maintenance_quoted_email_checkbox']))) {
	
	if (isset($_GET['maintenance_quoted_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['maintenance_quoted_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'maintenance quoted' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('maintenance quoted',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'maintenance quoted' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'maintenance quoted' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


if ((isset($_GET['maintenance_invoiced_alert_checkbox'])) OR (isset($_GET['maintenance_invoiced_email_checkbox']))) {
	
	if (isset($_GET['maintenance_invoiced_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['maintenance_invoiced_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'maintenance invoiced' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('maintenance invoiced',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'maintenance invoiced' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'maintenance invoiced' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}

if ((isset($_GET['maintenance_opened_alert_checkbox'])) OR (isset($_GET['maintenance_opened_email_checkbox']))) {
	
	if (isset($_GET['maintenance_opened_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['maintenance_opened_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'maintenance opened' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('maintenance opened',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'maintenance opened' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'maintenance opened' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}

if ((isset($_GET['maintenance_closed_alert_checkbox'])) OR (isset($_GET['maintenance_closed_email_checkbox']))) {
	
	if (isset($_GET['maintenance_closed_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['maintenance_closed_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'maintenance closed' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('maintenance closed',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'maintenance closed' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'maintenance closed' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


		
if ((isset($_GET['maintenance_completed_alert_checkbox'])) OR (isset($_GET['maintenance_completed_email_checkbox']))) {
	
	if (isset($_GET['maintenance_completed_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['maintenance_completed_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'maintenance completed' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('maintenance completed',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'maintenance completed' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'maintenance completed' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
if ((isset($_GET['maintenance_request_alert_checkbox'])) OR (isset($_GET['maintenance_request_email_checkbox']))) {
	
	if (isset($_GET['maintenance_request_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['maintenance_request_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'maintenance request' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('maintenance request',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'maintenance request' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'maintenance request' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}	
	

//==================end maintenance	

if ((isset($_GET['order_stock_alert_checkbox'])) OR (isset($_GET['order_stock_email_checkbox']))) {
	
	if (isset($_GET['order_stock_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['order_stock_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'order stock' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('order stock',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'order stock' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'order stock' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	

if  ( (isset($_GET['min_stock_alert_checkbox'])) OR (isset($_GET['min_stock_email_checkbox'])) ){
	
	if (isset($_GET['min_stock_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['min_stock_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	if (isset($_GET['electric_fencing_checkbox'])) { $condition_1 = 'Electric fencing';} else { $condition_1 = 'no'; }
	if (isset($_GET['fencing_checkbox'])) { $condition_2 = 'Fencing';} else { $condition_2 = 'no'; }
	if (isset($_GET['palisade_checkbox'])) { $condition_3 = 'Palisade';} else { $condition_3 = 'no'; }
	if (isset($_GET['mworks_checkbox'])) { $condition_4 = 'M-works';} else { $condition_4 = 'no'; }
	if (isset($_GET['other_checkbox'])) { $condition_5 = 'Other';} else { $condition_5 = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'min stock' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, condition_2, condition_3, condition_4, condition_5, send_alert, send_email, user_name ) VALUES ('min stock', '".$condition_1."', '".$condition_2."', '".$condition_3."', '".$condition_4."', '".$condition_5."', '".$alert."', '".$email."', '". $user_name."' )";
	echo '<br>*****'.$alert_query;
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', condition_2 = '".$condition_2."', condition_3 = '".$condition_3."', condition_4 = '".$condition_4."',condition_5 = '".$condition_5."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'min stock' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'min stock' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	

if ((isset($_GET['purchase_to_authorise_alert_checkbox'])) OR (isset($_GET['purchase_to_authorise_email_checkbox']))) {
	
	if (isset($_GET['purchase_to_authorise_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['purchase_to_authorise_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'purchase to authorise' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('purchase to authorise',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'purchase to authorise' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'purchase to authorise' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	

if ((isset($_GET['purchase_authorised_alert_checkbox'])) OR (isset($_GET['purchase_authorised_email_checkbox']))) {
	
	if (isset($_GET['purchase_authorised_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['purchase_authorised_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
		
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'purchase authorised' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('purchase authorised',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'purchase authorised' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'purchase authorised' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	

	if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	  window.location="user_list.php";
		</script>
        <?php		 
	   }
	          
       if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	     window.location="alerts.php?set=1&user_select=<?php echo $access_id?>";
	   </script>
        <?php		 
	   }
	   
?>
