<?php

if (isset($__N__)) return(false);
$__N__ = true;

function showData($hData, $usevertical = true, $title = "", $maximum = 300, $rActions = null) {
    if (!$hData) return;
    if ($usevertical && mysql_num_rows($hData) == 1) {
        $rData = mysql_fetch_array($hData, MYSQL_ASSOC);
        printArray($rData);
        return;
    } echo "<b>" . mysql_num_rows($hData) . "</b> total result rows retrieved.<br>";
    echo "<table border=0 bordercolor=black cellspacing=1 cellpadding=1 bgcolor=#808080>\n";
    if ($rActions == null) $rActions = array();
    if ($title != "") echo "<caption bgcolor=white>$title</caption>";
    echo "\t<tr bgcolor=#E0E0E0>\n";
    $fields = mysql_num_fields($hData);
    $names = array();
    for ($k = 0; $k < $fields; $k++) {
        $names[$k] = mysql_field_name($hData, $k);
        echo "\t\t<td nowrap><b>" . $names[$k] . "</td>\n";
    } echo "\t</tr>\n";
    while ($data = mysql_fetch_array($hData)) {
        if ($i++ > $maximum) {
            echo "<tr><td colspan=$fields align=left bgcolor=#C0C0C0>(listing discontinued at $maximum rows)</td></tr>";
            break;
        } echo "\t<tr bgcolor=#F0F0F0>\n";
        for ($k = 0; $k < $fields; $k++) {
            $fieldValue = (strlen($data[$k]) < 500 ? htmlspecialchars($data[$k]) : htmlspecialchars(substr($data[$k], 0, 500)) . " .....");
            if ($rActions[$names[$k]] != "") {
                $url = $rActions[$names[$k]];
                for ($i = 0; $i < $fields; $i++) {
                    $url = str_replace('$' . $names[$k], $data[$k], $url);
                } echo "\t\t<td nowrap><a href=\"" . $url . "\">" . $fieldValue . "</a></td>\n";
            } else {
                echo "\t\t<td nowrap>" . $fieldValue . "</td>\n";
            }
        } echo "\t</tr>\n";
    } echo "</table>\n";
}

function printArray($rArray, $ignoreNumericIndeces = true, $useIndexing = false, $asRow = false, $keysMatching = "") {
    if (!is_array($rArray)) {
        echo "printArray: nothing to print\n";
        return;
    } echo "<table border=0 bordercolor=black cellspacing=1 cellpadding=1 bgcolor=#808080><!-- printArray -->\n";
    $arrayCount = count($rArray);
    if ($arrayCount < 1) {
        echo "<tr><td nowrap bgcolor=#E8E8E8>(no values)</td></tr>\n";
        return;
    } if ($useIndexing) {
        if ($asRow) {
            echo "<tr>";
            for ($i = 0; $i < $arrayCount; $i++) {
                echo "\t<td nowrap bgcolor=#E8E8E8><b>$i</td>\n";
            } echo "</tr>\n</tr>\n";
            for ($i = 0; $i < $arrayCount; $i++) {
                echo "\t<td nowrap bgcolor=#F8F8F8>" . $rArray[$i] . "</td>\n";
            } echo "</tr>\n";
        } else {
            for ($i = 0; $i < $arrayCount; $i++) {
                echo "<tr>";
                echo "\t\t<td nowrap bgcolor=#E8E8E8><b>$i</td>\n";
                echo "\t\t<td nowrap bgcolor=#F8F8F8>" . $rArray[$i] . "</td>\n";
                echo "</tr>\n";
            }
        }
    } else {
        if ($asRow) {
            echo "<tr>";
            foreach ($rArray as $index => $value) {
                if ($ignoreNumericIndeces) {
                    $i = intval($index);
                    if ($i > 0 && $i < $arrayCount) continue;
                    if ($index == "0") continue;
                } echo "\t\t<td nowrap bgcolor=#E8E8E8><b>$index</td>\n";
            } echo "</tr>\n<tr>\n";
            foreach ($rArray as $index => $value) {
                if ($ignoreNumericIndeces) {
                    $i = intval($index);
                    if ($i > 0 && $i < $arrayCount) continue;
                    if ($index == "0") continue;
                } if ($keysMatching != "") if (!eregi($keysMatching, $index)) continue;
                echo "\t<td nowrap bgcolor=#F8F8F8>$value</td>\n";
            } echo "</tr>\n";
        } else {
            foreach ($rArray as $index => $value) {
                if ($ignoreNumericIndeces) {
                    $i = intval($index);
                    if ($i > 0 && $i < $arrayCount) continue;
                    if ($index == "0") continue;
                } if ($keysMatching != "") if (!eregi($keysMatching, $index)) continue;
                echo "<tr>";
                echo "\t\t<td nowrap bgcolor=#E8E8E8><b>$index</td>\n";
                echo "\t\t<td nowrap bgcolor=#F8F8F8>$value</td>\n";
                echo "</tr>\n";
            }
        }
    } echo "</table>\n";
}

