Advertisement
Guest User

Untitled

a guest
Sep 27th, 2014
2,829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.69 KB | None | 0 0
  1. $( document ).ready(function() {
  2.     var dataType = "";
  3.     $("#country").on("focus", function() {
  4.         dataType = "country";
  5.     });
  6.     $("#state").on("focus", function() {
  7.         dataType = "state";
  8.     });
  9.     $("#city").on("focus", function() {
  10.         dataType = "city";
  11.     });
  12.     $(".dataFloat").hover( function() {
  13.         $(".dataLabel").removeClass("itemhover");
  14.     });
  15.     $("#country").focus();
  16.     $("#country").on("input", function() {
  17.         if($("#country").val() == ""){
  18.             $("#dataCountry").hide();
  19.         } else {
  20.             $("#dataCountry").show();
  21.             var getText = $("#country").val();
  22.             $.ajax({
  23.                 type : 'post',
  24.                 url : 'get_country.php',
  25.                 data: { country: getText }
  26.             })
  27.             .done(function( msg ) {
  28.                 $("#dataCountry").html( msg );
  29.             });
  30.         }
  31.     });
  32.  
  33.     $("#country").on("dblclick", function() {
  34.         $("#dataCountry").show();
  35.         var getText = $("#country").val();
  36.         $.ajax({
  37.             type : 'post',
  38.             url : 'get_country.php',
  39.             data: { country: getText }
  40.         })
  41.         .done(function( msg ) {
  42.             $("#dataCountry").html( msg );
  43.         });
  44.     });
  45.  
  46.     $("#state").on("dblclick", function() {
  47.         $("#dataState").show();
  48.         var getText = $("#country").val();
  49.         var getState = $("#state").val();
  50.         $.ajax({
  51.             type : 'post',
  52.             url : 'get_states.php',
  53.             data: { state: getState, country: getText }
  54.         })
  55.         .done(function( msg ) {
  56.             $("#dataState").html( msg );
  57.         });
  58.     });
  59.  
  60.     $("#city").on("dblclick", function() {
  61.         $("#dataCity").show();
  62.         var getText = $("#country").val();
  63.         var getState = $("#state").val();
  64.         var getCity = $("#city").val();
  65.         $.ajax({
  66.             type : 'post',
  67.             url : 'get_city.php',
  68.             data: { state: getState, country: getText, city: getCity }
  69.         })
  70.         .done(function( msg ) {
  71.             $("#dataCity").html( msg );
  72.         });
  73.     });
  74.  
  75.     $("#country").on("click", function() {
  76.         if($("#country").val() == ""){
  77.             $("#dataCountry").hide();
  78.         }
  79.     });
  80.  
  81.     $("#state").on("click", function() {
  82.         if($("#state").val() == ""){
  83.             $("#dataState").hide();
  84.         }
  85.     });
  86.  
  87.     $("#city").on("click", function() {
  88.         if($("#city").val() == ""){
  89.             $("#dataCity").hide();
  90.         }
  91.     });
  92.  
  93.     $("#state").on("input", function() {
  94.         if($("#state").val() == ""){
  95.             $("#dataState").hide();
  96.         } else {
  97.             $("#dataState").show();
  98.             var getText = $("#country").val();
  99.             var getState = $("#state").val();
  100.             $.ajax({
  101.                 type : 'post',
  102.                 url : 'get_states.php',
  103.                 data: { state: getState, country: getText }
  104.             })
  105.             .done(function( msg ) {
  106.                 $("#dataState").html( msg );
  107.             });
  108.         }
  109.     });
  110.  
  111.     $("#city").on("input", function() {
  112.         if($("#city").val() == ""){
  113.             $("#dataCity").hide();
  114.         } else {
  115.             $("#dataCity").show();
  116.             var getText = $("#country").val();
  117.             var getState = $("#state").val();
  118.             var getCity = $("#city").val();
  119.             $.ajax({
  120.                 type : 'post',
  121.                 url : 'get_city.php',
  122.                 data: { state: getState, country: getText, city: getCity }
  123.             })
  124.             .done(function( msg ) {
  125.                 if (msg.indexOf("has no states") > -1){
  126.                     insertValState("");
  127.                 }
  128.                 if (msg == "No State"){
  129.                     insertValState("");
  130.                 }
  131.                 $("#dataCity").html( msg );
  132.             });
  133.         }
  134.     });
  135.  
  136.     $(document).keypress(function(e) {
  137.         switch(e.keyCode) {
  138.             case 38:
  139.                 navigate('up');
  140.                 break;
  141.             case 40:
  142.                 navigate('down');
  143.                 break;
  144.             case 13:
  145.                 if(currentData != '' && dataType != '') {
  146.                     if(dataType == "country"){
  147.                         insertValCountry(currentData);
  148.                         currentSelection = -1;
  149.                     } else if(dataType == "state"){
  150.                         insertValState(currentData);
  151.                         currentSelection = -1;
  152.                     } else if(dataType == "city"){
  153.                         insertValCity(currentData);
  154.                         currentSelection = -1;
  155.                     }
  156.                 }
  157.                 break;
  158.         }
  159.     });
  160. });
  161.  
  162. var currentSelection = -1;
  163. var scroll = 0;
  164.  
  165. function navigate(direction) {
  166.     if($(".dataLabel").lenth == 0) {
  167.         currentSelection = -1;
  168.     }
  169.  
  170.     if(direction == 'up' && currentSelection != -1) {
  171.         if(currentSelection != 0) {
  172.             currentSelection--;
  173.             if(scroll > 0){
  174.                 scroll-=21;
  175.             }
  176.         }
  177.     } else if (direction == 'down') {
  178.         if(currentSelection != $(".dataLabel").length -1) {
  179.             if(currentSelection > 7){
  180.                 scroll+=21;
  181.             }
  182.             currentSelection++;
  183.         }
  184.     }
  185.     setSelected(currentSelection);
  186. }
  187.  
  188. function setSelected(menuitem) {
  189.     $(".dataLabel").removeClass("itemhover");
  190.     $(".dataLabel").eq(menuitem).addClass("itemhover");
  191.     currentData = $(".dataLabel").eq(menuitem).text();
  192.     $(".dataFloat").scrollTop(scroll);
  193. }
  194.  
  195. function insertValCountry(myVal){
  196.     $("#country").val(myVal);
  197.     $("#dataCountry").html("");
  198.     $("#dataCountry").hide();
  199.     $("#country").attr("disabled", "disabled");
  200.     $("#country").css("background-color", "#39db00");
  201.     $(".state").show();
  202.     $("#delCountry").show();
  203. }
  204.  
  205. function insertValState(myVal){
  206.     $("#state").val(myVal);
  207.     $("#dataState").html("");
  208.     $("#dataState").hide();
  209.     $("#state").attr("disabled", "disabled");
  210.     $("#state").css("background-color", "#39db00");
  211.     $(".city").show();
  212.     $("#delState").show();
  213. }
  214.  
  215. function insertValCity(myVal){
  216.     $("#city").val(myVal);
  217.     $("#dataCity").html("");
  218.     $("#dataCity").hide();
  219.     $("#city").attr("disabled", "disabled");
  220.     $("#city").css("background-color", "#39db00");
  221.     $("#delCity").show();
  222.     showData();
  223. }
  224.  
  225. function delCountry(){
  226.     $("#country").val("");
  227.     $("#state").val("");
  228.     $("#city").val("");
  229.     $("#country").removeAttr("disabled");
  230.     $("#state").removeAttr("disabled");
  231.     $("#city").removeAttr("disabled");
  232.     $("#country").css("background-color", "#fff");
  233.     $("#state").css("background-color", "#fff");
  234.     $("#city").css("background-color", "#fff");
  235.     $(".state").hide();
  236.     $(".city").hide();
  237.     $("#delCountry").hide();
  238.     $("#delState").hide();
  239.     $("#delCity").hide();
  240.     $(".dataFloat").html("");
  241.     $(".dataFloat").hide();
  242.     $(".showData").html("");
  243.     $(".showData").hide();
  244.     $("#country").focus();
  245. }
  246.  
  247. function delState(){
  248.     $("#state").val("");
  249.     $("#city").val("");
  250.     $("#state").removeAttr("disabled");
  251.     $("#city").removeAttr("disabled");
  252.     $("#state").css("background-color", "#fff");
  253.     $("#city").css("background-color", "#fff");
  254.     $(".city").hide();
  255.     $("#delState").hide();
  256.     $("#delCity").hide();
  257.     $(".dataFloat").html("");
  258.     $(".dataFloat").hide();
  259.     $(".showData").html("");
  260.     $(".showData").hide();
  261.     $("#state").focus();
  262. }
  263.  
  264. function delCity(){
  265.     $("#city").val("");
  266.     $("#city").removeAttr("disabled");
  267.     $("#city").css("background-color", "#fff");
  268.     $("#delCity").hide();
  269.     $(".dataFloat").html("");
  270.     $(".dataFloat").hide();
  271.     $(".showData").html("");
  272.     $(".showData").hide();
  273.     $("#city").focus();
  274. }
  275.  
  276. function showData(){
  277.     $(".showData").show();
  278.     var getText = $("#country").val();
  279.     var getState = $("#state").val();
  280.     var getCity = $("#city").val();
  281.     $.ajax({
  282.         type : 'post',
  283.         url : 'get_data.php',
  284.         data: { state: getState, country: getText, city: getCity }
  285.     })
  286.     .done(function( msg ) {
  287.         $(".showData").html( msg );
  288.     });
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement