//********************************** keypad js **********************************

/*security keypad*/

keys =
{
    isUpper : true,
    isScrambled : false,
    alphabet_a_U : "ABCDEFGHIJKLMNOPQRSTUVWXYZİĞÜŞÖÇ",
    alphabet_a_S : "abcdefghijklmnopqrstuvwxyzığüşöç",
    alphabet_n : "0123456789",
    activeInput : null,
    padInUse : false,

    alphas : 
    [  
        { name : '_a_1', value: 'A' },
        { name : '_a_2', value: 'B' },
        { name : '_a_3', value: 'C' },
        { name : '_a_4', value: 'D' },
        { name : '_a_5', value: 'E' },
        { name : '_a_6', value: 'F' },
        { name : '_a_7', value: 'G' },
        { name : '_a_8', value: 'H' },
        { name : '_a_9', value: 'I' },
        { name : '_a_10', value: 'J' },
        { name : '_a_11', value: 'K' },
        { name : '_a_12', value: 'L' },
        { name : '_a_13', value: 'M' },
        { name : '_a_14', value: 'N' },
        { name : '_a_15', value: 'O' },
        { name : '_a_16', value: 'P' },
        { name : '_a_17', value: 'Q' },
        { name : '_a_18', value: 'R' },
        { name : '_a_19', value: 'S' },
        { name : '_a_20', value: 'T' },
        { name : '_a_21', value: 'U' },
        { name : '_a_22', value: 'V' },
        { name : '_a_23', value: 'W' },
        { name : '_a_24', value: 'X' },
        { name : '_a_25', value: 'Y' },
        { name : '_a_26', value: 'Z' },
		
		{ name : '_a_27', value: 'İ' },
		{ name : '_a_28', value: 'Ğ' },
		{ name : '_a_29', value: 'Ü' },
		{ name : '_a_30', value: 'Ş' },
		{ name : '_a_31', value: 'Ö' },
		{ name : '_a_32', value: 'Ç' }
    ],
    nums :
    [
        { name : '_n_0', value: '0' },
        { name : '_n_1', value: '1' },
        { name : '_n_2', value: '2' },
        { name : '_n_3', value: '3' },
        { name : '_n_4', value: '4' },
        { name : '_n_5', value: '5' },
        { name : '_n_6', value: '6' },
        { name : '_n_7', value: '7' },
        { name : '_n_8', value: '8' },
        { name : '_n_9', value: '9' }
        
    ],
    special :
    [
        { name : '_bkspc_', value: '' },
        { name : '_scarmble_', value: '' },
        { name : '_toggle_', value: '' }
    ],

    getScrambled : function( alphabet )
    {
        var result = '';
        var buff = alphabet.toString();
        while ( buff.length > 0 )
        {
            pos = Math.floor( Math.random() * buff.length );
            result += buff.charAt( pos );
            buff = buff.substr( 0, pos ) + buff.substr( pos + 1 );
        }
        return result;
    },
    
    scrambleKeys : function()
    {
        var sc_a = this.getScrambled( this.isUpper ? this.alphabet_a_U : this.alphabet_a_S );
        var sc_n = this.getScrambled( this.alphabet_n );
        var i = 0;
        for ( i=0; i<this.alphas.length; i++ )
            this.alphas[i].value = sc_a.charAt(i);
        for ( i=0; i<this.nums.length; i++ )
            this.nums[i].value = sc_n.charAt(i);
        this.isScrambled = true;
    },
   
    resetKeys : function()
    {
        var sc_a = this.isUpper ? this.alphabet_a_U : this.alphabet_a_S ;
        var i = 0;
        for ( i=0; i<this.alphas.length; i++ )
            this.alphas[i].value = sc_a.charAt(i);
        for ( i=0; i<this.nums.length; i++ )
            this.nums[i].value = this.alphabet_n.charAt(i);
        this.isScrambled = false;
    },
   
    switchCase : function()
    {
        this.isUpper = ! this.isUpper;
        if ( this.isScrambled )
            this.scrambleKeys();
        else
            this.resetKeys();
    },

    assignKeys : function()
    {
        for ( i=0; i<this.alphas.length; i++ )
            $('#'+this.alphas[i].name).attr('value',this.alphas[i].value);
        for ( i=0; i<this.nums.length; i++ )
            $('#'+this.nums[i].name).attr('value',this.nums[i].value);
    },

    show : function( iid )
    {
        $('#security-keypad').show();
	this.activeInput = $('#CartNo');
        this.padInUse = false;
    },

    hide : function( force )
    {
        if ( force || !this.padInUse )
        {
            $('#security-keypad').hide();
            this.padInUse = false;
        }
    }, 

    usePad : function()
    {
        this.padInUse = true;
    },

    scrambleKeyPressed : function()
    {
        if ( this.isScrambled )
            this.resetKeys();
        else
            this.scrambleKeys();
        this.assignKeys();
        $('#_scramble_').val( this.isScrambled ? $('#_title_reorder_').val() : $('#_title_scramble_').val() );
    },

    caseKeyPressed : function()
    {
        this.switchCase();
        this.assignKeys();
        $('#_toggle_').val( this.isUpper ? $('#_title_down_').val() : $('#_title_up_').val() );
    },

    keyPressed : function( ch )
    {
		this.activeInput.attr( 'value', this.activeInput.attr('value' ) + ch );
		//document.getElementById(activeInput).value=document.getElementById(activeInput).value+ ch;
    },

    bkspcPressed : function()
    {
        this.activeInput.attr( 'value', this.activeInput.attr('value' ).substr( 0, this.activeInput.attr( 'value' ).length - 1 ) ); 
    }
}



$( function() {
        for ( i=0; i<keys.alphas.length; i++ )
            $('#'+keys.alphas[i].name).click( 
                     function(e) { keys.keyPressed( e.target.value ) }  
            );
        for ( i=0; i<keys.nums.length; i++ )
            $('#'+keys.nums[i].name).click( 
                     function(e) { keys.keyPressed( e.target.value ) }  
            );
        $('#_dash_').click(
                     function(e) { keys.keyPressed( e.target.value ) }  
        );
        $('#_bkspc_').click(
                     function(e) { keys.bkspcPressed() }  
        );

});
