<?php

session_start();

include("config.inc.php");
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("function.inc.php");
include("Functions_UserAccess.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536)
		or die("Could not connect to DB");
 mysql_select_db($_SESSION['dbLabel'],$connection) or die("Could not find DB");

mysql_query("SET NAMES 'utf8'"); 
mysql_query('SET CHARACTER SET utf8');

 ///Adjusting Formats depending on type
$mainmode = $_POST["mainmode"]; if ($mainmode == ""){$mainmode = $_GET["mainmode"];}
$actionmode = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}

if($mainmode=='delete'){
	$vAccessId = $_POST["AccessId"]; if ($vAccessId == ""){$vAccessId = $_GET["AccessId"];}
}	
	
else{	
	$vAccessId = $_POST["txtAccessId"]; if ($vAccessId == ""){$vAccessId = $_GET["txtAccessId"];}
}

$vAccessId2 = $_POST["txtAccessId2"]; if ($vAccessId2 == ""){$vAccessId2 = $_GET["txtAccessId2"];}

$vAccessType = $_POST["txtAccessType"]; if ($vAccessType == ""){$vAccessType = $_GET["txtAccessType"];}


$vAccessCode = $_POST["txtAccessCode"]; if ($vAccessCode == ""){$vAccessCode = $_GET["txtAccessCode"];}
$vAccessCode=GetSQLValueString($vAccessCode,'text');

$vAccessName = $_POST["txtAccessName"]; if ($vAccessName == ""){$vAccessName = $_GET["txtAccessName"];}
$vAccessName=GetSQLValueString($vAccessName,'text');

$vAccessLanguage = $_POST["txtAccessLanguage"]; if ($vAccessLanguage == ""){$vAccessLanguage = $_GET["txtAccessLanguage"];}
$vAccessLanguage=GetSQLValueString($vAccessLanguage,'double');

$vAccessRemark = $_POST["txtAccessRemark"]; if ($vAccessRemark == ""){$vAccessRemark = $_GET["txtAccessRemark"];}
$vAccessRemark=GetSQLValueString($vAccessRemark,'text');

$vAccessPass = $_POST["txtAccessPassword"]; if ($vAccessPass == ""){$vAccessPass = $_GET["txtAccessPassword"];}

if($config['EncryptPassMod']==1){
	$query="select encryptpassword('$vAccessPass')";
	$result= mysql_query($query, $connection);
	$row=mysql_fetch_row($result);
	$vAccessPass=$row[0];	
}

$vAccessPass=GetSQLValueString($vAccessPass,'text');

$AccessFromDate = $_POST["txtFromDate"]; if ($AccessFromDate == ""){$AccessFromDate = $_GET["txtFromDate"];}

if($AccessFromDate!='')
	$AccessFromDate=insertMySQLDate($AccessFromDate);
$AccessFromDate=GetSQLValueString($AccessFromDate,'date');

$AccessToDate = $_POST["txtToDate"]; if ($AccessToDate == ""){$AccessToDate = $_GET["txtToDate"];}

if($AccessToDate!='')
	$AccessToDate=insertMySQLDate($AccessToDate);
$AccessToDate=GetSQLValueString($AccessToDate,'date');

$vInactiveReason = $_POST["txtInactiveReason"]; if ($vInactiveReason == ""){$vInactiveReason = $_GET["txtInactiveReason"];}
$vInactiveReason=GetSQLValueString($vInactiveReason,'text');

$vAccessGroup1 = $_POST["txtAccessGroup"]; if ($vAccessGroup == ""){$vAccessGroup = $_GET["txtAccessGroup"];}


$vAccessGroup = $_POST["txtAccessGroup"]; if ($vAccessGroup == ""){$vAccessGroup = $_GET["txtAccessGroup"];}
$vAccessGroup=GetSQLValueString($vAccessGroup,'text');

$vAccessShift = $_POST["txtAccessShift"]; if ($vAccessShift == ""){$vAccessShift = $_GET["txtAccessShift"];}
$vAccessShift=GetSQLValueString($vAccessShift,'double');

$vAccessLanguage = $_POST["txtAccessLanguage"]; if ($vAccessLanguage == ""){$vAccessLanguage = $_GET["txtAccessLanguage"];}
$vAccessLanguage=GetSQLValueString($vAccessLanguage,'double');

$vAccessActive = $_POST["txtAccessActive"]; if ($vAccessActive == ""){$vAccessActive = $_GET["txtAccessActive"];}
$vAccessActive=GetSQLValueString($vAccessActive,'double');

$vBelongs = $_POST["txtBelongsToGrp"]; if ($vBelongs == ""){$vBelongs = $_GET["txtBelongsToGrp"];}
$vBelongs=GetSQLValueString($vBelongs,'double');

$vDefaultCompany = $_POST["txtAccessDefaultSchema"]; if ($vDefaultCompany == ""){$vDefaultCompany = $_GET["txtAccessDefaultSchema"];}
$vDefaultCompany=GetSQLValueString($vDefaultCompany,'text');

$vDefaultYear = $_POST["txtAccessDefaultYear"]; if ($vDefaultYear == ""){$vDefaultYear = $_GET["txtAccessDefaultYear"];}
$vDefaultYear=GetSQLValueString($vDefaultYear,'text');

$vAccessEmail = $_POST["txtAccessEmail"]; if ($vAccessEmail == ""){$vAccessEmail = $_GET["txtAccessEmail"];}
$vAccessEmail=GetSQLValueString($vAccessEmail,'text');

$vSchemaDefault = $_POST["txtSchemaDefault"]; if ($vSchemaDefault == ""){$vSchemaDefault = $_GET["txtSchemaDefault"];}
$vSchemaDefault=GetSQLValueString($vSchemaDefault,'double');

$vAccessSchema = $_POST["txtAccessSchema"]; if ($vAccessSchema == ""){$vAccessSchema = $_GET["txtAccessSchema"];}
$vAccessSchema=GetSQLValueString($vAccessSchema,'double');

$vApplicationDefault = $_POST["txtApplicationDefault"]; if ($vApplicationDefault == ""){$vApplicationDefault = $_GET["txtApplicationDefault"];}
$vApplicationDefault=GetSQLValueString($vApplicationDefault,'double');

$vApplication = $_POST["txtApplication"]; if ($vApplication == ""){$vApplication = $_GET["txtApplication"];}
$vApplication=GetSQLValueString($vApplication,'double');

$vSubMenu = $_POST["txtSubMenu"]; if ($vSubMenu == ""){$vSubMenu = $_GET["txtSubMenu"];}
$vSubMenu=GetSQLValueString($vSubMenu,'double');

$vAccessFormId = $_POST["txtAccessFormId"]; if ($vAccessFormId == ""){$vAccessFormId = $_GET["txtAccessFormId"];}
$vAccessFormId=GetSQLValueString($vAccessFormId,'double');

$vAccessFieldId = $_POST["txtAccessFieldId"]; if ($vAccessFieldId == ""){$vAccessFieldId = $_GET["txtAccessFieldId"];}
$vAccessFieldId=GetSQLValueString($vAccessFieldId,'double');

