<?php
session_start();
// - ncinfo.php
require_once "main.php";
Logger::configure('log4php.xml');
$log = Logger::getLogger('ncinfo');
global $db, $defaultHomeLink;
$comz = $_SESSION['comnum'];
if ($_SESSION['access']<4){
    if ($_GET['submitType']=='Add') {
       // echo '<script>alert("'.$_GET['customer_username'].','.$_GET['id'].'");</script>';
            if (bill_checkuser($_GET['customer_username'],$_SESSION['comnum'])) {
        echo '<script>alert("Someone already has this Web Username. ");</script>'; 
    } else { 
        $test = bill_insert_customer($_GET['name'], $_GET['addr'], $_GET['addr1'],
                $_GET['city'], $_GET['state'], $_GET['zip'], $_GET['phone'], $_GET['phone1'],
                $_GET['ctype'],$_GET['bill_email'],$_GET['account'],$_SESSION['comnum'],$_GET['active'],
                $_GET['meternum'],$_GET['oname'], $_GET['oaddr'], $_GET['oaddr1'],$_GET['ocity'],
                $_GET['ostate'], $_GET['ozip'], $_GET['ophone'],$_GET['oemail'],$_GET['maddr'], 
                $_GET['lat'], $_GET['lon'],$_GET['rorder'],$_GET['customer_username'], md5 ($_GET['customer_password'], $_GET['metertype'],$_GET['metermiu']));
        if ($test) {
          $cusid = bill_get_customer_id($_SESSION['comnum'],$_GET['customer_username']);            
          $test1 = bill_insert_payment( $_SESSION['comnum'], $cusid[0]['id'],'1', '0', 'Account Created', '','0');
          $test2 = bill_insert_charge( $_SESSION['comnum'], $cusid[0]['id'], '1', '0', 'Account Created', '', '0' );       
        }        
        if (($test1==2) && ($test2==2)) {
            echo '<script type=text/javascript>alert("Account added successfully!");javascript:toggle();</script>';            
            $log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
            render($content);
        } else {
            echo '<script type=text/javascript>alert("There was a database error!  Please try again, if it fails again, please contact ProTek Services.");javascript:toggle();</script>';
            $log->warn($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
        }
        echo '<script type=text/javascript> parent.location.href = "'.$defaultHomeLink.'";</script>';
    }
    }
?>
    <script>
        function setType(type)
        {
            document.myForm.submitType.value=type;  
        }
    </script>
    <form name="myForm" action="#" onsubmit="jQuery.ajax({url:'includes/ncinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#accinfo').html(data);}});return false;">
        <table border="1">
            <tr>
                <td><label for="name">Billing Name*</label></td>
                <td><input type="text" name="name" id="name" tabindex=1></td>
                <td><label for="oname">Owner Name</label></td>
                <td><input type="text" name="oname" id="oname" tabindex=11></td>
                <td><label for="account">Account Number*</label></td>
                <td><input type="text" name="account" id="account" tabindex=21></td>
            </tr>
            <tr>
                <td><label for="addr">Billing Address</label></td>
                <td><input type="text" name="addr" id="addr" tabindex=2></td>
                <td><label for="oaddr">Owner Address</label></td>
                <td><input type="text" name="oaddr" id="oaddr" tabindex=12></td>
                <td><label for="maddr">Physical Address</label></td>
                <td><input type="text" name="maddr" id="maddr" tabindex=22></td>
            </tr>
            <tr>
                <td><label for="addr1">Billing Address 1</label></td>
                <td><input type="text" name="addr1" id="addr1" tabindex=3></td>
                <td><label for="oaddr1">Owner Address 1</label></td>
                <td><input type="text" name="oaddr1" id="oaddr1" tabindex=13></td>
                <td><label for="meternum">Meter Serial Number</label></td>
                <td><input type="text" name="meternum" id="meternum" tabindex=23></td>
            </tr>
            <tr>
                <td><label for="city">Billing City</label></td>
                <td><input type="text" name="city" id="city" tabindex=4></td>
                <td><label for="ocity">Owner City</label></td>
                <td><input type="text" name="ocity" id="ocity" tabindex=14></td>
                <td><label for="metertype">Meter Type</label></td>
                <td><input type="text" name="metertype" id="metertype" tabindex=24></td>
            </tr>
            <tr>
                <td><label for="state">Billing State</label></td>
                <td><input type="text" name="state" id="state" tabindex=5> </td>
                <td><label for="ostate">Owner State</label></td>
                <td><input type="text" name="ostate" id="ostate" tabindex=15> </td>
                <td><label for="metermiu">Radio Serial Number</label></td>
                <td><input type="text" name="metermiu" id="metermiu" tabindex=25></td>
            </tr>
            <tr>
                <td><label for="zip">Billing Zip</label></td>
                <td><input type="text" name="zip" id="zip" tabindex=6> </td>
                <td><label for="ozip">Owner Zip</label></td>
                <td><input type="text" name="ozip" id="ozip" tabindex=16> </td>
                <td><label for="lat">Meter Latitude</label></td>
                <td><input type="text" name="lat" id="lat" tabindex=26></td>
            </tr>
            <tr>
                <td><label for="bill_email">Billing Email</label></td>
                <td><input type="text" name="bill_email" id="bill_email" tabindex=7></td>
                <td><label for="oemail">Owner Email</label></td>
                <td><input type="text" name="oemail" id="oemail" tabindex=17></td>
                <td><label for="lon">Meter Longitude</label></td>
                <td><input type="text" name="lon" id="lon" tabindex=27></td>
            </tr>
            <tr>
                <td><label for="phone">Billing Phone</label></td>
                <td><input type="text" name="phone" id="phone" tabindex=8></td>
                <td><label for="ophone">Owner Phone</label></td>
                <td><input type="text" name="ophone" id="ophone" tabindex=18></td>
                <td><label for='customer_username'>Web User Name</label></td>
                <td><input type="text" name="customer_username" id="customer_username" tabindex=28 value="<?php echo substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8);?>"></td>
            </tr>
            <tr>
                <td><label for="phone1">Billing Phone 2</label></td>
                <td><input type="text" name="phone1" id="phone1" tabindex=9></td>
                <td><label for="ctype">Customer Type*</label></td>
                <td><select name="ctype" id="ctype" tabindex=19>
                        <option value="2">Bill to Both mail</option>
                        <option value="3">Bill to Billing email</option>
                        <option value="4">Bill to Both email</option>
                        <option value="5">Bill to Both Billing</option>
                        <option value="6">Bill to All Addresses</option>
                        <option selected="selected" value="1">Bill to Billing mail</option>
                    </select>
                </td>
                <td><label for='customer_password'>Web User Password</label></td>
                <td><input type="password" name="customer_password" id="customer_password" tabindex=29></td>
            </tr>
            <tr>
                <td><label for="rorder">Read Order</label></td>
                <td><input type="text" name="rorder" id="rorder" tabindex=10></td>
                <td><label for="active">Account Active*</label></td>
                <td><select name="active" id="active" tabindex=20>
                        <option value="0">Not Active</option>
                        <option selected="selected" value="1">Active</option>
                        </select>
                </td>
            </tr>
            <tr>
                <td><input type="hidden" name="zcomnum" value="<?php echo $comz; ?>">
                   <input type="hidden" name="submitType">
                   <input type="submit" onclick="setType('Add')" value="Click to Add Customer"/>
              
                </td>
                <td><button title="Return to the Customer Information screen." type="button" value="GoAccount" onClick="javascript:toggle();dynamic_Selectz('includes/acinfo.php', '#accinfo','custxxx', '#combobox', <?php echo $_SESSION['comnum']?>)">Cancel</button></td>
                <td></td>
                <td></td>
                <td></td>
                <td>* Required Fields</td>
            </tr>
        </table>  
    </form>
<?php
} else {
?>
    <script>alert('You do not have a high enough access level to Add accounts\n\
\n\
                    Please see your administrator.');
    </script>
<?php    
}
?>

