if($_REQUEST['client_id']=='new')
	{
					$client_sql = "SELECT client_code FROM `clientmaster` order by client_code desc limit 1";
					$client_result = DB_query($client_sql ,$db);
					$client_rows = DB_fetch_array($client_result);
					$code = $client_rows['client_code']; 
					if($code != '')
					{
						$code_val1 = substr($code, 0,2);
						$code_val2 = substr($code, -3);
						$increment = $code_val2 + 1;
						$strFinal = $code_val1.str_pad($increment,3,"0",STR_PAD_LEFT);
					}
					else
					{
						$strFinal = 'CL001';
					}
					
					 
					if($_REQUEST['item']!=''){
								$itemArray=$_REQUEST['item'];
								$itemIds=implode(",",$itemArray);
								}else{
								$itemIds='';
								}
					
				
										$sql = "INSERT clientmaster SET
										`client_code`	='".$strFinal."',
										`c_name`		='".$_POST['c_name']."', 
										`short_name`	='".$_POST['short_name']."',  
										`tax`			='".$_POST['tax']."', 
										`credit_limit`			='".$_POST['client_limit']."', 
										`credit_days`			='".$_POST['client_days']."',  
										`payment_terms`	='".$_POST['payment_terms']."',
										`ledger_id`		='".$_POST['ledger_id']."',
										`ccategory`		='".$_POST['ccategory']."', 
										`pre_address1`	='".$_POST['address1']."',
										`pre_address2`	='".$_POST['address2']."',
										`pre_address3`	='".$_POST['address3']."',  
										`pre_area`		='".$_POST['pre_area']."', 
										`fax`			='".$_POST['fax']."', 
										`pre_country`	='".$_POST['country']."',  
										`pre_mobile`	='".$_POST['mobile']."',
										`pre_state`		='".$_POST['state']."', 
										`pre_city`		='".$_POST['city']."', 
										`pre_pincode`	='".$_POST['zipcode']."',
										`per_email`     ='".$_POST['email']."',
										`lst_no`		='".$_POST['lst_no']."', 
										`cst_no`		= '".$_POST['cst_no']."', 
										`excies_no`		='".$_POST['excies_no']."',  
										`pan_no` 		= '".$_POST['pan_no']."',
										`tin_no` 		= '".$_POST['tin_no']."',
										`Extension`		='".$_POST['pre_ext']."',
										`pre_phone1`	='".$_POST['phone']."',
										`pre_phone2`	='".$_POST['pre_phone2']."',
										`cl_range`		='".$_POST['cl_range']."',
										`cl_division`	='".$_POST['cl_division']."',
										`Site`			='".$_POST['Site']."',
										`Store`			='".$_POST['dealer']."',
										`Zone`			='".$_POST['ZoneId']."',
										`CompanyId`		='".$_SESSION['ACGCompanyId']."',
										`assigned_items`='".$itemIds."',
										`comp_group`	='".$_REQUEST['comp_group']."',
										`comodity`		='".$_REQUEST['comodity']."'";
										$res = DB_query($sql,$db,$ErrMsg,$DbgMsg);
										$lastid=DB_Last_Insert_ID($db,'clientmaster', 'id');
										$comapany=$lastid;
				
									for($CR=1;$CR<count($_REQUEST['contact_name']);$CR++)
									{
										 $insert_contact="Insert into contact_information_master set
																id='', 
																master_code='".$lastid."',  
																master_prefix='C',  
																person ='".$_REQUEST['contact_name'][$CR]."', 
																designation='".$_REQUEST['designation'][$CR]."',  
																phone='".$_REQUEST['phone'][$CR]."',
																email='".$_REQUEST['contact_email'][$CR]."',
																department='".$_REQUEST['department'][$CR]."'";	
										$insert_res=DB_query($insert_contact,$db);			
									}	
									 
							
	}
	else
	{
		$comapany=$_REQUEST['client_id'];
									$sql = "UPDATE clientmaster SET
										`c_name`		='".$_POST['c_name']."', 
										`short_name`	='".$_POST['short_name']."',  
										`tax`			='".$_POST['tax']."', 
										`credit_limit`			='".$_POST['client_limit']."', 
										`credit_days`			='".$_POST['client_days']."',  
										`payment_terms`	='".$_POST['payment_terms']."',
										`ledger_id`		='".$_POST['ledger_id']."',
										`ccategory`		='".$_POST['ccategory']."', 
										`pre_address1`	='".$_POST['address1']."',
										`pre_address2`	='".$_POST['address2']."',
										`pre_address3`	='".$_POST['address3']."',  
										`pre_area`		='".$_POST['pre_area']."', 
										`fax`			='".$_POST['fax']."', 
										`pre_country`	='".$_POST['country']."',  
										`pre_mobile`	='".$_POST['mobile']."',
										`pre_state`		='".$_POST['state']."', 
										`pre_city`		='".$_POST['city']."', 
										`pre_pincode`	='".$_POST['zipcode']."',
										`per_email`     ='".$_POST['email']."',
										`lst_no`		='".$_POST['lst_no']."', 
										`cst_no`		= '".$_POST['cst_no']."', 
										`excies_no`		='".$_POST['excies_no']."',  
										`pan_no` 		= '".$_POST['pan_no']."',
										`tin_no` 		= '".$_POST['tin_no']."',
										`Extension`		='".$_POST['pre_ext']."',
										`pre_phone1`	='".$_POST['phone']."',
										`pre_phone2`	='".$_POST['pre_phone2']."',
										`cl_range`		='".$_POST['cl_range']."',
										`cl_division`	='".$_POST['cl_division']."',
										`Site`			='".$_POST['Site']."',
										`Store`			='".$_POST['dealer']."',
										`Zone`			='".$_POST['ZoneId']."',
										`CompanyId`		='".$_SESSION['ACGCompanyId']."',
										`assigned_items`='".$itemIds."',
										`comp_group`	='".$_REQUEST['comp_group']."',
										`comodity`		='".$_REQUEST['comodity']."' 
										WHERE id='".$_REQUEST['client_id']."'";
										$res = DB_query($sql,$db,$ErrMsg,$DbgMsg);
										
										for($CR=1;$CR<$_REQUEST['contact_name'];$CR++)
										{
										 	$insert_contact="UPDATE contact_information_master set
																master_code='".$_REQUEST['client_id']."',  
																master_prefix='C',  
																person ='".$_REQUEST['contact_name'][$CR]."', 
																designation='".$_REQUEST['designation'][$CR]."',  
																phone='".$_REQUEST['phone'][$CR]."',
																email='".$_REQUEST['contact_email'][$CR]."',
																department='".$_REQUEST['department'][$CR]."'
																WHERE id='".$_REQUEST['contact_det_id'][$CR]."'";	
											$insert_res=DB_query($insert_contact,$db);			
										}	
	
	}