$vApplicationActive = $_POST["txtAccessApplicationActive"]; if ($vApplicationActive == ""){$vApplicationActive = $_GET["txtAccessApplicationActive"];}
$vApplicationActive=GetSQLValueString($vApplicationActive,'double');

$vSchemaActive = $_POST["txtAccessSchemaActive"]; if ($vSchemaActive == ""){$vSchemaActive = $_GET["txtAccessSchemaActive"];}
$vSchemaActive=GetSQLValueString($vSchemaActive,'double');

$vAccessSchemaCurrentId = $_POST["txtAccessSchemaCurrentId"]; if ($vAccessSchemaCurrentId == ""){$vAccessSchemaCurrentId = $_GET["txtAccessSchemaCurrentId"];}
$vAccessSchemaCurrentId=GetSQLValueString($vAccessSchemaCurrentId,'double');

$vSchemaRestrictionId = $_POST["txtSchemaRestrictionId"]; if ($vSchemaRestrictionId == ""){$vSchemaRestrictionId = $_GET["txtSchemaRestrictionId"];}


$vSchemaRestrictionType = $_POST["txtType"]; if ($vSchemaRestrictionType == ""){$vSchemaRestrictionType = $_GET["txtType"];}
$vSchemaRestrictionType=GetSQLValueString($vSchemaRestrictionType,'text');

$vSchemaRestrictionOption = $_POST["txtRestrictions"]; if ($vSchemaRestrictionOption == ""){$vSchemaRestrictionOption = $_GET["txtRestrictions"];}
$vSchemaRestrictionOption=GetSQLValueString($vSchemaRestrictionOption,'double');

$vConditionId = $_POST["txtConditionId"]; if ($vConditionId == ""){$vConditionId = $_GET["txtConditionId"];}


$vConditionDefaultId = $_POST["txtCondition"]; if ($vConditionDefaultId == ""){$vConditionDefaultId = $_GET["txtCondition"];}
$vConditionDefaultId=GetSQLValueString($vConditionDefaultId,'double');

$vConditionValue = $_POST["txtConditionValue"]; if ($vConditionValue == ""){$vConditionValue = $_GET["txtConditionValue"];}
$vConditionValue=GetSQLValueString($vConditionValue,'double');



$vConditionDefaultValue = $_POST["txtConditionDefaultValue"]; if ($vConditionDefaultValue == ""){$vConditionDefaultValue = $_GET["txtConditionDefaultValue"];}


$vConditionInclude = $_POST["txtInclude"]; if ($vConditionInclude == ""){$vConditionInclude = $_GET["txtInclude"];}
$vConditionInclude=GetSQLValueString($vConditionInclude,'double');

$vConditionFormType = $_POST["txtConditionFormType"]; if ($vConditionFormType == ""){$vConditionFormType = $_GET["txtConditionFormType"];}
$vConditionFormType=GetSQLValueString($vConditionFormType,'text');

$vConditionEvent = $_POST["txtConditionEvent"]; if ($vConditionEvent == ""){$vConditionEvent = $_GET["txtConditionEvent"];}
$vConditionEvent=GetSQLValueString($vConditionEvent,'text');

$vIndex = $_POST["index"]; if ($vIndex == ""){$vIndex = $_GET["index"];}
$vTabId = $_POST["txtTabId"]; if ($vTabId == ""){$vTabId = $_GET["txtTabId"];}

$vLanguage=$_SESSION['languageid'];
$vAccessDb=$_SESSION['dbLabel'];



//Checking if code already exist
$vErrorExist=0;
if($mainmode=='edit' || $mainmode=='insert'){

	$sub_query="select count(*) from access where upper(access_code)=upper($vAccessCode)";

	if($mainmode=='edit')
		$sub_query=$sub_query." and access_id<>'".$vAccessId."'";
	//echo$sub_query;exit;
	$sub_result = mysql_query($sub_query, $connection);
	$sub_row=mysql_fetch_row($sub_result);
	$count=$sub_row[0];

	if($count>0){
		$message=GetMsg('1',$vLanguage,'error',$connection);
		$vErrorExist=1;
	}
}


if($mainmode=='insert' && $vErrorExist!=1){
	
	
	fbegin();
	$query="insert into access (access_type,Access_Code,access_name,access_password,access_remark,access_fromdate,access_todate,".
			" access_inactivereason,access_group,usershift_id,language_id,access_active,access_belongstogrp,access_DefaultComp,".
			" access_DefaultYear,access_email) ".
			" values($vAccessType,$vAccessCode,$vAccessName,$vAccessPass,$vAccessRemark,$AccessFromDate,$AccessToDate,$vInactiveReason,".
			" $vAccessGroup,$vAccessShift,$vAccessLanguage,$vAccessActive,$vBelongs,$vDefaultCompany,$vDefaultYear,$vAccessEmail)";
	
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	
	if($result){
		$vAccessId=mysql_insert_id();
		
		if($vAccessId!='' && $vAccessType=='0'){
		
			if($vAccessType=='1'){
				$vDefaultName='Group';
			}	
			else {
				$vDefaultName='User';
			}
		
			$query="call CreateUserFromGroup('".$vDefaultName."','".$vAccessId."',$vAccessGroup) ";
			//echo$query;exit;
			$result  = mysql_query($query, $connection);
			$message=mysql_error();
			
			
			if(!$result)
				$vErrorExist=1;
		}	
		else if($vAccessId!='' && $vAccessType=='1' && $vAccessId2!=''){
		
			
			$query="call CreateUserFromGroup('Group','".$vAccessId."',$vAccessId2) ";
			//echo$query;exit;
			$result  = mysql_query($query, $connection);
			$message=mysql_error();
			
			if(!$result)
				$vErrorExist=1;
		}	
	}
	else $vErrorExist=1;
	
	if($vErrorExist==1){
		frollback();
		
		$message=GetMsg('16',$vLanguage,'error',$connection);
		$vErrorExist=1;
	
	}	
	else fcommit();
}

