<?php
error_reporting(E_ALL);
require "auth_enduser.phph";
require "/nfscommon/html/admin/bam/uma_header_oracle.phph";
require "oci.phph";

class Host {
    public $datacenter, $host, $domain;

    public function __construct($dc, $host, $domain) {
        $this->host = $host;
        $this->domain = $domain;
        $this->datacenter = $dc;
    }
}


class CudaList {
    private $oddeven, $cstruct, $mstruct, $default, $default_class, $sorted;

    public function __construct($iAddress) {
        $tmpvar = split("\.", $iAddress);
        $this->oddeven = ((int)$tmpvar[3] % 2);

        $this->default = new Host("AM", "filtervipam11.rightnowtech.com",
            "custhelp.com");
        $this->default_class = "RNT";

        $this->cstruct = array();
        $this->mstruct = array();

        $this->sorted = False;
    }

    public function add($dc, $class, $host1, $host2, $domain) {
        # Structure for class
        if (! isset($this->cstruct[$dc])) {
            $this->cstruct[$dc] = array();
        }

        # Structure for domain
        if (! isset($this->mstruct[$domain])) {
            $this->mstruct[$domain] = array();
        }

        if ($this->oddeven) {
            $host = $host1;
        } else {
            $host = $host2;
        }

        $this->cstruct[$dc][$class] = new Host($dc, $host, $domain);
        $this->mstruct[$domain][$class] = new Host($dc, $host, $domain);
    }

    public function host($reply_to, $dc, $class) {
        $tmpvar = split("@", $reply_to);
        $acct = $tmpvar[0];
        $domain = $tmpvar[1];

        if (isset($this->mstruct[$domain][$class])) {
            return $this->mstruct[$domain][$class];
        } elseif (isset($this->cstruct[$dc][$class])) {
            return $this->cstruct[$dc][$class];
        } else {
            return $this->default;
        }
    }
        
    public function get($reply_to, $dc, $class) {
        $retval = "";
        // Sort array if it's not already sorted
        if (! $this->sorted) {
            ksort($this->cstruct);
        }

        $tmpvar = split("@", $reply_to);
        $acct = $tmpvar[0];
        $domain = $tmpvar[1];

        // Determine the domain name we will use
        if (isset($this->mstruct[$domain])) {
            // Use from reply_to first
            $localdomain = $domain;
        } elseif (isset( $this->cstruct[$dc][$class])) {
            // Use from mailbox data center second
            $localdomain = $this->cstruct[$dc][$class]->domain;
        } else {
            // Fall back to default
            $localdomain = $this->default->domain;
        }


        $fallback_ct = 0;
        while (list ($datacenter, $value) = each ($this->cstruct)) {
            while (list($myclass, $machine) = each ($value)) {
                if ($class != $myclass) {
                    continue;
                }
                $localclass = $class;
                if ($myclass != $class) {
                    $localclass = $this->default_class;
                }

                if (isset($value[$localclass])) {
                    $fallback_ct += 1;
                    // Class is defined for that data center
                    if ($machine->domain == $localdomain) {
                        $retval = sprintf(
                            "%s<option value=\"%s\" selected=\"selected\">%s@%s</option>\n",
                            $retval, htmlentities(sprintf("%s:%s:%s@%s", $value[$localclass]->host, $value[$localclass]->datacenter, $acct, $machine->domain)),
                            htmlentities($acct), htmlentities($machine->domain));
                    } else {
                        $retval = sprintf(
                            "%s<option value=\"%s\" >%s@%s</option>\n",
                            $retval, htmlentities(sprintf("%s:%s:%s@%s", $value[$localclass]->host, $value[$localclass]->datacenter, $acct, $machine->domain)),
                            htmlentities($acct), htmlentities($machine->domain));
                    }
                }
            }

        }
        if ($fallback_ct == 0) {
            $retval = sprintf(
                "%s<option value=\"%s\" >%s@%s</option>\n",
                $retval, htmlentities(sprintf("%s:%s:%s@%s", $this->default->host, $this->default->datacenter, $acct, $this->default->domain)),
                htmlentities($acct), htmlentities($this->default->domain));
        }
            

        return $retval;
    }
}

// select distinct(concat(concat(d.name, ' '),  c.description)) as info from class c, datacenters d , sites s, servers sv where s.class_id = c.class_id and s.database_server_id = sv.server_id and sv.datacenter_id = d.datacenter_id order by 1;
$machines = new CudaList($_SERVER["REMOTE_ADDR"]);
$machines->add("AM", "RNT", "filteram11.rightnowtech.com", "filteram12.rightnowtech.com", "mailam.custhelp.com");
$machines->add("AM", "SNT", "filteram11.rightnowtech.com", "filteram12.rightnowtech.com", "mailam.custhelp.com");

