function documentReady() { initJSSupport(); initDocumentInput(); initReferenceInput(); } function initJSSupport() { $('.js-support').attr('value', 'true'); } function initDocumentInput() { $('#form-submit-document').attr('disabled', 'disabled'); $('#edit-document').change(enableDocumentContinue) } function initReferenceInput() { $('#form-submit-reference').attr('disabled', 'disabled'); // enable autoadvance on the form: // focus will be transfered to the next control when the maximum number of characters are entered $('#input-reference').autoAdvance(); // check the state each time a key a pressed $('#guid-1').keyup(checkValidness); $('#guid-2').keyup(checkValidness); $('#guid-3').keyup(checkValidness); $('#guid-4').keyup(checkValidness); // do an initial check (values could be prefilled by the browser) checkValidness(); } // the divider of the checksum var divider = 1021; // the mapping of characters to their numerical value var chars = ['2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z']; var charset = new Array(); for (i = 0; i < chars.length; ++i) { charset[chars[i]] = i; } /** * Checks if the current input is a valid reference. */ function checkValidness() { var valid = 0; var guid = new Array(); guid[0] = $('#guid-1').val(); guid[1] = $('#guid-2').val(); guid[2] = $('#guid-3').val(); guid[3] = $('#guid-4').val(); if (guid[0].length == 6 && guid[1].length == 6 && guid[2].length == 6 && guid[3].length == 2) { // all fields are filled // calculate the checksum var val = 0; for (var i = 0; i < 3; ++i) { for (var j = 0; j < 6; ++j) { val = val * 32 + charset[guid[i].charAt(j)]; if (val > divider) { val = val % divider; } } } var remainder = charset[guid[3].charAt(0)] * 32 + charset[guid[3].charAt(1)]; if (val === remainder) { // the reference is valid if (valid !== 1) { $('#guid-empty').hide(); $('#guid-valid').show(); $('#guid-invalid').hide(); // enable the continue button and give it focus $('#form-submit-reference').removeAttr("disabled").focus(); valid = 1; } } else { // the reference is invalid if (valid !== -1) { $('#guid-empty').hide(); $('#guid-valid').hide(); $('#guid-invalid').show(); $('#form-submit-reference').attr('disabled', 'disabled'); valid = -1; } } } else { // the reference is not complete $('#guid-empty').show(); $('#guid-valid').hide(); $('#guid-invalid').hide(); $('#form-submit-reference').attr('disabled', 'disabled'); valid = 0; } } function enableDocumentContinue() { var file = $('#edit-document').val(); if (file !== null && file.length > 0) { $('#form-submit-document').removeAttr("disabled").focus(); } else { $('#form-submit-document').attr('disabled', 'disabled'); } }