<?php 
/**********************************************************************
	Copyright (C) Evolve Techniologies
	Author : Chathuri Gunawardana
	Email : chathuri@evolve-sl.com
	Company : Evolve Technologies(pvt)Ltd
***********************************************************************/
$page_security = 'SA_CUSTOMER';
$path_to_root = "../..";

include($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/db/customers_db.inc");

$js = "";
if ($use_popup_windows)
	$js .= get_js_open_window(900, 500);
if ($use_date_picker)
	$js .= get_js_date_picker();

/******************************BOF Chat Added**************************************/	
//page(_($help_context = "Customers"), @$_REQUEST['popup'], false, "", $js); 
page(_($help_context = "Students"), @$_REQUEST['popup'], false, "", $js); 
/******************************EOF Chat Added**************************************/

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/banking.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/ui/contacts_view.inc");

if (isset($_GET['debtor_no'])) 
{
	$_POST['customer_id'] = $_GET['debtor_no'];
}

if(!empty($_POST['val']))
{
	$selected_id = get_post('val','');
}
else
{	
	$selected_id = get_post('customer_id','');
}


$upload_file = "";
	
if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '')
{
	$stock_id = $_POST['stdRegNo'];
	$result = $_FILES['pic']['error'];
	$upload_file = 'Yes'; //Assume all is well to start off with
	
	$filename = company_path().'/images';
	if (!file_exists($filename))
	{
		mkdir($filename);
	}	
	$filename .= "/".item_img_name2($stock_id).".jpg";
	
	//But check for the worst 
	if ((list($width, $height, $type, $attr) = getimagesize($_FILES['pic']['tmp_name'])) !== false)
		$imagetype = $type;
	else
		$imagetype = false;
	//$imagetype = exif_imagetype($_FILES['pic']['tmp_name']);
	if ($imagetype != IMAGETYPE_GIF && $imagetype != IMAGETYPE_JPEG && $imagetype != IMAGETYPE_PNG){	
		//File type Check
		display_warning( _('Only graphics files can be uploaded'));
		$upload_file ='No';
	}
	elseif (@strtoupper(substr(trim($_FILES['pic']['name']), @in_array(strlen($_FILES['pic']['name']) - 3)), array('JPG','PNG','GIF')))
	{
		display_warning(_('Only graphics files are supported - a file extension of .jpg, .png or .gif is expected'));
		$upload_file ='No';
	}
	elseif ( $_FILES['pic']['size'] > ($max_image_size * 1024))
	{ //File Size Check
		display_warning(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $max_image_size);
		$upload_file ='No';
	}
	elseif (file_exists($filename))
	{
		$result = unlink($filename);
		if (!$result)
		{
			display_error(_('The existing image could not be removed'));
			$upload_file ='No';
		}
	}
	
	if ($upload_file == 'Yes')
	{
		$result  =  move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
	}
	$Ajax->activate('details');
 /* EOF Add Image upload for New Item  - by Ori */
}


//--------------------------------------------------------------------------------------------

