<style type="text/css">
   body .modal {
   /* new custom width */
   width: 750px;
   /* must be half of the width, minus scrollbar on the left (30px) */
   margin-left: -375px;
   }
</style>
<form role="form" class="form-horizontal" id="create_customer_form" action="#" method="post">
   <input type="hidden" value="<?php echo $type;?>" name="type" id="type"/>
   <input type="hidden" value="<?php echo $cus_id;?>" name="cus_id" id="cus_id"/>
<form role="form" class="form-horizontal" id="create_customer_form" action="#">
   <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      <h4 class="modal-title"><?php echo $pageName ?></h4>
      <font style="color:#333;">Please fill in the information below. The field labels marked with * are required input fields.</font>
   </div>
   <div class="modal-body">
      <div id="error"></div>
      <div class="row">
         <div class="col-md-12">
            <div class="col-md-5">
               <div class="form-group">
                  <h5>CODE *</h5>
                  <input type="text" class="form-control" name="cus_code" id="cus_code" <?php echo (isset($customer['cus_code']))?'value="'.$customer['cus_code'].'"':null;?> <?php if (isset($type)) if($type=='E') echo 'readonly';?>>
               </div>
               <div class="form-group">
                  <h5>NAME *</h5>
                  <input type="text" class="form-control" name="cus_name" id="cus_name" <?php echo (isset($customer['cus_name']))?'value="'.$customer['cus_name'].'"':null;?>>
               </div>
               <div class="form-group">
                  <h5>CONTACT NUMBER *</h5>
                  <input type="tel" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" class="form-control" name="cus_phone" id="cus_phone" <?php echo (isset($customer['cus_phone']))?'value="'.$customer['cus_phone'].'"':null;?>>
               </div>
               <div class="form-group">
                  <h5>ADDRESS *</h5>
                  <input type="text" class="form-control" name="cus_address" id="cus_address" <?php echo (isset($customer['cus_address']))?'value="'.$customer['cus_address'].'"':null;?>>
               </div>
                <div class="form-group">
                  <h5>NIC </h5>
                  <input type="text" class="form-control" name="cus_nic" id="cus_nic" <?php echo (isset($customer['cus_nic']))?'value="'.$customer['cus_nic'].'"':null;?>>
               </div>
               <div class="form-group">
                  <h5>OWNER NAME </h5>
                  <input type="text" class="form-control" name="owner_name" id="owner_name" <?php echo (isset($customer['owner_name']))?'value="'.$customer['owner_name'].'"':null;?>>
               </div>
            </div>
            <div class="col-md-5 pull-right">
               <div class="form-group">
                  <h5>CITY </h5>
                  <input type="text" class="form-control" name="city_name" id="city_name" <?php echo (isset($customer['city_name']))?'value="'.$customer['city_name'].'"':null;?>>
               </div>
               <div class="form-group">
                  <h5>GOOGLE PLACE ID </h5>
                  <input type="text" class="form-control" name="cus_state" id="cus_state" <?php echo (isset($customer['place_id']))?'value="'.$customer['place_id'].'"':null;?>>
               </div>
               <div class="form-group">
                  <h5>POSTAL CODE </h5>
                  <input type="text" class="form-control" name="cus_postal_code" id="cus_postal_code" <?php echo (isset($customer['cus_postal_code']))?'value="'.$customer['cus_postal_code'].'"':null;?>>
               </div>
               <div class="form-group">
                  <h5>CREDIT LIMIT *</h5>
                  <input type="text" class="form-control" name="cus_credit_limit" id="cus_credit_limit" <?php echo (isset($customer['cus_credit_limit']))?'value="'.$customer['cus_credit_limit'].'"':null;?>>
               </div>
               <div class="form-group">
                  <h5>E-MAIL ADDRESS</h5>
                  <input type="email"  class="form-control" name="cus_email" id="cus_email" <?php echo (isset($customer['cus_email']))?'value="'.$customer['cus_email'].'"':null;?>>
               </div>
               <div class="form-group" style="display: none">
                  <h5>Priority </h5>
                  <input type="text" class="form-control" name="priority" id="priority" value="<?php  if(isset($customer['priority'])) echo $customer['priority'];?>">
               </div>
               <div class="form-group">
                  <h5>ROUTE </h5>
                  <select  id="route_id" class="select2-container form-control search-select" name="route_id"  >
                     <option value="">- Please Select -</option>
                     <?php 										
                        foreach ($routes_list as $row)
                        {	?>                   
                     <option  value="<?php echo $row->route_id;?>"                                                       
                        <?php  if(isset($customer['route_id'])&& $row->route_id==$customer['route_id'])echo 'selected="selected"'?>
                        >												
                        <?php echo $row->route_name;?>                                                       
                     </option>
                     <?php }
                        ?>
                  </select>
               </div>
               <div class="form-group" style="display: none">
                  <h5>Country </h5>
                  <select class="select2-container form-control search-select" id="country_id" name="country_id" >
                     <option value="">-- Select Country --</option>
                     <?php 
                        foreach ($country_list as $row)
                        {
                        $sel='';
                        $seldef='';
                        if(isset($customer['country_id'])){
                        if($customer['country_id']==$row['country_id']){
                        $sel=' selected="selected"';
                        }
                        }else if($row['country_id']==251){
                        $seldef=' selected="selected"';
                        }
                        ?>       
                     <option<?php echo $sel;?><?php echo $seldef ?> value="<?php echo $row['country_id'];?>"><?php echo $row['country_short_name']; ?></option>
                     <?php }  ?>
                  </select>
               </div>
            </div>
         </div>
      </div>
      <!--col-md-12-->
   </div>
   <div class="modal-footer">
      <input type="submit" name="add_category" value="<?php echo $btnText; ?>" class="btn btn-primary">
   </div>
