<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$formname = 'Company / Branch';
$maindb = 'branch';
$file = 'cobran.php';
$grid = '';
$keyfld = "code";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'coblst.php';

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    mysqli_query($conn, "LOCK TABLES `branch` WRITE") OR die(mysqli_error($conn));

    $a = 1;
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $add1 = mysqli_real_escape_string($conn, $_POST['add1']);
    $add2 = mysqli_real_escape_string($conn, $_POST['add2']);
    $add3 = mysqli_real_escape_string($conn, $_POST['add3']);
    $city = mysqli_real_escape_string($conn, $_POST['city']);
    $pin = mysqli_real_escape_string($conn, $_POST['pin']);
    $mob = $_POST['mobile'];
    $mob2 = $_POST['mobile2'];
    $contpers = $_POST['contpers'];
    $tel = $_POST['tel'];
    $web = $_POST['web'];
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $bemail = mysqli_real_escape_string($conn, $_POST['ccemail']);
    $vatno = mysqli_real_escape_string($conn, $_POST['vattin']);
    $cstno = mysqli_real_escape_string($conn, $_POST['csttin']);
    $panno = mysqli_real_escape_string($conn, $_POST['pan']);
    $servno = mysqli_real_escape_string($conn, $_POST['servtax']);
   

    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `branch`(code, `compname`, `add1`, `add2`, `add3`, `city`, pin, `tel`,`mobile`, `mobile2`, `contpers`,
                                              `web`, `email`,`ccemail`,`vattinno`,`csttinno`, `servtaxno`,`panno`)
                                         VALUES($qvouch, '$name', '$add1', '$add2', '$add3', '$city','$pin','$tel','$mob', '$mob2','$contpers',
                                               '$web', '$email','$bemail' ,'$vatno', '$cstno','$servno','$panno')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `branch` SET `compname`='$name', `add1`='$add1', `add2`='$add2', `add3`='$add3',
                                `city` ='$city', pin='$pin', `tel` = '$tel',`mobile` = '$mob', `mobile2` = '$mob2', 
                                `contpers` = '$contpers', `web`='$web', `email`='$email',
                                `ccemail`='$bemail',`vattinno`='$vatno',`csttinno`='$cstno',`servtaxno`='$servno', 
                                `panno`='$panno'  WHERE code = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }


  //  mysqli_query($conn, "UNLOCK TABLES") OR die(mysqli_error($conn));

}

if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
include("includes/files.php");
?>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#email").blur(function () {
            var email = $("#email").val();
            if (email != 0) {
                if (isValidEmailAddress(email)) {
                    $("#validEmail").css({
                        "background-image": "url('validYes.png')"
                    });
                    $('#save').attr('disabled', false);
                }
                else {
                    $("#validEmail").css({
                        "background-image": "url('validNo.png')"
                    });
                    alert("INVALID Email");
                    $('#save').attr('disabled', true);
                    $('#email').focus();
                }
            }
            else {
                $("#validEmail").css({
                    "background-image": "none"
                });
            }
        });
    });
    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }
</script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#ccemail").blur(function () {
            var email = $("#ccemail").val();
            if (email != 0) {
                if (isValidEmailAddress(email)) {
                    $("#validEmail2").css({
                        "background-image": "url('validYes.png')"
                    });
                    $('#save').attr('disabled', false);
                }
                else {
                    $("#validEmail2").css({
                        "background-image": "url('validNo.png')"
                    });
                    alert("INVALID Cc-email");
                    $('#save').attr('disabled', true);
                    $('#ccemail').focus();
                }
            }
            else {
                $("#validEmail2").css({
                    "background-image": "none"
                });
            }
        });
    });
    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }
</script>


