$(document).ready(function(){
  $("#sameAsShippingAddress").click(function(){
      if($(this).is(':checked')){
      
      var address_line1 = $("#billaddline1").val();
      var address_line2 = $("#billaddline2").val();
      var address_line3 = $("#billaddline3").val();
	  var country = $("#country_select").val();
	  var cityid;
	  var cityname=$('#CreateCity').find(":selected").text();
      var state = $("#State").val();
	  var stateid = $("#State").data("state-id");
		console.log(country);
	  if(country != "India"){
		  console.log('im here');
		   cityid = $('#billCityManual').val();
		   cityname = $('.billCityManual').val();
		   $('.shipCityManual').removeClass('hide');
		   $('.shipCityManual').val(cityname);
		   $('.shipCityManual').prop('readonly', true);
			$('#CreateShiopToCity').next(".select2-container").hide();
			$("#country_select_shipping").append('<option value="'+country+'"  selected="selected">'+country+'</option>'); 
	  $("#ShipToState").attr("data-state-id",'96');
	  }else{
	        cityid = $('.billCity').val();
			console.log(cityid+"thisis callled");
			
			$("#CreateShiopToCity").append('<option value="'+cityid+'" id="appendedcity" selected="selected">'+cityname+'</option>'); 

			$('#CreateShiopToCity').next(".select2-container").show();
			$('.shipCityManual').addClass('hide');
			$('.shipCityManual').val('');
			$("#country_select_shipping").append('<option value="'+country+'"  selected="selected">'+country+'</option>'); 
			 $("#ShipToState").attr("data-state-id",stateid);
	  }
	
	  
      $('#country_select_shipping').prop('disabled', true);
			$('#CreateShiopToCity').prop('disabled', true);
      var pincode = $("#Pincode").val();
      var mobile = $("#billmobile").val();
      var companyname = $(".cname").val();

      //set the variable in lower form with vars set above
      $("#shipaddline1").attr('readonly',true);
      $("#shipaddline1").val(address_line1);
      $("#shipaddline2").attr('readonly',true);
      $("#shipaddline2").val(address_line2);
      $("#shipaddline3").attr('readonly',true);
      $("#shipaddline3").val(address_line3);
      // $("#CreateShiopToCity").prop("disabled", true);      
      $("#ShipToPincode").attr('readonly',true);
      $("#ShipToPincode").val(pincode);
      $("#ShipToState").val(state);
     // $("#ShipToState").attr("data-state-id",stateid);
      $("#ShipToCountry").val(country);
      $("#ShipToMobile").attr('readonly',true);
      $("#ShipToMobile").val(mobile);
      $("#ShipTocompanyname").attr('readonly',true);
      $("#ShipTocompanyname").val(companyname);

      





      }else{
      //uncheck - clear input
      var cityname="";
      var state = "";
      $("#shipaddline1").attr('readonly',false);
      $("#shipaddline1").val("");
      $("#shipaddline2").attr('readonly',false);
      $("#shipaddline2").val("");
      $("#shipaddline3").attr('readonly',false);
      $("#shipaddline3").val("");
      $("#CreateShiopToCity").prop("disabled", false);
      
      $("#ShipToPincode").attr('readonly',false);
      $("#ShipToPincode").val("");
      $("#ShipToDistrict").val("");
      $("#ShipToState").val("");
      $("#ShipToCountry").val("");
      $("#ShipToMobile").attr('readonly',false);
      $("#ShipToMobile").val("");

      $("#ShipTocompanyname").attr('readonly',false);
      $("#ShipTocompanyname").val("");
		
		$("#country_select_shipping").append('<option value="'+0+'"  selected="selected">'+"-select country-"+'</option>'); 
		$("#CreateShiopToCity").append('<option value="'+0+'"  selected="selected">'+"-search city-"+'</option>'); 

			$('#CreateShiopToCity').next(".select2-container").show();
			$('.shipCityManual').addClass('hide');
	$('#country_select_shipping').prop('disabled', false);
			$('#CreateShiopToCity').prop('disabled', false);
      $('#ShipToStreet').change(function(){

        var ship_street = $('#ShipToStreet').val();
    
        if(ship_street==""){
          
          $('#ShipToStreetblock').removeClass("hide");
          
          $('#ShipToStreetblock').css("color","red");
          
          $('#ShipToStreetblock').text('Enter Street !!!');
    
        }
    
        else{
    
          $('#ShipToStreetblock').addClass("hide");
    
        }
    
      
      });
    
      $('#ShiopToCity').click(function(){
        
        var ship_city = $("#ShiopToCity").val();
    
        if(ship_city==""){
          
          $('#ShipToCityblock').removeClass("hide");
          
          $('#ShipToCityblock').css("color","red");
          
          $('#ShipToCityblock').text('Enter City !!!');
    
        }
    
        else{
    
          $('#ShipToCityblock').addClass("hide");
    
        }
    
      
      });
    
    
      $('#ShipToPincode').change(function(){
    
        var ship_pincode = $('#ShipToPincode').val();
    
        var filter = /^[1-9][0-9]{5}$/;
    
        if(ship_pincode==""){
          
          $('#ShipToPincodeblock').removeClass("hide");
          
          $('#ShipToPincodeblock').css("color","red");
          
          $('#ShipToPincodeblock').text('Enter Pincode !!!');
    
        }
    
        else if(!filter.test(ship_pincode))
    
        {
          
          $('#ShipToPincodeblock').removeClass("hide");
          
          $('#ShipToPincodeblock').css("color","red");
          
          $('#ShipToPincodeblock').text('Invalid Pincode !!!');
    
        }
    
        else{
    
          $('#ShipToPincodeblock').addClass("hide");
    
        }
    
      
      });
    
      $('#ShipToMobile').change(function(){
    
        var ship_mobile = $('#ShipToMobile').val();
    
        var filter = /^\d{10}$/;
    
        if(ship_mobile==""){
          
          $('#ShipToMobileblock').removeClass("hide");
          
          $('#ShipToMobileblock').css("color","red");
          
          $('#ShipToMobileblock').text('Enter Mobile Number !!!');
    
        }
    
        else if(!filter.test(ship_mobile))
    
        {
          
          $('#ShipToMobileblock').removeClass("hide");
          
          $('#ShipToMobileblock').css("color","red");
          
          $('#ShipToMobileblock').text('Invalid Mobile Number !!!');
    
        }
    
        else{
    
          $('#ShipToMobileblock').addClass("hide");
    
        }
      });

    }
  });

      $('#billaddline1').on("change", function () {
        var address_line1=$("#billaddline1").val();
        $("#shipaddline1").attr('readonly',true);
        $("#shipaddline1").val(address_line1);
      });

      $('#billaddline2').on("change", function () {
        var address_line2=$("#billaddline2").val();
        $("#shipaddline2").attr('readonly',true);
        $("#shipaddline2").val(address_line2);
      });

      $('#billaddline3').on("change", function () {
        var address_line3=$("#billaddline1").val();
        $("#shipaddline3").attr('readonly',true);
        $("#shipaddline3").val(address_line3);
      });

      
      $('#CreateCity').on("change", function () {

		if($('#sameAsShippingAddress').is(':checked')){
			
        var cityid = $('#CreateCity').find(":selected").val();
        var cityname=$('#CreateCity').find(":selected").text();
        
        $("#CreateShiopToCity").empty();
        
        $("#CreateShiopToCity").prop("disabled", true);      

        $("#CreateShiopToCity").append('<option value="'+cityid+'" id="appendedcity" selected="selected">'+cityname+'</option>'); 

        var state = $("#State").val();
        var stateid = $("#State").data("state-id");

        var country=$("#Country").val();
        $("#ShipToCountry").attr('readonly',true);
        $("#ShipToCountry").val(country);
        $("#ShipToState").val(state);
		if(country !="India"){
        $("#ShipToState").attr("data-state-id",'96');
		}else{
        $("#ShipToState").attr("data-state-id",stateid);
        }
		}       
      });

      $('#Pincode').on("change", function () {
        var pincode=$("#Pincode").val();
        $("#ShipToPincode").attr('readonly',true);
        $("#ShipToPincode").val(pincode);
      });

      $('#billmobile').on("change", function () {
        var mobile=$("#billmobile").val();
        $("#ShipToMobile").attr('readonly',true);
        $("#ShipToMobile").val(mobile);
      });

  
  $('.addedemail').hide();
  $('.addedmobile').hide();  
 



count=1;
$('#addshippingaddresscreate').on('click',function(){

  count++;
  if(count<=100)
  {
    addaddress(count);
  } 
  else
  {
    
    alert("This the Maximum limit to adding address");
    $('#addshippingaddress').hide();
  }

 

});

  $(document).on('click', 'div.addressblock .fa-chevron-down', function () {
  
    console.log($(this).data('id'));
    var gettingid=$(this).data('id');
    // $(document).find(".addressblock .fa-chevron-down").toggleClass("rtoate180");
    $(document).find(".addressblockdetails-"+gettingid).stop().slideToggle(500);

  });

  $(document).on('click', 'div.addressblock .fa-minus', function () {

    console.log($(this).data('id'));
    var gettingid=$(this).data('id');
    // $(document).find(".addressblock .fa-chevron-down").toggleClass("rtoate180");
    $(document).find(".addressblock-"+gettingid).remove();
    count--;
    console.log(count);
  
    if(count<=100)
  {
    $('#addshippingaddress').show();
  } 
    

  });

});


