// auto tabbing function
var phone_field_length = 0;

function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length = obj.value.length;
	} else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length = obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
			}
		}
	}
}

/* State AJAX */
function update_state_select(country_id, select_name, callback) {
	new Ajax.Request('/blank.smpl', {
		parameters: { arg: 'state_list', country_id: country_id },
		onSuccess: function(transport) { 
			display_states(transport, country_id, select_name,
				callback);
		}
	});
}

function display_states(transport, country_id, select_name, callback) {
	var response = transport.responseText;
	var data = eval("(" + response + ")");
	var sel = document.getElementsByName(select_name)[0];
	sel.selectedIndex = 0;
	sel.length = 1;
	for (var i = 0; i < data.length; i++) {
		sel.options[sel.length] = new Option(data[i].name, data[i].id);
	}
	var country = $('country_container');
	var state   = $('state_container');
	var zip     = $('zip_container');
	if (country_id == 3) {
		if (country) { country.style.display = ''; }
		if (state)   { state.style.display   = 'none'; }
		if (zip)     { zip.style.display     = 'none'; }
	} else {
		if (country) { country.style.display = 'none'; }
		if (state)   { state.style.display   = ''; }
		if (zip)     { zip.style.display     = ''; }
	}
	if (callback) {
		callback();
	}
}

var NS4 = (navigator.appName == "Netscape"
	&& parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue) {
	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex) { 
	var selLength = theSel.length;
	if (selLength > 0) {
		theSel.options[theIndex] = null;
	}
}

function moveOptions(theSelFrom, theSelTo) {
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
  
	var i;
		// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for (i = selLength-1; i >= 0; i--) {
		if (theSelFrom.options[i].selected) {
			selectedText[selectedCount]
				= theSelFrom.options[i].text;
			selectedValues[selectedCount]
				= theSelFrom.options[i].value;
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}
  
	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for (i = selectedCount -1; i >= 0; i--) {
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}

	if (NS4) history.go(0);
}

function select_lists(lists, append_prefix) {
	for (var i = 0; i < lists.length; i++) {
        	var list = $(lists[i]);
        	if (list) { selectAllIn(list, append_prefix); }
	} 
}

function selectAllIn(list_container, append_prefix) {
        var length = list_container.length;
        for (var x = 0; x < length; x++) {
                if (append_prefix) {
                        var prefix = "000";
                        if (x > 10) {
                                prefix = "0";
                        } else if (x < 10) {
                                prefix = "00";
                        }
                        var xplus1 = x + 1;
                        prefix = "" + prefix + "" + xplus1 + "";
                } else {
                        prefix = ""
                }
                list_container[x].value = prefix + list_container[x].value;
                list_container[x].selected = true;
        }
}

function auto_logout_warning() {
	if (confirm("Warning: If you remain idle you will automatically be"
			+ " logged out.  Click 'OK' to remain logged in.")) {
		new Ajax.Request('/blank.smpl', {
			method: 'get',
			parameters: { arg: 'blank' }
		});
		setup_auto_logout_warning();
	}
}

function setup_auto_logout_warning() {
	setTimeout("auto_logout_warning()", 1000 * 60 * 45);
}

function setup(logged) {
	if (logged) { setup_auto_logout_warning(); }
}