else if($mainmode=='edit' && $vErrorExist!=1){

	/// check if user has changed the group
		$query = "select access_group as oldgroup from access where access_id='".$vAccessId."' ";
		$result  = mysql_query($query, $connection);
		while($rows=mysql_fetch_array($result)){
			extract($rows);
		}	

		if($vAccessGroup1 != $oldgroup and $vAccessGroup1!=''){
			fbegin();
		
			
			$query = "delete from accessforms where accessschema_id='".$txtAccessSchemaCurrentId."'";
			$result  = mysql_query($query, $connection);
 
			$query="call ChangeUserFromGroup('User','".$vAccessId."',$vAccessGroup,$txtAccessSchemaCurrentId.) ";
			
			$result1  = mysql_query($query, $connection) or die(mysql_error());
			
			$message=mysql_error();
 
		if($result1==1){
			fcommit();
			
			}
			else
			rollback();
		}
		

	$query="update access set access_type=$vAccessType,
			Access_Code=$vAccessCode,
			access_name=$vAccessName,
			access_password=$vAccessPass,
			access_remark=$vAccessRemark,
			access_fromdate=$AccessFromDate,
			access_todate=$AccessToDate,
			access_inactivereason=$vInactiveReason,
			access_group=$vAccessGroup,
			usershift_id=$vAccessShift,
			language_id=$vAccessLanguage,
			access_active=$vAccessActive,
			access_belongstogrp=$vBelongs,
			access_DefaultComp=$vDefaultCompany,
			access_DefaultYear=$vDefaultYear,
			access_email=$vAccessEmail
			where access_id='".$vAccessId."'";
	
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	fbegin();
	
	if($createcopies=='Parent' or $createcopies=='Teacher' or $createcopies=='Coordinator' or $createcopies=='Supervisor' or $createcopies=='Student' or $createcopies=='Client' or $createcopies=='SubClient'){
	//// copies to users from this user or group
	if($createcopies=='Teacher')
		$query = " select teacher_code from ".$_SESSION['dbName'].".teachers 
		   inner join ".$_SESSION['dbName'].".ranks on ".$_SESSION['dbName'].".teachers.rank_id = ".$_SESSION['dbName'].".ranks.rank_id
		   where teacher_active = 1 and ".$_SESSION['dbName'].".ranks.rank_description='Teacher' and teacher_code not in (select access_code from ".$_SESSION['dbLabel'].".access)";
	else
	if($createcopies=='Coordinator')
		$query = " select teacher_code from ".$_SESSION['dbName'].".teachers 
		   inner join ".$_SESSION['dbName'].".ranks on ".$_SESSION['dbName'].".teachers.rank_id = ".$_SESSION['dbName'].".ranks.rank_id
		   where teacher_active = 1 and ".$_SESSION['dbName'].".ranks.rank_description='Coordinator' and teacher_code not in (select access_code from ".$_SESSION['dbLabel'].".access)";
	else
	if($createcopies=='Supervisor')
		$query = " select teacher_code from ".$_SESSION['dbName'].".teachers 
		   inner join ".$_SESSION['dbName'].".ranks on ".$_SESSION['dbName'].".teachers.rank_id = ".$_SESSION['dbName'].".ranks.rank_id
		   where teacher_active = 1 and ".$_SESSION['dbName'].".ranks.rank_description='Supervisor' and teacher_code not in (select access_code from ".$_SESSION['dbLabel'].".access)";
	else
	if($createcopies=='Parent')
		$query = " select distinct family_code from  ".$_SESSION['dbName'].".families
		    inner join ".$_SESSION['dbName'].".students on ".$_SESSION['dbName'].".students.family_id = ".$_SESSION['dbName'].".families.family_id
    		where family_code not in (select access_code from ".$_SESSION['dbLabel'].".access) and ".$_SESSION['dbName'].".students.studentstatus_id=4";
    else
	if($createcopies=='Student')
		$query = " select distinct student_code from  ".$_SESSION['dbName'].".students
		       		where student_code not in (select access_code from ".$_SESSION['dbLabel'].".access) and ".$_SESSION['dbName'].".students.studentstatus_id=4";
	else if ($createcopies=='Client')
		$query = " select distinct ledger_number from  ".$_SESSION['dbName'].".clients
		       		where ledger_code not in (select access_code from ".$_SESSION['dbLabel'].".access)";
	else if ($createcopies=='SubClient')
		$query = " select distinct ledgerparty_code from  ".$_SESSION['dbName'].".ledgerParty
		       		where ledgerparty_code not in (select access_code from ".$_SESSION['dbLabel'].".access)";
	
	
	
	//echo$query;exit;
	$vAccessGroup=$vAccessId;
	$resultcopies = mysql_query($query, $connection) or die(mysql_error());
	while($rowcopies=mysql_fetch_row($resultcopies)){
	$vAccessType=0;
	
	 
	$query="insert into access (access_type,Access_Code,access_name,access_password,access_remark,access_fromdate,access_todate,".
			" access_inactivereason,access_group,usershift_id,language_id,access_active,access_belongstogrp,access_DefaultComp,".
			" access_DefaultYear,access_email) ".
			" values($vAccessType,'$rowcopies[0]','$createcopies',encryptpassword('$rowcopies[0]'),$vAccessRemark,$AccessFromDate,$AccessToDate,$vInactiveReason,".
			" $vAccessGroup,$vAccessShift,$vAccessLanguage,1,$vBelongs,$vDefaultCompany,$vDefaultYear,$vAccessEmail)";
	
    echo$query;exit;
	$result  = mysql_query($query, $connection) or die(mysql_error());
	
			if($result){
				$vAccessId=mysql_insert_id();
				
				if($vAccessId!='' && $vAccessType=='0'){
				
					if($vAccessType=='1'){
						$vDefaultName='Group';
					}	
					else {
						$vDefaultName='User';
					}
				
					$query="call CreateUserFromGroup('".$vDefaultName."','".$vAccessId."',$vAccessGroup) ";
					//echo$query."<br>";
					//exit;
					$result  = mysql_query($query, $connection) or die(mysql_error());
					$message=mysql_error();
					
					
					if(!$result)
						$vErrorExist=1;
				}	
				else if($vAccessId!='' && $vAccessType=='1' && $vAccessId2!=''){
				
					
					$query="call CreateUserFromGroup('Group','".$vAccessId."',$vAccessId2) ";
					//echo$query;exit;
					$result  = mysql_query($query, $connection);
					$message=mysql_error();
					
					if(!$result)
						$vErrorExist=1;
				}	
			}
			else $vErrorExist=1;
	
	
	}
	}
	if($vErrorExist==1){
		frollback();
		
		$message=GetMsg('16',$vLanguage,'error',$connection);
		$vErrorExist=1;
	
	}	
	else fcommit();
	
	
 
 
	
	
	
	
	//////////////////////////////////
	
	
}

else if($mainmode=='delete' && $vErrorExist!=1){

	$query="delete from accessschema where access_id='".$vAccessId."'";
	$result  = mysql_query($query, $connection);

	$query="delete from access where access_id='".$vAccessId."'";
	$result  = mysql_query($query, $connection);

}

else if($mainmode=='AddSchema' && $vErrorExist!=1){

	$query="insert into accessschema(company_id,access_id,startdate,enddate,accessschema_name,diryear,accessschema_id_default,accessschema_active)
			select company_id,'".$vAccessId."',startdate,enddate,accessschema_name,diryear,accessschema_id,'1' from accessschema
			where accessschema_id=$vSchemaDefault";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}	
	
	else if($result && $vAccessType==1){
		$query="select access_id from access where access_group='".$vAccessId."'";
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
		
			$vUserId=$row[0];
			
			$subquery="insert into accessschema(company_id,access_id,startdate,enddate,accessschema_name,diryear,accessschema_id_default,accessschema_active,defaulttype)
					select company_id,'".$vUserId."',startdate,enddate,accessschema_name,diryear,accessschema_id,'1','User' from accessschema
					where accessschema_id=$vSchemaDefault"; 
			$resultsub = mysql_query($subquery, $connection);
		}
	}
}