$(document).on('click', '#addalternate_email', function () {

  // $('#alternate_emailblock').append('<div class="addedemail">'+'<div class="col-md-6 pull-right">'+'<div class="form-group">'+'<label class="control-label" for="inputSuccess"> Alternate Email Id</label>'+'<input type="text" class="form-control cemail" name="company_email" id="alternate_cemail" style="width:300px;">'+'<i class="fa fa-minus" id="remobealternate_email"></i>'+'<span class="help-block hide" id="cemailblock">Help block with success</span>'+'</div>'+'</div>'+'</div>');

  $('.addedemail').show(); 
  
  $('#addalternate_email').hide(); 





});

$(document).on('click', '#remobealternate_email', function () {

  $('.addedemail').hide(); 

  $('#addalternate_email').show(); 

});

$(document).on('click', '#addalternate_mobile', function () {

  // $('#alternate_mobileblock').append('<div class="addedmobile">'+'<div class="col-md-6">'+'<div class="form-group">'+'<label class="control-label" for="ShipToMobile"> Alternate Mobile No</label>'+'<input type="text" class="form-control shipmobile mobile alternate_mobile" id="alternate_mobile" name="ShipToMobile" maxlength="10" style="width: 300px;">'+'<i class="fa fa-minus" id="remobealternate_mobile"></i>'+'<span class="help-block hide mobileblock" id="ShipToMobileblock">Help block with success</span>'+'</div>'+'</div>');

  $('.addedmobile').show(); 

  $('#addalternate_mobile').hide();

});

