$(function() {
  
        
        function selectAllText(textbox) {
            textbox.focus();
            textbox.select();
        }

        //Drop Down Number # 1
        $( "#flagTxt1" ).autocomplete({
                minLength: 0,
                source: flags,
                focus: function( event, ui ) {
                        $( "#flagTxt1" ).val(  ui.item.value );
                        $( "#flag-icon1" ).attr( "src", ui.item.icon );
                        return false;
                },
                select: function( event, ui ) {
                        $( "#flagTxt1" ).val(  ui.item.value );
                        $( "#flagTxt-id1" ).val( ui.item.value );
                        $( "#flag-icon1" ).attr( "src", ui.item.icon );
                        $('#storeChangeEvent1').val( 'true' );
                        $('#storeFlag1').val( ui.item.icon );
                        $('#storeCurrency1').val( ui.item.value );
                        $('#storeChangeEvent1').val( 'true' );
						
						var amount = $.trim($('#edit-amount-sell').val());
var langid = $.trim($('#langid').val());

                        var source = $.trim($('#flagTxt1').val());
                        var destination = $.trim($('#flagTxt2').val());
						 $.ajax({ 
						  url: "responsesell",
						  data: "amount=" + encodeURI(amount)+"&source="+encodeURI(source)+"&destination="+encodeURI(destination),
						  success: function(msg){
							eval(msg);
							
						  }
						});
                        return false;
                },
                create: function(event, ui){
                    $('#storeFlag1').val( $( "#flag-icon1" ).attr( "src") );
                    $('#storeCurrency1').val( $( "#flagTxt1" ).val() );
                    $('#storeChangeEvent1').val( 'false' );
                },
                open: function (event, ui){
                    $('#storeChangeEvent1').val( 'false' );
                },
                change: function (event, ui){
                    var getChangeStatus = $('#storeChangeEvent1').val();
                    if('false' == getChangeStatus){
                        $( "#flagTxt1" ).val( $('#storeCurrency1').val() );
                        $( "#flag-icon1" ).attr( "src", $('#storeFlag1').val() );
                    }
                },
                close: function(event, ui){
                    var getChangeStatus = $('#storeChangeEvent1').val();
                    if('false' == getChangeStatus){
                        $( "#flagTxt1" ).val( $('#storeCurrency1').val() );
                        $( "#flag-icon1" ).attr( "src", $('#storeFlag1').val() );
                    }
                }
        })
        .data( "autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( " <a><img src='"+ item.icon +"'>" + item.label + "<br>" + item.desc + "</a>" )
                        .appendTo( ul );
        };


        $('#dropArrow1, #flagTxt1, #flag-icon1').click(function(){

                if ( $('#flagTxt1').autocomplete( "widget" ).is( ":visible" ) ) {
                        $('#flagTxt1').autocomplete( "close" );
                        return;
                }

                $('#flagTxt1').autocomplete( "search", '' );
                selectAllText(jQuery('#flagTxt1'))
                
        });

        //jQuery('#flagTxt1').click(function() { selectAllText(jQuery('#flagTxt1')) });
        
        //END Drop Down Number # 1

        //~~~~~~~~~~###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        //Drop Down Number # 2
        $( "#flagTxt2" ).autocomplete({
                minLength: 0,
                source: flags,
                focus: function( event, ui ) {
                        $( "#flagTxt2" ).val(  ui.item.value );
                        $( "#flag-icon2" ).attr( "src", ui.item.icon );
                        return false;
                },
                select: function( event, ui ) {
                        $( "#flagTxt2" ).val(  ui.item.value );
                        $( "#flagTxt-id2" ).val( ui.item.value );
                        $( "#flag-icon2" ).attr( "src", ui.item.icon );
                        $('#storeChangeEvent2').val( 'true' );
                        $('#storeFlag2').val( ui.item.icon );
                        $('#storeCurrency2').val( ui.item.value );
                        $('#storeChangeEvent2').val( 'true' );
						
						var amount = $.trim($('#edit-amount-sell').val());
							var source = $.trim($('#flagTxt1').val());
							var destination = $.trim($('#flagTxt2').val());
                                       var langid = $.trim($('#langid').val());

			 
							$.ajax({ 
					  url: "responsesell",
					  data: "amount=" + encodeURI(amount)+"&source="+encodeURI(source)+"&destination="+encodeURI(destination),
					  success: function(msg){
						eval(msg);
						
					  }
					});
		
		
                        return false;
                },
                create: function(event, ui){
                    $('#storeFlag2').val( $( "#flag-icon2" ).attr( "src") );
                    $('#storeCurrency2').val( $( "#flagTxt2" ).val() );
                    $('#storeChangeEvent2').val( 'false' );
                },
                open: function (event, ui){
                    $('#storeChangeEvent2').val( 'false' );
                },
                change: function (event, ui){
                    var getChangeStatus = $('#storeChangeEvent2').val();
                    if('false' == getChangeStatus){
                        $( "#flagTxt2" ).val( $('#storeCurrency2').val() );
                        $( "#flag-icon2" ).attr( "src", $('#storeFlag2').val() );
                    }
                },
                close: function(event, ui){
                    var getChangeStatus = $('#storeChangeEvent2').val();
                    if('false' == getChangeStatus){
                        $( "#flagTxt2" ).val( $('#storeCurrency2').val() );
                        $( "#flag-icon2" ).attr( "src", $('#storeFlag2').val() );
                    }
                }
        })
        .data( "autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( " <a><img src='"+ item.icon +"'>" + item.label + "<br>" + item.desc + "</a>" )
                        .appendTo( ul );
        };


        $('#dropArrow2, #flagTxt2, #flag-icon2').click(function(){

                if ( $('#flagTxt2').autocomplete( "widget" ).is( ":visible" ) ) {
                        $('#flagTxt2').autocomplete( "close" );
                        return;
                }

                $('#flagTxt2').autocomplete( "search", '' );
                selectAllText(jQuery('#flagTxt2'))
        });
        //END Drop Down Number # 2



        //~~~~~~~~~~###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        //Drop Down Number # 3
        $( "#flagTxt3" ).autocomplete({
                minLength: 0,
                source: flags,
                focus: function( event, ui ) {
                        $( "#flagTxt3" ).val(  ui.item.value );
                        $( "#flag-icon3" ).attr( "src", ui.item.icon );
                        return false;
                },
                select: function( event, ui ) {
                        $( "#flagTxt3" ).val(  ui.item.value );
                        $( "#flagTxt-id3" ).val( ui.item.value );
                        $( "#flag-icon3" ).attr( "src", ui.item.icon );
                        $('#storeChangeEvent3').val( 'true' );
                        $('#storeFlag3').val( ui.item.icon );
                        $('#storeCurrency3').val( ui.item.value );
                        $('#storeChangeEvent3').val( 'true' );
						
						var amount = $.trim($('#edit-amount-buy').val());
                var source = $.trim($('#flagTxt4').val());
                var destination = $.trim($('#flagTxt3').val());
                var langid2  = $.trim($('#langid2').val());

              
						$.ajax({ 
				  url: "responsebuy",
				  data: "amount=" + encodeURI(amount)+"&source="+encodeURI(source)+"&destination="+encodeURI(destination),
				  success: function(msg){
					eval(msg);
					
				  }
				});
		
                        return false;
                },
                create: function(event, ui){
                    $('#storeFlag3').val( $( "#flag-icon3" ).attr( "src") );
                    $('#storeCurrency3').val( $( "#flagTxt3" ).val() );
                    $('#storeChangeEvent3').val( 'false' );
                },
                open: function (event, ui){
                    $('#storeChangeEvent3').val( 'false' );
                },
                change: function (event, ui){
                    var getChangeStatus = $('#storeChangeEvent3').val();
                    if('false' == getChangeStatus){
                        $( "#flagTxt3" ).val( $('#storeCurrency3').val() );
                        $( "#flag-icon3" ).attr( "src", $('#storeFlag3').val() );
                    }
                },
                close: function(event, ui){
                    var getChangeStatus = $('#storeChangeEvent3').val();
                    if('false' == getChangeStatus){
                        $( "#flagTxt3" ).val( $('#storeCurrency3').val() );
                        $( "#flag-icon3" ).attr( "src", $('#storeFlag3').val() );
                    }
                }
        })
        .data( "autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( " <a><img src='"+ item.icon +"'>" + item.label + "<br>" + item.desc + "</a>" )
                        .appendTo( ul );
        };


        $('#dropArrow3, #flagTxt3, #flag-icon3').click(function(){

                if ( $('#flagTxt3').autocomplete( "widget" ).is( ":visible" ) ) {
                        $('#flagTxt3').autocomplete( "close" );
                        return;
                }

                $('#flagTxt3').autocomplete( "search", '' );
                selectAllText(jQuery('#flagTxt3'))
        });
        //END Drop Down Number # 3


        //~~~~~~~~~~###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        //Drop Down Number # 4
        $( "#flagTxt4" ).autocomplete({
                minLength: 0,
                source: flags,
                focus: function( event, ui ) {
                        $( "#flagTxt4" ).val(  ui.item.value );
                        $( "#flag-icon4" ).attr( "src", ui.item.icon );
                        return false;
                },
                select: function( event, ui ) {
                        $( "#flagTxt4" ).val(  ui.item.value );
                        $( "#flagTxt-id4" ).val( ui.item.value );
                        $( "#flag-icon4" ).attr( "src", ui.item.icon );
                        $('#storeChangeEvent4').val( 'true' );
                        $('#storeFlag4').val( ui.item.icon );
                        $('#storeCurrency4').val( ui.item.value );
                        $('#storeChangeEvent4').val( 'true' );
						
 var langid2  = $.trim($('#langid2').val());
						var amount = $.trim($('#edit-amount-buy').val());
                var source = $.trim($('#flagTxt4').val());
                var destination = $.trim($('#flagTxt3').val());
 
						$.ajax({ 
				  url: "responsebuy",
				  data: "amount=" + encodeURI(amount)+"&source="+encodeURI(source)+"&destination="+encodeURI(destination),
				  success: function(msg){
					eval(msg);
					
				  }
				});
		
                        return false;
                },
                create: function(event, ui){
                    $('#storeFlag4').val( $( "#flag-icon4" ).attr( "src") );
                    $('#storeCurrency4').val( $( "#flagTxt4" ).val() );
                    $('#storeChangeEvent4').val( 'false' );
                },
                open: function (event, ui){
                    $('#storeChangeEvent4').val( 'false' );
                },
                change: function (event, ui){
                    var getChangeStatus = $('#storeChangeEvent4').val();
                    if('false' == getChangeStatus){
                        $( "#flagTxt4" ).val( $('#storeCurrency4').val() );
                        $( "#flag-icon4" ).attr( "src", $('#storeFlag4').val() );
                    }
                },
                close: function(event, ui){
                    var getChangeStatus = $('#storeChangeEvent4').val();
                    if('false' == getChangeStatus){
                        $( "#flagTxt4" ).val( $('#storeCurrency4').val() );
                        $( "#flag-icon4" ).attr( "src", $('#storeFlag4').val() );
                    }
                }
        })
        .data( "autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( " <a><img src='"+ item.icon +"'>" + item.label + "<br>" + item.desc + "</a>" )
                        .appendTo( ul );
        };


        $('#dropArrow4, #flagTxt4, #flag-icon4').click(function(){

                if ( $('#flagTxt4').autocomplete( "widget" ).is( ":visible" ) ) {
                        $('#flagTxt4').autocomplete( "close" );
                        return;
                }

                $('#flagTxt4').autocomplete( "search", '' );
                selectAllText(jQuery('#flagTxt4'))
        });
        //END Drop Down Number # 3



        

});
