//location of a text file in the following format //fieldname:Field Label:Field Value var listdata = '/dada_mail_support_files/data/selectdata.txt'; var debug = 0; //put here for debugging function debugArray( testArray, debugTag ) { if (debug == 0) return; for (key in testArray) { alert(debugTag + ": key " + key + " has value " + testArray[key]); } } function debugAlert( msg ) { if (debug == 0) return; alert( msg ); } // this function is used to replace the profile text field with // a profile select field after the form is rendered function createSelectField( oldObject, options, fieldName ) { // if this is a text field, we convert it to a select field if ( oldObject.type == 'text' ) { // create select field var newObject = document.createElement('select'); // set name/id of the select field to that of the old text field if(oldObject.name) newObject.name = oldObject.name; if(oldObject.id) newObject.id = oldObject.id; //replace the old text field with the new select field oldObject.parentNode.replaceChild(newObject,oldObject); } else { var newObject = oldObject; } // add an empty option to the top to facilitate validation if required. var myDefaultOption = document.createElement("option"); myDefaultOption.text = ""; myDefaultOption.value = ""; newObject.appendChild(myDefaultOption); // if this select already has a value filled in, we want to store it for later // so we can mark it as selected. var preFilled = ""; if ( oldObject.value.length > 0 ) { preFilled = oldObject.value; } // now populate the slect options from the data file for (var z=0;z 1 ) { var optionValue = options[z].split(':'); if ( optionValue[0] == fieldName ) { var myOption = document.createElement("option"); myOption.text = optionValue[1]; // remove all the newline characters. optionValue[2] = optionValue[2].replace(/(\r\n|\n|\r)/gm,""); myOption.value = optionValue[2]; // check to see if this option was previously selected and saved if ( optionValue[2] === preFilled ) { myOption.selected = true; } newObject.appendChild(myOption); } } } } function uniqueValues( options ) { var uniqueFieldValues = []; for (var i=0;i 1 ) { var optionValue = options[i].split(':'); uniqueFieldValues[optionValue[0]]=1; } } return uniqueFieldValues; } function checkForField( uniqueFieldValues, formObj, options ) { for (key in uniqueFieldValues) { // user templatre fields var formElementObj = formObj[key]; if ( formElementObj ) { debugAlert("Form element found with name " + key); createSelectField(formElementObj, options, key); //var currentValue = formElementObj.value; } //check for admin template fields formElementObj = formObj['field_value_' + key]; if ( formElementObj ) { debugAlert("Admin Form element found with name " + key); createSelectField(formElementObj, options, key); } } } //get all the forms on this page var forms = document.forms; debugAlert("Custom Select: Checking for forms"); if ( forms.length > 0 ) { // there are forms on the page. So now we pull the data file $(document).ready(function() { $.get(listdata, function(data) { // pull the text file and create an sorted array of options var options = data.split('\n'); options.sort(); // now loop through the options and create an lookup array for quicker access var uniqueFieldsArray = uniqueValues( options ); debugArray( uniqueFieldsArray, 'uniqueFieldsArray' ); // now loop through forms and see if they contain the fields we want to change for(var x=0;x