User:F.trott/dynamicSFDefaults.js

function setSFDefaults {

if (document.getElementById('sfdefaults') != null) { // detect span

var divs = document.getElementById('sfdefaults').getElementsByTagName('div');

var inputs = document.getElementsByTagName('input'); var selects = document.getElementsByTagName('select'); var textareas = document.getElementsByTagName('textarea');

var dates = new Array;

for (i = 0; i < divs.length; ++i) {

if (divs[i].className.match(/sfdefault/i) != null) {

var defName = divs[i].title; var defValue = divs[i].innerHTML; var defPreserve = (divs[i].className.match(/preserve/i) != null);

for (k = 0; k < inputs.length; ++k) {

if ( inputs[k].type.toLowerCase=='radio' ) {

if (inputs[k].name == defName && inputs[k].value == defValue) // radiobutton inputs[k].checked = (inputs[k].value == defValue);

} else if ( inputs[k].type.toLowerCase=='checkbox' ) {

if ((inputs[k].name.indexOf( defName ) == 0 && inputs[k].value == defValue) || // checkboxes                (inputs[k].name == defName + "[value]") ||                                  // checkbox                (inputs[k].name == defName) )                        // std:minor, std:watch inputs[k].checked = true;

} else if ( inputs[k].name == defName ) {

var isPartOfDate = (inputs[k].name.match(/.+\[.+\]\[(year|hour|minute|second|timezone)\]/) != null) var isDateToSet = false;

if (isPartOfDate) { var toFind = inputs[k].name.replace(/(.+\[.+\])\[(year|hour|minute|second|timezone)\]/,'$1'); for (l=0; l < dates.length; ++l) isDateToSet = (isDateToSet || (dates[l] == toFind)); //alert(inputs[k].name + " is part of date: " + isDateToSet + "; to find: " + toFind); }

if ( (isPartOfDate && isDateToSet) || (!isPartOfDate && (inputs[k].value == "" || !defPreserve))) {

inputs[k].value = defValue;

if (inputs[k].name.match(/.+\[.+\]\[day\]$/)) { dates.push(inputs[k].name.replace(/(.+\[.+\])\[day\]$/, '$1')); //alert("Pushed " + inputs[k].name.replace(/(.+\[.+\])\[day\]$/, '$1')); }           }

}

}

for (k = 0; k < selects.length; ++k) {

if ( selects[k].name == defName ) {   // text w/ values var isPartOfDate = (selects[k].name.match(/.+\[.+\]\[(month|ampm24h)\]/) != null) var isDateToSet = false;

if (isPartOfDate) { var toFind = selects[k].name.replace(/(.+\[.+\])\[(month|ampm24h)\]/,'$1') for (l=0; l < dates.length; ++l) isDateToSet = (isDateToSet || (dates[l] == toFind)); //alert(selects[k].name + " is part of date: " + isDateToSet); }

var defSelected = false; defSelected = false;

if (!isDateToSet && defPreserve ) { var options = selects[k].getElementsByTagName('option'); for (l = 0; ((l < options.length) && !defSelected); ++l) defSelected = (defSelected || options[l].defaultSelected); }

if ((isPartOfDate && isDateToSet) || (!isPartOfDate && !defSelected)) for (l = 0; l < selects[k].options.length; ++l) if (selects[k].options[l].value==defValue) selects[k].options[l].selected = true;

} else if (selects[k].name == defName + "[]" && selects[k].multiple) { // listbox

for (l = 0; l < selects[k].options.length; ++l) if (selects[k].options[l].value==defValue) selects[k].options[l].selected = true;

}       }

for (k = 0; k < textareas.length; ++k) if ( textareas[k].name==defName && ( textareas[k].innerHTML == "" || !defPreserve) ) // textarea, std:free text textareas[k].innerHTML=defValue;

}   }  } }

if (wgPageName == 'Special:AddData') addOnloadHook(setSFDefaults);