else if($mainmode=='UpdateApplicationAccess' && $vErrorExist!=1){

	$query="update accessapplication set accessapplication_active=$vApplicationActive
			where accessapplication_id=$vApplication";
		//	echo$query;exit;
	$result = mysql_query($query, $connection);
	$message = mysql_error();
	
	if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}
	else if($result && $vAccessType==1){
			//Getting Default Schema for this group to filter users schemas
		$query="select accessschema_id_default from accessschema where accessschema_id=$vAccessSchema";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessSchemaDefault=$row[0];
		
		$query="select accessapplication_application from accessapplication where accessapplication_id=$vApplication";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vApplicationDefault=$row[0];
		
		$query="select access.access_id,accessapplication_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				inner join accessapplication on accessapplication.accessschema_id=accessschema.accessschema_id
				where access_group='".$vAccessId."' and accessschema.accessschema_id_default='".$vAccessSchemaDefault."'
				and accessapplication.accessapplication_application='".$vApplicationDefault."'";
			//echo$query;exit;
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$subquery="update accessapplication set accessapplication_active=$vApplicationActive
					where accessapplication_id='$row[1]'";
			//	echo$query;exit;
			$resultsub = mysql_query($subquery, $connection);
			$message = mysql_error();
		}
	}
}

else if($mainmode=='UpdateSchemaAccess' && $vErrorExist!=1){

	$query="update accessschema set accessschema_active=$vSchemaActive
			where accessschema_id=$vAccessSchema";
		//echo$query;exit;
	$result = mysql_query($query, $connection);
	$message = mysql_error();
	
	if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}
	else if($result && $vAccessType==1){
			//Getting Default Schema for this group to filter users schemas
		$query="select accessschema_id_default from accessschema where accessschema_id=$vAccessSchema";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessSchemaDefault=$row[0];
		
		
		
		$query="select access.access_id,accessschema_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				
				where access_group='".$vAccessId."' and accessschema.accessschema_id_default='".$vAccessSchemaDefault."'";
			//echo$query;exit;
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$subquery="update accessschema set accessschema_active=$vSchemaActive
					where accessschema_id='$row[1]'";
			//	echo$query;exit;
			$resultsub = mysql_query($subquery, $connection);
			$message = mysql_error();
		}
	}
}

else if($mainmode=='AddApplication' && $vErrorExist!=1){

	if($vAccessType=='1'){
		$vDefaultName='Group';
	}	
	else {
		$vDefaultName='User';
	}
	
	$query="call AddApplicationToAccess('".$vDefaultName."',$vAccessSchema,$vApplicationDefault)";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}
	else if($result && $vAccessType==1){
	
		//Getting Default Schema for this group to filter users schemas
		$query="select accessschema_id_default from accessschema where accessschema_id=$vAccessSchema";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessSchemaDefault=$row[0];
		
		$query="select access.access_id,accessschema_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				where access_group='".$vAccessId."' and accessschema.accessschema_id_default='".$vAccessSchemaDefault."'";
			
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$subquery="call AddApplicationToAccess('User','$row[1]',$vApplicationDefault)";
			$resultsub  = mysql_query($subquery, $connection);
			$message = mysql_error();
		}
	}	
}

if($actionmode=='UpdateFormAccess'){
	
	$Type = $_POST["Type"]; if ($Type == ""){$Type = $_GET["Type"];}
	$vaccess = $_POST["vaccess"]; if ($vaccess == ""){$vaccess = $_GET["vaccess"];}
	
	$vaccessId = $_POST["vaccessId"]; if ($vaccessId == ""){$vaccessId = $_GET["vaccessId"];}
	$vAccessSchemaCurrentId = $_POST["vAccessSchemaCurrentId"]; if ($vAccessSchemaCurrentId == ""){$vAccessSchemaCurrentId = $_GET["vAccessSchemaCurrentId"];}

	
	/*$vFormView = $_POST["txtView".$vIndex]; if ($vFormView == ""){$vFormView = $_GET["txtView".$vIndex];}
	$vFormInsert = $_POST["txtInsert".$vIndex]; if ($vFormInsert == ""){$vFormInsert = $_GET["txtInsert".$vIndex];}
	$vFormEdit = $_POST["txtEdit".$vIndex]; if ($vFormEdit == ""){$vFormEdit = $_GET["txtEdit".$vIndex];}
	$vFormDelete = $_POST["txtDelete".$vIndex]; if ($vFormDelete == ""){$vFormDelete = $_GET["txtDelete".$vIndex];}
	$vFormPrint = $_POST["txtPrint".$vIndex]; if ($vFormPrint == ""){$vFormPrint = $_GET["txtPrint".$vIndex];}
	$vFormPreview = $_POST["txtPreview".$vIndex]; if ($vFormPreview == ""){$vFormPreview = $_GET["txtPreview".$vIndex];}
	$vFormExport = $_POST["txtExport".$vIndex]; if ($vFormExport == ""){$vFormExport = $_GET["txtExport".$vIndex];}
	$vFormDashboard = $_POST["txtDashboard".$vIndex]; if ($vFormDashboard == ""){$vFormDashboard = $_GET["txtDashboard".$vIndex];}
	
	
	$query="update accessforms set accessform_view=$vFormView,
								   accessform_insert=$vFormInsert,
								   accessform_edit=$vFormEdit,
								   accessform_delete=$vFormDelete,
								   accessform_print=$vFormPrint,
								   accessform_preview=$vFormPreview,
								  accessform_export=$vFormExport,
								  accessform_Dashboard=$vFormDashboard
				where accessform_id=$vAccessFormId";
	*/
	$query="update accessforms set $Type=$vaccess
				where accessform_id=$vAccessFormId";
	$result = mysql_query($query, $connection);

	/*if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}*/
	if($result && $vAccessType==1){
		//Getting Default Schema for this group to filter users schemas
		$query="select accessschema_id_default from accessschema where accessschema_id=$vAccessSchemaCurrentId";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessSchemaDefault=$row[0];
		
		
		
	
		$query="select access.access_id,accessschema_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				where access_group='".$vaccessId."' and accessschema.accessschema_id_default='".$vAccessSchemaDefault."'";
			
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$subquery="update accessforms set $Type=$vaccess
				where accessform_id_default=$vAccessFormId and accessschema_id=$row[1]";
			$resultsub  = mysql_query($subquery, $connection);
			$message = mysql_error();
			echo$subquery."<BR>";
		}
		
			//Getting Default Form for this group to filter users schemas
		$query="select accessform_id_default from accessforms where accessform_id=$vAccessFormId";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessFormId=$row[0];
		
			$query="select access.access_id,accessschema_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				where access_group='".$vaccessId."' and accessschema.accessschema_id_default='".$vAccessSchemaDefault."'";
			
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$subquery="update accessforms set $Type=$vaccess
				where accessform_id_default=$vAccessFormId and accessschema_id=$row[1]";
			$resultsub  = mysql_query($subquery, $connection);
			$message = mysql_error();
			echo$subquery."<BR>";
		}
	}
	
}