function can_process()
{
	if (strlen($_POST['CustName']) == 0) 
	{
		//display_error(_("The customer name cannot be empty."));
		display_error(_("Full name cannot be empty."));//*************Chat Added*********************/
		set_focus('CustName');
		return false;
	} 

	if (strlen($_POST['cust_ref']) == 0) 
	{
		//display_error(_("The customer short name cannot be empty."));
		display_error(_("Student register number cannot be empty."));//*************Chat Added*********************/
		set_focus('cust_ref');
		return false;
	} 
	if ((check_register_number($_POST['cust_ref']) == 1) && ($_POST['debtor_no'] != ''))
	{
		//display_error(_("The customer short name cannot be empty."));
		display_error(_("Register Number Dupliacate."));//*************Chat Added*********************/
		set_focus('cust_ref');
		return false;
	} 
	
	/******************************BOF Chat Added**************************************/	
	if (strlen($_POST['Nic']) == 0) 
	{
		display_error(_("NIC/Passport Number cannot be empty."));
		set_focus('Nic');
		return false;
	} 
	
	if((check_nic_number($_POST['Nic']) == 1)  && ($_POST['debtor_no'] != '')){
		display_error(_("NIC/Passport Number is Duplicate."));
		set_focus('Nic');
		return false;
	}

	if((check_uwefee_number($_POST['uweRegNo']) == 1)  && ($_POST['debtor_no'] != '')){
		display_error(_("UWE Fee Number is Duplicate."));
		set_focus('uweRegNo');
		return false;
	}

	if ($upload_file == 'No')
	{
		return false;
	}
	/*****************************EOF Chat Added**************************************/	
	
	
	
	//*************Chat Commented*********************/
	/*if (!check_num('credit_limit', 0))
	{
		display_error(_("The credit limit must be numeric and not less than zero."));
		set_focus('credit_limit');
		return false;		
	} 
	
	if (!check_num('pymt_discount', 0, 100)) 
	{
		display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
		set_focus('pymt_discount');
		return false;		
	} 
	
	if (!check_num('discount', 0, 100)) 
	{
		display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
		set_focus('discount');
		return false;		
	} */
	//*************Chat Commented*********************/
	
	return true;
}

//--------------------------------------------------------------------------------------------

