function confirmDeduct($_POST)
{
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($deduction, "string", 1, 100, "Invalid deduction name.");
    $v->isOk($creditor, "string", 1, 100, "Invalid creditor name.");
    $v->isOk($refno, "string", 1, 20, "Invalid reference number.");
    $v->isOk($accid, "num", 1, 20, "Invalid Account Number.");
    $v->isOk($expaccid, "num", 1, 20, "Invalid Expense Account Number.");
    $v->isOk($details, "string", 0, 100, "Invalid creditor details.");
    $v->isOk($type, "string", 1, 15, "Invalid type.");
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirmCust .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    core_connect();
    if ($creditor == "In House") {
        $sql = "SELECT accname FROM accounts WHERE accid='{$expaccid}'";
        $rslt = db_exec($sql) or errDie("Error reading account name for comfirmation.");
        if (pg_num_rows($rslt) < 1) {
            return "<li class='err'>Expense Account selected is invalid.</li>";
        } else {
            $accname = pg_fetch_result($rslt, 0, 0);
        }
    } else {
        $sql = "SELECT accname FROM accounts WHERE accid='{$accid}'";
        $rslt = db_exec($sql) or errDie("Error reading account name for comfirmation.");
        if (pg_num_rows($rslt) < 1) {
            return "<li class='err'>Account selected is invalid.</li>";
        } else {
            $accname = pg_fetch_result($rslt, 0, 0);
        }
    }
    db_connect();
    if ($key == "confirm") {
        $scale_from = array();
        $scale_to = array();
        $scale_amount = array();
        $get_scales = "SELECT * FROM salded_scales WHERE saldedid = (SELECT id FROM salded WHERE refno = '{$refno}' LIMIT 1)";
        $run_scales = db_exec($get_scales) or errDie("Unable to get salary deduction information.");
        if (pg_numrows($run_scales) > 0) {
            while ($darr = pg_fetch_array($run_scales)) {
                $scale_from[] = $darr['scale_from'];
                $scale_to[] = $darr['scale_to'];
                $scale_amount[] = $darr['scale_amount'];
            }
        }
    }
    if (isset($type) and $type == "Percentage") {
        # get current scales added
        foreach ($scale_from as $each => $own) {
            $own += 0;
            $scale_to[$each] += 0;
            $scale_amount[$each] += 0;
            # check for zero values
            if ($scale_to[$each] == "0" or $scale_amount[$each] == "0") {
                continue;
            }
            # first value can be zero, but cant then be greater than the to value
            if ($own >= $scale_to[$each]) {
                continue;
            }
            if (isset($remove_scale) and is_array($remove_scale)) {
                $rem = array_keys($remove_scale);
                if ($each == $rem[0]) {
                    continue;
                }
            }
            $scales_hidden .= "\n\t\t\t\t<input type='hidden' name='scale_from[]' value='{$own}'>\n\t\t\t\t<input type='hidden' name='scale_to[]' value='{$scale_to[$each]}'>\n\t\t\t\t<input type='hidden' name='scale_amount[]' value='{$scale_amount[$each]}'>";
            $scales_list .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$own}</td>\n\t\t\t\t\t<td>{$scale_to[$each]}</td>\n\t\t\t\t\t<td>{$scale_amount[$each]} <input type='submit' name='remove_scale[{$each}]' value='Remove'></td>\n\t\t\t\t</tr>";
        }
        # check for cross linked pairs
        if (!isset($new_scale_from) or !isset($new_scale_to) or !isset($new_scale_amount) or empty($new_scale_from) or empty($new_scale_to) or empty($new_scale_amount) or search_scale_array($scale_from, $scale_to, $new_scale_from) or search_scale_array($scale_from, $scale_to, $new_scale_to)) {
            $scale_error = "<tr><td colspan='3'><li class='err'>Duplicate Or Overlapping Scale Exists</li></td></tr>";
        } else {
            $scale_error = "";
            $scales_hidden .= "\n\t\t\t\t<input type='hidden' name='scale_from[]' value='{$new_scale_from}'>\n\t\t\t\t<input type='hidden' name='scale_to[]' value='{$new_scale_to}'>\n\t\t\t\t<input type='hidden' name='scale_amount[]' value='{$new_scale_amount}'>";
            $scales_list .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$new_scale_from}</td>\n\t\t\t\t\t<td>{$new_scale_to}</td>\n\t\t\t\t\t<td>{$new_scale_amount} <input type='submit' name='remove_scale[" . ($each + 1) . "]' value='Remove'></td>\n\t\t\t\t</tr>";
        }
        $scales_display = "\n\t\t\t{$scales_hidden}\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<th colspan='3'>Percentage Deduction Scales</th>\n\t\t\t</tr>\n\t\t\t{$scale_error}\n\t\t\t<tr>\n\t\t\t\t<th>From Amount</th>\n\t\t\t\t<th>To Amount</th>\n\t\t\t\t<th>Percentage</th>\n\t\t\t</tr>\n\t\t\t{$scales_list}\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><input type='text' size='5' name='new_scale_from'></td>\n\t\t\t\t<td><input type='text' size='5' name='new_scale_to'></td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='text' size='5' name='new_scale_amount'>\n\t\t\t\t\t<input type='submit' name='submit_scale' value='Add'>\n\t\t\t\t</td>\n\t\t\t</tr>";
    } else {
        $scales_display = "";
    }
    $confirmDeduct = "\n\t\t<h3>Confirm new salary deduction</h3>\n\t\t<form action='" . SELF . "' method='POST'>\n\t\t\t<input type='hidden' name='key' value='write'>\n\t\t\t<input type='hidden' name='deduction' value='{$deduction}'>\n\t\t\t<input type='hidden' name='creditor' value='{$creditor}'>\n\t\t\t<input type='hidden' name='refno' value='{$refno}'>\n\t\t\t<input type='hidden' name='accid' value='{$accid}'>\n\t\t\t<input type='hidden' name='expaccid' value='{$expaccid}'>\n\t\t\t<input type='hidden' name='details' value='{$details}'>\n\t\t\t<input type='hidden' name='type' value='{$type}'>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Field</th>\n\t\t\t\t<th>Value</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Name of deduction</td>\n\t\t\t\t<td align='center'>{$deduction}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Creditor name</td>\n\t\t\t\t<td align='center'>{$creditor}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Reference no.</td>\n\t\t\t\t<td align='center'>{$refno}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Account Number</td>\n\t\t\t\t<td align='center'>{$accname}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Creditor details</td>\n\t\t\t\t<td align='center'>{$details}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Deduction Type</td>\n\t\t\t\t<td align='center'>{$type}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' align='right'><input type='submit' name='submit_ded' value='Write &raquo;'></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t{$scales_display}\n\t\t</table>\n\t\t</form>\n\t\t<br>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $confirmDeduct;
}
function confirmDeduct($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($deduction, "string", 1, 100, "Invalid deduction name.");
    $v->isOk($creditor, "string", 1, 100, "Invalid creditor name.");
    $v->isOk($refno, "string", 1, 20, "Invalid reference number.");
    $v->isOk($catid, "string", 1, 20, "Invalid Category number.");
    $v->isOk($details, "string", 0, 100, "Invalid creditor details.");
    $v->isOk($taxable, "string", 1, 3, "Invalid taxablility option.");
    $v->isOk($type, "string", 1, 15, "Invalid type.");
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirmCust .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    # connect to db
    core_connect();
    // 	$dedacc= "<select name='accid' style='width: 230'>";
    // 		$sql = "SELECT * FROM accounts WHERE catid = '$catid' AND div = '".USER_DIV."'";
    // 		$accRslt = db_exec($sql);
    // 		$numrows = pg_numrows($accRslt);
    // 		if(empty($numrows)){
    // 			$paid = "There are no Balance accounts yet in Cubit.";
    // 		}else{
    // 			$prevtop = "";
    // 			while($acc = pg_fetch_array($accRslt)){
    // 				if(isb($acc['accid'])) {
    // 					continue;
    // 				}
    // 				if ( $acc["topacc"] == $prevtop && $acc["accnum"] != "000" ) {
    // 					$x = "&nbsp;&nbsp;-&nbsp;&nbsp;$acc[topacc]/$acc[accnum]";
    // 				} else {
    // 					$x = "$acc[topacc]/$acc[accnum]";
    // 					$prevtop = $acc["topacc"];
    // 				}
    // 				if (isset($accid) AND $accid == $acc['accid']){
    // 					$dedacc .= "<option value='$acc[accid]' selected>$x $acc[accname]</option>";
    // 				}else {
    // 					$dedacc .= "<option value='$acc[accid]'>$x $acc[accname]</option>";
    // 				}
    // 			}
    // 		}
    // 	$dedacc .= "</select>";
    // Expense account
    // 	$expacc = "<select name='expaccid' style='width: 230'>";
    // 	$sql = "SELECT * FROM accounts WHERE (catid='E10' OR catid='I10')AND div='".USER_DIV."'";
    // 	$expRslt = db_exec($sql);
    // 	$prevtop = "";
    // 	while($acc = pg_fetch_array($expRslt)) {
    // 		if(isb($acc['accid'])) {
    // 			continue;
    // 		}
    // 		if ( $acc["topacc"] == $prevtop && $acc["accnum"] != "000" ) {
    // 			$x = "&nbsp;&nbsp;-&nbsp;&nbsp;$acc[topacc]/$acc[accnum]";
    // 		} else {
    // 			$x = "$acc[topacc]/$acc[accnum]";
    // 			$prevtop = $acc["topacc"];
    // 		}
    // 		if (isset ($expaccid) AND $expaccid == $acc['accid']){
    // 			$expacc .= "<option value='$acc[accid]' selected>$x $acc[accname]</option>";
    // 		}else {
    // 			$expacc .= "<option value='$acc[accid]'>$x $acc[accname]</option>";
    // 		}
    // 	}
    if (isset($type) and $type == "Percentage") {
        # get current scales added
        foreach ($scale_from as $each => $own) {
            $own += 0;
            $scale_to[$each] += 0;
            $scale_amount[$each] += 0;
            # check for zero values
            if ($scale_to[$each] == "0" or $scale_amount[$each] == "0") {
                continue;
            }
            # first value can be zero, but cant then be greater than the to value
            if ($own >= $scale_to[$each]) {
                continue;
            }
            if (isset($remove_scale) and is_array($remove_scale)) {
                $rem = array_keys($remove_scale);
                if ($each == $rem[0]) {
                    continue;
                }
            }
            $scales_hidden .= "\n\t\t\t\t<input type='hidden' name='scale_from[]' value='{$own}'>\n\t\t\t\t<input type='hidden' name='scale_to[]' value='{$scale_to[$each]}'>\n\t\t\t\t<input type='hidden' name='scale_amount[]' value='{$scale_amount[$each]}'>";
            $scales_list .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$own}</td>\n\t\t\t\t\t<td>{$scale_to[$each]}</td>\n\t\t\t\t\t<td>{$scale_amount[$each]} <input type='submit' name='remove_scale[{$each}]' value='Remove'></td>\n\t\t\t\t</tr>";
        }
        # check for cross linked pairs
        if (!isset($new_scale_from) or !isset($new_scale_to) or !isset($new_scale_amount) or empty($new_scale_from) or empty($new_scale_to) or empty($new_scale_amount) or search_scale_array($scale_from, $scale_to, $new_scale_from) or search_scale_array($scale_from, $scale_to, $new_scale_to)) {
            $scale_error = "<tr><td colspan='3'><li class='err'>Duplicate Or Overlapping Scale Exists</li></td></tr>";
        } else {
            $scale_error = "";
            $scales_hidden .= "\n\t\t\t\t<input type='hidden' name='scale_from[]' value='{$new_scale_from}'>\n\t\t\t\t<input type='hidden' name='scale_to[]' value='{$new_scale_to}'>\n\t\t\t\t<input type='hidden' name='scale_amount[]' value='{$new_scale_amount}'>";
            $scales_list .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$new_scale_from}</td>\n\t\t\t\t\t<td>{$new_scale_to}</td>\n\t\t\t\t\t<td>{$new_scale_amount} <input type='submit' name='remove_scale[" . ($each + 1) . "]' value='Remove'></td>\n\t\t\t\t</tr>";
        }
        $scales_display = "\n\t\t\t{$scales_hidden}\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<th colspan='3'>Percentage Deduction Scales</th>\n\t\t\t</tr>\n\t\t\t{$scale_error}\n\t\t\t<tr>\n\t\t\t\t<th>From Amount</th>\n\t\t\t\t<th>To Amount</th>\n\t\t\t\t<th>Percentage</th>\n\t\t\t</tr>\n\t\t\t{$scales_list}\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><input type='text' size='5' name='new_scale_from'></td>\n\t\t\t\t<td><input type='text' size='5' name='new_scale_to'></td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='text' size='5' name='new_scale_amount'>\n\t\t\t\t\t<input type='submit' name='submit_scale' value='Add'>\n\t\t\t\t</td>\n\t\t\t</tr>";
    } else {
        $scales_display = "";
    }
    $confirmDeduct = "\n\t\t<h3>Confirm new Salary Deduction</h3>\n\t\t<form action='" . SELF . "' method='POST'>\n\t\t\t<input type='hidden' name='key' value='write'>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<input type='hidden' name='deduction' value='{$deduction}'>\n\t\t\t<input type='hidden' name='creditor' value='{$creditor}'>\n\t\t\t<input type='hidden' name='refno' value='{$refno}'>\n\t\t\t<input type='hidden' name='details' value='{$details}'>\n\t\t\t<input type='hidden' name='taxable' value='{$taxable}'>\n\t\t\t<input type='hidden' name='type' value='{$type}'>\n\t\t\t<input type='hidden' name='catid' value='{$catid}'>\n\t\t\t<tr>\n\t\t\t\t<th>Field</th>\n\t\t\t\t<th>Value</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Name of deduction</td>\n\t\t\t\t<td align='center'>{$deduction}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Creditor name</td>\n\t\t\t\t<td align='center'>{$creditor}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Reference no.</td>\n\t\t\t\t<td align='center'>{$refno}</td>\n\t\t\t</tr>";
    if ($creditor == "In House") {
        $confirmDeduct .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Expense Account</td>\n\t\t\t\t<td align='center'>" . mkAccSelect("expaccid", $expaccid, ACCTYPE_IE) . "</td>\n\t\t\t</tr>\n\t\t\t<input type='hidden' name='accid' value='0'>";
    } else {
        $confirmDeduct .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Creditor Account</td>\n\t\t\t\t<td align='center'>" . mkAccSelect("accid", $accid, ACCTYPE_B) . "</td>\n\t\t\t</tr>\n\t\t\t<input type='hidden' name='expaccid' value='0'>";
    }
    $confirmDeduct .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Creditor details</td>\n\t\t\t\t<td align='center'>{$details}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Deduct Before PAYE</td>\n\t\t\t\t<td align='center'>{$taxable}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Deduction Type</td>\n\t\t\t\t<td align='center'>{$type}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' align='right'><input type='submit' name='submit_ded' value='Write &raquo;'></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t{$scales_display}\n\t\t</table>\n\t\t</form>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $confirmDeduct;
}