else  if($actionmode=='UpdateFieldAccess'){
		
	$vFieldEdit = $_POST["txtEdit".$vIndex]; if ($vFieldEdit == ""){$vFieldEdit = $_GET["txtEdit".$vIndex];}
	$vFieldHide = $_POST["txtHide".$vIndex]; if ($vFieldHide == ""){$vFieldHide = $_GET["txtHide".$vIndex];}
	$vFieldMandatory = $_POST["txtMandatory".$vIndex]; if ($vFieldMandatory == ""){$vFieldMandatory = $_GET["txtMandatory".$vIndex];}
	$vFieldSearchable = $_POST["txtSearchable".$vIndex]; if ($vFieldSearchable == ""){$vFieldSearchable = $_GET["txtSearchable".$vIndex];}
	
	//Getting Default Schema for this group to filter users schemas
		$query="select accessschema_id_default from accessschema where accessschema_id=$vAccessSchemaCurrentId";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessSchemaDefault=$row[0];
		
			//Getting Default Form for this group to filter users schemas
		$query="select accessform_id_default from accessforms where accessform_id=$vAccessFormId";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessFormId=$row[0];
		
	if($vFieldHide=='')
		$vFieldHide=0;
		
	if($vFieldMandatory=='')
		$vFieldMandatory=0;	
	
	if($vFieldSearchable=='')
		$vFieldSearchable=0;	
		
	if($vFieldEdit=='')
		$vFieldEdit=0;	
		
	$query="update accessfields set accessfield_edit=$vFieldEdit,
								   accessfield_mandatory=$vFieldMandatory,
								   accessfield_searchable=$vFieldSearchable,
								   accessfield_hide=$vFieldHide
			where accessfield_id=$vAccessFieldId";
	//echo$query;exit;	
	$result = mysql_query($query, $connection);

	if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}
	else if($result && $vAccessType==1){
		$query="select access.access_id,accessschema_id,accessforms.accessform_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				inner join accessforms on accessforms.accessschema_id=accessschema.accessschema_id
				where access_group='".$vAccessId."' and accessschema.accessschema_id_default='".$vAccessSchemaDefault."'
				and accessform_id_default='".$vAccessFormId."'";
			//echo$query;exit;
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$subquery="update accessfields set accessfield_edit=$vFieldEdit,
								   accessfield_mandatory=$vFieldMandatory,
								   accessfield_searchable=$vFieldSearchable,
								   accessfield_hide=$vFieldHide
			where accessform_id=$row[2]";
			$resultsub = mysql_query($subquery, $connection);	
		}
	}	
}
else if($actionmode=='UpdateTabAccess'){

		$vTabActive = $_POST["txtActive".$vIndex]; if ($vTabActive == ""){$vTabActive = $_GET["txtActive".$vIndex];}

		//Getting Default Schema for this group to filter users schemas
		$query="select accessschema_id_default from accessschema where accessschema_id=$vAccessSchemaCurrentId";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessSchemaDefault=$row[0];
		
			//Getting Default Form for this group to filter users schemas
		$query="select accessform_id_default from accessforms where accessform_id=$vAccessFormId";
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vAccessFormId=$row[0];

		$query="update accessformstabs set accessformstab_active=$vTabActive
				where accessformstab_id='".$vTabId."'";
		//echo$query;exit;	
		$result = mysql_query($query, $connection);
	
		if($result && $vAccessType==0){
			$query="update access set access_group=null where access_id='".$vAccessId."'";
			$result = mysql_query($query, $connection);
		}
		else if($result && $vAccessType==1){
			$query="select access.access_id,accessschema_id,accessforms.accessform_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				inner join accessforms on accessforms.accessschema_id=accessschema.accessschema_id
				where access_group='".$vAccessId."' and accessschema.accessschema_id_default='".$vAccessSchemaDefault."'
				and accessform_id_default='".$vAccessFormId."'";
			//echo$query;exit;
			$result = mysql_query($query, $connection);
			while($row=mysql_fetch_row($result)){
				$subquery="update accessformstabs set accessformstab_active=$vTabActive,
								  
						where accessform_id=$row[2]";
					$resultsub = mysql_query($subquery, $connection);	
		
			}
		}
}

else  if($actionmode=='UpdateSchemaRestrictions'){
	$sub_query="select accessschema_name,diryear from accessschema where accessschema_id=$vAccessSchema";
	$resultsub = mysql_query($sub_query, $connection);
	$row_sub=mysql_fetch_row($resultsub);
	$vAccessSchemaName=$row_sub[0];
	$vAccessSchemaYear=$row_sub[1];
				
	$sub_query="select count(*) from schemarestrictions where upper(actiontype)=upper($vSchemaRestrictionType) 
			 and accessschema_id=$vAccessSchema ";

	if($vSchemaRestrictionId!='')
		$sub_query=$sub_query." and schemarestriction_id<>'".$vSchemaRestrictionId."'";
	
	$sub_result = mysql_query($sub_query, $connection);
	$sub_row=mysql_fetch_row($sub_result);
	$count=$sub_row[0];

	if($count>0){
		$message=GetMsg('17',$vLanguage,'error',$connection);
		$vErrorExist=1;
	}
	
	else{
		if($vSchemaRestrictionId=='' && $vErrorExist!=1){
			$query="insert into schemarestrictions(accessschema_id,restriction_id,actiontype)
					values($vAccessSchema,$vSchemaRestrictionOption,$vSchemaRestrictionType)";
			$result = mysql_query($query, $connection);
			
			
				
			if($result && $vAccessType==0){
				$query="update access set access_group=null where access_id='".$vAccessId."'";
				$result = mysql_query($query, $connection);
			}
			else if($result && $vAccessType==1){
			
				
				
				$query="select access.access_id,accessschema.accessschema_id from access 
						inner join accessschema on accessschema.access_id=access.access_id
						where access_group='".$vAccessId."' and upper(accessschema.accessschema_name)=upper('".$vAccessSchemaName."')
						and diryear='".$vAccessSchemaYear."'";
				$result = mysql_query($query, $connection);
				while($row=mysql_fetch_row($result)){
					$vAccessSchema=$row[1];
					
					$subquery="insert into schemarestrictions(accessschema_id,restriction_id,actiontype)
					values($vAccessSchema,$vSchemaRestrictionOption,$vSchemaRestrictionType)";
				
					$resultsub = mysql_query($subquery, $connection);	
				}	
			}
		}	
		else if($vSchemaRestrictionId!=''  && $vErrorExist!=1){
			
				//Get PRevious Restriction and type before update
				$sub_query="select restriction_id,upper(actiontype) from schemarestrictions where schemarestriction_id=$vSchemaRestrictionId";
				$resultsub = mysql_query($sub_query, $connection);
				$row_sub=mysql_fetch_row($resultsub);
				$vRestrictionId=$row_sub[0];
				$vActionType=$row_sub[1];	
				
			$query="update schemarestrictions set restriction_id=$vSchemaRestrictionOption,
												actiontype=$vSchemaRestrictionType
					where schemarestriction_id='".$vSchemaRestrictionId."'";
			$result = mysql_query($query, $connection);
			
			if($result && $vAccessType==0){
				$query="update access set access_group=null where access_id='".$vAccessId."'";
				$result = mysql_query($query, $connection);
			}
			else if($result && $vAccessType==1){
			
				//Querying between users of this group
				$query="select access.access_id,accessschema.accessschema_id from access 
						inner join accessschema on accessschema.access_id=access.access_id
						where access_group='".$vAccessId."' and upper(accessschema.accessschema_name)=upper('".$vAccessSchemaName."')
						and diryear='".$vAccessSchemaYear."'";
				
				$result = mysql_query($query, $connection);
				while($row=mysql_fetch_row($result)){
					$vAccessSchema=$row[1];
					
					$subquery="update schemarestrictions set restriction_id=$vSchemaRestrictionOption,
												actiontype=$vSchemaRestrictionType
								where accessschema_id='".$vAccessSchema."' and upper(actiontype)=upper('$vActionType') 
								and restriction_id='$vRestrictionId'";
					$resultsub = mysql_query($subquery, $connection);
				}	
			}
		}
	}
}

