I am building a form with multiple select option fields. I am trying to only show the next field if it exists as a combination in the CSV file I am reading in. It's working fine right now and I am using onchange for each select field to call the next function.
Is there a way to reduce to the code and make it more efficient?
var lines = [];
var datas = [];
$(document).ready(function() {
$.ajax({
type: "GET",
url: "data.txt",
dataType: "text",
success: function(data) {
processData(data);
}
});
});
function processData(allText) {
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
for (var i = 1; i < allTextLines.length; i++) {
var data = allTextLines[i].split(',');
if (data.length == headers.length) {
var tarr = [];
for (var j = 0; j < headers.length; j++) {
tarr.push(data[j]);
}
lines.push(tarr);
}
}
datas = lines;
regulatory = []
for (var i = 0; i < datas.length; i++) {
if ($.inArray(datas[i][0], regulatory) == -1) {
regulatory.push(datas[i][0]);
}
}
for (var i = 0; i < regulatory.length; i++) {
addOption(document.drop_list.Regulatory, regulatory[i], regulatory[i]);
}
}
function SelectDMZ() {
removeAllOptions(document.drop_list.DMZ);
addOption(document.drop_list.DMZ, "Select!", "Select!");
for (var i = 0; i < datas.length; i++) {
if (document.drop_list.Regulatory.value == datas[i][0]) {
if ($("#DMZ option:contains('" + datas[i][1] + "')").length == 0) {
addOption(document.drop_list.DMZ, datas[i][1], datas[i][1]);
}
}
}
}
function SelectCoS() {
removeAllOptions(document.drop_list.CoS);
addOption(document.drop_list.CoS, "Select!", "Select!");
for (var i = 0; i < datas.length; i++) {
if (document.drop_list.Regulatory.value == datas[i][0] & document.drop_list.DMZ.value == datas[i][1]) {
if ($("#CoS option:contains('" + datas[i][2] + "')").length == 0) {
addOption(document.drop_list.CoS, datas[i][2], datas[i][2]);
}
}
}
}
function SelectExclusive() {
removeAllOptions(document.drop_list.Exclusive);
addOption(document.drop_list.Exclusive, "Select!", "Select!");
for (var i = 0; i < datas.length; i++) {
if (document.drop_list.Regulatory.value == datas[i][0] & document.drop_list.DMZ.value == datas[i][1] & document.drop_list.CoS.value == datas[i][2]) {
if ($("#Exclusive option:contains('" + datas[i][3] + "')").length == 0) {
addOption(document.drop_list.Exclusive, datas[i][3], datas[i][3]);
}
}
}
}
function SelectNetwork() {
removeAllOptions(document.drop_list.Network);
addOption(document.drop_list.Network, "Select!", "Select!");
for (var i = 0; i < datas.length; i++) {
if (document.drop_list.Regulatory.value == datas[i][0] & document.drop_list.DMZ.value == datas[i][1] & document.drop_list.CoS.value == datas[i][2] & document.drop_list.Exclusive.value == datas[i][3]) {
if ($("#Network option:contains('" + datas[i][4] + "')").length == 0) {
addOption(document.drop_list.Network, datas[i][4], datas[i][4]);
}
}
}
}
function SelectCommunity() {
removeAllOptions(document.drop_list.Community);
addOption(document.drop_list.Community, "Select!", "Select!");
for (var i = 0; i < datas.length; i++) {
if (document.drop_list.Regulatory.value == datas[i][0] & document.drop_list.DMZ.value == datas[i][1] & document.drop_list.CoS.value == datas[i][2] & document.drop_list.Exclusive.value == datas[i][3] & document.drop_list.Network.value == datas[i][4]) {
if ($("#Community option:contains('" + datas[i][5] + "')").length == 0) {
addOption(document.drop_list.Community, datas[i][5], datas[i][5]);
}
}
}
}
function check_select() {
if (document.drop_list.Platform.value == "Select!"){
alert("Please select a platform!");
return false;
}
}
function removeAllOptions(selectbox) {
var i;
for (i = selectbox.options.length - 1; i >= 0; i--) {
//selectbox.options.remove(i);
selectbox.remove(i);
}
}
function addOption(selectbox, value, text) {
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}