<?php include("_config/dbconfig.php");
include("_includes/head.php");

$total_person = "";
$CompanyName = "";
$OwnerName = "";
$categories = array() ;
$website = "";
$products ="";
$address = "";
$city = "";
$country = "";
$lead_source = "";
$member_of = '';

function removeComma($string){
        
    return substr($string, 0, strlen($string)-2 );
    
}
if(isset($_SESSION['user_session'])){
$agent_id = $_SESSION['user_session'];
} 

$cities = $obj_jobs_class->getCities();

if(count($_POST)>0 && isset($_POST['add_form'])){
    
    /*echo "<pre>";
    print_r($_POST);
    echo '</pre>';*/

    //addClient
    $total_person = $_POST['total_person'];
    $CompanyName = $_POST['CompanyName'];
    $OwnerName = $_POST['owner'];
    $lead_source = $_POST['lead_source'];
    $categories = implode(",", $_POST['category']) ;
    $website = $_POST['website'];
    $products = $_POST['products'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $country = $_POST['country'];
    $fax = '';
    $member_of = $_POST['member_of']; 
	
    $add_by = $agent_id;
    
    $clientId = $obj_clients_class->addClient($CompanyName, $OwnerName, $categories, $website, $products, $address, $city, $country, $fax, $add_by, $lead_source, $member_of);
     
    
        for($i=1; $i<=$total_person; $i++){
                
         if(isset($_POST['person'.$i.'_f_name']) && !empty($_POST['person'.$i.'_f_name']) && isset($_POST['person'.$i.'_l_name']) && !empty($_POST['person'.$i.'_l_name'])  ){
                
                $arr[$i-1]['f_name'] = $_POST['person'.$i.'_f_name'];
                $arr[$i-1]['l_name'] = $_POST['person'.$i.'_f_name'];
                $arr[$i-1]['designation'] = $_POST['person'.$i.'_designation'];
                $contactEmail = ''; $contactMobile = '';
                $contactLandline = ''; $contactExt = '';
                
                for($j=1; $j<4; $j++){

                if(isset($_POST['person_'.$i.'_email'.$j])){
                    
                    if(!empty($_POST['person_'.$i.'_email'.$j])){
                        $arr[$i-1]['contact_persron']['email'][$j-1]    =    $_POST['person_'.$i.'_email'.$j] ;
                        $contactEmail .= $_POST['person_'.$i.'_email'.$j] .', ';
                        
                    }
                    
                    if(!empty($_POST['person_'.$i.'_mobile'.$j])){
                        $arr[$i-1]['contact_persron']['mobile'][$j-1]   =    $_POST['person_'.$i.'_mobile'.$j] ;
                        $contactMobile .=    $_POST['person_'.$i.'_mobile'.$j] .', ';
                    }
                    
                    if(!empty($_POST['person_'.$i.'_contact'.$j])){
                        $arr[$i-1]['contact_persron']['contact'][$j-1]  =    $_POST['person_'.$i.'_contact'.$j] ;
                        $contactLandline .=    $_POST['person_'.$i.'_contact'.$j].', ';
                        
                    }
                    
                    if(!empty($_POST['person_'.$i.'_ext'.$j])){
                        $arr[$i-1]['contact_persron']['ext'][$j-1]      =    $_POST['person_'.$i.'_ext'.$j] ;
                            $contactExt .=    $_POST['person_'.$i.'_ext'.$j] .', ' ;
                        
                    }
                    
                   }
                }
                
                
                $contactFname = $_POST['person'.$i.'_f_name'];
                $contactLname = $_POST['person'.$i.'_l_name'];
                $contactDesignation = $_POST['person'.$i.'_designation'];
                
                $contactEmail = removeComma($contactEmail);
                $contactMobile = removeComma($contactMobile);
                $contactLandline = removeComma($contactLandline);
                $contactExt = removeComma($contactExt); 
                
        $obj_clients_class->addClientPerson($clientId, $contactFname, $contactLname, $contactDesignation, $contactEmail, $contactMobile, $contactLandline, $contactExt);

     }
    } 
    echo "<script> window.location= 'client_view.php?edit=".base64_encode($clientId)."'</script>";
    die();
    
    
    
}


if(count($_POST)>0 && isset($_POST['update_form'])){
    
            /*echo "<pre>";
            print_r($_POST);
            echo '</pre>';
           */
if(count($_POST['category'])>0){

    //addClient
    $total_person = $_POST['total_person'];
    $CompanyName = $_POST['CompanyName'];
    $OwnerName = $_POST['owner'];
    
    $categories = implode(",", $_POST['category']);
    
    
    
    $lead_source = $_POST['lead_source'];
    $website = $_POST['website'];
    $products = $_POST['products'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $country = $_POST['country'];
    $member_of = $_POST['member_of'];
    
    $fax = '';
    $add_by = '0';
    $clientId = base64_decode($_GET['edit']);
    
    //$clientId = $obj_clients_class->addClient($CompanyName, $OwnerName, $categories, $website, $products, $address, $city, $country, $fax, $add_by);
    $obj_clients_class->addUpdateClient($clientId , $CompanyName, $OwnerName, $categories, $website, $products, $address,$city, $country, $fax, $add_by, $lead_source, $member_of);
    
    
    
    for($i=1; $i<=$total_person; $i++){
                
         if(isset($_POST['person'.$i.'_f_name']) && !empty($_POST['person'.$i.'_f_name']) && isset($_POST['person'.$i.'_l_name']) && !empty($_POST['person'.$i.'_l_name'])  ){
                
                $arr[$i-1]['f_name'] = $_POST['person'.$i.'_f_name'];
                $arr[$i-1]['l_name'] = $_POST['person'.$i.'_f_name'];
                $arr[$i-1]['designation'] = $_POST['person'.$i.'_designation'];
                $contactEmail = ''; $contactMobile = '';
                $contactLandline = ''; $contactExt = '';
                
                for($j=1; $j<4; $j++){

                if(isset($_POST['person_'.$i.'_email'.$j])){
                    
                    if(!empty($_POST['person_'.$i.'_email'.$j])){
                        $arr[$i-1]['contact_persron']['email'][$j-1]    =    $_POST['person_'.$i.'_email'.$j] ;
                        $contactEmail .= $_POST['person_'.$i.'_email'.$j] .', ';
                    }
                    
                    if(!empty($_POST['person_'.$i.'_mobile'.$j])){
                        $arr[$i-1]['contact_persron']['mobile'][$j-1]   =    $_POST['person_'.$i.'_mobile'.$j] ;
                        $contactMobile .=    $_POST['person_'.$i.'_mobile'.$j] .', ';
                    }
                    
                    if(!empty($_POST['person_'.$i.'_contact'.$j])){
                        $arr[$i-1]['contact_persron']['contact'][$j-1]  =    $_POST['person_'.$i.'_contact'.$j] ;
                        $contactLandline .=    $_POST['person_'.$i.'_contact'.$j].', ';
                    }
                    
                    if(!empty($_POST['person_'.$i.'_ext'.$j])){
                        $arr[$i-1]['contact_persron']['ext'][$j-1]      =    $_POST['person_'.$i.'_ext'.$j] ;
                            $contactExt .=    $_POST['person_'.$i.'_ext'.$j] .', ' ;
                    }
                   }
                }
                
                $contactFname = $_POST['person'.$i.'_f_name'];
                $contactLname = $_POST['person'.$i.'_l_name'];
                $contactDesignation = $_POST['person'.$i.'_designation'];
                
                $contactEmail = removeComma($contactEmail);
                $contactMobile = removeComma($contactMobile);
                $contactLandline = removeComma($contactLandline);
                $contactExt = removeComma($contactExt); 
                
                if(isset($_POST['person'.$i.'_id']) && !empty(($_POST['person'.$i.'_id']))){
                      $person_id = $_POST['person'.$i.'_id']."<br>";
                    $obj_clients_class->updateClientPerson($person_id, $clientId, $contactFname, $contactLname, $contactDesignation, $contactEmail, $contactMobile, $contactLandline, $contactExt);      
                        
                }else {
                    $obj_clients_class->addClientPerson($clientId, $contactFname, $contactLname, $contactDesignation, $contactEmail, $contactMobile, $contactLandline, $contactExt);
                }
         }
    } 
        echo "<script> window.location= 'client_controls.php?edit=".$_GET['edit']."'</script>";
      die();    
    }
}



  $total_person_contacts = 0;
if(isset($_GET['edit']) && !empty($_GET['edit'])){
    
            $id_        = base64_decode($_GET['edit']);
            $client     = $obj_clients_class->getClients($id_);
            
            $obj_category_class->getCategoriesName($client['categories']);
            
            if(isset($client['persons']))
            $total_person = count($client['persons']);
            $CompanyName = $client['company_name'];
            $OwnerName = $client['owner_name'];
            $categories_client = explode(",", $client['categories']) ;
              
            $website = $client['website'];
            $products = $client['products'];
            $address = $client['office_add'];
            $city = $client['city'];
            $country = $client['country'];
            $fax = '';
            $add_by = '0';  
            $lead_source = $client['lead_source'];
            $page_title = "Edit Client";
            $member_of = $client['member_of'];
            
            
            echo '<title>Edit Client</title>';
            
            $total_person_contacts = 0;
            // die(); 
       }
       
    else if(isset($_GET['view']) && !empty($_GET['view'])){
            $id_        = base64_decode($_GET['view']);
            $agent      = $obj_agents_class->getAgent($id_);

            $full_name      = $agent['name'];
            $father_name    = $agent['father_name'];
            $email          = $agent['email'];
            $password       = sha1($agent['password']);
            $contact        = $agent['contact_no'];
            $mobile         = $agent['mobile_no'];
            $gender         = $agent['gender'];
            $address        = $agent['address'];
            $cnic           = $agent['cnic'];
            $join_date      = $agent['join_date'];
            $profile_img    = $agent['profile_img'];
?>    
<?php           
       }       
       
       else { 
           $page_title = "Add Client";
           echo '<title>Add Client </title>'; 
       }
       
       
       
       
$categories = $obj_category_class->getAllCategories();       
/*echo '<pre>';
print_r($categories);
echo '</pre>';*/
?>

<?php include("_includes/header.php"); ?>

<div class="col-sm-12 right_form">
        <div class="title">
            <div class="row">
                <div class="col-md-8 col-sm-12">
                    <h1><?php echo $page_title; ?></h1>
                </div>
            </div>
        </div>
        
        
     <ol class="breadcrumb white">
          <li><a href="dashboard.php">Dashboard</a></li>
          <li><a href="clients.php">Clients</a></li>
          <li class="active">Add Client</li>
    </ol>
    
    
</div>


<div class="col-sm-12 form_inner">
    <form class="col-sm-12 form-box" id="_form" method="post" action="" enctype="multipart/form-data">
                <div class="row">
		
                        <div class="col-md-4 col-sm-6 form-group">
                              <label for="CompanyName">Company Name</label>
                              <input type="text" class="form-control required" onblur="checkCompany();" id="CompanyName" name="CompanyName" value="<?= $CompanyName ?>" required >
                              <label id="CompanyName-exists" class="error"  style="display: none;">   This company is already exists.</label>
                        </div>
                        <div class="col-md-4 col-sm-6 form-group">
                                <label for="Owner">Owner's Name</label>
                                <input type="text" class="form-control agent_form " id="Owner" name="owner" value="<?= $OwnerName ?>">
                        </div>
                    
                    
                        <div class="col-md-4 col-sm-6 form-group">
                                <label for="Website">Category</label> 
                               
                                <select class="chosen-select required"   multiple=""  name="category[]" required="required" onchange="setThis(this.value);">
                            <?php foreach($categories as $cats){
                                if(isset($cats['sub_cat']) && count($cats['sub_cat'])>0){
                                ?>
                                    <optgroup label="<?php echo $cats['category'];?>">
                                <?php foreach($cats['sub_cat'] as $cats2){$sel = "";
                                            if(in_array($cats2['cat_id'], $categories_client)){ $sel = "selected='selected'";}
                                    ?>
                                            <option <?php echo $sel; ?> value="<?php echo $cats2['cat_id'] ?>"><?php echo $cats2['category'] ?></option>
                                <?php } ?>
                                    </optgroup>
                            <?php }else {?>
                                    
                            <?php } }?>
                        </select>
                          <?php if(isset($categories_client)){?>
                                <input type="text" value="1" required="required" id="setVal" style="visibility: hidden; width: 5px;">
                          <?php } else {?>
                            <input type="text" value="" required="required" id="setVal" style="visibility: hidden; width: 5px;">
                          <?php } ?>
                        </div>
                        
                    <div class="clearfix"></div>
                        
                    <div class="col-md-4 col-sm-6 form-group">
                            <label for="Website">Website</label>
                            <input type="text" class="form-control agent_form " id="Website" name="website" value="<?= $website?>" >
                    </div>
                    
                    <div class="col-md-4 col-sm-6 form-group">
                        <label for="Products">Products</label>
                        <textarea class="form-control agent_form" id="Products" name="products"><?= $products?></textarea>
                    </div>    
                    
                     <div class="col-md-4 col-sm-6 form-group">
                            <label for="address">Office Address</label>
                            <textarea class="form-control agent_form" id="address" name="address"><?= $address?></textarea>
                    </div>
                    <div class="clearfix"></div>
                    
                    <div class="col-md-8" style="padding: 0px;">
     
                    <div class="col-md-4 col-sm-4 form-group">
                        <label for="City">City</label> 
                        <select class="form-control required chosen-select" name="city" >
                        <?php foreach ($cities as $citii){  ?>
                            <option value="<?= $citii['city']?>" <?php if(strtolower($citii['city']) == strtolower($city)) echo "selected='selected'"; ?> ><?= $citii['city']?></option>
                        <?php } ?>
                        </select>
                    </div>

                    <div class="col-md-4 col-sm-4 form-group">
                            <label for="ContactNum">Country</label>
                            <select class="form-control required" name="country" >
                                <option value="Pakistan">Pakistan</option>
                            </select>
                    </div>
                    
                    <div class="col-md-4 col-sm-4 form-group">
                            <label> Lead Source </label>
                            <input type="text" class="form-control agent_form " id="lead_source" name="lead_source" value="<?= $lead_source?>" >
                    </div>
                    </div>
                    <div class="col-md-4 col-sm-4 form-group">
                            <label> Member Of</label>
                            <input type="text" class="form-control agent_form " id="member_of" name="member_of" value="<?= $member_of ?>" >
                    </div>
                    
                    
                    

                    <div class="clearfix"></div>
                    <div class="col-md-12 col-sm-12 form-group">
                        <h4>Contact Persons <a style="font-size: 12px; cursor: pointer; padding: 5px;" onclick="addPerson();">Add More</a></h4>                        
                    </div>
                    <div class="clearfix"></div>   
                    
            <?php if(isset($_GET['edit']) && !empty($_GET['edit'])){ ?> 
                    <input type="hidden" value="<?php echo $total_person; ?>" id="total_person" name="total_person" >  
                    
            <?php $ii = 0;  
            if(isset($client['persons']))
            foreach($client['persons'] as $person){ $ii++; ?>

                    <div id="person_<?= $ii?>_row" class="client_border">
                        
                    <div class="col-md-5 col-sm-12 form-group">
                        
                        <div class="col-md-4 col-sm-12 form-group no-padds">
                                <label for="">First Name</label>
                                <input class="form-control required" type="text" name="person<?= $ii?>_f_name" value="<?php echo $person['f_name']?>"  >
                                <input type="hidden" name="person<?= $ii?>_id" value="<?php echo $person['person_id']?>"  >
                                
                        </div>

                        <div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;">
                                <label for="">Last Name</label>
                                <input class="form-control" type="text" name="person<?= $ii?>_l_name" value="<?php echo $person['l_name']?>" >
                        </div>

                        <div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;">
                                <label for="">Designation</label>
                                <input class="form-control" type="text" name="person<?= $ii?>_designation" value="<?php echo $person['designation']?>" >
                        </div>
                    </div>

<div class="col-md-7 col-sm-12 form-group">
<div class="clearfix"></div>                        
                        <div id="more_contact_<?=$ii ?>_">

<?php   $_emails = explode(", ", $person['email']);
        $_mobile = explode(", ", $person['mobile']);
        $_landline = explode(", ", $person['landline']);
        $_line_ext = explode(", ", $person['line_ext']);
        $contact_person_row=0;
        //print_r($_emails);
        for($innr = 1; $innr<4; $innr++ ){

        if(isset($_emails[$innr-1]) ||  isset($_mobile[$innr-1]) || isset($_landline[$innr-1]) || isset($_line_ext[$innr-1]) ){ ?>
        <div id="row_<?= $innr-1;?>_" class="">
        <div class="col-md-3 col-sm-12 form-group">
                <label >Email</label>
                <input class="form-control" type="email" name="person_<?= $ii ?>_email<?= $innr ?>" value="<?php if(isset($_emails[$innr-1])) echo $_emails[$innr-1]; ?>" >
        </div>

        <div class="col-md-3 col-sm-12 form-group">
                <label>Mobile #</label>
                <input class="form-control   digits  " maxlength="12"  type="text" name="person_<?= $ii ?>_mobile<?= $innr ?>" value="<?php if(isset($_mobile[$innr-1])) echo $_mobile[$innr-1] ?>" >
        </div>                    

        <div class="col-md-3 col-sm-12 form-group">
                <label>Land Line #</label>
                <input class="form-control digits" maxlength="12"  type="text" name="person_<?= $ii ?>_contact<?= $innr ?>" value="<?php if(isset($_landline[$innr-1])) echo  $_landline[$innr-1] ?>" >
        </div>

        <div class="col-md-2 col-sm-12 form-group">
                <label for="Address">Ext # </label>
                <input class="form-control digits  " maxlength="4" minlength="3"   type="text" name="person_<?= $ii ?>_ext<?= $innr ?>" value="<?php if(isset($_line_ext[$innr-1])) echo $_line_ext[$innr-1] ?>" > 
        </div>
          
            
  <?php
        
        if($innr > 1){  ?>
            <a class="col-md-1" style="float: right; font-size: 16px; cursor: pointer; padding: 5px;" onclick="remove_contact(<?= $innr-1  ?> , <?= $ii ?>);">
                <br><i class="fa fa-times"></i></a>
                <div class="clearfix"></div>     
        <?php }  ?>
        </div>
    <?php if($innr == 1){?>

    <?php } 
                $contact_person_row++;
        }
        
        $total_person_contacts += ($innr-1);
        
    } ?><div class="clearfix"></div>
        
</div>
                        <div class="clearfix"></div>
                        
                        <?php   if($contact_person_row >= 3){ 
                                $display = "display:none";
                        } else  $display = ""; ?>   
                        <a id="add_more" style="float: right; font-size: 12px; cursor: pointer; padding: 5px; <?=$display ?>" onclick="addMoreContact(<?=$ii?>);">Add More</a>    
                        <div class="clearfix"></div>
                        
                    </div>
                    <input type="hidden" id="person_<?=$ii?>_contact" value="<?php echo $contact_person_row;   $innr-1; ?>">
                        
                   <div class="clearfix"></div> 
                   <?php if($ii>1){ } ?>
                   <a class="remove-btn" onclick="delPerson(<?=$ii?>, <?php echo $person['person_id'] ?>);">Remove Person</a>
                   <?php?>
                   </div>
                    
                            
                    <?php } }else {?>
                    <input type="hidden" value="1" id="total_person" name="total_person" >
                        <div id="person_1_row" class="client_border">
                        
                    <div class="col-md-5 col-sm-12 form-group">
                        
                        <div class="col-md-4 col-sm-12 form-group no-padds">
                                <label for="">First Name</label>
                                <input class="form-control required" type="text" name="person1_f_name" value=""  >
                                
                        </div>

                        <div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;">
                                <label for="">Last Name</label>
                                <input class="form-control" type="text" name="person1_l_name" value="" >
                        </div>

                        <div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;">
                                <label for="">Designation</label>
                                <input class="form-control" type="text" name="person1_designation" value="" >
                        </div>
                    </div>
                    
                    <div class="col-md-7 col-sm-12 form-group">
                    
                    <div class="col-md-3 col-sm-12 form-group">
                            <label >Email</label>
                            <input class="form-control" type="email" name="person_1_email1" value="" >
                    </div>

                    <div class="col-md-3 col-sm-12 form-group">
                            <label>Mobile #</label>
                            <input class="form-control  digits  " maxlength="12"  type="text" name="person_1_mobile1" value="" >
                    </div>                    

                    <div class="col-md-3 col-sm-12 form-group">
                            <label>Land Line #</label>
                            <input class="form-control digits" maxlength="12"  type="text" name="person_1_contact1" value="" >
                    </div>

                    <div class="col-md-2 col-sm-12 form-group">
                            <label for="Address">Ext # </label>
                            <input class="form-control digits  " maxlength="4" minlength="3"   type="text" name="person_1_ext1" value="" >
                    </div>      

                        
                    <div class="clearfix"></div>                        
                        <div id="more_contact_1_">
                            
                        </div>
                    
                        <div class="clearfix"></div>

                        <a id="add_more" style="float: right; font-size: 12px; cursor: pointer; padding: 5px;" onclick="addMoreContact(1);">Add More</a>
                        <div class="clearfix"></div>

                    </div>
                    <input type="hidden" id="person_1_contact" value="1">
                        
                   <div class="clearfix"></div> 
                   
                   </div>
                   <?php } ?>
                    
                    <div id="more_persons">
                        
                    </div>
                    
                    <div class="clearfix"></div> 

<?php if(isset($_GET['edit']) && !empty($_GET['edit'])){ ?>
            <div class="col-sm-12 form-group">
                <button type="submit" class="btn btn-default" name="update_form"> Update </button>
            </div>
                    
                    
<?php } else {?>
            <div class="col-sm-12 form-group">
                <button type="submit" class="btn btn-default" name="add_form"> Submit </button>
            </div>
<?php } ?>                                        
            <div class="clearfix"></div> 
            
            
   
                                        
		</div>
            </form>
		</div>

<?php include("_includes/footer.php"); ?>

<script>
    
    $(document).ready(function(){
           $("#_form").validate();
       });

function thumbImg(){
	
            var imgg = $("#profile_img").val();
            arr = imgg.split(".");
            imgg = arr[1].toLowerCase();

            if(imgg == "jpg" || imgg =="jepg"  ||  imgg =="png"  || imgg =="gif"){
                $("#old_img").fadeOut(300);                    
            }
            else {
                    alert('Please select the valid file or format!');
                    $("#thumb_img").focus();
                    $("#thumb_img").val('');
            }
	}

var roww = <?= $total_person_contacts; ?>;

function addMoreContact(no_contact){
        var ro_val   = '';
        var ro_val11 = '';
        roww = roww+1;
        var ro_val11 = $("#person_"+no_contact+"_contact").val();
        
        console.log(ro_val11+"****************");
        
        var ro_val = parseInt(ro_val11)+1;
        $("#person_"+no_contact+"_contact").val(ro_val);
        
        var content =  '<div id="row_'+roww+'_" class="  form-group"><div class="col-md-3 col-sm-12 form-group"><label>Email</label><input class="form-control" type="email" name="person_'+no_contact+'_email'+ro_val+'" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Mobile #</label><input class="form-control digits  " maxlength="12"  type="text" name="person_'+no_contact+'_mobile'+ro_val+'" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Land Line #</label><input class="form-control digits  " maxlength="12" type="text" name="person_'+no_contact+'_contact'+ro_val+'" value="" ></div><div class="col-md-2 col-sm-12 form-group"><label>Ext # </label><input class="form-control " type="text digits  " maxlength="4" minlength="3"   name="person_'+no_contact+'_ext'+ro_val+'" value="" ></div><a class="col-md-1" style="float: right; font-size: 16px; cursor: pointer; padding: 5px;" onclick="remove_contact('+roww+', '+no_contact+');"><br><i class="fa fa-times"></i></a> <div class="clearfix"></div></div>';
        $( "#more_contact_"+no_contact+"_" ).append(content);
        if(ro_val>2){
            $("#person_"+no_contact+"_row #add_more").fadeOut(500);
        }
}


function remove_contact(row_id, person_row){
            
            $( "#person_"+person_row+"_row #row_"+row_id+"_" ).fadeOut(500, function(){
                        $( "#row_"+row_id+"_" ).html('');
            })

        var ro_val11 = $("#person_"+person_row+"_contact").val();
        console.log(ro_val11);
        
        ro_val= parseInt(ro_val11)-1;
        $("#person_"+person_row+"_contact").val(ro_val);
        
        if(ro_val<3){
            $("#person_"+person_row+"_row #add_more").fadeIn(500);
        }
}


function addPerson(){
    
      person  = $("#total_person").val();
   if(!person){person=0}
    
    person_row = parseInt(person) + 1;
    $("#total_person").val(person_row);
        
    var new_content = '<div id="person_'+person_row+'_row" class="client_border" ><div class="col-md-5 col-sm-12 form-group"><div class="col-md-4 col-sm-12 form-group no-padds"><label >First Name</label><input class="form-control required" type="text" name="person'+person_row+'_f_name" value=""  ></div><div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;"><label for="">Last Name</label><input class="form-control required" type="text" name="person'+person_row+'_l_name" value="" ></div><div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;"><label for="">Designation</label><input class="form-control " type="text" name="person'+person_row+'_designation" value="" ></div></div><div class="col-md-7 col-sm-12 form-group"><div class="col-md-3 col-sm-12 form-group"><label>Email</label><input class="form-control " type="email" name="person_'+person_row+'_email1" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Mobile #</label><input class="form-control digits" maxlength="12" type="text" name="person_'+person_row+'_mobile1" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Land Line #</label><input class="form-control digits" maxlength="12" type="text" name="person_'+person_row+'_contact1" value="" ></div><div class="col-md-2 col-sm-12 form-group"><label>Ext </label><input class="form-control digits" maxlength="4"  minlength="3" type="text" name="person_'+person_row+'_ext1" value="" ></div><div class="clearfix"></div><div id="more_contact_'+person_row+'_"></div><div class="clearfix"></div><a id="add_more" style="float: right; font-size: 12px; cursor: pointer; padding: 5px;" onclick="addMoreContact('+person_row+');">Add More</a><div class="clearfix"></div></div><input type="hidden" id="person_'+person_row+'_contact" value="1"><div class="clearfix"></div> <a class="remove-btn" onclick="removePerson('+person_row+');">Remove Person</a></div>';
    
    $( "#more_persons").append(new_content);
}

function removePerson(remId){
        
            //alert(remId);
            $("#person_"+remId+"_row").remove();
            /*person  = $("#total_person").val();
            person_row = parseInt(person) - 1;
            $("#total_person").val(person_row);*/
            
}

function delPerson(remId, idd){ 
    
        var aaaa = confirm("Are you sure you want to Delete!"); 
        console.log(aaaa);
        if(aaaa == true){
            $.post('ajax.php?del_person='+idd+'&a=1', function(data){
                    $("#person_"+remId+"_row").remove();
                    console.log(data);
            })
        }
        
}


function setThis(aa){
    if(aa!=''){
        $("#setVal").val("1");
    }else $("#setVal").val("");
    
}

function checkCompany(){
    
    var a  = $("#CompanyName").val();
    
    var url = "ajax.php?task=checkCompany&comp="+a+"&1=1<?php if(isset($_GET['edit'])) echo "&id=".base64_decode($_GET['edit'])?>"; 
    
    
        $.ajax({
              type: "POST",
              url: url,
              //data: 'id', // serializes the form's elements.
              success: function(data){ 
                        //alert(data);
                        data = data.trim();   
                        console.log(data);
                        
                        if(data=='yes'){
                          
                            //alert(data);
                            $("#CompanyName-exists").css("display", "block");
                            $("#CompanyName").val("");
                            $("#CompanyName").focus();
                        }
                        else if (data=="no"){
                            
                        }
                      /*$("#pending_task_"+assg+"_ #task_id_"+id+"_").html('');
                      $("#pending_task_"+assg+"_ #task_id_"+id+"_").css('display','none');*/
              }
            });
            
             
    
    
}



</script>
<?php  /* ?>
<div id="person_'+person_row+'_row"><div class="col-md-5 col-sm-12 form-group"><div class="col-md-4 col-sm-12 form-group no-padds"><label >First Name</label><input class="form-control required" type="text" name="person'+person_row+'_f_name" value=""  ></div><div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;"><label for="">Last Name</label><input class="form-control required" type="text" name="person'+person_row+'_l_name" value="" ></div><div class="col-md-4 col-sm-12 form-group " style="padding-right: 0px;"><label for="">Designation</label><input class="form-control required" type="text" name="person'+person_row+'_designation" value="" ></div></div><div class="col-md-7 col-sm-12 form-group"><div class="col-md-3 col-sm-12 form-group"><label>Email</label><input class="form-control required" type="email" name="person_'+person_row+'_email" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Mobile #</label><input class="form-control required" type="text" name="person_'+person_row+'_mobile" value="" ></div><div class="col-md-3 col-sm-12 form-group"><label>Contact #</label><input class="form-control " type="text" name="person_'+person_row+'_contact" value="" ></div><div class="col-md-2 col-sm-12 form-group"><label>Ext </label><input class="form-control" type="text" name="person_'+person_row+'_fax" value="" ></div><div class="clearfix"></div><div id="more_contact_'+person_row+'_"></div><div class="clearfix"></div><a id="add_more" style="float: right; font-size: 12px; cursor: pointer; padding: 5px;" onclick="addMoreContact('+person_row+');">Add More</a><div class="clearfix"></div></div><input type="hidden" id="person_'+person_row+'_contact" value="1"><div class="clearfix"></div> </div>
<?php */ ?>

<?php if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
<script>
$(document).ready(function(){ 
        $('.form-control').attr('readonly', 'readonly'); 
   });
</script>
<?php } ?>

 