else  if($actionmode=='DeleteSchemaRestrictions'){
	//Get PRevious Restriction and type before delete
	$sub_query="select restriction_id,upper(actiontype) from schemarestrictions where schemarestriction_id=$vSchemaRestrictionId";
	$resultsub = mysql_query($sub_query, $connection);
	$row_sub=mysql_fetch_row($resultsub);
	$vRestrictionId=$row_sub[0];
	$vActionType=$row_sub[1];	
	
	$query="delete from schemarestrictions where schemarestriction_id='".$vSchemaRestrictionId."'"; 
	$result = mysql_query($query, $connection);
	$message = mysql_error();
	
	$sub_query="select accessschema_name,diryear from accessschema where accessschema_id=$vAccessSchema";
	$resultsub = mysql_query($sub_query, $connection);
	$row_sub=mysql_fetch_row($resultsub);
	$vAccessSchemaName=$row_sub[0];
	$vAccessSchemaYear=$row_sub[1];
	
	if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}
	else if($result && $vAccessType==1){
	
		//Querying between users of this group
		$query="select access.access_id,accessschema.accessschema_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				where access_group='".$vAccessId."' and upper(accessschema.accessschema_name)=upper('".$vAccessSchemaName."')
				and diryear='".$vAccessSchemaYear."'";
				
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$vAccessSchema=$row[1];
					
			$subquery="delete from schemarestrictions 
					where accessschema_id='".$vAccessSchema."' and upper(actiontype)=upper('$vActionType') 
					and restriction_id='$vRestrictionId'";
			//echo$subquery;exit;
			$resultsub = mysql_query($subquery, $connection);
		}	
	}	
}

else  if($actionmode=='SubmitFormConditions'){

		$sub_query="select accessschema_name,diryear from accessschema where accessschema_id=$vAccessSchemaCurrentId";
		$resultsub = mysql_query($sub_query, $connection);
		$row_sub=mysql_fetch_row($resultsub);
		$vAccessSchemaName=$row_sub[0];
		$vAccessSchemaYear=$row_sub[1];
		
		$sub_query="select accessform_name from accessforms where accessform_id=$vAccessFormId";
		$resultsub = mysql_query($sub_query, $connection);
		$row_sub=mysql_fetch_row($resultsub);
		$vAccessFormName=$row_sub[0];
			
			
	if($vConditionId==''){		
		$query="insert into conditions (condition_id_default,accessschema_id,accessform_id,condition_actiontype,condition_type,condition_defaulttype,condition_name,condition_index,
				condition_caption,condition_description,condition_event)
				select $vConditionDefaultId,$vAccessSchemaCurrentId,$vAccessFormId,upper(condition_actiontype),upper(condition_type),if($vAccessType=0,'USER','GROUP'),condition_name,condition_index,
				condition_caption,condition_description,$vConditionEvent from conditions where condition_id=$vConditionDefaultId";
				
		//echo$query;exit;
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	else if($vConditionId!=''){
	
		$sub_query="select condition_caption,condition_index,condition_name from conditions where condition_id=$vConditionDefaultId";
	
		$resultsub = mysql_query($sub_query, $connection);
		$rowsub=mysql_fetch_row($resultsub );
		$vConditionCaption=$rowsub[0];
		$vIndex=$rowsub[1];
		$vConditionName=$rowsub[2];
		
		$vConditionName=GetSQLValueString($vConditionName,'text');
		$vConditionCaption=GetSQLValueString($vConditionCaption,'text');
		
		$query="update conditions set condition_id_default=$vConditionDefaultId,
									condition_index=$vIndex,		
									condition_name=$vConditionName,
									condition_caption=$vConditionCaption,
									condition_event=$vConditionEvent
				where condition_id='".$vConditionId."'";
			//echo$query;exit;
		$result = mysql_query($query, $connection);
	}	
	
	if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}
	else if($result && $vAccessType==1){
		
		//Querying between users of this group who has this form
		$query="select access.access_id,accessschema.accessschema_id,accessforms.accessform_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				inner join accessforms on accessforms.accessschema_id=accessschema.accessschema_id
				where access_group='".$vAccessId."' and upper(accessschema.accessschema_name)=upper('".$vAccessSchemaName."')
				and diryear='".$vAccessSchemaYear."' and upper(accessform_name)=upper('$vAccessFormName')";
		//	echo$query;exit;	
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$vAccessSchema=$row[1];
			$vAccessFormId=$row[2];
					
			if($vConditionId==''){			
				$subquery="insert into conditions (condition_id_default,accessschema_id,accessform_id,condition_actiontype,condition_type,condition_defaulttype,condition_name,condition_index,
				condition_caption,condition_description,condition_event)
				select $vConditionDefaultId,$vAccessSchema,$vAccessFormId,condition_actiontype,condition_type,'USER',condition_name,condition_index,
				condition_caption,condition_description,$vConditionEvent from conditions where condition_id=$vConditionDefaultId";
				$resultsub = mysql_query($subquery, $connection);
				
			}
			else if($vConditionId!=''){
				$query="update conditions set condition_id_default=$vConditionDefaultId,
									condition_index=$vIndex,		
									condition_name=$vConditionName,
									condition_caption=$vConditionCaption,
									condition_event=$vConditionEvent
				where accessschema_id='".$vAccessSchema."' and accessForm_id=$vAccessFormId and condition_id_default=$vConditionDefaultId";
				$result = mysql_query($query, $connection);
				
			}
		}	
	}
}

