// This implementation uses jQuery // My non-conflicting jQuery instance... var $raid = jQuery.noConflict(); var externalService; function formatForExternalService() { // Store all values to a map... var myMap = new Object(); // Get user's name and UCO var fullUser = $raid('#id-uzivatel').text(); var nameSurname = fullUser.match(/^[\w\s,.]+/)[0];// divUco.getElementsByTagName("b")[0].textContent; var uco = fullUser.match(/\d+/)[0]; myMap["uco"] = uco; myMap["name"] = nameSurname; //alert("User:" + nameSurname + ", uco:" + uco); // Get studies (may not be defined) //var studies = $raid('span').text(); myMap["study"] = ''; // studies; //alert("Studies:" + studies); // Walk through all input elements... var inputs = $raid(":input[name^='tst']"); // input elements starting with 'tst' in their 'name' attribute inputs.each(function() { var t = $raid(this).attr('type'); var n = $raid(this).attr('name'); if (t == "radio") { if ($raid(this).is( ":checked" )) { myMap[n] = $raid(this).parent().text(); } } else if (t == "checkbox") { if ($raid(this).is( ":checked" )) { if (n in myMap) { myMap[n] = myMap[n] + "#;#" + $raid(this).parent().text(); } else { myMap[n] = $raid(this).parent().text(); } } } else if (t == "text") { myMap[n] = $raid(this).val().replace(/(\r\n|\n|\r)/gm, " "); } else if ($raid(this).is('textarea')) { if (n != externalService.attr('name')) { myMap[n] = $raid(this).val().replace(/(\r\n|\n|\r)/gm, "#^#"); } } else if ($raid(this).is('select')) { $raid(this).contents().filter('option:selected').each(function() { if (n in myMap) { myMap[n] = myMap[n] + "#;#" + $raid(this).text(); } else { myMap[n] = $raid(this).text(); } }); } //alert('Name:' + n + ', Type:' + t + ", Value:" + $raid(this).val() + // ", Map update: " + myMap[n]); }); // Format the map to string... and store it to the correct text area var toSend = ""; for (var key in myMap) { toSend = toSend + key + ":" + myMap[key] + "\n"; } //alert("ToSend:" + toSend); externalService.attr("value", toSend); } // Register to the submit button... $raid(document).ready(function() { alert("Home assignment No.1"); // Save the e: question element... externalService = $raid("textarea[name*='_e_a_1']:last"); form = $raid("form[name='testform']"); form.submit(function(event) { // Format all student answers into the e: question formatForExternalService(); // Do regular form submit.... return; //event.preventDefault(); }); // Hide the e: question... externalService.hide(); });