function printObject(&$rObject) {
    print "<pre style='color: #000000; background: #F0F0F0; border: #808080 1px solid; padding: 4px;'>\n\n";
    print_r($rObject);
    echo "\n<!--";
    var_dump($rObject);
    echo "\n-->";
    print "</pre>\n";
}

function cellValue($value) {
    return $value == "" ? " " : $value;
}

function isInArray($value, $theArray) {
    if (count($theArray) == 0) return false;
    foreach ($theArray as $element) {
        if ($value == $element) return true;
    } return false;
}

function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
}

function IsValidEmail($email) {
    return( eregi("@", $email) && eregi("\\.", $email) );
}

function DateString($theString, $input = "MySQL") {
    if ($input == "MySQL") {
        return intval(substr($theString, 5, 2)) . "/" . intval(substr($theString, 8, 2)) . "/" . intval(substr($theString, 0, 4));
    }
}

function getHomeURL() {
    global $HTTP_HOST, $PHP_SELF;
    return "http://" . $HTTP_HOST . substr($PHP_SELF, 0, strpos($PHP_SELF, "/", 1));
}

function SQL_AutoExecutor($transID = 0, $AutoClean = false) {
    global $PHP_SELF;
    $successful = 0;
    $UNsuccessful = 0;
    $hSQLs = mysql_query("SELECT ID, cmd FROM sql WHERE transID='$transID' AND executed='N' ORDER BY ID");
    while ($rSQL = mysql_fetch_array($hSQLs)) {
        if (mysql_query($rSQL['cmd'])) {
            $successful++;
            if ($AutoClean) {
                mysql_query("DELETE FROM sql WHERE ID=" . $rSQL['ID']);
            } else {
                mysql_query("UPDATE sql SET executed='Y', successful='Y' WHERE ID=" . $rSQL['ID']);
            }
        } else {
            $UNsuccessful++;
            LogError($rSQL['cmd']);
            $errID = GetLastInsertID("err");
            mysql_query("UPDATE sql SET executed='Y', successful='N', ref3='$errID' WHERE ID=" . $rSQL['ID']);
        }
    } return $successful;
}

function array_add(&$array, $value) {
    $array[] = $value;
    end($array);
    $newKey = key($array);
    reset($array);
    return $newKey;
}

