﻿/*  Auto-post back
 *  Bob Davidson
 *
 *  Finds all inputs with 'autopost' class and sets them up to automatically
 *  "click" the button with 'autobutton' class, which the script then hides 
 *  from view.  If no 'autobutton' button is found, the script does not 
 *  continue.
 */

var button = null;

function is_class(obj,cn) {
 	return new RegExp('\\b'+cn+'\\b').test(obj.className);
}


function ap_find() {
	if(!document.getElementById || !document.getElementsByTagName)
		return;
	
	var hasButton = false;

	// First grab the autobutton	
	var inputs = document.getElementsByTagName('input');
	for(var x=inputs.length - 1; x>=0; x--) {
		if (is_class(inputs[x], 'autobutton')) {
			hasButton = true;
			// button is global
			button = inputs[x];
		}
	}
	
	// Don't continue if no button to click was found.
	if(!hasButton)
		return;
	
	var ap_posters=[];
	var i=0;
	
	// Grab any inputs.
	var selects = document.getElementsByTagName('select');
	for(var x=selects.length - 1; x>=0; x--) {
		if(is_class(selects[x], 'autopost')) {
			ap_posters[i] = selects[x];
			i++;
		}
	}	
	
	// If no "posters" were found to autopost to the button,
	// then don't continue.
	if(i == 0)
		return;
	
	for(var x=0; x < i; x++) {
		ap_posters[x].onchange = ap_dopost;
		button.style.display = "none";
	}
}

function ap_dopost() {
	button.click();
}

ap_find();