function handle_submit(&$selected_id)
{
	global $path_to_root, $Ajax, $auto_create_branch, $selected_id;

	if (!can_process())
		return;
		
	if ($selected_id) 
	{
		if (check_value('del_image'))
		{
			$filename = company_path().'/images/'.item_img_name2($_POST['stdRegNo']).".jpg";
			if (file_exists($filename))
				unlink($filename);
		}
		
		//update_customer($_POST['customer_id'], $_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
		/********************************BOF Chat Added********************************/	
		update_customer($_POST['customer_id'], $_POST['CustName'], $_POST['cust_ref'], $_POST['address'],$_POST['ContactAddress'], $_POST['GuardianName'],$_POST['GuardianHPhone'],$_POST['GuardianMPhone'],$_POST['Nic'],$_POST['Dob'], $_POST['Gender'],'1',$_POST['stdRegNo'],$_POST['uweRegNo'],
		/********************************EOF Chat Added********************************/
			(!empty($_POST['tax_id']) ? $_POST['tax_id'] : ""), (!empty($_POST['curr_code']) ? $_POST['curr_code'] : "LKR"), (!empty($_POST['dimension_id']) ? $_POST['dimension_id'] : ""), (!empty($_POST['dimension2_id']) ? $_POST['dimension2_id'] : ""),
			(!empty($_POST['credit_status']) ? $_POST['credit_status'] : "1"), (!empty($_POST['payment_terms']) ? $_POST['payment_terms'] : "5"), input_num('discount') / 100, input_num('pymt_discount') / 100,
			'1000000', (!empty($_POST['sales_type']) ? $_POST['sales_type'] : "1"), (!empty($_POST['notes']) ? $_POST['notes'] : ""));
			
		/********************************BOF Chat Added********************************/
			update_crm_person_fromName($_POST['cust_ref'], $_POST['CustName'],'', $_POST['address'], $_POST['Cob'], $_POST['Nationality'], $_POST['Domicile'], $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], '', '');
		/********************************EOF Chat Added********************************/
			
		update_record_status($_POST['customer_id'], $_POST['inactive'],
			'debtors_master', 'debtor_no');

		$Ajax->activate('customer_id'); // in case of status change
		display_notification(_("Student has been updated."));
	} 
	else 
	{ 	//it is a new customer

		begin_transaction();
		//add_customer($_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
		/********************************BOF Chat Added********************************/	
		add_customer($_POST['CustName'], $_POST['cust_ref'], $_POST['address'], $_POST['ContactAddress'], $_POST['GuardianName'], $_POST['GuardianHPhone'], $_POST['GuardianMPhone'], $_POST['Nic'], $_POST['Dob'], $_POST['Gender'],'1',$_POST['stdRegNo'],$_POST['uweRegNo'],
		/********************************EOF Chat Added********************************/	
			(!empty($_POST['tax_id']) ? $_POST['tax_id'] : ""), (!empty($_POST['curr_code']) ? $_POST['curr_code'] : "LKR"), (!empty($_POST['dimension_id']) ? $_POST['dimension_id'] : ""), (!empty($_POST['dimension2_id']) ? $_POST['dimension2_id'] : ""),
			(!empty($_POST['credit_status']) ? $_POST['credit_status'] : "1"), (!empty($_POST['payment_terms']) ? $_POST['payment_terms'] : "5"), input_num('discount') / 100, input_num('pymt_discount') / 100,
			'1000000', (!empty($_POST['sales_type']) ? $_POST['sales_type'] : "1"), (!empty($_POST['notes']) ? $_POST['notes'] : ""));

		$selected_id = $_POST['customer_id'] = db_insert_id();
         
		if (isset($auto_create_branch) && $auto_create_branch == 1)
		{
        	add_branch($selected_id, $_POST['CustName'], $_POST['cust_ref'], null, null, null, null, null, null, null, null, null, null, null, 
			(!empty($_POST['br_address']) ? $_POST['br_address'] : "") , (!empty($_POST['salesman']) ? $_POST['salesman'] : "1"), (!empty($_POST['area']) ? $_POST['area'] : "2"), (!empty($_POST['tax_group_id']) ? $_POST['tax_group_id'] : "1"), (!empty($_POST['sales_account']) ? $_POST['sales_account'] : ""),
				  (!empty($_POST['sales_discount_account']) ? $_POST['sales_discount_account'] : "4510"), (!empty($_POST['receivables_account']) ? $_POST['receivables_account'] : "1300"), (!empty($_POST['payment_discount_account']) ? $_POST['payment_discount_account'] : "4500"),(!empty( $_POST['default_location']) ?  $_POST['default_location'] : "LOC")
				 , (!empty( $_POST['br_post_address']) ?  $_POST['br_post_address'] : ""), (!empty( $_POST['disable_trans']) ?  $_POST['disable_trans'] : ""), (!empty( $_POST['group_no']) ?  $_POST['group_no'] : ""),
				  (!empty( $_POST['default_ship_via']) ?  $_POST['default_ship_via'] : "1"), (!empty( $_POST['notes']) ?  $_POST['notes'] : ""));
				  
			/*$_POST['address'], $_POST['salesman'], $_POST['area'], $_POST['tax_group_id'], '',
                get_company_pref('default_sales_discount_act'), get_company_pref('debtors_act'), get_company_pref('default_prompt_payment_act'),$_POST['location'], $_POST['address'], 0, 0, $_POST['ship_via'], $_POST['notes']);*/
                
				
        	$selected_branch = db_insert_id();
	
			//add_crm_person($_POST['cust_ref'],$_POST['CustName'], '', $_POST['address'],null,null,null,$_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], '', '');
			
			/********************************BOF Chat Added********************************/	
			add_crm_person($_POST['cust_ref'],$_POST['CustName'], '', $_POST['address'], $_POST['Cob'], $_POST['Nationality'], $_POST['Domicile'], $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], '', '');
			/********************************EOF Chat Added********************************/
			
			add_crm_contact('cust_branch', 'general', $selected_branch, db_insert_id());
		}
		commit_transaction();

		display_notification(_("A new student has been added."));

		if (isset($auto_create_branch) && $auto_create_branch == 1)
			display_notification(_("A default branch has been automatically created."));
		
		$Ajax->activate('_page_body');
	}
}
//--------------------------------------------------------------------------------------------

if (isset($_POST['btnsubmit'])) 
{
	handle_submit($selected_id);
}
//-------------------------------------------------------------------------------------------- 