else  if($actionmode=='DeleteFormConditions'){

	$sub_query="select accessschema_name,diryear from accessschema where accessschema_id=$vAccessSchemaCurrentId";
	$resultsub = mysql_query($sub_query, $connection);
	$row_sub=mysql_fetch_row($resultsub);
	$vAccessSchemaName=$row_sub[0];
	$vAccessSchemaYear=$row_sub[1];
		
	$sub_query="select accessform_name from accessforms 
				inner join conditions on conditions.accessform_id=accessforms.accessform_id
				where condition_id=$vConditionId";
	//echo$sub_query;exit;		
	$resultsub = mysql_query($sub_query, $connection);
	$row_sub=mysql_fetch_row($resultsub);
	$vAccessFormName=$row_sub[0];	
	
	$query="delete from conditions where condition_id=$vConditionId";
	$result = mysql_query($query, $connection);
	$message = mysql_error();
	
	if($result && $vAccessType==0){
		$query="update access set access_group=null where access_id='".$vAccessId."'";
		$result = mysql_query($query, $connection);
	}
	else if($result && $vAccessType==1){
		//Querying between users of this group who has this form
		$query="select access.access_id,accessschema.accessschema_id,accessforms.accessform_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				inner join accessforms on accessforms.accessschema_id=accessschema.accessschema_id
				where access_group='".$vAccessId."' and upper(accessschema.accessschema_name)=upper('".$vAccessSchemaName."')
				and diryear='".$vAccessSchemaYear."' and upper(accessform_name)=upper('$vAccessFormName')";
		//	echo$query;exit;	
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$vAccessSchema=$row[1];
			$vAccessFormId=$row[2];
			
			$subquery="delete from conditions where accessschema_id=$vAccessSchema and accessform_id=$vAccessFormId";
			$resultsub = mysql_query($subquery, $connection);
			$message = mysql_error();
		}
	}
}

else  if($actionmode=='SubmitUserConditions'){
	$vErrorExist=0;
	
	$sub_query="select accessschema_name,diryear from accessschema where accessschema_id=$vAccessSchemaCurrentId";
	$resultsub = mysql_query($sub_query, $connection);
	$row_sub=mysql_fetch_row($resultsub);
	$vAccessSchemaName=$row_sub[0];
	$vAccessSchemaYear=$row_sub[1];
	
	$query="select upper(condition_actiontype),upper(condition_name) from conditions where condition_id=$vConditionDefaultId";
	
	$result = mysql_query($query, $connection);
	$row=mysql_fetch_row($result);
	$vConditionType=$row[0];
	$vConditionName=$row[1];
	
	if($vConditionType=='FILTER' && $vConditionDefaultValue!=''){
		$query="select count(*) from conditions where upper(condition_name)=upper('".$vConditionName."') and condition_value_default is not null and condition_value_default<>'' and condition_formtype=$vConditionFormType";
			if($vConditionId!=''){
				$query=$query." and condition_id<>'".$vConditionId."'";
			}
		$result = mysql_query($query, $connection);
		$row=mysql_fetch_row($result);
		$vCount=$row[0];

		if($vCount>0) { 
			$message=GetMsg('41',$vLanguage,'error',$connection);
			$vErrorExist=1;
		}	
	}
	
	if($vErrorExist!=1){
		$vConditionDefaultValue=GetSQLValueString($vConditionDefaultValue,'double');
		if($vConditionId==''){
			
			
			$query="insert into conditions (condition_id_default,accessschema_id,condition_actiontype,condition_type,condition_defaulttype,condition_name,
					condition_caption,condition_description,condition_fromvalue,condition_value_default,access_id,condition_formtype,condition_include)
					select $vConditionDefaultId,$vAccessSchemaCurrentId,condition_actiontype,condition_type,if($vAccessType=0,'USER','GROUP'),condition_name,
					condition_caption,condition_description,$vConditionValue,$vConditionDefaultValue,$vAccessId,$vConditionFormType,$vConditionInclude from conditions where condition_id=$vConditionDefaultId";
			//echo$query;exit;
			$result = mysql_query($query, $connection);
			$message = mysql_error();
		}
		
		else if($vConditionId!=''){
			$query="update conditions set condition_fromvalue=$vConditionValue,
					condition_value_default=$vConditionDefaultValue,
					condition_include=$vConditionInclude,
					condition_formtype=$vConditionFormType
				where condition_id=$vConditionId";
			//echo$query;exit;
			$result = mysql_query($query, $connection);
			$message = mysql_error();
		}
		
		if($result){
		
			//Querying between users of this group
			$query="select access.access_id,accessschema.accessschema_id from access 
					inner join accessschema on accessschema.access_id=access.access_id
					where access_group='".$vAccessId."' and upper(accessschema.accessschema_name)=upper('".$vAccessSchemaName."')
					and diryear='".$vAccessSchemaYear."'";
			
			$result = mysql_query($query, $connection);
			while($row=mysql_fetch_row($result)){
				$vAccessSchema=$row[1];
						
				if($vConditionId==''){		
					$subquery="insert into conditions (condition_id_default,accessschema_id,condition_actiontype,condition_type,condition_defaulttype,condition_name,
						condition_caption,condition_description,condition_fromvalue,condition_value_default,access_id,condition_formtype)
						select $vConditionDefaultId,$vAccessSchema,condition_actiontype,condition_type,'USER',condition_name,
						condition_caption,condition_description,$vConditionValue,$vConditionDefaultValue,$vAccessId,$vConditionFormType from conditions where condition_id=$vConditionDefaultId";
					
					$result = mysql_query($subquery, $connection);
				}
				else if($vConditionId!=''){	
				
					$sub_query="select upper(condition_name) from conditions where condition_id=$vConditionId";
					$resultsub = mysql_query($sub_query, $connection);
					$row_sub=mysql_fetch_row($resultsub);
					$vConditionName=$row_sub[0];
					
					$query="update conditions set condition_fromvalue=$vConditionValue,
						condition_value_default=$vConditionDefaultValue,
						condition_include=$vConditionInclude,
						condition_formtype=$vConditionFormType
						where accessschema_id='".$vAccessSchema."'
						and upper(condition_name)=upper('$vConditionName')";	
					
					//echo$query;exit;
					$result = mysql_query($query, $connection);
					$message = mysql_error();
				}
			}	
		}
	}
}

else  if($actionmode=='DeleteUserConditions'){

	//Get condition name before delete
	$sub_query="select upper(condition_name) from conditions where condition_id=$vConditionId";
	$resultsub = mysql_query($sub_query, $connection);
	$row_sub=mysql_fetch_row($resultsub);
	$vConditionName=$row_sub[0];
		
	$sub_query="select accessschema_name,diryear from accessschema where accessschema_id=$vAccessSchemaCurrentId";
	$resultsub = mysql_query($sub_query, $connection);
	$row_sub=mysql_fetch_row($resultsub);
	$vAccessSchemaName=$row_sub[0];
	$vAccessSchemaYear=$row_sub[1];
	
	$query="delete from conditions where condition_id=$vConditionId";
	$result = mysql_query($query, $connection);
	$message = mysql_error();
	
	if($result){
		
		//Querying between users of this group who has this form
		$query="select access.access_id,accessschema.accessschema_id from access 
				inner join accessschema on accessschema.access_id=access.access_id
				where access_group='".$vAccessId."' and upper(accessschema.accessschema_name)=upper('".$vAccessSchemaName."')
				and diryear='".$vAccessSchemaYear."'";
			
		$result = mysql_query($query, $connection);
		while($row=mysql_fetch_row($result)){
			$vAccessSchema=$row[1];
			
			$subquery="delete from conditions where accessschema_id=$vAccessSchema and upper(condition_name)=upper('$vConditionName')";
			//echo$subquery;
			$resultsub = mysql_query($subquery, $connection);
			$message = mysql_error();
		}
	}
}

