1 //-----------------------------------------------
2 // Barcode Magic JavaScript
3 // Authors: Brandon W. King
5 //-----------------------------------------------
7 //---------------------------------------
8 // BCMagic Core Processing AJAX Callback
9 //---------------------------------------
10 var bcmagic_process_callback = function(data, textStatus) {
11 if (textStatus != 'success')
13 bcmagic_message('AJAX Status: '+textStatus);
21 if (data['mode'] == 'clear')
23 bcmagic_status('','');
25 else if (data['mode'] == 'redirect')
29 bcmagic_redirect(data['url']);
33 bcmagic_status('Error', 'No redirect URL provided by server');
36 else if (data['mode'] == 'autofill')
38 bcmagic_autofill(data['field'], data['value'])
41 bcmagic_message('Message recieved!');
42 bcmagic_status(data['mode'], data['status']);
48 bcmagic_message(data['msg']);
53 var bcmagic_callback = function(data, textStatus)
55 if (textStatus != 'success')
56 bcmagic_message('Failed!');
58 bcmagic_message('Success!');
61 var bcmagic_process = function(){
62 var magic = $("#id_magic");
63 var text = magic.attr('value');
64 magic.attr('value', '');
66 var bcm_mode = $("#id_bcm_mode");
67 var mode = bcm_mode.attr('value');
69 // Show what we have captured
70 bcmagic_message('Sent command to server');
71 $.post('/bcmagic/magic/', {'text': text, 'bcm_mode': mode}, bcmagic_process_callback, 'json');
74 var bcmagic_keyhandler = function(e) {
75 //Process upon enter key as input.
80 //---------------------------------------
82 //---------------------------------------
83 var bcmagic_message = function(text)
86 $("#bcm_msg").html(text);
88 // clear message after 3000ms
89 setTimeout(function() {
90 $("#bcm_msg").html('');
94 var bcmagic_status = function(state, text)
96 var msg = $('#bcm_status');
97 if (state.length > 0 || text.length > 0)
98 msg.html('<b>'+state+':</b> '+text);
104 var bcmagic_redirect = function(url)
106 bcmagic_message('Redirecting to:' + url);
107 window.location = url;
110 var bcmagic_autofill = function(field, val)
112 var txtbox = $('#'+field);
113 txtbox.attr('value', val);
115 var input_fields = $('form input').not(':hidden').not('[type=submit]');
117 // Count the number of text fields which have been filled in.
119 input_fields.each( function(){
120 if(this.value.length > 0){
125 // If the number of text fields filled in is equal to the number of input_fields in the form, submit the form!
126 if (count == input_fields.length)
128 bcmagic_status('Form Full', 'Form is now full and ready to process');
136 bcmagic_status('Form Fill Count', 'Count(' + count +') - Total(' + input_fields.length + ')');
140 //---------------------------------------
141 // Main Ready Function
142 //---------------------------------------
143 /*$(document).ready(function() {
145 // Grab initial focus on magic text input
146 $("#id_magic").focus();
148 // Set some initial text
149 //$("#id_magic").attr('value','Moo cow!');
151 // Trigger when enterkey is pressed
152 $("#id_magic").keypress(bcmagic_keyhandler)