if (isset($_POST['delete'])) 
{
	$cancel_delete = 0;

	// PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'

	if (key_in_foreign_table($selected_id, 'debtor_trans', 'debtor_no'))
	{
		$cancel_delete = 1;
		display_error(_("This student cannot be deleted because there are transactions that refer to it."));
	} 
	else 
	{
		if (key_in_foreign_table($selected_id, 'cust_branch', 'debtor_no'))
		{
			$cancel_delete = 1;
			display_error(_("Cannot delete the student record because orders have been created against it."));
		} 
		else 
		{
			if (key_in_foreign_table($selected_id, 'cust_branch', 'debtor_no'))
			{
				$cancel_delete = 1;
				display_error(_("Cannot delete this student because there are branch records set up against it."));
				//echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
			}
		}
	}
	
	if ($cancel_delete == 0) 
	{ 	//ie not cancelled the delete as a result of above tests
	
		//$stock_id = date("Y")."_".++$selected_id;
		$filename = company_path().'/images/'.item_img_name2($_POST['stdRegNo']).".jpg";
		if (file_exists($filename))
			unlink($filename);
		
		delete_customer($selected_id);

		display_notification(_("Selected student has been deleted."));
		unset($_POST['customer_id']);
		$selected_id = '';
		$Ajax->activate('_page_body');
	} //end if Delete Customer
}