else  if($actionmode=='LoadUserConditions'){

	$query_conditions="insert into conditions(accessapplication_id,condition_type,condition_event,condition_include,
						condition_formtype,condition_description,condition_name,condition_fromvalue,
						condition_tovalue,condition_actiontype,accessschema_id,
						accessform_id,condition_defaulttype,accessform_id_default,condition_value_default,condition_parent,Condition_caption,
						condition_index,condition_id_default) 
						
					select accessapplication_id,condition_type,condition_event,condition_include,
						condition_formtype,condition_description,condition_name,condition_fromvalue,
						condition_tovalue,condition_actiontype,$vAccessSchemaCurrentId,
						accessform_id,'User',accessform_id_default,condition_value_default,condition_parent,Condition_caption,
						condition_index,condition_id from conditions
						where upper(condition_type)=upper('User')
						and upper(condition_defaulttype)=upper('default')
						and upper(condition_actiontype)=upper('condition')
						
						and condition_id not in (select condition_id_default from conditions 
						where  upper(condition_type)=upper('User') and accessschema_id=$vAccessSchemaCurrentId
						and upper(condition_actiontype)=upper('condition'))";
	//echo$query_conditions;exit;
	$result_conditions = mysql_query($query_conditions,$connection);



}


$message=ReplaceHtmlChars($message);
$_POST['txtAccessCode']=ReplaceHtmlChars($_POST['txtAccessCode']);
$_POST['txtAccessName']=ReplaceHtmlChars($_POST['txtAccessName']);
$_POST['txtAccessCode2']=ReplaceHtmlChars($_POST['txtAccessCode2']);
$_POST['txtInactiveReason']=ReplaceHtmlChars($_POST['txtInactiveReason']);
$_POST['txtAccessEmail']=ReplaceHtmlChars($_POST['txtAccessEmail']);
$_POST['txtAccessRemark']=ReplaceHtmlChars($_POST['txtAccessRemark']);
	
		
if($actionmode=='UpdateFieldAccess')
	header ("Location: ./Form_FormsFields.php?txtAccessFormId=".$_POST["txtAccessFormId"]."&txtAccessSchemaCurrentId=".$_POST["txtAccessSchemaCurrentId"]."&txtAccessId=".$_POST["txtAccessId"]."&txtAccessType=".$_POST["txtAccessType"]."&message=".$message);
else if($actionmode=='UpdateSchemaRestrictions' || $actionmode=='DeleteSchemaRestrictions')
	header ("Location: ./Form_SchemaRestrictions.php?txtAccessSchema=".$_POST["txtAccessSchema"]."&txtAccessId=".$_POST["txtAccessId"]."&txtAccessType=".$_POST["txtAccessType"]."&message=".$message);
else if($actionmode=='SubmitFormConditions' || $actionmode=='DeleteFormConditions')
	header ("Location: ./Form_FormsConditions.php?txtAccessSchemaCurrentId=".$_POST["txtAccessSchemaCurrentId"]."&txtAccessFormId=".$_POST["txtAccessFormId"]."&txtAccessType=".$_POST["txtAccessType"]."&txtAccessId=".$_POST["txtAccessId"]."&message=".$message);	

else  if(($actionmode=='SubmitUserConditions' || $actionmode=='DeleteUserConditions' || $actionmode=='LoadUserConditions') && $vErrorExist!=1)	
		header ("Location: ./Form_UserConditions.php?txtAccessSchemaCurrentId=".$_POST["txtAccessSchemaCurrentId"]."&txtAccessType=".$_POST["txtAccessType"]."&txtActionType=".$_POST["txtActionType"]."&txtAccessId=".$_POST["txtAccessId"]."&message=".$message);	

else  if(($actionmode=='SubmitUserConditions' || $actionmode=='DeleteUserConditions' || $actionmode=='LoadUserConditions') && $vErrorExist==1)	
		header ("Location: ./Form_UserConditions.php?actionmode=error&txtAccessSchemaCurrentId=".$_POST["txtAccessSchemaCurrentId"]."&txtAccessType=".$_POST["txtAccessType"]."&txtActionType=".$_POST["txtActionType"]."&txtAccessId=".
				"".$_POST["txtAccessId"]."&txtConditionId=".$_POST["txtConditionId"]."&txtActionType=".$_POST["txtActionType"]."&txtCaption=".$_POST["txtCaption"]."&txtConditionValue=".
				"".$_POST["txtConditionValue"]."&txtConditionDefaultValue=".$_POST["txtConditionDefaultValue"]."&txtInclude=".$_POST["txtInclude"]."&txtCondition=".$_POST["txtCondition"]."&txtConditionFormType=".
				"".$_POST["txtConditionFormType"]."&message=".$message);	
		
else if($mainmode=='UpdateApplicationAccess' || $mainmode=='UpdateSchemaAccess')
	header ("Location: ./Form_Access.php?txtAccessId=".$_POST["txtAccessId"]."&txtAccessSchema=".$_POST["txtAccessSchema"]."&txtApplication=".$_POST["txtApplication"]."&vTabFocus=Company/Applications&message=".$message);		

else if($mainmode=='delete')
	header ("Location: ./Form_AccessSearch.php?message=".$message);
	
	
	
else if(($mainmode=='insert' || $mainmode=='edit') && $vErrorExist==1)
	header ("Location: ./Form_Access.php?mainmode=".$_POST['mainmode']."&txtAccessCode=".$_POST['txtAccessCode']."&txtAccessName=".$_POST['txtAccessName']."&txtAccessType=".
"".$_POST['txtAccessType']."&txtAccessActive=".$_POST['txtAccessActive']."&txtAccessCode2=".$_POST['txtAccessCode2']."&txtFromDate=".
"".$_POST['txtFromDate']."&txtToDate=".$_POST['txtToDate']."&txtInactiveReason=".$_POST['txtInactiveReason']."&txtAccessPassword=".
"".$_POST['txtAccessPassword']."&txtBelongsToGrp=".$_POST['txtBelongsToGrp']."&txtAccessConfirmPassword=".$_POST['txtAccessConfirmPassword']."&txtAccessShift=".
"".$_POST['txtAccessShift']."&txtAccessLanguage=".$_POST['txtAccessLanguage']."&txtAccessDefaultSchema=".$_POST['txtAccessDefaultSchema']."&txtAccessEmail=".
"".$_POST['txtAccessEmail']."&txtAccessRemark=".$_POST['txtAccessRemark']."&txtAccessDefaultYear=".$_POST['txtAccessDefaultYear']."&txtAccessGroup=".
"".$_POST['txtAccessGroup']."&message=".$message);

else	
header ("Location: ./Form_Access.php?mainmode=edit&txtAccessId=$vAccessId&txtAccessSchema=".$_POST["txtAccessSchema"]."&txtApplication=".$_POST["txtApplication"]."&txtAccessSchemaCurrent=".$_POST["txtAccessSchemaCurrent"]."&txtAccessSchemaCurrentId=".$_POST["txtAccessSchemaCurrentId"]."&txtAccessApplicationCurrent=".$_POST["txtAccessApplicationCurrent"]."&txtAccessApplicationCurrentId=".$_POST["txtAccessApplicationCurrentId"]."&txtKeyword=".$_POST["txtKeyword"]."&message=".$message);
