/* * File: formtest.js * Version: 1 (see Version History below) * Author: Russell Robinson, http://www.tectite.com/ * Created: 8 November 2010 * * Copyright (c) 2010 Root Software and Open Concepts (Vic) Pty Ltd * (ABN 12 130 429 248), Melbourne, Australia. * This script is free for all use as described in the "Copying and Use" and * "Warranty and Disclaimer" sections below. * * Description * ~~~~~~~~~~~ * This JavaScript script provides a simple way to fill a * form with test data. * It can be used with any HTML form and doesn't affect normal (production) * operation of the form. * In particular, it can be utilized by forms created by the Tectite * Form Designer, available at http://www.tectite.com/ * Visit us for support and updates. * Copying and Use (Software License) * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * This script is provided free of charge and may be freely distributed * and used provided that you: * 1. keep this header, including copyright and comments, * in place and unmodified; and, * 2. do not charge a fee for distributing it, without an agreement * in writing with Root Software allowing you to do so; and, * 3. if you modify this file before distributing it, you clearly * identify: * a) who you are * b) how to contact you * c) what changes you have made * d) why you have made those changes. * * By using any of our products, including this script, you are * agreeing to our standard Terms and Conditions, available here: * http://www.tectite.com/TermsAndConditions.pdf * * This is free software and the Software License shown above * is to be read in conjunction with our standard Terms and Conditions. * * Warranty and Disclaimer * ~~~~~~~~~~~~~~~~~~~~~~~ * This script is provided free-of-charge and with ABSOLUTELY NO WARRANTY. * It has not been verified for use in critical applications, including, * but not limited to, medicine, defense, aircraft, space exploration, * or any other potentially dangerous activity. * * By using this file you agree to indemnify Root Software and * Open Concepts (Vic) Pty Ltd, their agents, employees, directors and * associated companies and businesses from any liability whatsoever. * * We still care * ~~~~~~~~~~~~~ * If you find a bug or fault in this file, please report it to us. * We will respond to your report and make endeavours to rectify any * faults you've detected as soon as possible. * * To contact us please register on our forums at: * http://www.tectite.com/vbforums/ * or view our contact information: * http://www.tectite.com/contacts.php * * Version History * ~~~~~~~~~~~~~~~ * * Version 1, 8 November 2010 * Initial working version. */ function TectiteTestValue(elem,a_test_data) { var s_name = elem.getAttribute("name"); if (s_name) { var s_test_value = ""; var b_got_test = false; for (var ii = 0 ; ii < a_test_data.length ; ii++) { if (a_test_data[ii].substr(0,s_name.length) == s_name && a_test_data[ii].substr(s_name.length,1) == "=") { s_test_value = a_test_data[ii].substr(s_name.length+1); b_got_test = true; break; } } switch (elem.tagName.toLowerCase()) { case "input": switch (elem.getAttribute("type").toLowerCase()) { case "": case "text": if (elem.value == "") elem.value = b_got_test ? s_test_value : s_name; break; case "radio": /* * if no value is specified, then all * radio buttons will be checked, which results * in only the last one being checked */ if (!b_got_test || elem.value == s_test_value) elem.checked = true; break; case "checkbox": elem.checked = (b_got_test && parseInt(s_test_value) == 0) ? false : true; break; } break; case "textarea": if (elem.value == "") elem.value = b_got_test ? s_test_value : s_name; break; case "select": var b_got_selected = false; for (var i_opt = 0 ; i_opt < elem.options.length-2 ; i_opt++) if (elem.options[i_opt].selected) if (elem.options[i_opt].value != "") b_got_selected = true; if (b_got_selected) break; /* * if no value is specified, then select the last option */ if (!b_got_test) { for (var i_opt = 0 ; i_opt < elem.options.length-2 ; i_opt++) elem.options[i_opt].selected = false; elem.options[elem.options.length-1].selected = true; } else { for (var i_opt = 0 ; i_opt < elem.options.length ; i_opt++) { if (elem.options[i_opt].value == s_test_value) { elem.options[i_opt].selected = true; break; } } } break; default: /*alert(elem.getAttribute("name") + " is a " + elem.tagName);*/ break; } } } function TectiteLoadTestValues(s_form) { for (var i_form = 0 ; i_form < document.forms.length ; i_form++) if (document.forms[i_form].name == s_form) { var form = document.forms[i_form]; var a_test_data = []; if (form.testdata) a_test_data = form.testdata.value.split(/[ \t\r\n\f]*,[ \t\r\n\f]*/); for (var i_elem = 0 ; i_elem < form.elements.length ; i_elem++) TectiteTestValue(form.elements[i_elem],a_test_data); /* * append 'formtest=1' to the form's action attribute, so we * propagate the testing through to any following form pages */ var s_action = form.action; if (s_action.search(/\?/) == -1) form.action = s_action + "?formtest=1"; else form.action = s_action + "&formtest=1"; } } function TectiteMakeTestButton(s_form) { var s_qry = location.search.substring(1); // Get query string if (s_qry.search(/formtest=1/) != -1) { document.writeln('
'); } }