function customer_settings($selected_id) 
{
	global $SysPrefs, $path_to_root, $auto_create_branch, $pic_height;
	
	if (!$selected_id) 
	{
	 	if (list_updated('customer_id') || !isset($_POST['CustName'])) {

			/********************************BOF Chat Added********************************/
			$_POST['CustName'] = $_POST['cust_ref'] = $_POST['address'] = $_POST['ContactAddress'] 
			= $_POST['GuardianName'] = $_POST['GuardianHPhone'] = $_POST['GuardianMPhone'] = $_POST['uweRegNo'] = '';		
			
			$_POST['Cob'] = -1;
			$_POST['Nationality'] = -1;
			$_POST['Domicile'] = -1;
			
			$_POST['phone'] = '';
			$_POST['phone2']  = '';
			$_POST['email']  = '';
		
			$_POST['Nic'] = '';
			$_POST['Dob'] = '';
			$_POST['Gender'] = -1;
			
			/*$x = get_maxID();
			//$_POST['stdRegNo']
			$stdId = date("Y")."_".++$x;
			//$_SESSION['stdId'] = $stdId;
			$_POST['stdId'] = $stdId;*/
			$_POST['stdRegNo'] = '';
			/********************************EOF Chat Added********************************/	
			$_POST['tax_id']  = '';
			$_POST['dimension_id'] = 0;
			$_POST['dimension2_id'] = 0;
			$_POST['sales_type'] = -1;
			$_POST['curr_code']  = get_company_currency();
			$_POST['credit_status']  = 1;
			$_POST['payment_terms']  = $_POST['notes']  = '';

			$_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
			$_POST['credit_limit']	= price_format($SysPrefs->default_credit_limit());
		}
	}
	else 
	{
		$myrow = get_customer($selected_id);
		//print_r($myrow);
		
		$_POST['CustName'] = $myrow["name"];
		$_POST['cust_ref'] = $myrow["debtor_ref"];
		$_POST['address']  = $myrow["address"];
		
		/********************************BOF Chat Added********************************/
		
		$_POST['Cob'] = $myrow["cob"];
		$_POST['Nationality'] = $myrow["nationality"];
		$_POST['Domicile'] = $myrow["domicile"];
			
		$_POST['phone'] = $myrow["phone"];
		$_POST['phone2'] = $myrow["phone2"];
		$_POST['email'] = $myrow["email"];
		
		$_POST['ContactAddress'] = $myrow["contactAddress"];
		$_POST['GuardianName'] = $myrow["guardianName"];
		$_POST['GuardianHPhone'] = $myrow["guardianHomeNum"];
		$_POST['GuardianMPhone'] = $myrow["guardianMobileNum"];
		
		$_POST['Nic'] = $myrow["nic"];
		$_POST['Dob'] = sql2date($myrow["dob"]);
		$_POST['Gender'] = $myrow["gender"];
		//$_POST['stdId'] = $myrow["stdRegNo"];
		$_POST['stdRegNo'] = $myrow["stdRegNo"];
		$_POST['uweRegNo']= $myrow["uweRegNumber"];
		$_POST['del_image'] = 0;
		/********************************EOF Chat Added********************************/
		
		$_POST['tax_id']  = $myrow["tax_id"];
		$_POST['dimension_id']  = $myrow["dimension_id"];
		$_POST['dimension2_id']  = $myrow["dimension2_id"];
		$_POST['sales_type'] = $myrow["sales_type"];
		$_POST['curr_code']  = $myrow["curr_code"];
		$_POST['credit_status']  = $myrow["credit_status"];
		$_POST['payment_terms']  = $myrow["payment_terms"];
		$_POST['discount']  = percent_format($myrow["discount"] * 100);
		$_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
		
		//$_POST['credit_limit'] = price_format($myrow["credit_limit"]);
		$_POST['credit_limit']	= '';//*************Chat Added*********************/
		
		$_POST['notes']  = $myrow["notes"];
		$_POST['inactive'] = $myrow["inactive"];
	}

	start_outer_table(TABLESTYLE2);
	table_section(1);
	//table_section_title(_("Name and Address"));
	table_section_title(_("Personal And Contact Information"));/*************Chat Added**********************/
	
	/**************************BOF Chat Added**************************************/
	//text_row(_("val:"), 'val',$_POST['val'],40, 80);
	
	hidden('val',(!empty( $_POST['val']) ?  $_POST['val'] : ""));
	
	//text_row(_("stdId:"), 'stdId',$_POST['stdId'],40, 80);
	//hidden( 'stdId',$_POST['stdId']);
	/**************************EOF Chat Added**************************************/
	

	//text_row_label(_("Student Reg. Number:"), 'stdRegNo', $_POST['stdId'], 40, 80);/*********Chat Added**********/
	//text_row_label(_("Student Reg. Number:"), 'stdRegNo', $_SESSION['stdId'], 40, 80);/*********Chat Added**********/
	if($selected_id != ''){
		label_row(_("Student Reg.Num"),$_POST['cust_ref']);
		//label_row(_("UWE Reg. Number:"),$_POST['uweRegNo']);
		text_row(_("UWE Reg. Number:"), 'uweRegNo', $_POST['uweRegNo'], 40, 80);
		hidden('cust_ref', $_POST['cust_ref']);
		//hidden('uweRegNo', $_POST['uweRegNo']);
	}else{
		text_row(_("Student Reg.Num"), 'cust_ref', null, 30, 30);
		text_row(_("UWE Reg. Number:"), 'uweRegNo', $_POST['uweRegNo'], 40, 80);
	}
	
	text_row(_("Full Name:"), 'CustName', $_POST['CustName'], 40, 80);
	text_row(_("Known As:"), 'stdRegNo', $_POST['stdRegNo'], 40, 80);
	textarea_row(_("Permanent Address:"), 'address', $_POST['address'], 35, 5);

	/**************************BOF Chat Added**************************************/
	textarea_row(_("Contact Address:"), 'ContactAddress', $_POST['ContactAddress'], 35, 5);
	text_row(_("Home Phone Number:"), 'phone', null, 32, 30);
	text_row(_("Mobile Number:"), 'phone2', null, 32, 30);
	text_row(_("Fax Number:"), 'fax', null, 32, 30);
	email_row(_("E-mail:"), 'email', null, 35, 55);
	
	text_row(_("NIC/Passport Number:"), 'Nic', null, 32, 30);
	date_row(_("Date of Birth") . ":", 'Dob');
	gender_list_row(_("Gender:"), 'Gender', null);
	
	cob_list_row(_("Country Of Birth:"), 'Cob', null);
	nationality_list_row(_("Nationality:"), 'Nationality', null);
	domicile_list_row(_("Domicile:"), 'Domicile', null);
	//sales_persons_list_row(_("Sales Person:"), 'salesman', null);
	/**************************EOF Chat Added**************************************/
	
	// Add image upload for New Item  - by Joe
	/*file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
	// Add Image upload for New Item  - by Joe
	$stock_img_link = "";
	$check_remove_image = false;
	if (isset($_POST['stdRegNo']) && file_exists(company_path().'/images/'.item_img_name2($_POST['stdRegNo']).".jpg")){
	 // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
		$stock_img_link .= "<img id='item_img' alt = '[".$_POST['stdRegNo'].".jpg".
			"]' src='".company_path().'/images/'.item_img_name2($_POST['stdRegNo']).
			".jpg?nocache=".rand()."'"." height='$pic_height' border='0'>";
		$check_remove_image = true;
	}else{
		$stock_img_link .= _("No image");
	}

	label_row("&nbsp;", $stock_img_link);
	if ($check_remove_image)
		check_row(_("Delete Image:"), 'del_image');*/


	percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
	//text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
	hidden('tax_id', null);/*************Chat Added**********************/


	if (!$selected_id || is_new_customer($selected_id)) 
	{
		//currencies_list_row(_("Currency:"), 'curr_code', $_POST['curr_code']);
		hidden('curr_code', $_POST['curr_code']);/*************Chat Added**********************/
	} 
	else 
	{
		//label_row(_("Currency:"), $_POST['curr_code']);
		
		hidden('curr_code', $_POST['curr_code']);					
	}
	//sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);

	if($selected_id)
		record_status_list_row(_("Student status:"), 'inactive');
	elseif (isset($auto_create_branch) && $auto_create_branch == 1)
	{
		/*table_section_title(_("Branch"));
		text_row(_("Phone:"), 'phone', null, 32, 30);
		text_row(_("Secondary Phone Number:"), 'phone2', null, 32, 30);
		text_row(_("Fax Number:"), 'fax', null, 32, 30);
		email_row(_("E-mail:"), 'email', null, 35, 55);
		sales_persons_list_row( _("Sales Person:"), 'salesman', null);*/
	}
	table_section(2);

	table_section_title(_("Parent/Guardian's Information")); //***************************Chat Added

	
	/*percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
	amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);

	payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
	credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']);*/ 
	
	/**************************BOF Chat Added**************************************/
	text_row(_("Parent/Guardian's Name:"), 'GuardianName', $_POST['GuardianName'], 40, 80);
	text_row(_("Parent/Guardian's Home Phone Number:"), 'GuardianHPhone', null, 32, 30);
	text_row(_("Parent/Guardian's Mobile Number:"), 'GuardianMPhone', null, 32, 30);
	
	
	//hidden('discount', $_POST['discount']);
	hidden('pymt_discount', $_POST['pymt_discount']);
	hidden('credit_limit', $_POST['credit_limit']);
	hidden('payment_terms', $_POST['payment_terms']);
	hidden('credit_status', $_POST['credit_status']);
	
	/**************************EOF Chat Added**************************************/
	
	$dim = get_company_pref('use_dimension');
	if ($dim >= 1)
		//dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
		hidden('dimension_id', $_POST['dimension_id']);/*************Chat Added**********************/
	if ($dim > 1)
		//dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
		hidden('dimension2_id', $_POST['dimension2_id']);/*************Chat Added**********************/
	if ($dim < 1)
		hidden('dimension_id', 0);
	if ($dim < 2)
		hidden('dimension2_id', 0);

	if ($selected_id)  {
		start_row();
		echo '<td class="label">'._('Student Courses').':</td>';
	  	hyperlink_params_td($path_to_root . "/sales/manage/student_branches.php",
			'<b>'. (@$_REQUEST['popup'] ?  _("Select or &Add") : _("&Add or Edit ")).'</b>', 
			"debtor_no=".$selected_id.(@$_REQUEST['popup'] ? '&popup=1':''));
		end_row();
	}

	textarea_row(_("General Notes:"), 'notes', null, 35, 5);
	if (!$selected_id && isset($auto_create_branch) && $auto_create_branch == 1)
	{
		//table_section_title(_("Branch"));
		//locations_list_row(_("Default Inventory Location:"), 'location');
		//shippers_list_row(_("Default Shipping Company:"), 'ship_via');
		//sales_areas_list_row( _("Sales Area:"), 'area', null);
		tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);		
		

	// Add image upload for New Item  - by Joe
	file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
	// Add Image upload for New Item  - by Joe
	$stock_img_link = "";
	$check_remove_image = false;
	if (isset($_POST['stdRegNo']) && file_exists(company_path().'/images/'.item_img_name2($_POST['stdRegNo']).".jpg")){
	 // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
		$stock_img_link .= "<img id='item_img' alt = '[".$_POST['stdRegNo'].".jpg".
			"]' src='".company_path().'/images/'.item_img_name2($_POST['stdRegNo']).
			".jpg?nocache=".rand()."'"." height='$student_pic_height' width='$student_pic_width' border='0'>";
		$check_remove_image = true;
	}else{
		$stock_img_link .= _("No image");
	}

	label_row("&nbsp;", $stock_img_link);
	if ($check_remove_image)
		check_row(_("Delete Image:"), 'del_image');

		/************************BOF Chat Added******************/
		hidden('location');
		hidden('ship_via');
		hidden('area', null);
		//hidden('tax_group_id', null);
		/************************EOF Chat Added******************/
	}
	end_outer_table(1);

	div_start('controls');
	if (!$selected_id)
	{
		submit_center('btnsubmit', _("Add New Student"), true, '', 'default');
	} 
	else 
	{
		submit_center_first('btnsubmit', _("Update Student"), _('Update student data'), @$_REQUEST['popup'] ? true : 'default');
		submit_return('select', $selected_id, _("Select this student and return to document entry."));
		submit_center_last('delete', _("Delete Student"), _('Delete student data if have been never used'), true);
	}
	div_end();
}

