/* trustcode.js Forms usage: onKeyPress="return TCcheckKeyPress(this, event)" onKeyUp="TCcheckUpKey(this, event, loadXMLDoc)" /> */ var TC_LENGTH = 12; var TC_CHECKSUM_METHOD = 3; var goods = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // assume upper case var oldTC = ""; function asciiValue (c) { // restrict input to a single character c = c . charAt (0); // loop through all possible ASCII values var i; for (i = 0; i < 256; ++ i) { // convert i into a 2-digit hex string var h = i . toString (16); if (h . length == 1) h = "0" + h; // insert a % character into the string h = "%" + h; // determine the character represented by the escape code h = unescape (h); // if the characters match, we've found the ASCII value if (h == c) break; } return i; } function checkDigit(s) { var sum = 0; var skip = TC_CHECKSUM_METHOD % s.length; for (var i=0; i 0){ digit = checkDigit(code); lastpos = code.length - 1; if (digit == code.substr(lastpos, 1)){ return true; } } else { return true; } return false; } // Forms handling function TCcleanCode(trustcode) { // Make upper case var cleancode = trustcode.toUpperCase(); // Strip out spaces and dashes var newValue = ""; for (var i=0; i= 12) { return false; } return true; } function TCcheckUpKey(tcElement, e, requestHandler, changeHandler) // We have to put logic in here in case the user pastes text into the tc field { tcElement.value = TCcleanCode(tcElement.value); key = getkey(e); if (tcElement.value != oldTC || key == 13) { // Code has changed oldTC = tcElement.value; if (changeHandler) { changeHandler(); } // Check length if (tcElement.value.length < TC_LENGTH) { setWarning("Please enter a valid Trustcash number"); } else if (!TCcheckCode(tcElement.value)) { setError("Trustcash number failed check"); } else { // Submit to server requestHandler(); } } } /* Don't think we need this: function checkTrustcode(tc) { var reg = /^[a-zA-Z0-9]{12}$/; var trustcode = tc; while(trustcode.indexOf(" ") > 0) { temp = trustcode.substring(0, trustcode.indexOf(" ")); trustcode = temp + trustcode.substring(trustcode.indexOf(" ") + 1, trustcode.length); } while(trustcode.indexOf("-") > 0) { temp = trustcode.substring(0, trustcode.indexOf("-")); trustcode = temp + trustcode.substring(trustcode.indexOf("-") + 1, trustcode.length); } return reg.test(trustcode); } */