<?php
include('includes/functions.php'); # general functions
include('includes/imgFunctions.php'); # functions for resize, watermark

define  ("MAX_SIZE","2000000");
function getExtension($str) {
	$i = strrpos ($str,".");
	if (!$i) { return ""; }
	$l = strlen ($str) - $i;
   	$ext = substr ($str,$i+1,$l);
    return $ext;
}

if(isset($_SESSION['adminId']) && trim($_SESSION['adminId']) >0) {
	$id = trim($_SESSION['adminId']);
	$query = mysqli_query($con, "SELECT * FROM tbl_admin WHERE admin_id='$id'");
	$resEdit = mysqli_fetch_array($query);	
	$ed_admin_name = $resEdit['name'];
	$ed_admin_email = $resEdit['email'];	
	
	//get data from settings table
	$setRec = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_settings WHERE id = '1'"));
	
	$adminRec = mysqli_fetch_array(mysqli_query($con, "SELECT email FROM tbl_admin WHERE admin_id = '1'"));
	$admin_email = $adminRec['email'];
}




if(isset($_POST['update'])) {

	if(isset($_POST['operator_commission']) && trim($_POST['operator_commission']) !=''){
	$operator_commission = mysqli_real_escape_string($con, $_POST['operator_commission']); } else {
	$error = 'Enter operator commission percent'; }

	if(isset($_POST['admin_commission_admin']) && trim($_POST['admin_commission_admin']) !=''){
	$admin_commission_admin = mysqli_real_escape_string($con, $_POST['admin_commission_admin']); } else {
	$error = 'Enter admin commission percent (Admin Dispatch)'; }

	if(isset($_POST['admin_commission_direct']) && trim($_POST['admin_commission_direct']) !=''){
	$admin_commission_direct = mysqli_real_escape_string($con, $_POST['admin_commission_direct']); } else {
	$error = 'Enter admin commission percent (Direct)'; }
	
	if(isset($_POST['working_days']) && trim($_POST['working_days']) !=''){
		$working_days = mysqli_real_escape_string($con, $_POST['working_days']); 
		if(is_numeric($working_days) && $working_days>0 && $working_days<31){
			//ok
		} else {
			$error = 'Please enter a numeric value between 0 and 31 for working days';
		}
	} else {
	$error = 'Enter Working Days Count'; }
	
	if(isset($_POST['allow_advance_sick_leave']) && trim($_POST['allow_advance_sick_leave']) !=''){
	$allow_advance_sick_leave = mysqli_real_escape_string($con, $_POST['allow_advance_sick_leave']); } else {
	$error = 'Allow Advance Sick Leave? Select Yes/No'; }
	
	if(isset($_POST['sick_leave_count']) && trim($_POST['sick_leave_count']) !=''){
	$sick_leave_count = mysqli_real_escape_string($con, $_POST['sick_leave_count']); } else {
	$error = 'Enter Total Sick Leaves Per Year'; }
	
	if(isset($_POST['allow_advance_leave']) && trim($_POST['allow_advance_leave']) !=''){
	$allow_advance_leave = mysqli_real_escape_string($con, $_POST['allow_advance_leave']); } else {
	$error = 'Allow Advance Leave? Select Yes/No'; }
	
	if(isset($_POST['casual_leave_count']) && trim($_POST['casual_leave_count']) !=''){
	$casual_leave_count = mysqli_real_escape_string($con, $_POST['casual_leave_count']); } else {
	$error = 'Enter Total Casual Leaves Per Year'; }
	
	if(isset($_POST['paypal_config']) && trim($_POST['paypal_config']) !=''){
	$paypal_config = mysqli_real_escape_string($con, $_POST['paypal_config']); } else {
	$error = 'Select PayPal Config'; }
	
	if(isset($_POST['cca_config']) && trim($_POST['cca_config']) !=''){
	$cca_config = mysqli_real_escape_string($con, $_POST['cca_config']); } else {
	$error = 'Select CCAvenue Config'; }
	
	if(isset($_POST['pg_choice']) && trim($_POST['pg_choice']) !=''){
	$pg_choice = mysqli_real_escape_string($con, $_POST['pg_choice']); } else {
	$error = 'Select Payment Gateway Choice'; }	
	
	if(isset($_POST['color_scheme']) && trim($_POST['color_scheme']) !=''){
	$color_scheme = mysqli_real_escape_string($con, $_POST['color_scheme']); } else {
	$error = 'Select color scheme'; }	
	
		if(isset($_POST['enable_live_chat']) && trim($_POST['enable_live_chat']) !=''){
	$enable_live_chat = mysqli_real_escape_string($con, $_POST['enable_live_chat']); } else {
	$error = 'Select live chat Enable/Disable'; }
	
	if(isset($_POST['enable_online_payment']) && trim($_POST['enable_online_payment']) !=''){
	$enable_online_payment = mysqli_real_escape_string($con, $_POST['enable_online_payment']); } else {
	$error = 'Select Online Payment Enable/Disable'; }
	
	/*if(isset($_POST['name']) && trim($_POST['name']) !=''){
	$name = mysqli_real_escape_string($con, $_POST['name']); } else {
	$error = 'Enter Admin Name'; }*/
	
	if(isset($_POST['admin_email']) && trim($_POST['admin_email']) !=''){
		$admin_email = mysqli_real_escape_string($con, $_POST['admin_email']);
		$normal =  "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i";
		if (preg_match($normal, $admin_email)) {

		} else {
			$error = 'Please enter a valid notification email address';
		} 	
	} else {
		$error = 'Enter notification email address';
	}
	
	if(isset($_POST['from_email']) && trim($_POST['from_email']) !=''){
		$from_email = mysqli_real_escape_string($con, $_POST['from_email']);
		$normal =  "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i";
		if (preg_match($normal, $from_email)) {

		} else {
			$error = 'Please enter a valid from address';
		} 	
	} else {
		$error = 'Enter Admin From Email Id';
	}

	if(isset($_POST['copyright']) && trim($_POST['copyright']) !=''){
	$copyright = mysqli_real_escape_string($con, $_POST['copyright']); } else {
	$error = 'Enter Copyright text'; }
	
	if(isset($_FILES['logo']) && $_FILES['logo']['size'] > 0) {		
		if($_FILES['logo']['type'] == 'image/jpeg' || $_FILES['logo']['type'] == 'image/png' || $_FILES['logo']['type'] == 'image/pjpeg') {
			$logo_name = $_FILES['logo']['name'];			
			$logo_temp_name = $_FILES['logo']['tmp_name'];
			$logo_extension = pathinfo($logo_name, PATHINFO_EXTENSION);
		} else {
			$logo_name = FALSE;
			$logo_temp_name = FALSE;
			$error = "Invalid Image type for logo. Only jpg and png allowed";
		}
	}

	if(isset($_POST['site_name']) && trim($_POST['site_name']) !=''){
	$site_name = mysqli_real_escape_string($con, $_POST['site_name']); } else {
	$error = 'Enter Site Name'; }

	/*if(isset($_POST['email']) && trim($_POST['email']) !=''){
		$email = mysqli_real_escape_string($con, $_POST['email']);
		$normal =  "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i";
		if (preg_match($normal, $email)) {
			$chk = "SELECT `admin_id` FROM `tbl_admin` WHERE `email` = '".$email."' AND `admin_id` <> $id";
			$rs = mysqli_query($con, $chk);
			if(mysqli_num_rows($rs)>0){
				$error = "The email id already exists.."; 
			}
		} else {
			 $error = 'Please enter a valid email';
		}
	} else {
		$error = 'Enter Admin Email Address';
	}*/
	
	

	if(empty($error)) {
		
		$update = mysqli_query($con, "UPDATE tbl_admin SET `email` = '".$admin_email."' WHERE admin_id = '1'");
													
		$update1 = mysqli_query($con, "UPDATE tbl_settings SET `from_email` = '".$from_email."',
															   `cca_config` = '".$cca_config."',
															   `pg_choice` = '".$pg_choice."',
															   `paypal_config` ='".$paypal_config."',
															   `casual_leave_count` = '".$casual_leave_count."',
															   `allow_advance_leave` = '".$allow_advance_leave."',
															   `allow_advance_sick_leave` = '".$allow_advance_sick_leave."',
															   `sick_leave_count` = '".$sick_leave_count."',
															   `working_days` = '".$working_days."',
															   `operator_commission` = '".$operator_commission."',
															   `admin_commission_direct` = '".$admin_commission_direct."',
															   `admin_commission_admin` = '".$admin_commission_admin."',
															   `site_name` = '".$site_name."',
															   `copyright` = '".$copyright."',
															   `enable_online_payment` = '".$enable_online_payment."',
															   enable_live_chat = '".$enable_live_chat."',
															   color_scheme = '".$color_scheme."' WHERE id = '1'");
		$result = '';
		if($update1 != false) {
			$result =  true;
		}		
		
		if(isset($_FILES['logo']) && $_FILES['logo']['size'] > 0) {
			$logo_dir = LOGO_IMG_FOLDER;
			$logo_name = 'logo-'.time().'.'.$logo_extension; 
			if(move_uploaded_file($logo_temp_name, $logo_dir.$logo_name)){
				//resize2(LOGO_IMG_FOLDER.$logo_name, 200, 150, LOGO_IMG_FOLDER.'thumb/'.$logo_name);						
				mysqli_query($con, "UPDATE tbl_settings SET logo = '".$logo_name."' WHERE id = '1'");	
				$result =  true;					
			}			
		}

		if($result == true) {
		?>
			<script language="javascript">
			alert("Updated successfully");
			window.location="index.php?page=settings";			
			</script>
		<?php //exit;		
		}
	}
}

if(isset($_POST['submit_change_password'])) {
		
	if(strlen(trim($_POST['oldpwd'])) > 0) {		
		$oldpwd = trim($_POST['oldpwd']);		
	} else {
		$oldpwd = FALSE;
		$errorPW = "Please enter old password";		
	}
	if(strlen(trim($_POST['newpwd1'])) > 0) {		
		$newpwd1 = trim($_POST['newpwd1']);		
	} else {
		$newpwd1 = FALSE;
		$errorPW = "Please enter new password";		
	}
	if(strlen(trim($_POST['newpwd2'])) > 0) {		
		$newpwd2 = trim($_POST['newpwd2']);
	} else {
		$newpwd2 = FALSE;
		$errorPW = "Please enter new password";
	}
	if(!($newpwd1===$newpwd2)) {
		$errorPW = " Password Mismatch ";	
		$newpwd1 = FALSE;
	}
	
	if($oldpwd && $newpwd1) {
		$chkUserQry = "SELECT ad_username , ad_password FROM tbl_admin WHERE ad_username = '" . mysql_real_escape_string($username) ."' AND ad_password = MD5('$oldpwd')";
		//echo  $chkUserQry;
		$execUserQry = mysql_query( $chkUserQry);
		//$resUserQry = mysql_fetch_array($execUserQry);
		
		if(mysql_num_rows($execUserQry) > 0) {
	
			$UpdatePassQry = "UPDATE  tbl_admin SET ad_password = MD5('$newpwd1')  WHERE   ad_username = '" . mysql_real_escape_string($username) ."'"; 
			//echo $UpdatePassQry;
			$execPassQry = mysql_query($UpdatePassQry);
			if($execPassQry) {
				?>
				<SCRIPT LANGUAGE="JAVASCRIPT">
					alert("Password Changed successfully");
					window.location="index.php?page=home";
				</SCRIPT>
			<?php
			} 
 		} else {
			$errorPW = "username / password incorrect";
 		}
	}
}	


if(isset($_POST['update_cat'])) {
	
	$errorCat = '';
	
	if(isset($_POST['hourly_rate']) && trim($_POST['hourly_rate']) !=''){
		$hourly_rate = mysqli_real_escape_string($con, $_POST['hourly_rate']); 
		if(is_numeric($hourly_rate) && $hourly_rate>0){
			//ok
		} else {
			$errorCat = 'Please enter a numeric value for hourly rate';
		}
	} else {
	$errorCat = 'Enter Hourly Rate'; }
	
	if(isset($_POST['max_charge']) && trim($_POST['max_charge']) !=''){
		$max_charge = mysqli_real_escape_string($con, $_POST['max_charge']); 
		if(is_numeric($max_charge) && $max_charge>0){
			//ok
		} else {
			$errorCat = 'Please enter a numeric value for maximum charge';
		}
	} else {
	$errorCat = 'Enter Maximum Charge'; }
	
	if(isset($_POST['min_charge']) && trim($_POST['min_charge']) !=''){
		$min_charge = mysqli_real_escape_string($con, $_POST['min_charge']); 
		if(is_numeric($min_charge) && $min_charge>0){
			//ok
		} else {
			$errorCat = 'Please enter a numeric value for minimum charge';
		}
	} else {
	$errorCat = 'Enter Minimum Charge'; }
	
	if(isset($_POST['visit_charge']) && trim($_POST['visit_charge']) !=''){
		$visit_charge = mysqli_real_escape_string($con, $_POST['visit_charge']); 
		if(is_numeric($visit_charge)){
			//ok
		} else {
			$errorCat = 'Please enter a numeric value for visit charge';
		}
	} else {
	$errorCat = 'Enter Visit Charge'; }

	if(empty($errorCat)) {
		
		$update = mysqli_query($con, "UPDATE tbl_settings SET `visit_charge` = '".$visit_charge."', `min_charge` = '".$min_charge."', `max_charge` = '".$max_charge."', `hourly_rate` = '".$hourly_rate."' WHERE id = '1'");
		if($update) {	?>
			<script language="javascript">
			alert("Charges updated successfully");
			window.location="index.php?page=settings#categories";			
			</script>
		<?php //exit;		
		}
	}
}	


if(isset($_POST['update_cat_direct'])) {
	
	$errorCatDirect = '';
	
	if(isset($_POST['hourly_rate_direct']) && trim($_POST['hourly_rate_direct']) !=''){
		$hourly_rate_direct = mysqli_real_escape_string($con, $_POST['hourly_rate_direct']); 
		if(is_numeric($hourly_rate_direct) && $hourly_rate_direct>0){
			//ok
		} else {
			$errorCatDirect = 'Please enter a numeric value for hourly rate';
		}
	} else {
	$errorCatDirect = 'Enter Hourly Rate'; }
	
	if(isset($_POST['min_charge_direct']) && trim($_POST['min_charge_direct']) !=''){
		$min_charge_direct = mysqli_real_escape_string($con, $_POST['min_charge_direct']); 
		if(is_numeric($min_charge_direct) && $min_charge_direct>0){
			//ok
		} else {
			$errorCatDirect = 'Please enter a numeric value for minimum charge';
		}
	} else {
	$errorCatDirect = 'Enter Minimum Charge'; }
	
	if(isset($_POST['visit_charge_direct']) && trim($_POST['visit_charge_direct']) !=''){
		$visit_charge_direct = mysqli_real_escape_string($con, $_POST['visit_charge_direct']); 
		if(is_numeric($visit_charge_direct)){
			//ok
		} else {
			$errorCatDirect = 'Please enter a numeric value for visit charge';
		}
	} else {
	$errorCatDirect = 'Enter Visit Charge'; }

	if(empty($errorCatDirect)) {
		
		$update = mysqli_query($con, "UPDATE tbl_settings SET `visit_charge_direct` = '".$visit_charge_direct."', `min_charge_direct` = '".$min_charge_direct."', `hourly_rate_direct` = '".$hourly_rate_direct."' WHERE id = '1'");
		if($update) {	?>
			<script language="javascript">
			alert("Direct assign category charges updated successfully");
			window.location="index.php?page=settings#categories_direct";			
			</script>
		<?php //exit;		
		}
	}
}


if(isset($_POST['update_special'])) {
	
	$errorSpecial = '';
	
	if(isset($_POST['extra_charge_holiday']) && trim($_POST['extra_charge_holiday']) !=''){
		$extra_charge_holiday = mysqli_real_escape_string($con, $_POST['extra_charge_holiday']); 
		if(is_numeric($extra_charge_holiday) && $extra_charge_holiday>0 && $extra_charge_holiday<=100){
			//ok
		} else {
			$errorSpecial = 'Please enter a valid % value for Holiday extra';
		}
	} else {
	$errorSpecial = 'Enter Minimum Charge'; }
	
	if(isset($_POST['extra_charge_night']) && trim($_POST['extra_charge_night']) !=''){
		$extra_charge_night = mysqli_real_escape_string($con, $_POST['extra_charge_night']); 
		if(is_numeric($extra_charge_night) && $extra_charge_night>0 && $extra_charge_night<=100){
			//ok
		} else {
			$errorSpecial = 'Please enter a valid % value for Night extra';
		}
	} else {
	$errorSpecial = 'Enter Minimum Charge'; }	

	if(empty($errorSpecial)) {
		
		$update = mysqli_query($con, "UPDATE tbl_settings SET `extra_charge_night` = '".$extra_charge_night."', `extra_charge_holiday` = '".$extra_charge_holiday."' WHERE id = '1'");
		if($update) {	?>
			<script language="javascript">
			alert("Special charges updated successfully");
			window.location="index.php?page=settings#special_charge";			
			</script>
		<?php //exit;		
		}
	}
}	


if(isset($_POST['update_referral'])) {
	
	$errorReferral = '';
	
	if(isset($_POST['referee_commission_amount']) && trim($_POST['referee_commission_amount']) !=''){
		$referee_commission_amount = mysqli_real_escape_string($con, $_POST['referee_commission_amount']); 				
	} else {
		$referee_commission_amount = 0;
	}
	
	if(isset($_POST['referee_commission']) && trim($_POST['referee_commission']) !=''){
		$referee_commission = mysqli_real_escape_string($con, $_POST['referee_commission']); 		
		if($referee_commission == 'Yes'){
			if(isset($_POST['referee_commission_amount']) && trim($_POST['referee_commission_amount']) !=''){
				$referee_commission_amount = mysqli_real_escape_string($con, $_POST['referee_commission_amount']); 
				if(is_numeric($referee_commission_amount) && $referee_commission_amount>0){
					//ok
				} else {
					$errorReferral = 'Please enter a valid referee commission amount';
				}		
			} else {
				$errorReferral = 'Please enter referee commission amount';
			}
		}		
	} else {
		$errorReferral = 'Please select referee commission option';
	}
	
	
	if(isset($_POST['referrer_commission_amount']) && trim($_POST['referrer_commission_amount']) !=''){
		$referrer_commission_amount = mysqli_real_escape_string($con, $_POST['referrer_commission_amount']); 				
	} else {
		$referrer_commission_amount = 0;
	}
	
	if(isset($_POST['referrer_commission']) && trim($_POST['referrer_commission']) !=''){
		$referrer_commission = mysqli_real_escape_string($con, $_POST['referrer_commission']); 		
		if($referrer_commission == 'Yes'){
			if(isset($_POST['referrer_commission_amount']) && trim($_POST['referrer_commission_amount']) !=''){
				$referrer_commission_amount = mysqli_real_escape_string($con, $_POST['referrer_commission_amount']); 
				if(is_numeric($referrer_commission_amount) && $referrer_commission_amount>0){
					//ok
				} else {
					$errorReferral = 'Please enter a valid referral commission amount';
				}		
			} else {
				$errorReferral = 'Please enter referral commission amount';
			}
		}		
	} else {
		$errorReferral = 'Please select referral commission option';
	}
	
	if(empty($error)) {
		
		$update = mysqli_query($con, "UPDATE tbl_settings SET `referrer_commission` = '".$referrer_commission."', `referrer_commission_amount` = '".$referrer_commission_amount."', `referee_commission` = '".$referee_commission."', `referee_commission_amount` = '".$referee_commission_amount."' WHERE id = '1'");
		if($update) {	?>
			<script language="javascript">
			alert("Referral settings updated successfully");
			window.location="index.php?page=settings#referral";			
			</script>
		<?php //exit;		
		}
	}
}	


if(isset($_POST['update_support'])) {
	
	$errorSupport = '';
	
	/*if(isset($_POST['support_no_shop']) && trim($_POST['support_no_shop']) !=''){
		$support_no_shop = mysqli_real_escape_string($con, $_POST['support_no_shop']); 
		if((strlen($support_no_shop)>9 && strlen($support_no_shop)<=13) && substr_count($support_no_shop, ' ')==3){
			//ok
			$num_arr = explode(" ", $support_no_shop);
			foreach($num_arr as $num) {
				if(!is_numeric($num)) {
					$errorSupport = 'Enter a valid phone number for shop support without spaces';
					break;
				}
			}
		} else {
			$errorSupport = 'Enter a valid phone number for shop support without spaces';
		}		
	} else {
	$support_no_shop = ''; }*/
	
	if(isset($_POST['tech_support_email']) && strlen($_POST['tech_support_email'])>0) {
		$tech_support_email = trim($_POST['tech_support_email']);
		if (!filter_var($tech_support_email, FILTER_VALIDATE_EMAIL)) {
		  	$errorSupport = "Invalid tech support email";
		}
	} else {
		$tech_support_email = '';
	}
	
	if(isset($_POST['user_support_email']) && strlen($_POST['user_support_email'])>0) {
		$user_support_email = trim($_POST['user_support_email']);
		
		if (!filter_var($user_support_email, FILTER_VALIDATE_EMAIL)) {
		  	$errorSupport = "Invalid user support email";
		}
	} else {
		$user_support_email = '';
	}
	
	if(isset($_POST['support_no_tech']) && trim($_POST['support_no_tech']) !=''){
		$support_no_tech = mysqli_real_escape_string($con, $_POST['support_no_tech']); 
		if((strlen($support_no_tech)>9 && strlen($support_no_tech)<=13) && (substr_count($support_no_tech, ' ')==3 || substr_count($support_no_tech, ' ')==0)){
			//ok
			if (strpos($support_no_tech, ' ') !== false) {
				$num_arr = explode(" ", $support_no_tech);
				foreach($num_arr as $num) {
					if(!is_numeric($num)) {
						$errorSupport = 'Enter a valid phone number for tech support without spaces';
						break;
					}
				}
			}
		} else {
			$errorSupport = 'Enter a valid phone number for tech support without spaces';
		}		
	} else {
	$support_no_tech = ''; }
	
	if(isset($_POST['support_no_user']) && trim($_POST['support_no_user']) !=''){
		$support_no_user = mysqli_real_escape_string($con, $_POST['support_no_user']);		
	
		if((strlen($support_no_user)>9 && strlen($support_no_user)<=13) && (substr_count($support_no_user, ' ')==3 || substr_count($support_no_user, ' ')==0)){
			//ok
			if (strpos($support_no_user, ' ') !== false) {
				$num_arr = explode(" ", $support_no_user);
				foreach($num_arr as $num) {
					if(!is_numeric($num)) {
						$errorSupport = 'Enter a valid phone number for user support without spaces';
						break;
					}
				}
			}
		} else {
			$errorSupport = 'Enter a valid phone number for user support without spaces';
		}		
	} else {
	$support_no_user = ''; }
	
	

	if(empty($errorSupport)) {
		
		$update = mysqli_query($con, "UPDATE tbl_settings SET `support_no_user` = '".$support_no_user."', `support_no_tech` = '".$support_no_tech."', `user_support_email` = '".$user_support_email."', tech_support_email='".$tech_support_email."' WHERE id = '1'");
		if($update) {	?>
			<script language="javascript">
			alert("Support Numbers updated successfully");
			window.location="index.php?page=settings#support";			
			</script>
		<?php //exit;		
		}
	}
}


if(isset($_POST['update_social'])) {
	
	$errorSocial = '';
	
	if(isset($_POST['gplus']) && trim($_POST['gplus']) !=''){
		$gplus = mysqli_real_escape_string($con, $_POST['gplus']); 
		if (filter_var($gplus, FILTER_VALIDATE_URL) === FALSE) {
			$errorSocial = 'Please enter a valid gplus link';
		}		
	} else {
	$gplus = ''; }
	
	if(isset($_POST['twitter']) && trim($_POST['twitter']) !=''){
		$twitter = mysqli_real_escape_string($con, $_POST['twitter']); 
		if (filter_var($twitter, FILTER_VALIDATE_URL) === FALSE) {
			$errorSocial = 'Please enter a valid twitter link';
		}		
	} else {
	$twitter = ''; }
	
	if(isset($_POST['fb']) && trim($_POST['fb']) !=''){
		$fb = mysqli_real_escape_string($con, $_POST['fb']); 
		if (filter_var($fb, FILTER_VALIDATE_URL) === FALSE) {
			$errorSocial = 'Please enter a valid facebook link';
		}		
	} else {
	$fb = ''; }

	if(empty($errorSocial)) {
		
		$update = mysqli_query($con, "UPDATE tbl_social SET `fb` = '".$fb."', `twitter` = '".$twitter."', `gplus` = '".$gplus."' WHERE id = '1'");
		if($update) {	?>
			<script language="javascript">
			alert("Social Links updated successfully");
			window.location="index.php?page=settings#social";			
			</script>
		<?php //exit;		
		}
	}
}	

if(isset($_POST['update_app'])) {
	
	$errorApp = '';
	
	if(isset($_POST['app_tech_privacy_url']) && trim($_POST['app_tech_privacy_url']) !=''){
		$app_tech_privacy_url = mysqli_real_escape_string($con, $_POST['app_tech_privacy_url']); 		
	} else {
		$app_tech_privacy_url = '';
	}
	
	if(isset($_POST['app_user_privacy_url']) && trim($_POST['app_user_privacy_url']) !=''){
		$app_user_privacy_url = mysqli_real_escape_string($con, $_POST['app_user_privacy_url']); 		
	} else {
		$app_user_privacy_url = '';
	}
	
	if(isset($_POST['android_tech_app_link']) && trim($_POST['android_tech_app_link']) !=''){
		$android_tech_app_link = mysqli_real_escape_string($con, $_POST['android_tech_app_link']); 		
	} else {
		$android_tech_app_link = '';
	}
	
	if(isset($_POST['android_user_app_link']) && trim($_POST['android_user_app_link']) !=''){
		$android_user_app_link = mysqli_real_escape_string($con, $_POST['android_user_app_link']); 		
	} else {
		$android_user_app_link = '';
	}
	
	if(isset($_POST['share_link_android']) && trim($_POST['share_link_android']) !=''){
		$share_link_android = mysqli_real_escape_string($con, $_POST['share_link_android']); 		
	} else {
		$share_link_android = '';
	}
	
	if(isset($_POST['share_text_android']) && trim($_POST['share_text_android']) !=''){
		$share_text_android = mysqli_real_escape_string($con, $_POST['share_text_android']); 		
	} else {
		$share_text_android = '';
	}

	if(isset($_POST['ios_tech_app_link']) && trim($_POST['ios_tech_app_link']) !=''){
		$ios_tech_app_link = mysqli_real_escape_string($con, $_POST['ios_tech_app_link']); 		
	} else {
		$ios_tech_app_link = '';
	}
	
	if(isset($_POST['ios_user_app_link']) && trim($_POST['ios_user_app_link']) !=''){
		$ios_user_app_link = mysqli_real_escape_string($con, $_POST['ios_user_app_link']); 		
	} else {
		$ios_user_app_link = '';
	}
	
	if(isset($_POST['share_link_ios']) && trim($_POST['share_link_ios']) !=''){
		$share_link_ios = mysqli_real_escape_string($con, $_POST['share_link_ios']); 		
	} else {
		$share_link_ios = '';
	}
	
	if(isset($_POST['share_text_ios']) && trim($_POST['share_text_ios']) !=''){
		$share_text_ios = mysqli_real_escape_string($con, $_POST['share_text_ios']); 		
	} else {
		$share_text_ios = '';
	}

	if(empty($errorApp)) {
		
		$update = mysqli_query($con, "UPDATE tbl_settings SET 
		`share_text_ios` = '".$share_text_ios."', 
		`share_link_ios` = '".$share_link_ios."', 
		`ios_user_app_link` = '".$ios_user_app_link."',
		`ios_tech_app_link` = '".$ios_tech_app_link."', 
		`share_text_android` = '".$share_text_android."', 
		`share_link_android` = '".$share_link_android."', 
		`android_user_app_link` = '".$android_user_app_link."',
		`android_tech_app_link` = '".$android_tech_app_link."',
		`app_user_privacy_url` = '".$app_user_privacy_url."',
		`app_tech_privacy_url` = '".$app_tech_privacy_url."'  
		WHERE id = '1'");
		if($update) {	?>
			<script language="javascript">
			alert("App settings updated successfully");
			window.location="index.php?page=settings#apps";			
			</script>
		<?php //exit;		
		}
	}
}	

if(isset($_POST['update_notes'])) {
	
	if(isset($_POST['service_terms']) && trim($_POST['service_terms']) !=''){
		$serviceTerms = mysqli_real_escape_string($con, $_POST['service_terms']);				
	} else {
	$serviceTerms = ''; }
	
	if(isset($_POST['project_terms']) && trim($_POST['project_terms']) !=''){
		$projectTerms = mysqli_real_escape_string($con, $_POST['project_terms']);				
	} else {
	$projectTerms = ''; }
	
	if(isset($_POST['task_terms']) && trim($_POST['task_terms']) !=''){
		$taskTerms = mysqli_real_escape_string($con, $_POST['task_terms']);				
	} else {
	$taskTerms = ''; }
	
	if(isset($_POST['service_category_note']) && trim($_POST['service_category_note']) !=''){
		$serviceCatNote = mysqli_real_escape_string($con, $_POST['service_category_note']);				
	} else {
	$serviceCatNote = ''; }
	
	if(isset($_POST['project_category_note']) && trim($_POST['project_category_note']) !=''){
		$projectCatNote = mysqli_real_escape_string($con, $_POST['project_category_note']);				
	} else {
	$projectCatNote = ''; }
	

		
		$update = mysqli_query($con, "UPDATE tbl_settings SET `project_category_note` = '".$projectCatNote."', `service_category_note` = '".$serviceCatNote."', `task_terms` = '".$taskTerms."', `project_terms` = '".$projectTerms."', `service_terms` = '".$serviceTerms."' WHERE id = '1'");
		if($update) {	?>
			<script language="javascript">
			alert("Notes updated successfully");
			window.location="index.php?page=settings#notes";			
			</script>
		<?php //exit;		
		}
	
}	


$socialRec = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_social WHERE id = '1'"));

?>