function HiddenFields($FieldList, $ValueList = null, $appendName = "", $separator = "", $FieldNamesPrepend = "", $stripRequestSlashes = false) {
    
    if (!is_array($FieldList)) $FieldList = array($FieldList);
    $fieldString = "";
    if (isset($ValueList)) {
        if (!is_array($ValueList)) $ValueList = array($ValueList);
        if (@isset($g_Debugging) && $g_Debugging) echo "<b>using VALUELIST...</b><br>";
        foreach ($FieldList as $fieldName => $fieldValue) {
            $fieldString .= "<input type=hidden name=\"" . $fieldValue . $appendName . "\" value=\"" . htmlspecialchars($ValueList[$fieldName]) . "\">" . $separator;
        }
    } else {
        if (@isset($g_Debugging) && $g_Debugging) {
            echo "<b>using REQUEST...</b><br>";
            DumpInfo($FieldList, "HiddenFields");
        } foreach ($FieldList as $fieldName) {
            $fieldName = $FieldNamesPrepend . $fieldName;
            if (is_array($_POST[$fieldName])) {
                if (@isset($g_Debugging) && $g_Debugging) echo "is_array(HTTP_POST_VARS:$fieldName)<br>";foreach ($_POST[$fieldName] as $fieldValue) {
                    if ($stripRequestSlashes) $fieldValue = stripslashes($fieldValue);
                    $fieldString .= "<input type=hidden name=\"" . $fieldName . $appendName . "\" value=\"" . htmlspecialchars($fieldValue) . "\">" . $separator;
                }
            } elseif (is_array($_GET[$fieldName])) {
                if (@isset($g_Debugging) && $g_Debugging) echo "is_array(HTTP_GET_VARS:$fieldName)<br>";
                foreach ($_GET[$fieldName] as $fieldValue) {
                    if ($stripRequestSlashes) $fieldValue = stripslashes($fieldValue);
                    $fieldString .= "<input type=hidden name=\"" . $fieldName . $appendName . "\" value=\"" . htmlspecialchars($fieldValue) . "\">" . $separator;
                }
            } else {
                $fieldValue = $_POST[$fieldName] != "" ? $_POST[$fieldName] : $_GET[$fieldName];
                if (@isset($g_Debugging) && $g_Debugging) echo "REGULAR:$fieldValue<br>";
                if ($stripRequestSlashes) $fieldValue = stripslashes($fieldValue);
                $fieldString .= "<input type=hidden name=\"" . $fieldName . $appendName . "\" value=\"" . htmlspecialchars($fieldValue) . "\">" . $separator;
            }
        }
    } if (@isset($g_Debugging) && $g_Debugging) echo "!!!!!!!" . htmlspecialchars($fieldString) . "<br>\n";
    return $fieldString;
}

function QueryString($FieldList, $leadingChar = "&", $stripRequestSlashes = false) {
    $paramString = $leadingChar;
    foreach ($FieldList as $name) {
        $fieldValue = "";
        if (isset($_GET[$name])) {
            $fieldValue = $_GET[$name];
            if ($stripRequestSlashes) $fieldValue = stripslashes($fieldValue);
        } elseif (isset($_POST[$name])) {
            $fieldValue = $_POST[$name];
            if ($stripRequestSlashes) $fieldValue = stripslashes($fieldValue);
        } elseif (isset($GLOBALS[$name])) {
            $fieldValue = $GLOBALS[$name];
        } $paramString .= urlencode($name) . "=" . urlencode($fieldValue) . "&";
    } $paramString = substr($paramString, 0, strlen($paramString) - 1);
    return $paramString;
}

function RequestFieldNames($pattern = ".", $bRemovePattern = false) {
    $rMatches = array();
    $POST_FieldNames = array_keys($_POST);
    $GET_FieldNames = array_keys($_GET);
    MatchingKeys($POST_FieldNames, $rMatches, $pattern, $bRemovePattern);
    MatchingKeys($GET_FieldNames, $rMatches, $pattern, $bRemovePattern);
    return $rMatches;
}

function MatchingKeys(&$CheckArray, &$KeysArray, $pattern, $bRemovePattern = false) {
    for ($i = 0; $i < count($CheckArray); $i++) {
        if (eregi($pattern, $CheckArray[$i])) {
            if ($bRemovePattern) {
                $KeysArray[] = eregi_replace($pattern, "", $CheckArray[$i]);
            } else {
                $KeysArray[] = $CheckArray[$i];
            }
        }
    }
}

function AVD($theArray, $theKey, $theDefault = "") {
    return $theArray[$theKey] != "" ? $theArray[$theKey] : $theDefault;
}