$(document).on('click', '#remobealternate_mobile', function () {

  $('.addedmobile').hide(); 

  $('#addalternate_mobile').show();

});








$(document).on('click', '.addmobilebutton', function () {

  var gettingid=$(this).data('id');
  
  $('.addedmobile-'+gettingid).show();

  // $('.alternate_mobileblock-'+gettingid).append('<div class="addedmobile addedmobile-'+count+'">'+'<div class="col-md-6">'+'<div class="form-group">'+'<label class="control-label" for="ShipToMobile"> Alternate Mobile No</label>'+'<input type="text" class="form-control shipmobile mobile alternate_mobile" id="alternate_mobile" name="ShipToMobile" maxlength="10" style="width: 300px;">'+'<i class="fa fa-minus-circle remobealternate_mobile" data-id="'+gettingid+'"></i>'+'<span class="help-block hide mobileblock" id="ShipToMobileblock">Help block with success</span>'+'</div>'+'</div>');

  $('.addmobilebutton-'+gettingid).hide();

});

$(document).on('click', '.remobealternate_mobile', function () {

  var gettingid=$(this).data('id');
  
  $('.addedmobile-'+gettingid).hide();

  // $('.addedmobile-'+gettingid).css("display","none");

  // // $('.addedmobile-'+gettingid).remove();

  $('.addmobilebutton-'+gettingid).show();

});