</head>
<body onLoad="document.boq.vouchdt.focus();">
    <form action=''  name="customer" method="POST">
        <div class="row">
            <div class="col-lg-6" style='width:100%;float:center;'>
                <div class="box dark">
                    <header>
                        <div class="icons">
                            <i class="fa fa-edit"></i>
                        </div>
                        <h5><?= $formname ?></h5>
                        <div class="toolbar">
                            <ul class="nav pull-right">
                                <li>
                                    <?php include("includes/buttons.php"); ?>
                                </li>
                                <li>
                                    <a class="minimize-box" data-toggle="collapse" href="#div-4">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>
                            </ul>
                        </div><!-- /.toolbar -->
                    </header>
                    <div id="div-4" class="accordion-body collapse in body" style="width:50%;">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-lg-4"> Name </label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" maxlength="40" id='name' name="name" value="<?php echo ($addflag != 1) ? $get["compname"] : ""; ?>" class="form-control form" required autofocus>
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4"> Address </label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" maxlength="40" id='name' name="add1" value="<?php echo ($addflag != 1) ? $get["add1"] : ""; ?>" class="form-control form" >
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4"> </label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" maxlength="40" id='name' name="add2" value="<?php echo ($addflag != 1) ? $get["add2"] : ""; ?>" class="form-control form" >
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4"> </label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" maxlength="40" id='name' name="add3" value="<?php echo ($addflag != 1) ? $get["add3"] : ""; ?>" class="form-control form">
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4"> City </label>
                                <div class="col-lg-8"> 
                                    <input type="text" maxlength="25" name="city" class="form-control form" style="width: 50%;" value="<?php echo ($addflag != 1) ? $get['city'] : "MUMBAI"; ?>" />


                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4"> Pin </label>
                                <div class="col-lg-8"> 

                                    <input type="text" maxlength="8" size="8" name="pin" class="form-control form" style="width: 80px;" value="<?php echo ($addflag != 1) ? $get['pin'] : ""; ?>" />

                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4"> Telephone </label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" name="tel" maxlength="40" class="form-control form" value="<?php echo ($addflag != 1) ? $get['tel'] : ""; ?>" />
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4"> Website </label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" name="web" maxlength="40" class="form-control form" value="<?php echo ($addflag != 1) ? $get['web'] : ""; ?>"/>
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4"> Email</label>
                                <div class="col-lg-8"> 
                                    <input type="email" size="40" id="email" name="email" maxlength="40" class="form-control form" value="<?php echo ($addflag != 1) ? $get['email'] : ""; ?>" /><span id="validEmail"></span>
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4">Cc Email</label>
                                <div class="col-lg-8"> 
                                    <input type="email" size="40" id="ccemail" name="ccemail" maxlength="40" class="form-control form" value="<?php echo ($addflag != 1) ? $get['ccemail'] : ""; ?>" /><span id="validEmail2"></span>
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4">VAT TIN</label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" name="vattin" class="form-control form" value="<?php echo ($addflag != 1) ? $get['vattinno'] : ""; ?>"/>
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4">CST TIN</label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" name="csttin" class="form-control form"  value="<?php echo ($addflag != 1) ? $get['csttinno'] : ""; ?>"/>
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4">Pan No.</label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" name="pan"  class="form-control form" value="<?php echo ($addflag != 1) ? $get['panno'] : ""; ?>"/>
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4">Service Tax No.</label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" name="servtax" class="form-control form" value="<?php echo ($addflag != 1) ? $get['servtaxno'] : ""; ?>"/>
                                </div>
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4">Contact Person</label>
                                <div class="col-lg-8"> 
                                    <input type="text" size="40" name="contpers" class="form-control form" value="<?php echo ($addflag != 1) ? $get['contpers'] : ""; ?>" />
                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4">Mobile</label>
                                <div class="col-lg-8"> 
                                    <input type="text" maxlength="15" name="mobile" class="form-control form" style="width: 40%;" value="<?php echo ($addflag != 1) ? $get['mobile'] : ""; ?>" />

                                </div> 
                            </div> 

                            <div class="form-group">
                                <label class="control-label col-lg-4">Mobile 2</label>
                                <div class="col-lg-8"> 
                                    <input type="text" maxlength="15" name="mobile2" class="form-control form" style="width: 40%;" value="<?php echo ($addflag != 1) ? $get['mobile2'] : ""; ?>" />

                                </div> 

                            </div> 
                            
                        </div>   
                    </div> 
                </div> 
            </div> 
        </div> 



    </form>
    <?php
    include("includes/files_footer.php");
    ?>
<!--        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
    <script src="js/chosen.jquery.js" type="text/javascript"></script>
    <script type="text/javascript"> $(".chzn-select").chosen();
        $(".chzn-select-deselect").chosen({allow_single_deselect: true});</script>-->

</body>
</html>