</form>
<!-- start: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY VALIDATION -->
<script src="<?php echo asset_url(); ?>js/form-validation-create_customer.js"></script>
<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY  VALIDATION-->
<script>
   jQuery(document).ready(function() {
   	FormValidator.init();
   	//$(".search-select").select2();
   	$('#route_id').select2();
   	$('#country_id').select2();
   });
</script>
<script type="text/javascript">
   function insertCustomerData(){
   var type=$('#type').val();
   var cus_id=$('#cus_id').val();
   var country_id=$('#country_id').val();
   var city_name=$('#city_name').val();	
   var cus_name=$('#cus_name').val();
   var cus_code=$('#cus_code').val();
   var cus_email=$('#cus_email').val();
   var cus_phone=$('#cus_phone').val();	
   var cus_address=$('#cus_address').val();
   var cus_state=$('#cus_state').val();
   var cus_postal_code=$('#cus_postal_code').val();
   var cus_credit_limit=$('#cus_credit_limit').val();
   var priority=$('#priority').val();
   var route_id=$('#route_id').val();
   var cus_nic=$('#cus_nic').val();
   var owner_name=$('#owner_name').val();
   	$.post( "<?php echo base_url('customers/save_customer'); ?>", {type:type, cus_id:cus_id, country_id:country_id, city_name:city_name, cus_name:cus_name, cus_code:cus_code, cus_email:cus_email, cus_phone:cus_phone, cus_address:cus_address, cus_state:cus_state, cus_postal_code:cus_postal_code,cus_credit_limit:cus_credit_limit,route_id:route_id,priority:priority,cus_nic:cus_nic,owner_name:owner_name })
   	  .done(function( data ) {
   		
   	  var obj = jQuery.parseJSON(data);
   	 if (obj.status==0) 
   	{
   		$('div#error').html('<div class="alert alert-block alert-danger fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4 class="alert-heading"><i class="fa fa-times-circle"></i> Error!</h4>'+obj.validation+'</div>');
   		$('body').modalmanager('removeLoading');
   		$('body').attr('class','');
   	} else {
   	  // alert(obj.type); //last id
   	  
   	  /**/
   	  if($('#customer_id')){
   		  var data = {
   	
   		id: obj.id,
   	
   		text: $('#cus_name').val()+' / '+$('#cus_phone').val()
   	
   		};
   	
   		var newOption = new Option(data.text, data.id, false, true);
   	
   		$('#customer_id').append(newOption).trigger('change');
   	
   		$('#add_item').focus();
   	  }
   	  else loadGrid();
   	  /**/
   	  
   	  $('div#ajax-modal').modal('hide');
   	  // load customer data
   		
   	  if(obj.type=='E'){
   		 displayNotice('page','Customer has been updated successfully!');
   		  location.reload(true);
   	  }
   	  if(obj.type=='A'){
   		displayNotice('page','Customer has been added successfully!')  ;
   		  location.reload(true);
   	  }		
   	}
   	  
   
   	  });
   return false;
   }
       
</script>