function DumpInfo($obj, $msg = "") {
    echo "<pre>" . ($msg != "" ? $msg . " -- " : "") . "DumpInfo:<br><blockquote style='margin-left: 20px; background: #F8F8F0; border: #0040A0 1px solid;'>";
    var_dump($obj);
    echo "</blockquote></pre>";
}

function ScriptedEmailLink($email, $text = "", $subject = "", $body = "", $extraAnchorParms = "") {
    if ($text == "") {
        $text = str_replace("@", "@", $email);
        $text = str_replace(".", ".", $text);
        $text = str_replace("'", "'", $text);
        $text = str_replace("\"", '"',$text); } $parts = split("@",$email); if( count($parts) < 2 ) { return "<a href = \"javascript: window.open('mailto:" . $email . "');\" onMouseOver=\"window.status='$text;'\" onMouseOut=\"window.status=''; return true;\" $extraAnchorParms>$text</a>";
    } else {
        return "<a href=\"#\" onClick=\"window.open('mailto:" . $parts[0] . "'+'@'+'" . $parts[1] . "'); return false;\" onMouseOver=\"window.status='$text'; return true;\" onMouseOut=\"window.status=''; return true;\" $extraAnchorParms>$text</a>";
    }
}

function WriteLn($text) {
    echo $text . "<br>\n";
}

function WriteHidden($text) {
    if (is_array($text)) {
        echo "<!--\n\n";
        foreach ($text as $key => $value)
            echo $key . ": " . $value . "\n";
        echo "\n\n-->";
    } else {
        echo "<!--\n\n" . $text . "\n\n-->";
    }
}

function AdjustColorHTML($HtmlColor, $percent, $adjCode = "B") {
    $htmlcol = strlen($HtmlColor) > 6 ? substr($HtmlColor, 1, 6) : $HtmlColor;
    if (strlen($htmlcol) != 6) return $HtmlColor;
    $adj = strtoupper(substr($adjCode, 0, 1));
    $r1 = hexdec(substr($htmlcol, 0, 2));
    $g1 = hexdec(substr($htmlcol, 2, 2));
    $b1 = hexdec(substr($htmlcol, 4, 2));
    if ($adjCode == "C") {
        
    } elseif ($adjCode == "R") {
        
    } elseif ($adjCode == "G") {
        
    } elseif ($adjCode == "B") {
        
    } else {
        
    } $r2 = hexdec($r2);
    if (strlen($r2) < 2) $r2 = "0" . $r2;
    $g2 = hexdec($g2);
    if (strlen($g2) < 2) $g2 = "0" . $g2;
    $b2 = hexdec($b2);
    if (strlen($b2) < 2) $b2 = "0" . $b2;
    return "#" . $r2 . $g2 . $b2;
}

function MoneyFormat($amount, $bDollarSign = true) {
    if ($bDollarSign) {
        return "\$" . sprintf("%0.2f", $amount);
    } else {
        return sprintf("%0.2f", $amount);
    }
}

function LimitChars($fieldValue, $maxlength, $fullHover = true, $isHTML = true, $elipsis = "...", $css_class = "text", $css_style = "") {
    if (strlen($fieldValue) <= $maxlength) return $isHTML ? htmlspecialchars($fieldValue) : $fieldValue;
    $shortVal = substr($fieldValue, 0, $maxlength) . $elipsis;
    if (!$fullHover) return $isHTML ? htmlspecialchars($shortVal) : $shortVal;
    return "<a " . ($css_class == "" ? "" : "class='" . $css_class . "' ") . ($css_style == "" ? "" : "style='" . $css_style . "' ") . "title=\"" . str_replace("\r", "", str_replace("\n", "", htmlspecialchars($fieldValue))) . "\">" . htmlspecialchars($shortVal) . "</a>";
}

function Buffering_Start() {
    ob_start();
}

function & Buffering_Stop() {
    $contents = ob_get_contents();
    ob_end_clean();
}

function & Run_Buffered($code) {
    Buffering_Start();
    eval($code);
    $contents = & Buffering_Stop();
    return $contents;
}