//--------------------------------------------------------------------------------------------

check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a student."));
 
start_form(true);

if (db_has_customers()) 
{
	start_table(TABLESTYLE_NOBORDER);
	start_row();
	/******************************BOF Chat Added**************************************/
	//customer_list_cells(_("Select a customer: "), 'customer_id', null,_('New customer'), true, check_value('show_inactive'));
	
	student_list_cells(_("Select a Student : "), 'customer_id', (!empty( $_POST['customer_id']) ?  $_POST['customer_id'] : ""),_('New Student'), true, check_value('show_inactive'));
    //hidden('customer_id', (!empty( $_POST['customer_id']) ?  $_POST['customer_id'] : ""));
	
	searchBy_list_cells(_("Search By: "), 'searchBy', null);
	text_cells(null, 'searchText', '',null, '', true);
	/*ref_cells(_("Student Reg. Number :"), 'student_reg_num', '',null, '', true);
	ref_cells(_("UWE Reg. Number :"), 'uwe_reg_num', '',null, '', true);
	ref_cells(_("NIC Number:"), 'nic_num', '',null, '', true);*/
	
	submit_cells('SearchStudents', _("Search"),'','', 'default');
	
	/******************************EOF Chat Added**************************************/
	check_cells(_("Show inactive:"), 'show_inactive', null, true);
	end_row();
	end_table();

	if (get_post('_show_inactive_update')) {
		$Ajax->activate('customer_id');
		set_focus('customer_id');
	}
} 
else 
{
	hidden('customer_id');
}

if (!$selected_id)
	unset($_POST['_tabs_sel']); // force settings tab for new customer

tabbed_content_start('tabs', array(
		'settings' => array(_('&General settings'), $selected_id),
		//'contacts' => array(_('&Contacts'), $selected_id),
		//'transactions' => array(_('&Transactions'), $selected_id),
		//'orders' => array(_('Sales &Orders'), $selected_id),
	));
	
	switch (get_post('_tabs_sel')) {
		default:
		case 'settings':
			customer_settings($selected_id); 
			break;
		case 'contacts':
			$contacts = new contacts('contacts', $selected_id, 'customer');
			$contacts->show();
			break;
		case 'transactions':
			$_GET['customer_id'] = $selected_id;
			$_GET['popup'] = 1;
			include_once($path_to_root."/sales/inquiry/customer_inquiry.php");
			break;
		case 'orders':
			$_GET['customer_id'] = $selected_id;
			$_GET['popup'] = 1;
			include_once($path_to_root."/sales/inquiry/sales_orders_view.php");
			break;
	};
br();
tabbed_content_end();

hidden('popup', @$_REQUEST['popup']);
end_form();
end_page(@$_REQUEST['popup']);

?>