$(document).on('click', '.addshipcity', function () {
 
  var gettingid=$(this).data('id');

  citystate(gettingid);

});


  function addaddress(count){
      var section="";

         
      section=section+

    '<div class="addressblock addressblock-'+count+'" data-id="'+count+'">'+

      '<label class="control-label" for="Street">Address '+count+'</label>'+

      '&nbsp&nbsp<i class="fa fa-chevron-down" data-id="'+count+'"></i>'+

      '<i class="fa fa-minus pull-right" data-id="'+count+'"></i>'+

      '<div class="row">'+
										
				'<div class="col-md-6">'+
											
					'<label class="control-label" for="Street" >Set Address Name</label>'+
										
					'<input type="text" class="form-control address_name" value="Address '+count+'" autocomplete="off" />'+

				'</div>'+

			'</div>'+

      '<div class="addressblockdetails-'+count+'">'+

        '<div class="row">'+

          '<div class="col-md-4">'+
                    
            '<div class="form-group">'+

              '<label class="control-label" for="Street">Address Line:1 <sup><i class="fa fa-asterisk" style="font-size: 8px;" data-toggle="tooltip" data-placement="right" title="" data-original-title="THIS FIELD IS REQUIRED"></i></sup></label>'+

              '<input type="text" name="street" class="form-control shipaddl1" id="shipaddline1" maxlength="30">'+

              '<span class="help-block hide" id="bill_streetblock">Help block with success</span>'+

            '</div>'+

          '</div>'+

          '<div class="col-md-4">'+
                    
            '<div class="form-group">'+

              '<label class="control-label" for="Street">Address Line:2 <sup><i class="fa fa-asterisk" style="font-size: 8px;" data-toggle="tooltip" data-placement="right" title="" data-original-title="THIS FIELD IS REQUIRED"></i></sup></label>'+

              '<input type="text" name="street" class="form-control shipaddl2" id="shipaddline2" maxlength="30">'+

              '<span class="help-block hide" id="bill_streetblock">Help block with success</span>'+

            '</div>'+

          '</div>'+

          '<div class="col-md-4">'+
                    
            '<div class="form-group">'+

              '<label class="control-label" for="Street">Address Line:3</label>'+

              '<input type="text" name="street" class="form-control shipaddl3" id="shipaddline3" maxlength="30">'+

              '<span class="help-block hide" id="bill_streetblock">Help block with success</span>'+

            '</div>'+

          '</div>'+

        '</div>'+

        '<div class="row">'+

          '<div class="col-md-6">'+

            '<div class="form-group">'+

              '<label>City <sup><i class="fa fa-asterisk" style="font-size: 8px;" data-toggle="tooltip" data-placement="right" title="" data-original-title="THIS FIELD IS REQUIRED"></i></sup></label>'+

              '<select class="form-control  select2-cityship addshipcity shipcity added_city-'+count+'" id="CreateShiopToCity" name="CreateShiopToCity" style="width: 100%;" data-id="'+count+'">'+

                '<option value="0">- Search city -</option>'+

              '</select>'+

              '<span class="help-block hide" id="ShipToCityblock">Help block with success</span>'+

            '</div>'+

          '</div>'+

          '<div class="col-md-6">'+

            '<div class="form-group">'+

              '<label class="control-label" for="ShipToState"> State <sup><i class="fa fa-asterisk" style="font-size: 8px;" data-toggle="tooltip" data-placement="right" title="" data-original-title="THIS FIELD IS REQUIRED"></i></sup></label>'+

              '<input type="text" class="form-control shipstate shipstate-'+count+'" id="ShipToState" name="ShipToState" readonly data-state-id="">'+

              '<span class="help-block hide">Help block with success</span>'+

            '</div>'+

          '</div>'+          

        '</div>'+

        '<div class="row">'+

          '<div class="col-md-6">'+

            '<div class="form-group">'+

              '<label class="control-label" for="ShipToCountry"> Country <sup><i class="fa fa-asterisk" style="font-size: 8px;" data-toggle="tooltip" data-placement="right" title="" data-original-title="THIS FIELD IS REQUIRED"></i></sup></label>'+

              '<input type="text" class="form-control shipcountry shipcountry-'+count+'" id="ShipToCountry" name="ShipToCountry" readonly>'+

              '<span class="help-block hide">Help block with success</span>'+

            '</div>'+

          '</div>'+

          '<div class="col-md-6">'+

            '<div class="form-group">'+

              '<label class="control-label" for="ShipToPincode"> Pincode <sup><i class="fa fa-asterisk" style="font-size: 8px;" data-toggle="tooltip" data-placement="right" title="" data-original-title="THIS FIELD IS REQUIRED"></i></sup></label>'+

              '<input type="text" class="form-control only-numeric shippincode" name="ShipToPincode" id="ShipToPincode"  data-id="" maxlength="6" minlength="6">'+

              '<span class="help-block hide num-error" id="ShipToPincodeblock">Help block with success</span>'+

            '</div>'+

          '</div>'+

        '</div>'+

        '<div class="row">'+           

          '<div class="col-md-6">'+
                
            '<div class="form-group">'+

              '<label class="control-label" for="ShipToMobile"> Mobile No <sup><i class="fa fa-asterisk" style="font-size: 8px;" data-toggle="tooltip" data-placement="right" title="" data-original-title="THIS FIELD IS REQUIRED"></i></sup></label>'+

              '<div class="input-group">'+
              
                '<input type="text" class="form-control shipmobile" id="ShipToMobile" name="ShipToMobile" maxlength="10" minlength="10">'+

                '<span class="input-group-addon"><i class="fa fa-plus-circle addmobilebutton addmobilebutton-'+count+'"data-toggle="tooltip" data-placement="right" title="" data-original-title="ADD ALTERNATE MOBILE NUMBER" data-id="'+count+'"></i></span>'+

              '</div>'+

              '<span class="help-block hide" id="ShipToMobileblock">Help block with success</span>'+

            '</div>'+
                    
          '</div>'+

          '<div class="col-md-6">'+

						'<div class="form-group">'+
											
							'<label class="control-label" for="Street" >Receiver name <sup><i class="fa fa-asterisk" style="font-size: 8px;" data-toggle="tooltip" data-placement="right" title="" data-original-title="THIS FIELD IS REQUIRED"></i></sup></label>'+
										
							'<input type="text" class="form-control receiver_name" autocomplete="off" />'+

						'</div>'+

					'</div>'+

        '</div>'+

        '<div class="row alternate_mobileblock-'+count+'">'+

          '<div class="addedmobile addedmobile-'+count+'" style="display:none;">'+
        
            '<div class="col-md-6">'+
            
              '<div class="form-group">'+
            
                '<label class="control-label" for="ShipToMobile"> Alternate Mobile No</label>'+

                '<div class="input-group">'+
              
                  '<input type="text" class="form-control shipmobile mobile alternate_mobile" id="alternate_mobile" name="ShipToMobile" maxlength="10" minlength="10">'+
              
                  '<span class="input-group-addon"><i class="fa fa-minus-circle remobealternate_mobile" data-id="'+count+'"></i></span>'+

                '</div>'+
              
                '<span class="help-block hide mobileblock" id="ShipToMobileblock">Help block with success</span>'+
              
              '</div>'+
              
            '</div>'+

          '</div>'+

          '<div class="col-md-6 pull-right">'+

					  '<div class="form-group">'+

						  '<label class="control-label" for="ShipToPincode"> Ship Company Name</label>'+

						  '<input type="text" class="form-control shipTocompanyname" name="ShipTocompanyname" id="ShipTocompanyname">'+

						  '<span class="help-block hide " id="ShipTocompanynameblock">Help block with success</span>'+

					  '</div>'+

					'</div>'+

			  '</div>'+

      '</div>'+

    '</div>';
         
        $('#shippingaddresssection').append(section);

  }

  function citystate(gettingid)
  {
    $('.added_city-'+gettingid).select2({
      ajax: {
             url: url_getcity,
             type: "get",
             dataType: 'json',
             delay: 100,
             data: function (params) {
                return {
                   searchTerm: params.term // search term
                };
             },
             processResults: function (response) {
                return {
                   results: response
                };		   
            },
             cache: true
           }
      });
      
      $('.added_city-'+gettingid).on('select2:select', function (e) {
        console.log("next");
           var city = e.params.data.id;
           console.log(city);
           $.ajax(url_getstatefromcity,{
              type : 'get',
              data :{'city' : city} ,
              success : function(data){
                 
                 console.log(data);
                 $(document).find('.shipstate-'+gettingid).val(data.state.state_title);
                 $(document).find('.shipstate-'+gettingid).attr("data-state-id",data.district.state_id);
                 
                 $(document).find('.shipcountry-'+gettingid).val("India");
                 
              },
              error: function (jqXhr, textStatus, errorMessage) {
                 console.log('Error' + errorMessage);
         }
           });
     });
  }