#$machines->add("YH", "RNT", "filterca21.rightnowtech.com", "filterca22.rightnowtech.com", "mailyh.custhelp.com");

$machines->add("UPS", "RNT", "filtermw13.rightnowtech.com", "filtermw14.rightnowtech.com", "mailups.custhelp.com");

$machines->add("AP", "JetHot", "filtersy11.rightnowtech.com", "filtersy12.rightnowtech.com", "mailap.custhelp.com");
$machines->add("AP", "Mitsui", "filterjp11.rightnowtech.com", "filterjp12.rightnowtech.com", "e-srvc.com");
$machines->add("AP", "Okisoft", "filtersy11.rightnowtech.com", "filtersy12.rightnowtech.com", "mailap.custhelp.com");
$machines->add("AP", "RNT", "filtersy11.rightnowtech.com", "filtersy12.rightnowtech.com", "mailap.custhelp.com");
$machines->add("AP", "RNT-Japan", "filtersy11.rightnowtech.com", "filtersy12.rightnowtech.com", "mailap.custhelp.com");

$machines->add("AP_legacy", "JetHot", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailap-legacy.custhelp.com");
$machines->add("AP_legacy", "Mitsui", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "e-srvc-legacy.com");
$machines->add("AP_legacy", "Okisoft", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailap-legacy.custhelp.com");
$machines->add("AP_legacy", "RNT", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailap-legacy.custhelp.com");
$machines->add("AP_legacy", "RNT-Japan", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailap-legacy.custhelp.com");

$machines->add("JP", "JetHot", "filterjp11.rightnowtech.com", "filterjp12.rightnowtech.com", "mailjp.custhelp.com");
$machines->add("JP", "Mitsui", "filterjp11.rightnowtech.com", "filterjp12.rightnowtech.com", "e-srvc.com");
$machines->add("JP", "Okisoft", "filterjp11.rightnowtech.com", "filterjp12.rightnowtech.com", "mailjp.custhelp.com");
$machines->add("JP", "RNT", "filterjp11.rightnowtech.com", "filterjp12.rightnowtech.com", "mailjp.custhelp.com");
$machines->add("JP", "RNT-Japan", "filterjp11.rightnowtech.com", "filterjp12.rightnowtech.com", "mailjp.custhelp.com");

$machines->add("SY", "JetHot", "filtersy11.rightnowtech.com", "filtersy12.rightnowtech.com", "mailsy.custhelp.com");
$machines->add("SY", "Okisoft", "filtersy11.rightnowtech.com", "filtersy12.rightnowtech.com", "mailsy.custhelp.com");
$machines->add("SY", "RNT", "filtersy11.rightnowtech.com", "filtersy12.rightnowtech.com", "mailsy.custhelp.com");
$machines->add("SY", "RNT-Japan", "filtersy11.rightnowtech.com", "filtersy12.rightnowtech.com", "mailsy.custhelp.com");

$machines->add("SG", "JetHot", "filtersg11.rightnowtech.com", "filtersg12.rightnowtech.com", "mailsg.custhelp.com");
$machines->add("SG", "Okisoft", "filtersg11.rightnowtech.com", "filtersg12.rightnowtech.com", "mailsg.custhelp.com");
$machines->add("SG", "RNT", "filtersg11.rightnowtech.com", "filtersg12.rightnowtech.com", "mailsg.custhelp.com");
$machines->add("SG", "RNT-Japan", "filtersg11.rightnowtech.com", "filtersg12.rightnowtech.com", "mailsg.custhelp.com");

$machines->add("CA", "Convergys", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "email.esecurecare.net");
$machines->add("CA", "HIPPA", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailca.custhelp.com");
$machines->add("CA", "Internal", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailca.custhelp.com");
$machines->add("CA", "Mitsui", "filterjp11.rightnowtech.com", "filterjp12.rightnowtech.com", "e-srvc.com");
$machines->add("CA", "QA", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailca.custhelp.com");
$machines->add("CA", "RNT", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailca.custhelp.com");
$machines->add("CA", "Social Demo", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailca.custhelp.com");

$machines->add("CA_legacy", "Convergys", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "email-legacy.esecurecare.net");
$machines->add("CA_legacy", "HIPPA", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailca-legacy.custhelp.com");
$machines->add("CA_legacy", "Internal", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailca-legacy.custhelp.com");
$machines->add("CA_legacy", "Mitsui", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "e-srvc-legacy.com");
$machines->add("CA_legacy", "QA", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailca-legacy.custhelp.com");
$machines->add("CA_legacy", "RNT", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailca-legacy.custhelp.com");
$machines->add("CA_legacy", "Social Demo", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailca-legacy.custhelp.com");

#$machines->add("EU_legacy", "RNT", "filtergb11.rightnowtech.com", "filtergb11.rightnowtech.com", "maileu-legacy.custhelp.com");

$machines->add("EU", "RNT", "filtergb13.rightnowtech.com", "filtergb14.rightnowtech.com", "maileu.custhelp.com");

#$machines->add("GB_legacy", "RNT", "filtergb11.rightnowtech.com", "filtergb11.rightnowtech.com", "mailgb-legacy.custhelp.com");
#$machines->add("GB_legacy", "SNT", "filtergb11.rightnowtech.com", "filtergb11.rightnowtech.com", "mailgb-legacy.custhelp.com");

$machines->add("GB", "RNT", "filtergb13.rightnowtech.com", "filtergb14.rightnowtech.com", "mailgb.custhelp.com");
$machines->add("GB", "SNT", "filtergb13.rightnowtech.com", "filtergb14.rightnowtech.com", "mailgb.custhelp.com");

#TODO, get these domains base on new edomains tables, hack for now
if (in_array($_SESSION["auth_company_id"], array(115964, 114364))) {
    $machines->add("MW", "RNT", "filtervipmw15.rightnowtech.com", "filtervipmw15.rightnowtech.com", "service.amexgbt.com");
} else {
    $machines->add("MW", "HIPPA", "filtermw15.rightnowtech.com", "filtermw16.rightnowtech.com", "mailmw.custhelp.com");
    $machines->add("MW", "RNT", "filtermw15.rightnowtech.com", "filtermw16.rightnowtech.com", "mailmw.custhelp.com");
}

$machines->add("NJ", "RNT", "filtermw15.rightnowtech.com", "filtermw16.rightnowtech.com", "mailnj.custhelp.com");
$machines->add("FG", "RNT", "filtervipfg11.rightnowtech.com", "filtervipfg11.rightnowtech.com", "mailfg.custhelp.com");
$machines->add("FG", "US-PS", "filtervipfg11.rightnowtech.com", "filtervipfg11.rightnowtech.com", "mailfg.custhelp.com");
$machines->add("FD", "RNT", "filtervipfd11.rightnowtech.com", "filtervipfd11.rightnowtech.com", "mailfd.custhelp.com");
$machines->add("FD", "US-PS", "filtervipfd11.rightnowtech.com", "filtervipfd11.rightnowtech.com", "mailfd.custhelp.com");

$machines->add("PX", "RNT", "filtervippx11.rightnowtech.com", "filtervippx11.rightnowtech.com", "mailpx.custhelp.com");
#$machines->add("MW_legacy", "HIPPA", "filtermw11.rightnowtech.com", "filtermw12.rightnowtech.com", "mailmw-legacy.custhelp.com");
#$machines->add("MW_legacy", "RNT", "filtermw11.rightnowtech.com", "filtermw12.rightnowtech.com", "mailmw-legacy.custhelp.com");
#
#$machines->add("NJ_legacy", "RNT", "filtermw11.rightnowtech.com", "filtermw12.rightnowtech.com", "mailnj-legacy.custhelp.com");

$machines->add("PC_legacy", "HIPPA", "filterpc01.rightnowtech.com", "filterpc02.rightnowtech.com", "mailpc-legacy.custhelp.com");
$machines->add("PC_legacy", "PCI", "filterpc01.rightnowtech.com", "filterpc02.rightnowtech.com", "mailpc-legacy.custhelp.com");
$machines->add("PC_legacy", "RNT", "filterpc01.rightnowtech.com", "filterpc02.rightnowtech.com", "mailpc-legacy.custhelp.com");


$machines->add("PC", "HIPPA", "filtervippc03.rightnowtech.com", "filtervippc03.rightnowtech.com", "mailpc.custhelp.com");
$machines->add("PC", "PCI", "filtervippc03.rightnowtech.com", "filtervippc03.rightnowtech.com", "mailpc.custhelp.com");
$machines->add("PC", "RNT", "filtervippc03.rightnowtech.com", "filtervippc03.rightnowtech.com", "mailpc.custhelp.com");

$machines->add("VP", "HIPPA", "filtervp11.rightnowtech.com", "filtervp12.rightnowtech.com", "mailvp.custhelp.com");
$machines->add("VP", "PCI", "filtervp11.rightnowtech.com", "filtervp12.rightnowtech.com", "mailvp.custhelp.com");
$machines->add("VP", "RNT", "filtervp11.rightnowtech.com", "filtervp12.rightnowtech.com", "mailvp.custhelp.com");

#$machines->add("PS", "RNT", "filtervipps01.rightnowtech.com", "filtervipps01.rightnowtech.com", "mailps.custhelp.com");
#$machines->add("PS", "US-PS", "filtervipps01.rightnowtech.com", "filtervipps01.rightnowtech.com", "mailps.custhelp.com");
$machines->add("PS", "RNT", "filtervipfg11.rightnowtech.com", "filtervipfg11.rightnowtech.com", "mailfg.custhelp.com");
$machines->add("PS", "US-PS", "filtervipfg11.rightnowtech.com", "filtervipfg11.rightnowtech.com", "mailfg.custhelp.com");

#$machines->add("PS_legacy", "RNT", "filterps02.rightnowtech.com", "filterps02.rightnowtech.com", "mailps-legacy.custhelp.com");
#$machines->add("PS_legacy", "US-PS", "filterps02.rightnowtech.com", "filterps02.rightnowtech.com", "mailps-legacy.custhelp.com");

#$machines->add("UK_legacy", "RNT", "filtergb11.rightnowtech.com", "filtergb11.rightnowtech.com", "mailuk-legacy.custhelp.com");
#$machines->add("UK_legacy", "SNT", "filtergb11.rightnowtech.com", "filtergb11.rightnowtech.com", "mailuk-legacy.custhelp.com");

$machines->add("UK", "RNT", "filtergb13.rightnowtech.com", "filtergb14.rightnowtech.com", "mailuk.custhelp.com");
$machines->add("UK", "SNT", "filtergb13.rightnowtech.com", "filtergb14.rightnowtech.com", "mailuk.custhelp.com");

$machines->add("WC", "HIPPA", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailwc.custhelp.com");
$machines->add("WC", "QA", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailwc.custhelp.com");
$machines->add("WC", "RNT", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailwc.custhelp.com");

$machines->add("VA", "RNT", "filterva11.rightnowtech.com", "filterva12.rightnowtech.com", "mailva.custhelp.com");

$machines->add("TR", "RNT", "filtertr11.rightnowtech.com", "filtertr12.rightnowtech.com", "mailtr.custhelp.com");

$machines->add("CG", "RNT", "filtercg01.rightnowtech.com", "filtercg02.rightnowtech.com", "mailcg.custhelp.com");

$machines->add("FF", "RNT", "filterff11.rightnowtech.com", "filterff12.rightnowtech.com", "mailff.custhelp.com");

$machines->add("BR", "RNT", "filterbr01.rightnowtech.com", "filterbr02.rightnowtech.com", "mailbr.custhelp.com");

$machines->add("MN", "RNT", "filtermn11.rightnowtech.com", "filtermn12.rightnowtech.com", "mailmn.custhelp.com");

$machines->add("WC_legacy", "HIPPA", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailwc-legacy.custhelp.com");
$machines->add("WC_legacy", "QA", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailwc-legacy.custhelp.com");
$machines->add("WC_legacy", "RNT", "filtervipca11.rightnowtech.com", "filtervipca11.rightnowtech.com", "mailwc-legacy.custhelp.com");

#$fptr = fopen("https://cudaca01.int.rightnowtech.com/cgi-bin/index.cgi", "r");
##$fptr = fopen("https://filterca02.rightnowtech.com/cgi-bin/index.cgi", "r");
#
#$page = "";
#while ($data = fread($fptr, 16384)) {
    #$page = sprintf("%s%s", $page, $data);
#}
#fclose($fptr);
#
#$begin = strpos($page, "enc_key");
#$begin = strpos($page, "=", $begin) + 1;
#$end = strpos($page, ">", $begin);
#
#$hash = substr($page, $begin, $end - $begin);
#
#$begin = strpos($page, "name=et value");
#$begin = strpos($page, "=", $begin) + 1;
#$begin = strpos($page, "=",  $begin) + 1;
#$end = strpos($page, ">", $begin);
#
#$ts = substr($page, $begin, $end - $begin);

$fptr = fopen("/dev/urandom", "r");
$hash = md5(fread($fptr, 16));
fclose($fptr);
$ts = time() + 3600;

#printf("Hash: %s, TS: %s", $hash, $ts);
mysql_rw_connect("footer_enduser.phph");

$sql = sprintf("
    SELECT 
        s.site_name,
        m.mailbox_name,
        m.reply_to,
        m.mailbox_id,
        s.site_id,
        m.password,
        cl.description,
        dc.name
    FROM
        sites s,
        mailboxes m,
        companies c,
        class cl,
        servers sv,
        datacenters dc,
        mailbox_types mt
    WHERE s.site_id = m.site_id
        AND c.company_id = s.company_id
        AND s.company_id = %d
        AND s.class_id = cl.class_id
        AND m.server_id = sv.server_id
        AND sv.datacenter_id = dc.datacenter_id
        AND m.type_id = mt.type_id
        AND mt.name = 'service'
    ORDER BY m.reply_to
    ", $_SESSION["auth_company_id"]);

// Perform the query
$stmt = mysql_query($sql, $conn);
?>
<table width=640 border=0>
<tr>
    <td align=left width=500>
    <font size=+2><b>Ye Olde RightNow Technologies Customer Spam Management Shoppe</b></font>
    </td>
    <td align=right>&nbsp;</td>
</tr>
<tr>
    <td><font size=+1>For <b><?php echo $_SESSION["auth_company_info"] ?></b></font>
    </center></td>
</tr>
</table>
<p>Please select the email address that you are <b>actually using</b> from below.  
The default value is based on where your mail box is defined to live, however this
may not be where the mail is entering our network.
</p>
<p>For instance with our recent migrations the default may not get you into the 
right barracuda cluster.  A general guideline is as follows:
<ul>
    <li>If you recently migrated from the AP pod to the JP pod try the @mailap.custhelp.com address</li>
    <li>If you recently migrated from the NJ pod to the MW pod try the @mailnj.custhelp.com address</li>
    <li>If you recently migrated from the CA/WC pod to the VA pod try the @mailva.custhelp.com address</li>
</ul>
</p>
<hr align=left color=orange noshade width=640>
<table width="640">
<tr>
<th width=170 align=left>Database</th>
<th >Mailbox</th>
</tr>
<?php
$site_list = array();
while ($row = mysql_fetch_assoc($stmt)) {
    $site = $row["site_name"];
    // Skip any .disabled sites
    if ((ereg("\\.disabled", $site)) || (ereg("\\.retreat", $site))) {
        continue;
    }

    // Populate "new" site
    if (!array_key_exists($site, $site_list)) {
        $site_list[$site] = array();
    }

    // Populate the utility
    if (!array_key_exists($row["mailbox_name"], $site_list[$site])) {
        if ($_SESSION["auth_company_id"] == 17653) {
            $tmpvar = split("@", $row["reply_to"]);
            $rt = sprintf('%s@mailups.custhelp.com', $tmpvar[0]);
            $dc_override = "UPS";
        } elseif ($_SESSION["auth_company_id"] == 9817) {
            $tmpvar = split("@", $row["reply_to"]);
            $rt = sprintf('%s@mailyh.custhelp.com', $tmpvar[0]);
            $dc_override = "YH";
        } else {
            $rt = $row["reply_to"];
            $dc_override = $row["name"];
        }
        $site_list[$site][$row["mailbox_name"]] = array(
            "reply_to" => $rt,
            "mailbox_id" => $row["mailbox_id"],
            "password" => $row["password"],
            "site_id" => $row["site_id"],
            "class" => $row["description"],
            "datacenter" => $dc_override);
    }
}

?>
<script>
    function base64_encode(input) {
        var output = "";
        var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
    
        // convert into raw bytes for shipping up to remote
        input = unescape(encodeURIComponent(input))
    
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
    
            // passwords over HTTP are what they are. Use HTTPS.
            if (!isNaN(chr1)) {
                chr1 = chr1 ^ 85;
            }
            if (!isNaN(chr2)) {
                chr2 = chr2 ^ 170;
            }
            if (!isNaN(chr3)) {
                chr3 = chr3 ^ 51;
            }
    
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
    
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
    
            output = output +
            _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
        }
    
        return output;
    }
    function runform(inval, realm, mailbox, pw, b64pw) {
        harray = inval.split(":")
        host = harray[0]
        dc = harray[1]
        acct = harray[2].replace('-legacy.','.')
        dspam = document.getElementById('spam')
        
        drealm = document.getElementById('realm')
        duser = document.getElementById('user')
        ruser = document.getElementById('real_user')
        dpasswd = document.getElementById('password')
        switch (dc) {
            case "XX": 
                drealm.value = "B64"
                drealm.name = "enctype"
                dspam.action = "https://"+host+"/cgi-mod/index.cgi"
                duser.value = acct
                dpasswd.value = b64pw
                ruser.value = acct
                break
            case "AM": 
            case "AP": 
            case "AP_legacy": 
            case "BR": 
            case "CA":
            case "CG":
            case "EU":
            case "FD":
            case "FF":
            case "FG":
            case "GB":
            case "JP": 
            case "MN":
            case "MW":
            case "NJ":
            case "PC":
            case "PS":
            case "SG":
            case "SY": 
            case "TR":
            case "UK":
            case "UPS":
            case "VA":
            case "VP":
            case "WC":
            case "YH":
                drealm.value = "bc"
                drealm.name = "enctype"
                dspam.action = "https://"+host+"/cgi-mod/index.cgi"
                duser.value = acct
                dpasswd.value = base64_encode(pw) 
                ruser.value = acct
                break
            default:
                // drealm.value = realm
                // dspam.action = "https://"+host+"/cgi-bin/index.cgi"
                // duser.value = mailbox
                // dpasswd.value = pw
                drealm.value = "bc"
                drealm.name = "enctype"
                dspam.action = "https://"+host+"/cgi-mod/index.cgi"
                duser.value = acct
                dpasswd.value = base64_encode(pw)
                ruser.value = acct
                // drealm.value = "B64"
                // drealm.name = "enctype"
                // dspam.action = "https://"+host+"/cgi-mod/index.cgi"
                // duser.value = acct
                // ruser.value = acct
                // dpasswd.value = b64pw
        }

        dspam.submit()
    }
</script>
<form target="spam_window" id="spam" name="spam" method="post" action="xx">
    <input type="hidden" id="realm" name="realm" value="xx">
    <input type="hidden" id="real_user" name="real_user" value="">
    <input type="hidden" name="login_state" value="out">
    <input type="hidden" name="locale" value="en_US">
    <input type="hidden" id="user" name="user" value="xx">
    <input type="hidden" name="password_entry" value="">
    <input type="hidden" name="Submit" value="Login">
    <input type="hidden" id="password" name="password" value="xx">
    <input type="hidden" name="enc_key" value="<?php echo $hash ?>">
    <input type="hidden" name="et" value="<?php echo $ts ?>">
</form>
<?php
while (list ($site, $sval) = each ($site_list)) {
?>
    
    <tr><td></td><td><hr width=360> </td></tr>
<?php
    $first = 1;
    while (list ($mbox, $mval) = each($sval)) {

        switch ($site_list[$site][$mbox]["class"]) {
            case "Convergys":
                $realm="spemail.esecurecare.net";
                break;
            default:
                $realm="custhelp.com";
        }


            
        $mailbox = sprintf("x-%d-%d", $site_list[$site][$mbox]["site_id"],
            $site_list[$site][$mbox]["mailbox_id"]);
        $machine = $machines->host($site_list[$site][$mbox]["reply_to"], 
            $site_list[$site][$mbox]["datacenter"],
            $site_list[$site][$mbox]["class"]);
        $formdata = $machines->get($site_list[$site][$mbox]["reply_to"], 
            $site_list[$site][$mbox]["datacenter"],
            $site_list[$site][$mbox]["class"]);


        printf("
<tr><td align=left valign=top width=170>%s</td>
    <td align=left 0 valign=top>
        <input type=\"button\" name=\"dosubmit\" value=\"Open\" 
            onclick='runform(document.getElementById(\"%s\").value, \"%s\", \"%s\", \"%s\", \"%s\")'>
        <select id=\"%s\">%s</select>
        <br />
    </td>
</tr>

    
    ", $first ? $site : "",
        $mailbox, $realm, $mailbox,
        htmlspecialchars($site_list[$site][$mbox]["password"]),
        htmlspecialchars(base64_encode($site_list[$site][$mbox]["password"])),
        $mailbox, $formdata
        );

        if ($first) {
            $first = 0;
        }
    }
}
?>
</table>
<hr align=left color=orange noshade width=640>
<table width=640 border=0>
<tr>
    <td align=left width=500>
    <br>
    </td>
    <td align=right>&nbsp;</td>
</table>

<?php
require "/nfscommon/html/admin/bam/uma_footer_oracle.phph";
?>
